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