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