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