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