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