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