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