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