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