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