Fix install path problems (s/ardour2/ardour3).
[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
24 #include <gtkmm.h>
25
26 #include <gtkmm2ext/gtk_ui.h>
27
28 #include <sigc++/signal.h>
29
30 #include <ardour/playlist.h>
31 #include <ardour/tempo.h>
32 #include <ardour/midi_region.h>
33 #include <ardour/midi_source.h>
34 #include <ardour/midi_diskstream.h>
35 #include <ardour/midi_events.h>
36 #include <ardour/midi_model.h>
37
38 #include "streamview.h"
39 #include "midi_region_view.h"
40 #include "midi_streamview.h"
41 #include "midi_time_axis.h"
42 #include "simplerect.h"
43 #include "simpleline.h"
44 #include "canvas-hit.h"
45 #include "public_editor.h"
46 #include "ghostregion.h"
47 #include "midi_time_axis.h"
48 #include "automation_time_axis.h"
49 #include "automation_region_view.h"
50 #include "utils.h"
51 #include "midi_util.h"
52 #include "gui_thread.h"
53 #include "keyboard.h"
54
55 #include "i18n.h"
56
57 using namespace sigc;
58 using namespace ARDOUR;
59 using namespace PBD;
60 using namespace Editing;
61 using namespace ArdourCanvas;
62
63 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color)
64         : RegionView (parent, tv, r, spu, basic_color)
65         , _default_note_length(0.0)
66         , _active_notes(0)
67         , _note_group(new ArdourCanvas::Group(*parent))
68         , _delta_command(NULL)
69         , _mouse_state(None)
70         , _pressed_button(0)
71 {
72         group->lower_to_bottom();
73         _note_group->raise_to_top();
74
75         frame->property_fill_color_rgba() = 0xff000033;
76 }
77
78 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
79         : RegionView (parent, tv, r, spu, basic_color, visibility)
80         , _default_note_length(0.0)
81         , _active_notes(0)
82         , _note_group(new ArdourCanvas::Group(*parent))
83         , _delta_command(NULL)
84         , _mouse_state(None)
85         , _pressed_button(0)
86 {
87         _note_group->raise_to_top();
88 }
89
90 void
91 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
92 {
93         if (wfd)
94                 midi_region()->midi_source(0)->load_model();
95         
96         const Meter& m = trackview.session().tempo_map().meter_at(_region->position());
97         const Tempo& t = trackview.session().tempo_map().tempo_at(_region->position());
98         _default_note_length = m.frames_per_bar(t, trackview.session().frame_rate())
99                         / m.beats_per_bar();
100
101         _model = midi_region()->midi_source(0)->model();
102         _enable_display = false;
103         
104         RegionView::init(basic_color, false);
105
106         compute_colors (basic_color);
107
108         reset_width_dependent_items ((double) _region->length() / samples_per_unit);
109
110         set_y_position_and_height (0, trackview.height);
111
112         region_muted ();
113         region_resized (BoundsChanged);
114         region_locked ();
115
116         _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
117
118         set_colors ();
119
120         _enable_display = true;
121
122         if (_model) {
123                 if (wfd) {
124                         redisplay_model();
125                 }
126                 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
127         }
128
129         midi_region()->midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegionView::switch_source));
130         
131         group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
132
133 }
134
135 bool
136 MidiRegionView::canvas_event(GdkEvent* ev)
137 {
138         static bool delete_mod = false;
139         static Editing::MidiEditMode original_mode;
140
141         static double drag_start_x, drag_start_y;
142         static double last_x, last_y;
143         double event_x, event_y;
144         nframes_t event_frame = 0;
145
146         static ArdourCanvas::SimpleRect* drag_rect = NULL;
147         
148         if (trackview.editor.current_mouse_mode() != MouseNote)
149                 return false;
150
151         // Mmmm, spaghetti
152         
153         switch (ev->type) {
154         case GDK_KEY_PRESS:
155                 cout << "KEY" << endl;
156                 if (ev->key.keyval == GDK_Delete && !delete_mod) {
157                         delete_mod = true;
158                         original_mode = trackview.editor.current_midi_edit_mode();
159                         trackview.editor.set_midi_edit_mode(MidiEditErase);
160                         start_remove_command();
161                         _mouse_state = EraseTouchDragging;
162                 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
163                         _mouse_state = SelectTouchDragging;
164                 }
165                 return true;
166         
167         case GDK_KEY_RELEASE:
168                 if (ev->key.keyval == GDK_Delete) {
169                         if (_mouse_state == EraseTouchDragging) {
170                                 delete_selection();
171                                 apply_command();
172                         }
173                         if (delete_mod) {
174                                 trackview.editor.set_midi_edit_mode(original_mode);
175                                 delete_mod = false;
176                         }
177                 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
178                         _mouse_state = None;
179                 }
180                 return true;
181
182         case GDK_BUTTON_PRESS:
183                 if (_mouse_state != SelectTouchDragging && _mouse_state != EraseTouchDragging)
184                         _mouse_state = Pressed;
185                 _pressed_button = ev->button.button;
186                 return true;
187         
188         case GDK_ENTER_NOTIFY:
189                 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
190                 Keyboard::magic_widget_grab_focus();
191                 group->grab_focus();
192                 break;
193         
194         case GDK_MOTION_NOTIFY:
195                 event_x = ev->motion.x;
196                 event_y = ev->motion.y;
197                 group->w2i(event_x, event_y);
198                                 
199                 event_frame = trackview.editor.pixel_to_frame(event_x);
200                 trackview.editor.snap_to(event_frame);
201
202                 switch (_mouse_state) {
203                 case Pressed: // Drag start
204
205                         // Select drag start
206                         if (_pressed_button == 1 && trackview.editor.current_midi_edit_mode() == MidiEditSelect) {
207                                 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
208                                                 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
209                                 last_x = event_x;
210                                 last_y = event_y;
211                                 drag_start_x = event_x;
212                                 drag_start_y = event_y;
213
214                                 drag_rect = new ArdourCanvas::SimpleRect(*group);
215                                 drag_rect->property_x1() = event_x;
216                                 drag_rect->property_y1() = event_y;
217                                 drag_rect->property_x2() = event_x;
218                                 drag_rect->property_y2() = event_y;
219                                 drag_rect->property_outline_what() = 0xFF;
220                                 drag_rect->property_outline_color_rgba()
221                                         = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
222                                 drag_rect->property_fill_color_rgba()
223                                         = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
224
225                                 _mouse_state = SelectRectDragging;
226                                 return true;
227
228                         // Add note drag start
229                         } else if (trackview.editor.current_midi_edit_mode() == MidiEditPencil) {
230                                 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
231                                                 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
232                                 last_x = event_x;
233                                 last_y = event_y;
234                                 drag_start_x = event_x;
235                                 drag_start_y = event_y;
236
237                                 drag_rect = new ArdourCanvas::SimpleRect(*group);
238                                 drag_rect->property_x1() = trackview.editor.frame_to_pixel(event_frame);
239                                 
240                                 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
241                                 drag_rect->property_x2() = event_x;
242                                 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
243                                 drag_rect->property_outline_what() = 0xFF;
244                                 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
245
246                                 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
247
248                                 _mouse_state = AddDragging;
249                                 return true;
250                         }
251
252                         return false;
253
254                 case SelectRectDragging: // Select drag motion
255                 case AddDragging: // Add note drag motion
256                         if (ev->motion.is_hint) {
257                                 int t_x;
258                                 int t_y;
259                                 GdkModifierType state;
260                                 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
261                                 event_x = t_x;
262                                 event_y = t_y;
263                         }
264
265                         if (_mouse_state == AddDragging)
266                                 event_x = trackview.editor.frame_to_pixel(event_frame);
267
268                         if (drag_rect)
269                                 if (event_x > drag_start_x)
270                                         drag_rect->property_x2() = event_x;
271                                 else
272                                         drag_rect->property_x1() = event_x;
273
274                         if (drag_rect && _mouse_state == SelectRectDragging) {
275                                 if (event_y > drag_start_y)
276                                         drag_rect->property_y2() = event_y;
277                                 else
278                                         drag_rect->property_y1() = event_y;
279                                 
280                                 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
281                         }
282
283                         last_x = event_x;
284                         last_y = event_y;
285
286                 case EraseTouchDragging:
287                 case SelectTouchDragging:
288                         return false;
289
290                 default:
291                         break;
292                 }
293                 break;
294         
295         case GDK_BUTTON_RELEASE:
296                 event_x = ev->motion.x;
297                 event_y = ev->motion.y;
298                 group->w2i(event_x, event_y);
299                 group->ungrab(ev->button.time);
300                 event_frame = trackview.editor.pixel_to_frame(event_x);
301
302                 switch (_mouse_state) {
303                 case Pressed: // Clicked
304                         switch (trackview.editor.current_midi_edit_mode()) {
305                         case MidiEditSelect:
306                                 clear_selection();
307                                 break;
308                         case MidiEditPencil:
309                                 trackview.editor.snap_to(event_frame, -1);
310                                 event_x = trackview.editor.frame_to_pixel(event_frame);
311                                 create_note_at(event_x, event_y, _default_note_length);
312                         default:
313                                 break;
314                         }
315                         _mouse_state = None;
316                         return true;
317                 case SelectRectDragging: // Select drag done
318                         _mouse_state = None;
319                         delete drag_rect;
320                         drag_rect = NULL;
321                         return true;
322                 case AddDragging: // Add drag done
323                         _mouse_state = None;
324                         if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
325                                 create_note_at(drag_rect->property_x1(), drag_rect->property_y1(),
326                                                 trackview.editor.pixel_to_frame(
327                                                 drag_rect->property_x2() - drag_rect->property_x1()));
328                         }
329
330                         delete drag_rect;
331                         drag_rect = NULL;
332                         return true;
333                 default:
334                         break;
335                 }
336         
337         default:
338                 break;
339         }
340
341         return false;
342 }
343
344
345 /** Add a note to the model, and the view, at a canvas (click) coordinate */
346 void
347 MidiRegionView::create_note_at(double x, double y, double dur)
348 {
349         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
350         MidiStreamView* const view = mtv->midi_view();
351
352         double note = midi_stream_view()->y_to_note(y);
353
354         assert(note >= 0.0);
355         assert(note <= 127.0);
356
357         const nframes_t stamp = trackview.editor.pixel_to_frame (x);
358         assert(stamp >= 0);
359         //assert(stamp <= _region->length());
360
361         //const Meter& m = trackview.session().tempo_map().meter_at(stamp);
362         //const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
363         //double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
364
365         // Add a 1 beat long note (for now)
366         const boost::shared_ptr<Note> new_note(new Note(0, stamp, dur, (uint8_t)note, 0x40));
367         
368         view->update_bounds(new_note->note());
369
370         MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
371         cmd->add(new_note);
372         _model->apply_command(cmd);
373         
374         //add_note(new_note);
375 }
376
377
378 void
379 MidiRegionView::clear_events()
380 {
381         for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i)
382                 delete *i;
383         
384         MidiGhostRegion* gr;
385
386         for(vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
387                 if((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
388                         gr->clear_events();
389                 }
390         }
391
392         _events.clear();
393 }
394
395
396 void
397 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
398 {
399         _model = model;
400
401         if (_enable_display)
402                 redisplay_model();
403 }
404
405
406 void
407 MidiRegionView::redisplay_model()
408 {
409         // Don't redisplay the model if we're currently recording and displaying that
410         if (_active_notes)
411                 return;
412         
413         if (_model) {
414
415                 clear_events();
416                 begin_write();
417                 
418                 _model->read_lock();
419
420                 for (size_t i=0; i < _model->n_notes(); ++i)
421                         add_note(_model->note_at(i));
422
423                 end_write();
424
425                 /*for (Automatable::Controls::const_iterator i = _model->controls().begin();
426                                 i != _model->controls().end(); ++i) {
427
428                         assert(i->second);
429
430                         boost::shared_ptr<AutomationTimeAxisView> at
431                                 = midi_view()->automation_child(i->second->parameter());
432                         if (!at)
433                                 continue;
434
435                         Gdk::Color col = midi_stream_view()->get_region_color();
436                                 
437                         boost::shared_ptr<AutomationRegionView> arv;
438
439                         {
440                                 Glib::Mutex::Lock list_lock (i->second->list()->lock());
441
442                                 arv = boost::shared_ptr<AutomationRegionView>(
443                                                 new AutomationRegionView(at->canvas_display,
444                                                         *at.get(), _region, i->second->list(),
445                                                         midi_stream_view()->get_samples_per_unit(), col));
446                         }
447
448                         arv->set_duration(_region->length(), this);
449                         arv->init(col, true);
450
451                         _automation_children.insert(std::make_pair(i->second->parameter(), arv));
452                 }*/
453                 
454                 _model->read_unlock();
455
456         } else {
457                 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
458         }
459 }
460
461
462 MidiRegionView::~MidiRegionView ()
463 {
464         in_destructor = true;
465         if (_active_notes)
466                 end_write();
467
468         clear_events();
469
470         RegionViewGoingAway (this); /* EMIT_SIGNAL */
471 }
472
473
474 void
475 MidiRegionView::region_resized (Change what_changed)
476 {
477         RegionView::region_resized(what_changed);
478
479         if (what_changed & ARDOUR::PositionChanged) {
480         
481                 if (_enable_display)
482                         redisplay_model();
483         
484         } else if (what_changed & Change (StartChanged)) {
485
486                 //cerr << "MIDI RV START CHANGED" << endl;
487
488         } else if (what_changed & Change (LengthChanged)) {
489                 
490                 //cerr << "MIDI RV LENGTH CHANGED" << endl;
491         
492         }
493 }
494
495 void
496 MidiRegionView::reset_width_dependent_items (double pixel_width)
497 {
498         RegionView::reset_width_dependent_items(pixel_width);
499         assert(_pixel_width == pixel_width);
500                 
501         if (_enable_display)
502                 redisplay_model();
503 }
504
505 void
506 MidiRegionView::set_y_position_and_height (double y, double h)
507 {
508         RegionView::set_y_position_and_height(y, h - 1);
509
510         if (_enable_display) {
511                 
512                 _model->read_lock();
513
514                 for (std::vector<CanvasMidiEvent*>::const_iterator i = _events.begin(); i != _events.end(); ++i) {
515                         CanvasNote* note = dynamic_cast<CanvasNote*>(*i);
516                         if (note && note->note()) {
517                                 if(note->note()->note() < midi_stream_view()->lowest_note() ||
518                                    note->note()->note() > midi_stream_view()->highest_note()) {
519                                         if(canvas_item_visible(note)) {
520                                                 note->hide();
521                                         }
522                                 }
523                                 else {
524                                         const double y1 = midi_stream_view()->note_to_y(note->note()->note());
525                                         const double y2 = y1 + floor(midi_stream_view()->note_height());
526                                         
527                                         if(!canvas_item_visible(note)) {
528                                                 note->show();
529                                         }
530
531                                         note->property_y1() = y1;
532                                         note->property_y2() = y2;
533                                 }
534                         }
535                 }
536                 
537                 _model->read_unlock();
538         }
539
540         if (name_text) {
541                 name_text->raise_to_top();
542         }
543 }
544
545 GhostRegion*
546 MidiRegionView::add_ghost (TimeAxisView& tv)
547 {
548         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
549         CanvasNote* note;
550         assert(rtv);
551
552         double unit_position = _region->position () / samples_per_unit;
553         MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
554         MidiGhostRegion* ghost;
555
556         if(mtv && mtv->midi_view()) {
557                 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group.
558                    this is because it's nice to have midi notes on top of the note lines and
559                    audio waveforms under it.
560                  */
561                 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
562         }
563         else {
564                 ghost = new MidiGhostRegion (tv, trackview, unit_position);
565         }
566
567         ghost->set_height ();
568         ghost->set_duration (_region->length() / samples_per_unit);
569         ghosts.push_back (ghost);
570
571         for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
572                 if((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
573                         ghost->add_note(note);
574                 }
575         }
576
577         ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
578
579         return ghost;
580 }
581
582
583 /** Begin tracking note state for successive calls to add_event
584  */
585 void
586 MidiRegionView::begin_write()
587 {
588         assert(!_active_notes);
589         _active_notes = new CanvasNote*[128];
590         for (unsigned i=0; i < 128; ++i)
591                 _active_notes[i] = NULL;
592 }
593
594
595 /** Destroy note state for add_event
596  */
597 void
598 MidiRegionView::end_write()
599 {
600         delete[] _active_notes;
601         _active_notes = NULL;
602 }
603
604
605 /** Resolve an active MIDI note (while recording).
606  */
607 void
608 MidiRegionView::resolve_note(uint8_t note, double end_time)
609 {
610         if (midi_view()->note_mode() != Sustained)
611                 return;
612
613         if (_active_notes && _active_notes[note]) {
614                 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)end_time);
615                 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
616                 _active_notes[note] = NULL;
617         }
618 }
619
620
621 /** Extend active notes to rightmost edge of region (if length is changed)
622  */
623 void
624 MidiRegionView::extend_active_notes()
625 {
626         if (!_active_notes)
627                 return;
628
629         for (unsigned i=0; i < 128; ++i)
630                 if (_active_notes[i])
631                         _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
632 }
633
634
635 /** Add a MIDI note to the view (with duration).
636  *
637  * If in sustained mode, notes with duration 0 will be considered active
638  * notes, and resolve_note should be called when the corresponding note off
639  * event arrives, to properly display the note.
640  */
641 void
642 MidiRegionView::add_note(const boost::shared_ptr<Note> note)
643 {
644         assert(note->time() >= 0);
645         //assert(note->time() < _region->length());
646         assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
647
648         ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
649         
650         if (midi_view()->note_mode() == Sustained) {
651         
652                 //cerr << "MRV::add_note sustained " << note->note() << " @ " << note->time()
653                 //      << " .. " << note->end_time() << endl;
654
655                 const double y1 = midi_stream_view()->note_to_y(note->note());
656
657                 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
658                 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note->time());
659                 ev_rect->property_y1() = y1;
660                 if (note->duration() > 0)
661                         ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note->end_time()));
662                 else
663                         ev_rect->property_x2() = trackview.editor.frame_to_pixel(_region->length());
664                 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
665
666                 ev_rect->property_fill_color_rgba() = note_fill_color(note->velocity());
667                 ev_rect->property_outline_color_rgba() = note_outline_color(note->velocity());
668
669                 if (note->duration() == 0) {
670                         _active_notes[note->note()] = ev_rect;
671                         /* outline all but right edge */
672                         ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
673                 } else {
674                         /* outline all edges */
675                         ev_rect->property_outline_what() = (guint32) 0xF;
676                 }
677                 
678                 ev_rect->show();
679                 _events.push_back(ev_rect);
680
681                 MidiGhostRegion* gr;
682                 
683                 for(vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
684                         if((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
685                                 gr->add_note(ev_rect);
686                         }
687                 }
688
689         } else if (midi_view()->note_mode() == Percussive) {
690                 
691                 //cerr << "MRV::add_note percussive " << note->note() << " @ " << note->time()
692                 //      << " .. " << note->end_time() << endl;
693                 
694                 const double diamond_size = midi_stream_view()->note_height() / 2.0;
695                 const double x = trackview.editor.frame_to_pixel((nframes_t)note->time());
696                 const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
697
698                 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
699                 ev_diamond->move(x, y);
700                 ev_diamond->show();
701                 ev_diamond->property_fill_color_rgba() = note_fill_color(note->velocity());
702                 ev_diamond->property_outline_color_rgba() = note_outline_color(note->velocity());
703                 _events.push_back(ev_diamond);
704         
705         }
706 }
707
708 void
709 MidiRegionView::delete_selection()
710 {
711         assert(_delta_command);
712
713         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
714                 if ((*i)->selected())
715                         _delta_command->remove((*i)->note());
716
717         _selection.clear();
718 }
719
720 void
721 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasMidiEvent* ev)
722 {
723         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
724                 if ((*i)->selected() && (*i) != ev)
725                         (*i)->selected(false);
726
727         _selection.clear();
728 }
729
730 void
731 MidiRegionView::unique_select(ArdourCanvas::CanvasMidiEvent* ev)
732 {
733         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
734                 if ((*i) != ev)
735                         (*i)->selected(false);
736
737         _selection.clear();
738         _selection.insert(ev);
739         
740         if ( ! ev->selected())
741                 ev->selected(true);
742 }
743         
744 void
745 MidiRegionView::note_selected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
746 {
747         if ( ! add)
748                 clear_selection_except(ev);
749         
750         _selection.insert(ev);
751         
752         if ( ! ev->selected())
753                 ev->selected(true);
754 }
755
756
757 void
758 MidiRegionView::note_deselected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
759 {
760         if ( ! add)
761                 clear_selection_except(ev);
762         
763         _selection.erase(ev);
764         
765         if (ev->selected())
766                 ev->selected(false);
767 }
768
769
770 void
771 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
772 {
773         const double last_y = std::min(y1, y2);
774         const double y      = std::max(y1, y2);
775         
776         // FIXME: so, so, so much slower than this should be
777
778         if (x1 < x2) {
779                 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
780                         if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
781                                 (*i)->selected(true);
782                                 _selection.insert(*i);
783                         } else {
784                                 (*i)->selected(false);
785                                 _selection.erase(*i);
786                         }
787                 }
788         } else {
789                 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
790                         if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
791                                 (*i)->selected(true);
792                                 _selection.insert(*i);
793                         } else {
794                                 (*i)->selected(false);
795                                 _selection.erase(*i);
796                         }
797                 }
798         }
799 }
800
801         
802 void
803 MidiRegionView::move_selection(double dx, double dy)
804 {
805         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
806                 (*i)->item()->move(dx, dy);
807 }
808
809
810 void
811 MidiRegionView::note_dropped(CanvasMidiEvent* ev, double dt, uint8_t dnote)
812 {
813         // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
814         if (_selection.find(ev) != _selection.end()) {
815                 start_delta_command();
816
817                 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
818                         command_remove_note(*i);
819                         const boost::shared_ptr<Note> copy(new Note(*(*i)->note().get())); 
820
821                         copy->set_time((*i)->note()->time() + dt);
822                         copy->set_note((*i)->note()->note() + dnote);
823
824                         command_add_note(copy);
825                 }
826                 apply_command();
827         }
828 }
829         
830
831 void
832 MidiRegionView::note_entered(ArdourCanvas::CanvasMidiEvent* ev)
833 {
834         if (ev->note() && _mouse_state == EraseTouchDragging) {
835                 start_delta_command();
836                 ev->selected(true);
837                 _delta_command->remove(ev->note());
838         } else if (_mouse_state == SelectTouchDragging) {
839                 note_selected(ev, true);
840         }
841 }
842         
843
844 void
845 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
846 {
847         boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
848         if (msrc)
849                 display_model(msrc->model());
850 }
851
852 void
853 MidiRegionView::set_frame_color()
854 {
855         if (frame) {
856                 if (_selected && should_show_selection) {
857                         frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
858                 } else {
859                         frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
860                 }
861         }
862 }