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