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