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