add MIDI channel editing for current note selection, bound to "c" by default
[ardour.git] / gtk2_ardour / midi_region_view.cc
1 /*
2     Copyright (C) 2001-2011 Paul Davis
3     Author: David Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <cmath>
21 #include <cassert>
22 #include <algorithm>
23 #include <ostream>
24
25 #include <gtkmm.h>
26
27 #include <gtkmm2ext/gtk_ui.h>
28
29 #include <sigc++/signal.h>
30
31 #include "pbd/memento_command.h"
32 #include "pbd/stateful_diff_command.h"
33
34 #include "ardour/playlist.h"
35 #include "ardour/tempo.h"
36 #include "ardour/midi_region.h"
37 #include "ardour/midi_source.h"
38 #include "ardour/midi_model.h"
39 #include "ardour/midi_patch_manager.h"
40 #include "ardour/session.h"
41
42 #include "evoral/Parameter.hpp"
43 #include "evoral/MIDIParameters.hpp"
44 #include "evoral/Control.hpp"
45 #include "evoral/midi_util.h"
46
47 #include "automation_region_view.h"
48 #include "automation_time_axis.h"
49 #include "canvas-hit.h"
50 #include "canvas-note.h"
51 #include "canvas_patch_change.h"
52 #include "debug.h"
53 #include "editor.h"
54 #include "ghostregion.h"
55 #include "gui_thread.h"
56 #include "keyboard.h"
57 #include "midi_channel_dialog.h"
58 #include "midi_cut_buffer.h"
59 #include "midi_list_editor.h"
60 #include "midi_region_view.h"
61 #include "midi_streamview.h"
62 #include "midi_time_axis.h"
63 #include "midi_util.h"
64 #include "note_player.h"
65 #include "public_editor.h"
66 #include "rgb_macros.h"
67 #include "selection.h"
68 #include "simpleline.h"
69 #include "streamview.h"
70 #include "utils.h"
71 #include "mouse_cursors.h"
72 #include "patch_change_dialog.h"
73 #include "verbose_cursor.h"
74
75 #include "i18n.h"
76
77 using namespace ARDOUR;
78 using namespace PBD;
79 using namespace Editing;
80 using namespace ArdourCanvas;
81 using Gtkmm2ext::Keyboard;
82
83 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv,
84                                 boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color const & basic_color)
85         : RegionView (parent, tv, r, spu, basic_color)
86         , _force_channel(-1)
87         , _last_channel_selection(0xFFFF)
88         , _current_range_min(0)
89         , _current_range_max(0)
90         , _model_name(string())
91         , _custom_device_mode(string())
92         , _active_notes(0)
93         , _note_group(new ArdourCanvas::Group(*group))
94         , _note_diff_command (0)
95         , _ghost_note(0)
96         , _drag_rect (0)
97         , _step_edit_cursor (0)
98         , _step_edit_cursor_width (1.0)
99         , _step_edit_cursor_position (0.0)
100         , _channel_selection_scoped_note (0)
101         , _temporary_note_group (0)
102         , _mouse_state(None)
103         , _pressed_button(0)
104         , _sort_needed (true)
105         , _optimization_iterator (_events.end())
106         , _list_editor (0)
107         , _no_sound_notes (false)
108         , _last_event_x (0)
109         , _last_event_y (0)
110         , _pre_enter_cursor (0)
111 {
112         _note_group->raise_to_top();
113         PublicEditor::DropDownKeys.connect (sigc::mem_fun (*this, &MidiRegionView::drop_down_keys));
114
115         connect_to_diskstream ();
116 }
117
118 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv,
119                                 boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color,
120                                 TimeAxisViewItem::Visibility visibility)
121         : RegionView (parent, tv, r, spu, basic_color, false, visibility)
122         , _force_channel(-1)
123         , _last_channel_selection(0xFFFF)
124         , _model_name(string())
125         , _custom_device_mode(string())
126         , _active_notes(0)
127         , _note_group(new ArdourCanvas::Group(*parent))
128         , _note_diff_command (0)
129         , _ghost_note(0)
130         , _drag_rect (0)
131         , _step_edit_cursor (0)
132         , _step_edit_cursor_width (1.0)
133         , _step_edit_cursor_position (0.0)
134         , _channel_selection_scoped_note (0)
135         , _temporary_note_group (0)
136         , _mouse_state(None)
137         , _pressed_button(0)
138         , _sort_needed (true)
139         , _optimization_iterator (_events.end())
140         , _list_editor (0)
141         , _no_sound_notes (false)
142         , _last_event_x (0)
143         , _last_event_y (0)
144 {
145         _note_group->raise_to_top();
146         PublicEditor::DropDownKeys.connect (sigc::mem_fun (*this, &MidiRegionView::drop_down_keys));
147
148         connect_to_diskstream ();
149 }
150
151 MidiRegionView::MidiRegionView (const MidiRegionView& other)
152         : sigc::trackable(other)
153         , RegionView (other)
154         , _force_channel(-1)
155         , _last_channel_selection(0xFFFF)
156         , _model_name(string())
157         , _custom_device_mode(string())
158         , _active_notes(0)
159         , _note_group(new ArdourCanvas::Group(*get_canvas_group()))
160         , _note_diff_command (0)
161         , _ghost_note(0)
162         , _drag_rect (0)
163         , _step_edit_cursor (0)
164         , _step_edit_cursor_width (1.0)
165         , _step_edit_cursor_position (0.0)
166         , _channel_selection_scoped_note (0)
167         , _temporary_note_group (0)
168         , _mouse_state(None)
169         , _pressed_button(0)
170         , _sort_needed (true)
171         , _optimization_iterator (_events.end())
172         , _list_editor (0)
173         , _no_sound_notes (false)
174         , _last_event_x (0)
175         , _last_event_y (0)
176 {
177         Gdk::Color c;
178         int r,g,b,a;
179
180         UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
181         c.set_rgb_p (r/255.0, g/255.0, b/255.0);
182
183         init (c, false);
184 }
185
186 MidiRegionView::MidiRegionView (const MidiRegionView& other, boost::shared_ptr<MidiRegion> region)
187         : RegionView (other, boost::shared_ptr<Region> (region))
188         , _force_channel(-1)
189         , _last_channel_selection(0xFFFF)
190         , _model_name(string())
191         , _custom_device_mode(string())
192         , _active_notes(0)
193         , _note_group(new ArdourCanvas::Group(*get_canvas_group()))
194         , _note_diff_command (0)
195         , _ghost_note(0)
196         , _drag_rect (0)
197         , _step_edit_cursor (0)
198         , _step_edit_cursor_width (1.0)
199         , _step_edit_cursor_position (0.0)
200         , _channel_selection_scoped_note (0)
201         , _temporary_note_group (0)
202         , _mouse_state(None)
203         , _pressed_button(0)
204         , _sort_needed (true)
205         , _optimization_iterator (_events.end())
206         , _list_editor (0)
207         , _no_sound_notes (false)
208         , _last_event_x (0)
209         , _last_event_y (0)
210 {
211         Gdk::Color c;
212         int r,g,b,a;
213
214         UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
215         c.set_rgb_p (r/255.0, g/255.0, b/255.0);
216
217         init (c, true);
218 }
219
220 void
221 MidiRegionView::init (Gdk::Color const & basic_color, bool wfd)
222 {
223         PublicEditor::DropDownKeys.connect (sigc::mem_fun (*this, &MidiRegionView::drop_down_keys));
224
225         CanvasNoteEvent::CanvasNoteEventDeleted.connect (note_delete_connection, MISSING_INVALIDATOR,
226                                                          ui_bind (&MidiRegionView::maybe_remove_deleted_note_from_selection, this, _1),
227                                                          gui_context());
228
229         if (wfd) {
230                 midi_region()->midi_source(0)->load_model();
231         }
232
233         _model = midi_region()->midi_source(0)->model();
234         _enable_display = false;
235
236         RegionView::init (basic_color, false);
237
238         compute_colors (basic_color);
239
240         set_height (trackview.current_height());
241
242         region_muted ();
243         region_sync_changed ();
244         region_resized (ARDOUR::bounds_change);
245         region_locked ();
246
247         reset_width_dependent_items (_pixel_width);
248
249         set_colors ();
250
251         _enable_display = true;
252         if (_model) {
253                 if (wfd) {
254                         display_model (_model);
255                 }
256         }
257
258         group->raise_to_top();
259         group->signal_event().connect(
260                 sigc::mem_fun(this, &MidiRegionView::canvas_event), false);
261
262         midi_view()->signal_channel_mode_changed().connect(
263                 sigc::mem_fun(this, &MidiRegionView::midi_channel_mode_changed));
264
265         midi_view()->signal_midi_patch_settings_changed().connect(
266                 sigc::mem_fun(this, &MidiRegionView::midi_patch_settings_changed));
267
268         trackview.editor().SnapChanged.connect(snap_changed_connection, invalidator(*this),
269                                                ui_bind(&MidiRegionView::snap_changed, this),
270                                                gui_context());
271
272         connect_to_diskstream ();
273 }
274
275 void
276 MidiRegionView::connect_to_diskstream ()
277 {
278         midi_view()->midi_track()->DataRecorded.connect(
279                 *this, invalidator(*this),
280                 ui_bind(&MidiRegionView::data_recorded, this, _1, _2),
281                 gui_context());
282 }
283
284 bool
285 MidiRegionView::canvas_event(GdkEvent* ev)
286 {
287         switch (ev->type) {
288         case GDK_ENTER_NOTIFY:
289         case GDK_LEAVE_NOTIFY:
290                 _last_event_x = ev->crossing.x;
291                 _last_event_y = ev->crossing.y;
292                 break;
293         case GDK_MOTION_NOTIFY:
294                 _last_event_x = ev->motion.x;
295                 _last_event_y = ev->motion.y;
296                 break;
297         default:
298                 break;
299         }
300
301         if (!trackview.editor().internal_editing()) {
302                 return false;
303         }
304
305         switch (ev->type) {
306         case GDK_SCROLL:
307                 return scroll (&ev->scroll);
308
309         case GDK_KEY_PRESS:
310                 return key_press (&ev->key);
311
312         case GDK_KEY_RELEASE:
313                 return key_release (&ev->key);
314
315         case GDK_BUTTON_PRESS:
316                 return button_press (&ev->button);
317
318         case GDK_2BUTTON_PRESS:
319                 return true;
320
321         case GDK_BUTTON_RELEASE:
322                 return button_release (&ev->button);
323
324         case GDK_ENTER_NOTIFY:
325                 return enter_notify (&ev->crossing);
326
327         case GDK_LEAVE_NOTIFY:
328                 return leave_notify (&ev->crossing);
329
330         case GDK_MOTION_NOTIFY:
331                 return motion (&ev->motion);
332
333         default:
334                 break;
335         }
336
337         return false;
338 }
339
340 void
341 MidiRegionView::remove_ghost_note ()
342 {
343         delete _ghost_note;
344         _ghost_note = 0;
345 }
346
347 bool
348 MidiRegionView::enter_notify (GdkEventCrossing* ev)
349 {
350         trackview.editor().MouseModeChanged.connect (
351                 _mouse_mode_connection, invalidator (*this), ui_bind (&MidiRegionView::mouse_mode_changed, this), gui_context ()
352                                                      );
353
354         Keyboard::magic_widget_grab_focus();
355         group->grab_focus();
356
357         if (trackview.editor().current_mouse_mode() == MouseRange) {
358                 create_ghost_note (ev->x, ev->y);
359         }
360
361         return false;
362 }
363
364 bool
365 MidiRegionView::leave_notify (GdkEventCrossing*)
366 {
367         _mouse_mode_connection.disconnect ();
368
369         trackview.editor().verbose_cursor()->hide ();
370         remove_ghost_note ();
371         return false;
372 }
373
374 void
375 MidiRegionView::mouse_mode_changed ()
376 {
377         if (trackview.editor().current_mouse_mode() == MouseRange && trackview.editor().internal_editing()) {
378                 create_ghost_note (_last_event_x, _last_event_y);
379         } else {
380                 remove_ghost_note ();
381                 trackview.editor().verbose_cursor()->hide ();
382         }
383 }
384
385 bool
386 MidiRegionView::button_press (GdkEventButton* ev)
387 {
388         if (ev->button != 1) {
389                 return false;
390         }
391
392         _last_x = ev->x;
393         _last_y = ev->y;
394
395         group->w2i (_last_x, _last_y);
396
397         if (_mouse_state != SelectTouchDragging) {
398
399                 _pressed_button = ev->button;
400                 _mouse_state = Pressed;
401
402                 return true;
403         }
404
405         _pressed_button = ev->button;
406
407         return true;
408 }
409
410 bool
411 MidiRegionView::button_release (GdkEventButton* ev)
412 {
413         double event_x, event_y;
414         framepos_t event_frame = 0;
415
416         if (ev->button != 1) {
417                 return false;
418         }
419
420         event_x = ev->x;
421         event_y = ev->y;
422
423         group->w2i(event_x, event_y);
424         group->ungrab(ev->time);
425
426         event_frame = trackview.editor().pixel_to_frame(event_x);
427
428         switch (_mouse_state) {
429         case Pressed: // Clicked
430
431                 switch (trackview.editor().current_mouse_mode()) {
432                 case MouseObject:
433                 case MouseTimeFX:
434                         {
435                                 clear_selection();
436
437                                 if (Keyboard::is_insert_note_event(ev)) {
438
439                                         double event_x, event_y;
440
441                                         event_x = ev->x;
442                                         event_y = ev->y;
443                                         group->w2i(event_x, event_y);
444
445                                         bool success;
446                                         Evoral::MusicalTime beats = trackview.editor().get_grid_type_as_beats (success, trackview.editor().pixel_to_frame (event_x));
447
448                                         if (!success) {
449                                                 beats = 1;
450                                         }
451
452                                         create_note_at (event_x, event_y, beats, true);
453                                 }
454
455                                 break;
456                         }
457                 case MouseRange:
458                         {
459                                 bool success;
460                                 Evoral::MusicalTime beats = trackview.editor().get_grid_type_as_beats (success, trackview.editor().pixel_to_frame (event_x));
461
462                                 if (!success) {
463                                         beats = 1;
464                                 }
465
466                                 create_note_at (event_x, event_y, beats, true);
467
468                                 break;
469                         }
470                 default:
471                         break;
472                 }
473
474                 _mouse_state = None;
475                 break;
476
477         case SelectRectDragging: // Select drag done
478
479                 _mouse_state = None;
480                 delete _drag_rect;
481                 _drag_rect = 0;
482                 break;
483
484         case AddDragging: // Add drag done
485
486                 _mouse_state = None;
487
488                 if (Keyboard::is_insert_note_event(ev) || trackview.editor().current_mouse_mode() == MouseRange) {
489
490                         if (_drag_rect->property_x2() > _drag_rect->property_x1() + 2) {
491
492                                 const double x  = _drag_rect->property_x1();
493                                 const double length = trackview.editor().pixel_to_frame (_drag_rect->property_x2() - _drag_rect->property_x1());
494
495                                 create_note_at (x, _drag_rect->property_y1(), frames_to_beats(length), true);
496                         }
497                 }
498
499                 delete _drag_rect;
500                 _drag_rect = 0;
501
502                 create_ghost_note (ev->x, ev->y);
503
504         default:
505                 break;
506         }
507
508         return false;
509 }
510
511 bool
512 MidiRegionView::motion (GdkEventMotion* ev)
513 {
514         double event_x, event_y;
515         framepos_t event_frame = 0;
516
517         event_x = ev->x;
518         event_y = ev->y;
519         group->w2i(event_x, event_y);
520
521         // convert event_x to global frame
522         event_frame = snap_pixel_to_frame (event_x);
523
524         if (!_ghost_note && trackview.editor().current_mouse_mode() != MouseRange
525             && Keyboard::modifier_state_contains (ev->state, Keyboard::insert_note_modifier())
526             && _mouse_state != AddDragging) {
527
528                 create_ghost_note (ev->x, ev->y);
529         } else if (_ghost_note && trackview.editor().current_mouse_mode() != MouseRange
530                    && Keyboard::modifier_state_contains (ev->state, Keyboard::insert_note_modifier())) {
531
532                 update_ghost_note (ev->x, ev->y);
533         } else if (_ghost_note && trackview.editor().current_mouse_mode() != MouseRange) {
534
535                 delete _ghost_note;
536                 _ghost_note = 0;
537
538                 trackview.editor().verbose_cursor()->hide ();
539         } else if (_ghost_note && trackview.editor().current_mouse_mode() == MouseRange) {
540                 update_ghost_note (ev->x, ev->y);
541         }
542
543         /* any motion immediately hides velocity text that may have been visible */
544
545         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
546                 (*i)->hide_velocity ();
547         }
548
549         switch (_mouse_state) {
550         case Pressed: // Maybe start a drag, if we've moved a bit
551
552                 if (fabs (event_x - _last_x) < 1 && fabs (event_y - _last_y) < 1) {
553                         /* no appreciable movement since the button was pressed */
554                         return false;
555                 }
556
557                 if (_pressed_button == 1 && trackview.editor().current_mouse_mode() == MouseObject
558                     && !Keyboard::modifier_state_contains (ev->state, Keyboard::insert_note_modifier())) {
559                         // Select drag start
560
561                         group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
562                                     Gdk::Cursor(Gdk::FLEUR), ev->time);
563
564                         _last_x = event_x;
565                         _last_y = event_y;
566                         _drag_start_x = event_x;
567                         _drag_start_y = event_y;
568
569                         _drag_rect = new ArdourCanvas::SimpleRect(*group);
570                         _drag_rect->property_x1() = event_x;
571                         _drag_rect->property_y1() = event_y;
572                         _drag_rect->property_x2() = event_x;
573                         _drag_rect->property_y2() = event_y;
574                         _drag_rect->property_outline_what() = 0xFF;
575                         _drag_rect->property_outline_color_rgba()
576                                 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
577                         _drag_rect->property_fill_color_rgba()
578                                 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
579
580                         _mouse_state = SelectRectDragging;
581                         return true;
582
583                 } else if (trackview.editor().internal_editing()) {
584                         // Add note drag start
585
586                         delete _ghost_note;
587                         _ghost_note = 0;
588
589                         group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
590                                     Gdk::Cursor(Gdk::FLEUR), ev->time);
591
592                         _last_x = event_x;
593                         _last_y = event_y;
594                         _drag_start_x = event_x;
595                         _drag_start_y = event_y;
596
597                         _drag_rect = new ArdourCanvas::SimpleRect(*group);
598                         _drag_rect->property_x1() = trackview.editor().frame_to_pixel(event_frame);
599
600                         _drag_rect->property_y1() = midi_stream_view()->note_to_y(
601                                 midi_stream_view()->y_to_note(event_y));
602                         _drag_rect->property_x2() = trackview.editor().frame_to_pixel(event_frame);
603                         _drag_rect->property_y2() = _drag_rect->property_y1()
604                                 + floor(midi_stream_view()->note_height());
605                         _drag_rect->property_outline_what() = 0xFF;
606                         _drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
607                         _drag_rect->property_fill_color_rgba()    = 0xFFFFFF66;
608
609                         _mouse_state = AddDragging;
610
611                         if (_ghost_note) {
612
613                                 delete _ghost_note;
614                                 _ghost_note = 0;
615
616                                 trackview.editor().verbose_cursor()->hide ();
617                         }
618
619                         return true;
620                 }
621
622                 return false;
623
624         case SelectRectDragging: // Select drag motion
625         case AddDragging: // Add note drag motion
626
627                 if (ev->is_hint) {
628                         int t_x;
629                         int t_y;
630                         GdkModifierType state;
631                         gdk_window_get_pointer(ev->window, &t_x, &t_y, &state);
632                         event_x = t_x;
633                         event_y = t_y;
634                 }
635
636                 if (_mouse_state == AddDragging) {
637                         event_x = trackview.editor().frame_to_pixel(event_frame);
638                 }
639
640                 if (_drag_rect) {
641
642                         if (event_x > _drag_start_x) {
643                                 _drag_rect->property_x2() = event_x;
644                         }
645                         else {
646                                 _drag_rect->property_x1() = event_x;
647                         }
648                 }
649
650                 if (_drag_rect && _mouse_state == SelectRectDragging) {
651
652                         if (event_y > _drag_start_y) {
653                                 _drag_rect->property_y2() = event_y;
654                         }
655                         else {
656                                 _drag_rect->property_y1() = event_y;
657                         }
658
659                         update_drag_selection(_drag_start_x, event_x, _drag_start_y, event_y);
660                 }
661
662                 _last_x = event_x;
663                 _last_y = event_y;
664
665         case SelectTouchDragging:
666                 return false;
667
668         default:
669                 break;
670         }
671
672         return false;
673 }
674
675
676 bool
677 MidiRegionView::scroll (GdkEventScroll* ev)
678 {
679         if (_selection.empty()) {
680                 return false;
681         }
682
683         trackview.editor().verbose_cursor()->hide ();
684
685         bool fine = !Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier);
686
687         if (ev->direction == GDK_SCROLL_UP) {
688                 change_velocities (true, fine, false);
689         } else if (ev->direction == GDK_SCROLL_DOWN) {
690                 change_velocities (false, fine, false);
691         }
692         return true;
693 }
694
695 bool
696 MidiRegionView::key_press (GdkEventKey* ev)
697 {
698         /* since GTK bindings are generally activated on press, and since
699            detectable auto-repeat is the name of the game and only sends
700            repeated presses, carry out key actions at key press, not release.
701         */
702
703         if (ev->keyval == GDK_Alt_L || ev->keyval == GDK_Alt_R) {
704                 _mouse_state = SelectTouchDragging;
705                 return true;
706
707         } else if (ev->keyval == GDK_Escape) {
708                 clear_selection();
709                 _mouse_state = None;
710
711         } else if (ev->keyval == GDK_comma || ev->keyval == GDK_period) {
712
713                 bool start = (ev->keyval == GDK_comma);
714                 bool end = (ev->keyval == GDK_period);
715                 bool shorter = Keyboard::modifier_state_contains (ev->state, Keyboard::PrimaryModifier);
716                 bool fine = Keyboard::modifier_state_contains (ev->state, Keyboard::SecondaryModifier);
717
718                 change_note_lengths (fine, shorter, 0.0, start, end);
719
720                 return true;
721
722         } else if (ev->keyval == GDK_Delete) {
723
724                 delete_selection();
725                 return true;
726
727         } else if (ev->keyval == GDK_Tab) {
728
729                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
730                         goto_previous_note ();
731                 } else {
732                         goto_next_note ();
733                 }
734                 return true;
735
736         } else if (ev->keyval == GDK_Up) {
737
738                 bool allow_smush = Keyboard::modifier_state_contains (ev->state, Keyboard::TertiaryModifier);
739                 bool fine = !Keyboard::modifier_state_contains (ev->state, Keyboard::SecondaryModifier);
740
741                 if (Keyboard::modifier_state_contains (ev->state, Keyboard::PrimaryModifier)) {
742                         change_velocities (true, fine, allow_smush);
743                 } else {
744                         transpose (true, fine, allow_smush);
745                 }
746                 return true;
747
748         } else if (ev->keyval == GDK_Down) {
749
750                 bool allow_smush = Keyboard::modifier_state_contains (ev->state, Keyboard::TertiaryModifier);
751                 bool fine = !Keyboard::modifier_state_contains (ev->state, Keyboard::SecondaryModifier);
752
753                 if (Keyboard::modifier_state_contains (ev->state, Keyboard::PrimaryModifier)) {
754                         change_velocities (false, fine, allow_smush);
755                 } else {
756                         transpose (false, fine, allow_smush);
757                 }
758                 return true;
759
760         } else if (ev->keyval == GDK_Left) {
761
762                 nudge_notes (false);
763                 return true;
764
765         } else if (ev->keyval == GDK_Right) {
766
767                 nudge_notes (true);
768                 return true;
769
770         } else if (ev->keyval == GDK_Control_L) {
771                 return true;
772
773         } else if (ev->keyval == GDK_c) {
774                 channel_edit ();
775                 return true;
776         }
777
778         return false;
779 }
780
781 bool
782 MidiRegionView::key_release (GdkEventKey* ev)
783 {
784         if (ev->keyval == GDK_Alt_L || ev->keyval == GDK_Alt_R) {
785                 _mouse_state = None;
786                 return true;
787         }
788         return false;
789 }
790
791 void
792 MidiRegionView::channel_edit ()
793 {
794         bool first = true;
795         bool mixed = false;
796         uint8_t current_channel;
797
798         if (_selection.empty()) {
799                 return;
800         }
801         
802         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
803                 Selection::iterator next = i;
804                 if (first) {
805                         current_channel = (*i)->note()->channel ();
806                         first = false;
807                 } else {
808                         if (current_channel != (*i)->note()->channel()) {
809                                 mixed = true;
810                         }
811                 }
812         }
813
814         MidiChannelDialog channel_dialog (current_channel);
815         int ret = channel_dialog.run ();
816
817         switch (ret) {
818         case Gtk::RESPONSE_OK:
819                 break;
820         default:
821                 return;
822         }
823
824         uint8_t new_channel = channel_dialog.active_channel ();
825
826         start_note_diff_command (_("channel edit"));
827
828         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
829                 Selection::iterator next = i;
830                 ++next;
831                 change_note_channel (*i, new_channel);
832                 i = next;
833         }
834
835         apply_diff ();
836 }
837
838 void
839 MidiRegionView::show_list_editor ()
840 {
841         if (!_list_editor) {
842                 _list_editor = new MidiListEditor (trackview.session(), midi_region());
843         }
844         _list_editor->present ();
845 }
846
847 /** Add a note to the model, and the view, at a canvas (click) coordinate.
848  * \param x horizontal position in pixels
849  * \param y vertical position in pixels
850  * \param length duration of the note in beats, which will be snapped to the grid
851  * \param sh true to make the note 1 frame shorter than the snapped version of \a length.
852  */
853 void
854 MidiRegionView::create_note_at(double x, double y, double length, bool sh)
855 {
856         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
857         MidiStreamView* const view = mtv->midi_view();
858
859         double note = view->y_to_note(y);
860
861         assert(note >= 0.0);
862         assert(note <= 127.0);
863
864         // Start of note in frames relative to region start
865         framepos_t const start_frames = snap_pixel_to_frame (x);
866         assert(start_frames >= 0);
867
868         // Snap length
869         length = frames_to_beats(
870                 snap_frame_to_frame(start_frames + beats_to_frames(length)) - start_frames);
871
872         assert (length != 0);
873
874         if (sh) {
875                 length = frames_to_beats (beats_to_frames (length) - 1);
876         }
877
878         const boost::shared_ptr<NoteType> new_note (new NoteType (mtv->get_channel_for_add (),
879                                                                   frames_to_beats(start_frames + _region->start()), length,
880                                                                   (uint8_t)note, 0x40));
881
882         if (_model->contains (new_note)) {
883                 return;
884         }
885
886         view->update_note_range(new_note->note());
887
888         MidiModel::NoteDiffCommand* cmd = _model->new_note_diff_command("add note");
889         cmd->add (new_note);
890         _model->apply_command(*trackview.session(), cmd);
891
892         play_midi_note (new_note);
893 }
894
895 void
896 MidiRegionView::clear_events()
897 {
898         clear_selection();
899
900         MidiGhostRegion* gr;
901         for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
902                 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
903                         gr->clear_events();
904                 }
905         }
906
907         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
908                 delete *i;
909         }
910
911         _events.clear();
912         _patch_changes.clear();
913         _sys_exes.clear();
914         _optimization_iterator = _events.end();
915 }
916
917 void
918 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
919 {
920         _model = model;
921
922         content_connection.disconnect ();
923         _model->ContentsChanged.connect (content_connection, invalidator (*this), boost::bind (&MidiRegionView::redisplay_model, this), gui_context());
924
925         clear_events ();
926
927         if (_enable_display) {
928                 redisplay_model();
929         }
930 }
931
932 void
933 MidiRegionView::start_note_diff_command (string name)
934 {
935         if (!_note_diff_command) {
936                 _note_diff_command = _model->new_note_diff_command (name);
937         }
938 }
939
940 void
941 MidiRegionView::note_diff_add_note (const boost::shared_ptr<NoteType> note, bool selected, bool show_velocity)
942 {
943         if (_note_diff_command) {
944                 _note_diff_command->add (note);
945         }
946         if (selected) {
947                 _marked_for_selection.insert(note);
948         }
949         if (show_velocity) {
950                 _marked_for_velocity.insert(note);
951         }
952 }
953
954 void
955 MidiRegionView::note_diff_remove_note (ArdourCanvas::CanvasNoteEvent* ev)
956 {
957         if (_note_diff_command && ev->note()) {
958                 _note_diff_command->remove(ev->note());
959         }
960 }
961
962 void
963 MidiRegionView::note_diff_add_change (ArdourCanvas::CanvasNoteEvent* ev,
964                                       MidiModel::NoteDiffCommand::Property property,
965                                       uint8_t val)
966 {
967         if (_note_diff_command) {
968                 _note_diff_command->change (ev->note(), property, val);
969         }
970 }
971
972 void
973 MidiRegionView::note_diff_add_change (ArdourCanvas::CanvasNoteEvent* ev,
974                                       MidiModel::NoteDiffCommand::Property property,
975                                       Evoral::MusicalTime val)
976 {
977         if (_note_diff_command) {
978                 _note_diff_command->change (ev->note(), property, val);
979         }
980 }
981
982 void
983 MidiRegionView::apply_diff (bool as_subcommand)
984 {
985         bool add_or_remove;
986
987         if (!_note_diff_command) {
988                 return;
989         }
990
991         if ((add_or_remove = _note_diff_command->adds_or_removes())) {
992                 // Mark all selected notes for selection when model reloads
993                 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
994                         _marked_for_selection.insert((*i)->note());
995                 }
996         }
997
998         if (as_subcommand) {
999                 _model->apply_command_as_subcommand (*trackview.session(), _note_diff_command);
1000         } else {
1001                 _model->apply_command (*trackview.session(), _note_diff_command);
1002         }
1003
1004         _note_diff_command = 0;
1005         midi_view()->midi_track()->playlist_modified();
1006
1007         if (add_or_remove) {
1008                 _marked_for_selection.clear();
1009         }
1010
1011         _marked_for_velocity.clear();
1012 }
1013
1014 void
1015 MidiRegionView::abort_command()
1016 {
1017         delete _note_diff_command;
1018         _note_diff_command = 0;
1019         clear_selection();
1020 }
1021
1022 CanvasNoteEvent*
1023 MidiRegionView::find_canvas_note (boost::shared_ptr<NoteType> note)
1024 {
1025         if (_optimization_iterator != _events.end()) {
1026                 ++_optimization_iterator;
1027         }
1028
1029         if (_optimization_iterator != _events.end() && (*_optimization_iterator)->note() == note) {
1030                 return *_optimization_iterator;
1031         }
1032
1033         for (_optimization_iterator = _events.begin(); _optimization_iterator != _events.end(); ++_optimization_iterator) {
1034                 if ((*_optimization_iterator)->note() == note) {
1035                         return *_optimization_iterator;
1036                 }
1037         }
1038
1039         return 0;
1040 }
1041
1042 void
1043 MidiRegionView::get_events (Events& e, Evoral::Sequence<Evoral::MusicalTime>::NoteOperator op, uint8_t val, int chan_mask)
1044 {
1045         MidiModel::Notes notes;
1046         _model->get_notes (notes, op, val, chan_mask);
1047
1048         for (MidiModel::Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
1049                 CanvasNoteEvent* cne = find_canvas_note (*n);
1050                 if (cne) {
1051                         e.push_back (cne);
1052                 }
1053         }
1054 }
1055
1056 void
1057 MidiRegionView::redisplay_model()
1058 {
1059         // Don't redisplay the model if we're currently recording and displaying that
1060         if (_active_notes) {
1061                 return;
1062         }
1063
1064         if (!_model) {
1065                 return;
1066         }
1067
1068         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1069                 (*i)->invalidate ();
1070         }
1071
1072         MidiModel::ReadLock lock(_model->read_lock());
1073
1074         MidiModel::Notes& notes (_model->notes());
1075         _optimization_iterator = _events.begin();
1076
1077         for (MidiModel::Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
1078
1079                 boost::shared_ptr<NoteType> note (*n);
1080                 CanvasNoteEvent* cne;
1081                 bool visible;
1082
1083                 if (note_in_region_range (note, visible)) {
1084
1085                         if ((cne = find_canvas_note (note)) != 0) {
1086
1087                                 cne->validate ();
1088
1089                                 CanvasNote* cn;
1090                                 CanvasHit* ch;
1091
1092                                 if ((cn = dynamic_cast<CanvasNote*>(cne)) != 0) {
1093                                         update_note (cn);
1094                                 } else if ((ch = dynamic_cast<CanvasHit*>(cne)) != 0) {
1095                                         update_hit (ch);
1096                                 }
1097
1098                                 if (visible) {
1099                                         cne->show ();
1100                                 } else {
1101                                         cne->hide ();
1102                                 }
1103
1104                         } else {
1105
1106                                 add_note (note, visible);
1107                         }
1108
1109                 } else {
1110
1111                         if ((cne = find_canvas_note (note)) != 0) {
1112                                 cne->validate ();
1113                                 cne->hide ();
1114                         }
1115                 }
1116         }
1117
1118
1119         /* remove note items that are no longer valid */
1120
1121         for (Events::iterator i = _events.begin(); i != _events.end(); ) {
1122                 if (!(*i)->valid ()) {
1123                         delete *i;
1124                         i = _events.erase (i);
1125                 } else {
1126                         ++i;
1127                 }
1128         }
1129
1130         _patch_changes.clear();
1131         _sys_exes.clear();
1132
1133         display_sysexes();
1134         display_patch_changes ();
1135
1136         _marked_for_selection.clear ();
1137         _marked_for_velocity.clear ();
1138
1139         /* we may have caused _events to contain things out of order (e.g. if a note
1140            moved earlier or later). we don't generally need them in time order, but
1141            make a note that a sort is required for those cases that require it.
1142         */
1143
1144         _sort_needed = true;
1145 }
1146
1147 void
1148 MidiRegionView::display_patch_changes ()
1149 {
1150         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
1151         uint16_t chn_mask = mtv->channel_selector().get_selected_channels();
1152
1153         for (uint8_t i = 0; i < 16; ++i) {
1154                 if (chn_mask & (1<<i)) {
1155                         display_patch_changes_on_channel (i);
1156                 }
1157         }
1158 }
1159
1160 void
1161 MidiRegionView::display_patch_changes_on_channel (uint8_t channel)
1162 {
1163         for (MidiModel::PatchChanges::const_iterator i = _model->patch_changes().begin(); i != _model->patch_changes().end(); ++i) {
1164
1165                 if ((*i)->channel() != channel) {
1166                         continue;
1167                 }
1168
1169                 MIDI::Name::PatchPrimaryKey patch_key ((*i)->bank_msb (), (*i)->bank_lsb (), (*i)->program ());
1170
1171                 boost::shared_ptr<MIDI::Name::Patch> patch =
1172                         MIDI::Name::MidiPatchManager::instance().find_patch(
1173                                 _model_name, _custom_device_mode, channel, patch_key);
1174
1175                 if (patch != 0) {
1176                         add_canvas_patch_change (*i, patch->name());
1177                 } else {
1178                         char buf[16];
1179                         /* program and bank numbers are zero-based: convert to one-based */
1180                         snprintf (buf, 16, "%d\n%d", (*i)->program() + 1, (*i)->bank() + 1);
1181                         add_canvas_patch_change (*i, buf);
1182                 }
1183         }
1184 }
1185
1186 void
1187 MidiRegionView::display_sysexes()
1188 {
1189         for (MidiModel::SysExes::const_iterator i = _model->sysexes().begin(); i != _model->sysexes().end(); ++i) {
1190                 Evoral::MusicalTime time = (*i)->time();
1191                 assert(time >= 0);
1192
1193                 ostringstream str;
1194                 str << hex;
1195                 for (uint32_t b = 0; b < (*i)->size(); ++b) {
1196                         str << int((*i)->buffer()[b]);
1197                         if (b != (*i)->size() -1) {
1198                                 str << " ";
1199                         }
1200                 }
1201                 string text = str.str();
1202
1203                 const double x = trackview.editor().frame_to_pixel(beats_to_frames(time));
1204
1205                 double height = midi_stream_view()->contents_height();
1206
1207                 boost::shared_ptr<CanvasSysEx> sysex = boost::shared_ptr<CanvasSysEx>(
1208                         new CanvasSysEx(*this, *_note_group, text, height, x, 1.0));
1209
1210                 // Show unless patch change is beyond the region bounds
1211                 if (time - _region->start() >= _region->length() || time < _region->start()) {
1212                         sysex->hide();
1213                 } else {
1214                         sysex->show();
1215                 }
1216
1217                 _sys_exes.push_back(sysex);
1218         }
1219 }
1220
1221
1222 MidiRegionView::~MidiRegionView ()
1223 {
1224         in_destructor = true;
1225
1226         trackview.editor().verbose_cursor()->hide ();
1227
1228         note_delete_connection.disconnect ();
1229
1230         delete _list_editor;
1231
1232         RegionViewGoingAway (this); /* EMIT_SIGNAL */
1233
1234         if (_active_notes) {
1235                 end_write();
1236         }
1237
1238         _selection.clear();
1239         clear_events();
1240
1241         delete _note_group;
1242         delete _note_diff_command;
1243         delete _step_edit_cursor;
1244         delete _temporary_note_group;
1245 }
1246
1247 void
1248 MidiRegionView::region_resized (const PropertyChange& what_changed)
1249 {
1250         RegionView::region_resized(what_changed);
1251
1252         if (what_changed.contains (ARDOUR::Properties::position)) {
1253                 set_duration(_region->length(), 0);
1254                 if (_enable_display) {
1255                         redisplay_model();
1256                 }
1257         }
1258 }
1259
1260 void
1261 MidiRegionView::reset_width_dependent_items (double pixel_width)
1262 {
1263         RegionView::reset_width_dependent_items(pixel_width);
1264         assert(_pixel_width == pixel_width);
1265
1266         if (_enable_display) {
1267                 redisplay_model();
1268         }
1269
1270         move_step_edit_cursor (_step_edit_cursor_position);
1271         set_step_edit_cursor_width (_step_edit_cursor_width);
1272 }
1273
1274 void
1275 MidiRegionView::set_height (double height)
1276 {
1277         static const double FUDGE = 2.0;
1278         const double old_height = _height;
1279         RegionView::set_height(height);
1280         _height = height - FUDGE;
1281
1282         apply_note_range(midi_stream_view()->lowest_note(),
1283                          midi_stream_view()->highest_note(),
1284                          height != old_height + FUDGE);
1285
1286         if (name_pixbuf) {
1287                 name_pixbuf->raise_to_top();
1288         }
1289
1290         for (PatchChanges::iterator x = _patch_changes.begin(); x != _patch_changes.end(); ++x) {
1291                 (*x)->set_height (midi_stream_view()->contents_height());
1292         }
1293
1294         if (_step_edit_cursor) {
1295                 _step_edit_cursor->property_y2() = midi_stream_view()->contents_height();
1296         }
1297 }
1298
1299
1300 /** Apply the current note range from the stream view
1301  * by repositioning/hiding notes as necessary
1302  */
1303 void
1304 MidiRegionView::apply_note_range (uint8_t min, uint8_t max, bool force)
1305 {
1306         if (!_enable_display) {
1307                 return;
1308         }
1309
1310         if (!force && _current_range_min == min && _current_range_max == max) {
1311                 return;
1312         }
1313
1314         _current_range_min = min;
1315         _current_range_max = max;
1316
1317         for (Events::const_iterator i = _events.begin(); i != _events.end(); ++i) {
1318                 CanvasNoteEvent* event = *i;
1319                 boost::shared_ptr<NoteType> note (event->note());
1320
1321                 if (note->note() < _current_range_min ||
1322                     note->note() > _current_range_max) {
1323                         event->hide();
1324                 } else {
1325                         event->show();
1326                 }
1327
1328                 if (CanvasNote* cnote = dynamic_cast<CanvasNote*>(event)) {
1329
1330                         const double y1 = midi_stream_view()->note_to_y(note->note());
1331                         const double y2 = y1 + floor(midi_stream_view()->note_height());
1332
1333                         cnote->property_y1() = y1;
1334                         cnote->property_y2() = y2;
1335
1336                 } else if (CanvasHit* chit = dynamic_cast<CanvasHit*>(event)) {
1337
1338                         const double diamond_size = update_hit (chit);
1339
1340                         chit->set_height (diamond_size);
1341                 }
1342         }
1343 }
1344
1345 GhostRegion*
1346 MidiRegionView::add_ghost (TimeAxisView& tv)
1347 {
1348         CanvasNote* note;
1349
1350         double unit_position = _region->position () / samples_per_unit;
1351         MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
1352         MidiGhostRegion* ghost;
1353
1354         if (mtv && mtv->midi_view()) {
1355                 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group
1356                    to allow having midi notes on top of note lines and waveforms.
1357                 */
1358                 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
1359         } else {
1360                 ghost = new MidiGhostRegion (tv, trackview, unit_position);
1361         }
1362
1363         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1364                 if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
1365                         ghost->add_note(note);
1366                 }
1367         }
1368
1369         ghost->set_height ();
1370         ghost->set_duration (_region->length() / samples_per_unit);
1371         ghosts.push_back (ghost);
1372
1373         GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
1374
1375         return ghost;
1376 }
1377
1378
1379 /** Begin tracking note state for successive calls to add_event
1380  */
1381 void
1382 MidiRegionView::begin_write()
1383 {
1384         assert(!_active_notes);
1385         _active_notes = new CanvasNote*[128];
1386         for (unsigned i=0; i < 128; ++i) {
1387                 _active_notes[i] = 0;
1388         }
1389 }
1390
1391
1392 /** Destroy note state for add_event
1393  */
1394 void
1395 MidiRegionView::end_write()
1396 {
1397         delete[] _active_notes;
1398         _active_notes = 0;
1399         _marked_for_selection.clear();
1400         _marked_for_velocity.clear();
1401 }
1402
1403
1404 /** Resolve an active MIDI note (while recording).
1405  */
1406 void
1407 MidiRegionView::resolve_note(uint8_t note, double end_time)
1408 {
1409         if (midi_view()->note_mode() != Sustained) {
1410                 return;
1411         }
1412
1413         if (_active_notes && _active_notes[note]) {
1414
1415                 const framepos_t end_time_frames = beats_to_frames(end_time);
1416
1417                 _active_notes[note]->property_x2() = trackview.editor().frame_to_pixel(end_time_frames);
1418                 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
1419                 _active_notes[note] = 0;
1420         }
1421 }
1422
1423
1424 /** Extend active notes to rightmost edge of region (if length is changed)
1425  */
1426 void
1427 MidiRegionView::extend_active_notes()
1428 {
1429         if (!_active_notes) {
1430                 return;
1431         }
1432
1433         for (unsigned i=0; i < 128; ++i) {
1434                 if (_active_notes[i]) {
1435                         _active_notes[i]->property_x2() = trackview.editor().frame_to_pixel(_region->length());
1436                 }
1437         }
1438 }
1439
1440
1441 void
1442 MidiRegionView::play_midi_note(boost::shared_ptr<NoteType> note)
1443 {
1444         if (_no_sound_notes || !trackview.editor().sound_notes()) {
1445                 return;
1446         }
1447
1448         RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
1449
1450         if (!route_ui || !route_ui->midi_track()) {
1451                 return;
1452         }
1453
1454         NotePlayer* np = new NotePlayer (route_ui->midi_track());
1455         np->add (note);
1456         np->play ();
1457 }
1458
1459 void
1460 MidiRegionView::play_midi_chord (vector<boost::shared_ptr<NoteType> > notes)
1461 {
1462         if (_no_sound_notes || !trackview.editor().sound_notes()) {
1463                 return;
1464         }
1465
1466         RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
1467
1468         if (!route_ui || !route_ui->midi_track()) {
1469                 return;
1470         }
1471
1472         NotePlayer* np = new NotePlayer (route_ui->midi_track());
1473
1474         for (vector<boost::shared_ptr<NoteType> >::iterator n = notes.begin(); n != notes.end(); ++n) {
1475                 np->add (*n);
1476         }
1477
1478         np->play ();
1479 }
1480
1481
1482 bool
1483 MidiRegionView::note_in_region_range(const boost::shared_ptr<NoteType> note, bool& visible) const
1484 {
1485         const framepos_t note_start_frames = beats_to_frames(note->time());
1486
1487         bool outside = (note_start_frames - _region->start() >= _region->length()) ||
1488                 (note_start_frames < _region->start());
1489
1490         visible = (note->note() >= midi_stream_view()->lowest_note()) &&
1491                 (note->note() <= midi_stream_view()->highest_note());
1492
1493         return !outside;
1494 }
1495
1496 /** Update a canvas note's size from its model note.
1497  *  @param ev Canvas note to update.
1498  *  @param update_ghost_regions true to update the note in any ghost regions that we have, otherwise false.
1499  */
1500 void
1501 MidiRegionView::update_note (CanvasNote* ev, bool update_ghost_regions)
1502 {
1503         boost::shared_ptr<NoteType> note = ev->note();
1504
1505         const framepos_t note_start_frames = beats_to_frames(note->time());
1506
1507         /* trim note display to not overlap the end of its region */
1508         const framepos_t note_end_frames = min (beats_to_frames (note->end_time()), _region->start() + _region->length());
1509
1510         const double x = trackview.editor().frame_to_pixel(note_start_frames - _region->start());
1511         const double y1 = midi_stream_view()->note_to_y(note->note());
1512         const double note_endpixel = trackview.editor().frame_to_pixel(note_end_frames - _region->start());
1513
1514         ev->property_x1() = x;
1515         ev->property_y1() = y1;
1516
1517         if (note->length() > 0) {
1518                 ev->property_x2() = note_endpixel;
1519         } else {
1520                 ev->property_x2() = trackview.editor().frame_to_pixel(_region->length());
1521         }
1522
1523         ev->property_y2() = y1 + floor(midi_stream_view()->note_height());
1524
1525         if (note->length() == 0) {
1526                 if (_active_notes) {
1527                         assert(note->note() < 128);
1528                         // If this note is already active there's a stuck note,
1529                         // finish the old note rectangle
1530                         if (_active_notes[note->note()]) {
1531                                 CanvasNote* const old_rect = _active_notes[note->note()];
1532                                 boost::shared_ptr<NoteType> old_note = old_rect->note();
1533                                 old_rect->property_x2() = x;
1534                                 old_rect->property_outline_what() = (guint32) 0xF;
1535                         }
1536                         _active_notes[note->note()] = ev;
1537                 }
1538                 /* outline all but right edge */
1539                 ev->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
1540         } else {
1541                 /* outline all edges */
1542                 ev->property_outline_what() = (guint32) 0xF;
1543         }
1544
1545         if (update_ghost_regions) {
1546                 for (std::vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
1547                         MidiGhostRegion* gr = dynamic_cast<MidiGhostRegion*> (*i);
1548                         if (gr) {
1549                                 gr->update_note (ev);
1550                         }
1551                 }
1552         }
1553 }
1554
1555 double
1556 MidiRegionView::update_hit (CanvasHit* ev)
1557 {
1558         boost::shared_ptr<NoteType> note = ev->note();
1559
1560         const framepos_t note_start_frames = beats_to_frames(note->time());
1561         const double x = trackview.editor().frame_to_pixel(note_start_frames - _region->start());
1562         const double diamond_size = midi_stream_view()->note_height() / 2.0;
1563         const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
1564
1565         ev->move_to (x, y);
1566
1567         return diamond_size;
1568 }
1569
1570 /** Add a MIDI note to the view (with length).
1571  *
1572  * If in sustained mode, notes with length 0 will be considered active
1573  * notes, and resolve_note should be called when the corresponding note off
1574  * event arrives, to properly display the note.
1575  */
1576 void
1577 MidiRegionView::add_note(const boost::shared_ptr<NoteType> note, bool visible)
1578 {
1579         CanvasNoteEvent* event = 0;
1580
1581         assert(note->time() >= 0);
1582         assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
1583
1584         //ArdourCanvas::Group* const group = (ArdourCanvas::Group*) get_canvas_group();
1585
1586         if (midi_view()->note_mode() == Sustained) {
1587
1588                 CanvasNote* ev_rect = new CanvasNote(*this, *_note_group, note);
1589
1590                 update_note (ev_rect);
1591
1592                 event = ev_rect;
1593
1594                 MidiGhostRegion* gr;
1595
1596                 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
1597                         if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
1598                                 gr->add_note(ev_rect);
1599                         }
1600                 }
1601
1602         } else if (midi_view()->note_mode() == Percussive) {
1603
1604                 const double diamond_size = midi_stream_view()->note_height() / 2.0;
1605
1606                 CanvasHit* ev_diamond = new CanvasHit(*this, *_note_group, diamond_size, note);
1607
1608                 update_hit (ev_diamond);
1609
1610                 event = ev_diamond;
1611
1612         } else {
1613                 event = 0;
1614         }
1615
1616         if (event) {
1617                 if (_marked_for_selection.find(note) != _marked_for_selection.end()) {
1618                         note_selected(event, true);
1619                 }
1620
1621                 if (_marked_for_velocity.find(note) != _marked_for_velocity.end()) {
1622                         event->show_velocity();
1623                 }
1624
1625                 event->on_channel_selection_change(_last_channel_selection);
1626                 _events.push_back(event);
1627
1628                 if (visible) {
1629                         event->show();
1630                 } else {
1631                         event->hide ();
1632                 }
1633         }
1634
1635         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
1636         MidiStreamView* const view = mtv->midi_view();
1637
1638         view->update_note_range(note->note());
1639 }
1640
1641 void
1642 MidiRegionView::step_add_note (uint8_t channel, uint8_t number, uint8_t velocity,
1643                                Evoral::MusicalTime pos, Evoral::MusicalTime len)
1644 {
1645         boost::shared_ptr<NoteType> new_note (new NoteType (channel, pos, len, number, velocity));
1646
1647         /* potentially extend region to hold new note */
1648
1649         framepos_t end_frame = _region->position() + beats_to_frames (new_note->end_time());
1650         framepos_t region_end = _region->position() + _region->length() - 1;
1651
1652         if (end_frame > region_end) {
1653                 _region->set_length (end_frame - _region->position());
1654         }
1655
1656         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
1657         MidiStreamView* const view = mtv->midi_view();
1658
1659         view->update_note_range(new_note->note());
1660
1661         _marked_for_selection.clear ();
1662         clear_selection ();
1663
1664         start_note_diff_command (_("step add"));
1665         note_diff_add_note (new_note, true, false);
1666         apply_diff();
1667
1668         // last_step_edit_note = new_note;
1669 }
1670
1671 void
1672 MidiRegionView::step_sustain (Evoral::MusicalTime beats)
1673 {
1674         change_note_lengths (false, false, beats, false, true);
1675 }
1676
1677 void
1678 MidiRegionView::add_canvas_patch_change (MidiModel::PatchChangePtr patch, const string& displaytext)
1679 {
1680         assert (patch->time() >= 0);
1681
1682         const double x = trackview.editor().frame_to_pixel (beats_to_frames (patch->time()));
1683
1684         double const height = midi_stream_view()->contents_height();
1685
1686         boost::shared_ptr<CanvasPatchChange> patch_change = boost::shared_ptr<CanvasPatchChange>(
1687                 new CanvasPatchChange(*this, *_note_group,
1688                                       displaytext,
1689                                       height,
1690                                       x, 1.0,
1691                                       _model_name,
1692                                       _custom_device_mode,
1693                                       patch)
1694                           );
1695
1696         // Show unless patch change is beyond the region bounds
1697         if (patch->time() - _region->start() >= _region->length() || patch->time() < _region->start()) {
1698                 patch_change->hide();
1699         } else {
1700                 patch_change->show();
1701         }
1702
1703         _patch_changes.push_back (patch_change);
1704 }
1705
1706 void
1707 MidiRegionView::get_patch_key_at (Evoral::MusicalTime time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key)
1708 {
1709         MidiModel::PatchChanges::iterator i = _model->patch_change_lower_bound (time);
1710         while (i != _model->patch_changes().end() && (*i)->channel() != channel) {
1711                 ++i;
1712         }
1713
1714         if (i != _model->patch_changes().end()) {
1715                 key.msb = (*i)->bank_msb ();
1716                 key.lsb = (*i)->bank_lsb ();
1717                 key.program_number = (*i)->program ();
1718         } else {
1719                 key.msb = key.lsb = key.program_number = 0;
1720         }
1721
1722         assert (key.is_sane());
1723 }
1724
1725
1726 void
1727 MidiRegionView::change_patch_change (CanvasPatchChange& pc, const MIDI::Name::PatchPrimaryKey& new_patch)
1728 {
1729         MidiModel::PatchChangeDiffCommand* c = _model->new_patch_change_diff_command (_("alter patch change"));
1730
1731         if (pc.patch()->program() != new_patch.program_number) {
1732                 c->change_program (pc.patch (), new_patch.program_number);
1733         }
1734
1735         int const new_bank = (new_patch.msb << 7) | new_patch.lsb;
1736         if (pc.patch()->bank() != new_bank) {
1737                 c->change_bank (pc.patch (), new_bank);
1738         }
1739
1740         _model->apply_command (*trackview.session(), c);
1741
1742         _patch_changes.clear ();
1743         display_patch_changes ();
1744 }
1745
1746 void
1747 MidiRegionView::change_patch_change (MidiModel::PatchChangePtr old_change, const Evoral::PatchChange<Evoral::MusicalTime> & new_change)
1748 {
1749         MidiModel::PatchChangeDiffCommand* c = _model->new_patch_change_diff_command (_("alter patch change"));
1750
1751         if (old_change->time() != new_change.time()) {
1752                 c->change_time (old_change, new_change.time());
1753         }
1754
1755         if (old_change->channel() != new_change.channel()) {
1756                 c->change_channel (old_change, new_change.channel());
1757         }
1758
1759         if (old_change->program() != new_change.program()) {
1760                 c->change_program (old_change, new_change.program());
1761         }
1762
1763         if (old_change->bank() != new_change.bank()) {
1764                 c->change_bank (old_change, new_change.bank());
1765         }
1766
1767         _model->apply_command (*trackview.session(), c);
1768
1769         _patch_changes.clear ();
1770         display_patch_changes ();
1771 }
1772
1773 /** Add a patch change to the region.
1774  *  @param t Time in frames relative to region position
1775  *  @param patch Patch to add; time and channel are ignored (time is converted from t, and channel comes from
1776  *  MidiTimeAxisView::get_channel_for_add())
1777  */
1778 void
1779 MidiRegionView::add_patch_change (framecnt_t t, Evoral::PatchChange<Evoral::MusicalTime> const & patch)
1780 {
1781         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
1782
1783         MidiModel::PatchChangeDiffCommand* c = _model->new_patch_change_diff_command (_("add patch change"));
1784         c->add (MidiModel::PatchChangePtr (
1785                         new Evoral::PatchChange<Evoral::MusicalTime> (
1786                                 frames_to_beats (t + midi_region()->start()), mtv->get_channel_for_add(), patch.program(), patch.bank()
1787                                                                       )
1788                                            ));
1789
1790         _model->apply_command (*trackview.session(), c);
1791
1792         _patch_changes.clear ();
1793         display_patch_changes ();
1794 }
1795
1796 void
1797 MidiRegionView::move_patch_change (CanvasPatchChange& pc, Evoral::MusicalTime t)
1798 {
1799         MidiModel::PatchChangeDiffCommand* c = _model->new_patch_change_diff_command (_("move patch change"));
1800         c->change_time (pc.patch (), t);
1801         _model->apply_command (*trackview.session(), c);
1802
1803         _patch_changes.clear ();
1804         display_patch_changes ();
1805 }
1806
1807 void
1808 MidiRegionView::delete_patch_change (CanvasPatchChange* pc)
1809 {
1810         MidiModel::PatchChangeDiffCommand* c = _model->new_patch_change_diff_command (_("delete patch change"));
1811         c->remove (pc->patch ());
1812         _model->apply_command (*trackview.session(), c);
1813
1814         _patch_changes.clear ();
1815         display_patch_changes ();
1816 }
1817
1818 void
1819 MidiRegionView::previous_patch (CanvasPatchChange& patch)
1820 {
1821         if (patch.patch()->program() < 127) {
1822                 MIDI::Name::PatchPrimaryKey key;
1823                 get_patch_key_at (patch.patch()->time(), patch.patch()->channel(), key);
1824                 key.program_number++;
1825                 change_patch_change (patch, key);
1826         }
1827 }
1828
1829 void
1830 MidiRegionView::next_patch (CanvasPatchChange& patch)
1831 {
1832         if (patch.patch()->program() > 0) {
1833                 MIDI::Name::PatchPrimaryKey key;
1834                 get_patch_key_at (patch.patch()->time(), patch.patch()->channel(), key);
1835                 key.program_number--;
1836                 change_patch_change (patch, key);
1837         }
1838 }
1839
1840 void
1841 MidiRegionView::maybe_remove_deleted_note_from_selection (CanvasNoteEvent* cne)
1842 {
1843         if (_selection.empty()) {
1844                 return;
1845         }
1846
1847         if (_selection.erase (cne) > 0) {
1848                 cerr << "Erased a CNE from selection\n";
1849         }
1850 }
1851
1852 void
1853 MidiRegionView::delete_selection()
1854 {
1855         if (_selection.empty()) {
1856                 return;
1857         }
1858
1859         start_note_diff_command (_("delete selection"));
1860
1861         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1862                 if ((*i)->selected()) {
1863                         _note_diff_command->remove((*i)->note());
1864                 }
1865         }
1866
1867         _selection.clear();
1868
1869         apply_diff ();
1870 }
1871
1872 void
1873 MidiRegionView::delete_note (boost::shared_ptr<NoteType> n)
1874 {
1875         start_note_diff_command (_("delete note"));
1876         _note_diff_command->remove (n);
1877         apply_diff ();
1878
1879         trackview.editor().verbose_cursor()->hide ();
1880 }
1881
1882 void
1883 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev)
1884 {
1885         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1886                 if ((*i)->selected() && (*i) != ev) {
1887                         (*i)->set_selected(false);
1888                         (*i)->hide_velocity();
1889                 }
1890         }
1891
1892         _selection.clear();
1893 }
1894
1895 void
1896 MidiRegionView::unique_select(ArdourCanvas::CanvasNoteEvent* ev)
1897 {
1898         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
1899                 if ((*i) != ev) {
1900
1901                         Selection::iterator tmp = i;
1902                         ++tmp;
1903
1904                         (*i)->set_selected (false);
1905                         _selection.erase (i);
1906
1907                         i = tmp;
1908
1909                 } else {
1910                         ++i;
1911                 }
1912         }
1913
1914         /* don't bother with removing this regionview from the editor selection,
1915            since we're about to add another note, and thus put/keep this
1916            regionview in the editor selection.
1917         */
1918
1919         if (!ev->selected()) {
1920                 add_to_selection (ev);
1921         }
1922 }
1923
1924 void
1925 MidiRegionView::select_all_notes ()
1926 {
1927         clear_selection ();
1928
1929         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1930                 add_to_selection (*i);
1931         }
1932 }
1933
1934 void
1935 MidiRegionView::select_matching_notes (uint8_t notenum, uint16_t channel_mask, bool add, bool extend)
1936 {
1937         uint8_t low_note = 127;
1938         uint8_t high_note = 0;
1939         MidiModel::Notes& notes (_model->notes());
1940         _optimization_iterator = _events.begin();
1941
1942         if (!add) {
1943                 clear_selection ();
1944         }
1945
1946         if (extend && _selection.empty()) {
1947                 extend = false;
1948         }
1949
1950         if (extend) {
1951
1952                 /* scan existing selection to get note range */
1953
1954                 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1955                         if ((*i)->note()->note() < low_note) {
1956                                 low_note = (*i)->note()->note();
1957                         }
1958                         if ((*i)->note()->note() > high_note) {
1959                                 high_note = (*i)->note()->note();
1960                         }
1961                 }
1962
1963                 low_note = min (low_note, notenum);
1964                 high_note = max (high_note, notenum);
1965         }
1966
1967         _no_sound_notes = true;
1968
1969         for (MidiModel::Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
1970
1971                 boost::shared_ptr<NoteType> note (*n);
1972                 CanvasNoteEvent* cne;
1973                 bool select = false;
1974
1975                 if (((1 << note->channel()) & channel_mask) != 0) {
1976                         if (extend) {
1977                                 if ((note->note() >= low_note && note->note() <= high_note)) {
1978                                         select = true;
1979                                 }
1980                         } else if (note->note() == notenum) {
1981                                 select = true;
1982                         }
1983                 }
1984
1985                 if (select) {
1986                         if ((cne = find_canvas_note (note)) != 0) {
1987                                 // extend is false because we've taken care of it,
1988                                 // since it extends by time range, not pitch.
1989                                 note_selected (cne, add, false);
1990                         }
1991                 }
1992
1993                 add = true; // we need to add all remaining matching notes, even if the passed in value was false (for "set")
1994
1995         }
1996
1997         _no_sound_notes = false;
1998 }
1999
2000 void
2001 MidiRegionView::toggle_matching_notes (uint8_t notenum, uint16_t channel_mask)
2002 {
2003         MidiModel::Notes& notes (_model->notes());
2004         _optimization_iterator = _events.begin();
2005
2006         for (MidiModel::Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
2007
2008                 boost::shared_ptr<NoteType> note (*n);
2009                 CanvasNoteEvent* cne;
2010
2011                 if (note->note() == notenum && (((0x0001 << note->channel()) & channel_mask) != 0)) {
2012                         if ((cne = find_canvas_note (note)) != 0) {
2013                                 if (cne->selected()) {
2014                                         note_deselected (cne);
2015                                 } else {
2016                                         note_selected (cne, true, false);
2017                                 }
2018                         }
2019                 }
2020         }
2021 }
2022
2023 void
2024 MidiRegionView::note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add, bool extend)
2025 {
2026         if (!add) {
2027                 clear_selection_except(ev);
2028         }
2029
2030         if (!extend) {
2031
2032                 if (!ev->selected()) {
2033                         add_to_selection (ev);
2034                 }
2035
2036         } else {
2037                 /* find end of latest note selected, select all between that and the start of "ev" */
2038
2039                 Evoral::MusicalTime earliest = Evoral::MaxMusicalTime;
2040                 Evoral::MusicalTime latest = 0;
2041
2042                 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2043                         if ((*i)->note()->end_time() > latest) {
2044                                 latest = (*i)->note()->end_time();
2045                         }
2046                         if ((*i)->note()->time() < earliest) {
2047                                 earliest = (*i)->note()->time();
2048                         }
2049                 }
2050
2051                 if (ev->note()->end_time() > latest) {
2052                         latest = ev->note()->end_time();
2053                 }
2054
2055                 if (ev->note()->time() < earliest) {
2056                         earliest = ev->note()->time();
2057                 }
2058
2059                 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
2060
2061                         /* find notes entirely within OR spanning the earliest..latest range */
2062
2063                         if (((*i)->note()->time() >= earliest && (*i)->note()->end_time() <= latest) ||
2064                             ((*i)->note()->time() <= earliest && (*i)->note()->end_time() >= latest)) {
2065                                 add_to_selection (*i);
2066                         }
2067
2068 #if 0
2069                         /* if events were guaranteed to be time sorted, we could do this.
2070                            but as of sept 10th 2009, they no longer are.
2071                         */
2072
2073                         if ((*i)->note()->time() > latest) {
2074                                 break;
2075                         }
2076 #endif
2077                 }
2078         }
2079 }
2080
2081 void
2082 MidiRegionView::note_deselected(ArdourCanvas::CanvasNoteEvent* ev)
2083 {
2084         remove_from_selection (ev);
2085 }
2086
2087 void
2088 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
2089 {
2090         if (x1 > x2) {
2091                 swap (x1, x2);
2092         }
2093
2094         if (y1 > y2) {
2095                 swap (y1, y2);
2096         }
2097
2098         // TODO: Make this faster by storing the last updated selection rect, and only
2099         // adjusting things that are in the area that appears/disappeared.
2100         // We probably need a tree to be able to find events in O(log(n)) time.
2101
2102         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
2103
2104                 /* check if any corner of the note is inside the rect
2105
2106                    Notes:
2107                    1) this is computing "touched by", not "contained by" the rect.
2108                    2) this does not require that events be sorted in time.
2109                 */
2110
2111                 const double ix1 = (*i)->x1();
2112                 const double ix2 = (*i)->x2();
2113                 const double iy1 = (*i)->y1();
2114                 const double iy2 = (*i)->y2();
2115
2116                 if ((ix1 >= x1 && ix1 <= x2 && iy1 >= y1 && iy1 <= y2) ||
2117                     (ix1 >= x1 && ix1 <= x2 && iy2 >= y1 && iy2 <= y2) ||
2118                     (ix2 >= x1 && ix2 <= x2 && iy1 >= y1 && iy1 <= y2) ||
2119                     (ix2 >= x1 && ix2 <= x2 && iy2 >= y1 && iy2 <= y2)) {
2120
2121                         // Inside rectangle
2122                         if (!(*i)->selected()) {
2123                                 add_to_selection (*i);
2124                         }
2125                 } else if ((*i)->selected()) {
2126                         // Not inside rectangle
2127                         remove_from_selection (*i);
2128                 }
2129         }
2130 }
2131
2132 void
2133 MidiRegionView::remove_from_selection (CanvasNoteEvent* ev)
2134 {
2135         Selection::iterator i = _selection.find (ev);
2136
2137         if (i != _selection.end()) {
2138                 _selection.erase (i);
2139         }
2140
2141         ev->set_selected (false);
2142         ev->hide_velocity ();
2143
2144         if (_selection.empty()) {
2145                 PublicEditor& editor (trackview.editor());
2146                 editor.get_selection().remove (this);
2147         }
2148 }
2149
2150 void
2151 MidiRegionView::add_to_selection (CanvasNoteEvent* ev)
2152 {
2153         bool add_mrv_selection = false;
2154
2155         if (_selection.empty()) {
2156                 add_mrv_selection = true;
2157         }
2158
2159         if (_selection.insert (ev).second) {
2160                 ev->set_selected (true);
2161                 play_midi_note ((ev)->note());
2162         }
2163
2164         if (add_mrv_selection) {
2165                 PublicEditor& editor (trackview.editor());
2166                 editor.get_selection().add (this);
2167         }
2168 }
2169
2170 void
2171 MidiRegionView::move_selection(double dx, double dy, double cumulative_dy)
2172 {
2173         typedef vector<boost::shared_ptr<NoteType> > PossibleChord;
2174         PossibleChord to_play;
2175         Evoral::MusicalTime earliest = Evoral::MaxMusicalTime;
2176
2177         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2178                 if ((*i)->note()->time() < earliest) {
2179                         earliest = (*i)->note()->time();
2180                 }
2181         }
2182
2183         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2184                 if (Evoral::musical_time_equal ((*i)->note()->time(), earliest)) {
2185                         to_play.push_back ((*i)->note());
2186                 }
2187                 (*i)->move_event(dx, dy);
2188         }
2189
2190         if (dy && !_selection.empty() && !_no_sound_notes && trackview.editor().sound_notes()) {
2191
2192                 if (to_play.size() > 1) {
2193
2194                         PossibleChord shifted;
2195
2196                         for (PossibleChord::iterator n = to_play.begin(); n != to_play.end(); ++n) {
2197                                 boost::shared_ptr<NoteType> moved_note (new NoteType (**n));
2198                                 moved_note->set_note (moved_note->note() + cumulative_dy);
2199                                 shifted.push_back (moved_note);
2200                         }
2201
2202                         play_midi_chord (shifted);
2203
2204                 } else if (!to_play.empty()) {
2205
2206                         boost::shared_ptr<NoteType> moved_note (new NoteType (*to_play.front()));
2207                         moved_note->set_note (moved_note->note() + cumulative_dy);
2208                         play_midi_note (moved_note);
2209                 }
2210         }
2211 }
2212
2213 void
2214 MidiRegionView::note_dropped(CanvasNoteEvent *, frameoffset_t dt, int8_t dnote)
2215 {
2216         assert (!_selection.empty());
2217
2218         uint8_t lowest_note_in_selection  = 127;
2219         uint8_t highest_note_in_selection = 0;
2220         uint8_t highest_note_difference = 0;
2221
2222         // find highest and lowest notes first
2223
2224         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2225                 uint8_t pitch = (*i)->note()->note();
2226                 lowest_note_in_selection  = std::min(lowest_note_in_selection,  pitch);
2227                 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
2228         }
2229
2230         /*
2231           cerr << "dnote: " << (int) dnote << endl;
2232           cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note())
2233           << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
2234           cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): "
2235           << int(highest_note_in_selection) << endl;
2236           cerr << "selection size: " << _selection.size() << endl;
2237           cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
2238         */
2239
2240         // Make sure the note pitch does not exceed the MIDI standard range
2241         if (highest_note_in_selection + dnote > 127) {
2242                 highest_note_difference = highest_note_in_selection - 127;
2243         }
2244
2245         start_note_diff_command (_("move notes"));
2246
2247         for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ++i) {
2248
2249                 Evoral::MusicalTime new_time = frames_to_beats (beats_to_frames ((*i)->note()->time()) + dt);
2250
2251                 if (new_time < 0) {
2252                         continue;
2253                 }
2254
2255                 note_diff_add_change (*i, MidiModel::NoteDiffCommand::StartTime, new_time);
2256
2257                 uint8_t original_pitch = (*i)->note()->note();
2258                 uint8_t new_pitch      = original_pitch + dnote - highest_note_difference;
2259
2260                 // keep notes in standard midi range
2261                 clamp_to_0_127(new_pitch);
2262
2263                 // keep original pitch if note is dragged outside valid midi range
2264                 if ((original_pitch != 0 && new_pitch == 0)
2265                     || (original_pitch != 127 && new_pitch == 127)) {
2266                         new_pitch = original_pitch;
2267                 }
2268
2269                 lowest_note_in_selection  = std::min(lowest_note_in_selection,  new_pitch);
2270                 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
2271
2272                 note_diff_add_change (*i, MidiModel::NoteDiffCommand::NoteNumber, new_pitch);
2273         }
2274
2275         apply_diff();
2276
2277         // care about notes being moved beyond the upper/lower bounds on the canvas
2278         if (lowest_note_in_selection  < midi_stream_view()->lowest_note() ||
2279             highest_note_in_selection > midi_stream_view()->highest_note()) {
2280                 midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
2281         }
2282 }
2283
2284 framepos_t
2285 MidiRegionView::snap_pixel_to_frame(double x)
2286 {
2287         PublicEditor& editor (trackview.editor());
2288         return snap_frame_to_frame (editor.pixel_to_frame (x));
2289 }
2290
2291 /** Snap a frame offset within our region using the current snap settings.
2292  *  @param x Frame offset from this region's position.
2293  *  @return Snapped frame offset from this region's position.
2294  */
2295 frameoffset_t
2296 MidiRegionView::snap_frame_to_frame (frameoffset_t x)
2297 {
2298         PublicEditor& editor = trackview.editor();
2299
2300         /* x is region relative, convert it to global absolute frames */
2301         framepos_t const session_frame = x + _region->position();
2302
2303         /* try a snap in either direction */
2304         framepos_t frame = session_frame;
2305         editor.snap_to (frame, 0);
2306
2307         /* if we went off the beginning of the region, snap forwards */
2308         if (frame < _region->position ()) {
2309                 frame = session_frame;
2310                 editor.snap_to (frame, 1);
2311         }
2312
2313         /* back to region relative */
2314         return frame - _region->position();
2315 }
2316
2317 double
2318 MidiRegionView::snap_to_pixel(double x)
2319 {
2320         return (double) trackview.editor().frame_to_pixel(snap_pixel_to_frame(x));
2321 }
2322
2323 double
2324 MidiRegionView::get_position_pixels()
2325 {
2326         framepos_t region_frame = get_position();
2327         return trackview.editor().frame_to_pixel(region_frame);
2328 }
2329
2330 double
2331 MidiRegionView::get_end_position_pixels()
2332 {
2333         framepos_t frame = get_position() + get_duration ();
2334         return trackview.editor().frame_to_pixel(frame);
2335 }
2336
2337 framepos_t
2338 MidiRegionView::beats_to_frames(double beats) const
2339 {
2340         return _time_converter.to(beats);
2341 }
2342
2343 double
2344 MidiRegionView::frames_to_beats(framepos_t frames) const
2345 {
2346         return _time_converter.from(frames);
2347 }
2348
2349 void
2350 MidiRegionView::begin_resizing (bool /*at_front*/)
2351 {
2352         _resize_data.clear();
2353
2354         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2355                 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
2356
2357                 // only insert CanvasNotes into the map
2358                 if (note) {
2359                         NoteResizeData *resize_data = new NoteResizeData();
2360                         resize_data->canvas_note = note;
2361
2362                         // create a new SimpleRect from the note which will be the resize preview
2363                         SimpleRect *resize_rect = new SimpleRect(
2364                                 *_note_group, note->x1(), note->y1(), note->x2(), note->y2());
2365
2366                         // calculate the colors: get the color settings
2367                         uint32_t fill_color = UINT_RGBA_CHANGE_A(
2368                                 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(),
2369                                 128);
2370
2371                         // make the resize preview notes more transparent and bright
2372                         fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
2373
2374                         // calculate color based on note velocity
2375                         resize_rect->property_fill_color_rgba() = UINT_INTERPOLATE(
2376                                 CanvasNoteEvent::meter_style_fill_color(note->note()->velocity(), note->selected()),
2377                                 fill_color,
2378                                 0.85);
2379
2380                         resize_rect->property_outline_color_rgba() = CanvasNoteEvent::calculate_outline(
2381                                 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get());
2382
2383                         resize_data->resize_rect = resize_rect;
2384                         _resize_data.push_back(resize_data);
2385                 }
2386         }
2387 }
2388
2389 /** Update resizing notes while user drags.
2390  * @param primary `primary' note for the drag; ie the one that is used as the reference in non-relative mode.
2391  * @param at_front which end of the note (true == note on, false == note off)
2392  * @param delta_x change in mouse position since the start of the drag
2393  * @param relative true if relative resizing is taking place, false if absolute resizing.  This only makes
2394  * a difference when multiple notes are being resized; in relative mode, each note's length is changed by the
2395  * amount of the drag.  In non-relative mode, all selected notes are set to have the same start or end point
2396  * as the \a primary note.
2397  */
2398 void
2399 MidiRegionView::update_resizing (ArdourCanvas::CanvasNoteEvent* primary, bool at_front, double delta_x, bool relative)
2400 {
2401         bool cursor_set = false;
2402
2403         for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
2404                 SimpleRect* resize_rect = (*i)->resize_rect;
2405                 CanvasNote* canvas_note = (*i)->canvas_note;
2406                 double current_x;
2407
2408                 if (at_front) {
2409                         if (relative) {
2410                                 current_x = canvas_note->x1() + delta_x;
2411                         } else {
2412                                 current_x = primary->x1() + delta_x;
2413                         }
2414                 } else {
2415                         if (relative) {
2416                                 current_x = canvas_note->x2() + delta_x;
2417                         } else {
2418                                 current_x = primary->x2() + delta_x;
2419                         }
2420                 }
2421
2422                 if (at_front) {
2423                         resize_rect->property_x1() = snap_to_pixel(current_x);
2424                         resize_rect->property_x2() = canvas_note->x2();
2425                 } else {
2426                         resize_rect->property_x2() = snap_to_pixel(current_x);
2427                         resize_rect->property_x1() = canvas_note->x1();
2428                 }
2429
2430                 if (!cursor_set) {
2431                         double beats;
2432
2433                         beats = snap_pixel_to_frame (current_x);
2434                         beats = frames_to_beats (beats);
2435
2436                         double len;
2437
2438                         if (at_front) {
2439                                 if (beats < canvas_note->note()->end_time()) {
2440                                         len = canvas_note->note()->time() - beats;
2441                                         len += canvas_note->note()->length();
2442                                 } else {
2443                                         len = 0;
2444                                 }
2445                         } else {
2446                                 if (beats >= canvas_note->note()->time()) {
2447                                         len = beats - canvas_note->note()->time();
2448                                 } else {
2449                                         len = 0;
2450                                 }
2451                         }
2452
2453                         char buf[16];
2454                         snprintf (buf, sizeof (buf), "%.3g beats", len);
2455                         show_verbose_cursor (buf, 0, 0);
2456
2457                         cursor_set = true;
2458                 }
2459
2460         }
2461 }
2462
2463
2464 /** Finish resizing notes when the user releases the mouse button.
2465  *  Parameters the same as for \a update_resizing().
2466  */
2467 void
2468 MidiRegionView::commit_resizing (ArdourCanvas::CanvasNoteEvent* primary, bool at_front, double delta_x, bool relative)
2469 {
2470         start_note_diff_command (_("resize notes"));
2471
2472         for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
2473                 CanvasNote*  canvas_note = (*i)->canvas_note;
2474                 SimpleRect*  resize_rect = (*i)->resize_rect;
2475                 double current_x;
2476
2477                 if (at_front) {
2478                         if (relative) {
2479                                 current_x = canvas_note->x1() + delta_x;
2480                         } else {
2481                                 current_x = primary->x1() + delta_x;
2482                         }
2483                 } else {
2484                         if (relative) {
2485                                 current_x = canvas_note->x2() + delta_x;
2486                         } else {
2487                                 current_x = primary->x2() + delta_x;
2488                         }
2489                 }
2490
2491                 current_x = snap_pixel_to_frame (current_x);
2492                 current_x = frames_to_beats (current_x);
2493
2494                 if (at_front && current_x < canvas_note->note()->end_time()) {
2495                         note_diff_add_change (canvas_note, MidiModel::NoteDiffCommand::StartTime, current_x);
2496
2497                         double len = canvas_note->note()->time() - current_x;
2498                         len += canvas_note->note()->length();
2499
2500                         if (len > 0) {
2501                                 /* XXX convert to beats */
2502                                 note_diff_add_change (canvas_note, MidiModel::NoteDiffCommand::Length, len);
2503                         }
2504                 }
2505
2506                 if (!at_front) {
2507                         double len = current_x - canvas_note->note()->time();
2508
2509                         if (len > 0) {
2510                                 /* XXX convert to beats */
2511                                 note_diff_add_change (canvas_note, MidiModel::NoteDiffCommand::Length, len);
2512                         }
2513                 }
2514
2515                 delete resize_rect;
2516                 delete (*i);
2517         }
2518
2519         _resize_data.clear();
2520         apply_diff();
2521 }
2522
2523 void
2524 MidiRegionView::change_note_velocity(CanvasNoteEvent* event, int8_t velocity, bool relative)
2525 {
2526         uint8_t new_velocity;
2527
2528         if (relative) {
2529                 new_velocity = event->note()->velocity() + velocity;
2530                 clamp_to_0_127(new_velocity);
2531         } else {
2532                 new_velocity = velocity;
2533         }
2534
2535         event->set_selected (event->selected()); // change color
2536
2537         note_diff_add_change (event, MidiModel::NoteDiffCommand::Velocity, new_velocity);
2538 }
2539
2540 void
2541 MidiRegionView::change_note_note (CanvasNoteEvent* event, int8_t note, bool relative)
2542 {
2543         uint8_t new_note;
2544
2545         if (relative) {
2546                 new_note = event->note()->note() + note;
2547         } else {
2548                 new_note = note;
2549         }
2550
2551         clamp_to_0_127 (new_note);
2552         note_diff_add_change (event, MidiModel::NoteDiffCommand::NoteNumber, new_note);
2553 }
2554
2555 void
2556 MidiRegionView::trim_note (CanvasNoteEvent* event, Evoral::MusicalTime front_delta, Evoral::MusicalTime end_delta)
2557 {
2558         bool change_start = false;
2559         bool change_length = false;
2560         Evoral::MusicalTime new_start = 0;
2561         Evoral::MusicalTime new_length = 0;
2562
2563         /* NOTE: the semantics of the two delta arguments are slightly subtle:
2564
2565            front_delta: if positive - move the start of the note later in time (shortening it)
2566            if negative - move the start of the note earlier in time (lengthening it)
2567
2568            end_delta:   if positive - move the end of the note later in time (lengthening it)
2569            if negative - move the end of the note earlier in time (shortening it)
2570         */
2571
2572         if (front_delta) {
2573                 if (front_delta < 0) {
2574
2575                         if (event->note()->time() < -front_delta) {
2576                                 new_start = 0;
2577                         } else {
2578                                 new_start = event->note()->time() + front_delta; // moves earlier
2579                         }
2580
2581                         /* start moved toward zero, so move the end point out to where it used to be.
2582                            Note that front_delta is negative, so this increases the length.
2583                         */
2584
2585                         new_length = event->note()->length() - front_delta;
2586                         change_start = true;
2587                         change_length = true;
2588
2589                 } else {
2590
2591                         Evoral::MusicalTime new_pos = event->note()->time() + front_delta;
2592
2593                         if (new_pos < event->note()->end_time()) {
2594                                 new_start = event->note()->time() + front_delta;
2595                                 /* start moved toward the end, so move the end point back to where it used to be */
2596                                 new_length = event->note()->length() - front_delta;
2597                                 change_start = true;
2598                                 change_length = true;
2599                         }
2600                 }
2601
2602         }
2603
2604         if (end_delta) {
2605                 bool can_change = true;
2606                 if (end_delta < 0) {
2607                         if (event->note()->length() < -end_delta) {
2608                                 can_change = false;
2609                         }
2610                 }
2611
2612                 if (can_change) {
2613                         new_length = event->note()->length() + end_delta;
2614                         change_length = true;
2615                 }
2616         }
2617
2618         if (change_start) {
2619                 note_diff_add_change (event, MidiModel::NoteDiffCommand::StartTime, new_start);
2620         }
2621
2622         if (change_length) {
2623                 note_diff_add_change (event, MidiModel::NoteDiffCommand::Length, new_length);
2624         }
2625 }
2626
2627 void
2628 MidiRegionView::change_note_channel (CanvasNoteEvent* event, int8_t chn, bool relative)
2629 {
2630         uint8_t new_channel;
2631
2632         if (relative) {
2633                 if (chn < 0.0) {
2634                         if (event->note()->channel() < -chn) {
2635                                 new_channel = 0;
2636                         } else {
2637                                 new_channel = event->note()->channel() + chn;
2638                         }
2639                 } else {
2640                         new_channel = event->note()->channel() + chn;
2641                 }
2642         } else {
2643                 new_channel = (uint8_t) chn;
2644         }
2645
2646         note_diff_add_change (event, MidiModel::NoteDiffCommand::Channel, new_channel);
2647 }
2648
2649 void
2650 MidiRegionView::change_note_time (CanvasNoteEvent* event, Evoral::MusicalTime delta, bool relative)
2651 {
2652         Evoral::MusicalTime new_time;
2653
2654         if (relative) {
2655                 if (delta < 0.0) {
2656                         if (event->note()->time() < -delta) {
2657                                 new_time = 0;
2658                         } else {
2659                                 new_time = event->note()->time() + delta;
2660                         }
2661                 } else {
2662                         new_time = event->note()->time() + delta;
2663                 }
2664         } else {
2665                 new_time = delta;
2666         }
2667
2668         note_diff_add_change (event, MidiModel::NoteDiffCommand::StartTime, new_time);
2669 }
2670
2671 void
2672 MidiRegionView::change_note_length (CanvasNoteEvent* event, Evoral::MusicalTime t)
2673 {
2674         note_diff_add_change (event, MidiModel::NoteDiffCommand::Length, t);
2675 }
2676
2677 void
2678 MidiRegionView::change_velocities (bool up, bool fine, bool allow_smush)
2679 {
2680         int8_t delta;
2681
2682         if (_selection.empty()) {
2683                 return;
2684         }
2685
2686         if (fine) {
2687                 delta = 1;
2688         } else {
2689                 delta = 10;
2690         }
2691
2692         if (!up) {
2693                 delta = -delta;
2694         }
2695
2696         if (!allow_smush) {
2697                 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2698                         if ((*i)->note()->velocity() + delta == 0 || (*i)->note()->velocity() + delta == 127) {
2699                                 return;
2700                         }
2701                 }
2702         }
2703
2704         start_note_diff_command (_("change velocities"));
2705
2706         for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
2707                 Selection::iterator next = i;
2708                 ++next;
2709                 change_note_velocity (*i, delta, true);
2710                 i = next;
2711         }
2712
2713         apply_diff();
2714
2715         if (!_selection.empty()) {
2716                 char buf[24];
2717                 snprintf (buf, sizeof (buf), "Vel %d",
2718                           (int) (*_selection.begin())->note()->velocity());
2719                 show_verbose_cursor (buf, 10, 10);
2720         }
2721 }
2722
2723
2724 void
2725 MidiRegionView::transpose (bool up, bool fine, bool allow_smush)
2726 {
2727         if (_selection.empty()) {
2728                 return;
2729         }
2730
2731         int8_t delta;
2732
2733         if (fine) {
2734                 delta = 1;
2735         } else {
2736                 delta = 12;
2737         }
2738
2739         if (!up) {
2740                 delta = -delta;
2741         }
2742
2743         if (!allow_smush) {
2744                 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2745                         if (!up) {
2746                                 if ((int8_t) (*i)->note()->note() + delta <= 0) {
2747                                         return;
2748                                 }
2749                         } else {
2750                                 if ((int8_t) (*i)->note()->note() + delta > 127) {
2751                                         return;
2752                                 }
2753                         }
2754                 }
2755         }
2756
2757         start_note_diff_command (_("transpose"));
2758
2759         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
2760                 Selection::iterator next = i;
2761                 ++next;
2762                 change_note_note (*i, delta, true);
2763                 i = next;
2764         }
2765
2766         apply_diff ();
2767 }
2768
2769 void
2770 MidiRegionView::change_note_lengths (bool fine, bool shorter, Evoral::MusicalTime delta, bool start, bool end)
2771 {
2772         if (delta == 0.0) {
2773                 if (fine) {
2774                         delta = 1.0/128.0;
2775                 } else {
2776                         /* grab the current grid distance */
2777                         bool success;
2778                         delta = trackview.editor().get_grid_type_as_beats (success, _region->position());
2779                         if (!success) {
2780                                 /* XXX cannot get grid type as beats ... should always be possible ... FIX ME */
2781                                 cerr << "Grid type not available as beats - TO BE FIXED\n";
2782                                 return;
2783                         }
2784                 }
2785         }
2786
2787         if (shorter) {
2788                 delta = -delta;
2789         }
2790
2791         start_note_diff_command (_("change note lengths"));
2792
2793         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
2794                 Selection::iterator next = i;
2795                 ++next;
2796
2797                 /* note the negation of the delta for start */
2798
2799                 trim_note (*i, (start ? -delta : 0), (end ? delta : 0));
2800                 i = next;
2801         }
2802
2803         apply_diff ();
2804
2805 }
2806
2807 void
2808 MidiRegionView::nudge_notes (bool forward)
2809 {
2810         if (_selection.empty()) {
2811                 return;
2812         }
2813
2814         /* pick a note as the point along the timeline to get the nudge distance.
2815            its not necessarily the earliest note, so we may want to pull the notes out
2816            into a vector and sort before using the first one.
2817         */
2818
2819         framepos_t ref_point = _region->position() + beats_to_frames ((*(_selection.begin()))->note()->time());
2820         framepos_t unused;
2821         framepos_t distance;
2822
2823         if (trackview.editor().snap_mode() == Editing::SnapOff) {
2824
2825                 /* grid is off - use nudge distance */
2826
2827                 distance = trackview.editor().get_nudge_distance (ref_point, unused);
2828
2829         } else {
2830
2831                 /* use grid */
2832
2833                 framepos_t next_pos = ref_point;
2834
2835                 if (forward) {
2836                         if (max_framepos - 1 < next_pos) {
2837                                 next_pos += 1;
2838                         }
2839                 } else {
2840                         if (next_pos == 0) {
2841                                 return;
2842                         }
2843                         next_pos -= 1;
2844                 }
2845
2846                 trackview.editor().snap_to (next_pos, (forward ? 1 : -1), false);
2847                 distance = ref_point - next_pos;
2848         }
2849
2850         if (distance == 0) {
2851                 return;
2852         }
2853
2854         Evoral::MusicalTime delta = frames_to_beats (fabs (distance));
2855
2856         if (!forward) {
2857                 delta = -delta;
2858         }
2859
2860         start_note_diff_command (_("nudge"));
2861
2862         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
2863                 Selection::iterator next = i;
2864                 ++next;
2865                 change_note_time (*i, delta, true);
2866                 i = next;
2867         }
2868
2869         apply_diff ();
2870 }
2871
2872 void
2873 MidiRegionView::change_channel(uint8_t channel)
2874 {
2875         start_note_diff_command(_("change channel"));
2876         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2877                 note_diff_add_change (*i, MidiModel::NoteDiffCommand::Channel, channel);
2878         }
2879
2880         apply_diff();
2881 }
2882
2883
2884 void
2885 MidiRegionView::note_entered(ArdourCanvas::CanvasNoteEvent* ev)
2886 {
2887         Editor* editor = dynamic_cast<Editor*>(&trackview.editor());
2888
2889         _pre_enter_cursor = editor->get_canvas_cursor ();
2890
2891         if (_mouse_state == SelectTouchDragging) {
2892                 note_selected (ev, true);
2893         }
2894
2895         show_verbose_cursor (ev->note ());
2896 }
2897
2898 void
2899 MidiRegionView::note_left (ArdourCanvas::CanvasNoteEvent*)
2900 {
2901         Editor* editor = dynamic_cast<Editor*>(&trackview.editor());
2902
2903         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2904                 (*i)->hide_velocity ();
2905         }
2906
2907         editor->verbose_cursor()->hide ();
2908
2909         if (_pre_enter_cursor) {
2910                 editor->set_canvas_cursor (_pre_enter_cursor);
2911                 _pre_enter_cursor = 0;
2912         }
2913 }
2914
2915 void
2916 MidiRegionView::patch_entered (ArdourCanvas::CanvasPatchChange* ev)
2917 {
2918         ostringstream s;
2919         s << ((int) ev->patch()->program() + 1) << ":" << (ev->patch()->bank() + 1);
2920         show_verbose_cursor (s.str(), 10, 20);
2921 }
2922
2923 void
2924 MidiRegionView::patch_left (ArdourCanvas::CanvasPatchChange *)
2925 {
2926         trackview.editor().verbose_cursor()->hide ();
2927 }
2928
2929 void
2930 MidiRegionView::note_mouse_position (float x_fraction, float /*y_fraction*/, bool can_set_cursor)
2931 {
2932         Editor* editor = dynamic_cast<Editor*>(&trackview.editor());
2933
2934         if (x_fraction > 0.0 && x_fraction < 0.25) {
2935                 editor->set_canvas_cursor (editor->cursors()->left_side_trim);
2936         } else if (x_fraction >= 0.75 && x_fraction < 1.0) {
2937                 editor->set_canvas_cursor (editor->cursors()->right_side_trim);
2938         } else {
2939                 if (_pre_enter_cursor && can_set_cursor) {
2940                         editor->set_canvas_cursor (_pre_enter_cursor);
2941                 }
2942         }
2943 }
2944
2945 void
2946 MidiRegionView::set_frame_color()
2947 {
2948         uint32_t f;
2949
2950         TimeAxisViewItem::set_frame_color ();
2951
2952         if (!frame) {
2953                 return;
2954         }
2955
2956         if (_selected) {
2957                 f = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
2958         } else if (high_enough_for_name) {
2959                 f= ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
2960         } else {
2961                 f = fill_color;
2962         }
2963
2964         if (!rect_visible) {
2965                 f = UINT_RGBA_CHANGE_A (f, 0);
2966         }
2967
2968         frame->property_fill_color_rgba() = f;
2969 }
2970
2971 void
2972 MidiRegionView::midi_channel_mode_changed(ChannelMode mode, uint16_t mask)
2973 {
2974         switch (mode) {
2975         case AllChannels:
2976         case FilterChannels:
2977                 _force_channel = -1;
2978                 break;
2979         case ForceChannel:
2980                 _force_channel = mask;
2981                 mask = 0xFFFF; // Show all notes as active (below)
2982         };
2983
2984         // Update notes for selection
2985         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
2986                 (*i)->on_channel_selection_change(mask);
2987         }
2988
2989         _last_channel_selection = mask;
2990
2991         _patch_changes.clear ();
2992         display_patch_changes ();
2993 }
2994
2995 void
2996 MidiRegionView::midi_patch_settings_changed(std::string model, std::string custom_device_mode)
2997 {
2998         _model_name         = model;
2999         _custom_device_mode = custom_device_mode;
3000         redisplay_model();
3001 }
3002
3003 void
3004 MidiRegionView::cut_copy_clear (Editing::CutCopyOp op)
3005 {
3006         if (_selection.empty()) {
3007                 return;
3008         }
3009
3010         PublicEditor& editor (trackview.editor());
3011
3012         switch (op) {
3013         case Delete:
3014                 /* XXX what to do ? */
3015                 break;
3016         case Cut:
3017         case Copy:
3018                 editor.get_cut_buffer().add (selection_as_cut_buffer());
3019                 break;
3020         default:
3021                 break;
3022         }
3023
3024         if (op != Copy) {
3025
3026                 start_note_diff_command();
3027
3028                 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
3029                         switch (op) {
3030                         case Copy:
3031                                 break;
3032                         case Delete:
3033                         case Cut:
3034                         case Clear:
3035                                 note_diff_remove_note (*i);
3036                                 break;
3037                         }
3038                 }
3039
3040                 apply_diff();
3041         }
3042 }
3043
3044 MidiCutBuffer*
3045 MidiRegionView::selection_as_cut_buffer () const
3046 {
3047         Notes notes;
3048
3049         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
3050                 NoteType* n = (*i)->note().get();
3051                 notes.insert (boost::shared_ptr<NoteType> (new NoteType (*n)));
3052         }
3053
3054         MidiCutBuffer* cb = new MidiCutBuffer (trackview.session());
3055         cb->set (notes);
3056
3057         return cb;
3058 }
3059
3060 /** This method handles undo */
3061 void
3062 MidiRegionView::paste (framepos_t pos, float times, const MidiCutBuffer& mcb)
3063 {
3064         if (mcb.empty()) {
3065                 return;
3066         }
3067
3068         DEBUG_TRACE (DEBUG::CutNPaste, string_compose ("MIDI paste @ %1 times %2\n", pos, times));
3069
3070         trackview.session()->begin_reversible_command (_("paste"));
3071
3072         start_note_diff_command (_("paste"));
3073
3074         Evoral::MusicalTime beat_delta;
3075         Evoral::MusicalTime paste_pos_beats;
3076         Evoral::MusicalTime duration;
3077         Evoral::MusicalTime end_point = 0;
3078
3079         duration = (*mcb.notes().rbegin())->end_time() - (*mcb.notes().begin())->time();
3080         paste_pos_beats = frames_to_beats (pos - _region->position());
3081         beat_delta = (*mcb.notes().begin())->time() - paste_pos_beats;
3082         paste_pos_beats = 0;
3083
3084         DEBUG_TRACE (DEBUG::CutNPaste, string_compose ("Paste data spans from %1 to %2 (%3) ; paste pos beats = %4 (based on %5 - %6 ; beat delta = %7\n",
3085                                                        (*mcb.notes().begin())->time(),
3086                                                        (*mcb.notes().rbegin())->end_time(),
3087                                                        duration, pos, _region->position(),
3088                                                        paste_pos_beats, beat_delta));
3089
3090         clear_selection ();
3091
3092         for (int n = 0; n < (int) times; ++n) {
3093
3094                 for (Notes::const_iterator i = mcb.notes().begin(); i != mcb.notes().end(); ++i) {
3095
3096                         boost::shared_ptr<NoteType> copied_note (new NoteType (*((*i).get())));
3097                         copied_note->set_time (paste_pos_beats + copied_note->time() - beat_delta);
3098
3099                         /* make all newly added notes selected */
3100
3101                         note_diff_add_note (copied_note, true);
3102                         end_point = copied_note->end_time();
3103                 }
3104
3105                 paste_pos_beats += duration;
3106         }
3107
3108         /* if we pasted past the current end of the region, extend the region */
3109
3110         framepos_t end_frame = _region->position() + beats_to_frames (end_point);
3111         framepos_t region_end = _region->position() + _region->length() - 1;
3112
3113         if (end_frame > region_end) {
3114
3115                 DEBUG_TRACE (DEBUG::CutNPaste, string_compose ("Paste extended region from %1 to %2\n", region_end, end_frame));
3116
3117                 _region->clear_changes ();
3118                 _region->set_length (end_frame);
3119                 trackview.session()->add_command (new StatefulDiffCommand (_region));
3120         }
3121
3122         apply_diff (true);
3123
3124         trackview.session()->commit_reversible_command ();
3125 }
3126
3127 struct EventNoteTimeEarlyFirstComparator {
3128         bool operator() (CanvasNoteEvent* a, CanvasNoteEvent* b) {
3129                 return a->note()->time() < b->note()->time();
3130         }
3131 };
3132
3133 void
3134 MidiRegionView::time_sort_events ()
3135 {
3136         if (!_sort_needed) {
3137                 return;
3138         }
3139
3140         EventNoteTimeEarlyFirstComparator cmp;
3141         _events.sort (cmp);
3142
3143         _sort_needed = false;
3144 }
3145
3146 void
3147 MidiRegionView::goto_next_note ()
3148 {
3149         // framepos_t pos = -1;
3150         bool use_next = false;
3151
3152         if (_events.back()->selected()) {
3153                 return;
3154         }
3155
3156         time_sort_events ();
3157
3158         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
3159                 if ((*i)->selected()) {
3160                         use_next = true;
3161                         continue;
3162                 } else if (use_next) {
3163                         unique_select (*i);
3164                         // pos = _region->position() + beats_to_frames ((*i)->note()->time());
3165                         return;
3166                 }
3167         }
3168
3169         /* use the first one */
3170
3171         unique_select (_events.front());
3172
3173 }
3174
3175 void
3176 MidiRegionView::goto_previous_note ()
3177 {
3178         // framepos_t pos = -1;
3179         bool use_next = false;
3180
3181         if (_events.front()->selected()) {
3182                 return;
3183         }
3184
3185         time_sort_events ();
3186
3187         for (Events::reverse_iterator i = _events.rbegin(); i != _events.rend(); ++i) {
3188                 if ((*i)->selected()) {
3189                         use_next = true;
3190                         continue;
3191                 } else if (use_next) {
3192                         unique_select (*i);
3193                         // pos = _region->position() + beats_to_frames ((*i)->note()->time());
3194                         return;
3195                 }
3196         }
3197
3198         /* use the last one */
3199
3200         unique_select (*(_events.rbegin()));
3201 }
3202
3203 void
3204 MidiRegionView::selection_as_notelist (Notes& selected, bool allow_all_if_none_selected)
3205 {
3206         bool had_selected = false;
3207
3208         time_sort_events ();
3209
3210         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
3211                 if ((*i)->selected()) {
3212                         selected.insert ((*i)->note());
3213                         had_selected = true;
3214                 }
3215         }
3216
3217         if (allow_all_if_none_selected && !had_selected) {
3218                 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
3219                         selected.insert ((*i)->note());
3220                 }
3221         }
3222 }
3223
3224 void
3225 MidiRegionView::update_ghost_note (double x, double y)
3226 {
3227         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
3228
3229         _last_ghost_x = x;
3230         _last_ghost_y = y;
3231
3232         _note_group->w2i (x, y);
3233         framepos_t const f = snap_pixel_to_frame (x);
3234
3235         bool success;
3236         Evoral::MusicalTime beats = trackview.editor().get_grid_type_as_beats (success, f);
3237
3238         if (!success) {
3239                 beats = 1;
3240         }
3241
3242         double length = frames_to_beats (snap_frame_to_frame (f + beats_to_frames (beats)) - f);
3243
3244         _ghost_note->note()->set_time (frames_to_beats (f + _region->start()));
3245         _ghost_note->note()->set_length (length);
3246         _ghost_note->note()->set_note (midi_stream_view()->y_to_note (y));
3247         _ghost_note->note()->set_channel (mtv->get_channel_for_add ());
3248
3249         /* the ghost note does not appear in ghost regions, so pass false in here */
3250         update_note (_ghost_note, false);
3251
3252         show_verbose_cursor (_ghost_note->note ());
3253 }
3254
3255 void
3256 MidiRegionView::create_ghost_note (double x, double y)
3257 {
3258         delete _ghost_note;
3259         _ghost_note = 0;
3260
3261         boost::shared_ptr<NoteType> g (new NoteType);
3262         _ghost_note = new NoEventCanvasNote (*this, *_note_group, g);
3263         _ghost_note->property_outline_color_rgba() = 0x000000aa;
3264         update_ghost_note (x, y);
3265         _ghost_note->show ();
3266
3267         _last_ghost_x = x;
3268         _last_ghost_y = y;
3269
3270         show_verbose_cursor (_ghost_note->note ());
3271 }
3272
3273 void
3274 MidiRegionView::snap_changed ()
3275 {
3276         if (!_ghost_note) {
3277                 return;
3278         }
3279
3280         create_ghost_note (_last_ghost_x, _last_ghost_y);
3281 }
3282
3283 void
3284 MidiRegionView::drop_down_keys ()
3285 {
3286         _mouse_state = None;
3287 }
3288
3289 void
3290 MidiRegionView::maybe_select_by_position (GdkEventButton* ev, double /*x*/, double y)
3291 {
3292         double note = midi_stream_view()->y_to_note(y);
3293         Events e;
3294         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
3295
3296         uint16_t chn_mask = mtv->channel_selector().get_selected_channels();
3297
3298         if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
3299                 get_events (e, Evoral::Sequence<Evoral::MusicalTime>::PitchGreaterThanOrEqual, (uint8_t) floor (note), chn_mask);
3300         } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
3301                 get_events (e, Evoral::Sequence<Evoral::MusicalTime>::PitchLessThanOrEqual, (uint8_t) floor (note), chn_mask);
3302         } else {
3303                 return;
3304         }
3305
3306         bool add_mrv_selection = false;
3307
3308         if (_selection.empty()) {
3309                 add_mrv_selection = true;
3310         }
3311
3312         for (Events::iterator i = e.begin(); i != e.end(); ++i) {
3313                 if (_selection.insert (*i).second) {
3314                         (*i)->set_selected (true);
3315                 }
3316         }
3317
3318         if (add_mrv_selection) {
3319                 PublicEditor& editor (trackview.editor());
3320                 editor.get_selection().add (this);
3321         }
3322 }
3323
3324 void
3325 MidiRegionView::color_handler ()
3326 {
3327         RegionView::color_handler ();
3328
3329         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
3330                 (*i)->set_selected ((*i)->selected()); // will change color
3331         }
3332
3333         /* XXX probably more to do here */
3334 }
3335
3336 void
3337 MidiRegionView::enable_display (bool yn)
3338 {
3339         RegionView::enable_display (yn);
3340         if (yn) {
3341                 redisplay_model ();
3342         }
3343 }
3344
3345 void
3346 MidiRegionView::show_step_edit_cursor (Evoral::MusicalTime pos)
3347 {
3348         if (_step_edit_cursor == 0) {
3349                 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
3350
3351                 _step_edit_cursor = new ArdourCanvas::SimpleRect (*group);
3352                 _step_edit_cursor->property_y1() = 0;
3353                 _step_edit_cursor->property_y2() = midi_stream_view()->contents_height();
3354                 _step_edit_cursor->property_fill_color_rgba() = RGBA_TO_UINT (45,0,0,90);
3355                 _step_edit_cursor->property_outline_color_rgba() = RGBA_TO_UINT (85,0,0,90);
3356         }
3357
3358         move_step_edit_cursor (pos);
3359         _step_edit_cursor->show ();
3360 }
3361
3362 void
3363 MidiRegionView::move_step_edit_cursor (Evoral::MusicalTime pos)
3364 {
3365         _step_edit_cursor_position = pos;
3366
3367         if (_step_edit_cursor) {
3368                 double pixel = trackview.editor().frame_to_pixel (beats_to_frames (pos));
3369                 _step_edit_cursor->property_x1() = pixel;
3370                 set_step_edit_cursor_width (_step_edit_cursor_width);
3371         }
3372 }
3373
3374 void
3375 MidiRegionView::hide_step_edit_cursor ()
3376 {
3377         if (_step_edit_cursor) {
3378                 _step_edit_cursor->hide ();
3379         }
3380 }
3381
3382 void
3383 MidiRegionView::set_step_edit_cursor_width (Evoral::MusicalTime beats)
3384 {
3385         _step_edit_cursor_width = beats;
3386
3387         if (_step_edit_cursor) {
3388                 _step_edit_cursor->property_x2() = _step_edit_cursor->property_x1() + trackview.editor().frame_to_pixel (beats_to_frames (beats));
3389         }
3390 }
3391
3392 /** Called when a diskstream on our track has received some data.  Update the view, if applicable.
3393  *  @param buf Data that has been recorded.
3394  *  @param w Source that this data will end up in.
3395  */
3396 void
3397 MidiRegionView::data_recorded (boost::shared_ptr<MidiBuffer> buf, boost::weak_ptr<MidiSource> w)
3398 {
3399         if (!_active_notes) {
3400                 /* we aren't actively being recorded to */
3401                 return;
3402         }
3403
3404         boost::shared_ptr<MidiSource> src = w.lock ();
3405         if (!src || src != midi_region()->midi_source()) {
3406                 /* recorded data was not destined for our source */
3407                 return;
3408         }
3409
3410         MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*> (&trackview);
3411         BeatsFramesConverter converter (trackview.session()->tempo_map(), mtv->midi_track()->get_capture_start_frame (0));
3412
3413         framepos_t back = max_framepos;
3414
3415         for (MidiBuffer::iterator i = buf->begin(); i != buf->end(); ++i) {
3416                 Evoral::MIDIEvent<MidiBuffer::TimeType> const ev (*i, false);
3417                 assert (ev.buffer ());
3418
3419                 Evoral::MusicalTime const time_beats = converter.from (ev.time () - converter.origin_b ());
3420
3421                 if (ev.type() == MIDI_CMD_NOTE_ON) {
3422
3423                         boost::shared_ptr<NoteType> note (
3424                                 new NoteType (ev.channel(), time_beats, 0, ev.note(), ev.velocity())
3425                                                           );
3426
3427                         add_note (note, true);
3428
3429                         /* fix up our note range */
3430                         if (ev.note() < _current_range_min) {
3431                                 midi_stream_view()->apply_note_range (ev.note(), _current_range_max, true);
3432                         } else if (ev.note() > _current_range_max) {
3433                                 midi_stream_view()->apply_note_range (_current_range_min, ev.note(), true);
3434                         }
3435
3436                 } else if (ev.type() == MIDI_CMD_NOTE_OFF) {
3437                         resolve_note (ev.note (), time_beats);
3438                 }
3439
3440                 back = ev.time ();
3441         }
3442
3443         midi_stream_view()->check_record_layers (region(), back);
3444 }
3445
3446 void
3447 MidiRegionView::trim_front_starting ()
3448 {
3449         /* Reparent the note group to the region view's parent, so that it doesn't change
3450            when the region view is trimmed.
3451         */
3452         _temporary_note_group = new ArdourCanvas::Group (*group->property_parent ());
3453         _temporary_note_group->move (group->property_x(), group->property_y());
3454         _note_group->reparent (*_temporary_note_group);
3455 }
3456
3457 void
3458 MidiRegionView::trim_front_ending ()
3459 {
3460         _note_group->reparent (*group);
3461         delete _temporary_note_group;
3462         _temporary_note_group = 0;
3463
3464         if (_region->start() < 0) {
3465                 /* Trim drag made start time -ve; fix this */
3466                 midi_region()->fix_negative_start ();
3467         }
3468 }
3469
3470 void
3471 MidiRegionView::edit_patch_change (ArdourCanvas::CanvasPatchChange* pc)
3472 {
3473         PatchChangeDialog d (&_time_converter, trackview.session(), *pc->patch (), Gtk::Stock::APPLY);
3474         if (d.run () != Gtk::RESPONSE_ACCEPT) {
3475                 return;
3476         }
3477
3478         change_patch_change (pc->patch(), d.patch ());
3479 }
3480
3481
3482 void
3483 MidiRegionView::show_verbose_cursor (boost::shared_ptr<NoteType> n) const
3484 {
3485         char buf[24];
3486         snprintf (buf, sizeof (buf), "%s (%d) Chn %d\nVel %d",
3487                   Evoral::midi_note_name (n->note()).c_str(),
3488                   (int) n->note (),
3489                   (int) n->channel() + 1,
3490                   (int) n->velocity());
3491
3492         show_verbose_cursor (buf, 10, 20);
3493 }
3494
3495 void
3496 MidiRegionView::show_verbose_cursor (string const & text, double xoffset, double yoffset) const
3497 {
3498         double wx, wy;
3499
3500         trackview.editor().get_pointer_position (wx, wy);
3501
3502         wx += xoffset;
3503         wy += yoffset;
3504
3505         /* Flip the cursor above the mouse pointer if it would overlap the bottom of the canvas */
3506
3507         double x1, y1, x2, y2;
3508         trackview.editor().verbose_cursor()->canvas_item()->get_bounds (x1, y1, x2, y2);
3509
3510         if ((wy + y2 - y1) > trackview.editor().canvas_height()) {
3511                 wy -= (y2 - y1) + 2 * yoffset;
3512         }
3513
3514         trackview.editor().verbose_cursor()->set (text, wx, wy);
3515         trackview.editor().verbose_cursor()->show ();
3516 }