Fix some warnings.
[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(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
588         clear_events ();
589
590         if (_enable_display) {
591                 redisplay_model();
592         }
593 }
594         
595         
596 void
597 MidiRegionView::start_delta_command(string name)
598 {
599         if (!_delta_command) {
600                 _delta_command = _model->new_delta_command(name);
601         }
602 }
603
604 void
605 MidiRegionView::start_diff_command(string name)
606 {
607         if (!_diff_command) {
608                 _diff_command = _model->new_diff_command(name);
609         }
610 }
611
612 void
613 MidiRegionView::delta_add_note(const boost::shared_ptr<NoteType> note, bool selected, bool show_velocity)
614 {
615         if (_delta_command) {
616                 _delta_command->add(note);
617         }
618         if (selected) {
619                 _marked_for_selection.insert(note);
620         }
621         if (show_velocity) {
622                 _marked_for_velocity.insert(note);
623         }
624 }
625
626 void
627 MidiRegionView::delta_remove_note(ArdourCanvas::CanvasNoteEvent* ev)
628 {
629         if (_delta_command && ev->note()) {
630                 _delta_command->remove(ev->note());
631         }
632 }
633
634 void
635 MidiRegionView::diff_add_change (ArdourCanvas::CanvasNoteEvent* ev, 
636                                  MidiModel::DiffCommand::Property property,
637                                  uint8_t val)
638 {
639         if (_diff_command) {
640                 _diff_command->change (ev->note(), property, val);
641         }
642 }
643
644 void
645 MidiRegionView::diff_add_change (ArdourCanvas::CanvasNoteEvent* ev, 
646                                  MidiModel::DiffCommand::Property property,
647                                  Evoral::MusicalTime val)
648 {
649         if (_diff_command) {
650                 _diff_command->change (ev->note(), property, val);
651         }
652 }
653         
654 void
655 MidiRegionView::apply_delta()
656 {
657         if (!_delta_command) {
658                 return;
659         }
660
661         // Mark all selected notes for selection when model reloads
662         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
663                 _marked_for_selection.insert((*i)->note());
664         }
665         
666         _model->apply_command(trackview.session(), _delta_command);
667         _delta_command = 0; 
668         midi_view()->midi_track()->diskstream()->playlist_modified();
669
670         _marked_for_selection.clear();
671         _marked_for_velocity.clear();
672 }
673
674 void
675 MidiRegionView::apply_diff ()
676 {
677         if (!_diff_command) {
678                 return;
679         }
680
681         _model->apply_command(trackview.session(), _diff_command);
682         _diff_command = 0; 
683         midi_view()->midi_track()->diskstream()->playlist_modified();
684
685         _marked_for_velocity.clear();
686 }
687
688 void
689 MidiRegionView::apply_delta_as_subcommand()
690 {
691         if (!_delta_command) {
692                 return;
693         }
694
695         // Mark all selected notes for selection when model reloads
696         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
697                 _marked_for_selection.insert((*i)->note());
698         }
699         
700         _model->apply_command_as_subcommand(trackview.session(), _delta_command);
701         _delta_command = 0; 
702         midi_view()->midi_track()->diskstream()->playlist_modified();
703
704         _marked_for_selection.clear();
705         _marked_for_velocity.clear();
706 }
707
708 void
709 MidiRegionView::apply_diff_as_subcommand()
710 {
711         if (!_diff_command) {
712                 return;
713         }
714
715         // Mark all selected notes for selection when model reloads
716         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
717                 _marked_for_selection.insert((*i)->note());
718         }
719         
720         _model->apply_command_as_subcommand(trackview.session(), _diff_command);
721         _diff_command = 0; 
722         midi_view()->midi_track()->diskstream()->playlist_modified();
723
724         _marked_for_selection.clear();
725         _marked_for_velocity.clear();
726 }
727
728 void
729 MidiRegionView::abort_command()
730 {
731         delete _delta_command;
732         _delta_command = 0;
733         delete _diff_command;
734         _diff_command = 0;
735         clear_selection();
736 }
737
738 CanvasNoteEvent*
739 MidiRegionView::find_canvas_note (boost::shared_ptr<NoteType> note)
740 {
741         if (_optimization_iterator != _events.end()) {
742                 ++_optimization_iterator;
743         }
744         
745         if (_optimization_iterator != _events.end() && (*_optimization_iterator)->note() == note) {
746                 return *_optimization_iterator;
747         } 
748
749         for (_optimization_iterator = _events.begin(); _optimization_iterator != _events.end(); ++_optimization_iterator) {
750                 if ((*_optimization_iterator)->note() == note) {
751                         return *_optimization_iterator;
752                 }
753         }
754
755         return 0;
756 }
757
758 void
759 MidiRegionView::redisplay_model()
760 {
761         // Don't redisplay the model if we're currently recording and displaying that
762         if (_active_notes) {
763                 return;
764         }
765
766         if (!_model) {
767                 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
768                 return;
769         }
770
771         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
772                 (*i)->invalidate ();
773         }
774         
775         _model->read_lock();
776         
777         MidiModel::Notes& notes (_model->notes());
778         _optimization_iterator = _events.begin();
779         
780         for (MidiModel::Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
781
782                 boost::shared_ptr<NoteType> note (*n);
783                 CanvasNoteEvent* cne;
784                 bool visible;
785
786                 if (note_in_region_range (note, visible)) {
787                         
788                         if ((cne = find_canvas_note (note)) != 0) {
789                                 
790                                 cne->validate ();
791                                 
792                                 CanvasNote* cn;
793                                 CanvasHit* ch;
794                                 
795                                 if ((cn = dynamic_cast<CanvasNote*>(cne)) != 0) {
796                                         update_note (cn);
797                                 } else if ((ch = dynamic_cast<CanvasHit*>(cne)) != 0) {
798                                         update_hit (ch);
799                                 }
800
801                                 if (visible) {
802                                         cne->show ();
803                                 } else {
804                                         cne->hide ();
805                                 }
806                                 
807                         } else {
808                                 
809                                 add_note (note, visible);
810                         }
811                         
812                 } else {
813                         
814                         if ((cne = find_canvas_note (note)) != 0) {
815                                 cne->validate ();
816                                 cne->hide ();
817                         }
818                 }
819         }
820         
821         /* remove note items that are no longer valid */
822         
823         for (Events::iterator i = _events.begin(); i != _events.end(); ) {
824                 if (!(*i)->valid ()) {
825                         delete *i;
826                         i = _events.erase (i);
827                 } else {
828                         ++i;
829                 }
830         }
831         
832         display_sysexes();
833         display_program_changes();
834         
835         _model->read_unlock();
836         
837         _marked_for_selection.clear ();
838         _marked_for_velocity.clear ();
839
840         /* we may have caused _events to contain things out of order (e.g. if a note
841            moved earlier or later). we don't generally need them in time order, but
842            make a note that a sort is required for those cases that require it.
843         */
844
845         _sort_needed = true;
846 }
847
848 void
849 MidiRegionView::display_program_changes()
850 {
851         boost::shared_ptr<Evoral::Control> control = _model->control(MidiPgmChangeAutomation);
852         if (!control) {
853                 return;
854         }
855
856         Glib::Mutex::Lock lock (control->list()->lock());
857
858         uint8_t channel = control->parameter().channel();
859
860         for (AutomationList::const_iterator event = control->list()->begin();
861                         event != control->list()->end(); ++event) {
862                 double event_time     = (*event)->when;
863                 double program_number = floor((*event)->value + 0.5);
864
865                 // Get current value of bank select MSB at time of the program change
866                 Evoral::Parameter bank_select_msb(MidiCCAutomation, channel, MIDI_CTL_MSB_BANK);
867                 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
868                 uint8_t msb = 0;
869                 if (msb_control != 0) {
870                         msb = uint8_t(floor(msb_control->get_float(true, event_time) + 0.5));
871                 }
872
873                 // Get current value of bank select LSB at time of the program change
874                 Evoral::Parameter bank_select_lsb(MidiCCAutomation, channel, MIDI_CTL_LSB_BANK);
875                 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
876                 uint8_t lsb = 0;
877                 if (lsb_control != 0) {
878                         lsb = uint8_t(floor(lsb_control->get_float(true, event_time) + 0.5));
879                 }
880
881                 MIDI::Name::PatchPrimaryKey patch_key(msb, lsb, program_number);
882
883                 boost::shared_ptr<MIDI::Name::Patch> patch = 
884                         MIDI::Name::MidiPatchManager::instance().find_patch(
885                                         _model_name, _custom_device_mode, channel, patch_key);
886
887                 PCEvent program_change(event_time, uint8_t(program_number), channel);
888
889                 if (patch != 0) {
890                         add_pgm_change(program_change, patch->name());
891                 } else {
892                         char buf[4];
893                         snprintf(buf, 4, "%d", int(program_number));
894                         add_pgm_change(program_change, buf);
895                 }
896         }
897 }
898
899 void 
900 MidiRegionView::display_sysexes()
901 {
902         for (MidiModel::SysExes::const_iterator i = _model->sysexes().begin(); i != _model->sysexes().end(); ++i) {
903                 Evoral::MusicalTime time = (*i)->time();
904                 assert(time >= 0);
905                 
906                 ostringstream str;
907                 str << hex;
908                 for (uint32_t b = 0; b < (*i)->size(); ++b) {
909                         str << int((*i)->buffer()[b]);
910                         if (b != (*i)->size() -1) {
911                                 str << " ";
912                         }
913                 }
914                 string text = str.str();
915                 
916                 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
917
918                 const double x = trackview.editor().frame_to_pixel(beats_to_frames(time));
919                 
920                 double height = midi_stream_view()->contents_height();
921                 
922                 boost::shared_ptr<CanvasSysEx> sysex = boost::shared_ptr<CanvasSysEx>(
923                                 new CanvasSysEx(*this, *group, text, height, x, 1.0));
924                 
925                 // Show unless program change is beyond the region bounds
926                 if (time - _region->start() >= _region->length() || time < _region->start()) {
927                         sysex->hide();
928                 } else {
929                         sysex->show();
930                 }
931                 
932                 _sys_exes.push_back(sysex);
933         }
934 }
935
936
937 MidiRegionView::~MidiRegionView ()
938 {
939         in_destructor = true;
940
941         RegionViewGoingAway (this); /* EMIT_SIGNAL */
942
943         if (_active_notes) {
944                 end_write();
945         }
946
947         _selection.clear();
948         clear_events();
949         delete _note_group;
950         delete _delta_command;
951 }
952
953 void
954 MidiRegionView::region_resized (Change what_changed)
955 {
956         RegionView::region_resized(what_changed);
957         
958         if (what_changed & ARDOUR::PositionChanged) {
959                 set_duration(_region->length(), 0);
960                 if (_enable_display) {
961                         redisplay_model();
962                 }
963         } 
964 }
965
966 void
967 MidiRegionView::reset_width_dependent_items (double pixel_width)
968 {
969         RegionView::reset_width_dependent_items(pixel_width);
970         assert(_pixel_width == pixel_width);
971
972         if (_enable_display) {
973                 redisplay_model();
974         }
975 }
976
977 void
978 MidiRegionView::set_height (double height)
979 {
980         static const double FUDGE = 2.0;
981         const double old_height = _height;
982         RegionView::set_height(height);
983         _height = height - FUDGE;
984         
985         apply_note_range(midi_stream_view()->lowest_note(),
986                          midi_stream_view()->highest_note(),
987                          height != old_height + FUDGE);
988         
989         if (name_pixbuf) {
990                 name_pixbuf->raise_to_top();
991         }
992 }
993
994
995 /** Apply the current note range from the stream view
996  * by repositioning/hiding notes as necessary
997  */
998 void
999 MidiRegionView::apply_note_range (uint8_t min, uint8_t max, bool force)
1000 {
1001         if (!_enable_display) {
1002                 return;
1003         }
1004
1005         if (!force && _current_range_min == min && _current_range_max == max) {
1006                 return;
1007         }
1008
1009         _current_range_min = min;
1010         _current_range_max = max;
1011
1012         for (Events::const_iterator i = _events.begin(); i != _events.end(); ++i) {
1013                 CanvasNoteEvent* event = *i;
1014                 boost::shared_ptr<NoteType> note (event->note());
1015
1016                 if (note->note() < _current_range_min || 
1017                     note->note() > _current_range_max) {
1018                         event->hide();
1019                 } else {
1020                         event->show();
1021                 }
1022                 
1023                 if (CanvasNote* cnote = dynamic_cast<CanvasNote*>(event)) {
1024
1025                         const double y1 = midi_stream_view()->note_to_y(note->note());
1026                         const double y2 = y1 + floor(midi_stream_view()->note_height());
1027                         
1028                         cnote->property_y1() = y1;
1029                         cnote->property_y2() = y2;
1030
1031                 } else if (CanvasHit* chit = dynamic_cast<CanvasHit*>(event)) {
1032
1033                         double x = trackview.editor().frame_to_pixel(
1034                                 beats_to_frames(note->time()) - _region->start());
1035                         const double diamond_size = midi_stream_view()->note_height() / 2.0;
1036                         double y = midi_stream_view()->note_to_y(event->note()->note()) 
1037                                 + ((diamond_size-2.0) / 4.0);
1038                         
1039                         chit->set_height (diamond_size);
1040                         chit->move (x - chit->x1(), y - chit->y1());
1041                         chit->show ();
1042                 }
1043         }
1044 }
1045
1046 GhostRegion*
1047 MidiRegionView::add_ghost (TimeAxisView& tv)
1048 {
1049         CanvasNote* note;
1050
1051         double unit_position = _region->position () / samples_per_unit;
1052         MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
1053         MidiGhostRegion* ghost;
1054
1055         if (mtv && mtv->midi_view()) {
1056                 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group
1057                    to allow having midi notes on top of note lines and waveforms.
1058                  */
1059                 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
1060         } else {
1061                 ghost = new MidiGhostRegion (tv, trackview, unit_position);
1062         }
1063
1064         ghost->set_height ();
1065         ghost->set_duration (_region->length() / samples_per_unit);
1066         ghosts.push_back (ghost);
1067
1068         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1069                 if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
1070                         ghost->add_note(note);
1071                 }
1072         }
1073
1074         ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
1075
1076         return ghost;
1077 }
1078
1079
1080 /** Begin tracking note state for successive calls to add_event
1081  */
1082 void
1083 MidiRegionView::begin_write()
1084 {
1085         assert(!_active_notes);
1086         _active_notes = new CanvasNote*[128];
1087         for (unsigned i=0; i < 128; ++i) {
1088                 _active_notes[i] = 0;
1089         }
1090 }
1091
1092
1093 /** Destroy note state for add_event
1094  */
1095 void
1096 MidiRegionView::end_write()
1097 {
1098         delete[] _active_notes;
1099         _active_notes = 0;
1100         _marked_for_selection.clear();
1101         _marked_for_velocity.clear();
1102 }
1103
1104
1105 /** Resolve an active MIDI note (while recording).
1106  */
1107 void
1108 MidiRegionView::resolve_note(uint8_t note, double end_time)
1109 {
1110         if (midi_view()->note_mode() != Sustained) {
1111                 return;
1112         }
1113
1114         if (_active_notes && _active_notes[note]) {
1115                 const nframes64_t end_time_frames = beats_to_frames(end_time);
1116                 _active_notes[note]->property_x2() = trackview.editor().frame_to_pixel(end_time_frames);
1117                 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
1118                 _active_notes[note] = 0;
1119         }
1120 }
1121
1122
1123 /** Extend active notes to rightmost edge of region (if length is changed)
1124  */
1125 void
1126 MidiRegionView::extend_active_notes()
1127 {
1128         if (!_active_notes) {
1129                 return;
1130         }
1131
1132         for (unsigned i=0; i < 128; ++i) {
1133                 if (_active_notes[i]) {
1134                         _active_notes[i]->property_x2() = trackview.editor().frame_to_pixel(_region->length());
1135                 }
1136         }
1137 }
1138
1139 void 
1140 MidiRegionView::play_midi_note(boost::shared_ptr<NoteType> note)
1141 {
1142         if (!trackview.editor().sound_notes()) {
1143                 return;
1144         }
1145
1146         RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
1147         assert(route_ui);
1148         
1149         route_ui->midi_track()->write_immediate_event(
1150                         note->on_event().size(), note->on_event().buffer());
1151         
1152         const double note_length_beats = (note->off_event().time() - note->on_event().time());
1153         nframes_t note_length_ms = beats_to_frames(note_length_beats)
1154                         * (1000 / (double)route_ui->session().nominal_frame_rate());
1155         Glib::signal_timeout().connect(bind(mem_fun(this, &MidiRegionView::play_midi_note_off), note),
1156                         note_length_ms, G_PRIORITY_DEFAULT);
1157 }
1158
1159 bool
1160 MidiRegionView::play_midi_note_off(boost::shared_ptr<NoteType> note)
1161 {
1162         RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
1163         assert(route_ui);
1164         
1165         route_ui->midi_track()->write_immediate_event(
1166                         note->off_event().size(), note->off_event().buffer());
1167
1168         return false;
1169 }
1170
1171 bool
1172 MidiRegionView::note_in_region_range(const boost::shared_ptr<NoteType> note, bool& visible) const
1173 {
1174         const nframes64_t note_start_frames = beats_to_frames(note->time());
1175
1176         bool outside = (note_start_frames - _region->start() >= _region->length()) || 
1177                 (note_start_frames < _region->start());
1178
1179         visible = (note->note() >= midi_stream_view()->lowest_note()) &&
1180                 (note->note() <= midi_stream_view()->highest_note());
1181
1182         return !outside;
1183 }
1184
1185 void
1186 MidiRegionView::update_note (CanvasNote* ev)
1187 {
1188         boost::shared_ptr<NoteType> note = ev->note();
1189
1190         const nframes64_t note_start_frames = beats_to_frames(note->time());
1191         const nframes64_t note_end_frames   = beats_to_frames(note->end_time());
1192
1193         const double x = trackview.editor().frame_to_pixel(note_start_frames - _region->start());
1194
1195         
1196         const double y1 = midi_stream_view()->note_to_y(note->note());
1197         const double note_endpixel = 
1198                 trackview.editor().frame_to_pixel(note_end_frames - _region->start());
1199         
1200         ev->property_x1() = x;
1201         ev->property_y1() = y1;
1202         if (note->length() > 0) {
1203                 ev->property_x2() = note_endpixel;
1204         } else {
1205                 ev->property_x2() = trackview.editor().frame_to_pixel(_region->length());
1206         }
1207         ev->property_y2() = y1 + floor(midi_stream_view()->note_height());
1208         
1209         if (note->length() == 0) {
1210                 if (_active_notes) {
1211                         assert(note->note() < 128);
1212                         // If this note is already active there's a stuck note,
1213                         // finish the old note rectangle
1214                         if (_active_notes[note->note()]) {
1215                                 CanvasNote* const old_rect = _active_notes[note->note()];
1216                                 boost::shared_ptr<NoteType> old_note = old_rect->note();
1217                                 old_rect->property_x2() = x;
1218                                 old_rect->property_outline_what() = (guint32) 0xF;
1219                         }
1220                         _active_notes[note->note()] = ev;
1221                 }
1222                 /* outline all but right edge */
1223                 ev->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
1224         } else {
1225                 /* outline all edges */
1226                 ev->property_outline_what() = (guint32) 0xF;
1227         }
1228 }
1229
1230 void
1231 MidiRegionView::update_hit (CanvasHit* ev)
1232 {
1233         boost::shared_ptr<NoteType> note = ev->note();
1234
1235         const nframes64_t note_start_frames = beats_to_frames(note->time());
1236         const double x = trackview.editor().frame_to_pixel(note_start_frames - _region->start());
1237         const double diamond_size = midi_stream_view()->note_height() / 2.0;
1238         const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
1239
1240         ev->move(x, y);
1241 }
1242
1243 /** Add a MIDI note to the view (with length).
1244  *
1245  * If in sustained mode, notes with length 0 will be considered active
1246  * notes, and resolve_note should be called when the corresponding note off
1247  * event arrives, to properly display the note.
1248  */
1249 void
1250 MidiRegionView::add_note(const boost::shared_ptr<NoteType> note, bool visible)
1251 {
1252         CanvasNoteEvent* event = 0;
1253         
1254         assert(note->time() >= 0);
1255         assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
1256
1257         ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
1258
1259         if (midi_view()->note_mode() == Sustained) {
1260                 
1261                 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
1262
1263                 update_note (ev_rect);
1264
1265                 event = ev_rect;
1266
1267                 MidiGhostRegion* gr;
1268
1269                 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
1270                         if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
1271                                 gr->add_note(ev_rect);
1272                         }
1273                 }
1274
1275         } else if (midi_view()->note_mode() == Percussive) {
1276
1277                 const double diamond_size = midi_stream_view()->note_height() / 2.0;
1278
1279                 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size, note);
1280
1281                 update_hit (ev_diamond);
1282
1283                 event = ev_diamond;
1284
1285         } else {
1286                 event = 0;
1287         }
1288
1289         if (event) {
1290                 if (_marked_for_selection.find(note) != _marked_for_selection.end()) {
1291                         note_selected(event, true);
1292                 } 
1293
1294                 if (_marked_for_velocity.find(note) != _marked_for_velocity.end()) {
1295                         event->show_velocity();
1296                 }
1297                 event->on_channel_selection_change(_last_channel_selection);
1298                 _events.push_back(event);
1299
1300                 if (visible) {
1301                         event->show();
1302                 } else {
1303                         event->hide ();
1304                 }
1305         }
1306 }
1307
1308 void
1309 MidiRegionView::add_note (uint8_t channel, uint8_t number, uint8_t velocity, 
1310                           Evoral::MusicalTime pos, Evoral::MusicalTime len)
1311 {
1312         boost::shared_ptr<NoteType> new_note (new NoteType (channel, pos, len, number, velocity));
1313         
1314         start_delta_command (_("step add"));
1315         delta_add_note (new_note, true, false);
1316         apply_delta();
1317
1318         /* potentially extend region to hold new note */
1319
1320         nframes64_t end_frame = _region->position() + beats_to_frames (new_note->end_time());
1321         nframes64_t region_end = _region->position() + _region->length() - 1;
1322
1323         if (end_frame > region_end) {
1324                 _region->set_length (end_frame, this);
1325         } else {
1326                 redisplay_model ();
1327         }
1328 }
1329
1330 void
1331 MidiRegionView::add_pgm_change(PCEvent& program, const string& displaytext)
1332 {
1333         assert(program.time >= 0);
1334         
1335         ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
1336         const double x = trackview.editor().frame_to_pixel(beats_to_frames(program.time));
1337         
1338         double height = midi_stream_view()->contents_height();
1339         
1340         boost::shared_ptr<CanvasProgramChange> pgm_change = boost::shared_ptr<CanvasProgramChange>(
1341                         new CanvasProgramChange(*this, *group,
1342                                         displaytext, 
1343                                         height, 
1344                                         x, 1.0, 
1345                                         _model_name, 
1346                                         _custom_device_mode, 
1347                                         program.time, program.channel, program.value));
1348         
1349         // Show unless program change is beyond the region bounds
1350         if (program.time - _region->start() >= _region->length() || program.time < _region->start()) {
1351                 pgm_change->hide();
1352         } else {
1353                 pgm_change->show();
1354         }
1355         
1356         _pgm_changes.push_back(pgm_change);
1357 }
1358
1359 void
1360 MidiRegionView::get_patch_key_at(double time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key)
1361 {
1362         cerr << "getting patch key at " << time << " for channel " << channel << endl;
1363         Evoral::Parameter bank_select_msb(MidiCCAutomation, channel, MIDI_CTL_MSB_BANK);
1364         boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
1365         float msb = -1.0;
1366         if (msb_control != 0) {
1367                 msb = int(msb_control->get_float(true, time));
1368                 cerr << "got msb " << msb;
1369         }
1370
1371         Evoral::Parameter bank_select_lsb(MidiCCAutomation, channel, MIDI_CTL_LSB_BANK);
1372         boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
1373         float lsb = -1.0;
1374         if (lsb_control != 0) {
1375                 lsb = lsb_control->get_float(true, time);
1376                 cerr << " got lsb " << lsb;
1377         }
1378         
1379         Evoral::Parameter program_change(MidiPgmChangeAutomation, channel, 0);
1380         boost::shared_ptr<Evoral::Control> program_control = _model->control(program_change);
1381         float program_number = -1.0;
1382         if (program_control != 0) {
1383                 program_number = program_control->get_float(true, time);
1384                 cerr << " got program " << program_number << endl;
1385         }
1386         
1387         key.msb = (int) floor(msb + 0.5);
1388         key.lsb = (int) floor(lsb + 0.5);
1389         key.program_number = (int) floor(program_number + 0.5);
1390         assert(key.is_sane());
1391 }
1392
1393
1394 void 
1395 MidiRegionView::alter_program_change(PCEvent& old_program, const MIDI::Name::PatchPrimaryKey& new_patch)
1396 {
1397         // TODO: Get the real event here and alter them at the original times
1398         Evoral::Parameter bank_select_msb(MidiCCAutomation, old_program.channel, MIDI_CTL_MSB_BANK);
1399         boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
1400         if (msb_control != 0) {
1401                 msb_control->set_float(float(new_patch.msb), true, old_program.time);
1402         }
1403
1404         // TODO: Get the real event here and alter them at the original times
1405         Evoral::Parameter bank_select_lsb(MidiCCAutomation, old_program.channel, MIDI_CTL_LSB_BANK);
1406         boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
1407         if (lsb_control != 0) {
1408                 lsb_control->set_float(float(new_patch.lsb), true, old_program.time);
1409         }
1410         
1411         Evoral::Parameter program_change(MidiPgmChangeAutomation, old_program.channel, 0);
1412         boost::shared_ptr<Evoral::Control> program_control = _model->control(program_change);
1413         
1414         assert(program_control != 0);
1415         program_control->set_float(float(new_patch.program_number), true, old_program.time);
1416         
1417         redisplay_model();
1418 }
1419
1420 void
1421 MidiRegionView::program_selected(CanvasProgramChange& program, const MIDI::Name::PatchPrimaryKey& new_patch)
1422 {
1423         PCEvent program_change_event(program.event_time(), program.program(), program.channel());
1424         alter_program_change(program_change_event, new_patch);
1425 }
1426
1427 void 
1428 MidiRegionView::previous_program(CanvasProgramChange& program)
1429 {
1430         MIDI::Name::PatchPrimaryKey key;
1431         get_patch_key_at(program.event_time(), program.channel(), key);
1432         
1433         boost::shared_ptr<MIDI::Name::Patch> patch = 
1434                 MIDI::Name::MidiPatchManager::instance().previous_patch(
1435                                 _model_name,
1436                                 _custom_device_mode, 
1437                                 program.channel(), 
1438                                 key);
1439         
1440         PCEvent program_change_event(program.event_time(), program.program(), program.channel());
1441         if (patch) {
1442                 alter_program_change(program_change_event, patch->patch_primary_key());
1443         }
1444 }
1445
1446 void 
1447 MidiRegionView::next_program(CanvasProgramChange& program)
1448 {
1449         MIDI::Name::PatchPrimaryKey key;
1450         get_patch_key_at(program.event_time(), program.channel(), key);
1451         
1452         boost::shared_ptr<MIDI::Name::Patch> patch = 
1453                 MIDI::Name::MidiPatchManager::instance().next_patch(
1454                                 _model_name,
1455                                 _custom_device_mode, 
1456                                 program.channel(), 
1457                                 key);   
1458
1459         PCEvent program_change_event(program.event_time(), program.program(), program.channel());
1460         if (patch) {
1461                 alter_program_change(program_change_event, patch->patch_primary_key());
1462         }
1463 }
1464
1465 void
1466 MidiRegionView::delete_selection()
1467 {
1468         if (_selection.empty()) {
1469                 return;
1470         }
1471
1472         start_delta_command (_("delete selection"));
1473
1474         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1475                 if ((*i)->selected()) {
1476                         _delta_command->remove((*i)->note());
1477                 }
1478         }
1479
1480         _selection.clear();
1481
1482         apply_delta ();
1483 }
1484
1485 void
1486 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev)
1487 {
1488         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1489                 if ((*i)->selected() && (*i) != ev) {
1490                         (*i)->selected(false);
1491                         (*i)->hide_velocity();
1492                 }
1493         }
1494
1495         _selection.clear();
1496 }
1497
1498 void
1499 MidiRegionView::unique_select(ArdourCanvas::CanvasNoteEvent* ev)
1500 {
1501         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
1502                 if ((*i) != ev) {
1503
1504                         Selection::iterator tmp = i;
1505                         ++tmp;
1506
1507                         (*i)->selected (false);
1508                         _selection.erase (i);
1509
1510                         i = tmp;
1511
1512                 } else {
1513                         ++i;
1514                 }
1515         }
1516
1517         /* don't bother with removing this regionview from the editor selection,
1518            since we're about to add another note, and thus put/keep this
1519            regionview in the editor selection.
1520         */
1521
1522         if (!ev->selected()) {
1523                 add_to_selection (ev);
1524         }
1525 }
1526
1527 void
1528 MidiRegionView::note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add, bool extend)
1529 {
1530         if (!add) {
1531                 clear_selection_except(ev);
1532         }
1533
1534         if (!extend) {
1535
1536                 if (!ev->selected()) {
1537                         add_to_selection (ev);
1538                 }
1539
1540         } else {
1541                 /* find end of latest note selected, select all between that and the start of "ev" */
1542
1543                 Evoral::MusicalTime earliest = DBL_MAX;
1544                 Evoral::MusicalTime latest = 0;
1545
1546                 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1547                         if ((*i)->note()->end_time() > latest) {
1548                                 latest = (*i)->note()->end_time();
1549                         } 
1550                         if ((*i)->note()->time() < earliest) {
1551                                 earliest = (*i)->note()->time();
1552                         }
1553                 }
1554
1555                 if (ev->note()->end_time() > latest) {
1556                         latest = ev->note()->end_time();
1557                 }
1558
1559                 if (ev->note()->time() < earliest) {
1560                         earliest = ev->note()->time();
1561                 }
1562                 
1563                 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {           
1564                         
1565                         /* find notes entirely within OR spanning the earliest..latest range */
1566                         
1567                         if (((*i)->note()->time() >= earliest && (*i)->note()->end_time() <= latest) ||
1568                             ((*i)->note()->time() <= earliest && (*i)->note()->end_time() >= latest)) {
1569                                 add_to_selection (*i);
1570                         }                       
1571
1572 #if 0
1573                         /* if events were guaranteed to be time sorted, we could do this.
1574                            but as of sept 10th 2009, they no longer are.
1575                         */
1576                         
1577                         if ((*i)->note()->time() > latest) {
1578                                 break;
1579                         }
1580 #endif
1581                 }
1582         }
1583 }
1584
1585 void
1586 MidiRegionView::note_deselected(ArdourCanvas::CanvasNoteEvent* ev)
1587 {
1588         remove_from_selection (ev);
1589 }
1590
1591 void
1592 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
1593 {
1594         if (x1 > x2) {
1595                 swap (x1, x2);
1596         }
1597
1598         if (y1 > y2) {
1599                 swap (y1, y2);
1600         }
1601
1602         // TODO: Make this faster by storing the last updated selection rect, and only
1603         // adjusting things that are in the area that appears/disappeared.
1604         // We probably need a tree to be able to find events in O(log(n)) time.
1605
1606         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1607
1608                 /* check if any corner of the note is inside the rect
1609                    
1610                    Notes:
1611                      1) this is computing "touched by", not "contained by" the rect.
1612                      2) this does not require that events be sorted in time.
1613                  */
1614
1615                 const double ix1 = (*i)->x1();
1616                 const double ix2 = (*i)->x2();
1617                 const double iy1 = (*i)->y1();
1618                 const double iy2 = (*i)->y2();
1619
1620                 if ((ix1 >= x1 && ix1 <= x2 && iy1 >= y1 && iy1 <= y2) ||
1621                     (ix1 >= x1 && ix1 <= x2 && iy2 >= y1 && iy2 <= y2) ||
1622                     (ix2 >= x1 && ix2 <= x2 && iy1 >= y1 && iy1 <= y2) ||
1623                     (ix2 >= x1 && ix2 <= x2 && iy2 >= y1 && iy2 <= y2)) {
1624
1625                         // Inside rectangle
1626                         if (!(*i)->selected()) {
1627                                 add_to_selection (*i);
1628                         }
1629                 } else if ((*i)->selected()) {
1630                         // Not inside rectangle
1631                         remove_from_selection (*i);
1632                 }
1633         }
1634 }
1635
1636 void
1637 MidiRegionView::remove_from_selection (CanvasNoteEvent* ev)
1638 {
1639         Selection::iterator i = _selection.find (ev);
1640
1641         if (i != _selection.end()) {
1642                 _selection.erase (i);
1643         }
1644
1645         ev->selected (false);
1646         ev->hide_velocity ();
1647         
1648         if (_selection.empty()) {
1649                 PublicEditor& editor (trackview.editor());
1650                 editor.get_selection().remove (this);
1651         }
1652 }
1653
1654 void
1655 MidiRegionView::add_to_selection (CanvasNoteEvent* ev)
1656 {
1657         bool add_mrv_selection = false;
1658
1659         if (_selection.empty()) {
1660                 add_mrv_selection = true;
1661         }
1662
1663         if (_selection.insert (ev).second) {
1664                 ev->selected (true);
1665                 play_midi_note ((ev)->note());
1666         }
1667
1668         if (add_mrv_selection) {
1669                 PublicEditor& editor (trackview.editor());
1670                 editor.get_selection().add (this);
1671         }
1672 }
1673
1674 void
1675 MidiRegionView::move_selection(double dx, double dy)
1676 {
1677         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1678                 (*i)->move_event(dx, dy);
1679         }
1680 }
1681
1682 void
1683 MidiRegionView::note_dropped(CanvasNoteEvent* ev, double dt, uint8_t dnote)
1684 {
1685         // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
1686         if (_selection.find(ev) == _selection.end()) {
1687                 return;
1688         }
1689
1690         uint8_t lowest_note_in_selection  = midi_stream_view()->lowest_note();
1691         uint8_t highest_note_in_selection = midi_stream_view()->highest_note();
1692         uint8_t highest_note_difference = 0;
1693
1694         // find highest and lowest notes first
1695         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1696                 uint8_t pitch = (*i)->note()->note();
1697                 lowest_note_in_selection  = std::min(lowest_note_in_selection,  pitch);
1698                 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
1699         }
1700         
1701         /*
1702         cerr << "dnote: " << (int) dnote << endl;
1703         cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note()) 
1704              << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
1705         cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): " 
1706              << int(highest_note_in_selection) << endl;
1707         cerr << "selection size: " << _selection.size() << endl;
1708         cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
1709         */
1710         
1711         // Make sure the note pitch does not exceed the MIDI standard range
1712         if (dnote <= 127 && (highest_note_in_selection + dnote > 127)) {
1713                 highest_note_difference = highest_note_in_selection - 127;
1714         }
1715         
1716         start_diff_command(_("move notes"));
1717
1718         for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ++i) {
1719
1720                 nframes64_t start_frames = beats_to_frames((*i)->note()->time());
1721
1722                 if (dt >= 0) {
1723                         start_frames += snap_frame_to_frame(trackview.editor().pixel_to_frame(dt));
1724                 } else {
1725                         start_frames -= snap_frame_to_frame(trackview.editor().pixel_to_frame(-dt));
1726                 }
1727
1728                 Evoral::MusicalTime new_time = frames_to_beats(start_frames);
1729
1730                 if (new_time < 0) {
1731                         continue;
1732                 }
1733
1734                 diff_add_change (*i, MidiModel::DiffCommand::StartTime, new_time);
1735
1736                 uint8_t original_pitch = (*i)->note()->note();
1737                 uint8_t new_pitch      = original_pitch + dnote - highest_note_difference;
1738                 
1739                 // keep notes in standard midi range
1740                 clamp_to_0_127(new_pitch);
1741                 
1742                 // keep original pitch if note is dragged outside valid midi range
1743                 if ((original_pitch != 0 && new_pitch == 0)
1744                                 || (original_pitch != 127 && new_pitch == 127)) {
1745                         new_pitch = original_pitch;
1746                 }
1747
1748                 lowest_note_in_selection  = std::min(lowest_note_in_selection,  new_pitch);
1749                 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
1750
1751                 diff_add_change (*i, MidiModel::DiffCommand::NoteNumber, new_pitch);
1752         }
1753
1754         apply_diff();
1755         
1756         // care about notes being moved beyond the upper/lower bounds on the canvas
1757         if (lowest_note_in_selection  < midi_stream_view()->lowest_note() ||
1758                         highest_note_in_selection > midi_stream_view()->highest_note()) {
1759                 midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
1760         }
1761 }
1762
1763 nframes64_t
1764 MidiRegionView::snap_pixel_to_frame(double x)
1765 {
1766         PublicEditor& editor = trackview.editor();
1767         // x is region relative, convert it to global absolute frames
1768         nframes64_t frame = editor.pixel_to_frame(x) + _region->position();
1769         editor.snap_to(frame);
1770         return frame - _region->position(); // convert back to region relative
1771 }
1772
1773 nframes64_t
1774 MidiRegionView::snap_frame_to_frame(nframes64_t x)
1775 {
1776         PublicEditor& editor = trackview.editor();
1777         // x is region relative, convert it to global absolute frames
1778         nframes64_t frame = x + _region->position();
1779         editor.snap_to(frame);
1780         return frame - _region->position(); // convert back to region relative
1781 }
1782
1783 double
1784 MidiRegionView::snap_to_pixel(double x)
1785 {
1786         return (double) trackview.editor().frame_to_pixel(snap_pixel_to_frame(x));
1787 }
1788
1789 double
1790 MidiRegionView::get_position_pixels()
1791 {
1792         nframes64_t region_frame = get_position();
1793         return trackview.editor().frame_to_pixel(region_frame);
1794 }
1795
1796 double
1797 MidiRegionView::get_end_position_pixels()
1798 {
1799         nframes64_t frame = get_position() + get_duration ();
1800         return trackview.editor().frame_to_pixel(frame);
1801 }
1802
1803 nframes64_t
1804 MidiRegionView::beats_to_frames(double beats) const
1805 {
1806         return _time_converter.to(beats);
1807 }
1808
1809 double
1810 MidiRegionView::frames_to_beats(nframes64_t frames) const
1811 {
1812         return _time_converter.from(frames);
1813 }
1814
1815 void
1816 MidiRegionView::begin_resizing (bool /*at_front*/)
1817 {
1818         _resize_data.clear();
1819
1820         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1821                 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
1822
1823                 // only insert CanvasNotes into the map
1824                 if (note) {
1825                         NoteResizeData *resize_data = new NoteResizeData();
1826                         resize_data->canvas_note = note;
1827
1828                         // create a new SimpleRect from the note which will be the resize preview
1829                         SimpleRect *resize_rect = new SimpleRect(
1830                                         *group, note->x1(), note->y1(), note->x2(), note->y2());
1831
1832                         // calculate the colors: get the color settings
1833                         uint32_t fill_color = UINT_RGBA_CHANGE_A(
1834                                         ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(),
1835                                         128);
1836
1837                         // make the resize preview notes more transparent and bright
1838                         fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
1839
1840                         // calculate color based on note velocity
1841                         resize_rect->property_fill_color_rgba() = UINT_INTERPOLATE(
1842                                         CanvasNoteEvent::meter_style_fill_color(note->note()->velocity()),
1843                                         fill_color,
1844                                         0.85);
1845
1846                         resize_rect->property_outline_color_rgba() = CanvasNoteEvent::calculate_outline(
1847                                         ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get());
1848
1849                         resize_data->resize_rect = resize_rect;
1850                         _resize_data.push_back(resize_data);
1851                 }
1852         }
1853 }
1854
1855 void
1856 MidiRegionView::update_resizing (bool at_front, double delta_x, bool relative)
1857 {
1858         for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1859                 SimpleRect* resize_rect = (*i)->resize_rect;
1860                 CanvasNote* canvas_note = (*i)->canvas_note;
1861                 double current_x;
1862
1863                 if (at_front) {
1864                         if (relative) {
1865                                 current_x = canvas_note->x1() + delta_x;
1866                         } else {
1867                                 // x is in track relative, transform it to region relative
1868                                 current_x = delta_x - get_position_pixels();
1869                         }
1870                 } else {
1871                         if (relative) {
1872                                 current_x = canvas_note->x2() + delta_x;
1873                         } else {
1874                                 // x is in track relative, transform it to region relative
1875                                 current_x = delta_x - get_end_position_pixels ();
1876                         }
1877                 }
1878                 
1879                 if (at_front) {
1880                         resize_rect->property_x1() = snap_to_pixel(current_x);
1881                         resize_rect->property_x2() = canvas_note->x2();
1882                 } else {
1883                         resize_rect->property_x2() = snap_to_pixel(current_x);
1884                         resize_rect->property_x1() = canvas_note->x1();
1885                 }
1886         }
1887 }
1888
1889 void
1890 MidiRegionView::commit_resizing (bool at_front, double delta_x, bool relative)
1891 {
1892         start_diff_command(_("resize notes"));
1893
1894         for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1895                 CanvasNote*  canvas_note = (*i)->canvas_note;
1896                 SimpleRect*  resize_rect = (*i)->resize_rect;
1897                 const double region_start = get_position_pixels();
1898                 double current_x;
1899
1900                 if (at_front) {
1901                         if (relative) {
1902                                 current_x = canvas_note->x1() + delta_x;
1903                         } else {
1904                                 // x is in track relative, transform it to region relative
1905                                 current_x = region_start + delta_x;
1906                         }
1907                 } else {
1908                         if (relative) {
1909                                 current_x = canvas_note->x2() + delta_x;
1910                         } else {
1911                                 // x is in track relative, transform it to region relative
1912                                 current_x = region_start + delta_x;
1913                         }
1914                 }
1915                 
1916                 current_x = snap_pixel_to_frame (current_x);
1917                 current_x = frames_to_beats (current_x);
1918
1919                 if (at_front && current_x < canvas_note->note()->end_time()) {
1920                         diff_add_change (canvas_note, MidiModel::DiffCommand::StartTime, current_x);
1921                 }
1922
1923                 if (!at_front) {
1924                         double len = current_x - canvas_note->note()->time();
1925
1926                         if (len > 0) {
1927                                 /* XXX convert to beats */
1928                                 diff_add_change (canvas_note, MidiModel::DiffCommand::Length, len);
1929                         }
1930                 }
1931
1932                 delete resize_rect;
1933                 delete (*i);
1934         }
1935
1936         _resize_data.clear();
1937         apply_diff();
1938 }
1939
1940 void
1941 MidiRegionView::change_note_velocity(CanvasNoteEvent* event, int8_t velocity, bool relative)
1942 {
1943         uint8_t new_velocity;
1944
1945         if (relative) {
1946                 new_velocity = event->note()->velocity() + velocity;
1947                 clamp_to_0_127(new_velocity);
1948         } else {
1949                 new_velocity = velocity;
1950         }
1951
1952         diff_add_change (event, MidiModel::DiffCommand::Velocity, new_velocity);
1953 }
1954
1955 void
1956 MidiRegionView::change_note_note (CanvasNoteEvent* event, int8_t note, bool relative)
1957 {
1958         uint8_t new_note;
1959
1960         if (relative) {
1961                 new_note = event->note()->note() + note;
1962         } else {
1963                 new_note = note;
1964         }
1965
1966         clamp_to_0_127 (new_note);
1967         diff_add_change (event, MidiModel::DiffCommand::NoteNumber, new_note);
1968 }
1969
1970 void
1971 MidiRegionView::trim_note (CanvasNoteEvent* event, Evoral::MusicalTime front_delta, Evoral::MusicalTime end_delta)
1972 {
1973         bool change_start = false;
1974         bool change_length = false;
1975         Evoral::MusicalTime new_start;
1976         Evoral::MusicalTime new_length;
1977
1978         /* NOTE: the semantics of the two delta arguments are slightly subtle:
1979
1980            front_delta: if positive - move the start of the note later in time (shortening it)
1981                         if negative - move the start of the note earlier in time (lengthening it)
1982
1983            end_delta:   if positive - move the end of the note later in time (lengthening it)
1984                         if negative - move the end of the note earlier in time (shortening it)
1985          */
1986
1987         if (front_delta) {
1988                 if (front_delta < 0) {
1989
1990                         if (event->note()->time() < -front_delta) {
1991                                 new_start = 0;
1992                         } else {
1993                                 new_start = event->note()->time() + front_delta; // moves earlier
1994                         }
1995
1996                         /* start moved toward zero, so move the end point out to where it used to be.
1997                            Note that front_delta is negative, so this increases the length.
1998                         */
1999
2000                         new_length = event->note()->length() - front_delta;
2001                         change_start = true;
2002                         change_length = true;
2003
2004                 } else {
2005
2006                         Evoral::MusicalTime new_pos = event->note()->time() + front_delta;
2007                         
2008                         if (new_pos < event->note()->end_time()) {
2009                                 new_start = event->note()->time() + front_delta;
2010                                 /* start moved toward the end, so move the end point back to where it used to be */
2011                                 new_length = event->note()->length() - front_delta; 
2012                                 change_start = true;
2013                                 change_length = true;
2014                         }
2015                 }
2016
2017         }
2018
2019         if (end_delta) {
2020                 bool can_change = true;
2021                 if (end_delta < 0) {
2022                         if (event->note()->length() < -end_delta) {
2023                                 can_change = false;
2024                         }
2025                 } 
2026
2027                 if (can_change) {
2028                         new_length = event->note()->length() + end_delta;
2029                         change_length = true;
2030                 }
2031         }
2032
2033         if (change_start) {
2034                 diff_add_change (event, MidiModel::DiffCommand::StartTime, new_start);
2035         }
2036
2037         if (change_length) {
2038                 diff_add_change (event, MidiModel::DiffCommand::Length, new_length);
2039         }
2040 }
2041
2042 void
2043 MidiRegionView::change_note_time (CanvasNoteEvent* event, Evoral::MusicalTime delta, bool relative)
2044 {
2045         Evoral::MusicalTime new_time;
2046
2047         if (relative) {
2048                 if (delta < 0.0) {
2049                         if (event->note()->time() < -delta) {
2050                                 new_time = 0;
2051                         } else {
2052                                 new_time = event->note()->time() + delta;
2053                         } 
2054                 } else {
2055                         new_time = event->note()->time() + delta;
2056                 }
2057         } else {
2058                 new_time = delta;
2059         }
2060
2061         diff_add_change (event, MidiModel::DiffCommand::StartTime, new_time);
2062 }
2063
2064 void
2065 MidiRegionView::change_velocities (bool up, bool fine, bool allow_smush)
2066 {
2067         int8_t delta;
2068
2069         if (_selection.empty()) {
2070                 return;
2071         }
2072
2073         if (fine) {
2074                 delta = 1;
2075         } else {
2076                 delta = 10;
2077         }
2078
2079         if (!up) {
2080                 delta = -delta;
2081         }
2082
2083         if (!allow_smush) {
2084                 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2085                         if ((*i)->note()->velocity() + delta == 0 || (*i)->note()->velocity() + delta == 127) {
2086                                 return;
2087                         }
2088                 }
2089         }
2090
2091         start_diff_command(_("change velocities"));
2092         
2093         for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
2094                 Selection::iterator next = i;
2095                 ++next;
2096                 change_note_velocity (*i, delta, true);
2097                 i = next;
2098         }
2099         
2100         apply_diff();
2101 }
2102
2103
2104 void
2105 MidiRegionView::transpose (bool up, bool fine, bool allow_smush)
2106 {
2107         if (_selection.empty()) {
2108                 return;
2109         }
2110
2111         int8_t delta;
2112         
2113         if (fine) {
2114                 delta = 1;
2115         } else {
2116                 delta = 12;
2117         }
2118
2119         if (!up) {
2120                 delta = -delta;
2121         }
2122
2123         if (!allow_smush) {
2124                 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2125                         if (!up) {
2126                                 if ((int8_t) (*i)->note()->note() + delta <= 0) {
2127                                         return;
2128                                 }
2129                         } else {
2130                                 if ((int8_t) (*i)->note()->note() + delta > 127) {
2131                                         return;
2132                                 }
2133                         }
2134                 }
2135         }
2136
2137         start_diff_command (_("transpose"));
2138
2139         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
2140                 Selection::iterator next = i;
2141                 ++next;
2142                 change_note_note (*i, delta, true);
2143                 i = next;
2144         }
2145
2146         apply_diff ();
2147 }
2148
2149 void
2150 MidiRegionView::change_note_lengths (bool fine, bool shorter, bool start, bool end)
2151 {
2152         Evoral::MusicalTime delta;
2153
2154         if (fine) {
2155                 delta = 1.0/128.0;
2156         } else {
2157                 /* grab the current grid distance */
2158                 bool success;
2159                 delta = trackview.editor().get_grid_type_as_beats (success, _region->position());
2160                 if (!success) {
2161                         /* XXX cannot get grid type as beats ... should always be possible ... FIX ME */
2162                         cerr << "Grid type not available as beats - TO BE FIXED\n";
2163                         return;
2164                 }
2165         }
2166
2167         if (shorter) {
2168                 delta = -delta;
2169         }
2170         
2171         start_diff_command (_("change note lengths"));
2172
2173         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
2174                 Selection::iterator next = i;
2175                 ++next;
2176                 
2177                 /* note the negation of the delta for start */
2178
2179                 trim_note (*i, (start ? -delta : 0), (end ? delta : 0));
2180                 i = next;
2181         }
2182
2183         apply_diff ();
2184
2185 }
2186
2187 void
2188 MidiRegionView::nudge_notes (bool forward)
2189 {
2190         if (_selection.empty()) {
2191                 return;
2192         }
2193
2194         /* pick a note as the point along the timeline to get the nudge distance. 
2195            its not necessarily the earliest note, so we may want to pull the notes out 
2196            into a vector and sort before using the first one.
2197         */
2198
2199         nframes64_t ref_point = _region->position() + beats_to_frames ((*(_selection.begin()))->note()->time());
2200         nframes64_t unused;
2201         nframes64_t distance;
2202
2203         if ((distance = trackview.editor().get_nudge_distance (ref_point, unused)) == 0) {
2204
2205                 /* no nudge distance set - use grid */
2206
2207                 nframes64_t next_pos = ref_point;
2208                 
2209                 if (forward) {
2210                         /* XXX need check on max_frames, but that needs max_frames64 or something */
2211                         next_pos += 1;
2212                 } else { 
2213                         if (next_pos == 0) {
2214                                 return;
2215                         }
2216                         next_pos -= 1;
2217                 }
2218                 
2219                 cerr << "ref point was " << ref_point << " next was " << next_pos;
2220                 trackview.editor().snap_to (next_pos, (forward ? 1 : -1), false);
2221                 distance = ref_point - next_pos;
2222                 cerr << " final is " << next_pos << " distance = " << distance << endl;
2223         } 
2224                 
2225         if (distance == 0) {
2226                 return;
2227         }
2228
2229         Evoral::MusicalTime delta = frames_to_beats (fabs (distance));
2230
2231         if (!forward) {
2232                 delta = -delta;
2233         }
2234
2235         start_diff_command (_("nudge"));
2236
2237         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
2238                 Selection::iterator next = i;
2239                 ++next;
2240                 change_note_time (*i, delta, true);
2241                 i = next;
2242         }
2243
2244         apply_diff ();
2245 }
2246
2247 void
2248 MidiRegionView::change_channel(uint8_t channel)
2249 {
2250         start_diff_command(_("change channel"));
2251         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2252                 diff_add_change (*i, MidiModel::DiffCommand::Channel, channel);
2253         }
2254         apply_diff();
2255 }
2256
2257
2258 void
2259 MidiRegionView::note_entered(ArdourCanvas::CanvasNoteEvent* ev)
2260 {
2261         if (_mouse_state == SelectTouchDragging) {
2262                 note_selected(ev, true);
2263         }
2264
2265         PublicEditor& editor (trackview.editor());
2266         editor.show_verbose_canvas_cursor_with (Evoral::midi_note_name (ev->note()->note()));
2267 }
2268
2269 void
2270 MidiRegionView::note_left (ArdourCanvas::CanvasNoteEvent*)
2271 {
2272         PublicEditor& editor (trackview.editor());
2273         editor.hide_verbose_canvas_cursor ();
2274 }
2275         
2276
2277 void
2278 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
2279 {
2280         boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
2281         if (msrc)
2282                 display_model(msrc->model());
2283 }
2284
2285 void
2286 MidiRegionView::set_frame_color()
2287 {
2288         if (frame) {
2289                 if (_selected && should_show_selection) {
2290                         frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
2291                 } else {
2292                         frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
2293                 }
2294         }
2295 }
2296
2297 void 
2298 MidiRegionView::midi_channel_mode_changed(ChannelMode mode, uint16_t mask)
2299 {
2300         switch (mode) {
2301         case AllChannels:
2302         case FilterChannels:
2303                 _force_channel = -1;
2304                 break;
2305         case ForceChannel:
2306                 _force_channel = mask;
2307                 mask = 0xFFFF; // Show all notes as active (below)
2308         };
2309
2310         // Update notes for selection
2311         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
2312                 (*i)->on_channel_selection_change(mask);
2313         }
2314
2315         _last_channel_selection = mask;
2316 }
2317
2318 void 
2319 MidiRegionView::midi_patch_settings_changed(std::string model, std::string custom_device_mode)
2320 {
2321         _model_name         = model;
2322         _custom_device_mode = custom_device_mode;
2323         redisplay_model();
2324 }
2325
2326 void
2327 MidiRegionView::cut_copy_clear (Editing::CutCopyOp op)
2328 {
2329         if (_selection.empty()) {
2330                 return;
2331         }
2332
2333         PublicEditor& editor (trackview.editor());
2334
2335         switch (op) {
2336         case Cut:
2337         case Copy:
2338                 editor.get_cut_buffer().add (selection_as_cut_buffer());
2339                 break;
2340         default:
2341                 break;
2342         }
2343                 
2344         start_delta_command();
2345
2346         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2347                 switch (op) {
2348                 case Copy:
2349                         break;
2350                 case Cut:
2351                         delta_remove_note (*i);
2352                         break;
2353                 case Clear:
2354                         break;
2355                 }
2356         }
2357
2358         apply_delta();
2359 }
2360
2361 MidiCutBuffer*
2362 MidiRegionView::selection_as_cut_buffer () const
2363 {
2364         NoteList notes;
2365
2366         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2367                 notes.push_back (boost::shared_ptr<NoteType> (new NoteType (*((*i)->note().get()))));
2368         }
2369
2370         /* sort them into time order */
2371
2372         Evoral::Sequence<Evoral::MusicalTime>::LaterNoteComparator cmp;
2373         sort (notes.begin(), notes.end(),  cmp);
2374
2375         MidiCutBuffer* cb = new MidiCutBuffer (trackview.session());
2376         cb->set (notes);
2377         
2378         return cb;
2379 }
2380
2381 void
2382 MidiRegionView::paste (nframes64_t pos, float times, const MidiCutBuffer& mcb)
2383 {
2384         if (mcb.empty()) {
2385                 return;
2386         }
2387
2388         start_delta_command (_("paste"));
2389
2390         Evoral::MusicalTime beat_delta;
2391         Evoral::MusicalTime paste_pos_beats;
2392         Evoral::MusicalTime duration;
2393         Evoral::MusicalTime end_point;
2394
2395         duration = mcb.notes().back()->end_time() - mcb.notes().front()->time();
2396         paste_pos_beats = frames_to_beats (pos - _region->position());
2397         beat_delta = mcb.notes().front()->time() - paste_pos_beats;
2398         paste_pos_beats = 0;
2399
2400         _selection.clear ();
2401
2402         for (int n = 0; n < (int) times; ++n) {
2403
2404                 for (NoteList::const_iterator i = mcb.notes().begin(); i != mcb.notes().end(); ++i) {
2405                         
2406                         boost::shared_ptr<NoteType> copied_note (new NoteType (*((*i).get())));
2407                         copied_note->set_time (paste_pos_beats + copied_note->time() - beat_delta);
2408
2409                         /* make all newly added notes selected */
2410
2411                         delta_add_note (copied_note, true);
2412                         end_point = copied_note->end_time();
2413                 }
2414
2415                 paste_pos_beats += duration;
2416         }
2417
2418         /* if we pasted past the current end of the region, extend the region */
2419
2420         nframes64_t end_frame = _region->position() + beats_to_frames (end_point);
2421         nframes64_t region_end = _region->position() + _region->length() - 1;
2422
2423         if (end_frame > region_end) {
2424
2425                 trackview.session().begin_reversible_command (_("paste"));
2426
2427                 XMLNode& before (_region->get_state());
2428                 _region->set_length (end_frame, this);
2429                 trackview.session().add_command (new MementoCommand<Region>(*_region, &before, &_region->get_state()));
2430         }
2431         
2432         apply_delta ();
2433 }
2434
2435 struct EventNoteTimeEarlyFirstComparator {
2436     bool operator() (CanvasNoteEvent* a, CanvasNoteEvent* b) {
2437             return a->note()->time() < b->note()->time();
2438     }
2439 };
2440
2441 void
2442 MidiRegionView::time_sort_events ()
2443 {
2444         if (!_sort_needed) {
2445                 return;
2446         }
2447
2448         EventNoteTimeEarlyFirstComparator cmp;
2449         _events.sort (cmp);
2450
2451         _sort_needed = false;
2452 }
2453
2454 void
2455 MidiRegionView::goto_next_note ()
2456 {
2457         // nframes64_t pos = -1;
2458         bool use_next = false;
2459
2460         if (_events.back()->selected()) {
2461                 return;
2462         }
2463
2464         time_sort_events ();
2465
2466         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
2467                 if ((*i)->selected()) {
2468                         use_next = true;
2469                         continue;
2470                 } else if (use_next) {
2471                         unique_select (*i);
2472                         // pos = _region->position() + beats_to_frames ((*i)->note()->time());
2473                         return;
2474                 }
2475         }
2476
2477         /* use the first one */
2478
2479         unique_select (_events.front());
2480         
2481 }
2482
2483 void
2484 MidiRegionView::goto_previous_note ()
2485 {
2486         // nframes64_t pos = -1;
2487         bool use_next = false;
2488
2489         if (_events.front()->selected()) {
2490                 return;
2491         }
2492
2493         time_sort_events ();
2494
2495         for (Events::reverse_iterator i = _events.rbegin(); i != _events.rend(); ++i) {
2496                 if ((*i)->selected()) {
2497                         use_next = true;
2498                         continue;
2499                 } else if (use_next) {
2500                         unique_select (*i);
2501                         // pos = _region->position() + beats_to_frames ((*i)->note()->time());
2502                         return;
2503                 }
2504         }
2505
2506         /* use the last one */
2507
2508         unique_select (*(_events.rbegin()));
2509 }
2510
2511 void
2512 MidiRegionView::selection_as_notelist (NoteList& selected) 
2513 {
2514         time_sort_events ();
2515
2516         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
2517                 if ((*i)->selected()) {
2518                         selected.push_back ((*i)->note());
2519                 }
2520         }
2521 }
2522
2523