2 Copyright (C) 2008 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "evoral/midi_events.h"
21 #include "ardour/midi_track.h"
23 #include "gtkmm2ext/keyboard.h"
26 #include "piano_roll_header.h"
27 #include "midi_time_axis.h"
28 #include "midi_streamview.h"
29 #include "public_editor.h"
31 const int no_note = 0xff;
34 using namespace Gtkmm2ext;
36 PianoRollHeader::Color PianoRollHeader::white = PianoRollHeader::Color(0.77f, 0.78f, 0.76f);
37 PianoRollHeader::Color PianoRollHeader::white_highlight = PianoRollHeader::Color(0.87f, 0.88f, 0.86f);
38 PianoRollHeader::Color PianoRollHeader::white_shade_light = PianoRollHeader::Color(0.95f, 0.95f, 0.95f);
39 PianoRollHeader::Color PianoRollHeader::white_shade_dark = PianoRollHeader::Color(0.56f, 0.56f, 0.56f);
41 PianoRollHeader::Color PianoRollHeader::black = PianoRollHeader::Color(0.24f, 0.24f, 0.24f);
42 PianoRollHeader::Color PianoRollHeader::black_highlight = PianoRollHeader::Color(0.30f, 0.30f, 0.30f);
43 PianoRollHeader::Color PianoRollHeader::black_shade_light = PianoRollHeader::Color(0.46f, 0.46f, 0.46f);
44 PianoRollHeader::Color PianoRollHeader::black_shade_dark = PianoRollHeader::Color(0.1f, 0.1f, 0.1f);
46 PianoRollHeader::Color::Color()
53 PianoRollHeader::Color::Color(double _r, double _g, double _b)
61 PianoRollHeader::Color::set(const PianoRollHeader::Color& c)
68 PianoRollHeader::PianoRollHeader(MidiStreamView& v)
70 , _highlighted_note(no_note)
71 , _clicked_note(no_note)
74 add_events (Gdk::BUTTON_PRESS_MASK |
75 Gdk::BUTTON_RELEASE_MASK |
76 Gdk::POINTER_MOTION_MASK |
77 Gdk::ENTER_NOTIFY_MASK |
78 Gdk::LEAVE_NOTIFY_MASK |
81 for (int i = 0; i < 128; ++i) {
82 _active_notes[i] = false;
85 _view.NoteRangeChanged.connect (sigc::mem_fun (*this, &PianoRollHeader::note_range_changed));
89 create_path(Cairo::RefPtr<Cairo::Context> cr, double x[], double y[], int start, int stop)
91 cr->move_to(x[start], y[start]);
93 for (int i = start+1; i <= stop; ++i) {
94 cr->line_to(x[i], y[i]);
99 render_rect(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
100 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
102 cr->set_source_rgb(bg.r, bg.g, bg.b);
103 create_path(cr, x, y, 0, 4);
106 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
107 create_path(cr, x, y, 0, 2);
110 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
111 create_path(cr, x, y, 2, 4);
116 render_cf(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
117 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
119 cr->set_source_rgb(bg.r, bg.g, bg.b);
120 create_path(cr, x, y, 0, 6);
123 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
124 create_path(cr, x, y, 0, 4);
127 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
128 create_path(cr, x, y, 4, 6);
133 render_eb(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
134 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
136 cr->set_source_rgb(bg.r, bg.g, bg.b);
137 create_path(cr, x, y, 0, 6);
140 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
141 create_path(cr, x, y, 0, 2);
143 create_path(cr, x, y, 4, 5);
146 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
147 create_path(cr, x, y, 2, 4);
149 create_path(cr, x, y, 5, 6);
154 render_dga(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
155 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
157 cr->set_source_rgb(bg.r, bg.g, bg.b);
158 create_path(cr, x, y, 0, 8);
161 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
162 create_path(cr, x, y, 0, 4);
164 create_path(cr, x, y, 6, 7);
167 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
168 create_path(cr, x, y, 4, 6);
170 create_path(cr, x, y, 7, 8);
175 PianoRollHeader::get_path(PianoRollHeader::ItemType note_type, int note, double x[], double y[])
177 double y_pos = floor(_view.note_to_y(note)) - 0.5f;
179 double other_y1 = floor(_view.note_to_y(note+1)) + floor(_note_height / 2.0f) + 0.5f;
180 double other_y2 = floor(_view.note_to_y(note-1)) + floor(_note_height / 2.0f) - 1.0f;
181 double width = get_width();
184 note_height = floor(_view.contents_height()) - y_pos;
186 note_height = floor(_view.note_to_y(note - 1)) - y_pos;
190 case BLACK_SEPARATOR:
193 x[1] = _black_note_width;
196 case BLACK_MIDDLE_SEPARATOR:
197 x[0] = _black_note_width;
198 y[0] = y_pos + floor(_note_height / 2.0f);
204 y[0] = y_pos + note_height - 0.5f;
207 x[2] = _black_note_width;
209 x[3] = _black_note_width;
210 y[3] = y_pos + note_height - 0.5f;
212 y[4] = y_pos + note_height - 0.5f;
214 case WHITE_SEPARATOR:
222 y[0] = y_pos + note_height - 0.5f;
228 y[3] = y_pos + note_height - 0.5f;
230 y[4] = y_pos + note_height - 0.5f;
234 y[0] = y_pos + note_height - 1.5f;
237 x[2] = _black_note_width + 1.0f;
239 x[3] = _black_note_width + 1.0f;
244 y[5] = y_pos + note_height - 1.5f;
246 y[6] = y_pos + note_height - 1.5f;
250 y[0] = y_pos + note_height - 1.5f;
257 x[4] = _black_note_width + 1.0f;
259 x[5] = _black_note_width + 1.0f;
260 y[5] = y_pos + note_height - 1.5f;
262 y[6] = y_pos + note_height - 1.5f;
266 y[0] = y_pos + note_height - 1.5f;
269 x[2] = _black_note_width + 1.0f;
271 x[3] = _black_note_width + 1.0f;
277 x[6] = _black_note_width + 1.0f;
279 x[7] = _black_note_width + 1.0f;
280 y[7] = y_pos + note_height - 1.5f;
282 y[8] = y_pos + note_height - 1.5f;
290 PianoRollHeader::on_expose_event (GdkEventExpose* ev)
292 GdkRectangle& rect = ev->area;
295 Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context();
296 Cairo::RefPtr<Cairo::LinearGradient> pat = Cairo::LinearGradient::create(0, 0, _black_note_width, 0);
299 Color bg, tl_shadow, br_shadow;
301 int y1 = max(rect.y, 0);
302 int y2 = min(rect.y + rect.height, (int) floor(_view.contents_height() - 1.0f));
304 //Cairo::TextExtents te;
305 lowest = max(_view.lowest_note(), _view.y_to_note(y2));
306 highest = min(_view.highest_note(), _view.y_to_note(y1));
312 cr->select_font_face ("Georgia", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD);
313 font_size = min((double) 10.0f, _note_height - 4.0f);
314 cr->set_font_size(font_size);
316 /* fill the entire rect with the color for non-highlighted white notes.
317 * then we won't have to draw the background for those notes,
318 * and would only have to draw the background for the one highlighted white note*/
319 //cr->rectangle(rect.x, rect.y, rect.width, rect.height);
320 //cr->set_source_rgb(white.r, white.g, white.b);
323 cr->set_line_width(1.0f);
325 /* draw vertical lines with shade at both ends of the widget */
326 cr->set_source_rgb(0.0f, 0.0f, 0.0f);
327 cr->move_to(0.5f, rect.y);
328 cr->line_to(0.5f, rect.y + rect.height);
330 cr->move_to(get_width() - 0.5f, rect.y);
331 cr->line_to(get_width() - 0.5f, rect.y + rect.height);
334 //pat->add_color_stop_rgb(0.0, 0.33, 0.33, 0.33);
335 //pat->add_color_stop_rgb(0.2, 0.39, 0.39, 0.39);
336 //pat->add_color_stop_rgb(1.0, 0.22, 0.22, 0.22);
337 //cr->set_source(pat);
339 for (int i = lowest; i <= highest; ++i) {
349 if (i == _highlighted_note) {
350 bg.set(black_highlight);
355 if (_active_notes[i]) {
356 tl_shadow.set(black_shade_dark);
357 br_shadow.set(black_shade_light);
359 tl_shadow.set(black_shade_light);
360 br_shadow.set(black_shade_dark);
363 /* draw black separators */
364 cr->set_source_rgb(0.0f, 0.0f, 0.0f);
365 get_path(BLACK_SEPARATOR, i, x, y);
366 create_path(cr, x, y, 0, 1);
369 get_path(BLACK_MIDDLE_SEPARATOR, i, x, y);
370 create_path(cr, x, y, 0, 1);
373 get_path(BLACK, i, x, y);
374 render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
379 if (i == _highlighted_note) {
380 bg.set(white_highlight);
385 if (_active_notes[i]) {
386 tl_shadow.set(white_shade_dark);
387 br_shadow.set(white_shade_light);
389 tl_shadow.set(white_shade_light);
390 br_shadow.set(white_shade_dark);
396 if (i == _view.highest_note()) {
397 get_path(WHITE_RECT, i, x, y);
398 render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
400 get_path(WHITE_CF, i, x, y);
401 render_cf(cr, i, x, y, bg, tl_shadow, br_shadow);
408 if (i == _view.highest_note()) {
409 get_path(WHITE_EB, i, x, y);
410 render_eb(cr, i, x, y, bg, tl_shadow, br_shadow);
411 } else if (i == _view.lowest_note()) {
412 get_path(WHITE_CF, i, x, y);
413 render_cf(cr, i, x, y, bg, tl_shadow, br_shadow);
415 get_path(WHITE_DGA, i, x, y);
416 render_dga(cr, i, x, y, bg, tl_shadow, br_shadow);
422 cr->set_source_rgb(0.0f, 0.0f, 0.0f);
423 get_path(WHITE_SEPARATOR, i, x, y);
424 create_path(cr, x, y, 0, 1);
427 if (i == _view.lowest_note()) {
428 get_path(WHITE_RECT, i, x, y);
429 render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
431 get_path(WHITE_EB, i, x, y);
432 render_eb(cr, i, x, y, bg, tl_shadow, br_shadow);
444 /* render the name of which C this is */
447 double y = floor(_view.note_to_y(i)) - 0.5f;
448 double note_height = floor(_view.note_to_y(i - 1)) - y;
453 //cr->get_text_extents(s.str(), te);
454 cr->set_source_rgb(0.30f, 0.30f, 0.30f);
455 cr->move_to(2.0f, y + note_height - 1.0f - (note_height - font_size) / 2.0f);
456 cr->show_text(s.str());
464 PianoRollHeader::on_motion_notify_event (GdkEventMotion* ev)
468 int note = _view.y_to_note(ev->y);
470 if (editor().current_mouse_mode() == Editing::MouseRange) {
472 /* select note range */
474 if (Keyboard::no_modifiers_active (ev->state)) {
475 AddNoteSelection (note); // EMIT SIGNAL
482 if (_highlighted_note != no_note) {
483 if (note > _highlighted_note) {
484 invalidate_note_range(_highlighted_note, note);
486 invalidate_note_range(note, _highlighted_note);
489 _highlighted_note = note;
492 /* redraw already taken care of above */
493 if (_clicked_note != no_note && _clicked_note != note) {
494 _active_notes[_clicked_note] = false;
495 send_note_off(_clicked_note);
497 _clicked_note = note;
499 if (!_active_notes[note]) {
500 _active_notes[note] = true;
507 //win->process_updates(false);
513 PianoRollHeader::on_button_press_event (GdkEventButton* ev)
515 int note = _view.y_to_note(ev->y);
517 if (ev->button == 2 && ev->type == GDK_BUTTON_PRESS) {
518 if (Keyboard::no_modifiers_active (ev->state)) {
519 SetNoteSelection (note); // EMIT SIGNAL
525 if (ev->button == 1 && ev->type == GDK_BUTTON_PRESS && note >= 0 && note < 128) {
530 if (!_active_notes[note]) {
531 _active_notes[note] = true;
532 _clicked_note = note;
535 invalidate_note_range(note, note);
537 reset_clicked_note(note);
545 PianoRollHeader::on_button_release_event (GdkEventButton* ev)
547 int note = _view.y_to_note(ev->y);
549 if (editor().current_mouse_mode() == Editing::MouseRange) {
551 if (Keyboard::no_modifiers_active (ev->state)) {
552 AddNoteSelection (note); // EMIT SIGNAL
553 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
554 ToggleNoteSelection (note); // EMIT SIGNAL
555 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::RangeSelectModifier)) {
556 ExtendNoteSelection (note); // EMIT SIGNAL
564 if (note == _clicked_note) {
565 reset_clicked_note(note);
575 PianoRollHeader::on_enter_notify_event (GdkEventCrossing* ev)
577 _highlighted_note = _view.y_to_note(ev->y);
579 invalidate_note_range(_highlighted_note, _highlighted_note);
584 PianoRollHeader::on_leave_notify_event (GdkEventCrossing*)
586 invalidate_note_range(_highlighted_note, _highlighted_note);
588 if (_clicked_note != no_note) {
589 reset_clicked_note(_clicked_note, _clicked_note != _highlighted_note);
592 _highlighted_note = no_note;
597 PianoRollHeader::on_scroll_event (GdkEventScroll*)
603 PianoRollHeader::note_range_changed()
605 _note_height = floor(_view.note_height()) + 0.5f;
610 PianoRollHeader::invalidate_note_range(int lowest, int highest)
612 Glib::RefPtr<Gdk::Window> win = get_window();
615 // the non-rectangular geometry of some of the notes requires more
616 // redraws than the notes that actually changed.
617 switch(lowest % 12) {
620 lowest = max((int) _view.lowest_note(), lowest);
623 lowest = max((int) _view.lowest_note(), lowest - 1);
627 switch(highest % 12) {
630 highest = min((int) _view.highest_note(), highest);
637 highest = min((int) _view.highest_note(), highest + 1);
640 highest = min((int) _view.highest_note(), highest + 2);
644 double y = _view.note_to_y(highest);
645 double height = _view.note_to_y(lowest - 1) - y;
648 rect.set_width(get_width());
649 rect.set_y((int) floor(y));
650 rect.set_height((int) floor(height));
653 win->invalidate_rect(rect, false);
658 PianoRollHeader::on_size_request(Gtk::Requisition* r)
664 PianoRollHeader::on_size_allocate(Gtk::Allocation& a)
666 DrawingArea::on_size_allocate(a);
668 _black_note_width = floor(0.7 * get_width()) + 0.5f;
672 PianoRollHeader::send_note_on(uint8_t note)
674 boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
675 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*> (&_view.trackview ());
677 //cerr << "note on: " << (int) note << endl;
680 _event[0] = (MIDI_CMD_NOTE_ON | mtv->get_channel_for_add ());
684 track->write_immediate_event(3, _event);
689 PianoRollHeader::send_note_off(uint8_t note)
691 boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
692 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*> (&_view.trackview ());
695 _event[0] = (MIDI_CMD_NOTE_OFF | mtv->get_channel_for_add ());
699 track->write_immediate_event(3, _event);
704 PianoRollHeader::reset_clicked_note (uint8_t note, bool invalidate)
706 _active_notes[note] = false;
707 _clicked_note = no_note;
708 send_note_off (note);
710 invalidate_note_range (note, note);
715 PianoRollHeader::editor() const
717 return _view.trackview().editor();