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