fix up file renaming code a little bit
[ArdourMidi.git] / gtk2_ardour / piano_roll_header.cc
blobe88f551e91ff0853a3989c60fa8837b73ae70c36
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;
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 _clicked_note = no_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 _active_notes[note] = false;
551 _clicked_note = no_note;
552 send_note_off(note);
554 invalidate_note_range(note, note);
559 return true;
562 bool
563 PianoRollHeader::on_enter_notify_event (GdkEventCrossing* ev)
565 _highlighted_note = _view.y_to_note(ev->y);
567 invalidate_note_range(_highlighted_note, _highlighted_note);
568 return true;
571 bool
572 PianoRollHeader::on_leave_notify_event (GdkEventCrossing*)
574 invalidate_note_range(_highlighted_note, _highlighted_note);
576 if (_clicked_note != no_note) {
577 _active_notes[_clicked_note] = false;
578 send_note_off(_clicked_note);
580 if (_clicked_note != _highlighted_note) {
581 invalidate_note_range(_clicked_note, _clicked_note);
584 _clicked_note = no_note;
587 _highlighted_note = no_note;
588 return true;
591 bool
592 PianoRollHeader::on_scroll_event (GdkEventScroll*)
594 return true;
597 void
598 PianoRollHeader::note_range_changed()
600 _note_height = floor(_view.note_height()) + 0.5f;
602 queue_draw();
604 Glib::RefPtr<Gdk::Window> win = get_window();
606 if (win) {
607 win->process_updates(false);
611 void
612 PianoRollHeader::invalidate_note_range(int lowest, int highest)
614 Glib::RefPtr<Gdk::Window> win = get_window();
615 Gdk::Rectangle rect;
617 // the non-rectangular geometry of some of the notes requires more
618 // redraws than the notes that actually changed.
619 switch(lowest % 12) {
620 case 0:
621 case 5:
622 lowest = max((int) _view.lowest_note(), lowest);
623 break;
624 default:
625 lowest = max((int) _view.lowest_note(), lowest - 1);
626 break;
629 switch(highest % 12) {
630 case 4:
631 case 11:
632 highest = min((int) _view.highest_note(), highest);
633 break;
634 case 1:
635 case 3:
636 case 6:
637 case 8:
638 case 10:
639 highest = min((int) _view.highest_note(), highest + 1);
640 break;
641 default:
642 highest = min((int) _view.highest_note(), highest + 2);
643 break;
646 double y = _view.note_to_y(highest);
647 double height = _view.note_to_y(lowest - 1) - y;
649 rect.set_x(0);
650 rect.set_width(get_width());
651 rect.set_y((int) floor(y));
652 rect.set_height((int) floor(height));
654 if (win) {
655 win->invalidate_rect(rect, false);
659 void
660 PianoRollHeader::on_size_request(Gtk::Requisition* r)
662 r->width = 20;
665 void
666 PianoRollHeader::on_size_allocate(Gtk::Allocation& a)
668 DrawingArea::on_size_allocate(a);
670 _black_note_width = floor(0.7 * get_width()) + 0.5f;
673 void
674 PianoRollHeader::send_note_on(uint8_t note)
676 boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
678 //cerr << "note on: " << (int) note << endl;
680 if (track) {
681 _event[0] = (MIDI_CMD_NOTE_ON | track->default_channel());
682 _event[1] = note;
683 _event[2] = 100;
685 track->write_immediate_event(3, _event);
689 void
690 PianoRollHeader::send_note_off(uint8_t note)
692 boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
694 if (track) {
695 _event[0] = (MIDI_CMD_NOTE_OFF | track->default_channel());
696 _event[1] = note;
697 _event[2] = 100;
699 track->write_immediate_event(3, _event);