90fcce84eaefb3a16eef937f98fd0b17435f911f
[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_model.h>
36
37 #include "streamview.h"
38 #include "midi_region_view.h"
39 #include "midi_streamview.h"
40 #include "midi_time_axis.h"
41 #include "simpleline.h"
42 #include "canvas-hit.h"
43 #include "canvas-note.h"
44 #include "canvas-program-change.h"
45 #include "public_editor.h"
46 #include "ghostregion.h"
47 #include "midi_time_axis.h"
48 #include "automation_time_axis.h"
49 #include "automation_region_view.h"
50 #include "utils.h"
51 #include "midi_util.h"
52 #include "gui_thread.h"
53 #include "keyboard.h"
54
55 #include "i18n.h"
56
57 using namespace sigc;
58 using namespace ARDOUR;
59 using namespace PBD;
60 using namespace Editing;
61 using namespace ArdourCanvas;
62
63 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color)
64         : RegionView (parent, tv, r, spu, basic_color)
65         , _force_channel(-1)
66         , _last_channel_selection(0xFFFF)
67         , _default_note_length(0.0)
68         , _current_range_min(0)
69         , _current_range_max(0)
70         , _active_notes(0)
71         , _note_group(new ArdourCanvas::Group(*parent))
72         , _delta_command(NULL)
73         , _mouse_state(None)
74         , _pressed_button(0)
75 {
76         _note_group->raise_to_top();
77 }
78
79 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
80         : RegionView (parent, tv, r, spu, basic_color, false, visibility)
81         , _force_channel(-1)
82         , _last_channel_selection(0xFFFF)
83         , _default_note_length(0.0)
84         , _active_notes(0)
85         , _note_group(new ArdourCanvas::Group(*parent))
86         , _delta_command(NULL)
87         , _mouse_state(None)
88         , _pressed_button(0)
89         
90 {
91         _note_group->raise_to_top();
92 }
93
94 void
95 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
96 {
97         if (wfd)
98                 midi_region()->midi_source(0)->load_model();
99
100         const Meter& m = trackview.session().tempo_map().meter_at(_region->position());
101         const Tempo& t = trackview.session().tempo_map().tempo_at(_region->position());
102         _default_note_length = m.frames_per_bar(t, trackview.session().frame_rate())
103                         / m.beats_per_bar();
104
105         _model = midi_region()->midi_source(0)->model();
106         _enable_display = false;
107
108         RegionView::init (basic_color, false);
109
110         compute_colors (basic_color);
111
112         set_height (trackview.current_height());
113
114         region_muted ();
115         region_sync_changed ();
116         region_resized (BoundsChanged);
117         region_locked ();
118         
119         reset_width_dependent_items (_pixel_width);
120         //reset_width_dependent_items ((double) _region->length() / samples_per_unit);
121
122         set_colors ();
123
124         _enable_display = true;
125         if (_model) {
126                 if (wfd) {
127                         redisplay_model();
128                 }
129                 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
130         }
131
132         group->raise_to_top();
133         group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
134
135         midi_view()->signal_channel_mode_changed().connect(
136                         mem_fun(this, &MidiRegionView::midi_channel_mode_changed));
137 }
138
139 bool
140 MidiRegionView::canvas_event(GdkEvent* ev)
141 {
142         static bool delete_mod = false;
143         static Editing::MidiEditMode original_mode;
144
145         static double drag_start_x, drag_start_y;
146         static double last_x, last_y;
147         double event_x, event_y;
148         nframes64_t event_frame = 0;
149
150         static ArdourCanvas::SimpleRect* drag_rect = NULL;
151
152         if (trackview.editor.current_mouse_mode() != MouseNote)
153                 return false;
154
155         // Mmmm, spaghetti
156
157         switch (ev->type) {
158         case GDK_KEY_PRESS:
159                 if (ev->key.keyval == GDK_Delete && !delete_mod) {
160                         delete_mod = true;
161                         original_mode = trackview.editor.current_midi_edit_mode();
162                         trackview.editor.set_midi_edit_mode(MidiEditErase);
163                         start_delta_command(_("erase notes"));
164                         _mouse_state = EraseTouchDragging;
165                         return true;
166                 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
167                         _mouse_state = SelectTouchDragging;
168                         return true;
169                 } else if (ev->key.keyval == GDK_Escape) {
170                         clear_selection();
171                         _mouse_state = None;
172                 }
173                 return false;
174
175         case GDK_KEY_RELEASE:
176                 if (ev->key.keyval == GDK_Delete) {
177                         if (_mouse_state == EraseTouchDragging) {
178                                 delete_selection();
179                                 apply_command();
180                         }
181                         if (delete_mod) {
182                                 trackview.editor.set_midi_edit_mode(original_mode);
183                                 _mouse_state = None;
184                                 delete_mod = false;
185                         }
186                         return true;
187                 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
188                         _mouse_state = None;
189                         return true;
190                 }
191                 return false;
192
193         case GDK_BUTTON_PRESS:
194                 if (_mouse_state != SelectTouchDragging && 
195                         _mouse_state != EraseTouchDragging &&
196                         ev->button.button == 1) {
197                         _pressed_button = ev->button.button;
198                         _mouse_state = Pressed;
199                         return true;
200                 }
201                 _pressed_button = ev->button.button;
202                 return true;
203
204         case GDK_2BUTTON_PRESS:
205                 return true;
206
207         case GDK_ENTER_NOTIFY:
208                 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
209                 Keyboard::magic_widget_grab_focus();
210                 group->grab_focus();
211                 break;
212
213         case GDK_MOTION_NOTIFY:
214                 event_x = ev->motion.x;
215                 event_y = ev->motion.y;
216                 group->w2i(event_x, event_y);
217
218                 // convert event_x to global frame
219                 event_frame = trackview.editor.pixel_to_frame(event_x) + _region->position();
220                 trackview.editor.snap_to(event_frame);
221                 // convert event_frame back to local coordinates relative to position
222                 event_frame -= _region->position();
223
224                 switch (_mouse_state) {
225                 case Pressed: // Drag start
226
227                         // Select drag start
228                         if (_pressed_button == 1 && trackview.editor.current_midi_edit_mode() == MidiEditSelect) {
229                                 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
230                                                 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
231                                 last_x = event_x;
232                                 last_y = event_y;
233                                 drag_start_x = event_x;
234                                 drag_start_y = event_y;
235
236                                 drag_rect = new ArdourCanvas::SimpleRect(*group);
237                                 drag_rect->property_x1() = event_x;
238                                 drag_rect->property_y1() = event_y;
239                                 drag_rect->property_x2() = event_x;
240                                 drag_rect->property_y2() = event_y;
241                                 drag_rect->property_outline_what() = 0xFF;
242                                 drag_rect->property_outline_color_rgba()
243                                         = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
244                                 drag_rect->property_fill_color_rgba()
245                                         = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
246
247                                 _mouse_state = SelectRectDragging;
248                                 return true;
249
250                         // Add note drag start
251                         } else if (trackview.editor.current_midi_edit_mode() == MidiEditPencil) {
252                                 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
253                                                 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
254                                 last_x = event_x;
255                                 last_y = event_y;
256                                 drag_start_x = event_x;
257                                 drag_start_y = event_y;
258
259                                 drag_rect = new ArdourCanvas::SimpleRect(*group);
260                                 drag_rect->property_x1() = trackview.editor.frame_to_pixel(event_frame);
261
262                                 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
263                                 drag_rect->property_x2() = event_x;
264                                 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
265                                 drag_rect->property_outline_what() = 0xFF;
266                                 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
267
268                                 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
269
270                                 _mouse_state = AddDragging;
271                                 return true;
272                         }
273
274                         return false;
275
276                 case SelectRectDragging: // Select drag motion
277                 case AddDragging: // Add note drag motion
278                         if (ev->motion.is_hint) {
279                                 int t_x;
280                                 int t_y;
281                                 GdkModifierType state;
282                                 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
283                                 event_x = t_x;
284                                 event_y = t_y;
285                         }
286
287                         if (_mouse_state == AddDragging)
288                                 event_x = trackview.editor.frame_to_pixel(event_frame);
289
290                         if (drag_rect) {
291                                 if (event_x > drag_start_x)
292                                         drag_rect->property_x2() = event_x;
293                                 else
294                                         drag_rect->property_x1() = event_x;
295                         }
296
297                         if (drag_rect && _mouse_state == SelectRectDragging) {
298                                 if (event_y > drag_start_y)
299                                         drag_rect->property_y2() = event_y;
300                                 else
301                                         drag_rect->property_y1() = event_y;
302
303                                 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
304                         }
305
306                         last_x = event_x;
307                         last_y = event_y;
308
309                 case EraseTouchDragging:
310                 case SelectTouchDragging:
311                         return false;
312
313                 default:
314                         break;
315                 }
316                 break;
317
318         case GDK_BUTTON_RELEASE:
319                 event_x = ev->motion.x;
320                 event_y = ev->motion.y;
321                 group->w2i(event_x, event_y);
322                 group->ungrab(ev->button.time);
323                 event_frame = trackview.editor.pixel_to_frame(event_x);
324
325                 if (_pressed_button != 1) {
326                         return false;
327                 }
328                         
329                 switch (_mouse_state) {
330                 case Pressed: // Clicked
331                         switch (trackview.editor.current_midi_edit_mode()) {
332                         case MidiEditSelect:
333                         case MidiEditResize:
334                                 clear_selection();
335                                 break;
336                         case MidiEditPencil:
337                                 create_note_at(event_x, event_y, _default_note_length);
338                         default: break;
339                         }
340                         _mouse_state = None;
341                         break;
342                 case SelectRectDragging: // Select drag done
343                         _mouse_state = None;
344                         delete drag_rect;
345                         drag_rect = NULL;
346                         break;
347                 case AddDragging: // Add drag done
348                         _mouse_state = None;
349                         if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
350                                 const double x      = drag_rect->property_x1();
351                                 const double length = trackview.editor.pixel_to_frame(
352                                                         drag_rect->property_x2() - drag_rect->property_x1());
353                                         
354                                 create_note_at(x, drag_rect->property_y1(), length);
355                         }
356
357                         delete drag_rect;
358                         drag_rect = NULL;
359                 default: break;
360                 }
361
362         default: break;
363         }
364
365         return false;
366 }
367
368
369 /** Add a note to the model, and the view, at a canvas (click) coordinate */
370 void
371 MidiRegionView::create_note_at(double x, double y, double length)
372 {
373         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
374         MidiStreamView* const view = mtv->midi_view();
375
376         double note = midi_stream_view()->y_to_note(y);
377
378         assert(note >= 0.0);
379         assert(note <= 127.0);
380
381         nframes64_t new_note_time = trackview.editor.pixel_to_frame (x);
382         assert(new_note_time >= 0);
383         new_note_time += _region->start();
384
385         /*
386         const Meter& m = trackview.session().tempo_map().meter_at(new_note_time);
387         const Tempo& t = trackview.session().tempo_map().tempo_at(new_note_time);
388         double length = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
389         */
390         
391         // we need to snap here again in nframes64_t in order to be sample accurate 
392         // since note time is region-absolute but snap_to_frame expects position-relative
393         // time we have to coordinate transform back and forth here.
394         nframes64_t new_note_time_position_relative = new_note_time      - _region->start(); 
395         new_note_time = snap_to_frame(new_note_time_position_relative) + _region->start();
396         
397         // we need to snap the length too to be sample accurate
398         nframes64_t new_note_length = nframes_t(length);
399         new_note_length = snap_to_frame(new_note_time_position_relative + new_note_length) + _region->start() 
400                             - new_note_time;
401
402         const boost::shared_ptr<Evoral::Note> new_note(new Evoral::Note(
403                         0, new_note_time, new_note_length, (uint8_t)note, 0x40));
404         view->update_note_range(new_note->note());
405
406         MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
407         cmd->add(new_note);
408         _model->apply_command(trackview.session(), cmd);
409 }
410
411
412 void
413 MidiRegionView::clear_events()
414 {
415         clear_selection();
416
417         MidiGhostRegion* gr;
418         for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
419                 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
420                         gr->clear_events();
421                 }
422         }
423
424         for (Events::iterator i = _events.begin(); i != _events.end(); ++i)
425                 delete *i;
426
427         _events.clear();
428         _pgm_changes.clear();
429 }
430
431
432 void
433 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
434 {
435         _model = model;
436
437         if (_enable_display)
438                 redisplay_model();
439 }
440         
441         
442 void
443 MidiRegionView::start_delta_command(string name)
444 {
445         if (!_delta_command)
446                 _delta_command = _model->new_delta_command(name);
447 }
448
449 void
450 MidiRegionView::command_add_note(const boost::shared_ptr<Evoral::Note> note, bool selected)
451 {
452         if (_delta_command)
453                 _delta_command->add(note);
454
455         if (selected)
456                 _marked_for_selection.insert(note);
457 }
458
459 void
460 MidiRegionView::command_remove_note(ArdourCanvas::CanvasNoteEvent* ev)
461 {
462         if (_delta_command && ev->note()) {
463                 _delta_command->remove(ev->note());
464         }
465 }
466         
467 void
468 MidiRegionView::apply_command()
469 {
470         if (!_delta_command) {
471                 return;
472         }
473
474         // Mark all selected notes for selection when model reloads
475         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
476                 _marked_for_selection.insert((*i)->note());
477         }
478         
479         _model->apply_command(trackview.session(), _delta_command);
480         _delta_command = NULL; 
481         midi_view()->midi_track()->diskstream()->playlist_modified();
482
483         _marked_for_selection.clear();
484 }
485         
486
487 void
488 MidiRegionView::abort_command()
489 {
490         delete _delta_command;
491         _delta_command = NULL;
492         clear_selection();
493 }
494
495
496 void
497 MidiRegionView::redisplay_model()
498 {
499         // Don't redisplay the model if we're currently recording and displaying that
500         if (_active_notes)
501                 return;
502
503         if (_model) {
504
505                 clear_events();
506                 _model->read_lock();
507                 
508                 /*MidiModel::Notes notes = _model->notes();
509                 cerr << endl << _model->midi_source()->name() << " : redisplaying " << notes.size() << " notes:" << endl;
510                 for (MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
511                         cerr << "NOTE  time: " << (*i)->time()
512                                  << "  pitch: " << int((*i)->note()) 
513                              << "  length: " << (*i)->length() 
514                              << "  end-time: " << (*i)->end_time() 
515                              << "  velocity: " << int((*i)->velocity()) 
516                              << endl;
517                 }*/
518                 
519                 for (size_t i = 0; i < _model->n_notes(); ++i)
520                         add_note(_model->note_at(i));
521
522                 // Draw program change 'flags'
523                 for (Automatable::Controls::iterator control = _model->controls().begin();
524                                 control != _model->controls().end(); ++control) {
525                         if (control->first.type() == MidiPgmChangeAutomation) {
526                                 Glib::Mutex::Lock list_lock (control->second->list()->lock());
527                                 
528                                 for (AutomationList::const_iterator event = control->second->list()->begin();
529                                                 event != control->second->list()->end(); ++event) {
530                                         Evoral::ControlIterator iter(control->second->list(), (*event)->when, (*event)->value);
531                                         boost::shared_ptr<Evoral::Event> event(new Evoral::Event());
532                                         _model->control_to_midi_event(event, iter);
533                                         add_pgm_change(event);
534                                 }
535                                 break;
536                         }
537                 }
538
539                 // Is this necessary?
540                 /*for (Automatable::Controls::const_iterator i = _model->controls().begin();
541                                 i != _model->controls().end(); ++i) {
542
543                         assert(i->second);
544
545                         boost::shared_ptr<AutomationTimeAxisView> at
546                                 = midi_view()->automation_child(i->second->parameter());
547                         if (!at)
548                                 continue;
549
550                         Gdk::Color col = midi_stream_view()->get_region_color();
551
552                         boost::shared_ptr<AutomationRegionView> arv;
553
554                         {
555                                 Glib::Mutex::Lock list_lock (i->second->list()->lock());
556
557                                 arv = boost::shared_ptr<AutomationRegionView>(
558                                                 new AutomationRegionView(at->canvas_display,
559                                                         *at.get(), _region, i->second->list(),
560                                                         midi_stream_view()->get_samples_per_unit(), col));
561                         }
562
563                         arv->set_duration(_region->length(), this);
564                         arv->init(col, true);
565
566                         _automation_children.insert(std::make_pair(i->second->parameter(), arv));
567                 }*/
568
569                 _model->read_unlock();
570
571         } else {
572                 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
573         }
574 }
575
576
577 MidiRegionView::~MidiRegionView ()
578 {
579         in_destructor = true;
580
581         RegionViewGoingAway (this); /* EMIT_SIGNAL */
582
583         if (_active_notes) {
584                 end_write();
585         }
586
587         _selection.clear();
588         clear_events();
589         delete _note_group;
590         delete _delta_command;
591 }
592
593
594 void
595 MidiRegionView::region_resized (Change what_changed)
596 {
597         RegionView::region_resized(what_changed);
598         
599         if (what_changed & ARDOUR::PositionChanged) {
600                 if (_enable_display)
601                         redisplay_model();
602         } 
603 }
604
605 void
606 MidiRegionView::reset_width_dependent_items (double pixel_width)
607 {
608         RegionView::reset_width_dependent_items(pixel_width);
609         assert(_pixel_width == pixel_width);
610
611         if (_enable_display)
612                 redisplay_model();
613 }
614
615 void
616 MidiRegionView::set_height (gdouble height)
617 {
618         static const double FUDGE = 2;
619         const double old_height = _height;
620         RegionView::set_height(height);
621         _height = height - FUDGE;
622         
623         apply_note_range(midi_stream_view()->lowest_note(),
624                          midi_stream_view()->highest_note(),
625                          height != old_height + FUDGE);
626         
627         if (name_text) {
628                 name_text->raise_to_top();
629         }
630 }
631
632
633 /** Apply the current note range from the stream view
634  * by repositioning/hiding notes as necessary
635  */
636 void
637 MidiRegionView::apply_note_range (uint8_t min, uint8_t max, bool force)
638 {
639         if (_enable_display) {
640                 if (!force && _current_range_min == min && _current_range_max == max) {
641                         return;
642                 }
643                 
644                 _current_range_min = min;
645                 _current_range_max = max;
646
647                 for (Events::const_iterator i = _events.begin(); i != _events.end(); ++i) {
648                         CanvasNoteEvent* event = *i;
649                         Item* item = dynamic_cast<Item*>(event);
650                         assert(item);
651                         if (event && event->note()) {
652                                 if (event->note()->note() < _current_range_min || event->note()->note() > _current_range_max) {
653                                         if (canvas_item_visible(item)) {
654                                                 item->hide();
655                                         }
656                                 } else {
657                                         if (!canvas_item_visible(item)) {
658                                                 item->show();
659                                         }
660
661                                         event->hide_velocity();
662                                         if (CanvasNote* note = dynamic_cast<CanvasNote*>(event)) {
663                                                 const double y1 = midi_stream_view()->note_to_y(event->note()->note());
664                                                 const double y2 = y1 + floor(midi_stream_view()->note_height());
665
666                                                 note->property_y1() = y1;
667                                                 note->property_y2() = y2;
668                                         } else if (CanvasHit* hit = dynamic_cast<CanvasHit*>(event)) {
669                                                 double x = trackview.editor.frame_to_pixel((nframes64_t)
670                                                                 event->note()->time() - _region->start());
671                                                 const double diamond_size = midi_stream_view()->note_height() / 2.0;
672                                                 double y = midi_stream_view()->note_to_y(event->note()->note()) 
673                                                                  + ((diamond_size-2.0) / 4.0);
674                                                 
675                                                 hit->set_height(diamond_size);
676                                                 hit->move(x-hit->x1(), y-hit->y1());
677                                                 hit->show();
678                                         }
679                                         if (event->selected()) {
680                                                 event->show_velocity();
681                                         }
682                                 }
683                         }
684                 }
685
686         }
687 }
688
689 GhostRegion*
690 MidiRegionView::add_ghost (TimeAxisView& tv)
691 {
692         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
693         CanvasNote* note;
694         assert(rtv);
695
696         double unit_position = _region->position () / samples_per_unit;
697         MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
698         MidiGhostRegion* ghost;
699
700         if (mtv && mtv->midi_view()) {
701                 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group.
702                    this is because it's nice to have midi notes on top of the note lines and
703                    audio waveforms under it.
704                  */
705                 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
706         }
707         else {
708                 ghost = new MidiGhostRegion (tv, trackview, unit_position);
709         }
710
711         ghost->set_height ();
712         ghost->set_duration (_region->length() / samples_per_unit);
713         ghosts.push_back (ghost);
714
715         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
716                 if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
717                         ghost->add_note(note);
718                 }
719         }
720
721         ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
722
723         return ghost;
724 }
725
726
727 /** Begin tracking note state for successive calls to add_event
728  */
729 void
730 MidiRegionView::begin_write()
731 {
732         assert(!_active_notes);
733         _active_notes = new CanvasNote*[128];
734         for (unsigned i=0; i < 128; ++i) {
735                 _active_notes[i] = NULL;
736         }
737 }
738
739
740 /** Destroy note state for add_event
741  */
742 void
743 MidiRegionView::end_write()
744 {
745         delete[] _active_notes;
746         _active_notes = NULL;
747         _marked_for_selection.clear();
748 }
749
750
751 /** Resolve an active MIDI note (while recording).
752  */
753 void
754 MidiRegionView::resolve_note(uint8_t note, double end_time)
755 {
756         if (midi_view()->note_mode() != Sustained)
757                 return;
758
759         if (_active_notes && _active_notes[note]) {
760                 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes64_t)end_time);
761                 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
762                 _active_notes[note] = NULL;
763         }
764 }
765
766
767 /** Extend active notes to rightmost edge of region (if length is changed)
768  */
769 void
770 MidiRegionView::extend_active_notes()
771 {
772         if (!_active_notes) {
773                 return;
774         }
775
776         for (unsigned i=0; i < 128; ++i) {
777                 if (_active_notes[i]) {
778                         _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
779                 }
780         }
781 }
782
783
784 /** Add a MIDI note to the view (with length).
785  *
786  * If in sustained mode, notes with length 0 will be considered active
787  * notes, and resolve_note should be called when the corresponding note off
788  * event arrives, to properly display the note.
789  */
790 void
791 MidiRegionView::add_note(const boost::shared_ptr<Evoral::Note> note)
792 {
793         assert(note->time() >= 0);
794         assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
795         
796         // dont display notes beyond the region bounds
797         if ( note->time() - _region->start() >= _region->length() ||
798                 note->time() <  _region->start() ||
799                 note->note() < midi_stream_view()->lowest_note() ||
800                 note->note() > midi_stream_view()->highest_note() ) {
801                 return;
802         }
803         
804         ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
805
806         CanvasNoteEvent* event = 0;
807         
808         const double x = trackview.editor.frame_to_pixel((nframes64_t)note->time() - _region->start());
809         
810         if (midi_view()->note_mode() == Sustained) {
811
812                 const double y1 = midi_stream_view()->note_to_y(note->note());
813                 const double note_endpixel = 
814                         trackview.editor.frame_to_pixel((nframes64_t)note->end_time() - _region->start());
815                 
816                 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
817                 ev_rect->property_x1() = x;
818                 ev_rect->property_y1() = y1;
819                 if (note->length() > 0)
820                         ev_rect->property_x2() = note_endpixel;
821                 else
822                         ev_rect->property_x2() = trackview.editor.frame_to_pixel(_region->length());
823                 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
824
825                 if (note->length() == 0) {
826
827                         if (_active_notes) {
828                                 assert(note->note() < 128);
829                                 // If this note is already active there's a stuck note,
830                                 // finish the old note rectangle
831                                 if (_active_notes[note->note()]) {
832                                         CanvasNote* const old_rect = _active_notes[note->note()];
833                                         boost::shared_ptr<Evoral::Note> old_note = old_rect->note();
834                                         cerr << "MidiModel: WARNING: Note has length 0: chan " << old_note->channel()
835                                                 << "note " << (int)old_note->note() << " @ " << old_note->time() << endl;
836                                         /* FIXME: How large to make it?  Make it a diamond? */
837                                         old_rect->property_x2() = old_rect->property_x1() + 2.0;
838                                         old_rect->property_outline_what() = (guint32) 0xF;
839                                 }
840                                 _active_notes[note->note()] = ev_rect;
841                         }
842                         /* outline all but right edge */
843                         ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
844                 } else {
845                         /* outline all edges */
846                         ev_rect->property_outline_what() = (guint32) 0xF;
847                 }
848
849                 ev_rect->show();
850                 _events.push_back(ev_rect);
851                 event = ev_rect;
852
853                 MidiGhostRegion* gr;
854
855                 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
856                         if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
857                                 gr->add_note(ev_rect);
858                         }
859                 }
860
861         } else if (midi_view()->note_mode() == Percussive) {
862
863                 //cerr << "MRV::add_note percussive " << note->note() << " @ " << note->time()
864                 //      << " .. " << note->end_time() << endl;
865
866                 const double diamond_size = midi_stream_view()->note_height() / 2.0;
867                 const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
868
869                 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size, note);
870                 ev_diamond->move(x, y);
871                 ev_diamond->show();
872                 _events.push_back(ev_diamond);
873                 event = ev_diamond;
874         } else {
875                 event = 0;
876         }
877
878         if (event) {                    
879                 if (_marked_for_selection.find(note) != _marked_for_selection.end()) {
880                         note_selected(event, true);
881                 }
882                 event->on_channel_selection_change(_last_channel_selection);
883         }
884 }
885
886 void
887 MidiRegionView::add_pgm_change(boost::shared_ptr<Evoral::Event> event)
888 {
889         assert(event->time() >= 0);
890         
891         // dont display notes beyond the region bounds
892         if (event->time() - _region->start() >= _region->length() || event->time() <  _region->start()) 
893                 return;
894         
895         ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
896         const double x = trackview.editor.frame_to_pixel((nframes64_t)event->time() - _region->start());
897         
898         double height = midi_stream_view()->contents_height();
899         _pgm_changes.push_back(
900                 boost::shared_ptr<CanvasProgramChange>(
901                         new CanvasProgramChange(*this, *group, event, height, x, 1.0)));
902 }
903
904 void
905 MidiRegionView::delete_selection()
906 {
907         assert(_delta_command);
908
909         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
910                 if ((*i)->selected()) {
911                         _delta_command->remove((*i)->note());
912                 }
913         }
914
915         _selection.clear();
916 }
917
918 void
919 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev)
920 {
921         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
922                 if ((*i)->selected() && (*i) != ev) {
923                         (*i)->selected(false);
924                 }
925         }
926
927         _selection.clear();
928 }
929
930 void
931 MidiRegionView::unique_select(ArdourCanvas::CanvasNoteEvent* ev)
932 {
933         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
934                 if ((*i) != ev) {
935                         (*i)->selected(false);
936                 }
937         }
938
939         _selection.clear();
940         _selection.insert(ev);
941
942         if ( ! ev->selected()) {
943                 ev->selected(true);
944         }
945 }
946
947 void
948 MidiRegionView::note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
949 {
950         if ( ! add) {
951                 clear_selection_except(ev);
952         }
953
954         _selection.insert(ev);
955
956         if ( ! ev->selected()) {
957                 ev->selected(true);
958         }
959 }
960
961
962 void
963 MidiRegionView::note_deselected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
964 {
965         if ( ! add) {
966                 clear_selection_except(ev);
967         }
968
969         _selection.erase(ev);
970
971         if (ev->selected()) {
972                 ev->selected(false);
973         }
974 }
975
976
977 void
978 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
979 {
980         const double last_y = std::min(y1, y2);
981         const double y      = std::max(y1, y2);
982
983         // TODO: Make this faster by storing the last updated selection rect, and only
984         // adjusting things that are in the area that appears/disappeared.
985         // We probably need a tree to be able to find events in O(log(n)) time.
986
987 #ifndef NDEBUG
988         double last_x1 = 0.0;
989 #endif
990
991         if (x1 < x2) {
992                 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
993 #ifndef NDEBUG
994                         // Events should always be sorted by increasing x1() here
995                         assert((*i)->x1() >= last_x1);
996                         last_x1 = (*i)->x1();
997 #endif
998                         // Inside rectangle
999                         if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1000                                 if (!(*i)->selected()) {
1001                                         (*i)->selected(true);
1002                                         _selection.insert(*i);
1003                                 }
1004                         // Not inside rectangle
1005                         } else if ((*i)->selected()) {
1006                                 (*i)->selected(false);
1007                                 _selection.erase(*i);
1008                         }
1009                 }
1010         } else {
1011                 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1012 #ifndef NDEBUG
1013                         // Events should always be sorted by increasing x1() here
1014                         assert((*i)->x1() >= last_x1);
1015                         last_x1 = (*i)->x1();
1016 #endif
1017                         // Inside rectangle
1018                         if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1019                                 if (!(*i)->selected()) {
1020                                         (*i)->selected(true);
1021                                         _selection.insert(*i);
1022                                 }
1023                         // Not inside rectangle
1024                         } else if ((*i)->selected()) {
1025                                 (*i)->selected(false);
1026                                 _selection.erase(*i);
1027                         }
1028                 }
1029         }
1030 }
1031
1032
1033 void
1034 MidiRegionView::move_selection(double dx, double dy)
1035 {
1036         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
1037                 (*i)->move_event(dx, dy);
1038 }
1039
1040
1041 void
1042 MidiRegionView::note_dropped(CanvasNoteEvent* ev, double dt, uint8_t dnote)
1043 {
1044         // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
1045         if (_selection.find(ev) != _selection.end()) {
1046                 uint8_t lowest_note_in_selection  = midi_stream_view()->lowest_note();
1047                 uint8_t highest_note_in_selection = midi_stream_view()->highest_note();
1048                 uint8_t highest_note_difference = 0;
1049
1050                 // find highest and lowest notes first
1051                 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1052                         uint8_t pitch = (*i)->note()->note();
1053                         lowest_note_in_selection  = std::min(lowest_note_in_selection,  pitch);
1054                         highest_note_in_selection = std::max(highest_note_in_selection, pitch);
1055                 }
1056                 
1057                 /*
1058                 cerr << "dnote: " << (int) dnote << endl;
1059                 cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note()) 
1060                      << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
1061                 cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): " 
1062                      << int(highest_note_in_selection) << endl;
1063                 cerr << "selection size: " << _selection.size() << endl;
1064                 cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
1065                 */
1066                 
1067                 // Make sure the note pitch does not exceed the MIDI standard range
1068                 if (dnote <= 127 && (highest_note_in_selection + dnote > 127)) {
1069                         highest_note_difference = highest_note_in_selection - 127;
1070                 }
1071                 
1072                 start_delta_command(_("move notes"));
1073
1074                 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
1075                         Selection::iterator next = i;
1076                         ++next;
1077
1078                         const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(*i)->note().get()));
1079
1080                         // we need to snap here again in nframes64_t in order to be sample accurate 
1081                         double new_note_time = (*i)->note()->time();
1082                         new_note_time +=  dt;
1083
1084                         // keep notes inside region if dragged beyond left region bound
1085                         if (new_note_time < _region->start()) {                         
1086                                 new_note_time = _region->start();
1087                         }
1088                         
1089                         // since note time is region-absolute but snap_to_frame expects position-relative
1090                         // time we have to coordinate transform back and forth here.
1091                         new_note_time = snap_to_frame(nframes64_t(new_note_time) - _region->start()) + _region->start();
1092                         
1093                         copy->set_time(new_note_time);
1094
1095                         uint8_t original_pitch = (*i)->note()->note();
1096                         uint8_t new_pitch =  original_pitch + dnote - highest_note_difference;
1097                         
1098                         // keep notes in standard midi range
1099                         clamp_0_to_127(new_pitch);
1100                         
1101                         //notes which are dragged beyond the standard midi range snap back to their original place
1102                         if ((original_pitch != 0 && new_pitch == 0) || (original_pitch != 127 && new_pitch == 127)) {
1103                                 new_pitch = original_pitch;
1104                         }
1105
1106                         lowest_note_in_selection  = std::min(lowest_note_in_selection,  new_pitch);
1107                         highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
1108
1109                         copy->set_note(new_pitch);
1110                         
1111                         command_remove_note(*i);
1112                         command_add_note(copy, true);
1113
1114                         i = next;
1115                 }
1116
1117                 apply_command();
1118                 
1119                 // care about notes being moved beyond the upper/lower bounds on the canvas
1120                 if (lowest_note_in_selection  < midi_stream_view()->lowest_note() ||
1121                                 highest_note_in_selection > midi_stream_view()->highest_note()) {
1122                         midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
1123                 }
1124         }
1125 }
1126
1127 nframes64_t
1128 MidiRegionView::snap_to_frame(double x)
1129 {
1130         PublicEditor &editor = trackview.editor;
1131         // x is region relative
1132         // convert x to global frame
1133         nframes64_t frame = editor.pixel_to_frame(x) + _region->position();
1134         editor.snap_to(frame);
1135         // convert event_frame back to local coordinates relative to position
1136         frame -= _region->position();
1137         return frame;
1138 }
1139
1140 nframes64_t
1141 MidiRegionView::snap_to_frame(nframes64_t x)
1142 {
1143         PublicEditor &editor = trackview.editor;
1144         // x is region relative
1145         // convert x to global frame
1146         nframes64_t frame = x + _region->position();
1147         editor.snap_to(frame);
1148         // convert event_frame back to local coordinates relative to position
1149         frame -= _region->position();
1150         return frame;
1151 }
1152
1153 double
1154 MidiRegionView::snap_to_pixel(double x)
1155 {
1156         return (double) trackview.editor.frame_to_pixel(snap_to_frame(x));
1157 }
1158
1159 double
1160 MidiRegionView::get_position_pixels(void)
1161 {
1162         nframes64_t  region_frame  = get_position();
1163         return trackview.editor.frame_to_pixel(region_frame);
1164 }
1165
1166 void
1167 MidiRegionView::begin_resizing(CanvasNote::NoteEnd note_end)
1168 {
1169         _resize_data.clear();
1170
1171         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1172                 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
1173
1174                 // only insert CanvasNotes into the map
1175                 if (note) {
1176                         NoteResizeData *resize_data = new NoteResizeData();
1177                         resize_data->canvas_note = note;
1178
1179                         // create a new SimpleRect from the note which will be the resize preview
1180                         SimpleRect *resize_rect =
1181                                 new SimpleRect(
1182                                                 *group,
1183                                                 note->x1(),
1184                                                 note->y1(),
1185                                                 note->x2(),
1186                                                 note->y2());
1187
1188                         // calculate the colors: get the color settings
1189                         uint32_t fill_color =
1190                                 UINT_RGBA_CHANGE_A(
1191                                                 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get(),
1192                                                 128);
1193
1194                         // make the resize preview notes more transparent and bright
1195                         fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
1196
1197                         // calculate color based on note velocity
1198                         resize_rect->property_fill_color_rgba() =
1199                                 UINT_INTERPOLATE(
1200                                         note_fill_color(note->note()->velocity()),
1201                                         fill_color,
1202                                         0.85);
1203
1204                         resize_rect->property_outline_color_rgba() =
1205                                 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get();
1206
1207                         resize_data->resize_rect = resize_rect;
1208
1209                         if (note_end == CanvasNote::NOTE_ON) {
1210                                 resize_data->current_x = note->x1();
1211                         } else { // NOTE_OFF
1212                                 resize_data->current_x = note->x2();
1213                         }
1214
1215                         _resize_data.push_back(resize_data);
1216                 }
1217         }
1218 }
1219
1220 void
1221 MidiRegionView::update_resizing(CanvasNote::NoteEnd note_end, double x, bool relative)
1222 {
1223         for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1224                 SimpleRect     *resize_rect = (*i)->resize_rect;
1225                 CanvasNote     *canvas_note = (*i)->canvas_note;
1226
1227                 const double region_start = get_position_pixels();
1228
1229                 if (relative) {
1230                         (*i)->current_x = (*i)->current_x + x;
1231                 } else {
1232                         // x is in track relative, transform it to region relative
1233                         (*i)->current_x = x - region_start;
1234                 }
1235
1236                 double current_x = (*i)->current_x;
1237
1238                 if (note_end == CanvasNote::NOTE_ON) {
1239                         resize_rect->property_x1() = snap_to_pixel(current_x);
1240                         resize_rect->property_x2() = canvas_note->x2();
1241                 } else {
1242                         resize_rect->property_x2() = snap_to_pixel(current_x);
1243                         resize_rect->property_x1() = canvas_note->x1();
1244                 }
1245         }
1246 }
1247
1248 void
1249 MidiRegionView::commit_resizing(CanvasNote::NoteEnd note_end, double event_x, bool relative)
1250 {
1251         start_delta_command(_("resize notes"));
1252
1253         for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1254                 CanvasNote*  canvas_note = (*i)->canvas_note;
1255                 SimpleRect*  resize_rect = (*i)->resize_rect;
1256                 double       current_x   = (*i)->current_x;
1257                 const double position    = get_position_pixels();
1258
1259                 if (!relative) {
1260                         // event_x is in track relative, transform it to region relative
1261                         current_x = event_x - position;
1262                 }
1263
1264                 // because snapping works on world coordinates we have to transform current_x
1265                 // to world coordinates before snapping and transform it back afterwards
1266                 nframes64_t current_frame = snap_to_frame(current_x);
1267                 // transform to region start relative
1268                 current_frame += _region->start();
1269                 
1270                 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(canvas_note->note().get())));
1271
1272                 // resize beginning of note
1273                 if (note_end == CanvasNote::NOTE_ON && current_frame < copy->end_time()) {
1274                         command_remove_note(canvas_note);
1275                         copy->on_event().time() = current_frame;
1276                         command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1277                 }
1278                 // resize end of note
1279                 if (note_end == CanvasNote::NOTE_OFF && current_frame > copy->time()) {
1280                         command_remove_note(canvas_note);
1281                         copy->off_event().time() = current_frame;
1282                         command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1283                 }
1284
1285                 delete resize_rect;
1286                 delete (*i);
1287         }
1288
1289         _resize_data.clear();
1290         apply_command();
1291 }
1292
1293
1294 void
1295 MidiRegionView::change_velocity(uint8_t velocity, bool relative)
1296 {
1297         start_delta_command(_("change velocity"));
1298         for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1299                 Selection::iterator next = i;
1300                 ++next;
1301
1302                 CanvasNoteEvent *event = *i;
1303                 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(event->note().get())));
1304
1305                 if (relative) {
1306                         uint8_t new_velocity = copy->velocity() + velocity;
1307                         clamp_0_to_127(new_velocity);
1308                                 
1309                         copy->set_velocity(new_velocity);
1310                 } else { // absolute
1311                         copy->set_velocity(velocity);                   
1312                 }
1313                 
1314                 command_remove_note(event);
1315                 command_add_note(copy, true);
1316                 
1317                 i = next;
1318         }
1319         
1320         apply_command();
1321 }
1322
1323 void
1324 MidiRegionView::change_channel(uint8_t channel)
1325 {
1326         start_delta_command(_("change channel"));
1327         for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1328                 Selection::iterator next = i;
1329                 ++next;
1330
1331                 CanvasNoteEvent *event = *i;
1332                 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(event->note().get())));
1333
1334                 copy->set_channel(channel);
1335                 
1336                 command_remove_note(event);
1337                 command_add_note(copy, true);
1338                 
1339                 i = next;
1340         }
1341         
1342         apply_command();
1343 }
1344
1345
1346 void
1347 MidiRegionView::note_entered(ArdourCanvas::CanvasNoteEvent* ev)
1348 {
1349         if (ev->note() && _mouse_state == EraseTouchDragging) {
1350                 start_delta_command(_("note entered"));
1351                 ev->selected(true);
1352                 _delta_command->remove(ev->note());
1353         } else if (_mouse_state == SelectTouchDragging) {
1354                 note_selected(ev, true);
1355         }
1356 }
1357
1358
1359 void
1360 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
1361 {
1362         boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
1363         if (msrc)
1364                 display_model(msrc->model());
1365 }
1366
1367 void
1368 MidiRegionView::set_frame_color()
1369 {
1370         if (frame) {
1371                 if (_selected && should_show_selection) {
1372                         frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
1373                 } else {
1374                         frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
1375                 }
1376         }
1377 }
1378
1379 void 
1380 MidiRegionView::midi_channel_mode_changed(ChannelMode mode, uint16_t mask)
1381 {
1382         switch (mode) {
1383         case AllChannels:
1384         case FilterChannels:
1385                 _force_channel = -1;
1386                 break;
1387         case ForceChannel:
1388                 _force_channel = mask;
1389                 mask = 0xFFFF; // Show all notes as active (below)
1390         };
1391
1392         // Update notes for selection
1393         for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1394                 (*i)->on_channel_selection_change(mask);
1395         }
1396
1397         _last_channel_selection = mask;
1398 }
1399