Committed underlay support (from Audun).
[ardour.git] / gtk2_ardour / piano_roll_header.cc
1 /*
2     Copyright (C) 2008 Paul Davis 
3
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.
8
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.
13
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.
17 */
18
19 #include <ardour/midi_track.h>
20
21 #include "piano_roll_header.h"
22 #include "midi_time_axis.h"
23 #include "midi_streamview.h"
24
25 #include <iostream>
26
27 const int no_note = 0xff;
28
29 using namespace std;
30
31 PianoRollHeader::Color PianoRollHeader::white = PianoRollHeader::Color(0.77f, 0.78f, 0.76f);
32 PianoRollHeader::Color PianoRollHeader::white_highlight = PianoRollHeader::Color(0.87f, 0.88f, 0.86f);
33 PianoRollHeader::Color PianoRollHeader::white_shade_light = PianoRollHeader::Color(0.95f, 0.95f, 0.95f);
34 PianoRollHeader::Color PianoRollHeader::white_shade_dark = PianoRollHeader::Color(0.56f, 0.56f, 0.56f);
35
36 PianoRollHeader::Color PianoRollHeader::black = PianoRollHeader::Color(0.24f, 0.24f, 0.24f);
37 PianoRollHeader::Color PianoRollHeader::black_highlight = PianoRollHeader::Color(0.30f, 0.30f, 0.30f);
38 PianoRollHeader::Color PianoRollHeader::black_shade_light = PianoRollHeader::Color(0.46f, 0.46f, 0.46f);
39 PianoRollHeader::Color PianoRollHeader::black_shade_dark = PianoRollHeader::Color(0.1f, 0.1f, 0.1f);
40
41 PianoRollHeader::Color::Color()
42         : r(1.0f)
43         , g(1.0f)
44         , b(1.0f) {
45 }
46
47 PianoRollHeader::Color::Color(double _r, double _g, double _b)
48         : r(_r)
49         , g(_g)
50         , b(_b) {
51 }
52
53 inline void
54 PianoRollHeader::Color::set(const PianoRollHeader::Color& c) {
55         r = c.r;
56         g = c.g;
57         b = c.b;
58 }
59
60 PianoRollHeader::PianoRollHeader(MidiStreamView& v) 
61         : _view(v) 
62         , _highlighted_note(no_note)
63         , _clicked_note(no_note)
64         , _dragging(false) {
65
66         add_events (Gdk::BUTTON_PRESS_MASK |
67                     Gdk::BUTTON_RELEASE_MASK |
68                     Gdk::POINTER_MOTION_MASK |
69                     Gdk::ENTER_NOTIFY_MASK |
70                     Gdk::LEAVE_NOTIFY_MASK |
71                     Gdk::SCROLL_MASK);
72
73         for(int i = 0; i < 128; ++i) {
74                 _active_notes[i] = false;
75         }
76
77         _view.NoteRangeChanged.connect (mem_fun (*this, &PianoRollHeader::note_range_changed));
78 }
79
80 inline void
81 create_path(Cairo::RefPtr<Cairo::Context> cr, double x[], double y[], int start, int stop) {
82         cr->move_to(x[start], y[start]);
83
84         for(int i = start+1; i <= stop; ++i) {
85                 cr->line_to(x[i], y[i]);
86         }
87 }
88
89 inline void 
90 render_rect(Cairo::RefPtr<Cairo::Context> cr, int note, double x[], double y[],
91              PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow) {
92         cr->set_source_rgb(bg.r, bg.g, bg.b);
93         create_path(cr, x, y, 0, 4);
94         cr->fill();
95
96         cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
97         create_path(cr, x, y, 0, 2);
98         cr->stroke();
99
100         cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
101         create_path(cr, x, y, 2, 4);
102         cr->stroke();
103 }
104
105 inline void
106 render_cf(Cairo::RefPtr<Cairo::Context> cr, int note, double x[], double y[],
107                 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow) {
108         cr->set_source_rgb(bg.r, bg.g, bg.b);
109         create_path(cr, x, y, 0, 6);
110         cr->fill();
111
112         cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
113         create_path(cr, x, y, 0, 4);
114         cr->stroke();
115
116         cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
117         create_path(cr, x, y, 4, 6);
118         cr->stroke();
119 }
120
121 inline void
122 render_eb(Cairo::RefPtr<Cairo::Context> cr, int note, double x[], double y[],
123                 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow) {
124         cr->set_source_rgb(bg.r, bg.g, bg.b);
125         create_path(cr, x, y, 0, 6);
126         cr->fill();
127
128         cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
129         create_path(cr, x, y, 0, 2);
130         cr->stroke();
131         create_path(cr, x, y, 4, 5);
132         cr->stroke();
133
134         cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
135         create_path(cr, x, y, 2, 4);
136         cr->stroke();
137         create_path(cr, x, y, 5, 6);
138         cr->stroke();
139 }
140
141 inline void
142 render_dga(Cairo::RefPtr<Cairo::Context> cr, int note, double x[], double y[],
143                  PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow) {
144         cr->set_source_rgb(bg.r, bg.g, bg.b);
145         create_path(cr, x, y, 0, 8);
146         cr->fill();
147
148         cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
149         create_path(cr, x, y, 0, 4);
150         cr->stroke();
151         create_path(cr, x, y, 6, 7);
152         cr->stroke();
153
154         cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
155         create_path(cr, x, y, 4, 6);
156         cr->stroke();
157         create_path(cr, x, y, 7, 8);
158         cr->stroke();
159 }
160
161 void
162 PianoRollHeader::get_path(PianoRollHeader::ItemType note_type, int note, double x[], double y[]) {
163         double y_pos = floor(_view.note_to_y(note)) - 0.5f;
164         double note_height;
165         double other_y1 = floor(_view.note_to_y(note+1)) + floor(_note_height / 2.0f) + 0.5f;
166         double other_y2 = floor(_view.note_to_y(note-1)) + floor(_note_height / 2.0f) - 1.0f;
167         double width = get_width();
168
169         if(note == 0) {
170                 note_height = floor(_view.contents_height()) - y_pos;
171         }
172         else {
173                 note_height = floor(_view.note_to_y(note - 1)) - y_pos;
174         }
175
176         switch(note_type) {
177         case BLACK_SEPARATOR:
178                 x[0] = 1.5f;
179                 y[0] = y_pos;
180                 x[1] = _black_note_width;
181                 y[1] = y_pos;
182                 break;
183         case BLACK_MIDDLE_SEPARATOR:
184                 x[0] = _black_note_width;
185                 y[0] = y_pos + floor(_note_height / 2.0f);
186                 x[1] = width - 1.0f;
187                 y[1] = y[0];
188                 break;
189         case BLACK:
190                 x[0] = 1.5f;
191                 y[0] = y_pos + note_height - 0.5f;
192                 x[1] = 1.5f;
193                 y[1] = y_pos + 1.0f;
194                 x[2] = _black_note_width;
195                 y[2] = y_pos + 1.0f;
196                 x[3] = _black_note_width;
197                 y[3] = y_pos + note_height - 0.5f;
198                 x[4] = 1.5f;
199                 y[4] = y_pos + note_height - 0.5f;
200                 return;
201         case WHITE_SEPARATOR:
202                 x[0] = 1.5f;
203                 y[0] = y_pos;
204                 x[1] = width - 1.5f;
205                 y[1] = y_pos;
206                 break;
207         case WHITE_RECT:
208                 x[0] = 1.5f;
209                 y[0] = y_pos + note_height - 0.5f;
210                 x[1] = 1.5f;
211                 y[1] = y_pos + 1.0f;
212                 x[2] = width - 1.5f;
213                 y[2] = y_pos + 1.0f;
214                 x[3] = width - 1.5f;
215                 y[3] = y_pos + note_height - 0.5f;
216                 x[4] = 1.5f;
217                 y[4] = y_pos + note_height - 0.5f;
218                 return;
219         case WHITE_CF:
220                 x[0] = 1.5f;
221                 y[0] = y_pos + note_height - 1.5f;
222                 x[1] = 1.5f;
223                 y[1] = y_pos + 1.0f;
224                 x[2] = _black_note_width + 1.0f;
225                 y[2] = y_pos + 1.0f;
226                 x[3] = _black_note_width + 1.0f;
227                 y[3] = other_y1;
228                 x[4] = width - 1.5f;
229                 y[4] = other_y1;
230                 x[5] = width - 1.5f;
231                 y[5] = y_pos + note_height - 1.5f;
232                 x[6] = 1.5f;
233                 y[6] = y_pos + note_height - 1.5f;
234                 return;
235         case WHITE_EB:
236                 x[0] = 1.5f;
237                 y[0] = y_pos + note_height - 1.5f;
238                 x[1] = 1.5f;
239                 y[1] = y_pos + 1.0f;
240                 x[2] = width - 1.5f;
241                 y[2] = y_pos + 1.0f;
242                 x[3] = width - 1.5f;
243                 y[3] = other_y2;
244                 x[4] = _black_note_width + 1.0f;
245                 y[4] = other_y2;
246                 x[5] = _black_note_width + 1.0f;
247                 y[5] = y_pos + note_height - 1.5f;
248                 x[6] = 1.5f;
249                 y[6] = y_pos + note_height - 1.5f;
250                 return;
251         case WHITE_DGA:
252                 x[0] = 1.5f;
253                 y[0] = y_pos + note_height - 1.5f;
254                 x[1] = 1.5f;
255                 y[1] = y_pos + 1.0f;
256                 x[2] = _black_note_width + 1.0f;
257                 y[2] = y_pos + 1.0f;
258                 x[3] = _black_note_width + 1.0f;
259                 y[3] = other_y1;
260                 x[4] = width - 1.5f;
261                 y[4] = other_y1;
262                 x[5] = width - 1.5f;
263                 y[5] = other_y2;
264                 x[6] = _black_note_width + 1.0f;
265                 y[6] = other_y2;
266                 x[7] = _black_note_width + 1.0f;
267                 y[7] = y_pos + note_height - 1.5f;
268                 x[8] = 1.5f;
269                 y[8] = y_pos + note_height - 1.5f;
270                 return;
271         default:
272                 return;
273         }
274 }
275
276 bool
277 PianoRollHeader::on_expose_event (GdkEventExpose* ev) {
278         GdkRectangle& rect = ev->area;
279         double font_size;
280         int lowest, highest;
281         Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context();
282         Cairo::RefPtr<Cairo::LinearGradient> pat = Cairo::LinearGradient::create(0, 0, _black_note_width, 0);
283         double x[9];
284         double y[9];
285         Color bg, tl_shadow, br_shadow;
286         int oct_rel;
287         int y1 = max(rect.y, 0);
288         int y2 = min(rect.y + rect.height, (int) floor(_view.contents_height() - 1.0f));
289
290         //Cairo::TextExtents te;
291         lowest = max(_view.lowest_note(), _view.y_to_note(y2));
292         highest = min(_view.highest_note(), _view.y_to_note(y1));
293
294         if(lowest > 127) {
295                 lowest = 0;
296         }
297
298         cr->select_font_face ("Georgia", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD);
299         font_size = min((double) 10.0f, _note_height - 4.0f);
300         cr->set_font_size(font_size);
301
302         /* fill the entire rect with the color for non-highlighted white notes.
303          * then we won't have to draw the background for those notes,
304          * and would only have to draw the background for the one highlighted white note*/
305         //cr->rectangle(rect.x, rect.y, rect.width, rect.height);
306         //cr->set_source_rgb(white.r, white.g, white.b);
307         //cr->fill();
308
309         cr->set_line_width(1.0f);
310
311         /* draw vertical lines with shade at both ends of the widget */
312         cr->set_source_rgb(0.0f, 0.0f, 0.0f);
313         cr->move_to(0.5f, rect.y);
314         cr->line_to(0.5f, rect.y + rect.height);
315         cr->stroke();
316         cr->move_to(get_width() - 0.5f, rect.y);
317         cr->line_to(get_width() - 0.5f, rect.y + rect.height);
318         cr->stroke();
319
320         //pat->add_color_stop_rgb(0.0, 0.33, 0.33, 0.33);
321         //pat->add_color_stop_rgb(0.2, 0.39, 0.39, 0.39);
322         //pat->add_color_stop_rgb(1.0, 0.22, 0.22, 0.22);
323         //cr->set_source(pat);
324
325         for(int i = lowest; i <= highest; ++i) {
326                 oct_rel = i % 12;
327
328                 switch(oct_rel) {
329                 case 1:
330                 case 3:
331                 case 6:
332                 case 8:
333                 case 10:
334                         /* black note */
335                         if(i == _highlighted_note) {
336                                 bg.set(black_highlight);
337                         }
338                         else {
339                                 bg.set(black);
340                         }
341
342                         if(_active_notes[i]) {
343                                 tl_shadow.set(black_shade_dark);
344                                 br_shadow.set(black_shade_light);
345                         }
346                         else {
347                                 tl_shadow.set(black_shade_light);
348                                 br_shadow.set(black_shade_dark);
349                         }
350
351                         /* draw black separators */
352                         cr->set_source_rgb(0.0f, 0.0f, 0.0f);
353                         get_path(BLACK_SEPARATOR, i, x, y);
354                         create_path(cr, x, y, 0, 1);
355                         cr->stroke();
356
357                         get_path(BLACK_MIDDLE_SEPARATOR, i, x, y);
358                         create_path(cr, x, y, 0, 1);
359                         cr->stroke();
360
361                         get_path(BLACK, i, x, y);
362                         render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
363                         break;
364
365                 default:
366                         /* white note */
367                         if(i == _highlighted_note) {
368                                 bg.set(white_highlight);
369                         }
370                         else {
371                                 bg.set(white);
372                         }
373
374                         if(_active_notes[i]) {
375                                 tl_shadow.set(white_shade_dark);
376                                 br_shadow.set(white_shade_light);
377                         }
378                         else {
379                                 tl_shadow.set(white_shade_light);
380                                 br_shadow.set(white_shade_dark);
381                         }
382
383                         switch(oct_rel) {
384                         case 0:
385                         case 5:
386                                 if(i == _view.highest_note()) {
387                                         get_path(WHITE_RECT, i, x, y);
388                                         render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
389                                 }
390                                 else {
391                                         get_path(WHITE_CF, i, x, y);
392                                         render_cf(cr, i, x, y, bg, tl_shadow, br_shadow);
393                                 }
394                                 break;
395
396                         case 2:
397                         case 7:
398                         case 9:
399                                 if(i == _view.highest_note()) {
400                                         get_path(WHITE_EB, i, x, y);
401                                         render_eb(cr, i, x, y, bg, tl_shadow, br_shadow);
402                                 }
403                                 else if(i == _view.lowest_note()) {
404                                         get_path(WHITE_CF, i, x, y);
405                                         render_cf(cr, i, x, y, bg, tl_shadow, br_shadow);
406                                 }
407                                 else {
408                                         get_path(WHITE_DGA, i, x, y);
409                                         render_dga(cr, i, x, y, bg, tl_shadow, br_shadow);
410                                 }
411                                 break;
412
413                         case 4:
414                         case 11:
415                                 cr->set_source_rgb(0.0f, 0.0f, 0.0f);
416                                 get_path(WHITE_SEPARATOR, i, x, y);
417                                 create_path(cr, x, y, 0, 1);
418                                 cr->stroke();
419
420                                 if(i == _view.lowest_note()) {
421                                         get_path(WHITE_RECT, i, x, y);
422                                         render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
423                                 }
424                                 else {
425                                         get_path(WHITE_EB, i, x, y);
426                                         render_eb(cr, i, x, y, bg, tl_shadow, br_shadow);
427                                 }
428                                 break;
429
430                         default:
431                                 break;
432
433                         }
434                         break;
435
436                 }
437
438                 /* render the name of which C this is */
439                 if(oct_rel == 0) {
440                         std::stringstream s;
441                         double y = floor(_view.note_to_y(i)) - 0.5f;
442                         double note_height = floor(_view.note_to_y(i - 1)) - y;
443                         
444                         int cn = i / 12;
445                         s << "C" << cn;
446                         
447                         //cr->get_text_extents(s.str(), te);
448                         cr->set_source_rgb(0.30f, 0.30f, 0.30f);
449                         cr->move_to(2.0f, y + note_height - 1.0f - (note_height - font_size) / 2.0f);
450                         cr->show_text(s.str());
451                 }
452         }
453
454         return true;
455 }
456
457 bool
458 PianoRollHeader::on_motion_notify_event (GdkEventMotion* ev) {
459         int note = _view.y_to_note(ev->y);
460
461         if(_highlighted_note != no_note) {
462                 if(note > _highlighted_note) {
463                         invalidate_note_range(_highlighted_note, note);
464                 }
465                 else {
466                         invalidate_note_range(note, _highlighted_note);
467                 }
468
469                 _highlighted_note = note;
470         }
471
472         /* redraw already taken care of above */
473         if(_clicked_note != no_note && _clicked_note != note) {
474                 _active_notes[_clicked_note] = false;
475                 send_note_off(_clicked_note);
476
477                 _clicked_note = note;
478
479                 if(!_active_notes[note]) {
480                         _active_notes[note] = true;
481                         send_note_on(note);
482                 }
483         }
484
485         //win->process_updates(false);
486
487         return true;
488 }
489
490 bool
491 PianoRollHeader::on_button_press_event (GdkEventButton* ev) {
492         int note = _view.y_to_note(ev->y);
493
494         if(ev->type == GDK_BUTTON_PRESS && note >= 0 && note < 128) {
495                 add_modal_grab();
496                 _dragging = true;
497
498                 if(!_active_notes[note]) {
499                         _active_notes[note] = true;
500                         _clicked_note = note;
501                         send_note_on(note);
502                         
503                         invalidate_note_range(note, note);
504                 }
505                 else {
506                         _clicked_note = no_note;
507                 }
508         }
509
510         return true;
511 }
512
513 bool
514 PianoRollHeader::on_button_release_event (GdkEventButton* ev) {
515         int note = _view.y_to_note(ev->y);
516
517         if(_dragging) {
518                 remove_modal_grab();
519                 _dragging = false;
520
521                 if(note == _clicked_note) {
522                         _active_notes[note] = false;
523                         _clicked_note = no_note;
524                         send_note_off(note);
525                         
526                         invalidate_note_range(note, note);
527                 }
528         }
529
530         return true;
531 }
532
533 bool
534 PianoRollHeader::on_enter_notify_event (GdkEventCrossing* ev) {
535         _highlighted_note = _view.y_to_note(ev->y);
536
537         invalidate_note_range(_highlighted_note, _highlighted_note);
538         return true;
539 }
540
541 bool
542 PianoRollHeader::on_leave_notify_event (GdkEventCrossing*) {
543         invalidate_note_range(_highlighted_note, _highlighted_note);
544         
545         if(_clicked_note != no_note) {
546                 _active_notes[_clicked_note] = false;
547                 send_note_off(_clicked_note);
548
549                 if(_clicked_note != _highlighted_note) {
550                         invalidate_note_range(_clicked_note, _clicked_note);
551                 }
552
553                 _clicked_note = no_note;
554         }
555
556         _highlighted_note = no_note;
557         return true;
558 }
559
560 bool
561 PianoRollHeader::on_scroll_event (GdkEventScroll* ev) {
562         return true;
563 }
564
565 void
566 PianoRollHeader::note_range_changed() {
567         _note_height = floor(_view.note_height()) + 0.5f;
568
569         queue_draw();
570
571         Glib::RefPtr<Gdk::Window> win = get_window();
572
573         if(win) {
574                 win->process_updates(false);
575         }
576 }
577
578 void
579 PianoRollHeader::invalidate_note_range(int lowest, int highest) {
580         Glib::RefPtr<Gdk::Window> win = get_window();
581         Gdk::Rectangle rect;
582
583         // the non-rectangular geometry of some of the notes requires more
584         // redraws than the notes that actually changed.
585         switch(lowest % 12) {
586         case 0:
587         case 5:
588                 lowest = max((int) _view.lowest_note(), lowest);
589                 break;
590         default:
591                 lowest = max((int) _view.lowest_note(), lowest - 1);
592                 break;
593         }
594
595         switch(highest % 12) {
596         case 4:
597         case 11:
598                 highest = min((int) _view.highest_note(), highest);
599                 break;
600         case 1:
601         case 3:
602         case 6:
603         case 8:
604         case 10:
605                 highest = min((int) _view.highest_note(), highest + 1);
606                 break;
607         default:
608                 highest = min((int) _view.highest_note(), highest + 2);
609                 break;
610         }
611
612         double y = _view.note_to_y(highest);
613         double height = _view.note_to_y(lowest - 1) - y;
614
615         rect.set_x(0);
616         rect.set_width(get_width());
617         rect.set_y((int) floor(y));
618         rect.set_height((int) floor(height));
619
620         if(win) {
621                 win->invalidate_rect(rect, false);
622         }
623 }
624
625 void
626 PianoRollHeader::on_size_request(Gtk::Requisition* r) {
627         r->width = 20;
628 }
629
630 void
631 PianoRollHeader::on_size_allocate(Gtk::Allocation& a) {
632         DrawingArea::on_size_allocate(a);
633         
634         _black_note_width = floor(0.7 * get_width()) + 0.5f;
635 }
636
637 void
638 PianoRollHeader::send_note_on(uint8_t note) {
639         boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
640
641         //cerr << "note on: " << (int) note << endl;
642
643         if(track) {
644                 _event[0] = MIDI_CMD_NOTE_ON;
645                 _event[1] = note;
646                 _event[2] = 100;
647
648                 track->write_immediate_event(3, _event);
649         }
650 }
651
652 void
653 PianoRollHeader::send_note_off(uint8_t note) {
654         boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
655
656         if(track) {
657                 _event[0] = MIDI_CMD_NOTE_OFF;
658                 _event[1] = note;
659                 _event[2] = 100;
660
661                 track->write_immediate_event(3, _event);
662         }
663 }