globally change all use of "frame" to refer to audio into "sample".
[ardour.git] / gtk2_ardour / mixer_ui.cc
1 /*
2     Copyright (C) 2000-2004 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <algorithm>
25 #include <map>
26 #include <sigc++/bind.h>
27
28 #include <boost/foreach.hpp>
29
30 #include <glibmm/threads.h>
31
32 #include <gtkmm/accelmap.h>
33 #include <gtkmm/stock.h>
34
35 #include "pbd/convert.h"
36 #include "pbd/stacktrace.h"
37 #include "pbd/unwind.h"
38
39 #include "ardour/amp.h"
40 #include "ardour/debug.h"
41 #include "ardour/audio_track.h"
42 #include "ardour/midi_track.h"
43 #include "ardour/plugin_manager.h"
44 #include "ardour/route_group.h"
45 #include "ardour/selection.h"
46 #include "ardour/session.h"
47 #include "ardour/vca.h"
48 #include "ardour/vca_manager.h"
49
50 #include "gtkmm2ext/gtk_ui.h"
51 #include "gtkmm2ext/keyboard.h"
52 #include "gtkmm2ext/utils.h"
53 #include "gtkmm2ext/window_title.h"
54 #include "gtkmm2ext/doi.h"
55
56 #include "widgets/tearoff.h"
57
58 #include "keyboard.h"
59 #include "mixer_ui.h"
60 #include "mixer_strip.h"
61 #include "monitor_section.h"
62 #include "plugin_selector.h"
63 #include "public_editor.h"
64 #include "mouse_cursors.h"
65 #include "ardour_ui.h"
66 #include "utils.h"
67 #include "route_sorter.h"
68 #include "actions.h"
69 #include "gui_thread.h"
70 #include "mixer_group_tabs.h"
71 #include "route_sorter.h"
72 #include "timers.h"
73 #include "ui_config.h"
74 #include "vca_master_strip.h"
75
76 #include "pbd/i18n.h"
77
78 using namespace ARDOUR;
79 using namespace ARDOUR_UI_UTILS;
80 using namespace PBD;
81 using namespace Gtk;
82 using namespace Glib;
83 using namespace Gtkmm2ext;
84 using namespace std;
85
86 using PBD::atoi;
87 using PBD::Unwinder;
88
89 Mixer_UI* Mixer_UI::_instance = 0;
90
91 Mixer_UI*
92 Mixer_UI::instance ()
93 {
94         if (!_instance) {
95                 _instance  = new Mixer_UI;
96         }
97
98         return _instance;
99 }
100
101 Mixer_UI::Mixer_UI ()
102         : Tabbable (_content, _("Mixer"))
103         , no_track_list_redisplay (false)
104         , in_group_row_change (false)
105         , track_menu (0)
106         , _monitor_section (0)
107         , _plugin_selector (0)
108         , _strip_width (UIConfiguration::instance().get_default_narrow_ms() ? Narrow : Wide)
109         , ignore_reorder (false)
110         , _in_group_rebuild_or_clear (false)
111         , _route_deletion_in_progress (false)
112         , _maximised (false)
113         , _show_mixer_list (true)
114         , myactions (X_("mixer"))
115         , _selection (*this, *this)
116 {
117         register_actions ();
118         load_bindings ();
119         _content.set_data ("ardour-bindings", bindings);
120
121         PresentationInfo::Change.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::presentation_info_changed, this, _1), gui_context());
122
123         scroller.set_can_default (true);
124         // set_default (scroller);
125
126         scroller_base.set_flags (Gtk::CAN_FOCUS);
127         scroller_base.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
128         scroller_base.set_name ("MixerWindow");
129         scroller_base.signal_button_release_event().connect (sigc::mem_fun(*this, &Mixer_UI::strip_scroller_button_release));
130
131         /* set up drag-n-drop */
132         vector<TargetEntry> target_table;
133         target_table.push_back (TargetEntry ("PluginFavoritePtr"));
134         scroller_base.drag_dest_set (target_table);
135         scroller_base.signal_drag_data_received().connect (sigc::mem_fun(*this, &Mixer_UI::scroller_drag_data_received));
136
137         // add as last item of strip packer
138         strip_packer.pack_end (scroller_base, true, true);
139
140         _group_tabs = new MixerGroupTabs (this);
141         VBox* b = manage (new VBox);
142         b->set_spacing (0);
143         b->set_border_width (0);
144         b->pack_start (*_group_tabs, PACK_SHRINK);
145         b->pack_start (strip_packer);
146         b->show_all ();
147         b->signal_scroll_event().connect (sigc::mem_fun (*this, &Mixer_UI::on_scroll_event), false);
148
149         scroller.add (*b);
150         scroller.set_policy (Gtk::POLICY_ALWAYS, Gtk::POLICY_AUTOMATIC);
151
152         setup_track_display ();
153
154         group_model = ListStore::create (group_columns);
155         group_display.set_model (group_model);
156         group_display.append_column (_("Show"), group_columns.visible);
157         group_display.append_column (_("Group"), group_columns.text);
158         group_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
159         group_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
160         group_display.get_column (0)->set_expand(false);
161         group_display.get_column (1)->set_expand(true);
162         group_display.get_column (1)->set_sizing (Gtk::TREE_VIEW_COLUMN_FIXED);
163         group_display.set_name ("EditGroupList");
164         group_display.get_selection()->set_mode (Gtk::SELECTION_SINGLE);
165         group_display.set_reorderable (true);
166         group_display.set_headers_visible (true);
167         group_display.set_rules_hint (true);
168         group_display.set_can_focus(false);
169
170         /* name is directly editable */
171
172         CellRendererText* name_cell = dynamic_cast<CellRendererText*>(group_display.get_column_cell_renderer (1));
173         name_cell->property_editable() = true;
174         name_cell->signal_edited().connect (sigc::mem_fun (*this, &Mixer_UI::route_group_name_edit));
175
176         /* use checkbox for the active column */
177
178         CellRendererToggle* active_cell = dynamic_cast<CellRendererToggle*>(group_display.get_column_cell_renderer (0));
179         active_cell->property_activatable() = true;
180         active_cell->property_radio() = false;
181
182         group_model->signal_row_changed().connect (sigc::mem_fun (*this, &Mixer_UI::route_group_row_change));
183         /* We use this to notice drag-and-drop reorders of the group list */
184         group_model->signal_row_deleted().connect (sigc::mem_fun (*this, &Mixer_UI::route_group_row_deleted));
185         group_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::group_display_button_press), false);
186
187         group_display_scroller.add (group_display);
188         group_display_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
189
190         HBox* route_group_display_button_box = manage (new HBox());
191
192         Button* route_group_add_button = manage (new Button ());
193         Button* route_group_remove_button = manage (new Button ());
194
195         Widget* w;
196
197         w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
198         w->show();
199         route_group_add_button->add (*w);
200
201         w = manage (new Image (Stock::REMOVE, ICON_SIZE_BUTTON));
202         w->show();
203         route_group_remove_button->add (*w);
204
205         route_group_display_button_box->set_homogeneous (true);
206
207         route_group_add_button->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::new_route_group));
208         route_group_remove_button->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::remove_selected_route_group));
209
210         route_group_display_button_box->add (*route_group_add_button);
211         route_group_display_button_box->add (*route_group_remove_button);
212
213         group_display_vbox.pack_start (group_display_scroller, true, true);
214         group_display_vbox.pack_start (*route_group_display_button_box, false, false);
215
216         group_display_sample.set_name ("BaseFrame");
217         group_display_sample.set_shadow_type (Gtk::SHADOW_IN);
218         group_display_sample.add (group_display_vbox);
219
220
221         list<TargetEntry> target_list;
222         target_list.push_back (TargetEntry ("PluginPresetPtr"));
223
224         favorite_plugins_model = PluginTreeStore::create (favorite_plugins_columns);
225         favorite_plugins_display.set_model (favorite_plugins_model);
226         favorite_plugins_display.append_column (_("Favorite Plugins"), favorite_plugins_columns.name);
227         favorite_plugins_display.set_name ("EditGroupList");
228         favorite_plugins_display.get_selection()->set_mode (Gtk::SELECTION_SINGLE);
229         favorite_plugins_display.set_reorderable (false);
230         favorite_plugins_display.set_headers_visible (true);
231         favorite_plugins_display.set_rules_hint (true);
232         favorite_plugins_display.set_can_focus (false);
233         favorite_plugins_display.set_tooltip_column (0);
234         favorite_plugins_display.add_object_drag (favorite_plugins_columns.plugin.index(), "PluginFavoritePtr");
235         favorite_plugins_display.set_drag_column (favorite_plugins_columns.name.index());
236         favorite_plugins_display.add_drop_targets (target_list);
237         favorite_plugins_display.signal_row_activated().connect (sigc::mem_fun (*this, &Mixer_UI::plugin_row_activated));
238         favorite_plugins_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::plugin_row_button_press), false);
239         favorite_plugins_display.signal_drop.connect (sigc::mem_fun (*this, &Mixer_UI::plugin_drop));
240         favorite_plugins_display.signal_row_expanded().connect (sigc::mem_fun (*this, &Mixer_UI::save_favorite_ui_state));
241         favorite_plugins_display.signal_row_collapsed().connect (sigc::mem_fun (*this, &Mixer_UI::save_favorite_ui_state));
242         favorite_plugins_model->signal_row_has_child_toggled().connect (sigc::mem_fun (*this, &Mixer_UI::sync_treeview_favorite_ui_state));
243
244         favorite_plugins_scroller.add (favorite_plugins_display);
245         favorite_plugins_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
246
247         favorite_plugins_sample.set_name ("BaseFrame");
248         favorite_plugins_sample.set_shadow_type (Gtk::SHADOW_IN);
249         favorite_plugins_sample.add (favorite_plugins_scroller);
250
251         rhs_pane1.add (favorite_plugins_sample);
252         rhs_pane1.add (track_display_sample);
253
254         rhs_pane2.add (rhs_pane1);
255         rhs_pane2.add (group_display_sample);
256
257         list_vpacker.pack_start (rhs_pane2, true, true);
258
259         vca_label_bar.set_size_request (-1, 16 + 1); /* must match height in GroupTabs::set_size_request()  + 1 border px*/
260         vca_vpacker.pack_start (vca_label_bar, false, false);
261
262         vca_scroller_base.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
263         vca_scroller_base.set_name (X_("MixerWindow"));
264         vca_scroller_base.signal_button_release_event().connect (sigc::mem_fun(*this, &Mixer_UI::masters_scroller_button_release), false);
265         vca_hpacker.pack_end (vca_scroller_base, true, true);
266
267         vca_scroller.add (vca_hpacker);
268         vca_scroller.set_policy (Gtk::POLICY_ALWAYS, Gtk::POLICY_AUTOMATIC);
269         vca_scroller.signal_button_release_event().connect (sigc::mem_fun(*this, &Mixer_UI::strip_scroller_button_release));
270
271         vca_vpacker.pack_start (vca_scroller, true, true);
272
273         inner_pane.add (scroller);
274         inner_pane.add (vca_vpacker);
275
276         global_hpacker.pack_start (inner_pane, true, true);
277         global_hpacker.pack_start (out_packer, false, false);
278
279         list_hpane.set_check_divider_position (true);
280         list_hpane.add (list_vpacker);
281         list_hpane.add (global_hpacker);
282         list_hpane.set_child_minsize (list_vpacker, 1);
283
284         XMLNode const * settings = ARDOUR_UI::instance()->mixer_settings();
285         float fract;
286
287         if (!settings || !settings->get_property ("mixer-rhs-pane1-pos", fract) || fract > 1.0) {
288                 fract = 0.6f;
289         }
290         rhs_pane1.set_divider (0, fract);
291
292         if (!settings || !settings->get_property ("mixer-rhs-pane2-pos", fract) || fract > 1.0) {
293                 fract = 0.7f;
294         }
295         rhs_pane2.set_divider (0, fract);
296
297         if (!settings || !settings->get_property ("mixer-list-hpane-pos", fract) || fract > 1.0) {
298                 fract = 0.2f;
299         }
300         list_hpane.set_divider (0, fract);
301
302         if (!settings || !settings->get_property ("mixer-inner-pane-pos", fract) || fract > 1.0) {
303                 fract = 0.8f;
304         }
305         inner_pane.set_divider (0, fract);
306
307         rhs_pane1.set_drag_cursor (*PublicEditor::instance().cursors()->expand_up_down);
308         rhs_pane2.set_drag_cursor (*PublicEditor::instance().cursors()->expand_up_down);
309         list_hpane.set_drag_cursor (*PublicEditor::instance().cursors()->expand_left_right);
310         inner_pane.set_drag_cursor (*PublicEditor::instance().cursors()->expand_left_right);
311
312         _content.pack_start (list_hpane, true, true);
313
314         update_title ();
315
316         route_group_display_button_box->show();
317         route_group_add_button->show();
318         route_group_remove_button->show();
319
320         _content.show ();
321         _content.set_name ("MixerWindow");
322
323         global_hpacker.show();
324         scroller.show();
325         scroller_base.show();
326         scroller_hpacker.show();
327         mixer_scroller_vpacker.show();
328         list_vpacker.show();
329         group_display_button_label.show();
330         group_display_button.show();
331         group_display_scroller.show();
332         favorite_plugins_scroller.show();
333         group_display_vbox.show();
334         group_display_sample.show();
335         favorite_plugins_sample.show();
336         rhs_pane1.show();
337         rhs_pane2.show();
338         strip_packer.show();
339         inner_pane.show();
340         vca_scroller.show();
341         vca_vpacker.show();
342         vca_hpacker.show();
343         vca_label_bar.show();
344         vca_label.show();
345         vca_scroller_base.show();
346         out_packer.show();
347         list_hpane.show();
348         group_display.show();
349         favorite_plugins_display.show();
350
351         MixerStrip::CatchDeletion.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::remove_strip, this, _1), gui_context());
352         VCAMasterStrip::CatchDeletion.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::remove_master, this, _1), gui_context());
353
354         /* handle escape */
355
356         ARDOUR_UI::instance()->Escape.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::escape, this), gui_context());
357
358 #ifndef DEFER_PLUGIN_SELECTOR_LOAD
359         _plugin_selector = new PluginSelector (PluginManager::instance ());
360 #else
361 #error implement deferred Plugin-Favorite list
362 #endif
363         PluginManager::instance ().PluginListChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::refill_favorite_plugins, this), gui_context());
364         PluginManager::instance ().PluginStatusesChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::refill_favorite_plugins, this), gui_context());
365         ARDOUR::Plugin::PresetsChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::refill_favorite_plugins, this), gui_context());
366 }
367
368 Mixer_UI::~Mixer_UI ()
369 {
370         if (_monitor_section) {
371                 monitor_section_detached ();
372                 delete _monitor_section;
373         }
374         delete _plugin_selector;
375         delete track_menu;
376 }
377
378 void
379 Mixer_UI::escape ()
380 {
381         select_none ();
382 }
383
384 Gtk::Window*
385 Mixer_UI::use_own_window (bool and_fill_it)
386 {
387         bool new_window = !own_window();
388
389         Gtk::Window* win = Tabbable::use_own_window (and_fill_it);
390
391         if (win && new_window) {
392                 win->set_name ("MixerWindow");
393                 ARDOUR_UI::instance()->setup_toplevel_window (*win, _("Mixer"), this);
394                 win->signal_event().connect (sigc::bind (sigc::ptr_fun (&Keyboard::catch_user_event_for_pre_dialog_focus), win));
395                 win->set_data ("ardour-bindings", bindings);
396                 update_title ();
397                 if (!win->get_focus()) {
398                         /* set focus widget to something, anything */
399                         win->set_focus (scroller);
400                 }
401         }
402
403         return win;
404 }
405
406 void
407 Mixer_UI::show_window ()
408 {
409         Tabbable::show_window ();
410
411         /* show/hide group tabs as required */
412         parameter_changed ("show-group-tabs");
413
414         /* now reset each strips width so the right widgets are shown */
415
416         TreeModel::Children rows = track_model->children();
417         TreeModel::Children::iterator ri;
418
419         for (ri = rows.begin(); ri != rows.end(); ++ri) {
420                 AxisView* av = (*ri)[stripable_columns.strip];
421                 MixerStrip* ms = dynamic_cast<MixerStrip*> (av);
422                 if (!ms) {
423                         continue;
424                 }
425                 ms->set_width_enum (ms->get_width_enum (), ms->width_owner());
426                 /* Fix visibility of mixer strip stuff */
427                 ms->parameter_changed (X_("mixer-element-visibility"));
428         }
429
430         /* force focus into main area */
431         scroller_base.grab_focus ();
432 }
433
434 void
435 Mixer_UI::remove_master (VCAMasterStrip* vms)
436 {
437         if (_session && _session->deletion_in_progress()) {
438                 /* its all being taken care of */
439                 return;
440         }
441
442         TreeModel::Children rows = track_model->children();
443         TreeModel::Children::iterator ri;
444
445         for (ri = rows.begin(); ri != rows.end(); ++ri) {
446                 if ((*ri)[stripable_columns.strip] == vms) {
447                         PBD::Unwinder<bool> uw (_route_deletion_in_progress, true);
448                         track_model->erase (ri);
449                         break;
450                 }
451         }
452 }
453
454 bool
455 Mixer_UI::masters_scroller_button_release (GdkEventButton* ev)
456 {
457         using namespace Menu_Helpers;
458
459         if (Keyboard::is_context_menu_event (ev)) {
460                 ARDOUR_UI::instance()->add_route ();
461                 return true;
462         }
463
464         return false;
465 }
466
467 void
468 Mixer_UI::add_masters (VCAList& vlist)
469 {
470         StripableList sl;
471
472         for (VCAList::iterator v = vlist.begin(); v != vlist.end(); ++v) {
473                 sl.push_back (boost::dynamic_pointer_cast<Stripable> (*v));
474         }
475
476         add_stripables (sl);
477 }
478
479 void
480 Mixer_UI::add_routes (RouteList& rlist)
481 {
482         StripableList sl;
483
484         for (RouteList::iterator r = rlist.begin(); r != rlist.end(); ++r) {
485                 sl.push_back (*r);
486         }
487
488         add_stripables (sl);
489 }
490
491 void
492 Mixer_UI::add_stripables (StripableList& slist)
493 {
494         Gtk::TreeModel::Children::iterator insert_iter = track_model->children().end();
495         bool from_scratch = (track_model->children().size() == 0);
496         uint32_t nroutes = 0;
497
498         slist.sort (Stripable::Sorter());
499
500         for (Gtk::TreeModel::Children::iterator it = track_model->children().begin(); it != track_model->children().end(); ++it) {
501                 boost::shared_ptr<Stripable> s = (*it)[stripable_columns.stripable];
502
503                 if (!s) {
504                         continue;
505                 }
506
507                 nroutes++;
508
509                 // XXX what does this special case do?
510                 if (s->presentation_info().order() == (slist.front()->presentation_info().order() + slist.size())) {
511                         insert_iter = it;
512                         break;
513                 }
514         }
515
516         MixerStrip* strip;
517
518         try {
519                 PBD::Unwinder<bool> uw (no_track_list_redisplay, true);
520
521                 track_display.set_model (Glib::RefPtr<ListStore>());
522
523                 for (StripableList::iterator s = slist.begin(); s != slist.end(); ++s) {
524
525                         boost::shared_ptr<Route> route;
526                         boost::shared_ptr<VCA> vca;
527
528                         if ((vca  = boost::dynamic_pointer_cast<VCA> (*s))) {
529
530                                 VCAMasterStrip* vms = new VCAMasterStrip (_session, vca);
531
532                                 TreeModel::Row row = *(track_model->append());
533
534                                 row[stripable_columns.text] = vca->name();
535                                 row[stripable_columns.visible] = vms->marked_for_display ();
536                                 row[stripable_columns.strip] = vms;
537                                 row[stripable_columns.stripable] = vca;
538
539                                 vms->signal_button_release_event().connect (sigc::bind (sigc::mem_fun(*this, &Mixer_UI::vca_button_release_event), vms));
540
541                         } else if ((route = boost::dynamic_pointer_cast<Route> (*s))) {
542
543                                 if (route->is_auditioner()) {
544                                         continue;
545                                 }
546
547                                 if (route->is_monitor()) {
548
549                                         if (!_monitor_section) {
550                                                 _monitor_section = new MonitorSection (_session);
551
552                                                 XMLNode* mnode = ARDOUR_UI::instance()->tearoff_settings (X_("monitor-section"));
553                                                 if (mnode) {
554                                                         _monitor_section->tearoff().set_state (*mnode);
555                                                 }
556                                         }
557
558                                         out_packer.pack_end (_monitor_section->tearoff(), false, false);
559                                         _monitor_section->set_session (_session);
560                                         _monitor_section->tearoff().show_all ();
561
562                                         _monitor_section->tearoff().Detach.connect (sigc::mem_fun(*this, &Mixer_UI::monitor_section_detached));
563                                         _monitor_section->tearoff().Attach.connect (sigc::mem_fun(*this, &Mixer_UI::monitor_section_attached));
564
565                                         if (_monitor_section->tearoff().torn_off()) {
566                                                 monitor_section_detached ();
567                                         } else {
568                                                 monitor_section_attached ();
569                                         }
570
571                                         route->DropReferences.connect (*this, invalidator(*this), boost::bind (&Mixer_UI::monitor_section_going_away, this), gui_context());
572
573                                         /* no regular strip shown for control out */
574
575                                         continue;
576                                 }
577
578                                 strip = new MixerStrip (*this, _session, route);
579                                 strips.push_back (strip);
580
581                                 UIConfiguration::instance().get_default_narrow_ms() ? _strip_width = Narrow : _strip_width = Wide;
582
583                                 if (strip->width_owner() != strip) {
584                                         strip->set_width_enum (_strip_width, this);
585                                 }
586
587                                 show_strip (strip);
588
589                                 if (!route->is_master()) {
590
591                                         TreeModel::Row row = *(track_model->insert (insert_iter));
592
593                                         row[stripable_columns.text] = route->name();
594                                         row[stripable_columns.visible] = strip->marked_for_display();
595                                         row[stripable_columns.stripable] = route;
596                                         row[stripable_columns.strip] = strip;
597
598                                 } else {
599
600                                         out_packer.pack_start (*strip, false, false);
601                                         strip->set_packed (true);
602                                 }
603
604                                 strip->WidthChanged.connect (sigc::mem_fun(*this, &Mixer_UI::strip_width_changed));
605                                 strip->signal_button_release_event().connect (sigc::bind (sigc::mem_fun(*this, &Mixer_UI::strip_button_release_event), strip));
606                         }
607
608                         (*s)->presentation_info().PropertyChanged.connect (*this, invalidator(*this), boost::bind (&Mixer_UI::stripable_property_changed, this, _1, boost::weak_ptr<Stripable>(*s)), gui_context());
609                         (*s)->PropertyChanged.connect (*this, invalidator(*this), boost::bind (&Mixer_UI::stripable_property_changed, this, _1, boost::weak_ptr<Stripable>(*s)), gui_context());
610                 }
611
612         } catch (const std::exception& e) {
613                 error << string_compose (_("Error adding GUI elements for new tracks/busses %1"), e.what()) << endmsg;
614         }
615
616         track_display.set_model (track_model);
617
618         /* catch up on selection state, which we left to the editor to set */
619         sync_treeview_from_presentation_info (PropertyChange (Properties::selected));
620
621         if (!from_scratch) {
622                 sync_presentation_info_from_treeview ();
623         }
624
625         redisplay_track_list ();
626 }
627
628 void
629 Mixer_UI::deselect_all_strip_processors ()
630 {
631         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
632                 (*i)->deselect_all_processors();
633         }
634 }
635
636 void
637 Mixer_UI::select_all_tracks ()
638 {
639         PublicEditor::instance().select_all_tracks ();
640 }
641
642 void
643 Mixer_UI::select_none ()
644 {
645         _selection.clear_routes();
646         deselect_all_strip_processors();
647 }
648
649 void
650 Mixer_UI::delete_processors ()
651 {
652         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
653                 (*i)->delete_processors();
654         }
655 }
656
657
658 void
659 Mixer_UI::remove_strip (MixerStrip* strip)
660 {
661         if (_session && _session->deletion_in_progress()) {
662                 /* its all being taken care of */
663                 return;
664         }
665
666         TreeModel::Children rows = track_model->children();
667         TreeModel::Children::iterator ri;
668         list<MixerStrip *>::iterator i;
669
670         if ((i = find (strips.begin(), strips.end(), strip)) != strips.end()) {
671                 strips.erase (i);
672         }
673
674         PBD::Unwinder<bool> uwi (ignore_reorder, true);
675
676         for (ri = rows.begin(); ri != rows.end(); ++ri) {
677                 if ((*ri)[stripable_columns.strip] == strip) {
678                         PBD::Unwinder<bool> uw (_route_deletion_in_progress, true);
679                         track_model->erase (ri);
680                         break;
681                 }
682         }
683 }
684
685 void
686 Mixer_UI::presentation_info_changed (PropertyChange const & what_changed)
687 {
688         if (what_changed.contains (Properties::selected)) {
689                 _selection.presentation_info_changed (what_changed);
690         }
691
692         PropertyChange soh;
693         soh.add (Properties::selected);
694         soh.add (Properties::order);
695         soh.add (Properties::hidden);
696
697         if (what_changed.contains (soh)) {
698                 sync_treeview_from_presentation_info (what_changed);
699         }
700 }
701
702 void
703 Mixer_UI::sync_presentation_info_from_treeview ()
704 {
705         if (ignore_reorder || !_session || _session->deletion_in_progress()) {
706                 return;
707         }
708
709         TreeModel::Children rows = track_model->children();
710
711         if (rows.empty()) {
712                 return;
713         }
714
715         DEBUG_TRACE (DEBUG::OrderKeys, "mixer sync presentation info from treeview\n");
716
717         TreeModel::Children::iterator ri;
718         bool change = false;
719
720         PresentationInfo::order_t master_key = _session->master_order_key ();
721         PresentationInfo::order_t order = 0;
722
723         PresentationInfo::ChangeSuspender cs;
724
725         for (ri = rows.begin(); ri != rows.end(); ++ri) {
726                 bool visible = (*ri)[stripable_columns.visible];
727                 boost::shared_ptr<Stripable> stripable = (*ri)[stripable_columns.stripable];
728
729 #ifndef NDEBUG // these should not exist in the mixer's treeview
730                 if (!stripable) {
731                         assert (0);
732                         continue;
733                 }
734                 if (stripable->is_monitor() || stripable->is_auditioner()) {
735                         assert (0);
736                         continue;
737                 }
738                 if (stripable->is_master()) {
739                         assert (0);
740                         continue;
741                 }
742 #endif
743
744                 stripable->presentation_info().set_hidden (!visible);
745
746                 // leave master where it is.
747                 if (order == master_key) {
748                         ++order;
749                 }
750
751                 if (order != stripable->presentation_info().order()) {
752                         stripable->set_presentation_order (order);
753                         change = true;
754                 }
755                 ++order;
756         }
757
758         change |= _session->ensure_stripable_sort_order ();
759
760         if (change) {
761                 DEBUG_TRACE (DEBUG::OrderKeys, "... notify PI change from mixer GUI\n");
762                 _session->set_dirty();
763         }
764 }
765
766 void
767 Mixer_UI::sync_treeview_from_presentation_info (PropertyChange const & what_changed)
768 {
769         if (!_session || _session->deletion_in_progress()) {
770                 return;
771         }
772
773         DEBUG_TRACE (DEBUG::OrderKeys, "mixer sync model from presentation info.\n");
774
775         /* we could get here after either a change in the Mixer or Editor sort
776          * order, but either way, the mixer order keys reflect the intended
777          * order for the GUI, so reorder the treeview model to match it.
778          */
779
780         vector<int> neworder;
781         TreeModel::Children rows = track_model->children();
782         uint32_t old_order = 0;
783         bool changed = false;
784
785         if (rows.empty()) {
786                 return;
787         }
788
789         TreeOrderKeys sorted;
790         for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri, ++old_order) {
791                 boost::shared_ptr<Stripable> stripable = (*ri)[stripable_columns.stripable];
792                 sorted.push_back (TreeOrderKey (old_order, stripable));
793         }
794
795         TreeOrderKeySorter cmp;
796
797         sort (sorted.begin(), sorted.end(), cmp);
798         neworder.assign (sorted.size(), 0);
799
800         uint32_t n = 0;
801
802         for (TreeOrderKeys::iterator sr = sorted.begin(); sr != sorted.end(); ++sr, ++n) {
803
804                 neworder[n] = sr->old_display_order;
805
806                 if (sr->old_display_order != n) {
807                         changed = true;
808                 }
809         }
810
811         if (changed) {
812                 Unwinder<bool> uw (ignore_reorder, true);
813                 track_model->reorder (neworder);
814         }
815
816         if (what_changed.contains (Properties::selected)) {
817
818                 PresentationInfo::ChangeSuspender cs;
819
820                 for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
821                         boost::shared_ptr<Stripable> stripable = (*i)->stripable();
822                         if (stripable && stripable->is_selected()) {
823                                 _selection.add (*i);
824                         } else {
825                                 _selection.remove (*i);
826                         }
827                 }
828
829                 if (!_selection.axes.empty() && !PublicEditor::instance().track_selection_change_without_scroll ()) {
830                         move_stripable_into_view ((*_selection.axes.begin())->stripable());
831                 }
832
833                 TreeModel::Children rows = track_model->children();
834                 for (TreeModel::Children::const_iterator i = rows.begin(); i != rows.end(); ++i) {
835                         AxisView* av = (*i)[stripable_columns.strip];
836                         VCAMasterStrip* vms = dynamic_cast<VCAMasterStrip*> (av);
837                         if (!vms) {
838                                 continue;
839                         }
840                         if (vms->vca() && vms->vca()->is_selected()) {
841                                 _selection.add (vms);
842                         } else {
843                                 _selection.remove (vms);
844                         }
845                 }
846         }
847
848         redisplay_track_list ();
849 }
850
851
852 MixerStrip*
853 Mixer_UI::strip_by_route (boost::shared_ptr<Route> r) const
854 {
855         for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
856                 if ((*i)->route() == r) {
857                         return (*i);
858                 }
859         }
860
861         return 0;
862 }
863
864 MixerStrip*
865 Mixer_UI::strip_by_stripable (boost::shared_ptr<Stripable> s) const
866 {
867         for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
868                 if ((*i)->stripable() == s) {
869                         return (*i);
870                 }
871         }
872
873         return 0;
874 }
875
876 AxisView*
877 Mixer_UI::axis_view_by_stripable (boost::shared_ptr<Stripable> s) const
878 {
879         for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
880                 if ((*i)->stripable() == s) {
881                         return (*i);
882                 }
883         }
884
885         TreeModel::Children rows = track_model->children();
886         for (TreeModel::Children::const_iterator i = rows.begin(); i != rows.end(); ++i) {
887                 AxisView* av = (*i)[stripable_columns.strip];
888                 VCAMasterStrip* vms = dynamic_cast<VCAMasterStrip*> (av);
889                 if (vms && vms->stripable () == s) {
890                         return av;
891                 }
892         }
893
894         return 0;
895 }
896
897 AxisView*
898 Mixer_UI::axis_view_by_control (boost::shared_ptr<AutomationControl> c) const
899 {
900         for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
901                 if ((*i)->control() == c) {
902                         return (*i);
903                 }
904         }
905
906         return 0;
907 }
908
909 struct MixerStripSorter {
910         bool operator() (const MixerStrip* ms_a, const MixerStrip* ms_b)
911         {
912                 boost::shared_ptr<ARDOUR::Stripable> const& a = ms_a->stripable ();
913                 boost::shared_ptr<ARDOUR::Stripable> const& b = ms_b->stripable ();
914                 return ARDOUR::Stripable::Sorter(true)(a, b);
915         }
916 };
917
918 bool
919 Mixer_UI::strip_button_release_event (GdkEventButton *ev, MixerStrip *strip)
920 {
921         if (ev->button == 1) {
922                 if (_selection.selected (strip)) {
923                         /* primary-click: toggle selection state of strip */
924                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
925                                 _selection.remove (strip, true);
926                         } else if (_selection.axes.size() > 1) {
927                                 /* de-select others */
928                                 _selection.set (strip);
929                         }
930                         PublicEditor& pe = PublicEditor::instance();
931                         TimeAxisView* tav = pe.time_axis_view_from_stripable (strip->stripable());
932                         if (tav) {
933                                 pe.set_selected_mixer_strip (*tav);
934                         }
935                 } else {
936                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
937                                 _selection.add (strip, true);
938                         } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::RangeSelectModifier)) {
939
940                                 /* extend selection */
941
942                                 vector<MixerStrip*> tmp;
943                                 bool accumulate = false;
944                                 bool found_another = false;
945
946                                 strips.sort (MixerStripSorter());
947
948                                 for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
949                                         MixerStrip* ms = *i;
950                                         assert (ms);
951
952                                         if (ms == strip) {
953                                                 /* hit clicked strip, start accumulating till we hit the first
954                                                    selected strip
955                                                 */
956                                                 if (accumulate) {
957                                                         /* done */
958                                                         break;
959                                                 } else {
960                                                         accumulate = true;
961                                                 }
962                                         } else if (_selection.selected (ms)) {
963                                                 /* hit selected strip. if currently accumulating others,
964                                                    we're done. if not accumulating others, start doing so.
965                                                 */
966                                                 found_another = true;
967                                                 if (accumulate) {
968                                                         /* done */
969                                                         break;
970                                                 } else {
971                                                         accumulate = true;
972                                                 }
973                                         } else {
974                                                 if (accumulate) {
975                                                         tmp.push_back (ms);
976                                                 }
977                                         }
978                                 }
979
980                                 tmp.push_back (strip);
981
982                                 if (found_another) {
983                                         PresentationInfo::ChangeSuspender cs;
984                                         for (vector<MixerStrip*>::iterator i = tmp.begin(); i != tmp.end(); ++i) {
985                                                 _selection.add (*i, true);
986                                         }
987                                 } else {
988                                         _selection.set (strip);  //user wants to start a range selection, but there aren't any others selected yet
989                                 }
990                         } else {
991                                 _selection.set (strip);
992                         }
993                 }
994         }
995
996         return true;
997 }
998
999 bool
1000 Mixer_UI::vca_button_release_event (GdkEventButton *ev, VCAMasterStrip *strip)
1001 {
1002         _selection.set (strip);
1003         return true;
1004 }
1005
1006 void
1007 Mixer_UI::set_session (Session* sess)
1008 {
1009         SessionHandlePtr::set_session (sess);
1010
1011         if (_plugin_selector) {
1012                 _plugin_selector->set_session (_session);
1013         }
1014
1015         _group_tabs->set_session (sess);
1016
1017         if (_monitor_section) {
1018                 _monitor_section->set_session (_session);
1019         }
1020
1021         if (!_session) {
1022                 _selection.clear ();
1023                 return;
1024         }
1025
1026         refill_favorite_plugins();
1027
1028         XMLNode* node = ARDOUR_UI::instance()->mixer_settings();
1029         set_state (*node, 0);
1030
1031         update_title ();
1032
1033         initial_track_display ();
1034
1035         _session->RouteAdded.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::add_routes, this, _1), gui_context());
1036         _session->route_group_added.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::add_route_group, this, _1), gui_context());
1037         _session->route_group_removed.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::route_groups_changed, this), gui_context());
1038         _session->route_groups_reordered.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::route_groups_changed, this), gui_context());
1039         _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::parameter_changed, this, _1), gui_context());
1040         _session->DirtyChanged.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::update_title, this), gui_context());
1041         _session->StateSaved.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::update_title, this), gui_context());
1042
1043         _session->vca_manager().VCAAdded.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::add_masters, this, _1), gui_context());
1044
1045         Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::parameter_changed, this, _1), gui_context ());
1046
1047         route_groups_changed ();
1048
1049         if (_visible) {
1050                 show_window();
1051         }
1052
1053         /* catch up on selection state, etc. */
1054
1055         PropertyChange sc;
1056         sc.add (Properties::selected);
1057         _selection.presentation_info_changed (sc);
1058
1059         start_updating ();
1060 }
1061
1062 void
1063 Mixer_UI::session_going_away ()
1064 {
1065         ENSURE_GUI_THREAD (*this, &Mixer_UI::session_going_away);
1066
1067         _in_group_rebuild_or_clear = true;
1068         group_model->clear ();
1069         _in_group_rebuild_or_clear = false;
1070
1071         _selection.clear ();
1072         track_model->clear ();
1073
1074         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
1075                 delete (*i);
1076         }
1077
1078         if (_monitor_section) {
1079                 _monitor_section->tearoff().hide_visible ();
1080         }
1081
1082         monitor_section_detached ();
1083
1084         strips.clear ();
1085
1086         stop_updating ();
1087
1088         SessionHandlePtr::session_going_away ();
1089
1090         _session = 0;
1091         update_title ();
1092 }
1093
1094 void
1095 Mixer_UI::track_visibility_changed (std::string const & path)
1096 {
1097         if (_session && _session->deletion_in_progress()) {
1098                 return;
1099         }
1100
1101         TreeIter iter;
1102
1103         if ((iter = track_model->get_iter (path))) {
1104
1105                 AxisView* av = (*iter)[stripable_columns.strip];
1106                 bool visible = (*iter)[stripable_columns.visible];
1107
1108                 if (av->set_marked_for_display (!visible)) {
1109                         update_track_visibility ();
1110                 }
1111         }
1112 }
1113
1114 void
1115 Mixer_UI::update_track_visibility ()
1116 {
1117         TreeModel::Children rows = track_model->children();
1118         TreeModel::Children::iterator i;
1119
1120         {
1121                 Unwinder<bool> uw (no_track_list_redisplay, true);
1122
1123                 for (i = rows.begin(); i != rows.end(); ++i) {
1124                         AxisView* av = (*i)[stripable_columns.strip];
1125                         (*i)[stripable_columns.visible] = av->marked_for_display ();
1126                 }
1127
1128                 /* force presentation catch up with visibility changes
1129                  */
1130
1131                 sync_presentation_info_from_treeview ();
1132         }
1133
1134         redisplay_track_list ();
1135 }
1136
1137 void
1138 Mixer_UI::show_strip (MixerStrip* ms)
1139 {
1140         TreeModel::Children rows = track_model->children();
1141         TreeModel::Children::iterator i;
1142
1143         for (i = rows.begin(); i != rows.end(); ++i) {
1144
1145                 AxisView* av = (*i)[stripable_columns.strip];
1146                 MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1147                 if (strip == ms) {
1148                         (*i)[stripable_columns.visible] = true;
1149                         av->set_marked_for_display (true);
1150                         update_track_visibility ();
1151                         break;
1152                 }
1153         }
1154 }
1155
1156 void
1157 Mixer_UI::hide_strip (MixerStrip* ms)
1158 {
1159         TreeModel::Children rows = track_model->children();
1160         TreeModel::Children::iterator i;
1161
1162         for (i = rows.begin(); i != rows.end(); ++i) {
1163
1164                 AxisView* av = (*i)[stripable_columns.strip];
1165                 MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1166                 if (strip == ms) {
1167                         (*i)[stripable_columns.visible] = false;
1168                         av->set_marked_for_display (false);
1169                         update_track_visibility ();
1170                         break;
1171                 }
1172         }
1173 }
1174
1175 gint
1176 Mixer_UI::start_updating ()
1177 {
1178         fast_screen_update_connection = Timers::super_rapid_connect (sigc::mem_fun(*this, &Mixer_UI::fast_update_strips));
1179         return 0;
1180 }
1181
1182 gint
1183 Mixer_UI::stop_updating ()
1184 {
1185         fast_screen_update_connection.disconnect();
1186         return 0;
1187 }
1188
1189 void
1190 Mixer_UI::fast_update_strips ()
1191 {
1192         if (_content.is_mapped () && _session) {
1193                 for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
1194                         (*i)->fast_update ();
1195                 }
1196         }
1197 }
1198
1199 void
1200 Mixer_UI::set_all_strips_visibility (bool yn)
1201 {
1202         TreeModel::Children rows = track_model->children();
1203         TreeModel::Children::iterator i;
1204
1205         {
1206                 Unwinder<bool> uw (no_track_list_redisplay, true);
1207
1208                 for (i = rows.begin(); i != rows.end(); ++i) {
1209
1210                         AxisView* av = (*i)[stripable_columns.strip];
1211                         MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1212
1213                         if (!strip) {
1214                                 continue;
1215                         }
1216
1217                         if (strip->route()->is_master() || strip->route()->is_monitor()) {
1218                                 continue;
1219                         }
1220
1221                         (*i)[stripable_columns.visible] = yn;
1222                 }
1223         }
1224
1225         redisplay_track_list ();
1226 }
1227
1228
1229 void
1230 Mixer_UI::set_all_audio_midi_visibility (int tracks, bool yn)
1231 {
1232         TreeModel::Children rows = track_model->children();
1233         TreeModel::Children::iterator i;
1234
1235         {
1236                 Unwinder<bool> uw (no_track_list_redisplay, true);
1237
1238                 for (i = rows.begin(); i != rows.end(); ++i) {
1239
1240                         AxisView* av = (*i)[stripable_columns.strip];
1241                         MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1242
1243                         if (!strip) {
1244                                 continue;
1245                         }
1246
1247                         if (strip->route()->is_master() || strip->route()->is_monitor()) {
1248                                 continue;
1249                         }
1250
1251                         boost::shared_ptr<AudioTrack> at = strip->audio_track();
1252                         boost::shared_ptr<MidiTrack> mt = strip->midi_track();
1253
1254                         switch (tracks) {
1255                         case 0:
1256                                 (*i)[stripable_columns.visible] = yn;
1257                                 break;
1258
1259                         case 1:
1260                                 if (at) { /* track */
1261                                         (*i)[stripable_columns.visible] = yn;
1262                                 }
1263                                 break;
1264
1265                         case 2:
1266                                 if (!at && !mt) { /* bus */
1267                                         (*i)[stripable_columns.visible] = yn;
1268                                 }
1269                                 break;
1270
1271                         case 3:
1272                                 if (mt) { /* midi-track */
1273                                         (*i)[stripable_columns.visible] = yn;
1274                                 }
1275                                 break;
1276                         }
1277                 }
1278         }
1279
1280         redisplay_track_list ();
1281 }
1282
1283 void
1284 Mixer_UI::hide_all_routes ()
1285 {
1286         set_all_strips_visibility (false);
1287 }
1288
1289 void
1290 Mixer_UI::show_all_routes ()
1291 {
1292         set_all_strips_visibility (true);
1293 }
1294
1295 void
1296 Mixer_UI::show_all_audiobus ()
1297 {
1298         set_all_audio_midi_visibility (2, true);
1299 }
1300 void
1301 Mixer_UI::hide_all_audiobus ()
1302 {
1303         set_all_audio_midi_visibility (2, false);
1304 }
1305
1306 void
1307 Mixer_UI::show_all_audiotracks()
1308 {
1309         set_all_audio_midi_visibility (1, true);
1310 }
1311 void
1312 Mixer_UI::hide_all_audiotracks ()
1313 {
1314         set_all_audio_midi_visibility (1, false);
1315 }
1316
1317 void
1318 Mixer_UI::show_all_miditracks()
1319 {
1320         set_all_audio_midi_visibility (3, true);
1321 }
1322 void
1323 Mixer_UI::hide_all_miditracks ()
1324 {
1325         set_all_audio_midi_visibility (3, false);
1326 }
1327
1328 void
1329 Mixer_UI::track_list_reorder (const TreeModel::Path&, const TreeModel::iterator&, int* /*new_order*/)
1330 {
1331         DEBUG_TRACE (DEBUG::OrderKeys, "mixer UI treeview reordered\n");
1332         sync_presentation_info_from_treeview ();
1333 }
1334
1335 void
1336 Mixer_UI::track_list_delete (const Gtk::TreeModel::Path&)
1337 {
1338         /* this happens as the second step of a DnD within the treeview as well
1339            as when a row/route is actually deleted.
1340
1341            if it was a deletion then we have to force a redisplay because
1342            order keys may not have changed.
1343         */
1344
1345         DEBUG_TRACE (DEBUG::OrderKeys, "mixer UI treeview row deleted\n");
1346         sync_presentation_info_from_treeview ();
1347
1348         if (_route_deletion_in_progress) {
1349                 redisplay_track_list ();
1350         }
1351 }
1352
1353 void
1354 Mixer_UI::spill_redisplay (boost::shared_ptr<VCA> vca)
1355 {
1356         TreeModel::Children rows = track_model->children();
1357         std::list<boost::shared_ptr<VCA> > vcas;
1358         vcas.push_back (vca);
1359
1360         for (TreeModel::Children::const_iterator i = rows.begin(); i != rows.end(); ++i) {
1361                 AxisView* av = (*i)[stripable_columns.strip];
1362                 VCAMasterStrip* vms = dynamic_cast<VCAMasterStrip*> (av);
1363                 if (vms && vms->vca()->slaved_to (vca)) {
1364                         vcas.push_back (vms->vca());
1365                 }
1366         }
1367
1368         for (TreeModel::Children::const_iterator i = rows.begin(); i != rows.end(); ++i) {
1369
1370                 AxisView* av = (*i)[stripable_columns.strip];
1371                 MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1372                 bool const visible = (*i)[stripable_columns.visible];
1373
1374                 if (!strip) {
1375                         /* we're in the middle of changing a row, don't worry */
1376                         continue;
1377                 }
1378
1379                 if (!strip->route()) {
1380                         /* non-route element */
1381                         continue;
1382                 }
1383
1384                 if (strip->route()->is_master() || strip->route()->is_monitor()) {
1385                         continue;
1386                 }
1387
1388                 bool slaved = false;
1389                 for (std::list<boost::shared_ptr<VCA> >::const_iterator m = vcas.begin(); m != vcas.end(); ++m) {
1390                         if (strip->route()->slaved_to (*m)) {
1391                                 slaved = true;
1392                                 break;
1393                         }
1394                 }
1395
1396                 if (slaved && visible) {
1397
1398                         if (strip->packed()) {
1399                                 strip_packer.reorder_child (*strip, -1); /* put at end */
1400                         } else {
1401                                 strip_packer.pack_start (*strip, false, false);
1402                                 strip->set_packed (true);
1403                         }
1404
1405                 } else {
1406
1407                         if (strip->packed()) {
1408                                 strip_packer.remove (*strip);
1409                                 strip->set_packed (false);
1410                         }
1411                 }
1412         }
1413 }
1414
1415 void
1416 Mixer_UI::redisplay_track_list ()
1417 {
1418         if (no_track_list_redisplay) {
1419                 return;
1420         }
1421
1422         boost::shared_ptr<Stripable> ss = spilled_strip.lock ();
1423         if (ss) {
1424                 boost::shared_ptr<VCA> sv = boost::dynamic_pointer_cast<VCA> (ss);
1425                 if (sv) {
1426                         spill_redisplay (sv);
1427                         return;
1428                 }
1429         }
1430
1431         TreeModel::Children rows = track_model->children();
1432         TreeModel::Children::iterator i;
1433         uint32_t n_masters = 0;
1434
1435         container_clear (vca_hpacker);
1436         vca_hpacker.pack_end (vca_scroller_base, true, true);
1437
1438         for (i = rows.begin(); i != rows.end(); ++i) {
1439
1440                 AxisView* s = (*i)[stripable_columns.strip];
1441                 bool const visible = (*i)[stripable_columns.visible];
1442                 boost::shared_ptr<Stripable> stripable = (*i)[stripable_columns.stripable];
1443
1444                 if (!s) {
1445                         /* we're in the middle of changing a row, don't worry */
1446                         continue;
1447                 }
1448
1449                 VCAMasterStrip* vms;
1450
1451                 if ((vms = dynamic_cast<VCAMasterStrip*> (s))) {
1452                         if (visible) {
1453                                 vca_hpacker.pack_start (*vms, false, false);
1454                                 vms->show ();
1455                                 n_masters++;
1456                         }
1457                         continue;
1458                 }
1459
1460                 MixerStrip* strip = dynamic_cast<MixerStrip*> (s);
1461
1462                 if (!strip) {
1463                         continue;
1464                 }
1465
1466                 if (visible) {
1467
1468                         if (strip->packed()) {
1469                                 strip_packer.reorder_child (*strip, -1); /* put at end */
1470                         } else {
1471                                 strip_packer.pack_start (*strip, false, false);
1472                                 strip->set_packed (true);
1473                         }
1474
1475                 } else {
1476
1477                         if (stripable->is_master() || stripable->is_monitor()) {
1478                                 /* do nothing, these cannot be hidden */
1479                         } else {
1480                                 if (strip->packed()) {
1481                                         strip_packer.remove (*strip);
1482                                         strip->set_packed (false);
1483                                 }
1484                         }
1485                 }
1486         }
1487
1488         /* update visibility of VCA assign buttons */
1489
1490         if (n_masters == 0) {
1491                 UIConfiguration::instance().set_mixer_strip_visibility (VisibilityGroup::remove_element (UIConfiguration::instance().get_mixer_strip_visibility(), X_("VCA")));
1492                 vca_vpacker.hide ();
1493         } else {
1494                 UIConfiguration::instance().set_mixer_strip_visibility (VisibilityGroup::add_element (UIConfiguration::instance().get_mixer_strip_visibility(), X_("VCA")));
1495                 vca_vpacker.show ();
1496         }
1497
1498         _group_tabs->set_dirty ();
1499 }
1500
1501 void
1502 Mixer_UI::strip_width_changed ()
1503 {
1504         _group_tabs->set_dirty ();
1505
1506 #ifdef __APPLE__
1507         TreeModel::Children rows = track_model->children();
1508         TreeModel::Children::iterator i;
1509         long order;
1510
1511         for (order = 0, i = rows.begin(); i != rows.end(); ++i, ++order) {
1512                 AxisView* av = (*i)[stripable_columns.strip];
1513                 MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1514
1515                 if (strip == 0) {
1516                         continue;
1517                 }
1518
1519                 bool visible = (*i)[stripable_columns.visible];
1520
1521                 if (visible) {
1522                         strip->queue_draw();
1523                 }
1524         }
1525 #endif
1526
1527 }
1528
1529 struct PresentationInfoMixerSorter
1530 {
1531         bool operator() (boost::shared_ptr<Stripable> a, boost::shared_ptr<Stripable> b) {
1532                 if (a->is_master()) {
1533                         /* master after everything else */
1534                         return false;
1535                 } else if (b->is_master()) {
1536                         /* everything else before master */
1537                         return true;
1538                 }
1539                 return a->presentation_info().order () < b->presentation_info().order ();
1540         }
1541 };
1542
1543 void
1544 Mixer_UI::initial_track_display ()
1545 {
1546         StripableList sl;
1547
1548         boost::shared_ptr<RouteList> routes = _session->get_routes();
1549
1550         for (RouteList::iterator r = routes->begin(); r != routes->end(); ++r) {
1551                 sl.push_back (*r);
1552         }
1553
1554         VCAList vcas = _session->vca_manager().vcas();
1555
1556         for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
1557                 sl.push_back (boost::dynamic_pointer_cast<Stripable> (*v));
1558         }
1559
1560         sl.sort (PresentationInfoMixerSorter());
1561
1562         {
1563                 /* These are also used inside ::add_stripables() but we need
1564                  *  them here because we're going to clear the track_model also.
1565                  */
1566                 Unwinder<bool> uw1 (no_track_list_redisplay, true);
1567                 Unwinder<bool> uw2 (ignore_reorder, true);
1568
1569                 track_model->clear ();
1570                 add_stripables (sl);
1571         }
1572
1573         sync_treeview_from_presentation_info (Properties::order);
1574 }
1575
1576 bool
1577 Mixer_UI::track_display_button_press (GdkEventButton* ev)
1578 {
1579         if (Keyboard::is_context_menu_event (ev)) {
1580                 if (track_menu == 0) {
1581                         build_track_menu ();
1582                 }
1583                 track_menu->popup (ev->button, ev->time);
1584                 return true;
1585         }
1586         if ((ev->type == GDK_BUTTON_PRESS) && (ev->button == 1)) {
1587                 TreeModel::Path path;
1588                 TreeViewColumn* column;
1589                 int cellx, celly;
1590                 if (track_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
1591                         TreeIter iter = track_model->get_iter (path);
1592                         if ((*iter)[stripable_columns.visible]) {
1593                                 boost::shared_ptr<ARDOUR::Stripable> s = (*iter)[stripable_columns.stripable];
1594                                 move_stripable_into_view (s);
1595                         }
1596                 }
1597         }
1598
1599         return false;
1600 }
1601
1602 void
1603 Mixer_UI::move_stripable_into_view (boost::shared_ptr<ARDOUR::Stripable> s)
1604 {
1605         if (!scroller.get_hscrollbar()) {
1606                 return;
1607         }
1608         if (s->presentation_info().special () || s->presentation_info().flag_match (PresentationInfo::VCA)) {
1609                 return;
1610         }
1611 #ifdef MIXBUS
1612         if (s->mixbus ()) {
1613                 return;
1614         }
1615 #endif
1616         bool found = false;
1617         int x0 = 0;
1618         Gtk::Allocation alloc;
1619         for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
1620                 if ((*i)->route() == s) {
1621                         int y;
1622                         found = true;
1623                         (*i)->translate_coordinates (strip_packer, 0, 0, x0, y);
1624                         alloc = (*i)->get_allocation ();
1625                         break;
1626                 }
1627         }
1628         if (!found) {
1629                 return;
1630         }
1631
1632         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
1633
1634         if (x0 < adj->get_value()) {
1635                 adj->set_value (max (adj->get_lower(), min (adj->get_upper(), (double) x0)));
1636         } else if (x0 + alloc.get_width() >= adj->get_value() + adj->get_page_size()) {
1637                 int x1 = x0 + alloc.get_width() - adj->get_page_size();
1638                 adj->set_value (max (adj->get_lower(), min (adj->get_upper(), (double) x1)));
1639         }
1640 }
1641
1642 void
1643 Mixer_UI::build_track_menu ()
1644 {
1645         using namespace Menu_Helpers;
1646         using namespace Gtk;
1647
1648         track_menu = new Menu;
1649         track_menu->set_name ("ArdourContextMenu");
1650         MenuList& items = track_menu->items();
1651
1652         items.push_back (MenuElem (_("Show All"), sigc::mem_fun(*this, &Mixer_UI::show_all_routes)));
1653         items.push_back (MenuElem (_("Hide All"), sigc::mem_fun(*this, &Mixer_UI::hide_all_routes)));
1654         items.push_back (MenuElem (_("Show All Audio Tracks"), sigc::mem_fun(*this, &Mixer_UI::show_all_audiotracks)));
1655         items.push_back (MenuElem (_("Hide All Audio Tracks"), sigc::mem_fun(*this, &Mixer_UI::hide_all_audiotracks)));
1656         items.push_back (MenuElem (_("Show All Audio Busses"), sigc::mem_fun(*this, &Mixer_UI::show_all_audiobus)));
1657         items.push_back (MenuElem (_("Hide All Audio Busses"), sigc::mem_fun(*this, &Mixer_UI::hide_all_audiobus)));
1658         items.push_back (MenuElem (_("Show All Midi Tracks"), sigc::mem_fun (*this, &Mixer_UI::show_all_miditracks)));
1659         items.push_back (MenuElem (_("Hide All Midi Tracks"), sigc::mem_fun (*this, &Mixer_UI::hide_all_miditracks)));
1660
1661 }
1662
1663 void
1664 Mixer_UI::stripable_property_changed (const PropertyChange& what_changed, boost::weak_ptr<Stripable> ws)
1665 {
1666         if (!what_changed.contains (ARDOUR::Properties::hidden) && !what_changed.contains (ARDOUR::Properties::name)) {
1667                 return;
1668         }
1669
1670         boost::shared_ptr<Stripable> s = ws.lock ();
1671
1672         if (!s) {
1673                 return;
1674         }
1675
1676         TreeModel::Children rows = track_model->children();
1677         TreeModel::Children::iterator i;
1678
1679         for (i = rows.begin(); i != rows.end(); ++i) {
1680                 boost::shared_ptr<Stripable> ss = (*i)[stripable_columns.stripable];
1681
1682                 if (s == ss) {
1683
1684                         if (what_changed.contains (ARDOUR::Properties::name)) {
1685                                 (*i)[stripable_columns.text] = s->name();
1686                         }
1687
1688                         if (what_changed.contains (ARDOUR::Properties::hidden)) {
1689                                 (*i)[stripable_columns.visible] = !s->presentation_info().hidden();
1690                                 redisplay_track_list ();
1691                         }
1692
1693                         return;
1694                 }
1695         }
1696
1697         if (s->is_master ()) {
1698                 return;
1699         }
1700
1701         error << _("track display list item for renamed strip not found!") << endmsg;
1702 }
1703
1704 bool
1705 Mixer_UI::group_display_button_press (GdkEventButton* ev)
1706 {
1707         TreeModel::Path path;
1708         TreeViewColumn* column;
1709         int cellx;
1710         int celly;
1711
1712         if (!group_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
1713                 if (ev->button == 3) {
1714                         _group_tabs->get_menu(0)->popup (ev->button, ev->time);
1715                 }
1716                 return true;
1717         }
1718
1719         TreeIter iter = group_model->get_iter (path);
1720         if (!iter) {
1721                 if (ev->button == 3) {
1722                         _group_tabs->get_menu(0)->popup (ev->button, ev->time);
1723                 }
1724                 return true;
1725         }
1726
1727         RouteGroup* group = (*iter)[group_columns.group];
1728
1729         if (Keyboard::is_context_menu_event (ev)) {
1730                 _group_tabs->get_menu(group)->popup (1, ev->time);
1731                 return true;
1732         }
1733
1734         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
1735         case 1:
1736                 if (Keyboard::is_edit_event (ev)) {
1737                         if (group) {
1738                                 // edit_route_group (group);
1739 #ifdef __APPLE__
1740                                 group_display.queue_draw();
1741 #endif
1742                                 return true;
1743                         }
1744                 }
1745                 break;
1746
1747         case 0:
1748         {
1749                 bool visible = (*iter)[group_columns.visible];
1750                 (*iter)[group_columns.visible] = !visible;
1751 #ifdef __APPLE__
1752                 group_display.queue_draw();
1753 #endif
1754                 return true;
1755         }
1756
1757         default:
1758                 break;
1759         }
1760
1761         return false;
1762  }
1763
1764 void
1765 Mixer_UI::activate_all_route_groups ()
1766 {
1767         _session->foreach_route_group (sigc::bind (sigc::mem_fun (*this, &Mixer_UI::set_route_group_activation), true));
1768 }
1769
1770 void
1771 Mixer_UI::disable_all_route_groups ()
1772 {
1773         _session->foreach_route_group (sigc::bind (sigc::mem_fun (*this, &Mixer_UI::set_route_group_activation), false));
1774 }
1775
1776 void
1777 Mixer_UI::route_groups_changed ()
1778 {
1779         ENSURE_GUI_THREAD (*this, &Mixer_UI::route_groups_changed);
1780
1781         _in_group_rebuild_or_clear = true;
1782
1783         /* just rebuild the while thing */
1784
1785         group_model->clear ();
1786
1787 #if 0
1788         /* this is currently not used,
1789          * Mixer_UI::group_display_button_press() has a case for it,
1790          * and a commented edit_route_group() but that's n/a since 2011.
1791          *
1792          * This code is left as reminder that
1793          * row[group_columns.group] = 0 has special meaning.
1794          */
1795         {
1796                 TreeModel::Row row;
1797                 row = *(group_model->append());
1798                 row[group_columns.visible] = true;
1799                 row[group_columns.text] = (_("-all-"));
1800                 row[group_columns.group] = 0;
1801         }
1802 #endif
1803
1804         _session->foreach_route_group (sigc::mem_fun (*this, &Mixer_UI::add_route_group));
1805
1806         _group_tabs->set_dirty ();
1807         _in_group_rebuild_or_clear = false;
1808 }
1809
1810 void
1811 Mixer_UI::new_route_group ()
1812 {
1813         _group_tabs->run_new_group_dialog (0, false);
1814 }
1815
1816 void
1817 Mixer_UI::remove_selected_route_group ()
1818 {
1819         Glib::RefPtr<TreeSelection> selection = group_display.get_selection();
1820         TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
1821
1822         if (rows.empty()) {
1823                 return;
1824         }
1825
1826         TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
1827         TreeIter iter;
1828
1829         /* selection mode is single, so rows.begin() is it */
1830
1831         if ((iter = group_model->get_iter (*i))) {
1832
1833                 RouteGroup* rg = (*iter)[group_columns.group];
1834
1835                 if (rg) {
1836                         _session->remove_route_group (*rg);
1837                 }
1838         }
1839 }
1840
1841 void
1842 Mixer_UI::route_group_property_changed (RouteGroup* group, const PropertyChange& change)
1843 {
1844         if (in_group_row_change) {
1845                 return;
1846         }
1847
1848         /* force an update of any mixer strips that are using this group,
1849            otherwise mix group names don't change in mixer strips
1850         */
1851
1852         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
1853                 if ((*i)->route_group() == group) {
1854                         (*i)->route_group_changed();
1855                 }
1856         }
1857
1858         TreeModel::iterator i;
1859         TreeModel::Children rows = group_model->children();
1860         Glib::RefPtr<TreeSelection> selection = group_display.get_selection();
1861
1862         in_group_row_change = true;
1863
1864         for (i = rows.begin(); i != rows.end(); ++i) {
1865                 if ((*i)[group_columns.group] == group) {
1866                         (*i)[group_columns.visible] = !group->is_hidden ();
1867                         (*i)[group_columns.text] = group->name ();
1868                         break;
1869                 }
1870         }
1871
1872         in_group_row_change = false;
1873
1874         if (change.contains (Properties::name)) {
1875                 _group_tabs->set_dirty ();
1876         }
1877
1878         for (list<MixerStrip*>::iterator j = strips.begin(); j != strips.end(); ++j) {
1879                 if ((*j)->route_group() == group) {
1880                         if (group->is_hidden ()) {
1881                                 hide_strip (*j);
1882                         } else {
1883                                 show_strip (*j);
1884                         }
1885                 }
1886         }
1887 }
1888
1889 void
1890 Mixer_UI::show_mixer_list (bool yn)
1891 {
1892         if (yn) {
1893                 list_vpacker.show ();
1894         } else {
1895                 list_vpacker.hide ();
1896         }
1897
1898         _show_mixer_list = yn;
1899 }
1900
1901 void
1902 Mixer_UI::show_monitor_section (bool yn)
1903 {
1904         if (!monitor_section()) {
1905                 return;
1906         }
1907         if (monitor_section()->tearoff().torn_off()) {
1908                 return;
1909         }
1910
1911         if (yn) {
1912                 monitor_section()->tearoff().show();
1913         } else {
1914                 monitor_section()->tearoff().hide();
1915         }
1916 }
1917
1918 void
1919 Mixer_UI::route_group_name_edit (const std::string& path, const std::string& new_text)
1920 {
1921         RouteGroup* group;
1922         TreeIter iter;
1923
1924         if ((iter = group_model->get_iter (path))) {
1925
1926                 if ((group = (*iter)[group_columns.group]) == 0) {
1927                         return;
1928                 }
1929
1930                 if (new_text != group->name()) {
1931                         group->set_name (new_text);
1932                 }
1933         }
1934 }
1935
1936 void
1937 Mixer_UI::route_group_row_change (const Gtk::TreeModel::Path&, const Gtk::TreeModel::iterator& iter)
1938 {
1939         RouteGroup* group;
1940
1941         if (in_group_row_change) {
1942                 return;
1943         }
1944
1945         if ((group = (*iter)[group_columns.group]) == 0) {
1946                 return;
1947         }
1948
1949         std::string name = (*iter)[group_columns.text];
1950
1951         if (name != group->name()) {
1952                 group->set_name (name);
1953         }
1954
1955         bool hidden = !(*iter)[group_columns.visible];
1956
1957         if (hidden != group->is_hidden ()) {
1958                 group->set_hidden (hidden, this);
1959         }
1960 }
1961
1962 /** Called when a group model row is deleted, but also when the model is
1963  *  reordered by a user drag-and-drop; the latter is what we are
1964  *  interested in here.
1965  */
1966 void
1967 Mixer_UI::route_group_row_deleted (Gtk::TreeModel::Path const &)
1968 {
1969         if (_in_group_rebuild_or_clear) {
1970                 return;
1971         }
1972
1973         /* Re-write the session's route group list so that the new order is preserved */
1974
1975         list<RouteGroup*> new_list;
1976
1977         Gtk::TreeModel::Children children = group_model->children();
1978         for (Gtk::TreeModel::Children::iterator i = children.begin(); i != children.end(); ++i) {
1979                 RouteGroup* g = (*i)[group_columns.group];
1980                 if (g) {
1981                         new_list.push_back (g);
1982                 }
1983         }
1984
1985         _session->reorder_route_groups (new_list);
1986 }
1987
1988
1989 void
1990 Mixer_UI::add_route_group (RouteGroup* group)
1991 {
1992         ENSURE_GUI_THREAD (*this, &Mixer_UI::add_route_group, group)
1993         bool focus = false;
1994
1995         in_group_row_change = true;
1996
1997         TreeModel::Row row = *(group_model->append());
1998         row[group_columns.visible] = !group->is_hidden ();
1999         row[group_columns.group] = group;
2000         if (!group->name().empty()) {
2001                 row[group_columns.text] = group->name();
2002         } else {
2003                 row[group_columns.text] = _("unnamed");
2004                 focus = true;
2005         }
2006
2007         group->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::route_group_property_changed, this, group, _1), gui_context());
2008
2009         if (focus) {
2010                 TreeViewColumn* col = group_display.get_column (0);
2011                 CellRendererText* name_cell = dynamic_cast<CellRendererText*>(group_display.get_column_cell_renderer (1));
2012                 group_display.set_cursor (group_model->get_path (row), *col, *name_cell, true);
2013         }
2014
2015         _group_tabs->set_dirty ();
2016
2017         in_group_row_change = false;
2018 }
2019
2020 bool
2021 Mixer_UI::strip_scroller_button_release (GdkEventButton* ev)
2022 {
2023         using namespace Menu_Helpers;
2024
2025         if (Keyboard::is_context_menu_event (ev)) {
2026                 ARDOUR_UI::instance()->add_route ();
2027                 return true;
2028         }
2029
2030         return false;
2031 }
2032
2033 void
2034 Mixer_UI::scroller_drag_data_received (const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, const Gtk::SelectionData& data, guint info, guint time)
2035 {
2036         if (data.get_target() != "PluginFavoritePtr") {
2037                 context->drag_finish (false, false, time);
2038                 return;
2039         }
2040
2041         const void * d = data.get_data();
2042         const Gtkmm2ext::DnDTreeView<ARDOUR::PluginPresetPtr>* tv = reinterpret_cast<const Gtkmm2ext::DnDTreeView<ARDOUR::PluginPresetPtr>*>(d);
2043
2044         PluginPresetList nfos;
2045         TreeView* source;
2046         tv->get_object_drag_data (nfos, &source);
2047
2048         Route::ProcessorList pl;
2049         bool ok = false;
2050
2051         for (list<PluginPresetPtr>::const_iterator i = nfos.begin(); i != nfos.end(); ++i) {
2052                 PluginPresetPtr ppp = (*i);
2053                 PluginInfoPtr pip = ppp->_pip;
2054                 if (!pip->is_instrument ()) {
2055                         continue;
2056                 }
2057                 ARDOUR_UI::instance()->session_add_midi_route (true, (RouteGroup*) 0, 1, _("MIDI"), Config->get_strict_io (), pip, ppp->_preset.valid ? &ppp->_preset : 0, PresentationInfo::max_order);
2058                 ok = true;
2059         }
2060
2061         context->drag_finish (ok, false, time);
2062 }
2063
2064 void
2065 Mixer_UI::set_strip_width (Width w, bool save)
2066 {
2067         _strip_width = w;
2068
2069         for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
2070                 (*i)->set_width_enum (w, save ? (*i)->width_owner() : this);
2071         }
2072 }
2073
2074
2075 struct PluginStateSorter {
2076 public:
2077         bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
2078                 std::list<std::string>::const_iterator aiter = std::find(_user.begin(), _user.end(), (*a).unique_id);
2079                 std::list<std::string>::const_iterator biter = std::find(_user.begin(), _user.end(), (*b).unique_id);
2080                 if (aiter != _user.end() && biter != _user.end()) {
2081                         return std::distance (_user.begin(), aiter)  < std::distance (_user.begin(), biter);
2082                 }
2083                 if (aiter != _user.end()) {
2084                         return true;
2085                 }
2086                 if (biter != _user.end()) {
2087                         return false;
2088                 }
2089                 return ARDOUR::cmp_nocase((*a).name, (*b).name) == -1;
2090         }
2091
2092         PluginStateSorter(std::list<std::string> user) : _user (user)  {}
2093 private:
2094         std::list<std::string> _user;
2095 };
2096
2097 int
2098 Mixer_UI::set_state (const XMLNode& node, int version)
2099 {
2100         bool yn;
2101
2102         Tabbable::set_state (node, version);
2103
2104         if (node.get_property ("narrow-strips", yn)) {
2105                 if (yn) {
2106                         set_strip_width (Narrow);
2107                 } else {
2108                         set_strip_width (Wide);
2109                 }
2110         }
2111
2112         node.get_property ("show-mixer", _visible);
2113
2114         if (node.get_property ("maximised", yn)) {
2115                 Glib::RefPtr<Action> act = ActionManager::get_action (X_("Common"), X_("ToggleMaximalMixer"));
2116                 assert (act);
2117                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2118                 bool fs = tact && tact->get_active();
2119                 if (yn ^ fs) {
2120                         ActionManager::do_action ("Common", "ToggleMaximalMixer");
2121                 }
2122         }
2123
2124         if (node.get_property ("show-mixer-list", yn)) {
2125                 Glib::RefPtr<Action> act = ActionManager::get_action (X_("Common"), X_("ToggleMixerList"));
2126                 assert (act);
2127                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2128
2129                 /* do it twice to force the change */
2130                 tact->set_active (!yn);
2131                 tact->set_active (yn);
2132         }
2133
2134         if (node.get_property ("monitor-section-visible", yn)) {
2135                 Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2136                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2137                 /* do it twice to force the change */
2138                 tact->set_active (yn);
2139                 show_monitor_section (yn);
2140         }
2141
2142
2143         XMLNode* plugin_order;
2144         if ((plugin_order = find_named_node (node, "PluginOrder")) != 0) {
2145                 store_current_favorite_order ();
2146                 std::list<string> order;
2147                 const XMLNodeList& kids = plugin_order->children("PluginInfo");
2148                 XMLNodeConstIterator i;
2149                 for (i = kids.begin(); i != kids.end(); ++i) {
2150                         std::string unique_id;
2151                         if ((*i)->get_property ("unique-id", unique_id)) {
2152                                 order.push_back (unique_id);
2153                                 if ((*i)->get_property ("expanded", yn)) {
2154                                         favorite_ui_state[unique_id] = yn;
2155                                 }
2156                         }
2157                 }
2158                 PluginStateSorter cmp (order);
2159                 favorite_order.sort (cmp);
2160                 sync_treeview_from_favorite_order ();
2161         }
2162         return 0;
2163 }
2164
2165 XMLNode&
2166 Mixer_UI::get_state ()
2167 {
2168         XMLNode* node = new XMLNode (X_("Mixer"));
2169
2170         node->add_child_nocopy (Tabbable::get_state());
2171
2172         node->set_property (X_("mixer-rhs-pane1-pos"), rhs_pane1.get_divider());
2173         node->set_property (X_("mixer-rhs_pane2-pos"), rhs_pane2.get_divider());
2174         node->set_property (X_("mixer-list-hpane-pos"), list_hpane.get_divider());
2175         node->set_property (X_("mixer-inner-pane-pos"),  inner_pane.get_divider());
2176
2177         node->set_property ("narrow-strips", (_strip_width == Narrow));
2178         node->set_property ("show-mixer", _visible);
2179         node->set_property ("show-mixer-list", _show_mixer_list);
2180         node->set_property ("maximised", _maximised);
2181
2182         Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2183         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2184         assert (tact);
2185         node->set_property ("monitor-section-visible", tact->get_active ());
2186
2187         store_current_favorite_order ();
2188         XMLNode* plugin_order = new XMLNode ("PluginOrder");
2189         uint32_t cnt = 0;
2190         for (PluginInfoList::const_iterator i = favorite_order.begin(); i != favorite_order.end(); ++i, ++cnt) {
2191                 XMLNode* p = new XMLNode ("PluginInfo");
2192                 p->set_property ("sort", cnt);
2193                 p->set_property ("unique-id", (*i)->unique_id);
2194                 if (favorite_ui_state.find ((*i)->unique_id) != favorite_ui_state.end ()) {
2195                         p->set_property ("expanded", favorite_ui_state[(*i)->unique_id]);
2196                 }
2197                 plugin_order->add_child_nocopy (*p);
2198         }
2199         node->add_child_nocopy (*plugin_order);
2200
2201         return *node;
2202 }
2203
2204 void
2205 Mixer_UI::scroll_left ()
2206 {
2207         if (!scroller.get_hscrollbar()) return;
2208         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
2209         int sc_w = scroller.get_width();
2210         int sp_w = strip_packer.get_width();
2211         if (sp_w <= sc_w) {
2212                 return;
2213         }
2214         int lp = adj->get_value();
2215         int lm = 0;
2216         using namespace Gtk::Box_Helpers;
2217         const BoxList& strips = strip_packer.children();
2218         for (BoxList::const_iterator i = strips.begin(); i != strips.end(); ++i) {
2219                 lm += i->get_widget()->get_width ();
2220                 if (lm >= lp) {
2221                         lm -= i->get_widget()->get_width ();
2222                         break;
2223                 }
2224         }
2225         scroller.get_hscrollbar()->set_value (max (adj->get_lower(), min (adj->get_upper(), lm - 1.0)));
2226 }
2227
2228 void
2229 Mixer_UI::scroll_right ()
2230 {
2231         if (!scroller.get_hscrollbar()) return;
2232         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
2233         int sc_w = scroller.get_width();
2234         int sp_w = strip_packer.get_width();
2235         if (sp_w <= sc_w) {
2236                 return;
2237         }
2238         int lp = adj->get_value();
2239         int lm = 0;
2240         using namespace Gtk::Box_Helpers;
2241         const BoxList& strips = strip_packer.children();
2242         for (BoxList::const_iterator i = strips.begin(); i != strips.end(); ++i) {
2243                 lm += i->get_widget()->get_width ();
2244                 if (lm > lp + 1) {
2245                         break;
2246                 }
2247         }
2248         scroller.get_hscrollbar()->set_value (max (adj->get_lower(), min (adj->get_upper(), lm - 1.0)));
2249 }
2250
2251 bool
2252 Mixer_UI::on_scroll_event (GdkEventScroll* ev)
2253 {
2254         switch (ev->direction) {
2255         case GDK_SCROLL_LEFT:
2256                 scroll_left ();
2257                 return true;
2258         case GDK_SCROLL_UP:
2259                 if (ev->state & Keyboard::TertiaryModifier) {
2260                         scroll_left ();
2261                         return true;
2262                 }
2263                 return false;
2264
2265         case GDK_SCROLL_RIGHT:
2266                 scroll_right ();
2267                 return true;
2268
2269         case GDK_SCROLL_DOWN:
2270                 if (ev->state & Keyboard::TertiaryModifier) {
2271                         scroll_right ();
2272                         return true;
2273                 }
2274                 return false;
2275         }
2276
2277         return false;
2278 }
2279
2280
2281 void
2282 Mixer_UI::parameter_changed (string const & p)
2283 {
2284         if (p == "show-group-tabs") {
2285                 bool const s = _session->config.get_show_group_tabs ();
2286                 if (s) {
2287                         _group_tabs->show ();
2288                 } else {
2289                         _group_tabs->hide ();
2290                 }
2291         } else if (p == "default-narrow_ms") {
2292                 bool const s = UIConfiguration::instance().get_default_narrow_ms ();
2293                 for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
2294                         (*i)->set_width_enum (s ? Narrow : Wide, this);
2295                 }
2296         }
2297 }
2298
2299 void
2300 Mixer_UI::set_route_group_activation (RouteGroup* g, bool a)
2301 {
2302         g->set_active (a, this);
2303 }
2304
2305 PluginSelector*
2306 Mixer_UI::plugin_selector()
2307 {
2308 #ifdef DEFER_PLUGIN_SELECTOR_LOAD
2309         if (!_plugin_selector)
2310                 _plugin_selector = new PluginSelector (PluginManager::instance());
2311 #endif
2312
2313         return _plugin_selector;
2314 }
2315
2316 void
2317 Mixer_UI::setup_track_display ()
2318 {
2319         track_model = ListStore::create (stripable_columns);
2320         track_display.set_model (track_model);
2321         track_display.append_column (_("Show"), stripable_columns.visible);
2322         track_display.append_column (_("Strips"), stripable_columns.text);
2323         track_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
2324         track_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
2325         track_display.get_column (0)->set_expand(false);
2326         track_display.get_column (1)->set_expand(true);
2327         track_display.get_column (1)->set_sizing (Gtk::TREE_VIEW_COLUMN_FIXED);
2328         track_display.set_name (X_("EditGroupList"));
2329         track_display.get_selection()->set_mode (Gtk::SELECTION_NONE);
2330         track_display.set_reorderable (true);
2331         track_display.set_headers_visible (true);
2332         track_display.set_can_focus(false);
2333
2334         track_model->signal_row_deleted().connect (sigc::mem_fun (*this, &Mixer_UI::track_list_delete));
2335         track_model->signal_rows_reordered().connect (sigc::mem_fun (*this, &Mixer_UI::track_list_reorder));
2336
2337         CellRendererToggle* track_list_visible_cell = dynamic_cast<CellRendererToggle*>(track_display.get_column_cell_renderer (0));
2338         track_list_visible_cell->property_activatable() = true;
2339         track_list_visible_cell->property_radio() = false;
2340         track_list_visible_cell->signal_toggled().connect (sigc::mem_fun (*this, &Mixer_UI::track_visibility_changed));
2341
2342         track_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::track_display_button_press), false);
2343
2344         track_display_scroller.add (track_display);
2345         track_display_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
2346
2347         VBox* v = manage (new VBox);
2348         v->show ();
2349         v->pack_start (track_display_scroller, true, true);
2350
2351         Button* b = manage (new Button);
2352         b->show ();
2353         Widget* w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
2354         w->show ();
2355         b->add (*w);
2356
2357         b->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::new_track_or_bus));
2358
2359         v->pack_start (*b, false, false);
2360
2361         track_display_sample.set_name("BaseFrame");
2362         track_display_sample.set_shadow_type (Gtk::SHADOW_IN);
2363         track_display_sample.add (*v);
2364
2365         track_display_scroller.show();
2366         track_display_sample.show();
2367         track_display.show();
2368 }
2369
2370 void
2371 Mixer_UI::new_track_or_bus ()
2372 {
2373         ARDOUR_UI::instance()->add_route ();
2374 }
2375
2376 void
2377 Mixer_UI::update_title ()
2378 {
2379         if (!own_window()) {
2380                 return;
2381         }
2382
2383         if (_session) {
2384                 string n;
2385
2386                 if (_session->snap_name() != _session->name()) {
2387                         n = _session->snap_name ();
2388                 } else {
2389                         n = _session->name ();
2390                 }
2391
2392                 if (_session->dirty ()) {
2393                         n = "*" + n;
2394                 }
2395
2396                 WindowTitle title (n);
2397                 title += S_("Window|Mixer");
2398                 title += Glib::get_application_name ();
2399                 own_window()->set_title (title.get_string());
2400
2401         } else {
2402
2403                 WindowTitle title (S_("Window|Mixer"));
2404                 title += Glib::get_application_name ();
2405                 own_window()->set_title (title.get_string());
2406         }
2407 }
2408
2409 MixerStrip*
2410 Mixer_UI::strip_by_x (int x)
2411 {
2412         for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
2413                 int x1, x2, y;
2414
2415                 (*i)->translate_coordinates (_content, 0, 0, x1, y);
2416                 x2 = x1 + (*i)->get_width();
2417
2418                 if (x >= x1 && x <= x2) {
2419                         return (*i);
2420                 }
2421         }
2422
2423         return 0;
2424 }
2425
2426 void
2427 Mixer_UI::set_axis_targets_for_operation ()
2428 {
2429         _axis_targets.clear ();
2430
2431         if (!_selection.empty()) {
2432                 _axis_targets = _selection.axes;
2433                 return;
2434         }
2435
2436 //  removed "implicit" selections of strips, after discussion on IRC
2437
2438 }
2439
2440 void
2441 Mixer_UI::monitor_section_going_away ()
2442 {
2443         if (_monitor_section) {
2444                 XMLNode* ui_node = Config->extra_xml(X_("UI"));
2445                 /* immediate state save.
2446                  *
2447                  * Tearoff settings are otherwise only stored during
2448                  * save_ardour_state(). The mon-section may or may not
2449                  * exist at that point.
2450                  * */
2451                 if (ui_node) {
2452                         XMLNode* tearoff_node = ui_node->child (X_("Tearoffs"));
2453                         if (tearoff_node) {
2454                                 tearoff_node->remove_nodes_and_delete (X_("monitor-section"));
2455                                 XMLNode* t = new XMLNode (X_("monitor-section"));
2456                                 _monitor_section->tearoff().add_state (*t);
2457                                 tearoff_node->add_child_nocopy (*t);
2458                         }
2459                 }
2460                 monitor_section_detached ();
2461                 out_packer.remove (_monitor_section->tearoff());
2462                 _monitor_section->set_session (0);
2463                 delete _monitor_section;
2464                 _monitor_section = 0;
2465         }
2466 }
2467
2468 void
2469 Mixer_UI::toggle_midi_input_active (bool flip_others)
2470 {
2471         boost::shared_ptr<RouteList> rl (new RouteList);
2472         bool onoff = false;
2473
2474         set_axis_targets_for_operation ();
2475
2476         for (AxisViewSelection::iterator r = _axis_targets.begin(); r != _axis_targets.end(); ++r) {
2477                 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> ((*r)->stripable());
2478
2479                 if (mt) {
2480                         rl->push_back (mt);
2481                         onoff = !mt->input_active();
2482                 }
2483         }
2484
2485         _session->set_exclusive_input_active (rl, onoff, flip_others);
2486 }
2487
2488 void
2489 Mixer_UI::maximise_mixer_space ()
2490 {
2491         if (!own_window()) {
2492                 return;
2493         }
2494
2495         if (_maximised) {
2496                 return;
2497         }
2498
2499         _window->fullscreen ();
2500         _maximised = true;
2501 }
2502
2503 void
2504 Mixer_UI::restore_mixer_space ()
2505 {
2506         if (!own_window()) {
2507                 return;
2508         }
2509
2510         if (!_maximised) {
2511                 return;
2512         }
2513
2514         own_window()->unfullscreen();
2515         _maximised = false;
2516 }
2517
2518 void
2519 Mixer_UI::monitor_section_attached ()
2520 {
2521         Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2522         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2523         act->set_sensitive (true);
2524         show_monitor_section (tact->get_active ());
2525 }
2526
2527 void
2528 Mixer_UI::monitor_section_detached ()
2529 {
2530         Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2531         act->set_sensitive (false);
2532 }
2533
2534 void
2535 Mixer_UI::store_current_favorite_order ()
2536 {
2537         typedef Gtk::TreeModel::Children type_children;
2538         type_children children = favorite_plugins_model->children();
2539         favorite_order.clear();
2540         for(type_children::iterator iter = children.begin(); iter != children.end(); ++iter)
2541         {
2542                 Gtk::TreeModel::Row row = *iter;
2543                 ARDOUR::PluginPresetPtr ppp = row[favorite_plugins_columns.plugin];
2544                 favorite_order.push_back (ppp->_pip);
2545                 std::string name = row[favorite_plugins_columns.name];
2546                 favorite_ui_state[(*ppp->_pip).unique_id] = favorite_plugins_display.row_expanded (favorite_plugins_model->get_path(iter));
2547         }
2548 }
2549
2550 void
2551 Mixer_UI::save_favorite_ui_state (const TreeModel::iterator& iter, const TreeModel::Path& path)
2552 {
2553         Gtk::TreeModel::Row row = *iter;
2554         ARDOUR::PluginPresetPtr ppp = row[favorite_plugins_columns.plugin];
2555         assert (ppp);
2556         favorite_ui_state[(*ppp->_pip).unique_id] = favorite_plugins_display.row_expanded (favorite_plugins_model->get_path(iter));
2557 }
2558
2559 void
2560 Mixer_UI::refiller (PluginInfoList& result, const PluginInfoList& plugs)
2561 {
2562         PluginManager& manager (PluginManager::instance());
2563         for (PluginInfoList::const_iterator i = plugs.begin(); i != plugs.end(); ++i) {
2564                 if (manager.get_status (*i) != PluginManager::Favorite) {
2565                         continue;
2566                 }
2567                 result.push_back (*i);
2568         }
2569 }
2570
2571 struct PluginCustomSorter {
2572 public:
2573         bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
2574                 PluginInfoList::const_iterator aiter = _user.begin();
2575                 PluginInfoList::const_iterator biter = _user.begin();
2576                 while (aiter != _user.end()) { if ((*aiter)->unique_id == a->unique_id) { break; } ++aiter; }
2577                 while (biter != _user.end()) { if ((*biter)->unique_id == b->unique_id) { break; } ++biter; }
2578
2579                 if (aiter != _user.end() && biter != _user.end()) {
2580                         return std::distance (_user.begin(), aiter) < std::distance (_user.begin(), biter);
2581                 }
2582                 if (aiter != _user.end()) {
2583                         return true;
2584                 }
2585                 if (biter != _user.end()) {
2586                         return false;
2587                 }
2588                 return ARDOUR::cmp_nocase((*a).name, (*b).name) == -1;
2589         }
2590         PluginCustomSorter(PluginInfoList user) : _user (user)  {}
2591 private:
2592         PluginInfoList _user;
2593 };
2594
2595 void
2596 Mixer_UI::refill_favorite_plugins ()
2597 {
2598         PluginInfoList plugs;
2599         PluginManager& mgr (PluginManager::instance());
2600
2601 #ifdef LV2_SUPPORT
2602         refiller (plugs, mgr.lv2_plugin_info ());
2603 #endif
2604 #ifdef WINDOWS_VST_SUPPORT
2605         refiller (plugs, mgr.windows_vst_plugin_info ());
2606 #endif
2607 #ifdef LXVST_SUPPORT
2608         refiller (plugs, mgr.lxvst_plugin_info ());
2609 #endif
2610 #ifdef MACVST_SUPPORT
2611         refiller (plugs, mgr.mac_vst_plugin_info ());
2612 #endif
2613 #ifdef AUDIOUNIT_SUPPORT
2614         refiller (plugs, mgr.au_plugin_info ());
2615 #endif
2616         refiller (plugs, mgr.ladspa_plugin_info ());
2617         refiller (plugs, mgr.lua_plugin_info ());
2618
2619         store_current_favorite_order ();
2620
2621         PluginCustomSorter cmp (favorite_order);
2622         plugs.sort (cmp);
2623
2624         favorite_order = plugs;
2625
2626         sync_treeview_from_favorite_order ();
2627 }
2628
2629 void
2630 Mixer_UI::sync_treeview_favorite_ui_state (const TreeModel::Path& path, const TreeModel::iterator&)
2631 {
2632         TreeIter iter;
2633         if (!(iter = favorite_plugins_model->get_iter (path))) {
2634                 return;
2635         }
2636         ARDOUR::PluginPresetPtr ppp = (*iter)[favorite_plugins_columns.plugin];
2637         if (!ppp) {
2638                 return;
2639         }
2640         PluginInfoPtr pip = ppp->_pip;
2641         if (favorite_ui_state.find (pip->unique_id) != favorite_ui_state.end ()) {
2642                 if (favorite_ui_state[pip->unique_id]) {
2643                         favorite_plugins_display.expand_row (path, true);
2644                 }
2645         }
2646 }
2647
2648 void
2649 Mixer_UI::sync_treeview_from_favorite_order ()
2650 {
2651         favorite_plugins_model->clear ();
2652         for (PluginInfoList::const_iterator i = favorite_order.begin(); i != favorite_order.end(); ++i) {
2653                 PluginInfoPtr pip = (*i);
2654
2655                 TreeModel::Row newrow = *(favorite_plugins_model->append());
2656                 newrow[favorite_plugins_columns.name] = (*i)->name;
2657                 newrow[favorite_plugins_columns.plugin] = PluginPresetPtr (new PluginPreset(pip));
2658                 if (!_session) {
2659                         continue;
2660                 }
2661
2662                 vector<ARDOUR::Plugin::PresetRecord> presets = (*i)->get_presets (true);
2663                 for (vector<ARDOUR::Plugin::PresetRecord>::const_iterator j = presets.begin(); j != presets.end(); ++j) {
2664                         Gtk::TreeModel::Row child_row = *(favorite_plugins_model->append (newrow.children()));
2665                         child_row[favorite_plugins_columns.name] = (*j).label;
2666                         child_row[favorite_plugins_columns.plugin] = PluginPresetPtr (new PluginPreset(pip, &(*j)));
2667                 }
2668                 if (favorite_ui_state.find (pip->unique_id) != favorite_ui_state.end ()) {
2669                         if (favorite_ui_state[pip->unique_id]) {
2670                                 favorite_plugins_display.expand_row (favorite_plugins_model->get_path(newrow), true);
2671                         }
2672                 }
2673         }
2674 }
2675
2676 void
2677 Mixer_UI::popup_note_context_menu (GdkEventButton *ev)
2678 {
2679         using namespace Gtk::Menu_Helpers;
2680
2681         Gtk::Menu* m = manage (new Menu);
2682         MenuList& items = m->items ();
2683
2684         if (_selection.axes.empty()) {
2685                 items.push_back (MenuElem (_("No Track/Bus is selected.")));
2686         } else {
2687                 items.push_back (MenuElem (_("Add at the top"),
2688                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddTop)));
2689                 items.push_back (MenuElem (_("Add Pre-Fader"),
2690                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddPreFader)));
2691                 items.push_back (MenuElem (_("Add Post-Fader"),
2692                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddPostFader)));
2693                 items.push_back (MenuElem (_("Add at the end"),
2694                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddBottom)));
2695         }
2696
2697         items.push_back (SeparatorElem());
2698
2699         items.push_back (MenuElem (_("Remove from favorites"), sigc::mem_fun (*this, &Mixer_UI::remove_selected_from_favorites)));
2700
2701         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2702         if (ppp && ppp->_preset.valid && ppp->_preset.user) {
2703                 // we cannot currently delete AU presets
2704                 if (!ppp->_pip || ppp->_pip->type != AudioUnit) {
2705                         items.push_back (MenuElem (_("Delete Preset"), sigc::mem_fun (*this, &Mixer_UI::delete_selected_preset)));
2706                 }
2707         }
2708
2709         m->popup (ev->button, ev->time);
2710 }
2711
2712 bool
2713 Mixer_UI::plugin_row_button_press (GdkEventButton *ev)
2714 {
2715         if ((ev->type == GDK_BUTTON_PRESS) && (ev->button == 3) ) {
2716                 TreeModel::Path path;
2717                 TreeViewColumn* column;
2718                 int cellx, celly;
2719                 if (favorite_plugins_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
2720                         Glib::RefPtr<Gtk::TreeView::Selection> selection = favorite_plugins_display.get_selection();
2721                         if (selection) {
2722                                 selection->unselect_all();
2723                                 selection->select(path);
2724                         }
2725                 }
2726                 ARDOUR::PluginPresetPtr ppp = selected_plugin();
2727                 if (ppp) {
2728                         popup_note_context_menu (ev);
2729                 }
2730         }
2731         return false;
2732 }
2733
2734
2735 PluginPresetPtr
2736 Mixer_UI::selected_plugin ()
2737 {
2738         Glib::RefPtr<Gtk::TreeView::Selection> selection = favorite_plugins_display.get_selection();
2739         if (!selection) {
2740                 return PluginPresetPtr();
2741         }
2742         Gtk::TreeModel::iterator iter = selection->get_selected();
2743         if (!iter) {
2744                 return PluginPresetPtr();
2745         }
2746         return (*iter)[favorite_plugins_columns.plugin];
2747 }
2748
2749 void
2750 Mixer_UI::add_selected_processor (ProcessorPosition pos)
2751 {
2752         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2753         if (ppp) {
2754                 add_favorite_processor (ppp, pos);
2755         }
2756 }
2757
2758 void
2759 Mixer_UI::delete_selected_preset ()
2760 {
2761         if (!_session) {
2762                 return;
2763         }
2764         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2765         if (!ppp || !ppp->_preset.valid || !ppp->_preset.user) {
2766                 return;
2767         }
2768         PluginPtr plugin = ppp->_pip->load (*_session);
2769         plugin->get_presets();
2770         plugin->remove_preset (ppp->_preset.label);
2771 }
2772
2773 void
2774 Mixer_UI::remove_selected_from_favorites ()
2775 {
2776         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2777         if (!ppp) {
2778                 return;
2779         }
2780         PluginManager::PluginStatusType status = PluginManager::Normal;
2781         PluginManager& manager (PluginManager::instance());
2782
2783         manager.set_status (ppp->_pip->type, ppp->_pip->unique_id, status);
2784         manager.save_statuses ();
2785 }
2786
2787 void
2788 Mixer_UI::plugin_row_activated (const TreeModel::Path& path, TreeViewColumn* column)
2789 {
2790         TreeIter iter;
2791         if (!(iter = favorite_plugins_model->get_iter (path))) {
2792                 return;
2793         }
2794         ARDOUR::PluginPresetPtr ppp = (*iter)[favorite_plugins_columns.plugin];
2795         add_favorite_processor (ppp, AddPreFader); // TODO: preference?!
2796 }
2797
2798 void
2799 Mixer_UI::add_favorite_processor (ARDOUR::PluginPresetPtr ppp, ProcessorPosition pos)
2800 {
2801         if (!_session || _selection.axes.empty()) {
2802                 return;
2803         }
2804
2805         PluginInfoPtr pip = ppp->_pip;
2806         for (AxisViewSelection::iterator i = _selection.axes.begin(); i != _selection.axes.end(); ++i) {
2807                 boost::shared_ptr<ARDOUR::Route> rt = boost::dynamic_pointer_cast<ARDOUR::Route> ((*i)->stripable());
2808
2809                 if (!rt) {
2810                         continue;
2811                 }
2812
2813                 PluginPtr p = pip->load (*_session);
2814
2815                 if (!p) {
2816                         continue;
2817                 }
2818
2819                 if (ppp->_preset.valid) {
2820                         p->load_preset (ppp->_preset);
2821                 }
2822
2823                 Route::ProcessorStreams err;
2824                 boost::shared_ptr<Processor> processor (new PluginInsert (*_session, p));
2825
2826                 switch (pos) {
2827                         case AddTop:
2828                                 rt->add_processor_by_index (processor, 0, &err, Config->get_new_plugins_active ());
2829                                 break;
2830                         case AddPreFader:
2831                                 rt->add_processor (processor, PreFader, &err, Config->get_new_plugins_active ());
2832                                 break;
2833                         case AddPostFader:
2834                                 {
2835                                         int idx = 0;
2836                                         int pos = 0;
2837                                         for (;;++idx) {
2838                                                 boost::shared_ptr<Processor> np = rt->nth_processor (idx);
2839                                                 if (!np) {
2840                                                         break;
2841                                                 }
2842                                                 if (!np->display_to_user()) {
2843                                                         continue;
2844                                                 }
2845                                                 if (boost::dynamic_pointer_cast<Amp> (np) && // Fader, not Trim
2846                                                                 boost::dynamic_pointer_cast<Amp> (np)->gain_control()->parameter().type() == GainAutomation) {
2847                                                         break;
2848                                                 }
2849                                                 ++pos;
2850                                         }
2851                                         rt->add_processor_by_index (processor, ++pos, &err, Config->get_new_plugins_active ());
2852                                 }
2853                                 break;
2854                         case AddBottom:
2855                                 rt->add_processor_by_index (processor, -1, &err, Config->get_new_plugins_active ());
2856                                 break;
2857                 }
2858         }
2859 }
2860
2861 bool
2862 PluginTreeStore::row_drop_possible_vfunc(const Gtk::TreeModel::Path& dest, const Gtk::SelectionData& data) const
2863 {
2864         if (data.get_target() != "GTK_TREE_MODEL_ROW") {
2865                 return false;
2866         }
2867
2868         // only allow to re-order top-level items
2869         TreePath src;
2870         if (TreePath::get_from_selection_data (data, src)) {
2871                 if (src.up() && src.up()) {
2872                         return false;
2873                 }
2874         }
2875
2876         // don't allow to drop as child-rows.
2877         Gtk::TreeModel::Path _dest = dest; // un const
2878         const bool is_child = _dest.up (); // explicit bool for clang
2879         if (!is_child || _dest.empty ()) {
2880                 return true;
2881         }
2882         return false;
2883 }
2884
2885 void
2886 Mixer_UI::plugin_drop (const Glib::RefPtr<Gdk::DragContext>&, const Gtk::SelectionData& data)
2887 {
2888         if (data.get_target() != "PluginPresetPtr") {
2889                 return;
2890         }
2891         if (data.get_length() != sizeof (PluginPresetPtr)) {
2892                 return;
2893         }
2894         const void *d = data.get_data();
2895         const PluginPresetPtr ppp = *(static_cast<const PluginPresetPtr*> (d));
2896
2897         PluginManager::PluginStatusType status = PluginManager::Favorite;
2898         PluginManager& manager (PluginManager::instance());
2899
2900         manager.set_status (ppp->_pip->type, ppp->_pip->unique_id, status);
2901         manager.save_statuses ();
2902 }
2903
2904 void
2905 Mixer_UI::do_vca_assign (boost::shared_ptr<VCA> vca)
2906 {
2907         /* call protected MixerActor:: method */
2908         vca_assign (vca);
2909 }
2910
2911 void
2912 Mixer_UI::do_vca_unassign (boost::shared_ptr<VCA> vca)
2913 {
2914         /* call protected MixerActor:: method */
2915         vca_unassign (vca);
2916 }
2917
2918 void
2919 Mixer_UI::show_spill (boost::shared_ptr<Stripable> s)
2920 {
2921         boost::shared_ptr<Stripable> ss = spilled_strip.lock();
2922         if (ss != s) {
2923                 spilled_strip = s;
2924                 show_spill_change (s); /* EMIT SIGNAL */
2925                 if (s) {
2926                         _group_tabs->hide ();
2927                 } else {
2928                         _group_tabs->show ();
2929                 }
2930                 redisplay_track_list ();
2931         }
2932 }
2933
2934 bool
2935 Mixer_UI::showing_spill_for (boost::shared_ptr<Stripable> s) const
2936 {
2937         return s == spilled_strip.lock();
2938 }
2939
2940 void
2941 Mixer_UI::show_editor_window () const
2942 {
2943         PublicEditor::instance().make_visible ();
2944 }
2945
2946 void
2947 Mixer_UI::register_actions ()
2948 {
2949         Glib::RefPtr<ActionGroup> group = myactions.create_action_group (X_("Mixer"));
2950
2951         myactions.register_action (group, "show-editor", _("Show Editor"), sigc::mem_fun (*this, &Mixer_UI::show_editor_window));
2952
2953         myactions.register_action (group, "solo", _("Toggle Solo on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::solo_action));
2954         myactions.register_action (group, "mute", _("Toggle Mute on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::mute_action));
2955         myactions.register_action (group, "recenable", _("Toggle Rec-enable on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::rec_enable_action));
2956         myactions.register_action (group, "increment-gain", _("Decrease Gain on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::step_gain_up_action));
2957         myactions.register_action (group, "decrement-gain", _("Increase Gain on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::step_gain_down_action));
2958         myactions.register_action (group, "unity-gain", _("Set Gain to 0dB on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::unity_gain_action));
2959
2960
2961         myactions.register_action (group, "copy-processors", _("Copy Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::copy_processors));
2962         myactions.register_action (group, "cut-processors", _("Cut Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::cut_processors));
2963         myactions.register_action (group, "paste-processors", _("Paste Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::paste_processors));
2964         myactions.register_action (group, "delete-processors", _("Delete Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::delete_processors));
2965         myactions.register_action (group, "select-all-processors", _("Select All (visible) Processors"), sigc::mem_fun (*this, &Mixer_UI::select_all_processors));
2966         myactions.register_action (group, "toggle-processors", _("Toggle Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::toggle_processors));
2967         myactions.register_action (group, "ab-plugins", _("Toggle Selected Plugins"), sigc::mem_fun (*this, &Mixer_UI::ab_plugins));
2968         myactions.register_action (group, "select-none", _("Deselect all strips and processors"), sigc::mem_fun (*this, &Mixer_UI::select_none));
2969         myactions.register_action (group, "select-all-tracks", _("Select All Tracks"), sigc::mem_fun (*this, &Mixer_UI::select_all_tracks));
2970
2971         myactions.register_action (group, "scroll-left", _("Scroll Mixer Window to the left"), sigc::mem_fun (*this, &Mixer_UI::scroll_left));
2972         myactions.register_action (group, "scroll-right", _("Scroll Mixer Window to the right"), sigc::mem_fun (*this, &Mixer_UI::scroll_right));
2973
2974         myactions.register_action (group, "toggle-midi-input-active", _("Toggle MIDI Input Active for Mixer-Selected Tracks/Busses"),
2975                                    sigc::bind (sigc::mem_fun (*this, &Mixer_UI::toggle_midi_input_active), false));
2976 }
2977
2978 void
2979 Mixer_UI::load_bindings ()
2980 {
2981         bindings = Bindings::get_bindings (X_("Mixer"), myactions);
2982 }
2983
2984 template<class T> void
2985 Mixer_UI::control_action (boost::shared_ptr<T> (Stripable::*get_control)() const)
2986 {
2987         boost::shared_ptr<ControlList> cl (new ControlList);
2988         boost::shared_ptr<AutomationControl> ac;
2989         bool val = false;
2990         bool have_val = false;
2991
2992         set_axis_targets_for_operation ();
2993
2994         BOOST_FOREACH(AxisView* r, _axis_targets) {
2995                 boost::shared_ptr<Stripable> s = r->stripable();
2996                 if (s) {
2997                         ac = (s.get()->*get_control)();
2998                         if (ac) {
2999                                 cl->push_back (ac);
3000                                 if (!have_val) {
3001                                         val = !ac->get_value();
3002                                         have_val = true;
3003                                 }
3004                         }
3005                 }
3006         }
3007
3008         _session->set_controls (cl,  val, Controllable::UseGroup);
3009 }
3010
3011 void
3012 Mixer_UI::solo_action ()
3013 {
3014         control_action (&Stripable::solo_control);
3015 }
3016
3017 void
3018 Mixer_UI::mute_action ()
3019 {
3020         control_action (&Stripable::mute_control);
3021 }
3022
3023 void
3024 Mixer_UI::rec_enable_action ()
3025 {
3026         control_action (&Stripable::rec_enable_control);
3027 }
3028
3029 AutomationControlSet
3030 Mixer_UI::selected_gaincontrols ()
3031 {
3032         set_axis_targets_for_operation ();
3033         AutomationControlSet rv;
3034         BOOST_FOREACH(AxisView* r, _axis_targets) {
3035                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3036                 if (ms) {
3037                         boost::shared_ptr<GainControl> ac (ms->route()->gain_control());
3038                         ControlList cl (ac->grouped_controls());
3039                         for (ControlList::const_iterator c = cl.begin(); c != cl.end (); ++c) {
3040                                 rv.insert (*c);
3041                         }
3042                         rv.insert (ac);
3043                 }
3044         }
3045         return rv;
3046 }
3047
3048 void
3049 Mixer_UI::step_gain_up_action ()
3050 {
3051         AutomationControlSet acs = selected_gaincontrols ();
3052         for (AutomationControlSet::const_iterator i = acs.begin(); i != acs.end (); ++i) {
3053                 boost::shared_ptr<GainControl> ac = boost::dynamic_pointer_cast<GainControl> (*i);
3054                 assert (ac);
3055                 ac->set_value (dB_to_coefficient (accurate_coefficient_to_dB (ac->get_value()) + 0.1), Controllable::NoGroup);
3056         }
3057 }
3058
3059 void
3060 Mixer_UI::step_gain_down_action ()
3061 {
3062         AutomationControlSet acs = selected_gaincontrols ();
3063         for (AutomationControlSet::const_iterator i = acs.begin(); i != acs.end (); ++i) {
3064                 boost::shared_ptr<GainControl> ac = boost::dynamic_pointer_cast<GainControl> (*i);
3065                 assert (ac);
3066                 ac->set_value (dB_to_coefficient (accurate_coefficient_to_dB (ac->get_value()) - 0.1), Controllable::NoGroup);
3067         }
3068 }
3069
3070 void
3071 Mixer_UI::unity_gain_action ()
3072 {
3073         set_axis_targets_for_operation ();
3074
3075         BOOST_FOREACH(AxisView* r, _axis_targets) {
3076                 boost::shared_ptr<Stripable> s = r->stripable();
3077                 if (s) {
3078                         boost::shared_ptr<AutomationControl> ac = s->gain_control();
3079                         if (ac) {
3080                                 ac->set_value (1.0, Controllable::UseGroup);
3081                         }
3082                 }
3083         }
3084 }
3085
3086 void
3087 Mixer_UI::copy_processors ()
3088 {
3089         set_axis_targets_for_operation ();
3090
3091         BOOST_FOREACH(AxisView* r, _axis_targets) {
3092                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3093                 if (ms) {
3094                         ms->copy_processors ();
3095                 }
3096         }
3097 }
3098 void
3099 Mixer_UI::cut_processors ()
3100 {
3101         set_axis_targets_for_operation ();
3102
3103         BOOST_FOREACH(AxisView* r, _axis_targets) {
3104                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3105                 if (ms) {
3106                         ms->cut_processors ();
3107                 }
3108         }
3109 }
3110 void
3111 Mixer_UI::paste_processors ()
3112 {
3113         set_axis_targets_for_operation ();
3114
3115         BOOST_FOREACH(AxisView* r, _axis_targets) {
3116                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3117                 if (ms) {
3118                         ms->paste_processors ();
3119                 }
3120         }
3121 }
3122 void
3123 Mixer_UI::select_all_processors ()
3124 {
3125         set_axis_targets_for_operation ();
3126
3127         BOOST_FOREACH(AxisView* r, _axis_targets) {
3128                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3129                 if (ms) {
3130                         ms->select_all_processors ();
3131                 }
3132         }
3133 }
3134 void
3135 Mixer_UI::toggle_processors ()
3136 {
3137         set_axis_targets_for_operation ();
3138
3139         BOOST_FOREACH(AxisView* r, _axis_targets) {
3140                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3141                 if (ms) {
3142                         ms->toggle_processors ();
3143                 }
3144         }
3145 }
3146 void
3147 Mixer_UI::ab_plugins ()
3148 {
3149         set_axis_targets_for_operation ();
3150
3151         BOOST_FOREACH(AxisView* r, _axis_targets) {
3152                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3153                 if (ms) {
3154                         ms->ab_plugins ();
3155                 }
3156         }
3157 }
3158
3159 void
3160 Mixer_UI::vca_assign (boost::shared_ptr<VCA> vca)
3161 {
3162         set_axis_targets_for_operation ();
3163         BOOST_FOREACH(AxisView* r, _axis_targets) {
3164                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3165                 if (ms) {
3166                         ms->vca_assign (vca);
3167                 }
3168         }
3169 }
3170
3171 void
3172 Mixer_UI::vca_unassign (boost::shared_ptr<VCA> vca)
3173 {
3174         set_axis_targets_for_operation ();
3175         BOOST_FOREACH(AxisView* r, _axis_targets) {
3176                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3177                 if (ms) {
3178                         ms->vca_unassign (vca);
3179                 }
3180         }
3181 }