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