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