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