ardour
piano_roll_header.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 Paul Davis
3 
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.
8 
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.
13 
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.
17 */
18 
19 #include <iostream>
20 #include "evoral/midi_events.h"
21 #include "ardour/midi_track.h"
22 
23 #include "gtkmm2ext/keyboard.h"
24 
25 #include "editing.h"
26 #include "piano_roll_header.h"
27 #include "midi_time_axis.h"
28 #include "midi_streamview.h"
29 #include "public_editor.h"
30 
31 const int no_note = 0xff;
32 
33 using namespace std;
34 using namespace Gtkmm2ext;
35 
40 
45 
47  : r(1.0f)
48  , g(1.0f)
49  , b(1.0f)
50 {
51 }
52 
53 PianoRollHeader::Color::Color(double _r, double _g, double _b)
54  : r(_r)
55  , g(_g)
56  , b(_b)
57 {
58 }
59 
60 inline void
62 {
63  r = c.r;
64  g = c.g;
65  b = c.b;
66 }
67 
69  : _view(v)
72  , _dragging(false)
73 {
74  add_events (Gdk::BUTTON_PRESS_MASK |
75  Gdk::BUTTON_RELEASE_MASK |
76  Gdk::POINTER_MOTION_MASK |
77  Gdk::ENTER_NOTIFY_MASK |
78  Gdk::LEAVE_NOTIFY_MASK |
79  Gdk::SCROLL_MASK);
80 
81  for (int i = 0; i < 128; ++i) {
82  _active_notes[i] = false;
83  }
84 
85  _view.NoteRangeChanged.connect (sigc::mem_fun (*this, &PianoRollHeader::note_range_changed));
86 }
87 
88 inline void
89 create_path(Cairo::RefPtr<Cairo::Context> cr, double x[], double y[], int start, int stop)
90 {
91  cr->move_to(x[start], y[start]);
92 
93  for (int i = start+1; i <= stop; ++i) {
94  cr->line_to(x[i], y[i]);
95  }
96 }
97 
98 inline void
99 render_rect(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
101 {
102  cr->set_source_rgb(bg.r, bg.g, bg.b);
103  create_path(cr, x, y, 0, 4);
104  cr->fill();
105 
106  cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
107  create_path(cr, x, y, 0, 2);
108  cr->stroke();
109 
110  cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
111  create_path(cr, x, y, 2, 4);
112  cr->stroke();
113 }
114 
115 inline void
116 render_cf(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
118 {
119  cr->set_source_rgb(bg.r, bg.g, bg.b);
120  create_path(cr, x, y, 0, 6);
121  cr->fill();
122 
123  cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
124  create_path(cr, x, y, 0, 4);
125  cr->stroke();
126 
127  cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
128  create_path(cr, x, y, 4, 6);
129  cr->stroke();
130 }
131 
132 inline void
133 render_eb(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
135 {
136  cr->set_source_rgb(bg.r, bg.g, bg.b);
137  create_path(cr, x, y, 0, 6);
138  cr->fill();
139 
140  cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
141  create_path(cr, x, y, 0, 2);
142  cr->stroke();
143  create_path(cr, x, y, 4, 5);
144  cr->stroke();
145 
146  cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
147  create_path(cr, x, y, 2, 4);
148  cr->stroke();
149  create_path(cr, x, y, 5, 6);
150  cr->stroke();
151 }
152 
153 inline void
154 render_dga(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
156 {
157  cr->set_source_rgb(bg.r, bg.g, bg.b);
158  create_path(cr, x, y, 0, 8);
159  cr->fill();
160 
161  cr->set_source_rgb(tl_shadow.r, tl_shadow.g, tl_shadow.b);
162  create_path(cr, x, y, 0, 4);
163  cr->stroke();
164  create_path(cr, x, y, 6, 7);
165  cr->stroke();
166 
167  cr->set_source_rgb(br_shadow.r, br_shadow.g, br_shadow.b);
168  create_path(cr, x, y, 4, 6);
169  cr->stroke();
170  create_path(cr, x, y, 7, 8);
171  cr->stroke();
172 }
173 
174 void
175 PianoRollHeader::get_path(PianoRollHeader::ItemType note_type, int note, double x[], double y[])
176 {
177  double y_pos = floor(_view.note_to_y(note)) + 1.5f;
178  double note_height;
179  double other_y1 = floor(_view.note_to_y(note+1)) + floor(_note_height / 2.0f) + 2.5f;
180  double other_y2 = floor(_view.note_to_y(note-1)) + floor(_note_height / 2.0f) + 1.0f;
181  double width = get_width();
182 
183  if (note == 0) {
184  note_height = floor(_view.contents_height()) - y_pos + 2.;
185  } else {
186  note_height = floor(_view.note_to_y(note - 1)) - y_pos + 2.;
187  }
188 
189  switch (note_type) {
190  case BLACK_SEPARATOR:
191  x[0] = 1.5f;
192  y[0] = y_pos;
193  x[1] = _black_note_width;
194  y[1] = y_pos;
195  break;
197  x[0] = _black_note_width;
198  y[0] = y_pos + floor(_note_height / 2.0f);
199  x[1] = width - 1.0f;
200  y[1] = y[0];
201  break;
202  case BLACK:
203  x[0] = 1.5f;
204  y[0] = y_pos + note_height - 0.5f;
205  x[1] = 1.5f;
206  y[1] = y_pos + 1.0f;
207  x[2] = _black_note_width;
208  y[2] = y_pos + 1.0f;
209  x[3] = _black_note_width;
210  y[3] = y_pos + note_height - 0.5f;
211  x[4] = 1.5f;
212  y[4] = y_pos + note_height - 0.5f;
213  return;
214  case WHITE_SEPARATOR:
215  x[0] = 1.5f;
216  y[0] = y_pos;
217  x[1] = width - 1.5f;
218  y[1] = y_pos;
219  break;
220  case WHITE_RECT:
221  x[0] = 1.5f;
222  y[0] = y_pos + note_height - 0.5f;
223  x[1] = 1.5f;
224  y[1] = y_pos + 1.0f;
225  x[2] = width - 1.5f;
226  y[2] = y_pos + 1.0f;
227  x[3] = width - 1.5f;
228  y[3] = y_pos + note_height - 0.5f;
229  x[4] = 1.5f;
230  y[4] = y_pos + note_height - 0.5f;
231  return;
232  case WHITE_CF:
233  x[0] = 1.5f;
234  y[0] = y_pos + note_height - 1.5f;
235  x[1] = 1.5f;
236  y[1] = y_pos + 1.0f;
237  x[2] = _black_note_width + 1.0f;
238  y[2] = y_pos + 1.0f;
239  x[3] = _black_note_width + 1.0f;
240  y[3] = other_y1;
241  x[4] = width - 1.5f;
242  y[4] = other_y1;
243  x[5] = width - 1.5f;
244  y[5] = y_pos + note_height - 1.5f;
245  x[6] = 1.5f;
246  y[6] = y_pos + note_height - 1.5f;
247  return;
248  case WHITE_EB:
249  x[0] = 1.5f;
250  y[0] = y_pos + note_height - 1.5f;
251  x[1] = 1.5f;
252  y[1] = y_pos + 1.0f;
253  x[2] = width - 1.5f;
254  y[2] = y_pos + 1.0f;
255  x[3] = width - 1.5f;
256  y[3] = other_y2;
257  x[4] = _black_note_width + 1.0f;
258  y[4] = other_y2;
259  x[5] = _black_note_width + 1.0f;
260  y[5] = y_pos + note_height - 1.5f;
261  x[6] = 1.5f;
262  y[6] = y_pos + note_height - 1.5f;
263  return;
264  case WHITE_DGA:
265  x[0] = 1.5f;
266  y[0] = y_pos + note_height - 1.5f;
267  x[1] = 1.5f;
268  y[1] = y_pos + 1.0f;
269  x[2] = _black_note_width + 1.0f;
270  y[2] = y_pos + 1.0f;
271  x[3] = _black_note_width + 1.0f;
272  y[3] = other_y1;
273  x[4] = width - 1.5f;
274  y[4] = other_y1;
275  x[5] = width - 1.5f;
276  y[5] = other_y2;
277  x[6] = _black_note_width + 1.0f;
278  y[6] = other_y2;
279  x[7] = _black_note_width + 1.0f;
280  y[7] = y_pos + note_height - 1.5f;
281  x[8] = 1.5f;
282  y[8] = y_pos + note_height - 1.5f;
283  return;
284  default:
285  return;
286  }
287 }
288 
289 bool
291 {
292  GdkRectangle& rect = ev->area;
293  double font_size;
294  int lowest, highest;
295  Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context();
296  Cairo::RefPtr<Cairo::LinearGradient> pat = Cairo::LinearGradient::create(0, 0, _black_note_width, 0);
297  double x[9];
298  double y[9];
299  Color bg, tl_shadow, br_shadow;
300  int oct_rel;
301  int y1 = max(rect.y, 0);
302  int y2 = min(rect.y + rect.height, (int) floor(_view.contents_height() - 1.0f));
303 
304  //Cairo::TextExtents te;
305  lowest = max(_view.lowest_note(), _view.y_to_note(y2));
306  highest = min(_view.highest_note(), _view.y_to_note(y1));
307 
308  if (lowest > 127) {
309  lowest = 0;
310  }
311 
312  cr->select_font_face ("Georgia", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD);
313  font_size = min((double) 10.0f, _note_height - 4.0f);
314  cr->set_font_size(font_size);
315 
316  /* fill the entire rect with the color for non-highlighted white notes.
317  * then we won't have to draw the background for those notes,
318  * and would only have to draw the background for the one highlighted white note*/
319  //cr->rectangle(rect.x, rect.y, rect.width, rect.height);
320  //cr->set_source_rgb(white.r, white.g, white.b);
321  //cr->fill();
322 
323  cr->set_line_width(1.0f);
324 
325  /* draw vertical lines with shade at both ends of the widget */
326  cr->set_source_rgb(0.0f, 0.0f, 0.0f);
327  cr->move_to(0.5f, rect.y);
328  cr->line_to(0.5f, rect.y + rect.height);
329  cr->stroke();
330  cr->move_to(get_width() - 0.5f, rect.y);
331  cr->line_to(get_width() - 0.5f, rect.y + rect.height);
332  cr->stroke();
333 
334  //pat->add_color_stop_rgb(0.0, 0.33, 0.33, 0.33);
335  //pat->add_color_stop_rgb(0.2, 0.39, 0.39, 0.39);
336  //pat->add_color_stop_rgb(1.0, 0.22, 0.22, 0.22);
337  //cr->set_source(pat);
338 
339  for (int i = lowest; i <= highest; ++i) {
340  oct_rel = i % 12;
341 
342  switch (oct_rel) {
343  case 1:
344  case 3:
345  case 6:
346  case 8:
347  case 10:
348  /* black note */
349  if (i == _highlighted_note) {
350  bg.set(black_highlight);
351  } else {
352  bg.set(black);
353  }
354 
355  if (_active_notes[i]) {
356  tl_shadow.set(black_shade_dark);
357  br_shadow.set(black_shade_light);
358  } else {
359  tl_shadow.set(black_shade_light);
360  br_shadow.set(black_shade_dark);
361  }
362 
363  /* draw black separators */
364  cr->set_source_rgb(0.0f, 0.0f, 0.0f);
365  get_path(BLACK_SEPARATOR, i, x, y);
366  create_path(cr, x, y, 0, 1);
367  cr->stroke();
368 
370  create_path(cr, x, y, 0, 1);
371  cr->stroke();
372 
373  get_path(BLACK, i, x, y);
374  render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
375  break;
376 
377  default:
378  /* white note */
379  if (i == _highlighted_note) {
380  bg.set(white_highlight);
381  } else {
382  bg.set(white);
383  }
384 
385  if (_active_notes[i]) {
386  tl_shadow.set(white_shade_dark);
387  br_shadow.set(white_shade_light);
388  } else {
389  tl_shadow.set(white_shade_light);
390  br_shadow.set(white_shade_dark);
391  }
392 
393  switch(oct_rel) {
394  case 0:
395  case 5:
396  if (i == _view.highest_note()) {
397  get_path(WHITE_RECT, i, x, y);
398  render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
399  } else {
400  get_path(WHITE_CF, i, x, y);
401  render_cf(cr, i, x, y, bg, tl_shadow, br_shadow);
402  }
403  break;
404 
405  case 2:
406  case 7:
407  case 9:
408  if (i == _view.highest_note()) {
409  get_path(WHITE_EB, i, x, y);
410  render_eb(cr, i, x, y, bg, tl_shadow, br_shadow);
411  } else if (i == _view.lowest_note()) {
412  get_path(WHITE_CF, i, x, y);
413  render_cf(cr, i, x, y, bg, tl_shadow, br_shadow);
414  } else {
415  get_path(WHITE_DGA, i, x, y);
416  render_dga(cr, i, x, y, bg, tl_shadow, br_shadow);
417  }
418  break;
419 
420  case 4:
421  case 11:
422  cr->set_source_rgb(0.0f, 0.0f, 0.0f);
423  get_path(WHITE_SEPARATOR, i, x, y);
424  create_path(cr, x, y, 0, 1);
425  cr->stroke();
426 
427  if (i == _view.lowest_note()) {
428  get_path(WHITE_RECT, i, x, y);
429  render_rect(cr, i, x, y, bg, tl_shadow, br_shadow);
430  } else {
431  get_path(WHITE_EB, i, x, y);
432  render_eb(cr, i, x, y, bg, tl_shadow, br_shadow);
433  }
434  break;
435 
436  default:
437  break;
438 
439  }
440  break;
441 
442  }
443 
444  /* render the name of which C this is */
445  if (oct_rel == 0) {
446  std::stringstream s;
447  double y = floor(_view.note_to_y(i)) - 0.5f;
448  double note_height = floor(_view.note_to_y(i - 1)) - y;
449 
450  int cn = i / 12 - 1;
451  s << "C" << cn;
452 
453  //cr->get_text_extents(s.str(), te);
454  cr->set_source_rgb(0.30f, 0.30f, 0.30f);
455  cr->move_to(2.0f, y + note_height - 1.0f - (note_height - font_size) / 2.0f);
456  cr->show_text(s.str());
457  }
458  }
459 
460  return true;
461 }
462 
463 bool
465 {
466  if (_dragging) {
467 
468  int note = _view.y_to_note(ev->y);
469 
470  if (editor().current_mouse_mode() == Editing::MouseRange) {
471 
472  /* select note range */
473 
474  if (Keyboard::no_modifiers_active (ev->state)) {
475  AddNoteSelection (note); // EMIT SIGNAL
476  }
477 
478  } else {
479 
480  /* play notes */
481 
482  if (_highlighted_note != no_note) {
483  if (note > _highlighted_note) {
485  } else {
487  }
488 
489  _highlighted_note = note;
490  }
491 
492  /* redraw already taken care of above */
493  if (_clicked_note != no_note && _clicked_note != note) {
494  _active_notes[_clicked_note] = false;
496 
497  _clicked_note = note;
498 
499  if (!_active_notes[note]) {
500  _active_notes[note] = true;
501  send_note_on(note);
502  }
503  }
504  }
505  }
506 
507  //win->process_updates(false);
508 
509  return true;
510 }
511 
512 bool
514 {
515  int note = _view.y_to_note(ev->y);
516  bool tertiary = Keyboard::modifier_state_contains (ev->state, Keyboard::TertiaryModifier);
517 
518  if (ev->button == 2 && Keyboard::no_modifiers_active (ev->state)) {
519  SetNoteSelection (note); // EMIT SIGNAL
520  return true;
521  } else if (tertiary && (ev->button == 1 || ev->button == 2)) {
522  ExtendNoteSelection (note); // EMIT SIGNAL
523  return true;
524  } else if (ev->button == 1 && note >= 0 && note < 128) {
525  add_modal_grab();
526  _dragging = true;
527 
528  if (!_active_notes[note]) {
529  _active_notes[note] = true;
530  _clicked_note = note;
531  send_note_on(note);
532 
533  invalidate_note_range(note, note);
534  } else {
535  reset_clicked_note(note);
536  }
537  }
538 
539  return true;
540 }
541 
542 bool
544 {
545  int note = _view.y_to_note(ev->y);
546 
547  if (editor().current_mouse_mode() == Editing::MouseRange) {
548 
549  if (Keyboard::no_modifiers_active (ev->state)) {
550  AddNoteSelection (note); // EMIT SIGNAL
551  } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
552  ToggleNoteSelection (note); // EMIT SIGNAL
553  } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::RangeSelectModifier)) {
554  ExtendNoteSelection (note); // EMIT SIGNAL
555  }
556 
557  } else {
558 
559  if (_dragging) {
560  remove_modal_grab();
561 
562  if (note == _clicked_note) {
563  reset_clicked_note(note);
564  }
565  }
566  }
567 
568  _dragging = false;
569  return true;
570 }
571 
572 bool
574 {
576 
578  return true;
579 }
580 
581 bool
583 {
585 
586  if (_clicked_note != no_note) {
588  }
589 
591  return true;
592 }
593 
594 bool
596 {
597  return true;
598 }
599 
600 void
602 {
603  _note_height = floor(_view.note_height()) + 0.5f;
604  queue_draw();
605 }
606 
607 void
609 {
610  Glib::RefPtr<Gdk::Window> win = get_window();
611  Gdk::Rectangle rect;
612 
613  // the non-rectangular geometry of some of the notes requires more
614  // redraws than the notes that actually changed.
615  switch(lowest % 12) {
616  case 0:
617  case 5:
618  lowest = max((int) _view.lowest_note(), lowest);
619  break;
620  default:
621  lowest = max((int) _view.lowest_note(), lowest - 1);
622  break;
623  }
624 
625  switch(highest % 12) {
626  case 4:
627  case 11:
628  highest = min((int) _view.highest_note(), highest);
629  break;
630  case 1:
631  case 3:
632  case 6:
633  case 8:
634  case 10:
635  highest = min((int) _view.highest_note(), highest + 1);
636  break;
637  default:
638  highest = min((int) _view.highest_note(), highest + 2);
639  break;
640  }
641 
642  double y = _view.note_to_y(highest);
643  double height = _view.note_to_y(lowest - 1) - y;
644 
645  rect.set_x(0);
646  rect.set_width(get_width());
647  rect.set_y((int) floor(y));
648  rect.set_height((int) floor(height));
649 
650  if (win) {
651  win->invalidate_rect(rect, false);
652  }
653 }
654 
655 void
657 {
658  r->width = 20;
659 }
660 
661 void
663 {
664  DrawingArea::on_size_allocate(a);
665 
666  _black_note_width = floor(0.7 * get_width()) + 0.5f;
667 }
668 
669 void
671 {
673  MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*> (&_view.trackview ());
674 
675  //cerr << "note on: " << (int) note << endl;
676 
677  if (track) {
679  _event[1] = note;
680  _event[2] = 100;
681 
682  track->write_immediate_event(3, _event);
683  }
684 }
685 
686 void
688 {
690  MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*> (&_view.trackview ());
691 
692  if (track) {
694  _event[1] = note;
695  _event[2] = 100;
696 
697  track->write_immediate_event(3, _event);
698  }
699 }
700 
701 void
702 PianoRollHeader::reset_clicked_note (uint8_t note, bool invalidate)
703 {
704  _active_notes[note] = false;
706  send_note_off (note);
707  if (invalidate) {
708  invalidate_note_range (note, note);
709  }
710 }
711 
714 {
715  return _view.trackview().editor();
716 }
PianoRollHeader(MidiStreamView &)
#define MIDI_CMD_NOTE_OFF
Definition: midi_events.h:107
boost::shared_ptr< ARDOUR::MidiTrack > midi_track() const
Definition: route_ui.cc:1762
#define MIDI_CMD_NOTE_ON
Definition: midi_events.h:108
PublicEditor & editor() const
static Color black
bool on_button_press_event(GdkEventButton *)
const int no_note
void render_dga(Cairo::RefPtr< Cairo::Context > cr, int, double x[], double y[], PianoRollHeader::Color &bg, PianoRollHeader::Color &tl_shadow, PianoRollHeader::Color &br_shadow)
void create_path(Cairo::RefPtr< Cairo::Context > cr, double x[], double y[], int start, int stop)
static Color white_shade_light
Representation of the interface of the Editor class.
bool on_motion_notify_event(GdkEventMotion *)
RouteTimeAxisView & trackview()
Definition: streamview.h:66
tuple f
Definition: signals.py:35
Definition: Beats.hpp:239
bool on_button_release_event(GdkEventButton *)
LIBARDOUR_API PBD::PropertyDescriptor< framepos_t > start
Definition: region.cc:63
sigc::signal< void > NoteRangeChanged
uint8_t lowest_note() const
void set(const Color &c)
sigc::signal< void, uint8_t > AddNoteSelection
bool on_scroll_event(GdkEventScroll *)
uint8_t get_channel_for_add() const
static Color white
bool on_expose_event(GdkEventExpose *)
PublicEditor & editor() const
static Color white_highlight
MidiStreamView & _view
void render_eb(Cairo::RefPtr< Cairo::Context > cr, int, double x[], double y[], PianoRollHeader::Color &bg, PianoRollHeader::Color &tl_shadow, PianoRollHeader::Color &br_shadow)
void send_note_off(uint8_t note)
void on_size_request(Gtk::Requisition *)
static Color black_shade_dark
void render_rect(Cairo::RefPtr< Cairo::Context > cr, int, double x[], double y[], PianoRollHeader::Color &bg, PianoRollHeader::Color &tl_shadow, PianoRollHeader::Color &br_shadow)
bool on_leave_notify_event(GdkEventCrossing *)
bool write_immediate_event(size_t size, const uint8_t *buf)
Definition: midi_track.cc:667
void invalidate_note_range(int lowest, int highest)
double note_to_y(uint8_t note) const
double note_height() const
sigc::signal< void, uint8_t > ToggleNoteSelection
uint8_t highest_note() const
uint8_t y_to_note(double y) const
void on_size_allocate(Gtk::Allocation &a)
sigc::signal< void, uint8_t > SetNoteSelection
static Color white_shade_dark
static double note_height(TimeAxisView &trackview, MidiStreamView *mv)
Definition: ghostregion.cc:254
void render_cf(Cairo::RefPtr< Cairo::Context > cr, int, double x[], double y[], PianoRollHeader::Color &bg, PianoRollHeader::Color &tl_shadow, PianoRollHeader::Color &br_shadow)
double contents_height() const
static Color black_shade_light
bool on_enter_notify_event(GdkEventCrossing *)
static Color black_highlight
void reset_clicked_note(uint8_t, bool invalidate=true)
void get_path(ItemType, int note, double x[], double y[])
sigc::signal< void, uint8_t > ExtendNoteSelection
void send_note_on(uint8_t note)