Attempt #46 to understand MIDI data locking.
[ardour2.git] / gtk2_ardour / piano_roll_header.cc
blob02d656d69863efceed4831a8d2416f5ac32a2da0
1 /*
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.
19 #include <iostream>
20 #include "evoral/midi_events.h"
21 #include "ardour/midi_track.h"
23 #include "gtkmm2ext/keyboard.h"
25 #include "piano_roll_header.h"
26 #include "midi_time_axis.h"
27 #include "midi_streamview.h"
29 const int no_note = 0xff;
31 using namespace std;
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(0.87f, 0.88f, 0.86f);
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.30f, 0.30f, 0.30f);
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()
45 : r(1.0f)
46 , g(1.0f)
47 , b(1.0f)
51 PianoRollHeader::Color::Color(double _r, double _g, double _b)
52 : r(_r)
53 , g(_g)
54 , b(_b)
58 inline void
59 PianoRollHeader::Color::set(const PianoRollHeader::Color& c)
61 r = c.r;
62 g = c.g;
63 b = c.b;
66 PianoRollHeader::PianoRollHeader(MidiStreamView& v)
67 : _view(v)
68 , _highlighted_note(no_note)
69 , _clicked_note(no_note)
70 , _dragging(false)
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 |
77 Gdk::SCROLL_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));
86 inline void
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]);
96 inline void
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);
102 cr->fill();
104 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
105 create_path(cr, x, y, 0, 2);
106 cr->stroke();
108 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
109 create_path(cr, x, y, 2, 4);
110 cr->stroke();
113 inline void
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);
119 cr->fill();
121 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
122 create_path(cr, x, y, 0, 4);
123 cr->stroke();
125 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
126 create_path(cr, x, y, 4, 6);
127 cr->stroke();
130 inline void
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);
136 cr->fill();
138 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
139 create_path(cr, x, y, 0, 2);
140 cr->stroke();
141 create_path(cr, x, y, 4, 5);
142 cr->stroke();
144 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
145 create_path(cr, x, y, 2, 4);
146 cr->stroke();
147 create_path(cr, x, y, 5, 6);
148 cr->stroke();
151 inline void
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);
157 cr->fill();
159 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
160 create_path(cr, x, y, 0, 4);
161 cr->stroke();
162 create_path(cr, x, y, 6, 7);
163 cr->stroke();
165 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
166 create_path(cr, x, y, 4, 6);
167 cr->stroke();
168 create_path(cr, x, y, 7, 8);
169 cr->stroke();
172 void
173 PianoRollHeader::get_path(PianoRollHeader::ItemType note_type, int note, double x[], double y[])
175 double y_pos = floor(_view.note_to_y(note)) - 0.5f;
176 double note_height;
177 double other_y1 = floor(_view.note_to_y(note+1)) + floor(_note_height / 2.0f) + 0.5f;
178 double other_y2 = floor(_view.note_to_y(note-1)) + floor(_note_height / 2.0f) - 1.0f;
179 double width = get_width();
181 if (note == 0) {
182 note_height = floor(_view.contents_height()) - y_pos;
183 } else {
184 note_height = floor(_view.note_to_y(note - 1)) - y_pos;
187 switch (note_type) {
188 case BLACK_SEPARATOR:
189 x[0] = 1.5f;
190 y[0] = y_pos;
191 x[1] = _black_note_width;
192 y[1] = y_pos;
193 break;
194 case BLACK_MIDDLE_SEPARATOR:
195 x[0] = _black_note_width;
196 y[0] = y_pos + floor(_note_height / 2.0f);
197 x[1] = width - 1.0f;
198 y[1] = y[0];
199 break;
200 case BLACK:
201 x[0] = 1.5f;
202 y[0] = y_pos + note_height - 0.5f;
203 x[1] = 1.5f;
204 y[1] = y_pos + 1.0f;
205 x[2] = _black_note_width;
206 y[2] = y_pos + 1.0f;
207 x[3] = _black_note_width;
208 y[3] = y_pos + note_height - 0.5f;
209 x[4] = 1.5f;
210 y[4] = y_pos + note_height - 0.5f;
211 return;
212 case WHITE_SEPARATOR:
213 x[0] = 1.5f;
214 y[0] = y_pos;
215 x[1] = width - 1.5f;
216 y[1] = y_pos;
217 break;
218 case WHITE_RECT:
219 x[0] = 1.5f;
220 y[0] = y_pos + note_height - 0.5f;
221 x[1] = 1.5f;
222 y[1] = y_pos + 1.0f;
223 x[2] = width - 1.5f;
224 y[2] = y_pos + 1.0f;
225 x[3] = width - 1.5f;
226 y[3] = y_pos + note_height - 0.5f;
227 x[4] = 1.5f;
228 y[4] = y_pos + note_height - 0.5f;
229 return;
230 case WHITE_CF:
231 x[0] = 1.5f;
232 y[0] = y_pos + note_height - 1.5f;
233 x[1] = 1.5f;
234 y[1] = y_pos + 1.0f;
235 x[2] = _black_note_width + 1.0f;
236 y[2] = y_pos + 1.0f;
237 x[3] = _black_note_width + 1.0f;
238 y[3] = other_y1;
239 x[4] = width - 1.5f;
240 y[4] = other_y1;
241 x[5] = width - 1.5f;
242 y[5] = y_pos + note_height - 1.5f;
243 x[6] = 1.5f;
244 y[6] = y_pos + note_height - 1.5f;
245 return;
246 case WHITE_EB:
247 x[0] = 1.5f;
248 y[0] = y_pos + note_height - 1.5f;
249 x[1] = 1.5f;
250 y[1] = y_pos + 1.0f;
251 x[2] = width - 1.5f;
252 y[2] = y_pos + 1.0f;
253 x[3] = width - 1.5f;
254 y[3] = other_y2;
255 x[4] = _black_note_width + 1.0f;
256 y[4] = other_y2;
257 x[5] = _black_note_width + 1.0f;
258 y[5] = y_pos + note_height - 1.5f;
259 x[6] = 1.5f;
260 y[6] = y_pos + note_height - 1.5f;
261 return;
262 case WHITE_DGA:
263 x[0] = 1.5f;
264 y[0] = y_pos + note_height - 1.5f;
265 x[1] = 1.5f;
266 y[1] = y_pos + 1.0f;
267 x[2] = _black_note_width + 1.0f;
268 y[2] = y_pos + 1.0f;
269 x[3] = _black_note_width + 1.0f;
270 y[3] = other_y1;
271 x[4] = width - 1.5f;
272 y[4] = other_y1;
273 x[5] = width - 1.5f;
274 y[5] = other_y2;
275 x[6] = _black_note_width + 1.0f;
276 y[6] = other_y2;
277 x[7] = _black_note_width + 1.0f;
278 y[7] = y_pos + note_height - 1.5f;
279 x[8] = 1.5f;
280 y[8] = y_pos + note_height - 1.5f;
281 return;
282 default:
283 return;
287 bool
288 PianoRollHeader::on_expose_event (GdkEventExpose* ev)
290 GdkRectangle& rect = ev->area;
291 double font_size;
292 int lowest, highest;
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);
295 double x[9];
296 double y[9];
297 Color bg, tl_shadow, br_shadow;
298 int oct_rel;
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));
306 if (lowest > 127) {
307 lowest = 0;
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);
319 //cr->fill();
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);
327 cr->stroke();
328 cr->move_to(get_width() - 0.5f, rect.y);
329 cr->line_to(get_width() - 0.5f, rect.y + rect.height);
330 cr->stroke();
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) {
338 oct_rel = i % 12;
340 switch (oct_rel) {
341 case 1:
342 case 3:
343 case 6:
344 case 8:
345 case 10:
346 /* black note */
347 if (i == _highlighted_note) {
348 bg.set(black_highlight);
349 } else {
350 bg.set(black);
353 if (_active_notes[i]) {
354 tl_shadow.set(black_shade_dark);
355 br_shadow.set(black_shade_light);
356 } else {
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);
365 cr->stroke();
367 get_path(BLACK_MIDDLE_SEPARATOR, i, x, y);
368 create_path(cr, x, y, 0, 1);
369 cr->stroke();
371 get_path(BLACK, i, x, y);
372 render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
373 break;
375 default:
376 /* white note */
377 if (i == _highlighted_note) {
378 bg.set(white_highlight);
379 } else {
380 bg.set(white);
383 if (_active_notes[i]) {
384 tl_shadow.set(white_shade_dark);
385 br_shadow.set(white_shade_light);
386 } else {
387 tl_shadow.set(white_shade_light);
388 br_shadow.set(white_shade_dark);
391 switch(oct_rel) {
392 case 0:
393 case 5:
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);
397 } else {
398 get_path(WHITE_CF, i, x, y);
399 render_cf(cr, i, x, y, bg, tl_shadow, br_shadow);
401 break;
403 case 2:
404 case 7:
405 case 9:
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);
412 } else {
413 get_path(WHITE_DGA, i, x, y);
414 render_dga(cr, i, x, y, bg, tl_shadow, br_shadow);
416 break;
418 case 4:
419 case 11:
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);
423 cr->stroke();
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);
428 } else {
429 get_path(WHITE_EB, i, x, y);
430 render_eb(cr, i, x, y, bg, tl_shadow, br_shadow);
432 break;
434 default:
435 break;
438 break;
442 /* render the name of which C this is */
443 if (oct_rel == 0) {
444 std::stringstream s;
445 double y = floor(_view.note_to_y(i)) - 0.5f;
446 double note_height = floor(_view.note_to_y(i - 1)) - y;
448 int cn = i / 12 - 1;
449 s << "C" << cn;
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());
458 return true;
461 bool
462 PianoRollHeader::on_motion_notify_event (GdkEventMotion* ev)
464 if (_dragging) {
466 int note = _view.y_to_note(ev->y);
468 if (_highlighted_note != no_note) {
469 if (note > _highlighted_note) {
470 invalidate_note_range(_highlighted_note, note);
471 } else {
472 invalidate_note_range(note, _highlighted_note);
475 _highlighted_note = note;
478 /* redraw already taken care of above */
479 if (_clicked_note != no_note && _clicked_note != note) {
480 _active_notes[_clicked_note] = false;
481 send_note_off(_clicked_note);
483 _clicked_note = note;
485 if (!_active_notes[note]) {
486 _active_notes[note] = true;
487 send_note_on(note);
492 //win->process_updates(false);
494 return true;
497 bool
498 PianoRollHeader::on_button_press_event (GdkEventButton* ev)
500 int note = _view.y_to_note(ev->y);
502 if (ev->button == 2) {
503 send_note_on (note);
504 /* relax till release */
505 } else {
507 if (ev->type == GDK_BUTTON_PRESS && note >= 0 && note < 128) {
509 add_modal_grab();
510 _dragging = true;
512 if (!_active_notes[note]) {
513 _active_notes[note] = true;
514 _clicked_note = note;
515 send_note_on(note);
517 invalidate_note_range(note, note);
518 } else {
519 reset_clicked_note(note);
524 return true;
527 bool
528 PianoRollHeader::on_button_release_event (GdkEventButton* ev)
530 int note = _view.y_to_note(ev->y);
532 if (ev->button == 2) {
533 send_note_off (note);
535 if (Keyboard::no_modifiers_active (ev->state)) {
536 AddNoteSelection (note); // EMIT SIGNAL
537 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
538 ToggleNoteSelection (note); // EMIT SIGNAL
539 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::RangeSelectModifier)) {
540 ExtendNoteSelection (note); // EMIT SIGNAL
543 } else {
545 if (_dragging) {
546 remove_modal_grab();
547 _dragging = false;
549 if (note == _clicked_note) {
550 reset_clicked_note(note);
555 return true;
558 bool
559 PianoRollHeader::on_enter_notify_event (GdkEventCrossing* ev)
561 _highlighted_note = _view.y_to_note(ev->y);
563 invalidate_note_range(_highlighted_note, _highlighted_note);
564 return true;
567 bool
568 PianoRollHeader::on_leave_notify_event (GdkEventCrossing*)
570 invalidate_note_range(_highlighted_note, _highlighted_note);
572 if (_clicked_note != no_note) {
573 reset_clicked_note(_clicked_note, _clicked_note != _highlighted_note);
576 _highlighted_note = no_note;
577 return true;
580 bool
581 PianoRollHeader::on_scroll_event (GdkEventScroll*)
583 return true;
586 void
587 PianoRollHeader::note_range_changed()
589 _note_height = floor(_view.note_height()) + 0.5f;
590 queue_draw();
593 void
594 PianoRollHeader::invalidate_note_range(int lowest, int highest)
596 Glib::RefPtr<Gdk::Window> win = get_window();
597 Gdk::Rectangle rect;
599 // the non-rectangular geometry of some of the notes requires more
600 // redraws than the notes that actually changed.
601 switch(lowest % 12) {
602 case 0:
603 case 5:
604 lowest = max((int) _view.lowest_note(), lowest);
605 break;
606 default:
607 lowest = max((int) _view.lowest_note(), lowest - 1);
608 break;
611 switch(highest % 12) {
612 case 4:
613 case 11:
614 highest = min((int) _view.highest_note(), highest);
615 break;
616 case 1:
617 case 3:
618 case 6:
619 case 8:
620 case 10:
621 highest = min((int) _view.highest_note(), highest + 1);
622 break;
623 default:
624 highest = min((int) _view.highest_note(), highest + 2);
625 break;
628 double y = _view.note_to_y(highest);
629 double height = _view.note_to_y(lowest - 1) - y;
631 rect.set_x(0);
632 rect.set_width(get_width());
633 rect.set_y((int) floor(y));
634 rect.set_height((int) floor(height));
636 if (win) {
637 win->invalidate_rect(rect, false);
641 void
642 PianoRollHeader::on_size_request(Gtk::Requisition* r)
644 r->width = 20;
647 void
648 PianoRollHeader::on_size_allocate(Gtk::Allocation& a)
650 DrawingArea::on_size_allocate(a);
652 _black_note_width = floor(0.7 * get_width()) + 0.5f;
655 void
656 PianoRollHeader::send_note_on(uint8_t note)
658 boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
660 //cerr << "note on: " << (int) note << endl;
662 if (track) {
663 _event[0] = (MIDI_CMD_NOTE_ON | track->default_channel());
664 _event[1] = note;
665 _event[2] = 100;
667 track->write_immediate_event(3, _event);
671 void
672 PianoRollHeader::send_note_off(uint8_t note)
674 boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
676 if (track) {
677 _event[0] = (MIDI_CMD_NOTE_OFF | track->default_channel());
678 _event[1] = note;
679 _event[2] = 100;
681 track->write_immediate_event(3, _event);
685 void
686 PianoRollHeader::reset_clicked_note (uint8_t note, bool invalidate)
688 _active_notes[note] = false;
689 _clicked_note = no_note;
690 send_note_off (note);
691 if (invalidate) {
692 invalidate_note_range (note, note);