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