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