Remove idiocy.
[ardour2.git] / gtk2_ardour / piano_roll_header.cc
blobcfdf2bcd67f3ba078093eadf16bcbcb87766ec54
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 "piano_roll_header.h"
24 #include "midi_time_axis.h"
25 #include "midi_streamview.h"
27 const int no_note = 0xff;
29 using namespace std;
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);
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);
41 PianoRollHeader::Color::Color()
42 : r(1.0f)
43 , g(1.0f)
44 , b(1.0f)
48 PianoRollHeader::Color::Color(double _r, double _g, double _b)
49 : r(_r)
50 , g(_g)
51 , b(_b)
55 inline void
56 PianoRollHeader::Color::set(const PianoRollHeader::Color& c)
58 r = c.r;
59 g = c.g;
60 b = c.b;
63 PianoRollHeader::PianoRollHeader(MidiStreamView& v)
64 : _view(v)
65 , _highlighted_note(no_note)
66 , _clicked_note(no_note)
67 , _dragging(false)
69 add_events (Gdk::BUTTON_PRESS_MASK |
70 Gdk::BUTTON_RELEASE_MASK |
71 Gdk::POINTER_MOTION_MASK |
72 Gdk::ENTER_NOTIFY_MASK |
73 Gdk::LEAVE_NOTIFY_MASK |
74 Gdk::SCROLL_MASK);
76 for (int i = 0; i < 128; ++i) {
77 _active_notes[i] = false;
80 _view.NoteRangeChanged.connect (mem_fun (*this, &PianoRollHeader::note_range_changed));
83 inline void
84 create_path(Cairo::RefPtr<Cairo::Context> cr, double x[], double y[], int start, int stop)
86 cr->move_to(x[start], y[start]);
88 for (int i = start+1; i <= stop; ++i) {
89 cr->line_to(x[i], y[i]);
93 inline void
94 render_rect(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
95 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
97 cr->set_source_rgb(bg.r, bg.g, bg.b);
98 create_path(cr, x, y, 0, 4);
99 cr->fill();
101 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
102 create_path(cr, x, y, 0, 2);
103 cr->stroke();
105 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
106 create_path(cr, x, y, 2, 4);
107 cr->stroke();
110 inline void
111 render_cf(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
112 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
114 cr->set_source_rgb(bg.r, bg.g, bg.b);
115 create_path(cr, x, y, 0, 6);
116 cr->fill();
118 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
119 create_path(cr, x, y, 0, 4);
120 cr->stroke();
122 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
123 create_path(cr, x, y, 4, 6);
124 cr->stroke();
127 inline void
128 render_eb(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
129 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
131 cr->set_source_rgb(bg.r, bg.g, bg.b);
132 create_path(cr, x, y, 0, 6);
133 cr->fill();
135 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
136 create_path(cr, x, y, 0, 2);
137 cr->stroke();
138 create_path(cr, x, y, 4, 5);
139 cr->stroke();
141 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
142 create_path(cr, x, y, 2, 4);
143 cr->stroke();
144 create_path(cr, x, y, 5, 6);
145 cr->stroke();
148 inline void
149 render_dga(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
150 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
152 cr->set_source_rgb(bg.r, bg.g, bg.b);
153 create_path(cr, x, y, 0, 8);
154 cr->fill();
156 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
157 create_path(cr, x, y, 0, 4);
158 cr->stroke();
159 create_path(cr, x, y, 6, 7);
160 cr->stroke();
162 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
163 create_path(cr, x, y, 4, 6);
164 cr->stroke();
165 create_path(cr, x, y, 7, 8);
166 cr->stroke();
169 void
170 PianoRollHeader::get_path(PianoRollHeader::ItemType note_type, int note, double x[], double y[])
172 double y_pos = floor(_view.note_to_y(note)) - 0.5f;
173 double note_height;
174 double other_y1 = floor(_view.note_to_y(note+1)) + floor(_note_height / 2.0f) + 0.5f;
175 double other_y2 = floor(_view.note_to_y(note-1)) + floor(_note_height / 2.0f) - 1.0f;
176 double width = get_width();
178 if (note == 0) {
179 note_height = floor(_view.contents_height()) - y_pos;
180 } else {
181 note_height = floor(_view.note_to_y(note - 1)) - y_pos;
184 switch (note_type) {
185 case BLACK_SEPARATOR:
186 x[0] = 1.5f;
187 y[0] = y_pos;
188 x[1] = _black_note_width;
189 y[1] = y_pos;
190 break;
191 case BLACK_MIDDLE_SEPARATOR:
192 x[0] = _black_note_width;
193 y[0] = y_pos + floor(_note_height / 2.0f);
194 x[1] = width - 1.0f;
195 y[1] = y[0];
196 break;
197 case BLACK:
198 x[0] = 1.5f;
199 y[0] = y_pos + note_height - 0.5f;
200 x[1] = 1.5f;
201 y[1] = y_pos + 1.0f;
202 x[2] = _black_note_width;
203 y[2] = y_pos + 1.0f;
204 x[3] = _black_note_width;
205 y[3] = y_pos + note_height - 0.5f;
206 x[4] = 1.5f;
207 y[4] = y_pos + note_height - 0.5f;
208 return;
209 case WHITE_SEPARATOR:
210 x[0] = 1.5f;
211 y[0] = y_pos;
212 x[1] = width - 1.5f;
213 y[1] = y_pos;
214 break;
215 case WHITE_RECT:
216 x[0] = 1.5f;
217 y[0] = y_pos + note_height - 0.5f;
218 x[1] = 1.5f;
219 y[1] = y_pos + 1.0f;
220 x[2] = width - 1.5f;
221 y[2] = y_pos + 1.0f;
222 x[3] = width - 1.5f;
223 y[3] = y_pos + note_height - 0.5f;
224 x[4] = 1.5f;
225 y[4] = y_pos + note_height - 0.5f;
226 return;
227 case WHITE_CF:
228 x[0] = 1.5f;
229 y[0] = y_pos + note_height - 1.5f;
230 x[1] = 1.5f;
231 y[1] = y_pos + 1.0f;
232 x[2] = _black_note_width + 1.0f;
233 y[2] = y_pos + 1.0f;
234 x[3] = _black_note_width + 1.0f;
235 y[3] = other_y1;
236 x[4] = width - 1.5f;
237 y[4] = other_y1;
238 x[5] = width - 1.5f;
239 y[5] = y_pos + note_height - 1.5f;
240 x[6] = 1.5f;
241 y[6] = y_pos + note_height - 1.5f;
242 return;
243 case WHITE_EB:
244 x[0] = 1.5f;
245 y[0] = y_pos + note_height - 1.5f;
246 x[1] = 1.5f;
247 y[1] = y_pos + 1.0f;
248 x[2] = width - 1.5f;
249 y[2] = y_pos + 1.0f;
250 x[3] = width - 1.5f;
251 y[3] = other_y2;
252 x[4] = _black_note_width + 1.0f;
253 y[4] = other_y2;
254 x[5] = _black_note_width + 1.0f;
255 y[5] = y_pos + note_height - 1.5f;
256 x[6] = 1.5f;
257 y[6] = y_pos + note_height - 1.5f;
258 return;
259 case WHITE_DGA:
260 x[0] = 1.5f;
261 y[0] = y_pos + note_height - 1.5f;
262 x[1] = 1.5f;
263 y[1] = y_pos + 1.0f;
264 x[2] = _black_note_width + 1.0f;
265 y[2] = y_pos + 1.0f;
266 x[3] = _black_note_width + 1.0f;
267 y[3] = other_y1;
268 x[4] = width - 1.5f;
269 y[4] = other_y1;
270 x[5] = width - 1.5f;
271 y[5] = other_y2;
272 x[6] = _black_note_width + 1.0f;
273 y[6] = other_y2;
274 x[7] = _black_note_width + 1.0f;
275 y[7] = y_pos + note_height - 1.5f;
276 x[8] = 1.5f;
277 y[8] = y_pos + note_height - 1.5f;
278 return;
279 default:
280 return;
284 bool
285 PianoRollHeader::on_expose_event (GdkEventExpose* ev)
287 GdkRectangle& rect = ev->area;
288 double font_size;
289 int lowest, highest;
290 Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context();
291 Cairo::RefPtr<Cairo::LinearGradient> pat = Cairo::LinearGradient::create(0, 0, _black_note_width, 0);
292 double x[9];
293 double y[9];
294 Color bg, tl_shadow, br_shadow;
295 int oct_rel;
296 int y1 = max(rect.y, 0);
297 int y2 = min(rect.y + rect.height, (int) floor(_view.contents_height() - 1.0f));
299 //Cairo::TextExtents te;
300 lowest = max(_view.lowest_note(), _view.y_to_note(y2));
301 highest = min(_view.highest_note(), _view.y_to_note(y1));
303 if (lowest > 127) {
304 lowest = 0;
307 cr->select_font_face ("Georgia", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD);
308 font_size = min((double) 10.0f, _note_height - 4.0f);
309 cr->set_font_size(font_size);
311 /* fill the entire rect with the color for non-highlighted white notes.
312 * then we won't have to draw the background for those notes,
313 * and would only have to draw the background for the one highlighted white note*/
314 //cr->rectangle(rect.x, rect.y, rect.width, rect.height);
315 //cr->set_source_rgb(white.r, white.g, white.b);
316 //cr->fill();
318 cr->set_line_width(1.0f);
320 /* draw vertical lines with shade at both ends of the widget */
321 cr->set_source_rgb(0.0f, 0.0f, 0.0f);
322 cr->move_to(0.5f, rect.y);
323 cr->line_to(0.5f, rect.y + rect.height);
324 cr->stroke();
325 cr->move_to(get_width() - 0.5f, rect.y);
326 cr->line_to(get_width() - 0.5f, rect.y + rect.height);
327 cr->stroke();
329 //pat->add_color_stop_rgb(0.0, 0.33, 0.33, 0.33);
330 //pat->add_color_stop_rgb(0.2, 0.39, 0.39, 0.39);
331 //pat->add_color_stop_rgb(1.0, 0.22, 0.22, 0.22);
332 //cr->set_source(pat);
334 for (int i = lowest; i <= highest; ++i) {
335 oct_rel = i % 12;
337 switch (oct_rel) {
338 case 1:
339 case 3:
340 case 6:
341 case 8:
342 case 10:
343 /* black note */
344 if (i == _highlighted_note) {
345 bg.set(black_highlight);
346 } else {
347 bg.set(black);
350 if (_active_notes[i]) {
351 tl_shadow.set(black_shade_dark);
352 br_shadow.set(black_shade_light);
353 } else {
354 tl_shadow.set(black_shade_light);
355 br_shadow.set(black_shade_dark);
358 /* draw black separators */
359 cr->set_source_rgb(0.0f, 0.0f, 0.0f);
360 get_path(BLACK_SEPARATOR, i, x, y);
361 create_path(cr, x, y, 0, 1);
362 cr->stroke();
364 get_path(BLACK_MIDDLE_SEPARATOR, i, x, y);
365 create_path(cr, x, y, 0, 1);
366 cr->stroke();
368 get_path(BLACK, i, x, y);
369 render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
370 break;
372 default:
373 /* white note */
374 if (i == _highlighted_note) {
375 bg.set(white_highlight);
376 } else {
377 bg.set(white);
380 if (_active_notes[i]) {
381 tl_shadow.set(white_shade_dark);
382 br_shadow.set(white_shade_light);
383 } else {
384 tl_shadow.set(white_shade_light);
385 br_shadow.set(white_shade_dark);
388 switch(oct_rel) {
389 case 0:
390 case 5:
391 if (i == _view.highest_note()) {
392 get_path(WHITE_RECT, i, x, y);
393 render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
394 } else {
395 get_path(WHITE_CF, i, x, y);
396 render_cf(cr, i, x, y, bg, tl_shadow, br_shadow);
398 break;
400 case 2:
401 case 7:
402 case 9:
403 if (i == _view.highest_note()) {
404 get_path(WHITE_EB, i, x, y);
405 render_eb(cr, i, x, y, bg, tl_shadow, br_shadow);
406 } else if (i == _view.lowest_note()) {
407 get_path(WHITE_CF, i, x, y);
408 render_cf(cr, i, x, y, bg, tl_shadow, br_shadow);
409 } else {
410 get_path(WHITE_DGA, i, x, y);
411 render_dga(cr, i, x, y, bg, tl_shadow, br_shadow);
413 break;
415 case 4:
416 case 11:
417 cr->set_source_rgb(0.0f, 0.0f, 0.0f);
418 get_path(WHITE_SEPARATOR, i, x, y);
419 create_path(cr, x, y, 0, 1);
420 cr->stroke();
422 if (i == _view.lowest_note()) {
423 get_path(WHITE_RECT, i, x, y);
424 render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
425 } else {
426 get_path(WHITE_EB, i, x, y);
427 render_eb(cr, i, x, y, bg, tl_shadow, br_shadow);
429 break;
431 default:
432 break;
435 break;
439 /* render the name of which C this is */
440 if (oct_rel == 0) {
441 std::stringstream s;
442 double y = floor(_view.note_to_y(i)) - 0.5f;
443 double note_height = floor(_view.note_to_y(i - 1)) - y;
445 int cn = i / 12;
446 s << "C" << cn;
448 //cr->get_text_extents(s.str(), te);
449 cr->set_source_rgb(0.30f, 0.30f, 0.30f);
450 cr->move_to(2.0f, y + note_height - 1.0f - (note_height - font_size) / 2.0f);
451 cr->show_text(s.str());
455 return true;
458 bool
459 PianoRollHeader::on_motion_notify_event (GdkEventMotion* ev)
461 int note = _view.y_to_note(ev->y);
463 if (_highlighted_note != no_note) {
464 if (note > _highlighted_note) {
465 invalidate_note_range(_highlighted_note, note);
466 } else {
467 invalidate_note_range(note, _highlighted_note);
470 _highlighted_note = note;
473 /* redraw already taken care of above */
474 if (_clicked_note != no_note && _clicked_note != note) {
475 _active_notes[_clicked_note] = false;
476 send_note_off(_clicked_note);
478 _clicked_note = note;
480 if (!_active_notes[note]) {
481 _active_notes[note] = true;
482 send_note_on(note);
486 //win->process_updates(false);
488 return true;
491 bool
492 PianoRollHeader::on_button_press_event (GdkEventButton* ev)
494 int note = _view.y_to_note(ev->y);
496 if (ev->type == GDK_BUTTON_PRESS && note >= 0 && note < 128) {
497 add_modal_grab();
498 _dragging = true;
500 if (!_active_notes[note]) {
501 _active_notes[note] = true;
502 _clicked_note = note;
503 send_note_on(note);
505 invalidate_note_range(note, note);
506 } else {
507 _clicked_note = no_note;
511 return true;
514 bool
515 PianoRollHeader::on_button_release_event (GdkEventButton* ev)
517 int note = _view.y_to_note(ev->y);
519 if (_dragging) {
520 remove_modal_grab();
521 _dragging = false;
523 if (note == _clicked_note) {
524 _active_notes[note] = false;
525 _clicked_note = no_note;
526 send_note_off(note);
528 invalidate_note_range(note, note);
532 return true;
535 bool
536 PianoRollHeader::on_enter_notify_event (GdkEventCrossing* ev)
538 _highlighted_note = _view.y_to_note(ev->y);
540 invalidate_note_range(_highlighted_note, _highlighted_note);
541 return true;
544 bool
545 PianoRollHeader::on_leave_notify_event (GdkEventCrossing*)
547 invalidate_note_range(_highlighted_note, _highlighted_note);
549 if (_clicked_note != no_note) {
550 _active_notes[_clicked_note] = false;
551 send_note_off(_clicked_note);
553 if (_clicked_note != _highlighted_note) {
554 invalidate_note_range(_clicked_note, _clicked_note);
557 _clicked_note = no_note;
560 _highlighted_note = no_note;
561 return true;
564 bool
565 PianoRollHeader::on_scroll_event (GdkEventScroll*)
567 return true;
570 void
571 PianoRollHeader::note_range_changed()
573 _note_height = floor(_view.note_height()) + 0.5f;
575 queue_draw();
577 Glib::RefPtr<Gdk::Window> win = get_window();
579 if (win) {
580 win->process_updates(false);
584 void
585 PianoRollHeader::invalidate_note_range(int lowest, int highest)
587 Glib::RefPtr<Gdk::Window> win = get_window();
588 Gdk::Rectangle rect;
590 // the non-rectangular geometry of some of the notes requires more
591 // redraws than the notes that actually changed.
592 switch(lowest % 12) {
593 case 0:
594 case 5:
595 lowest = max((int) _view.lowest_note(), lowest);
596 break;
597 default:
598 lowest = max((int) _view.lowest_note(), lowest - 1);
599 break;
602 switch(highest % 12) {
603 case 4:
604 case 11:
605 highest = min((int) _view.highest_note(), highest);
606 break;
607 case 1:
608 case 3:
609 case 6:
610 case 8:
611 case 10:
612 highest = min((int) _view.highest_note(), highest + 1);
613 break;
614 default:
615 highest = min((int) _view.highest_note(), highest + 2);
616 break;
619 double y = _view.note_to_y(highest);
620 double height = _view.note_to_y(lowest - 1) - y;
622 rect.set_x(0);
623 rect.set_width(get_width());
624 rect.set_y((int) floor(y));
625 rect.set_height((int) floor(height));
627 if (win) {
628 win->invalidate_rect(rect, false);
632 void
633 PianoRollHeader::on_size_request(Gtk::Requisition* r)
635 r->width = 20;
638 void
639 PianoRollHeader::on_size_allocate(Gtk::Allocation& a)
641 DrawingArea::on_size_allocate(a);
643 _black_note_width = floor(0.7 * get_width()) + 0.5f;
646 void
647 PianoRollHeader::send_note_on(uint8_t note)
649 boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
651 //cerr << "note on: " << (int) note << endl;
653 if (track) {
654 _event[0] = (MIDI_CMD_NOTE_ON | track->default_channel());
655 _event[1] = note;
656 _event[2] = 100;
658 track->write_immediate_event(3, _event);
662 void
663 PianoRollHeader::send_note_off(uint8_t note)
665 boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
667 if (track) {
668 _event[0] = (MIDI_CMD_NOTE_OFF | track->default_channel());
669 _event[1] = note;
670 _event[2] = 100;
672 track->write_immediate_event(3, _event);