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