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"
32 using namespace Gtkmm2ext;
34 PianoRollHeader::Color PianoRollHeader::white = PianoRollHeader::Color(0.77f, 0.78f, 0.76f);
35 PianoRollHeader::Color PianoRollHeader::white_highlight = PianoRollHeader::Color(1.00f, 0.40f, 0.40f);
36 PianoRollHeader::Color PianoRollHeader::white_shade_light = PianoRollHeader::Color(0.95f, 0.95f, 0.95f);
37 PianoRollHeader::Color PianoRollHeader::white_shade_dark = PianoRollHeader::Color(0.56f, 0.56f, 0.56f);
39 PianoRollHeader::Color PianoRollHeader::black = PianoRollHeader::Color(0.24f, 0.24f, 0.24f);
40 PianoRollHeader::Color PianoRollHeader::black_highlight = PianoRollHeader::Color(0.60f, 0.10f, 0.10f);
41 PianoRollHeader::Color PianoRollHeader::black_shade_light = PianoRollHeader::Color(0.46f, 0.46f, 0.46f);
42 PianoRollHeader::Color PianoRollHeader::black_shade_dark = PianoRollHeader::Color(0.1f, 0.1f, 0.1f);
44 PianoRollHeader::Color::Color()
51 PianoRollHeader::Color::Color(double _r, double _g, double _b)
59 PianoRollHeader::Color::set(const PianoRollHeader::Color& c)
66 PianoRollHeader::PianoRollHeader(MidiStreamView& v)
68 , _highlighted_note(NO_MIDI_NOTE)
69 , _clicked_note(NO_MIDI_NOTE)
72 add_events (Gdk::BUTTON_PRESS_MASK |
73 Gdk::BUTTON_RELEASE_MASK |
74 Gdk::POINTER_MOTION_MASK |
75 Gdk::ENTER_NOTIFY_MASK |
76 Gdk::LEAVE_NOTIFY_MASK |
79 for (int i = 0; i < 128; ++i) {
80 _active_notes[i] = false;
83 _view.NoteRangeChanged.connect (sigc::mem_fun (*this, &PianoRollHeader::note_range_changed));
87 create_path(Cairo::RefPtr<Cairo::Context> cr, double x[], double y[], int start, int stop)
89 cr->move_to(x[start], y[start]);
91 for (int i = start+1; i <= stop; ++i) {
92 cr->line_to(x[i], y[i]);
97 render_rect(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
98 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
100 cr->set_source_rgb(bg.r, bg.g, bg.b);
101 create_path(cr, x, y, 0, 4);
104 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
105 create_path(cr, x, y, 0, 2);
108 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
109 create_path(cr, x, y, 2, 4);
114 render_cf(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
115 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
117 cr->set_source_rgb(bg.r, bg.g, bg.b);
118 create_path(cr, x, y, 0, 6);
121 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
122 create_path(cr, x, y, 0, 4);
125 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
126 create_path(cr, x, y, 4, 6);
131 render_eb(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
132 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
134 cr->set_source_rgb(bg.r, bg.g, bg.b);
135 create_path(cr, x, y, 0, 6);
138 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
139 create_path(cr, x, y, 0, 2);
141 create_path(cr, x, y, 4, 5);
144 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
145 create_path(cr, x, y, 2, 4);
147 create_path(cr, x, y, 5, 6);
152 render_dga(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
153 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
155 cr->set_source_rgb(bg.r, bg.g, bg.b);
156 create_path(cr, x, y, 0, 8);
159 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
160 create_path(cr, x, y, 0, 4);
162 create_path(cr, x, y, 6, 7);
165 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
166 create_path(cr, x, y, 4, 6);
168 create_path(cr, x, y, 7, 8);
173 PianoRollHeader::get_path(PianoRollHeader::ItemType note_type, int note, double x[], double y[])
175 double y_pos = floor(_view.note_to_y(note)) + 1.5f;
177 double other_y1 = floor(_view.note_to_y(note+1)) + floor(_note_height / 2.0f) + 2.5f;
178 double other_y2 = floor(_view.note_to_y(note-1)) + floor(_note_height / 2.0f) + 1.0f;
179 double width = get_width();
182 note_height = floor(_view.contents_height()) - y_pos + 2.;
184 note_height = floor(_view.note_to_y(note - 1)) - y_pos + 2.;
188 case BLACK_SEPARATOR:
191 x[1] = _black_note_width;
194 case BLACK_MIDDLE_SEPARATOR:
195 x[0] = _black_note_width;
196 y[0] = y_pos + floor(_note_height / 2.0f);
202 y[0] = y_pos + note_height - 0.5f;
205 x[2] = _black_note_width;
207 x[3] = _black_note_width;
208 y[3] = y_pos + note_height - 0.5f;
210 y[4] = y_pos + note_height - 0.5f;
212 case WHITE_SEPARATOR:
220 y[0] = y_pos + note_height - 0.5f;
226 y[3] = y_pos + note_height - 0.5f;
228 y[4] = y_pos + note_height - 0.5f;
232 y[0] = y_pos + note_height - 1.5f;
235 x[2] = _black_note_width + 1.0f;
237 x[3] = _black_note_width + 1.0f;
242 y[5] = y_pos + note_height - 1.5f;
244 y[6] = y_pos + note_height - 1.5f;
248 y[0] = y_pos + note_height - 1.5f;
255 x[4] = _black_note_width + 1.0f;
257 x[5] = _black_note_width + 1.0f;
258 y[5] = y_pos + note_height - 1.5f;
260 y[6] = y_pos + note_height - 1.5f;
264 y[0] = y_pos + note_height - 1.5f;
267 x[2] = _black_note_width + 1.0f;
269 x[3] = _black_note_width + 1.0f;
275 x[6] = _black_note_width + 1.0f;
277 x[7] = _black_note_width + 1.0f;
278 y[7] = y_pos + note_height - 1.5f;
280 y[8] = y_pos + note_height - 1.5f;
288 PianoRollHeader::on_expose_event (GdkEventExpose* ev)
290 GdkRectangle& rect = ev->area;
293 Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context();
294 Cairo::RefPtr<Cairo::LinearGradient> pat = Cairo::LinearGradient::create(0, 0, _black_note_width, 0);
297 Color bg, tl_shadow, br_shadow;
299 int y1 = max(rect.y, 0);
300 int y2 = min(rect.y + rect.height, (int) floor(_view.contents_height() - 1.0f));
302 //Cairo::TextExtents te;
303 lowest = max(_view.lowest_note(), _view.y_to_note(y2));
304 highest = min(_view.highest_note(), _view.y_to_note(y1));
310 cr->select_font_face ("Georgia", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD);
311 font_size = min((double) 10.0f, _note_height - 4.0f);
312 cr->set_font_size(font_size);
314 /* fill the entire rect with the color for non-highlighted white notes.
315 * then we won't have to draw the background for those notes,
316 * and would only have to draw the background for the one highlighted white note*/
317 //cr->rectangle(rect.x, rect.y, rect.width, rect.height);
318 //cr->set_source_rgb(white.r, white.g, white.b);
321 cr->set_line_width(1.0f);
323 /* draw vertical lines with shade at both ends of the widget */
324 cr->set_source_rgb(0.0f, 0.0f, 0.0f);
325 cr->move_to(0.5f, rect.y);
326 cr->line_to(0.5f, rect.y + rect.height);
328 cr->move_to(get_width() - 0.5f, rect.y);
329 cr->line_to(get_width() - 0.5f, rect.y + rect.height);
332 //pat->add_color_stop_rgb(0.0, 0.33, 0.33, 0.33);
333 //pat->add_color_stop_rgb(0.2, 0.39, 0.39, 0.39);
334 //pat->add_color_stop_rgb(1.0, 0.22, 0.22, 0.22);
335 //cr->set_source(pat);
337 for (int i = lowest; i <= highest; ++i) {
347 if (i == _highlighted_note) {
348 bg.set(black_highlight);
353 if (_active_notes[i]) {
354 tl_shadow.set(black_shade_dark);
355 br_shadow.set(black_shade_light);
357 tl_shadow.set(black_shade_light);
358 br_shadow.set(black_shade_dark);
361 /* draw black separators */
362 cr->set_source_rgb(0.0f, 0.0f, 0.0f);
363 get_path(BLACK_SEPARATOR, i, x, y);
364 create_path(cr, x, y, 0, 1);
367 get_path(BLACK_MIDDLE_SEPARATOR, i, x, y);
368 create_path(cr, x, y, 0, 1);
371 get_path(BLACK, i, x, y);
372 render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
377 if (i == _highlighted_note) {
378 bg.set(white_highlight);
383 if (_active_notes[i]) {
384 tl_shadow.set(white_shade_dark);
385 br_shadow.set(white_shade_light);
387 tl_shadow.set(white_shade_light);
388 br_shadow.set(white_shade_dark);
394 if (i == _view.highest_note()) {
395 get_path(WHITE_RECT, i, x, y);
396 render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
398 get_path(WHITE_CF, i, x, y);
399 render_cf(cr, i, x, y, bg, tl_shadow, br_shadow);
406 if (i == _view.highest_note()) {
407 get_path(WHITE_EB, i, x, y);
408 render_eb(cr, i, x, y, bg, tl_shadow, br_shadow);
409 } else if (i == _view.lowest_note()) {
410 get_path(WHITE_CF, i, x, y);
411 render_cf(cr, i, x, y, bg, tl_shadow, br_shadow);
413 get_path(WHITE_DGA, i, x, y);
414 render_dga(cr, i, x, y, bg, tl_shadow, br_shadow);
420 cr->set_source_rgb(0.0f, 0.0f, 0.0f);
421 get_path(WHITE_SEPARATOR, i, x, y);
422 create_path(cr, x, y, 0, 1);
425 if (i == _view.lowest_note()) {
426 get_path(WHITE_RECT, i, x, y);
427 render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
429 get_path(WHITE_EB, i, x, y);
430 render_eb(cr, i, x, y, bg, tl_shadow, br_shadow);
442 /* render the name of which C this is */
445 double y = floor(_view.note_to_y(i)) - 0.5f;
446 double note_height = floor(_view.note_to_y(i - 1)) - y;
451 //cr->get_text_extents(s.str(), te);
452 cr->set_source_rgb(0.30f, 0.30f, 0.30f);
453 cr->move_to(2.0f, y + note_height - 1.0f - (note_height - font_size) / 2.0f);
454 cr->show_text(s.str());
462 PianoRollHeader::on_motion_notify_event (GdkEventMotion* ev)
464 int note = _view.y_to_note(ev->y);
465 set_note_highlight (note);
469 if ( false /*editor().current_mouse_mode() == Editing::MouseRange*/ ) { //ToDo: fix this. this mode is buggy, and of questionable utility anyway
471 /* select note range */
473 if (Keyboard::no_modifiers_active (ev->state)) {
474 AddNoteSelection (note); // EMIT SIGNAL
479 /* redraw already taken care of above in set_note_highlight */
480 if (_clicked_note != NO_MIDI_NOTE && _clicked_note != note) {
481 _active_notes[_clicked_note] = false;
482 send_note_off(_clicked_note);
484 _clicked_note = note;
486 if (!_active_notes[note]) {
487 _active_notes[note] = true;
494 //win->process_updates(false);
500 PianoRollHeader::on_button_press_event (GdkEventButton* ev)
502 int note = _view.y_to_note(ev->y);
503 bool tertiary = Keyboard::modifier_state_contains (ev->state, Keyboard::TertiaryModifier);
505 if (ev->button == 2 && Keyboard::no_modifiers_active (ev->state)) {
506 SetNoteSelection (note); // EMIT SIGNAL
508 } else if (tertiary && (ev->button == 1 || ev->button == 2)) {
509 ExtendNoteSelection (note); // EMIT SIGNAL
511 } else if (ev->button == 1 && note >= 0 && note < 128) {
515 if (!_active_notes[note]) {
516 _active_notes[note] = true;
517 _clicked_note = note;
520 invalidate_note_range(note, note);
522 reset_clicked_note(note);
530 PianoRollHeader::on_button_release_event (GdkEventButton* ev)
532 int note = _view.y_to_note(ev->y);
534 if (false /*editor().current_mouse_mode() == Editing::MouseRange*/ ) { //Todo: this mode is buggy, and of questionable utility anyway
536 if (Keyboard::no_modifiers_active (ev->state)) {
537 AddNoteSelection (note); // EMIT SIGNAL
538 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
539 ToggleNoteSelection (note); // EMIT SIGNAL
540 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::RangeSelectModifier)) {
541 ExtendNoteSelection (note); // EMIT SIGNAL
549 if (note == _clicked_note) {
550 reset_clicked_note(note);
560 PianoRollHeader::set_note_highlight (uint8_t note) {
561 if (_highlighted_note == note) {
565 if (_highlighted_note != NO_MIDI_NOTE) {
566 if (note > _highlighted_note) {
567 invalidate_note_range (_highlighted_note, note);
569 invalidate_note_range (note, _highlighted_note);
573 _highlighted_note = note;
575 if (_highlighted_note != NO_MIDI_NOTE) {
576 invalidate_note_range (_highlighted_note, _highlighted_note);
581 PianoRollHeader::on_enter_notify_event (GdkEventCrossing* ev)
583 set_note_highlight (_view.y_to_note(ev->y));
588 PianoRollHeader::on_leave_notify_event (GdkEventCrossing*)
590 invalidate_note_range(_highlighted_note, _highlighted_note);
592 if (_clicked_note != NO_MIDI_NOTE) {
593 reset_clicked_note(_clicked_note, _clicked_note != _highlighted_note);
596 _highlighted_note = NO_MIDI_NOTE;
601 PianoRollHeader::on_scroll_event (GdkEventScroll*)
607 PianoRollHeader::note_range_changed()
609 _note_height = floor(_view.note_height()) + 0.5f;
614 PianoRollHeader::invalidate_note_range(int lowest, int highest)
616 Glib::RefPtr<Gdk::Window> win = get_window();
619 // the non-rectangular geometry of some of the notes requires more
620 // redraws than the notes that actually changed.
621 switch(lowest % 12) {
624 lowest = max((int) _view.lowest_note(), lowest);
627 lowest = max((int) _view.lowest_note(), lowest - 1);
631 switch(highest % 12) {
634 highest = min((int) _view.highest_note(), highest);
641 highest = min((int) _view.highest_note(), highest + 1);
644 highest = min((int) _view.highest_note(), highest + 2);
648 double y = _view.note_to_y(highest);
649 double height = _view.note_to_y(lowest - 1) - y;
652 rect.set_width(get_width());
653 rect.set_y((int) floor(y));
654 rect.set_height((int) floor(height));
657 win->invalidate_rect(rect, false);
662 PianoRollHeader::on_size_request(Gtk::Requisition* r)
668 PianoRollHeader::on_size_allocate(Gtk::Allocation& a)
670 DrawingArea::on_size_allocate(a);
672 _black_note_width = floor(0.7 * get_width()) + 0.5f;
676 PianoRollHeader::send_note_on(uint8_t note)
678 boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
679 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*> (&_view.trackview ());
681 //cerr << "note on: " << (int) note << endl;
684 _event[0] = (MIDI_CMD_NOTE_ON | mtv->get_channel_for_add ());
688 track->write_immediate_event(3, _event);
693 PianoRollHeader::send_note_off(uint8_t note)
695 boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
696 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*> (&_view.trackview ());
699 _event[0] = (MIDI_CMD_NOTE_OFF | mtv->get_channel_for_add ());
703 track->write_immediate_event(3, _event);
708 PianoRollHeader::reset_clicked_note (uint8_t note, bool invalidate)
710 _active_notes[note] = false;
711 _clicked_note = NO_MIDI_NOTE;
712 send_note_off (note);
714 invalidate_note_range (note, note);
719 PianoRollHeader::editor() const
721 return _view.trackview().editor();