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 <ardour/midi_track.h>
21 #include "piano_roll_header.h"
22 #include "midi_time_axis.h"
23 #include "midi_streamview.h"
27 const int no_note = 0xff;
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()
47 PianoRollHeader::Color::Color(double _r, double _g, double _b)
54 PianoRollHeader::Color::set(const PianoRollHeader::Color& c) {
60 PianoRollHeader::PianoRollHeader(MidiStreamView& v)
62 , _highlighted_note(no_note)
63 , _clicked_note(no_note)
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 |
73 for(int i = 0; i < 128; ++i) {
74 _active_notes[i] = false;
77 _view.NoteRangeChanged.connect (mem_fun (*this, &PianoRollHeader::note_range_changed));
81 create_path(Cairo::RefPtr<Cairo::Context> cr, double x[], double y[], int start, int stop) {
82 cr->move_to(x[start], y[start]);
84 for(int i = start+1; i <= stop; ++i) {
85 cr->line_to(x[i], y[i]);
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);
96 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
97 create_path(cr, x, y, 0, 2);
100 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
101 create_path(cr, x, y, 2, 4);
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);
112 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
113 create_path(cr, x, y, 0, 4);
116 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
117 create_path(cr, x, y, 4, 6);
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);
128 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
129 create_path(cr, x, y, 0, 2);
131 create_path(cr, x, y, 4, 5);
134 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
135 create_path(cr, x, y, 2, 4);
137 create_path(cr, x, y, 5, 6);
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);
148 cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
149 create_path(cr, x, y, 0, 4);
151 create_path(cr, x, y, 6, 7);
154 cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
155 create_path(cr, x, y, 4, 6);
157 create_path(cr, x, y, 7, 8);
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;
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();
170 note_height = floor(_view.contents_height()) - y_pos;
173 note_height = floor(_view.note_to_y(note - 1)) - y_pos;
177 case BLACK_SEPARATOR:
180 x[1] = _black_note_width;
183 case BLACK_MIDDLE_SEPARATOR:
184 x[0] = _black_note_width;
185 y[0] = y_pos + floor(_note_height / 2.0f);
191 y[0] = y_pos + note_height - 0.5f;
194 x[2] = _black_note_width;
196 x[3] = _black_note_width;
197 y[3] = y_pos + note_height - 0.5f;
199 y[4] = y_pos + note_height - 0.5f;
201 case WHITE_SEPARATOR:
209 y[0] = y_pos + note_height - 0.5f;
215 y[3] = y_pos + note_height - 0.5f;
217 y[4] = y_pos + note_height - 0.5f;
221 y[0] = y_pos + note_height - 1.5f;
224 x[2] = _black_note_width + 1.0f;
226 x[3] = _black_note_width + 1.0f;
231 y[5] = y_pos + note_height - 1.5f;
233 y[6] = y_pos + note_height - 1.5f;
237 y[0] = y_pos + note_height - 1.5f;
244 x[4] = _black_note_width + 1.0f;
246 x[5] = _black_note_width + 1.0f;
247 y[5] = y_pos + note_height - 1.5f;
249 y[6] = y_pos + note_height - 1.5f;
253 y[0] = y_pos + note_height - 1.5f;
256 x[2] = _black_note_width + 1.0f;
258 x[3] = _black_note_width + 1.0f;
264 x[6] = _black_note_width + 1.0f;
266 x[7] = _black_note_width + 1.0f;
267 y[7] = y_pos + note_height - 1.5f;
269 y[8] = y_pos + note_height - 1.5f;
277 PianoRollHeader::on_expose_event (GdkEventExpose* ev) {
278 GdkRectangle& rect = ev->area;
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);
285 Color bg, tl_shadow, br_shadow;
287 int y1 = max(rect.y, 0);
288 int y2 = min(rect.y + rect.height, (int) floor(_view.contents_height() - 1.0f));
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));
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);
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);
309 cr->set_line_width(1.0f);
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);
316 cr->move_to(get_width() - 0.5f, rect.y);
317 cr->line_to(get_width() - 0.5f, rect.y + rect.height);
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);
325 for(int i = lowest; i <= highest; ++i) {
335 if(i == _highlighted_note) {
336 bg.set(black_highlight);
342 if(_active_notes[i]) {
343 tl_shadow.set(black_shade_dark);
344 br_shadow.set(black_shade_light);
347 tl_shadow.set(black_shade_light);
348 br_shadow.set(black_shade_dark);
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);
357 get_path(BLACK_MIDDLE_SEPARATOR, i, x, y);
358 create_path(cr, x, y, 0, 1);
361 get_path(BLACK, i, x, y);
362 render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
367 if(i == _highlighted_note) {
368 bg.set(white_highlight);
374 if(_active_notes[i]) {
375 tl_shadow.set(white_shade_dark);
376 br_shadow.set(white_shade_light);
379 tl_shadow.set(white_shade_light);
380 br_shadow.set(white_shade_dark);
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);
391 get_path(WHITE_CF, i, x, y);
392 render_cf(cr, i, x, y, bg, tl_shadow, br_shadow);
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);
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);
408 get_path(WHITE_DGA, i, x, y);
409 render_dga(cr, i, x, y, bg, tl_shadow, br_shadow);
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);
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);
425 get_path(WHITE_EB, i, x, y);
426 render_eb(cr, i, x, y, bg, tl_shadow, br_shadow);
438 /* render the name of which C this is */
441 double y = floor(_view.note_to_y(i)) - 0.5f;
442 double note_height = floor(_view.note_to_y(i - 1)) - y;
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());
458 PianoRollHeader::on_motion_notify_event (GdkEventMotion* ev) {
459 int note = _view.y_to_note(ev->y);
461 if(_highlighted_note != no_note) {
462 if(note > _highlighted_note) {
463 invalidate_note_range(_highlighted_note, note);
466 invalidate_note_range(note, _highlighted_note);
469 _highlighted_note = note;
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);
477 _clicked_note = note;
479 if(!_active_notes[note]) {
480 _active_notes[note] = true;
485 //win->process_updates(false);
491 PianoRollHeader::on_button_press_event (GdkEventButton* ev) {
492 int note = _view.y_to_note(ev->y);
494 if(ev->type == GDK_BUTTON_PRESS && note >= 0 && note < 128) {
498 if(!_active_notes[note]) {
499 _active_notes[note] = true;
500 _clicked_note = note;
503 invalidate_note_range(note, note);
506 _clicked_note = no_note;
514 PianoRollHeader::on_button_release_event (GdkEventButton* ev) {
515 int note = _view.y_to_note(ev->y);
521 if(note == _clicked_note) {
522 _active_notes[note] = false;
523 _clicked_note = no_note;
526 invalidate_note_range(note, note);
534 PianoRollHeader::on_enter_notify_event (GdkEventCrossing* ev) {
535 _highlighted_note = _view.y_to_note(ev->y);
537 invalidate_note_range(_highlighted_note, _highlighted_note);
542 PianoRollHeader::on_leave_notify_event (GdkEventCrossing*) {
543 invalidate_note_range(_highlighted_note, _highlighted_note);
545 if(_clicked_note != no_note) {
546 _active_notes[_clicked_note] = false;
547 send_note_off(_clicked_note);
549 if(_clicked_note != _highlighted_note) {
550 invalidate_note_range(_clicked_note, _clicked_note);
553 _clicked_note = no_note;
556 _highlighted_note = no_note;
561 PianoRollHeader::on_scroll_event (GdkEventScroll* ev) {
566 PianoRollHeader::note_range_changed() {
567 _note_height = floor(_view.note_height()) + 0.5f;
571 Glib::RefPtr<Gdk::Window> win = get_window();
574 win->process_updates(false);
579 PianoRollHeader::invalidate_note_range(int lowest, int highest) {
580 Glib::RefPtr<Gdk::Window> win = get_window();
583 // the non-rectangular geometry of some of the notes requires more
584 // redraws than the notes that actually changed.
585 switch(lowest % 12) {
588 lowest = max((int) _view.lowest_note(), lowest);
591 lowest = max((int) _view.lowest_note(), lowest - 1);
595 switch(highest % 12) {
598 highest = min((int) _view.highest_note(), highest);
605 highest = min((int) _view.highest_note(), highest + 1);
608 highest = min((int) _view.highest_note(), highest + 2);
612 double y = _view.note_to_y(highest);
613 double height = _view.note_to_y(lowest - 1) - y;
616 rect.set_width(get_width());
617 rect.set_y((int) floor(y));
618 rect.set_height((int) floor(height));
621 win->invalidate_rect(rect, false);
626 PianoRollHeader::on_size_request(Gtk::Requisition* r) {
631 PianoRollHeader::on_size_allocate(Gtk::Allocation& a) {
632 DrawingArea::on_size_allocate(a);
634 _black_note_width = floor(0.7 * get_width()) + 0.5f;
638 PianoRollHeader::send_note_on(uint8_t note) {
639 boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
641 //cerr << "note on: " << (int) note << endl;
644 _event[0] = MIDI_CMD_NOTE_ON;
648 track->write_immediate_event(3, _event);
653 PianoRollHeader::send_note_off(uint8_t note) {
654 boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
657 _event[0] = MIDI_CMD_NOTE_OFF;
661 track->write_immediate_event(3, _event);