Fix VCA pane being made visible when redisplaying tracks
[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         } else {
1517                 UIConfiguration::instance().set_mixer_strip_visibility (VisibilityGroup::add_element (UIConfiguration::instance().get_mixer_strip_visibility(), X_("VCA")));
1518
1519                 Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleVCAPane");
1520                 if (act) {
1521                         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
1522                         showhide_vcas (tact->get_active());
1523                 } else {
1524                         vca_vpacker.show ();
1525                 }
1526         }
1527
1528         _group_tabs->set_dirty ();
1529 }
1530
1531 void
1532 Mixer_UI::strip_width_changed ()
1533 {
1534         _group_tabs->set_dirty ();
1535
1536 #ifdef __APPLE__
1537         TreeModel::Children rows = track_model->children();
1538         TreeModel::Children::iterator i;
1539         long order;
1540
1541         for (order = 0, i = rows.begin(); i != rows.end(); ++i, ++order) {
1542                 AxisView* av = (*i)[stripable_columns.strip];
1543                 MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1544
1545                 if (strip == 0) {
1546                         continue;
1547                 }
1548
1549                 bool visible = (*i)[stripable_columns.visible];
1550
1551                 if (visible) {
1552                         strip->queue_draw();
1553                 }
1554         }
1555 #endif
1556
1557 }
1558
1559 struct PresentationInfoMixerSorter
1560 {
1561         bool operator() (boost::shared_ptr<Stripable> a, boost::shared_ptr<Stripable> b) {
1562                 if (a->is_master()) {
1563                         /* master after everything else */
1564                         return false;
1565                 } else if (b->is_master()) {
1566                         /* everything else before master */
1567                         return true;
1568                 }
1569                 return a->presentation_info().order () < b->presentation_info().order ();
1570         }
1571 };
1572
1573 void
1574 Mixer_UI::initial_track_display ()
1575 {
1576         StripableList sl;
1577
1578         boost::shared_ptr<RouteList> routes = _session->get_routes();
1579
1580         for (RouteList::iterator r = routes->begin(); r != routes->end(); ++r) {
1581                 sl.push_back (*r);
1582         }
1583
1584         VCAList vcas = _session->vca_manager().vcas();
1585
1586         for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
1587                 sl.push_back (boost::dynamic_pointer_cast<Stripable> (*v));
1588         }
1589
1590         sl.sort (PresentationInfoMixerSorter());
1591
1592         {
1593                 /* These are also used inside ::add_stripables() but we need
1594                  *  them here because we're going to clear the track_model also.
1595                  */
1596                 Unwinder<bool> uw1 (no_track_list_redisplay, true);
1597                 Unwinder<bool> uw2 (ignore_reorder, true);
1598
1599                 track_model->clear ();
1600                 add_stripables (sl);
1601         }
1602
1603         sync_treeview_from_presentation_info (Properties::order);
1604 }
1605
1606 bool
1607 Mixer_UI::track_display_button_press (GdkEventButton* ev)
1608 {
1609         if (Keyboard::is_context_menu_event (ev)) {
1610                 if (track_menu == 0) {
1611                         build_track_menu ();
1612                 }
1613                 track_menu->popup (ev->button, ev->time);
1614                 return true;
1615         }
1616         if ((ev->type == GDK_BUTTON_PRESS) && (ev->button == 1)) {
1617                 TreeModel::Path path;
1618                 TreeViewColumn* column;
1619                 int cellx, celly;
1620                 if (track_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
1621                         TreeIter iter = track_model->get_iter (path);
1622                         if ((*iter)[stripable_columns.visible]) {
1623                                 boost::shared_ptr<ARDOUR::Stripable> s = (*iter)[stripable_columns.stripable];
1624                                 move_stripable_into_view (s);
1625                         }
1626                 }
1627         }
1628
1629         return false;
1630 }
1631
1632 void
1633 Mixer_UI::move_stripable_into_view (boost::shared_ptr<ARDOUR::Stripable> s)
1634 {
1635         if (!scroller.get_hscrollbar()) {
1636                 return;
1637         }
1638         if (s->presentation_info().special () || s->presentation_info().flag_match (PresentationInfo::VCA)) {
1639                 return;
1640         }
1641 #ifdef MIXBUS
1642         if (s->mixbus ()) {
1643                 return;
1644         }
1645 #endif
1646         bool found = false;
1647         int x0 = 0;
1648         Gtk::Allocation alloc;
1649         for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
1650                 if ((*i)->route() == s) {
1651                         int y;
1652                         found = true;
1653                         (*i)->translate_coordinates (strip_packer, 0, 0, x0, y);
1654                         alloc = (*i)->get_allocation ();
1655                         break;
1656                 }
1657         }
1658         if (!found) {
1659                 return;
1660         }
1661
1662         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
1663
1664         if (x0 < adj->get_value()) {
1665                 adj->set_value (max (adj->get_lower(), min (adj->get_upper(), (double) x0)));
1666         } else if (x0 + alloc.get_width() >= adj->get_value() + adj->get_page_size()) {
1667                 int x1 = x0 + alloc.get_width() - adj->get_page_size();
1668                 adj->set_value (max (adj->get_lower(), min (adj->get_upper(), (double) x1)));
1669         }
1670 }
1671
1672 void
1673 Mixer_UI::build_track_menu ()
1674 {
1675         using namespace Menu_Helpers;
1676         using namespace Gtk;
1677
1678         track_menu = new Menu;
1679         track_menu->set_name ("ArdourContextMenu");
1680         MenuList& items = track_menu->items();
1681
1682         items.push_back (MenuElem (_("Show All"), sigc::mem_fun(*this, &Mixer_UI::show_all_routes)));
1683         items.push_back (MenuElem (_("Hide All"), sigc::mem_fun(*this, &Mixer_UI::hide_all_routes)));
1684         items.push_back (MenuElem (_("Show All Audio Tracks"), sigc::mem_fun(*this, &Mixer_UI::show_all_audiotracks)));
1685         items.push_back (MenuElem (_("Hide All Audio Tracks"), sigc::mem_fun(*this, &Mixer_UI::hide_all_audiotracks)));
1686         items.push_back (MenuElem (_("Show All Audio Busses"), sigc::mem_fun(*this, &Mixer_UI::show_all_audiobus)));
1687         items.push_back (MenuElem (_("Hide All Audio Busses"), sigc::mem_fun(*this, &Mixer_UI::hide_all_audiobus)));
1688         items.push_back (MenuElem (_("Show All Midi Tracks"), sigc::mem_fun (*this, &Mixer_UI::show_all_miditracks)));
1689         items.push_back (MenuElem (_("Hide All Midi Tracks"), sigc::mem_fun (*this, &Mixer_UI::hide_all_miditracks)));
1690
1691 }
1692
1693 void
1694 Mixer_UI::stripable_property_changed (const PropertyChange& what_changed, boost::weak_ptr<Stripable> ws)
1695 {
1696         if (!what_changed.contains (ARDOUR::Properties::hidden) && !what_changed.contains (ARDOUR::Properties::name)) {
1697                 return;
1698         }
1699
1700         boost::shared_ptr<Stripable> s = ws.lock ();
1701
1702         if (!s) {
1703                 return;
1704         }
1705
1706         TreeModel::Children rows = track_model->children();
1707         TreeModel::Children::iterator i;
1708
1709         for (i = rows.begin(); i != rows.end(); ++i) {
1710                 boost::shared_ptr<Stripable> ss = (*i)[stripable_columns.stripable];
1711
1712                 if (s == ss) {
1713
1714                         if (what_changed.contains (ARDOUR::Properties::name)) {
1715                                 (*i)[stripable_columns.text] = s->name();
1716                         }
1717
1718                         if (what_changed.contains (ARDOUR::Properties::hidden)) {
1719                                 (*i)[stripable_columns.visible] = !s->presentation_info().hidden();
1720                                 redisplay_track_list ();
1721                         }
1722
1723                         return;
1724                 }
1725         }
1726
1727         if (s->is_master ()) {
1728                 return;
1729         }
1730
1731         error << _("track display list item for renamed strip not found!") << endmsg;
1732 }
1733
1734 bool
1735 Mixer_UI::group_display_button_press (GdkEventButton* ev)
1736 {
1737         TreeModel::Path path;
1738         TreeViewColumn* column;
1739         int cellx;
1740         int celly;
1741
1742         if (!group_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
1743                 if (ev->button == 3) {
1744                         _group_tabs->get_menu(0)->popup (ev->button, ev->time);
1745                 }
1746                 return true;
1747         }
1748
1749         TreeIter iter = group_model->get_iter (path);
1750         if (!iter) {
1751                 if (ev->button == 3) {
1752                         _group_tabs->get_menu(0)->popup (ev->button, ev->time);
1753                 }
1754                 return true;
1755         }
1756
1757         RouteGroup* group = (*iter)[group_columns.group];
1758
1759         if (Keyboard::is_context_menu_event (ev)) {
1760                 _group_tabs->get_menu(group)->popup (1, ev->time);
1761                 return true;
1762         }
1763
1764         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
1765         case 1:
1766                 if (Keyboard::is_edit_event (ev)) {
1767                         if (group) {
1768                                 // edit_route_group (group);
1769 #ifdef __APPLE__
1770                                 group_display.queue_draw();
1771 #endif
1772                                 return true;
1773                         }
1774                 }
1775                 break;
1776
1777         case 0:
1778         {
1779                 bool visible = (*iter)[group_columns.visible];
1780                 (*iter)[group_columns.visible] = !visible;
1781 #ifdef __APPLE__
1782                 group_display.queue_draw();
1783 #endif
1784                 return true;
1785         }
1786
1787         default:
1788                 break;
1789         }
1790
1791         return false;
1792  }
1793
1794 void
1795 Mixer_UI::activate_all_route_groups ()
1796 {
1797         _session->foreach_route_group (sigc::bind (sigc::mem_fun (*this, &Mixer_UI::set_route_group_activation), true));
1798 }
1799
1800 void
1801 Mixer_UI::disable_all_route_groups ()
1802 {
1803         _session->foreach_route_group (sigc::bind (sigc::mem_fun (*this, &Mixer_UI::set_route_group_activation), false));
1804 }
1805
1806 void
1807 Mixer_UI::route_groups_changed ()
1808 {
1809         ENSURE_GUI_THREAD (*this, &Mixer_UI::route_groups_changed);
1810
1811         _in_group_rebuild_or_clear = true;
1812
1813         /* just rebuild the while thing */
1814
1815         group_model->clear ();
1816
1817 #if 0
1818         /* this is currently not used,
1819          * Mixer_UI::group_display_button_press() has a case for it,
1820          * and a commented edit_route_group() but that's n/a since 2011.
1821          *
1822          * This code is left as reminder that
1823          * row[group_columns.group] = 0 has special meaning.
1824          */
1825         {
1826                 TreeModel::Row row;
1827                 row = *(group_model->append());
1828                 row[group_columns.visible] = true;
1829                 row[group_columns.text] = (_("-all-"));
1830                 row[group_columns.group] = 0;
1831         }
1832 #endif
1833
1834         _session->foreach_route_group (sigc::mem_fun (*this, &Mixer_UI::add_route_group));
1835
1836         _group_tabs->set_dirty ();
1837         _in_group_rebuild_or_clear = false;
1838 }
1839
1840 void
1841 Mixer_UI::new_route_group ()
1842 {
1843         _group_tabs->run_new_group_dialog (0, false);
1844 }
1845
1846 void
1847 Mixer_UI::remove_selected_route_group ()
1848 {
1849         Glib::RefPtr<TreeSelection> selection = group_display.get_selection();
1850         TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
1851
1852         if (rows.empty()) {
1853                 return;
1854         }
1855
1856         TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
1857         TreeIter iter;
1858
1859         /* selection mode is single, so rows.begin() is it */
1860
1861         if ((iter = group_model->get_iter (*i))) {
1862
1863                 RouteGroup* rg = (*iter)[group_columns.group];
1864
1865                 if (rg) {
1866                         _session->remove_route_group (*rg);
1867                 }
1868         }
1869 }
1870
1871 void
1872 Mixer_UI::route_group_property_changed (RouteGroup* group, const PropertyChange& change)
1873 {
1874         if (in_group_row_change) {
1875                 return;
1876         }
1877
1878         /* force an update of any mixer strips that are using this group,
1879            otherwise mix group names don't change in mixer strips
1880         */
1881
1882         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
1883                 if ((*i)->route_group() == group) {
1884                         (*i)->route_group_changed();
1885                 }
1886         }
1887
1888         TreeModel::iterator i;
1889         TreeModel::Children rows = group_model->children();
1890         Glib::RefPtr<TreeSelection> selection = group_display.get_selection();
1891
1892         in_group_row_change = true;
1893
1894         for (i = rows.begin(); i != rows.end(); ++i) {
1895                 if ((*i)[group_columns.group] == group) {
1896                         (*i)[group_columns.visible] = !group->is_hidden ();
1897                         (*i)[group_columns.text] = group->name ();
1898                         break;
1899                 }
1900         }
1901
1902         in_group_row_change = false;
1903
1904         if (change.contains (Properties::name)) {
1905                 _group_tabs->set_dirty ();
1906         }
1907
1908         for (list<MixerStrip*>::iterator j = strips.begin(); j != strips.end(); ++j) {
1909                 if ((*j)->route_group() == group) {
1910                         if (group->is_hidden ()) {
1911                                 hide_strip (*j);
1912                         } else {
1913                                 show_strip (*j);
1914                         }
1915                 }
1916         }
1917 }
1918
1919 void
1920 Mixer_UI::show_mixer_list (bool yn)
1921 {
1922         if (yn) {
1923                 list_vpacker.show ();
1924         } else {
1925                 list_vpacker.hide ();
1926         }
1927
1928         _show_mixer_list = yn;
1929 }
1930
1931 void
1932 Mixer_UI::show_monitor_section (bool yn)
1933 {
1934         if (!monitor_section()) {
1935                 return;
1936         }
1937         if (monitor_section()->tearoff().torn_off()) {
1938                 return;
1939         }
1940
1941         if (yn) {
1942                 monitor_section()->tearoff().show();
1943         } else {
1944                 monitor_section()->tearoff().hide();
1945         }
1946 }
1947
1948 void
1949 Mixer_UI::route_group_name_edit (const std::string& path, const std::string& new_text)
1950 {
1951         RouteGroup* group;
1952         TreeIter iter;
1953
1954         if ((iter = group_model->get_iter (path))) {
1955
1956                 if ((group = (*iter)[group_columns.group]) == 0) {
1957                         return;
1958                 }
1959
1960                 if (new_text != group->name()) {
1961                         group->set_name (new_text);
1962                 }
1963         }
1964 }
1965
1966 void
1967 Mixer_UI::route_group_row_change (const Gtk::TreeModel::Path&, const Gtk::TreeModel::iterator& iter)
1968 {
1969         RouteGroup* group;
1970
1971         if (in_group_row_change) {
1972                 return;
1973         }
1974
1975         if ((group = (*iter)[group_columns.group]) == 0) {
1976                 return;
1977         }
1978
1979         std::string name = (*iter)[group_columns.text];
1980
1981         if (name != group->name()) {
1982                 group->set_name (name);
1983         }
1984
1985         bool hidden = !(*iter)[group_columns.visible];
1986
1987         if (hidden != group->is_hidden ()) {
1988                 group->set_hidden (hidden, this);
1989         }
1990 }
1991
1992 /** Called when a group model row is deleted, but also when the model is
1993  *  reordered by a user drag-and-drop; the latter is what we are
1994  *  interested in here.
1995  */
1996 void
1997 Mixer_UI::route_group_row_deleted (Gtk::TreeModel::Path const &)
1998 {
1999         if (_in_group_rebuild_or_clear) {
2000                 return;
2001         }
2002
2003         /* Re-write the session's route group list so that the new order is preserved */
2004
2005         list<RouteGroup*> new_list;
2006
2007         Gtk::TreeModel::Children children = group_model->children();
2008         for (Gtk::TreeModel::Children::iterator i = children.begin(); i != children.end(); ++i) {
2009                 RouteGroup* g = (*i)[group_columns.group];
2010                 if (g) {
2011                         new_list.push_back (g);
2012                 }
2013         }
2014
2015         _session->reorder_route_groups (new_list);
2016 }
2017
2018
2019 void
2020 Mixer_UI::add_route_group (RouteGroup* group)
2021 {
2022         ENSURE_GUI_THREAD (*this, &Mixer_UI::add_route_group, group)
2023         bool focus = false;
2024
2025         in_group_row_change = true;
2026
2027         TreeModel::Row row = *(group_model->append());
2028         row[group_columns.visible] = !group->is_hidden ();
2029         row[group_columns.group] = group;
2030         if (!group->name().empty()) {
2031                 row[group_columns.text] = group->name();
2032         } else {
2033                 row[group_columns.text] = _("unnamed");
2034                 focus = true;
2035         }
2036
2037         group->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::route_group_property_changed, this, group, _1), gui_context());
2038
2039         if (focus) {
2040                 TreeViewColumn* col = group_display.get_column (0);
2041                 CellRendererText* name_cell = dynamic_cast<CellRendererText*>(group_display.get_column_cell_renderer (1));
2042                 group_display.set_cursor (group_model->get_path (row), *col, *name_cell, true);
2043         }
2044
2045         _group_tabs->set_dirty ();
2046
2047         in_group_row_change = false;
2048 }
2049
2050 bool
2051 Mixer_UI::strip_scroller_button_release (GdkEventButton* ev)
2052 {
2053         using namespace Menu_Helpers;
2054
2055         if (Keyboard::is_context_menu_event (ev)) {
2056                 ARDOUR_UI::instance()->add_route ();
2057                 return true;
2058         }
2059
2060         return false;
2061 }
2062
2063 void
2064 Mixer_UI::scroller_drag_data_received (const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, const Gtk::SelectionData& data, guint info, guint time)
2065 {
2066         if (data.get_target() != "PluginFavoritePtr") {
2067                 context->drag_finish (false, false, time);
2068                 return;
2069         }
2070
2071         const void * d = data.get_data();
2072         const Gtkmm2ext::DnDTreeView<ARDOUR::PluginPresetPtr>* tv = reinterpret_cast<const Gtkmm2ext::DnDTreeView<ARDOUR::PluginPresetPtr>*>(d);
2073
2074         PluginPresetList nfos;
2075         TreeView* source;
2076         tv->get_object_drag_data (nfos, &source);
2077
2078         Route::ProcessorList pl;
2079         bool ok = false;
2080
2081         for (list<PluginPresetPtr>::const_iterator i = nfos.begin(); i != nfos.end(); ++i) {
2082                 PluginPresetPtr ppp = (*i);
2083                 PluginInfoPtr pip = ppp->_pip;
2084                 if (!pip->is_instrument ()) {
2085                         continue;
2086                 }
2087                 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);
2088                 ok = true;
2089         }
2090
2091         context->drag_finish (ok, false, time);
2092 }
2093
2094 void
2095 Mixer_UI::set_strip_width (Width w, bool save)
2096 {
2097         _strip_width = w;
2098
2099         for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
2100                 (*i)->set_width_enum (w, save ? (*i)->width_owner() : this);
2101         }
2102 }
2103
2104
2105 struct PluginStateSorter {
2106 public:
2107         bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
2108                 std::list<std::string>::const_iterator aiter = std::find(_user.begin(), _user.end(), (*a).unique_id);
2109                 std::list<std::string>::const_iterator biter = std::find(_user.begin(), _user.end(), (*b).unique_id);
2110                 if (aiter != _user.end() && biter != _user.end()) {
2111                         return std::distance (_user.begin(), aiter)  < std::distance (_user.begin(), biter);
2112                 }
2113                 if (aiter != _user.end()) {
2114                         return true;
2115                 }
2116                 if (biter != _user.end()) {
2117                         return false;
2118                 }
2119                 return ARDOUR::cmp_nocase((*a).name, (*b).name) == -1;
2120         }
2121
2122         PluginStateSorter(std::list<std::string> user) : _user (user)  {}
2123 private:
2124         std::list<std::string> _user;
2125 };
2126
2127 int
2128 Mixer_UI::set_state (const XMLNode& node, int version)
2129 {
2130         bool yn;
2131
2132         Tabbable::set_state (node, version);
2133
2134         if (node.get_property ("narrow-strips", yn)) {
2135                 if (yn) {
2136                         set_strip_width (Narrow);
2137                 } else {
2138                         set_strip_width (Wide);
2139                 }
2140         }
2141
2142         node.get_property ("show-mixer", _visible);
2143
2144         if (node.get_property ("maximised", yn)) {
2145                 Glib::RefPtr<Action> act = ActionManager::get_action (X_("Common"), X_("ToggleMaximalMixer"));
2146                 assert (act);
2147                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2148                 bool fs = tact && tact->get_active();
2149                 if (yn ^ fs) {
2150                         ActionManager::do_action ("Common", "ToggleMaximalMixer");
2151                 }
2152         }
2153
2154         if (node.get_property ("show-mixer-list", yn)) {
2155                 Glib::RefPtr<Action> act = ActionManager::get_action (X_("Common"), X_("ToggleMixerList"));
2156                 assert (act);
2157                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2158
2159                 /* do it twice to force the change */
2160                 tact->set_active (!yn);
2161                 tact->set_active (yn);
2162         }
2163
2164         if (node.get_property ("monitor-section-visible", yn)) {
2165                 Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2166                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2167                 /* do it twice to force the change */
2168                 tact->set_active (yn);
2169                 show_monitor_section (yn);
2170         }
2171
2172         //check for the user's plugin_order file
2173         XMLNode plugin_order_new(X_("PO"));
2174         if (PluginManager::instance().load_plugin_order_file(plugin_order_new)) {
2175                 store_current_favorite_order ();
2176                 std::list<string> order;
2177                 const XMLNodeList& kids = plugin_order_new.children("PluginInfo");
2178                 XMLNodeConstIterator i;
2179                 for (i = kids.begin(); i != kids.end(); ++i) {
2180                         std::string unique_id;
2181                         if ((*i)->get_property ("unique-id", unique_id)) {
2182                                 order.push_back (unique_id);
2183                                 if ((*i)->get_property ("expanded", yn)) {
2184                                         favorite_ui_state[unique_id] = yn;
2185                                 }
2186                         }
2187                 }
2188                 PluginStateSorter cmp (order);
2189                 favorite_order.sort (cmp);
2190                 sync_treeview_from_favorite_order ();
2191
2192         } else {
2193                 //if there is no user file, then use an existing one from instant.xml
2194                 //NOTE: if you are loading an old session, this might come from the session's instant.xml
2195                 //Todo:  in the next major version, we should probably stop doing the instant.xml check, and just use the new file
2196                 XMLNode* plugin_order;
2197                 if ((plugin_order = find_named_node (node, "PluginOrder")) != 0) {
2198                         store_current_favorite_order ();
2199                         std::list<string> order;
2200                         const XMLNodeList& kids = plugin_order->children("PluginInfo");
2201                         XMLNodeConstIterator i;
2202                         for (i = kids.begin(); i != kids.end(); ++i) {
2203                                 std::string unique_id;
2204                                 if ((*i)->get_property ("unique-id", unique_id)) {
2205                                         order.push_back (unique_id);
2206                                         if ((*i)->get_property ("expanded", yn)) {
2207                                                 favorite_ui_state[unique_id] = yn;
2208                                         }
2209                                 }
2210                         }
2211
2212                         PluginStateSorter cmp (order);
2213                         favorite_order.sort (cmp);
2214                         sync_treeview_from_favorite_order ();
2215                 }
2216         }
2217
2218         return 0;
2219 }
2220
2221 void
2222 Mixer_UI::save_plugin_order_file ()
2223 {
2224         //this writes the plugin order to the user's preference file ( plugin_metadata/plugin_order )
2225
2226         //NOTE:  this replaces the old code that stores info in instant.xml
2227         //why?  because instant.xml prefers the per-session settings, and we want this to be a global pref
2228
2229         store_current_favorite_order ();
2230         XMLNode plugin_order ("PluginOrder");
2231         uint32_t cnt = 0;
2232         for (PluginInfoList::const_iterator i = favorite_order.begin(); i != favorite_order.end(); ++i, ++cnt) {
2233                 XMLNode* p = new XMLNode ("PluginInfo");
2234                 p->set_property ("sort", cnt);
2235                 p->set_property ("unique-id", (*i)->unique_id);
2236                 if (favorite_ui_state.find ((*i)->unique_id) != favorite_ui_state.end ()) {
2237                         p->set_property ("expanded", favorite_ui_state[(*i)->unique_id]);
2238                 }
2239                 plugin_order.add_child_nocopy (*p);
2240         }
2241         PluginManager::instance().save_plugin_order_file( plugin_order );
2242 }
2243
2244 XMLNode&
2245 Mixer_UI::get_state ()
2246 {
2247         XMLNode* node = new XMLNode (X_("Mixer"));
2248
2249         node->add_child_nocopy (Tabbable::get_state());
2250
2251         node->set_property (X_("mixer-rhs-pane1-pos"), rhs_pane1.get_divider());
2252         node->set_property (X_("mixer-rhs_pane2-pos"), rhs_pane2.get_divider());
2253         node->set_property (X_("mixer-list-hpane-pos"), list_hpane.get_divider());
2254         node->set_property (X_("mixer-inner-pane-pos"),  inner_pane.get_divider());
2255
2256         node->set_property ("narrow-strips", (_strip_width == Narrow));
2257         node->set_property ("show-mixer", _visible);
2258         node->set_property ("show-mixer-list", _show_mixer_list);
2259         node->set_property ("maximised", _maximised);
2260
2261         Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2262         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2263         assert (tact);
2264         node->set_property ("monitor-section-visible", tact->get_active ());
2265
2266         return *node;
2267 }
2268
2269 void
2270 Mixer_UI::scroll_left ()
2271 {
2272         if (!scroller.get_hscrollbar()) return;
2273         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
2274         int sc_w = scroller.get_width();
2275         int sp_w = strip_packer.get_width();
2276         if (sp_w <= sc_w) {
2277                 return;
2278         }
2279         int lp = adj->get_value();
2280         int lm = 0;
2281         using namespace Gtk::Box_Helpers;
2282         const BoxList& strips = strip_packer.children();
2283         for (BoxList::const_iterator i = strips.begin(); i != strips.end(); ++i) {
2284                 if (i->get_widget() == & add_button) {
2285                         continue;
2286                 }
2287 #ifdef MIXBUS
2288                 if (i->get_widget() == &mb_shadow) {
2289                         continue;
2290                 }
2291 #endif
2292                 lm += i->get_widget()->get_width ();
2293                 if (lm >= lp) {
2294                         lm -= i->get_widget()->get_width ();
2295                         break;
2296                 }
2297         }
2298         scroller.get_hscrollbar()->set_value (max (adj->get_lower(), min (adj->get_upper(), lm - 1.0)));
2299 }
2300
2301 void
2302 Mixer_UI::scroll_right ()
2303 {
2304         if (!scroller.get_hscrollbar()) return;
2305         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
2306         int sc_w = scroller.get_width();
2307         int sp_w = strip_packer.get_width();
2308         if (sp_w <= sc_w) {
2309                 return;
2310         }
2311         int lp = adj->get_value();
2312         int lm = 0;
2313         using namespace Gtk::Box_Helpers;
2314         const BoxList& strips = strip_packer.children();
2315         for (BoxList::const_iterator i = strips.begin(); i != strips.end(); ++i) {
2316                 if (i->get_widget() == & add_button) {
2317                         continue;
2318                 }
2319 #ifdef MIXBUS
2320                 if (i->get_widget() == &mb_shadow) {
2321                         continue;
2322                 }
2323 #endif
2324                 lm += i->get_widget()->get_width ();
2325                 if (lm > lp + 1) {
2326                         break;
2327                 }
2328         }
2329         scroller.get_hscrollbar()->set_value (max (adj->get_lower(), min (adj->get_upper(), lm - 1.0)));
2330 }
2331
2332 bool
2333 Mixer_UI::on_scroll_event (GdkEventScroll* ev)
2334 {
2335         switch (ev->direction) {
2336         case GDK_SCROLL_LEFT:
2337                 scroll_left ();
2338                 return true;
2339         case GDK_SCROLL_UP:
2340                 if (ev->state & Keyboard::TertiaryModifier) {
2341                         scroll_left ();
2342                         return true;
2343                 }
2344                 return false;
2345
2346         case GDK_SCROLL_RIGHT:
2347                 scroll_right ();
2348                 return true;
2349
2350         case GDK_SCROLL_DOWN:
2351                 if (ev->state & Keyboard::TertiaryModifier) {
2352                         scroll_right ();
2353                         return true;
2354                 }
2355                 return false;
2356         }
2357
2358         return false;
2359 }
2360
2361
2362 void
2363 Mixer_UI::parameter_changed (string const & p)
2364 {
2365         if (p == "show-group-tabs") {
2366                 bool const s = _session->config.get_show_group_tabs ();
2367                 if (s) {
2368                         _group_tabs->show ();
2369                 } else {
2370                         _group_tabs->hide ();
2371                 }
2372         } else if (p == "default-narrow_ms") {
2373                 bool const s = UIConfiguration::instance().get_default_narrow_ms ();
2374                 for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
2375                         (*i)->set_width_enum (s ? Narrow : Wide, this);
2376                 }
2377         }
2378 }
2379
2380 void
2381 Mixer_UI::set_route_group_activation (RouteGroup* g, bool a)
2382 {
2383         g->set_active (a, this);
2384 }
2385
2386 PluginSelector*
2387 Mixer_UI::plugin_selector()
2388 {
2389 #ifdef DEFER_PLUGIN_SELECTOR_LOAD
2390         if (!_plugin_selector)
2391                 _plugin_selector = new PluginSelector (PluginManager::instance());
2392 #endif
2393
2394         return _plugin_selector;
2395 }
2396
2397 void
2398 Mixer_UI::setup_track_display ()
2399 {
2400         track_model = ListStore::create (stripable_columns);
2401         track_display.set_model (track_model);
2402         track_display.append_column (_("Show"), stripable_columns.visible);
2403         track_display.append_column (_("Strips"), stripable_columns.text);
2404         track_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
2405         track_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
2406         track_display.get_column (0)->set_expand(false);
2407         track_display.get_column (1)->set_expand(true);
2408         track_display.get_column (1)->set_sizing (Gtk::TREE_VIEW_COLUMN_FIXED);
2409         track_display.set_name (X_("EditGroupList"));
2410         track_display.get_selection()->set_mode (Gtk::SELECTION_NONE);
2411         track_display.set_reorderable (true);
2412         track_display.set_headers_visible (true);
2413         track_display.set_can_focus(false);
2414
2415         track_model->signal_row_deleted().connect (sigc::mem_fun (*this, &Mixer_UI::track_list_delete));
2416         track_model->signal_rows_reordered().connect (sigc::mem_fun (*this, &Mixer_UI::track_list_reorder));
2417
2418         CellRendererToggle* track_list_visible_cell = dynamic_cast<CellRendererToggle*>(track_display.get_column_cell_renderer (0));
2419         track_list_visible_cell->property_activatable() = true;
2420         track_list_visible_cell->property_radio() = false;
2421         track_list_visible_cell->signal_toggled().connect (sigc::mem_fun (*this, &Mixer_UI::track_visibility_changed));
2422
2423         track_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::track_display_button_press), false);
2424
2425         track_display_scroller.add (track_display);
2426         track_display_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
2427
2428         VBox* v = manage (new VBox);
2429         v->show ();
2430         v->pack_start (track_display_scroller, true, true);
2431
2432         track_display_frame.set_name("BaseFrame");
2433         track_display_frame.set_shadow_type (Gtk::SHADOW_IN);
2434         track_display_frame.add (*v);
2435
2436         track_display_scroller.show();
2437         track_display_frame.show();
2438         track_display.show();
2439 }
2440
2441 void
2442 Mixer_UI::new_track_or_bus ()
2443 {
2444         ARDOUR_UI::instance()->add_route ();
2445 }
2446
2447 void
2448 Mixer_UI::update_title ()
2449 {
2450         if (!own_window()) {
2451                 return;
2452         }
2453
2454         if (_session) {
2455                 string n;
2456
2457                 if (_session->snap_name() != _session->name()) {
2458                         n = _session->snap_name ();
2459                 } else {
2460                         n = _session->name ();
2461                 }
2462
2463                 if (_session->dirty ()) {
2464                         n = "*" + n;
2465                 }
2466
2467                 WindowTitle title (n);
2468                 title += S_("Window|Mixer");
2469                 title += Glib::get_application_name ();
2470                 own_window()->set_title (title.get_string());
2471
2472         } else {
2473
2474                 WindowTitle title (S_("Window|Mixer"));
2475                 title += Glib::get_application_name ();
2476                 own_window()->set_title (title.get_string());
2477         }
2478 }
2479
2480 MixerStrip*
2481 Mixer_UI::strip_by_x (int x)
2482 {
2483         for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
2484                 int x1, x2, y;
2485
2486                 (*i)->translate_coordinates (_content, 0, 0, x1, y);
2487                 x2 = x1 + (*i)->get_width();
2488
2489                 if (x >= x1 && x <= x2) {
2490                         return (*i);
2491                 }
2492         }
2493
2494         return 0;
2495 }
2496
2497 void
2498 Mixer_UI::set_axis_targets_for_operation ()
2499 {
2500         _axis_targets.clear ();
2501
2502         if (!_selection.empty()) {
2503                 _axis_targets = _selection.axes;
2504                 return;
2505         }
2506
2507 //  removed "implicit" selections of strips, after discussion on IRC
2508
2509 }
2510
2511 void
2512 Mixer_UI::monitor_section_going_away ()
2513 {
2514         if (_monitor_section) {
2515                 XMLNode* ui_node = Config->extra_xml(X_("UI"));
2516                 /* immediate state save.
2517                  *
2518                  * Tearoff settings are otherwise only stored during
2519                  * save_ardour_state(). The mon-section may or may not
2520                  * exist at that point.
2521                  * */
2522                 if (ui_node) {
2523                         XMLNode* tearoff_node = ui_node->child (X_("Tearoffs"));
2524                         if (tearoff_node) {
2525                                 tearoff_node->remove_nodes_and_delete (X_("monitor-section"));
2526                                 XMLNode* t = new XMLNode (X_("monitor-section"));
2527                                 _monitor_section->tearoff().add_state (*t);
2528                                 tearoff_node->add_child_nocopy (*t);
2529                         }
2530                 }
2531                 monitor_section_detached ();
2532                 out_packer.remove (_monitor_section->tearoff());
2533                 _monitor_section->set_session (0);
2534                 delete _monitor_section;
2535                 _monitor_section = 0;
2536         }
2537 }
2538
2539 void
2540 Mixer_UI::toggle_midi_input_active (bool flip_others)
2541 {
2542         boost::shared_ptr<RouteList> rl (new RouteList);
2543         bool onoff = false;
2544
2545         set_axis_targets_for_operation ();
2546
2547         for (AxisViewSelection::iterator r = _axis_targets.begin(); r != _axis_targets.end(); ++r) {
2548                 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> ((*r)->stripable());
2549
2550                 if (mt) {
2551                         rl->push_back (mt);
2552                         onoff = !mt->input_active();
2553                 }
2554         }
2555
2556         _session->set_exclusive_input_active (rl, onoff, flip_others);
2557 }
2558
2559 void
2560 Mixer_UI::maximise_mixer_space ()
2561 {
2562         if (!own_window()) {
2563                 return;
2564         }
2565
2566         if (_maximised) {
2567                 return;
2568         }
2569
2570         _window->fullscreen ();
2571         _maximised = true;
2572 }
2573
2574 void
2575 Mixer_UI::restore_mixer_space ()
2576 {
2577         if (!own_window()) {
2578                 return;
2579         }
2580
2581         if (!_maximised) {
2582                 return;
2583         }
2584
2585         own_window()->unfullscreen();
2586         _maximised = false;
2587 }
2588
2589 void
2590 Mixer_UI::monitor_section_attached ()
2591 {
2592         Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2593         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2594         act->set_sensitive (true);
2595         show_monitor_section (tact->get_active ());
2596 }
2597
2598 void
2599 Mixer_UI::monitor_section_detached ()
2600 {
2601         Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2602         act->set_sensitive (false);
2603 }
2604
2605 void
2606 Mixer_UI::store_current_favorite_order ()
2607 {
2608         typedef Gtk::TreeModel::Children type_children;
2609         type_children children = favorite_plugins_model->children();
2610         favorite_order.clear();
2611         for(type_children::iterator iter = children.begin(); iter != children.end(); ++iter)
2612         {
2613                 Gtk::TreeModel::Row row = *iter;
2614                 ARDOUR::PluginPresetPtr ppp = row[favorite_plugins_columns.plugin];
2615                 favorite_order.push_back (ppp->_pip);
2616                 std::string name = row[favorite_plugins_columns.name];
2617                 favorite_ui_state[(*ppp->_pip).unique_id] = favorite_plugins_display.row_expanded (favorite_plugins_model->get_path(iter));
2618         }
2619 }
2620
2621 void
2622 Mixer_UI::save_favorite_ui_state (const TreeModel::iterator& iter, const TreeModel::Path& path)
2623 {
2624         Gtk::TreeModel::Row row = *iter;
2625         ARDOUR::PluginPresetPtr ppp = row[favorite_plugins_columns.plugin];
2626         assert (ppp);
2627         favorite_ui_state[(*ppp->_pip).unique_id] = favorite_plugins_display.row_expanded (favorite_plugins_model->get_path(iter));
2628 }
2629
2630 void
2631 Mixer_UI::refiller (PluginInfoList& result, const PluginInfoList& plugs)
2632 {
2633         PluginManager& manager (PluginManager::instance());
2634         for (PluginInfoList::const_iterator i = plugs.begin(); i != plugs.end(); ++i) {
2635
2636                 /* not a Favorite? skip it */
2637                 if (manager.get_status (*i) != PluginManager::Favorite) {
2638                         continue;
2639                 }
2640
2641                 /* Check the tag combo selection, and skip this plugin if it doesn't match the selected tag(s) */
2642                 string test = favorite_plugins_tag_combo.get_active_text();
2643                 if (test != _("Show All")) {
2644                         vector<string> tags = manager.get_tags(*i);
2645
2646                         //does the selected tag match any of the tags in the plugin?
2647                         vector<string>::iterator tt =  find (tags.begin(), tags.end(), test);
2648                         if (tt == tags.end()) {
2649                                 continue;
2650                         }
2651                 }
2652
2653                 result.push_back (*i);
2654         }
2655 }
2656
2657 struct PluginCustomSorter {
2658 public:
2659         bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
2660                 PluginInfoList::const_iterator aiter = _user.begin();
2661                 PluginInfoList::const_iterator biter = _user.begin();
2662                 while (aiter != _user.end()) { if ((*aiter)->unique_id == a->unique_id) { break; } ++aiter; }
2663                 while (biter != _user.end()) { if ((*biter)->unique_id == b->unique_id) { break; } ++biter; }
2664
2665                 if (aiter != _user.end() && biter != _user.end()) {
2666                         return std::distance (_user.begin(), aiter) < std::distance (_user.begin(), biter);
2667                 }
2668                 if (aiter != _user.end()) {
2669                         return true;
2670                 }
2671                 if (biter != _user.end()) {
2672                         return false;
2673                 }
2674                 return ARDOUR::cmp_nocase((*a).name, (*b).name) == -1;
2675         }
2676         PluginCustomSorter(PluginInfoList user) : _user (user)  {}
2677 private:
2678         PluginInfoList _user;
2679 };
2680
2681 void
2682 Mixer_UI::refill_favorite_plugins ()
2683 {
2684         PluginInfoList plugs;
2685         PluginManager& mgr (PluginManager::instance());
2686
2687 #ifdef LV2_SUPPORT
2688         refiller (plugs, mgr.lv2_plugin_info ());
2689 #endif
2690 #ifdef WINDOWS_VST_SUPPORT
2691         refiller (plugs, mgr.windows_vst_plugin_info ());
2692 #endif
2693 #ifdef LXVST_SUPPORT
2694         refiller (plugs, mgr.lxvst_plugin_info ());
2695 #endif
2696 #ifdef MACVST_SUPPORT
2697         refiller (plugs, mgr.mac_vst_plugin_info ());
2698 #endif
2699 #ifdef AUDIOUNIT_SUPPORT
2700         refiller (plugs, mgr.au_plugin_info ());
2701 #endif
2702         refiller (plugs, mgr.ladspa_plugin_info ());
2703         refiller (plugs, mgr.lua_plugin_info ());
2704
2705         store_current_favorite_order ();
2706
2707         PluginCustomSorter cmp (favorite_order);
2708         plugs.sort (cmp);
2709
2710         favorite_order = plugs;
2711
2712         sync_treeview_from_favorite_order ();
2713 }
2714
2715 void
2716 Mixer_UI::plugin_list_changed ()
2717 {
2718         refill_favorite_plugins();
2719         refill_tag_combo();
2720 }
2721
2722 void
2723 Mixer_UI::refill_tag_combo ()
2724 {
2725         PluginManager& mgr (PluginManager::instance());
2726
2727         std::vector<std::string> tags = mgr.get_all_tags (PluginManager::OnlyFavorites);
2728
2729         favorite_plugins_tag_combo.clear();
2730         favorite_plugins_tag_combo.append_text (_("Show All"));
2731
2732         for (vector<string>::iterator t = tags.begin (); t != tags.end (); ++t) {
2733                 favorite_plugins_tag_combo.append_text (*t);
2734         }
2735
2736         favorite_plugins_tag_combo.set_active_text (_("Show All"));
2737 }
2738
2739 void
2740 Mixer_UI::sync_treeview_favorite_ui_state (const TreeModel::Path& path, const TreeModel::iterator&)
2741 {
2742         TreeIter iter;
2743         if (!(iter = favorite_plugins_model->get_iter (path))) {
2744                 return;
2745         }
2746         ARDOUR::PluginPresetPtr ppp = (*iter)[favorite_plugins_columns.plugin];
2747         if (!ppp) {
2748                 return;
2749         }
2750         PluginInfoPtr pip = ppp->_pip;
2751         if (favorite_ui_state.find (pip->unique_id) != favorite_ui_state.end ()) {
2752                 if (favorite_ui_state[pip->unique_id]) {
2753                         favorite_plugins_display.expand_row (path, true);
2754                 }
2755         }
2756 }
2757
2758 void
2759 Mixer_UI::sync_treeview_from_favorite_order ()
2760 {
2761         favorite_plugins_model->clear ();
2762         for (PluginInfoList::const_iterator i = favorite_order.begin(); i != favorite_order.end(); ++i) {
2763                 PluginInfoPtr pip = (*i);
2764
2765                 TreeModel::Row newrow = *(favorite_plugins_model->append());
2766                 newrow[favorite_plugins_columns.name] = (*i)->name;
2767                 newrow[favorite_plugins_columns.plugin] = PluginPresetPtr (new PluginPreset(pip));
2768                 if (!_session) {
2769                         continue;
2770                 }
2771
2772                 vector<ARDOUR::Plugin::PresetRecord> presets = (*i)->get_presets (true);
2773                 for (vector<ARDOUR::Plugin::PresetRecord>::const_iterator j = presets.begin(); j != presets.end(); ++j) {
2774                         Gtk::TreeModel::Row child_row = *(favorite_plugins_model->append (newrow.children()));
2775                         child_row[favorite_plugins_columns.name] = (*j).label;
2776                         child_row[favorite_plugins_columns.plugin] = PluginPresetPtr (new PluginPreset(pip, &(*j)));
2777                 }
2778                 if (favorite_ui_state.find (pip->unique_id) != favorite_ui_state.end ()) {
2779                         if (favorite_ui_state[pip->unique_id]) {
2780                                 favorite_plugins_display.expand_row (favorite_plugins_model->get_path(newrow), true);
2781                         }
2782                 }
2783         }
2784 }
2785
2786 void
2787 Mixer_UI::popup_note_context_menu (GdkEventButton *ev)
2788 {
2789         using namespace Gtk::Menu_Helpers;
2790
2791         Gtk::Menu* m = manage (new Menu);
2792         MenuList& items = m->items ();
2793
2794         if (_selection.axes.empty()) {
2795                 items.push_back (MenuElem (_("No Track/Bus is selected.")));
2796         } else {
2797                 items.push_back (MenuElem (_("Add at the top"),
2798                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddTop)));
2799                 items.push_back (MenuElem (_("Add Pre-Fader"),
2800                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddPreFader)));
2801                 items.push_back (MenuElem (_("Add Post-Fader"),
2802                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddPostFader)));
2803                 items.push_back (MenuElem (_("Add at the end"),
2804                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddBottom)));
2805         }
2806
2807         items.push_back (SeparatorElem());
2808
2809         items.push_back (MenuElem (_("Remove from favorites"), sigc::mem_fun (*this, &Mixer_UI::remove_selected_from_favorites)));
2810
2811         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2812         if (ppp && ppp->_preset.valid && ppp->_preset.user) {
2813                 // we cannot currently delete AU presets
2814                 if (!ppp->_pip || ppp->_pip->type != AudioUnit) {
2815                         items.push_back (MenuElem (_("Delete Preset"), sigc::mem_fun (*this, &Mixer_UI::delete_selected_preset)));
2816                 }
2817         }
2818
2819         m->popup (ev->button, ev->time);
2820 }
2821
2822 bool
2823 Mixer_UI::plugin_row_button_press (GdkEventButton *ev)
2824 {
2825         if ((ev->type == GDK_BUTTON_PRESS) && (ev->button == 3)) {
2826                 TreeModel::Path path;
2827                 TreeViewColumn* column;
2828                 int cellx, celly;
2829                 if (favorite_plugins_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
2830                         Glib::RefPtr<Gtk::TreeView::Selection> selection = favorite_plugins_display.get_selection();
2831                         if (selection) {
2832                                 selection->unselect_all();
2833                                 selection->select(path);
2834                         }
2835                 }
2836                 ARDOUR::PluginPresetPtr ppp = selected_plugin();
2837                 if (ppp) {
2838                         popup_note_context_menu (ev);
2839                 }
2840         }
2841         return false;
2842 }
2843
2844
2845 PluginPresetPtr
2846 Mixer_UI::selected_plugin ()
2847 {
2848         Glib::RefPtr<Gtk::TreeView::Selection> selection = favorite_plugins_display.get_selection();
2849         if (!selection) {
2850                 return PluginPresetPtr();
2851         }
2852         Gtk::TreeModel::iterator iter = selection->get_selected();
2853         if (!iter) {
2854                 return PluginPresetPtr();
2855         }
2856         return (*iter)[favorite_plugins_columns.plugin];
2857 }
2858
2859 void
2860 Mixer_UI::add_selected_processor (ProcessorPosition pos)
2861 {
2862         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2863         if (ppp) {
2864                 add_favorite_processor (ppp, pos);
2865         }
2866 }
2867
2868 void
2869 Mixer_UI::delete_selected_preset ()
2870 {
2871         if (!_session) {
2872                 return;
2873         }
2874         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2875         if (!ppp || !ppp->_preset.valid || !ppp->_preset.user) {
2876                 return;
2877         }
2878         PluginPtr plugin = ppp->_pip->load (*_session);
2879         plugin->get_presets();
2880         plugin->remove_preset (ppp->_preset.label);
2881 }
2882
2883 void
2884 Mixer_UI::remove_selected_from_favorites ()
2885 {
2886         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2887         if (!ppp) {
2888                 return;
2889         }
2890         PluginManager::PluginStatusType status = PluginManager::Normal;
2891         PluginManager& manager (PluginManager::instance());
2892
2893         manager.set_status (ppp->_pip->type, ppp->_pip->unique_id, status);
2894         manager.save_statuses ();
2895 }
2896
2897 void
2898 Mixer_UI::plugin_row_activated (const TreeModel::Path& path, TreeViewColumn* column)
2899 {
2900         TreeIter iter;
2901         if (!(iter = favorite_plugins_model->get_iter (path))) {
2902                 return;
2903         }
2904         ARDOUR::PluginPresetPtr ppp = (*iter)[favorite_plugins_columns.plugin];
2905         add_favorite_processor (ppp, AddPreFader); // TODO: preference?!
2906 }
2907
2908 void
2909 Mixer_UI::add_favorite_processor (ARDOUR::PluginPresetPtr ppp, ProcessorPosition pos)
2910 {
2911         if (!_session || _selection.axes.empty()) {
2912                 return;
2913         }
2914
2915         PluginInfoPtr pip = ppp->_pip;
2916         for (AxisViewSelection::iterator i = _selection.axes.begin(); i != _selection.axes.end(); ++i) {
2917                 boost::shared_ptr<ARDOUR::Route> rt = boost::dynamic_pointer_cast<ARDOUR::Route> ((*i)->stripable());
2918
2919                 if (!rt) {
2920                         continue;
2921                 }
2922
2923                 PluginPtr p = pip->load (*_session);
2924
2925                 if (!p) {
2926                         continue;
2927                 }
2928
2929                 if (ppp->_preset.valid) {
2930                         p->load_preset (ppp->_preset);
2931                 }
2932
2933                 Route::ProcessorStreams err;
2934                 boost::shared_ptr<Processor> processor (new PluginInsert (*_session, p));
2935
2936                 switch (pos) {
2937                         case AddTop:
2938                                 rt->add_processor_by_index (processor, 0, &err, Config->get_new_plugins_active ());
2939                                 break;
2940                         case AddPreFader:
2941                                 rt->add_processor (processor, PreFader, &err, Config->get_new_plugins_active ());
2942                                 break;
2943                         case AddPostFader:
2944                                 {
2945                                         int idx = 0;
2946                                         int pos = 0;
2947                                         for (;;++idx) {
2948                                                 boost::shared_ptr<Processor> np = rt->nth_processor (idx);
2949                                                 if (!np) {
2950                                                         break;
2951                                                 }
2952                                                 if (!np->display_to_user()) {
2953                                                         continue;
2954                                                 }
2955                                                 if (boost::dynamic_pointer_cast<Amp> (np) && // Fader, not Trim
2956                                                                 boost::dynamic_pointer_cast<Amp> (np)->gain_control()->parameter().type() == GainAutomation) {
2957                                                         break;
2958                                                 }
2959                                                 ++pos;
2960                                         }
2961                                         rt->add_processor_by_index (processor, ++pos, &err, Config->get_new_plugins_active ());
2962                                 }
2963                                 break;
2964                         case AddBottom:
2965                                 rt->add_processor_by_index (processor, -1, &err, Config->get_new_plugins_active ());
2966                                 break;
2967                 }
2968         }
2969 }
2970
2971 bool
2972 PluginTreeStore::row_drop_possible_vfunc(const Gtk::TreeModel::Path& dest, const Gtk::SelectionData& data) const
2973 {
2974         if (data.get_target() != "GTK_TREE_MODEL_ROW") {
2975                 return false;
2976         }
2977
2978         // only allow to re-order top-level items
2979         TreePath src;
2980         if (TreePath::get_from_selection_data (data, src)) {
2981                 if (src.up() && src.up()) {
2982                         return false;
2983                 }
2984         }
2985
2986         // don't allow to drop as child-rows.
2987         Gtk::TreeModel::Path _dest = dest; // un const
2988         const bool is_child = _dest.up (); // explicit bool for clang
2989         if (!is_child || _dest.empty ()) {
2990                 return true;
2991         }
2992         return false;
2993 }
2994
2995 void
2996 Mixer_UI::plugin_drop (const Glib::RefPtr<Gdk::DragContext>&, const Gtk::SelectionData& data)
2997 {
2998         if (data.get_target() != "PluginPresetPtr") {
2999                 return;
3000         }
3001         if (data.get_length() != sizeof (PluginPresetPtr)) {
3002                 return;
3003         }
3004         const void *d = data.get_data();
3005         const PluginPresetPtr ppp = *(static_cast<const PluginPresetPtr*> (d));
3006
3007         PluginManager::PluginStatusType status = PluginManager::Favorite;
3008         PluginManager& manager (PluginManager::instance());
3009
3010         manager.set_status (ppp->_pip->type, ppp->_pip->unique_id, status);
3011         manager.save_statuses ();
3012 }
3013
3014 void
3015 Mixer_UI::do_vca_assign (boost::shared_ptr<VCA> vca)
3016 {
3017         /* call protected MixerActor:: method */
3018         vca_assign (vca);
3019 }
3020
3021 void
3022 Mixer_UI::do_vca_unassign (boost::shared_ptr<VCA> vca)
3023 {
3024         /* call protected MixerActor:: method */
3025         vca_unassign (vca);
3026 }
3027
3028 void
3029 Mixer_UI::show_spill (boost::shared_ptr<Stripable> s)
3030 {
3031         boost::shared_ptr<Stripable> ss = spilled_strip.lock();
3032         if (ss != s) {
3033                 spilled_strip = s;
3034                 show_spill_change (s); /* EMIT SIGNAL */
3035                 if (s) {
3036                         _group_tabs->hide ();
3037                 } else {
3038                         _group_tabs->show ();
3039                 }
3040                 redisplay_track_list ();
3041         }
3042 }
3043
3044 bool
3045 Mixer_UI::showing_spill_for (boost::shared_ptr<Stripable> s) const
3046 {
3047         return s == spilled_strip.lock();
3048 }
3049
3050 void
3051 Mixer_UI::show_editor_window () const
3052 {
3053         PublicEditor::instance().make_visible ();
3054 }
3055
3056 void
3057 Mixer_UI::register_actions ()
3058 {
3059         Glib::RefPtr<ActionGroup> group = myactions.create_action_group (X_("Mixer"));
3060
3061         myactions.register_action (group, "show-editor", _("Show Editor"), sigc::mem_fun (*this, &Mixer_UI::show_editor_window));
3062
3063         myactions.register_action (group, "solo", _("Toggle Solo on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::solo_action));
3064         myactions.register_action (group, "mute", _("Toggle Mute on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::mute_action));
3065         myactions.register_action (group, "recenable", _("Toggle Rec-enable on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::rec_enable_action));
3066         myactions.register_action (group, "increment-gain", _("Decrease Gain on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::step_gain_up_action));
3067         myactions.register_action (group, "decrement-gain", _("Increase Gain on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::step_gain_down_action));
3068         myactions.register_action (group, "unity-gain", _("Set Gain to 0dB on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::unity_gain_action));
3069
3070
3071         myactions.register_action (group, "copy-processors", _("Copy Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::copy_processors));
3072         myactions.register_action (group, "cut-processors", _("Cut Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::cut_processors));
3073         myactions.register_action (group, "paste-processors", _("Paste Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::paste_processors));
3074         myactions.register_action (group, "delete-processors", _("Delete Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::delete_processors));
3075         myactions.register_action (group, "select-all-processors", _("Select All (visible) Processors"), sigc::mem_fun (*this, &Mixer_UI::select_all_processors));
3076         myactions.register_action (group, "toggle-processors", _("Toggle Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::toggle_processors));
3077         myactions.register_action (group, "ab-plugins", _("Toggle Selected Plugins"), sigc::mem_fun (*this, &Mixer_UI::ab_plugins));
3078         myactions.register_action (group, "select-none", _("Deselect all strips and processors"), sigc::mem_fun (*this, &Mixer_UI::select_none));
3079         myactions.register_action (group, "select-all-tracks", _("Select All Tracks"), sigc::mem_fun (*this, &Mixer_UI::select_all_tracks));
3080
3081         myactions.register_action (group, "scroll-left", _("Scroll Mixer Window to the left"), sigc::mem_fun (*this, &Mixer_UI::scroll_left));
3082         myactions.register_action (group, "scroll-right", _("Scroll Mixer Window to the right"), sigc::mem_fun (*this, &Mixer_UI::scroll_right));
3083
3084         myactions.register_action (group, "toggle-midi-input-active", _("Toggle MIDI Input Active for Mixer-Selected Tracks/Busses"),
3085                                    sigc::bind (sigc::mem_fun (*this, &Mixer_UI::toggle_midi_input_active), false));
3086 }
3087
3088 void
3089 Mixer_UI::load_bindings ()
3090 {
3091         bindings = Bindings::get_bindings (X_("Mixer"), myactions);
3092 }
3093
3094 template<class T> void
3095 Mixer_UI::control_action (boost::shared_ptr<T> (Stripable::*get_control)() const)
3096 {
3097         boost::shared_ptr<ControlList> cl (new ControlList);
3098         boost::shared_ptr<AutomationControl> ac;
3099         bool val = false;
3100         bool have_val = false;
3101
3102         set_axis_targets_for_operation ();
3103
3104         BOOST_FOREACH(AxisView* r, _axis_targets) {
3105                 boost::shared_ptr<Stripable> s = r->stripable();
3106                 if (s) {
3107                         ac = (s.get()->*get_control)();
3108                         if (ac) {
3109                                 cl->push_back (ac);
3110                                 if (!have_val) {
3111                                         val = !ac->get_value();
3112                                         have_val = true;
3113                                 }
3114                         }
3115                 }
3116         }
3117
3118         _session->set_controls (cl,  val, Controllable::UseGroup);
3119 }
3120
3121 void
3122 Mixer_UI::solo_action ()
3123 {
3124         control_action (&Stripable::solo_control);
3125 }
3126
3127 void
3128 Mixer_UI::mute_action ()
3129 {
3130         control_action (&Stripable::mute_control);
3131 }
3132
3133 void
3134 Mixer_UI::rec_enable_action ()
3135 {
3136         control_action (&Stripable::rec_enable_control);
3137 }
3138
3139 AutomationControlSet
3140 Mixer_UI::selected_gaincontrols ()
3141 {
3142         set_axis_targets_for_operation ();
3143         AutomationControlSet rv;
3144         BOOST_FOREACH(AxisView* r, _axis_targets) {
3145                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3146                 if (ms) {
3147                         boost::shared_ptr<GainControl> ac (ms->route()->gain_control());
3148                         ControlList cl (ac->grouped_controls());
3149                         for (ControlList::const_iterator c = cl.begin(); c != cl.end (); ++c) {
3150                                 rv.insert (*c);
3151                         }
3152                         rv.insert (ac);
3153                 }
3154         }
3155         return rv;
3156 }
3157
3158 void
3159 Mixer_UI::step_gain_up_action ()
3160 {
3161         AutomationControlSet acs = selected_gaincontrols ();
3162         for (AutomationControlSet::const_iterator i = acs.begin(); i != acs.end (); ++i) {
3163                 boost::shared_ptr<GainControl> ac = boost::dynamic_pointer_cast<GainControl> (*i);
3164                 assert (ac);
3165                 ac->set_value (dB_to_coefficient (accurate_coefficient_to_dB (ac->get_value()) + 0.1), Controllable::NoGroup);
3166         }
3167 }
3168
3169 void
3170 Mixer_UI::step_gain_down_action ()
3171 {
3172         AutomationControlSet acs = selected_gaincontrols ();
3173         for (AutomationControlSet::const_iterator i = acs.begin(); i != acs.end (); ++i) {
3174                 boost::shared_ptr<GainControl> ac = boost::dynamic_pointer_cast<GainControl> (*i);
3175                 assert (ac);
3176                 ac->set_value (dB_to_coefficient (accurate_coefficient_to_dB (ac->get_value()) - 0.1), Controllable::NoGroup);
3177         }
3178 }
3179
3180 void
3181 Mixer_UI::unity_gain_action ()
3182 {
3183         set_axis_targets_for_operation ();
3184
3185         BOOST_FOREACH(AxisView* r, _axis_targets) {
3186                 boost::shared_ptr<Stripable> s = r->stripable();
3187                 if (s) {
3188                         boost::shared_ptr<AutomationControl> ac = s->gain_control();
3189                         if (ac) {
3190                                 ac->set_value (1.0, Controllable::UseGroup);
3191                         }
3192                 }
3193         }
3194 }
3195
3196 void
3197 Mixer_UI::copy_processors ()
3198 {
3199         set_axis_targets_for_operation ();
3200
3201         BOOST_FOREACH(AxisView* r, _axis_targets) {
3202                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3203                 if (ms) {
3204                         ms->copy_processors ();
3205                 }
3206         }
3207 }
3208 void
3209 Mixer_UI::cut_processors ()
3210 {
3211         set_axis_targets_for_operation ();
3212
3213         BOOST_FOREACH(AxisView* r, _axis_targets) {
3214                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3215                 if (ms) {
3216                         ms->cut_processors ();
3217                 }
3218         }
3219 }
3220 void
3221 Mixer_UI::paste_processors ()
3222 {
3223         set_axis_targets_for_operation ();
3224
3225         BOOST_FOREACH(AxisView* r, _axis_targets) {
3226                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3227                 if (ms) {
3228                         ms->paste_processors ();
3229                 }
3230         }
3231 }
3232 void
3233 Mixer_UI::select_all_processors ()
3234 {
3235         set_axis_targets_for_operation ();
3236
3237         BOOST_FOREACH(AxisView* r, _axis_targets) {
3238                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3239                 if (ms) {
3240                         ms->select_all_processors ();
3241                 }
3242         }
3243 }
3244 void
3245 Mixer_UI::toggle_processors ()
3246 {
3247         set_axis_targets_for_operation ();
3248
3249         BOOST_FOREACH(AxisView* r, _axis_targets) {
3250                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3251                 if (ms) {
3252                         ms->toggle_processors ();
3253                 }
3254         }
3255 }
3256 void
3257 Mixer_UI::ab_plugins ()
3258 {
3259         set_axis_targets_for_operation ();
3260
3261         BOOST_FOREACH(AxisView* r, _axis_targets) {
3262                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3263                 if (ms) {
3264                         ms->ab_plugins ();
3265                 }
3266         }
3267 }
3268
3269 void
3270 Mixer_UI::vca_assign (boost::shared_ptr<VCA> vca)
3271 {
3272         set_axis_targets_for_operation ();
3273         BOOST_FOREACH(AxisView* r, _axis_targets) {
3274                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3275                 if (ms) {
3276                         ms->vca_assign (vca);
3277                 }
3278         }
3279 }
3280
3281 void
3282 Mixer_UI::vca_unassign (boost::shared_ptr<VCA> vca)
3283 {
3284         set_axis_targets_for_operation ();
3285         BOOST_FOREACH(AxisView* r, _axis_targets) {
3286                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3287                 if (ms) {
3288                         ms->vca_unassign (vca);
3289                 }
3290         }
3291 }