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