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