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