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