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