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