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