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