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.
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;
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()
51 PianoRollHeader::Color::Color(double _r
, double _g
, double _b
)
59 PianoRollHeader::Color::set(const PianoRollHeader::Color
& c
)
66 PianoRollHeader::PianoRollHeader(MidiStreamView
& v
)
68 , _highlighted_note(no_note
)
69 , _clicked_note(no_note
)
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
|
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
));
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
]);
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);
104 cr
->set_source_rgb(tl_shadow
.r
, tl_shadow
.g
, tl_shadow
.b
);
105 create_path(cr
, x
, y
, 0, 2);
108 cr
->set_source_rgb(br_shadow
.r
, br_shadow
.g
, br_shadow
.b
);
109 create_path(cr
, x
, y
, 2, 4);
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);
121 cr
->set_source_rgb(tl_shadow
.r
, tl_shadow
.g
, tl_shadow
.b
);
122 create_path(cr
, x
, y
, 0, 4);
125 cr
->set_source_rgb(br_shadow
.r
, br_shadow
.g
, br_shadow
.b
);
126 create_path(cr
, x
, y
, 4, 6);
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);
138 cr
->set_source_rgb(tl_shadow
.r
, tl_shadow
.g
, tl_shadow
.b
);
139 create_path(cr
, x
, y
, 0, 2);
141 create_path(cr
, x
, y
, 4, 5);
144 cr
->set_source_rgb(br_shadow
.r
, br_shadow
.g
, br_shadow
.b
);
145 create_path(cr
, x
, y
, 2, 4);
147 create_path(cr
, x
, y
, 5, 6);
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);
159 cr
->set_source_rgb(tl_shadow
.r
, tl_shadow
.g
, tl_shadow
.b
);
160 create_path(cr
, x
, y
, 0, 4);
162 create_path(cr
, x
, y
, 6, 7);
165 cr
->set_source_rgb(br_shadow
.r
, br_shadow
.g
, br_shadow
.b
);
166 create_path(cr
, x
, y
, 4, 6);
168 create_path(cr
, x
, y
, 7, 8);
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
;
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();
182 note_height
= floor(_view
.contents_height()) - y_pos
;
184 note_height
= floor(_view
.note_to_y(note
- 1)) - y_pos
;
188 case BLACK_SEPARATOR
:
191 x
[1] = _black_note_width
;
194 case BLACK_MIDDLE_SEPARATOR
:
195 x
[0] = _black_note_width
;
196 y
[0] = y_pos
+ floor(_note_height
/ 2.0f
);
202 y
[0] = y_pos
+ note_height
- 0.5f
;
205 x
[2] = _black_note_width
;
207 x
[3] = _black_note_width
;
208 y
[3] = y_pos
+ note_height
- 0.5f
;
210 y
[4] = y_pos
+ note_height
- 0.5f
;
212 case WHITE_SEPARATOR
:
220 y
[0] = y_pos
+ note_height
- 0.5f
;
226 y
[3] = y_pos
+ note_height
- 0.5f
;
228 y
[4] = y_pos
+ note_height
- 0.5f
;
232 y
[0] = y_pos
+ note_height
- 1.5f
;
235 x
[2] = _black_note_width
+ 1.0f
;
237 x
[3] = _black_note_width
+ 1.0f
;
242 y
[5] = y_pos
+ note_height
- 1.5f
;
244 y
[6] = y_pos
+ note_height
- 1.5f
;
248 y
[0] = y_pos
+ note_height
- 1.5f
;
255 x
[4] = _black_note_width
+ 1.0f
;
257 x
[5] = _black_note_width
+ 1.0f
;
258 y
[5] = y_pos
+ note_height
- 1.5f
;
260 y
[6] = y_pos
+ note_height
- 1.5f
;
264 y
[0] = y_pos
+ note_height
- 1.5f
;
267 x
[2] = _black_note_width
+ 1.0f
;
269 x
[3] = _black_note_width
+ 1.0f
;
275 x
[6] = _black_note_width
+ 1.0f
;
277 x
[7] = _black_note_width
+ 1.0f
;
278 y
[7] = y_pos
+ note_height
- 1.5f
;
280 y
[8] = y_pos
+ note_height
- 1.5f
;
288 PianoRollHeader::on_expose_event (GdkEventExpose
* ev
)
290 GdkRectangle
& rect
= ev
->area
;
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);
297 Color bg
, tl_shadow
, br_shadow
;
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
));
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);
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
);
328 cr
->move_to(get_width() - 0.5f
, rect
.y
);
329 cr
->line_to(get_width() - 0.5f
, rect
.y
+ rect
.height
);
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
) {
347 if (i
== _highlighted_note
) {
348 bg
.set(black_highlight
);
353 if (_active_notes
[i
]) {
354 tl_shadow
.set(black_shade_dark
);
355 br_shadow
.set(black_shade_light
);
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);
367 get_path(BLACK_MIDDLE_SEPARATOR
, i
, x
, y
);
368 create_path(cr
, x
, y
, 0, 1);
371 get_path(BLACK
, i
, x
, y
);
372 render_rect(cr
, i
, x
, y
, bg
, tl_shadow
, br_shadow
);
377 if (i
== _highlighted_note
) {
378 bg
.set(white_highlight
);
383 if (_active_notes
[i
]) {
384 tl_shadow
.set(white_shade_dark
);
385 br_shadow
.set(white_shade_light
);
387 tl_shadow
.set(white_shade_light
);
388 br_shadow
.set(white_shade_dark
);
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
);
398 get_path(WHITE_CF
, i
, x
, y
);
399 render_cf(cr
, i
, x
, y
, bg
, tl_shadow
, br_shadow
);
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
);
413 get_path(WHITE_DGA
, i
, x
, y
);
414 render_dga(cr
, i
, x
, y
, bg
, tl_shadow
, br_shadow
);
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);
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
);
429 get_path(WHITE_EB
, i
, x
, y
);
430 render_eb(cr
, i
, x
, y
, bg
, tl_shadow
, br_shadow
);
442 /* render the name of which C this is */
445 double y
= floor(_view
.note_to_y(i
)) - 0.5f
;
446 double note_height
= floor(_view
.note_to_y(i
- 1)) - y
;
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());
462 PianoRollHeader::on_motion_notify_event (GdkEventMotion
* ev
)
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
);
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;
492 //win->process_updates(false);
498 PianoRollHeader::on_button_press_event (GdkEventButton
* ev
)
500 int note
= _view
.y_to_note(ev
->y
);
502 if (ev
->button
== 2) {
504 /* relax till release */
507 if (ev
->type
== GDK_BUTTON_PRESS
&& note
>= 0 && note
< 128) {
512 if (!_active_notes
[note
]) {
513 _active_notes
[note
] = true;
514 _clicked_note
= note
;
517 invalidate_note_range(note
, note
);
519 reset_clicked_note(note
);
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
549 if (note
== _clicked_note
) {
550 reset_clicked_note(note
);
559 PianoRollHeader::on_enter_notify_event (GdkEventCrossing
* ev
)
561 _highlighted_note
= _view
.y_to_note(ev
->y
);
563 invalidate_note_range(_highlighted_note
, _highlighted_note
);
568 PianoRollHeader::on_leave_notify_event (GdkEventCrossing
*)
570 invalidate_note_range(_highlighted_note
, _highlighted_note
);
572 if (_clicked_note
!= no_note
) {
573 reset_clicked_note(_clicked_note
, _clicked_note
!= _highlighted_note
);
576 _highlighted_note
= no_note
;
581 PianoRollHeader::on_scroll_event (GdkEventScroll
*)
587 PianoRollHeader::note_range_changed()
589 _note_height
= floor(_view
.note_height()) + 0.5f
;
594 PianoRollHeader::invalidate_note_range(int lowest
, int highest
)
596 Glib::RefPtr
<Gdk::Window
> win
= get_window();
599 // the non-rectangular geometry of some of the notes requires more
600 // redraws than the notes that actually changed.
601 switch(lowest
% 12) {
604 lowest
= max((int) _view
.lowest_note(), lowest
);
607 lowest
= max((int) _view
.lowest_note(), lowest
- 1);
611 switch(highest
% 12) {
614 highest
= min((int) _view
.highest_note(), highest
);
621 highest
= min((int) _view
.highest_note(), highest
+ 1);
624 highest
= min((int) _view
.highest_note(), highest
+ 2);
628 double y
= _view
.note_to_y(highest
);
629 double height
= _view
.note_to_y(lowest
- 1) - y
;
632 rect
.set_width(get_width());
633 rect
.set_y((int) floor(y
));
634 rect
.set_height((int) floor(height
));
637 win
->invalidate_rect(rect
, false);
642 PianoRollHeader::on_size_request(Gtk::Requisition
* r
)
648 PianoRollHeader::on_size_allocate(Gtk::Allocation
& a
)
650 DrawingArea::on_size_allocate(a
);
652 _black_note_width
= floor(0.7 * get_width()) + 0.5f
;
656 PianoRollHeader::send_note_on(uint8_t note
)
658 boost::shared_ptr
<ARDOUR::MidiTrack
> track
= _view
.trackview().midi_track();
660 //cerr << "note on: " << (int) note << endl;
663 _event
[0] = (MIDI_CMD_NOTE_ON
| track
->default_channel());
667 track
->write_immediate_event(3, _event
);
672 PianoRollHeader::send_note_off(uint8_t note
)
674 boost::shared_ptr
<ARDOUR::MidiTrack
> track
= _view
.trackview().midi_track();
677 _event
[0] = (MIDI_CMD_NOTE_OFF
| track
->default_channel());
681 track
->write_immediate_event(3, _event
);
686 PianoRollHeader::reset_clicked_note (uint8_t note
, bool invalidate
)
688 _active_notes
[note
] = false;
689 _clicked_note
= no_note
;
690 send_note_off (note
);
692 invalidate_note_range (note
, note
);