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