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