Apply note resizing patch from hans.
[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 "simpleline.h"
43 #include "canvas-hit.h"
44 #include "public_editor.h"
45 #include "ghostregion.h"
46 #include "midi_time_axis.h"
47 #include "automation_time_axis.h"
48 #include "automation_region_view.h"
49 #include "utils.h"
50 #include "midi_util.h"
51 #include "gui_thread.h"
52 #include "keyboard.h"
53
54 #include "i18n.h"
55
56 using namespace sigc;
57 using namespace ARDOUR;
58 using namespace PBD;
59 using namespace Editing;
60 using namespace ArdourCanvas;
61
62 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, 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         group->lower_to_bottom();
72         _note_group->raise_to_top();
73
74         frame->property_fill_color_rgba() = 0xff000033;
75 }
76
77 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
78         : RegionView (parent, tv, r, spu, basic_color, visibility)
79         , _default_note_length(0.0)
80         , _active_notes(0)
81         , _note_group(new ArdourCanvas::Group(*parent))
82         , _delta_command(NULL)
83         , _mouse_state(None)
84         , _pressed_button(0)
85 {
86         _note_group->raise_to_top();
87 }
88
89 void
90 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
91 {
92         if (wfd)
93                 midi_region()->midi_source(0)->load_model();
94
95         const Meter& m = trackview.session().tempo_map().meter_at(_region->position());
96         const Tempo& t = trackview.session().tempo_map().tempo_at(_region->position());
97         _default_note_length = m.frames_per_bar(t, trackview.session().frame_rate())
98                         / m.beats_per_bar();
99
100         _model = midi_region()->midi_source(0)->model();
101         _enable_display = false;
102
103         RegionView::init(basic_color, false);
104
105         compute_colors (basic_color);
106
107         reset_width_dependent_items ((double) _region->length() / samples_per_unit);
108
109         set_y_position_and_height (0, trackview.height);
110
111         region_muted ();
112         region_resized (BoundsChanged);
113         region_locked ();
114
115         _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
116
117         set_colors ();
118
119         _enable_display = true;
120
121         if (_model) {
122                 if (wfd) {
123                         redisplay_model();
124                 }
125                 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
126         }
127
128         midi_region()->midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegionView::switch_source));
129
130         group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
131
132 }
133
134 bool
135 MidiRegionView::canvas_event(GdkEvent* ev)
136 {
137         static bool delete_mod = false;
138         static Editing::MidiEditMode original_mode;
139
140         static double drag_start_x, drag_start_y;
141         static double last_x, last_y;
142         double event_x, event_y;
143         nframes_t event_frame = 0;
144
145         static ArdourCanvas::SimpleRect* drag_rect = NULL;
146
147         if (trackview.editor.current_mouse_mode() != MouseNote)
148                 return false;
149
150         // Mmmm, spaghetti
151
152         switch (ev->type) {
153         case GDK_KEY_PRESS:
154                 if (ev->key.keyval == GDK_Delete && !delete_mod) {
155                         delete_mod = true;
156                         original_mode = trackview.editor.current_midi_edit_mode();
157                         trackview.editor.set_midi_edit_mode(MidiEditErase);
158                         start_delta_command();
159                         _mouse_state = EraseTouchDragging;
160                         return true;
161                 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
162                         _mouse_state = SelectTouchDragging;
163                         return true;
164                 }
165                 return false;
166
167         case GDK_KEY_RELEASE:
168                 if (ev->key.keyval == GDK_Delete) {
169                         if (_mouse_state == EraseTouchDragging) {
170                                 delete_selection();
171                                 apply_command();
172                         }
173                         if (delete_mod) {
174                                 trackview.editor.set_midi_edit_mode(original_mode);
175                                 delete_mod = false;
176                         }
177                         return true;
178                 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
179                         _mouse_state = None;
180                         return true;
181                 }
182                 return false;
183
184         case GDK_BUTTON_PRESS:
185                 if (_mouse_state != SelectTouchDragging && _mouse_state != EraseTouchDragging)
186                         _mouse_state = Pressed;
187                 _pressed_button = ev->button.button;
188                 return true;
189
190         case GDK_ENTER_NOTIFY:
191                 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
192                 Keyboard::magic_widget_grab_focus();
193                 group->grab_focus();
194                 break;
195
196         case GDK_MOTION_NOTIFY:
197                 event_x = ev->motion.x;
198                 event_y = ev->motion.y;
199                 group->w2i(event_x, event_y);
200
201                 event_frame = trackview.editor.pixel_to_frame(event_x);
202                 trackview.editor.snap_to(event_frame);
203
204                 switch (_mouse_state) {
205                 case Pressed: // Drag start
206
207                         // Select drag start
208                         if (_pressed_button == 1 && trackview.editor.current_midi_edit_mode() == MidiEditSelect) {
209                                 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
210                                                 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
211                                 last_x = event_x;
212                                 last_y = event_y;
213                                 drag_start_x = event_x;
214                                 drag_start_y = event_y;
215
216                                 drag_rect = new ArdourCanvas::SimpleRect(*group);
217                                 drag_rect->property_x1() = event_x;
218                                 drag_rect->property_y1() = event_y;
219                                 drag_rect->property_x2() = event_x;
220                                 drag_rect->property_y2() = event_y;
221                                 drag_rect->property_outline_what() = 0xFF;
222                                 drag_rect->property_outline_color_rgba()
223                                         = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
224                                 drag_rect->property_fill_color_rgba()
225                                         = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
226
227                                 _mouse_state = SelectRectDragging;
228                                 return true;
229
230                         // Add note drag start
231                         } else if (trackview.editor.current_midi_edit_mode() == MidiEditPencil) {
232                                 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
233                                                 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
234                                 last_x = event_x;
235                                 last_y = event_y;
236                                 drag_start_x = event_x;
237                                 drag_start_y = event_y;
238
239                                 drag_rect = new ArdourCanvas::SimpleRect(*group);
240                                 drag_rect->property_x1() = trackview.editor.frame_to_pixel(event_frame);
241
242                                 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
243                                 drag_rect->property_x2() = event_x;
244                                 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
245                                 drag_rect->property_outline_what() = 0xFF;
246                                 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
247
248                                 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
249
250                                 _mouse_state = AddDragging;
251                                 return true;
252                         }
253
254                         return false;
255
256                 case SelectRectDragging: // Select drag motion
257                 case AddDragging: // Add note drag motion
258                         if (ev->motion.is_hint) {
259                                 int t_x;
260                                 int t_y;
261                                 GdkModifierType state;
262                                 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
263                                 event_x = t_x;
264                                 event_y = t_y;
265                         }
266
267                         if (_mouse_state == AddDragging)
268                                 event_x = trackview.editor.frame_to_pixel(event_frame);
269
270                         if (drag_rect)
271                                 if (event_x > drag_start_x)
272                                         drag_rect->property_x2() = event_x;
273                                 else
274                                         drag_rect->property_x1() = event_x;
275
276                         if (drag_rect && _mouse_state == SelectRectDragging) {
277                                 if (event_y > drag_start_y)
278                                         drag_rect->property_y2() = event_y;
279                                 else
280                                         drag_rect->property_y1() = event_y;
281
282                                 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
283                         }
284
285                         last_x = event_x;
286                         last_y = event_y;
287
288                 case EraseTouchDragging:
289                 case SelectTouchDragging:
290                         return false;
291
292                 default:
293                         break;
294                 }
295                 break;
296
297         case GDK_BUTTON_RELEASE:
298                 event_x = ev->motion.x;
299                 event_y = ev->motion.y;
300                 group->w2i(event_x, event_y);
301                 group->ungrab(ev->button.time);
302                 event_frame = trackview.editor.pixel_to_frame(event_x);
303
304                 switch (_mouse_state) {
305                 case Pressed: // Clicked
306                         switch (trackview.editor.current_midi_edit_mode()) {
307                         case MidiEditSelect:
308                                 clear_selection();
309                                 break;
310                         case MidiEditPencil:
311                                 trackview.editor.snap_to(event_frame, -1);
312                                 event_x = trackview.editor.frame_to_pixel(event_frame);
313                                 create_note_at(event_x, event_y, _default_note_length);
314                         default:
315                                 break;
316                         }
317                         _mouse_state = None;
318                         return true;
319                 case SelectRectDragging: // Select drag done
320                         _mouse_state = None;
321                         delete drag_rect;
322                         drag_rect = NULL;
323                         return true;
324                 case AddDragging: // Add drag done
325                         _mouse_state = None;
326                         if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
327                                 create_note_at(drag_rect->property_x1(), drag_rect->property_y1(),
328                                                 trackview.editor.pixel_to_frame(
329                                                 drag_rect->property_x2() - drag_rect->property_x1()));
330                         }
331
332                         delete drag_rect;
333                         drag_rect = NULL;
334                         return true;
335                 default:
336                         break;
337                 }
338
339         default:
340                 break;
341         }
342
343         return false;
344 }
345
346
347 /** Add a note to the model, and the view, at a canvas (click) coordinate */
348 void
349 MidiRegionView::create_note_at(double x, double y, double dur)
350 {
351         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
352         MidiStreamView* const view = mtv->midi_view();
353
354         double note = midi_stream_view()->y_to_note(y);
355
356         assert(note >= 0.0);
357         assert(note <= 127.0);
358
359         const nframes_t stamp = trackview.editor.pixel_to_frame (x);
360         assert(stamp >= 0);
361         //assert(stamp <= _region->length());
362
363         //const Meter& m = trackview.session().tempo_map().meter_at(stamp);
364         //const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
365         //double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
366
367         // Add a 1 beat long note (for now)
368         const boost::shared_ptr<Note> new_note(new Note(0, stamp, dur, (uint8_t)note, 0x40));
369
370         view->update_bounds(new_note->note());
371
372         MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
373         cmd->add(new_note);
374         _model->apply_command(cmd);
375
376         //add_note(new_note);
377 }
378
379
380 void
381 MidiRegionView::clear_events()
382 {
383         clear_selection();
384
385         MidiGhostRegion* gr;
386         for (vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
387                 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
388                         gr->clear_events();
389                 }
390         }
391
392         for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i)
393                 delete *i;
394
395         _events.clear();
396 }
397
398
399 void
400 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
401 {
402         _model = model;
403
404         if (_enable_display)
405                 redisplay_model();
406 }
407
408
409 void
410 MidiRegionView::redisplay_model()
411 {
412         // Don't redisplay the model if we're currently recording and displaying that
413         if (_active_notes)
414                 return;
415
416         if (_model) {
417
418                 clear_events();
419                 begin_write();
420
421                 _model->read_lock();
422
423                 for (size_t i=0; i < _model->n_notes(); ++i)
424                         add_note(_model->note_at(i));
425
426                 end_write();
427
428                 /*for (Automatable::Controls::const_iterator i = _model->controls().begin();
429                                 i != _model->controls().end(); ++i) {
430
431                         assert(i->second);
432
433                         boost::shared_ptr<AutomationTimeAxisView> at
434                                 = midi_view()->automation_child(i->second->parameter());
435                         if (!at)
436                                 continue;
437
438                         Gdk::Color col = midi_stream_view()->get_region_color();
439
440                         boost::shared_ptr<AutomationRegionView> arv;
441
442                         {
443                                 Glib::Mutex::Lock list_lock (i->second->list()->lock());
444
445                                 arv = boost::shared_ptr<AutomationRegionView>(
446                                                 new AutomationRegionView(at->canvas_display,
447                                                         *at.get(), _region, i->second->list(),
448                                                         midi_stream_view()->get_samples_per_unit(), col));
449                         }
450
451                         arv->set_duration(_region->length(), this);
452                         arv->init(col, true);
453
454                         _automation_children.insert(std::make_pair(i->second->parameter(), arv));
455                 }*/
456
457                 _model->read_unlock();
458
459         } else {
460                 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
461         }
462 }
463
464
465 MidiRegionView::~MidiRegionView ()
466 {
467         in_destructor = true;
468
469         RegionViewGoingAway (this); /* EMIT_SIGNAL */
470
471         if (_active_notes)
472                 end_write();
473
474         _selection.clear();
475         clear_events();
476         delete _note_group;
477         delete _delta_command;
478 }
479
480
481 void
482 MidiRegionView::region_resized (Change what_changed)
483 {
484         RegionView::region_resized(what_changed);
485
486         if (what_changed & ARDOUR::PositionChanged) {
487
488                 if (_enable_display)
489                         redisplay_model();
490
491         } else if (what_changed & Change (StartChanged)) {
492
493                 //cerr << "MIDI RV START CHANGED" << endl;
494
495         } else if (what_changed & Change (LengthChanged)) {
496
497                 //cerr << "MIDI RV LENGTH CHANGED" << endl;
498
499         }
500 }
501
502 void
503 MidiRegionView::reset_width_dependent_items (double pixel_width)
504 {
505         RegionView::reset_width_dependent_items(pixel_width);
506         assert(_pixel_width == pixel_width);
507
508         if (_enable_display)
509                 redisplay_model();
510 }
511
512 void
513 MidiRegionView::set_y_position_and_height (double y, double h)
514 {
515         RegionView::set_y_position_and_height(y, h - 1);
516
517         if (_enable_display) {
518
519                 _model->read_lock();
520
521                 for (std::vector<CanvasMidiEvent*>::const_iterator i = _events.begin(); i != _events.end(); ++i) {
522                         CanvasNote* note = dynamic_cast<CanvasNote*>(*i);
523                         if (note && note->note()) {
524                                 if (note->note()->note() < midi_stream_view()->lowest_note() ||
525                                    note->note()->note() > midi_stream_view()->highest_note()) {
526                                         if (canvas_item_visible(note)) {
527                                                 note->hide();
528                                         }
529                                 }
530                                 else {
531                                         const double y1 = midi_stream_view()->note_to_y(note->note()->note());
532                                         const double y2 = y1 + floor(midi_stream_view()->note_height());
533
534                                         if (!canvas_item_visible(note)) {
535                                                 note->show();
536                                         }
537
538                                         note->property_y1() = y1;
539                                         note->property_y2() = y2;
540                                 }
541                         }
542                 }
543
544                 _model->read_unlock();
545         }
546
547         if (name_text) {
548                 name_text->raise_to_top();
549         }
550 }
551
552 GhostRegion*
553 MidiRegionView::add_ghost (TimeAxisView& tv)
554 {
555         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
556         CanvasNote* note;
557         assert(rtv);
558
559         double unit_position = _region->position () / samples_per_unit;
560         MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
561         MidiGhostRegion* ghost;
562
563         if (mtv && mtv->midi_view()) {
564                 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group.
565                    this is because it's nice to have midi notes on top of the note lines and
566                    audio waveforms under it.
567                  */
568                 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
569         }
570         else {
571                 ghost = new MidiGhostRegion (tv, trackview, unit_position);
572         }
573
574         ghost->set_height ();
575         ghost->set_duration (_region->length() / samples_per_unit);
576         ghosts.push_back (ghost);
577
578         for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
579                 if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
580                         ghost->add_note(note);
581                 }
582         }
583
584         ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
585
586         return ghost;
587 }
588
589
590 /** Begin tracking note state for successive calls to add_event
591  */
592 void
593 MidiRegionView::begin_write()
594 {
595         assert(!_active_notes);
596         _active_notes = new CanvasNote*[128];
597         for (unsigned i=0; i < 128; ++i)
598                 _active_notes[i] = NULL;
599 }
600
601
602 /** Destroy note state for add_event
603  */
604 void
605 MidiRegionView::end_write()
606 {
607         delete[] _active_notes;
608         _active_notes = NULL;
609 }
610
611
612 /** Resolve an active MIDI note (while recording).
613  */
614 void
615 MidiRegionView::resolve_note(uint8_t note, double end_time)
616 {
617         if (midi_view()->note_mode() != Sustained)
618                 return;
619
620         if (_active_notes && _active_notes[note]) {
621                 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)end_time);
622                 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
623                 _active_notes[note] = NULL;
624         }
625 }
626
627
628 /** Extend active notes to rightmost edge of region (if length is changed)
629  */
630 void
631 MidiRegionView::extend_active_notes()
632 {
633         if (!_active_notes)
634                 return;
635
636         for (unsigned i=0; i < 128; ++i)
637                 if (_active_notes[i])
638                         _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
639 }
640
641
642 /** Add a MIDI note to the view (with duration).
643  *
644  * If in sustained mode, notes with duration 0 will be considered active
645  * notes, and resolve_note should be called when the corresponding note off
646  * event arrives, to properly display the note.
647  */
648 void
649 MidiRegionView::add_note(const boost::shared_ptr<Note> note)
650 {
651         assert(note->time() >= 0);
652         //assert(note->time() < _region->length());
653         assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
654
655         ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
656
657         if (midi_view()->note_mode() == Sustained) {
658
659                 //cerr << "MRV::add_note sustained " << note->note() << " @ " << note->time()
660                 //      << " .. " << note->end_time() << endl;
661
662                 const double y1 = midi_stream_view()->note_to_y(note->note());
663
664                 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
665                 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note->time());
666                 ev_rect->property_y1() = y1;
667                 if (note->duration() > 0)
668                         ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note->end_time()));
669                 else
670                         ev_rect->property_x2() = trackview.editor.frame_to_pixel(_region->length());
671                 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
672
673                 ev_rect->property_fill_color_rgba() = note_fill_color(note->velocity());
674                 ev_rect->property_outline_color_rgba() = note_outline_color(note->velocity());
675
676                 if (note->duration() == 0) {
677                         _active_notes[note->note()] = ev_rect;
678                         /* outline all but right edge */
679                         ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
680                 } else {
681                         /* outline all edges */
682                         ev_rect->property_outline_what() = (guint32) 0xF;
683                 }
684
685                 ev_rect->show();
686                 _events.push_back(ev_rect);
687
688                 MidiGhostRegion* gr;
689
690                 for (vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
691                         if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
692                                 gr->add_note(ev_rect);
693                         }
694                 }
695
696         } else if (midi_view()->note_mode() == Percussive) {
697
698                 //cerr << "MRV::add_note percussive " << note->note() << " @ " << note->time()
699                 //      << " .. " << note->end_time() << endl;
700
701                 const double diamond_size = midi_stream_view()->note_height() / 2.0;
702                 const double x = trackview.editor.frame_to_pixel((nframes_t)note->time());
703                 const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
704
705                 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
706                 ev_diamond->move(x, y);
707                 ev_diamond->show();
708                 ev_diamond->property_fill_color_rgba() = note_fill_color(note->velocity());
709                 ev_diamond->property_outline_color_rgba() = note_outline_color(note->velocity());
710                 _events.push_back(ev_diamond);
711
712         }
713 }
714
715 void
716 MidiRegionView::delete_selection()
717 {
718         assert(_delta_command);
719
720         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
721                 if ((*i)->selected())
722                         _delta_command->remove((*i)->note());
723
724         _selection.clear();
725 }
726
727 void
728 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasMidiEvent* ev)
729 {
730         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
731                 if ((*i)->selected() && (*i) != ev)
732                         (*i)->selected(false);
733
734         _selection.clear();
735 }
736
737 void
738 MidiRegionView::unique_select(ArdourCanvas::CanvasMidiEvent* ev)
739 {
740         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
741                 if ((*i) != ev)
742                         (*i)->selected(false);
743
744         _selection.clear();
745         _selection.insert(ev);
746
747         if ( ! ev->selected())
748                 ev->selected(true);
749 }
750
751 void
752 MidiRegionView::note_selected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
753 {
754         if ( ! add)
755                 clear_selection_except(ev);
756
757         _selection.insert(ev);
758
759         if ( ! ev->selected())
760                 ev->selected(true);
761 }
762
763
764 void
765 MidiRegionView::note_deselected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
766 {
767         if ( ! add)
768                 clear_selection_except(ev);
769
770         _selection.erase(ev);
771
772         if (ev->selected())
773                 ev->selected(false);
774 }
775
776
777 void
778 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
779 {
780         const double last_y = std::min(y1, y2);
781         const double y      = std::max(y1, y2);
782
783         // FIXME: so, so, so much slower than this should be
784
785         if (x1 < x2) {
786                 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
787                         if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
788                                 (*i)->selected(true);
789                                 _selection.insert(*i);
790                         } else {
791                                 (*i)->selected(false);
792                                 _selection.erase(*i);
793                         }
794                 }
795         } else {
796                 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
797                         if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
798                                 (*i)->selected(true);
799                                 _selection.insert(*i);
800                         } else {
801                                 (*i)->selected(false);
802                                 _selection.erase(*i);
803                         }
804                 }
805         }
806 }
807
808
809 void
810 MidiRegionView::move_selection(double dx, double dy)
811 {
812         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
813                 (*i)->item()->move(dx, dy);
814 }
815
816
817 void
818 MidiRegionView::note_dropped(CanvasMidiEvent* ev, double dt, uint8_t dnote)
819 {
820         // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
821         if (_selection.find(ev) != _selection.end()) {
822                 start_delta_command();
823
824                 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
825                         Selection::iterator next = i;
826                         ++next;
827
828                         command_remove_note(*i);
829                         const boost::shared_ptr<Note> copy(new Note(*(*i)->note().get()));
830
831                         copy->set_time((*i)->note()->time() + dt);
832                         copy->set_note((*i)->note()->note() + dnote);
833
834                         command_add_note(copy);
835
836                         _selection.erase(i);
837                         i = next;
838                 }
839                 apply_command();
840         }
841 }
842
843 void
844 MidiRegionView::begin_resizing(CanvasNote::NoteEnd note_end)
845 {
846         _resize_data.clear();
847
848         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
849                 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
850
851                 // only insert CanvasNotes into the map
852                 if(note) {
853                         NoteResizeData *resize_data = new NoteResizeData();
854                         resize_data->canvas_note = note;
855
856                         SimpleRect *resize_rect = new SimpleRect(*group, note->x1(), note->y1(), note->x2(), note->y2());
857
858                         uint fill_color = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get(), 128);
859                         fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
860
861                         resize_rect->property_fill_color_rgba() =
862                                 UINT_INTERPOLATE(
863                                         note_fill_color(note->note()->velocity()),
864                                         fill_color,
865                                         0.85);
866
867                         resize_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get();
868                         resize_data->resize_rect = resize_rect;
869
870                         if(note_end == CanvasNote::NOTE_ON) {
871                                 resize_data->current_x = note->x1();
872                         } else { // NOTE_OFF
873                                 resize_data->current_x = note->x2();
874                         }
875
876                         _resize_data.push_back(resize_data);
877                 }
878         }
879 }
880
881 double
882 MidiRegionView::snap_to(double x)
883 {
884         PublicEditor &editor = trackview.editor;
885
886         nframes_t frame = editor.pixel_to_frame(x);
887         editor.snap_to(frame);
888         return (double) editor.frame_to_pixel(frame);
889 }
890
891 void
892 MidiRegionView::update_resizing(CanvasNote::NoteEnd note_end, double dx, bool relative)
893 {
894
895
896         for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
897                 SimpleRect     *resize_rect = (*i)->resize_rect;
898                 CanvasNote     *canvas_note = (*i)->canvas_note;
899
900                 if(relative) {
901                         (*i)->current_x = (*i)->current_x + dx;
902                 } else {
903                         (*i)->current_x = dx;
904                 }
905
906                 double current_x = (*i)->current_x;
907
908                 if(note_end == CanvasNote::NOTE_ON) {
909                         resize_rect->property_x1() = snap_to(current_x);
910                         resize_rect->property_x2() = canvas_note->x2();
911                 } else {
912                         resize_rect->property_x2() = snap_to(current_x);
913                         resize_rect->property_x1() = canvas_note->x1();
914                 }
915         }
916 }
917
918 void
919 MidiRegionView::commit_resizing(CanvasNote::NoteEnd note_end, double event_x, bool relative)
920 {
921         start_delta_command();
922
923         for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
924                 CanvasNote *canvas_note = (*i)->canvas_note;
925                 SimpleRect *resize_rect = (*i)->resize_rect;
926                 double      current_x   = (*i)->current_x;
927
928
929                 if(!relative) {
930                         current_x = event_x;
931                 }
932
933                 nframes_t current_frame = trackview.editor.pixel_to_frame(current_x);
934                 trackview.editor.snap_to(current_frame);
935
936                 const boost::shared_ptr<Note> copy(new Note(*(canvas_note->note().get())));
937
938                 // resize beginning of note
939                 if (note_end == CanvasNote::NOTE_ON && current_frame < copy->end_time()) {
940                         command_remove_note(canvas_note);
941                         copy->on_event().time() = current_frame;
942                         command_add_note(copy);
943                 }
944                 // resize end of note
945                 if (note_end == CanvasNote::NOTE_OFF && current_frame > copy->time()) {
946                         command_remove_note(canvas_note);
947                         copy->off_event().time() = current_frame;
948                         command_add_note(copy);
949                 }
950
951                 delete resize_rect;
952                 delete (*i);
953         }
954
955         _resize_data.clear();
956         apply_command();
957         clear_selection();
958 }
959
960 void
961 MidiRegionView::note_entered(ArdourCanvas::CanvasMidiEvent* ev)
962 {
963         if (ev->note() && _mouse_state == EraseTouchDragging) {
964                 start_delta_command();
965                 ev->selected(true);
966                 _delta_command->remove(ev->note());
967         } else if (_mouse_state == SelectTouchDragging) {
968                 note_selected(ev, true);
969         }
970 }
971
972
973 void
974 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
975 {
976         boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
977         if (msrc)
978                 display_model(msrc->model());
979 }
980
981 void
982 MidiRegionView::set_frame_color()
983 {
984         if (frame) {
985                 if (_selected && should_show_selection) {
986                         frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
987                 } else {
988                         frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
989                 }
990         }
991 }