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