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 "piano_roll_header.h"
24 #include "midi_time_axis.h"
25 #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()
48 PianoRollHeader::Color::Color(double _r
, double _g
, double _b
)
56 PianoRollHeader::Color::set(const PianoRollHeader::Color
& c
)
63 PianoRollHeader::PianoRollHeader(MidiStreamView
& v
)
65 , _highlighted_note(no_note
)
66 , _clicked_note(no_note
)
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
|
76 for (int i
= 0; i
< 128; ++i
) {
77 _active_notes
[i
] = false;
80 _view
.NoteRangeChanged
.connect (mem_fun (*this, &PianoRollHeader::note_range_changed
));
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
]);
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);
101 cr
->set_source_rgb(tl_shadow
.r
, tl_shadow
.g
, tl_shadow
.b
);
102 create_path(cr
, x
, y
, 0, 2);
105 cr
->set_source_rgb(br_shadow
.r
, br_shadow
.g
, br_shadow
.b
);
106 create_path(cr
, x
, y
, 2, 4);
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);
118 cr
->set_source_rgb(tl_shadow
.r
, tl_shadow
.g
, tl_shadow
.b
);
119 create_path(cr
, x
, y
, 0, 4);
122 cr
->set_source_rgb(br_shadow
.r
, br_shadow
.g
, br_shadow
.b
);
123 create_path(cr
, x
, y
, 4, 6);
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);
135 cr
->set_source_rgb(tl_shadow
.r
, tl_shadow
.g
, tl_shadow
.b
);
136 create_path(cr
, x
, y
, 0, 2);
138 create_path(cr
, x
, y
, 4, 5);
141 cr
->set_source_rgb(br_shadow
.r
, br_shadow
.g
, br_shadow
.b
);
142 create_path(cr
, x
, y
, 2, 4);
144 create_path(cr
, x
, y
, 5, 6);
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);
156 cr
->set_source_rgb(tl_shadow
.r
, tl_shadow
.g
, tl_shadow
.b
);
157 create_path(cr
, x
, y
, 0, 4);
159 create_path(cr
, x
, y
, 6, 7);
162 cr
->set_source_rgb(br_shadow
.r
, br_shadow
.g
, br_shadow
.b
);
163 create_path(cr
, x
, y
, 4, 6);
165 create_path(cr
, x
, y
, 7, 8);
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
;
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();
179 note_height
= floor(_view
.contents_height()) - y_pos
;
181 note_height
= floor(_view
.note_to_y(note
- 1)) - y_pos
;
185 case BLACK_SEPARATOR
:
188 x
[1] = _black_note_width
;
191 case BLACK_MIDDLE_SEPARATOR
:
192 x
[0] = _black_note_width
;
193 y
[0] = y_pos
+ floor(_note_height
/ 2.0f
);
199 y
[0] = y_pos
+ note_height
- 0.5f
;
202 x
[2] = _black_note_width
;
204 x
[3] = _black_note_width
;
205 y
[3] = y_pos
+ note_height
- 0.5f
;
207 y
[4] = y_pos
+ note_height
- 0.5f
;
209 case WHITE_SEPARATOR
:
217 y
[0] = y_pos
+ note_height
- 0.5f
;
223 y
[3] = y_pos
+ note_height
- 0.5f
;
225 y
[4] = y_pos
+ note_height
- 0.5f
;
229 y
[0] = y_pos
+ note_height
- 1.5f
;
232 x
[2] = _black_note_width
+ 1.0f
;
234 x
[3] = _black_note_width
+ 1.0f
;
239 y
[5] = y_pos
+ note_height
- 1.5f
;
241 y
[6] = y_pos
+ note_height
- 1.5f
;
245 y
[0] = y_pos
+ note_height
- 1.5f
;
252 x
[4] = _black_note_width
+ 1.0f
;
254 x
[5] = _black_note_width
+ 1.0f
;
255 y
[5] = y_pos
+ note_height
- 1.5f
;
257 y
[6] = y_pos
+ note_height
- 1.5f
;
261 y
[0] = y_pos
+ note_height
- 1.5f
;
264 x
[2] = _black_note_width
+ 1.0f
;
266 x
[3] = _black_note_width
+ 1.0f
;
272 x
[6] = _black_note_width
+ 1.0f
;
274 x
[7] = _black_note_width
+ 1.0f
;
275 y
[7] = y_pos
+ note_height
- 1.5f
;
277 y
[8] = y_pos
+ note_height
- 1.5f
;
285 PianoRollHeader::on_expose_event (GdkEventExpose
* ev
)
287 GdkRectangle
& rect
= ev
->area
;
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);
294 Color bg
, tl_shadow
, br_shadow
;
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
));
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);
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
);
325 cr
->move_to(get_width() - 0.5f
, rect
.y
);
326 cr
->line_to(get_width() - 0.5f
, rect
.y
+ rect
.height
);
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
) {
344 if (i
== _highlighted_note
) {
345 bg
.set(black_highlight
);
350 if (_active_notes
[i
]) {
351 tl_shadow
.set(black_shade_dark
);
352 br_shadow
.set(black_shade_light
);
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);
364 get_path(BLACK_MIDDLE_SEPARATOR
, i
, x
, y
);
365 create_path(cr
, x
, y
, 0, 1);
368 get_path(BLACK
, i
, x
, y
);
369 render_rect(cr
, i
, x
, y
, bg
, tl_shadow
, br_shadow
);
374 if (i
== _highlighted_note
) {
375 bg
.set(white_highlight
);
380 if (_active_notes
[i
]) {
381 tl_shadow
.set(white_shade_dark
);
382 br_shadow
.set(white_shade_light
);
384 tl_shadow
.set(white_shade_light
);
385 br_shadow
.set(white_shade_dark
);
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
);
395 get_path(WHITE_CF
, i
, x
, y
);
396 render_cf(cr
, i
, x
, y
, bg
, tl_shadow
, br_shadow
);
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
);
410 get_path(WHITE_DGA
, i
, x
, y
);
411 render_dga(cr
, i
, x
, y
, bg
, tl_shadow
, br_shadow
);
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);
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
);
426 get_path(WHITE_EB
, i
, x
, y
);
427 render_eb(cr
, i
, x
, y
, bg
, tl_shadow
, br_shadow
);
439 /* render the name of which C this is */
442 double y
= floor(_view
.note_to_y(i
)) - 0.5f
;
443 double note_height
= floor(_view
.note_to_y(i
- 1)) - y
;
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());
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
);
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;
486 //win->process_updates(false);
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) {
500 if (!_active_notes
[note
]) {
501 _active_notes
[note
] = true;
502 _clicked_note
= note
;
505 invalidate_note_range(note
, note
);
507 _clicked_note
= no_note
;
515 PianoRollHeader::on_button_release_event (GdkEventButton
* ev
)
517 int note
= _view
.y_to_note(ev
->y
);
523 if (note
== _clicked_note
) {
524 _active_notes
[note
] = false;
525 _clicked_note
= no_note
;
528 invalidate_note_range(note
, note
);
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
);
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
;
565 PianoRollHeader::on_scroll_event (GdkEventScroll
*)
571 PianoRollHeader::note_range_changed()
573 _note_height
= floor(_view
.note_height()) + 0.5f
;
577 Glib::RefPtr
<Gdk::Window
> win
= get_window();
580 win
->process_updates(false);
585 PianoRollHeader::invalidate_note_range(int lowest
, int highest
)
587 Glib::RefPtr
<Gdk::Window
> win
= get_window();
590 // the non-rectangular geometry of some of the notes requires more
591 // redraws than the notes that actually changed.
592 switch(lowest
% 12) {
595 lowest
= max((int) _view
.lowest_note(), lowest
);
598 lowest
= max((int) _view
.lowest_note(), lowest
- 1);
602 switch(highest
% 12) {
605 highest
= min((int) _view
.highest_note(), highest
);
612 highest
= min((int) _view
.highest_note(), highest
+ 1);
615 highest
= min((int) _view
.highest_note(), highest
+ 2);
619 double y
= _view
.note_to_y(highest
);
620 double height
= _view
.note_to_y(lowest
- 1) - y
;
623 rect
.set_width(get_width());
624 rect
.set_y((int) floor(y
));
625 rect
.set_height((int) floor(height
));
628 win
->invalidate_rect(rect
, false);
633 PianoRollHeader::on_size_request(Gtk::Requisition
* r
)
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
;
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;
654 _event
[0] = (MIDI_CMD_NOTE_ON
| track
->default_channel());
658 track
->write_immediate_event(3, _event
);
663 PianoRollHeader::send_note_off(uint8_t note
)
665 boost::shared_ptr
<ARDOUR::MidiTrack
> track
= _view
.trackview().midi_track();
668 _event
[0] = (MIDI_CMD_NOTE_OFF
| track
->default_channel());
672 track
->write_immediate_event(3, _event
);