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