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