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"
30 #include "ui_config.h"
33 using namespace Gtkmm2ext;
35 PianoRollHeader::Color PianoRollHeader::white = PianoRollHeader::Color(0.77f, 0.78f, 0.76f);
36 PianoRollHeader::Color PianoRollHeader::white_highlight = PianoRollHeader::Color(1.00f, 0.40f, 0.40f);
37 PianoRollHeader::Color PianoRollHeader::white_shade_light = PianoRollHeader::Color(0.95f, 0.95f, 0.95f);
38 PianoRollHeader::Color PianoRollHeader::white_shade_dark = PianoRollHeader::Color(0.56f, 0.56f, 0.56f);
40 PianoRollHeader::Color PianoRollHeader::black = PianoRollHeader::Color(0.24f, 0.24f, 0.24f);
41 PianoRollHeader::Color PianoRollHeader::black_highlight = PianoRollHeader::Color(0.60f, 0.10f, 0.10f);
42 PianoRollHeader::Color PianoRollHeader::black_shade_light = PianoRollHeader::Color(0.46f, 0.46f, 0.46f);
43 PianoRollHeader::Color PianoRollHeader::black_shade_dark = PianoRollHeader::Color(0.1f, 0.1f, 0.1f);
45 PianoRollHeader::Color::Color()
52 PianoRollHeader::Color::Color(double _r, double _g, double _b)
60 PianoRollHeader::Color::set(const PianoRollHeader::Color& c)
67 PianoRollHeader::PianoRollHeader(MidiStreamView& v)
69 , _highlighted_note(NO_MIDI_NOTE)
70 , _clicked_note(NO_MIDI_NOTE)
73 add_events (Gdk::BUTTON_PRESS_MASK |
74 Gdk::BUTTON_RELEASE_MASK |
75 Gdk::POINTER_MOTION_MASK |
76 Gdk::ENTER_NOTIFY_MASK |
77 Gdk::LEAVE_NOTIFY_MASK |
80 for (int i = 0; i < 128; ++i) {
81 _active_notes[i] = false;
84 _view.NoteRangeChanged.connect (sigc::mem_fun (*this, &PianoRollHeader::note_range_changed));
88 create_path(Cairo::RefPtr<Cairo::Context> cr, double x[], double y[], int start, int stop)
90 cr->move_to(x[start], y[start]);
92 for (int i = start+1; i <= stop; ++i) {
93 cr->line_to(x[i], y[i]);
98 render_rect(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
99 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
101 cr->set_source_rgb(bg.r, bg.g, bg.b);
102 create_path(cr, x, y, 0, 4);
105 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
106 create_path(cr, x, y, 0, 2);
109 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
110 create_path(cr, x, y, 2, 4);
115 render_cf(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
116 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
118 cr->set_source_rgb(bg.r, bg.g, bg.b);
119 create_path(cr, x, y, 0, 6);
122 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
123 create_path(cr, x, y, 0, 4);
126 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
127 create_path(cr, x, y, 4, 6);
132 render_eb(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
133 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
135 cr->set_source_rgb(bg.r, bg.g, bg.b);
136 create_path(cr, x, y, 0, 6);
139 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
140 create_path(cr, x, y, 0, 2);
142 create_path(cr, x, y, 4, 5);
145 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
146 create_path(cr, x, y, 2, 4);
148 create_path(cr, x, y, 5, 6);
153 render_dga(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
154 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
156 cr->set_source_rgb(bg.r, bg.g, bg.b);
157 create_path(cr, x, y, 0, 8);
160 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
161 create_path(cr, x, y, 0, 4);
163 create_path(cr, x, y, 6, 7);
166 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
167 create_path(cr, x, y, 4, 6);
169 create_path(cr, x, y, 7, 8);
174 PianoRollHeader::get_path(PianoRollHeader::ItemType note_type, int note, double x[], double y[])
176 double y_pos = floor(_view.note_to_y(note)) + 1.5f;
178 double other_y1 = floor(_view.note_to_y(note+1)) + floor(_note_height / 2.0f) + 2.5f;
179 double other_y2 = floor(_view.note_to_y(note-1)) + floor(_note_height / 2.0f) + 1.0f;
180 double width = get_width();
183 note_height = floor(_view.contents_height()) - y_pos + 2.;
185 note_height = floor(_view.note_to_y(note - 1)) - y_pos + 2.;
189 case BLACK_SEPARATOR:
192 x[1] = _black_note_width;
195 case BLACK_MIDDLE_SEPARATOR:
196 x[0] = _black_note_width;
197 y[0] = y_pos + floor(_note_height / 2.0f);
203 y[0] = y_pos + note_height - 0.5f;
206 x[2] = _black_note_width;
208 x[3] = _black_note_width;
209 y[3] = y_pos + note_height - 0.5f;
211 y[4] = y_pos + note_height - 0.5f;
213 case WHITE_SEPARATOR:
221 y[0] = y_pos + note_height - 0.5f;
227 y[3] = y_pos + note_height - 0.5f;
229 y[4] = y_pos + note_height - 0.5f;
233 y[0] = y_pos + note_height - 1.5f;
236 x[2] = _black_note_width + 1.0f;
238 x[3] = _black_note_width + 1.0f;
243 y[5] = y_pos + note_height - 1.5f;
245 y[6] = y_pos + note_height - 1.5f;
249 y[0] = y_pos + note_height - 1.5f;
256 x[4] = _black_note_width + 1.0f;
258 x[5] = _black_note_width + 1.0f;
259 y[5] = y_pos + note_height - 1.5f;
261 y[6] = y_pos + note_height - 1.5f;
265 y[0] = y_pos + note_height - 1.5f;
268 x[2] = _black_note_width + 1.0f;
270 x[3] = _black_note_width + 1.0f;
276 x[6] = _black_note_width + 1.0f;
278 x[7] = _black_note_width + 1.0f;
279 y[7] = y_pos + note_height - 1.5f;
281 y[8] = y_pos + note_height - 1.5f;
289 PianoRollHeader::on_expose_event (GdkEventExpose* ev)
291 GdkRectangle& rect = ev->area;
294 Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context();
295 Cairo::RefPtr<Cairo::LinearGradient> pat = Cairo::LinearGradient::create(0, 0, _black_note_width, 0);
298 Color bg, tl_shadow, br_shadow;
300 int y1 = max(rect.y, 0);
301 int y2 = min(rect.y + rect.height, (int) floor(_view.contents_height() - 1.0f));
303 //Cairo::TextExtents te;
304 lowest = max(_view.lowest_note(), _view.y_to_note(y2));
305 highest = min(_view.highest_note(), _view.y_to_note(y1));
311 cr->select_font_face ("Georgia", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD);
312 font_size = min((double) 10.0f, _note_height - 4.0f);
313 cr->set_font_size(font_size);
315 /* fill the entire rect with the color for non-highlighted white notes.
316 * then we won't have to draw the background for those notes,
317 * and would only have to draw the background for the one highlighted white note*/
318 //cr->rectangle(rect.x, rect.y, rect.width, rect.height);
319 //cr->set_source_rgb(white.r, white.g, white.b);
322 cr->set_line_width(1.0f);
324 /* draw vertical lines with shade at both ends of the widget */
325 cr->set_source_rgb(0.0f, 0.0f, 0.0f);
326 cr->move_to(0.5f, rect.y);
327 cr->line_to(0.5f, rect.y + rect.height);
329 cr->move_to(get_width() - 0.5f, rect.y);
330 cr->line_to(get_width() - 0.5f, rect.y + rect.height);
333 //pat->add_color_stop_rgb(0.0, 0.33, 0.33, 0.33);
334 //pat->add_color_stop_rgb(0.2, 0.39, 0.39, 0.39);
335 //pat->add_color_stop_rgb(1.0, 0.22, 0.22, 0.22);
336 //cr->set_source(pat);
338 for (int i = lowest; i <= highest; ++i) {
348 if (i == _highlighted_note) {
349 bg.set(black_highlight);
354 if (_active_notes[i]) {
355 tl_shadow.set(black_shade_dark);
356 br_shadow.set(black_shade_light);
358 tl_shadow.set(black_shade_light);
359 br_shadow.set(black_shade_dark);
362 /* draw black separators */
363 cr->set_source_rgb(0.0f, 0.0f, 0.0f);
364 get_path(BLACK_SEPARATOR, i, x, y);
365 create_path(cr, x, y, 0, 1);
368 get_path(BLACK_MIDDLE_SEPARATOR, i, x, y);
369 create_path(cr, x, y, 0, 1);
372 get_path(BLACK, i, x, y);
373 render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
378 if (i == _highlighted_note) {
379 bg.set(white_highlight);
384 if (_active_notes[i]) {
385 tl_shadow.set(white_shade_dark);
386 br_shadow.set(white_shade_light);
388 tl_shadow.set(white_shade_light);
389 br_shadow.set(white_shade_dark);
395 if (i == _view.highest_note()) {
396 get_path(WHITE_RECT, i, x, y);
397 render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
399 get_path(WHITE_CF, i, x, y);
400 render_cf(cr, i, x, y, bg, tl_shadow, br_shadow);
407 if (i == _view.highest_note()) {
408 get_path(WHITE_EB, i, x, y);
409 render_eb(cr, i, x, y, bg, tl_shadow, br_shadow);
410 } else if (i == _view.lowest_note()) {
411 get_path(WHITE_CF, i, x, y);
412 render_cf(cr, i, x, y, bg, tl_shadow, br_shadow);
414 get_path(WHITE_DGA, i, x, y);
415 render_dga(cr, i, x, y, bg, tl_shadow, br_shadow);
421 cr->set_source_rgb(0.0f, 0.0f, 0.0f);
422 get_path(WHITE_SEPARATOR, i, x, y);
423 create_path(cr, x, y, 0, 1);
426 if (i == _view.lowest_note()) {
427 get_path(WHITE_RECT, i, x, y);
428 render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
430 get_path(WHITE_EB, i, x, y);
431 render_eb(cr, i, x, y, bg, tl_shadow, br_shadow);
443 /* render the name of which C this is */
446 double y = floor(_view.note_to_y(i)) - 0.5f;
447 double note_height = floor(_view.note_to_y(i - 1)) - y;
452 //cr->get_text_extents(s.str(), te);
453 cr->set_source_rgb(0.30f, 0.30f, 0.30f);
454 cr->move_to(2.0f, y + note_height - 1.0f - (note_height - font_size) / 2.0f);
455 cr->show_text(s.str());
463 PianoRollHeader::on_motion_notify_event (GdkEventMotion* ev)
465 int note = _view.y_to_note(ev->y);
466 set_note_highlight (note);
470 if ( false /*editor().current_mouse_mode() == Editing::MouseRange*/ ) { //ToDo: fix this. this mode is buggy, and of questionable utility anyway
472 /* select note range */
474 if (Keyboard::no_modifiers_active (ev->state)) {
475 AddNoteSelection (note); // EMIT SIGNAL
480 /* redraw already taken care of above in set_note_highlight */
481 if (_clicked_note != NO_MIDI_NOTE && _clicked_note != note) {
482 _active_notes[_clicked_note] = false;
483 send_note_off(_clicked_note);
485 _clicked_note = note;
487 if (!_active_notes[note]) {
488 _active_notes[note] = true;
495 //win->process_updates(false);
501 PianoRollHeader::on_button_press_event (GdkEventButton* ev)
503 int note = _view.y_to_note(ev->y);
504 bool tertiary = Keyboard::modifier_state_contains (ev->state, Keyboard::TertiaryModifier);
506 if (ev->button == 2 && Keyboard::no_modifiers_active (ev->state)) {
507 SetNoteSelection (note); // EMIT SIGNAL
509 } else if (tertiary && (ev->button == 1 || ev->button == 2)) {
510 ExtendNoteSelection (note); // EMIT SIGNAL
512 } else if (ev->button == 1 && note >= 0 && note < 128) {
516 if (!_active_notes[note]) {
517 _active_notes[note] = true;
518 _clicked_note = note;
521 invalidate_note_range(note, note);
523 reset_clicked_note(note);
531 PianoRollHeader::on_button_release_event (GdkEventButton* ev)
533 int note = _view.y_to_note(ev->y);
535 if (false /*editor().current_mouse_mode() == Editing::MouseRange*/ ) { //Todo: this mode is buggy, and of questionable utility anyway
537 if (Keyboard::no_modifiers_active (ev->state)) {
538 AddNoteSelection (note); // EMIT SIGNAL
539 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
540 ToggleNoteSelection (note); // EMIT SIGNAL
541 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::RangeSelectModifier)) {
542 ExtendNoteSelection (note); // EMIT SIGNAL
550 if (note == _clicked_note) {
551 reset_clicked_note(note);
561 PianoRollHeader::set_note_highlight (uint8_t note) {
562 if (_highlighted_note == note) {
566 if (_highlighted_note != NO_MIDI_NOTE) {
567 if (note > _highlighted_note) {
568 invalidate_note_range (_highlighted_note, note);
570 invalidate_note_range (note, _highlighted_note);
574 _highlighted_note = note;
576 if (_highlighted_note != NO_MIDI_NOTE) {
577 invalidate_note_range (_highlighted_note, _highlighted_note);
582 PianoRollHeader::on_enter_notify_event (GdkEventCrossing* ev)
584 set_note_highlight (_view.y_to_note(ev->y));
589 PianoRollHeader::on_leave_notify_event (GdkEventCrossing*)
591 invalidate_note_range(_highlighted_note, _highlighted_note);
593 if (_clicked_note != NO_MIDI_NOTE) {
594 reset_clicked_note(_clicked_note, _clicked_note != _highlighted_note);
597 _highlighted_note = NO_MIDI_NOTE;
602 PianoRollHeader::on_scroll_event (GdkEventScroll*)
608 PianoRollHeader::note_range_changed()
610 _note_height = floor(_view.note_height()) + 0.5f;
615 PianoRollHeader::invalidate_note_range(int lowest, int highest)
617 Glib::RefPtr<Gdk::Window> win = get_window();
620 // the non-rectangular geometry of some of the notes requires more
621 // redraws than the notes that actually changed.
622 switch(lowest % 12) {
625 lowest = max((int) _view.lowest_note(), lowest);
628 lowest = max((int) _view.lowest_note(), lowest - 1);
632 switch(highest % 12) {
635 highest = min((int) _view.highest_note(), highest);
642 highest = min((int) _view.highest_note(), highest + 1);
645 highest = min((int) _view.highest_note(), highest + 2);
649 double y = _view.note_to_y(highest);
650 double height = _view.note_to_y(lowest - 1) - y;
653 rect.set_width(get_width());
654 rect.set_y((int) floor(y));
655 rect.set_height((int) floor(height));
658 win->invalidate_rect(rect, false);
663 PianoRollHeader::on_size_request(Gtk::Requisition* r)
665 r->width = std::max (20.f, rintf (20.f * UIConfiguration::instance().get_ui_scale()));
669 PianoRollHeader::on_size_allocate(Gtk::Allocation& a)
671 DrawingArea::on_size_allocate(a);
673 _black_note_width = floor(0.7 * get_width()) + 0.5f;
677 PianoRollHeader::send_note_on(uint8_t note)
679 boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
680 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*> (&_view.trackview ());
682 //cerr << "note on: " << (int) note << endl;
685 _event[0] = (MIDI_CMD_NOTE_ON | mtv->get_channel_for_add ());
689 track->write_immediate_event(3, _event);
694 PianoRollHeader::send_note_off(uint8_t note)
696 boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
697 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*> (&_view.trackview ());
700 _event[0] = (MIDI_CMD_NOTE_OFF | mtv->get_channel_for_add ());
704 track->write_immediate_event(3, _event);
709 PianoRollHeader::reset_clicked_note (uint8_t note, bool invalidate)
711 _active_notes[note] = false;
712 _clicked_note = NO_MIDI_NOTE;
713 send_note_off (note);
715 invalidate_note_range (note, note);
720 PianoRollHeader::editor() const
722 return _view.trackview().editor();