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