use new method in MidiPatchManager to use MIDNAM data when setting a MidiTimeAxisView
[ardour.git] / gtk2_ardour / editor_routes.cc
1 /*
2  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2009-2014 David Robillard <d@drobilla.net>
4  * Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2013-2015 Nick Mainsbridge <mainsbridge@gmail.com>
6  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
7  * Copyright (C) 2016-2018 Len Ovens <len@ovenwerks.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include <cstdlib>
25 #include <cassert>
26 #include <cmath>
27 #include <list>
28 #include <vector>
29 #include <algorithm>
30
31 #include "pbd/unknown_type.h"
32 #include "pbd/unwind.h"
33
34 #include "ardour/debug.h"
35 #include "ardour/audio_track.h"
36 #include "ardour/midi_track.h"
37 #include "ardour/route.h"
38 #include "ardour/selection.h"
39 #include "ardour/session.h"
40 #include "ardour/solo_isolate_control.h"
41 #include "ardour/utils.h"
42 #include "ardour/vca.h"
43 #include "ardour/vca_manager.h"
44
45 #include "gtkmm2ext/cell_renderer_pixbuf_multi.h"
46 #include "gtkmm2ext/cell_renderer_pixbuf_toggle.h"
47 #include "gtkmm2ext/treeutils.h"
48
49 #include "widgets/tooltips.h"
50
51 #include "actions.h"
52 #include "ardour_ui.h"
53 #include "audio_time_axis.h"
54 #include "editor.h"
55 #include "editor_group_tabs.h"
56 #include "editor_routes.h"
57 #include "gui_thread.h"
58 #include "keyboard.h"
59 #include "midi_time_axis.h"
60 #include "mixer_strip.h"
61 #include "plugin_setup_dialog.h"
62 #include "route_sorter.h"
63 #include "vca_time_axis.h"
64 #include "utils.h"
65
66 #include "pbd/i18n.h"
67
68 using namespace std;
69 using namespace ARDOUR;
70 using namespace ArdourWidgets;
71 using namespace ARDOUR_UI_UTILS;
72 using namespace PBD;
73 using namespace Gtk;
74 using namespace Gtkmm2ext;
75 using namespace Glib;
76 using Gtkmm2ext::Keyboard;
77
78 struct ColumnInfo {
79         int         index;
80         const char* label;
81         const char* tooltip;
82 };
83
84 EditorRoutes::EditorRoutes (Editor* e)
85         : EditorComponent (e)
86         , _ignore_reorder (false)
87         , _ignore_selection_change (false)
88         , _no_redisplay (false)
89         , _adding_routes (false)
90         , _route_deletion_in_progress (false)
91         , _redisplay_on_resume (false)
92         , _redisplay_active (0)
93         , _queue_tv_update (0)
94         , _menu (0)
95         , old_focus (0)
96         , name_editable (0)
97 {
98         static const int column_width = 22;
99
100         _scroller.add (_display);
101         _scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
102
103         _model = ListStore::create (_columns);
104         _display.set_model (_model);
105
106         // Record enable toggle
107         CellRendererPixbufMulti* rec_col_renderer = manage (new CellRendererPixbufMulti());
108
109         rec_col_renderer->set_pixbuf (0, ::get_icon("record-normal-disabled"));
110         rec_col_renderer->set_pixbuf (1, ::get_icon("record-normal-in-progress"));
111         rec_col_renderer->set_pixbuf (2, ::get_icon("record-normal-enabled"));
112         rec_col_renderer->set_pixbuf (3, ::get_icon("record-step"));
113         rec_col_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_rec_enable_changed));
114
115         TreeViewColumn* rec_state_column = manage (new TreeViewColumn("R", *rec_col_renderer));
116
117         rec_state_column->add_attribute(rec_col_renderer->property_state(), _columns.rec_state);
118         rec_state_column->add_attribute(rec_col_renderer->property_visible(), _columns.is_track);
119
120         rec_state_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
121         rec_state_column->set_alignment(ALIGN_CENTER);
122         rec_state_column->set_expand(false);
123         rec_state_column->set_fixed_width(column_width);
124
125
126         // Record safe toggle
127         CellRendererPixbufMulti* rec_safe_renderer = manage (new CellRendererPixbufMulti ());
128
129         rec_safe_renderer->set_pixbuf (0, ::get_icon("rec-safe-disabled"));
130         rec_safe_renderer->set_pixbuf (1, ::get_icon("rec-safe-enabled"));
131         rec_safe_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_rec_safe_toggled));
132
133         TreeViewColumn* rec_safe_column = manage (new TreeViewColumn(_("RS"), *rec_safe_renderer));
134         rec_safe_column->add_attribute(rec_safe_renderer->property_state(), _columns.rec_safe);
135         rec_safe_column->add_attribute(rec_safe_renderer->property_visible(), _columns.is_track);
136         rec_safe_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
137         rec_safe_column->set_alignment(ALIGN_CENTER);
138         rec_safe_column->set_expand(false);
139         rec_safe_column->set_fixed_width(column_width);
140
141
142         // MIDI Input Active
143
144         CellRendererPixbufMulti* input_active_col_renderer = manage (new CellRendererPixbufMulti());
145         input_active_col_renderer->set_pixbuf (0, ::get_icon("midi-input-inactive"));
146         input_active_col_renderer->set_pixbuf (1, ::get_icon("midi-input-active"));
147         input_active_col_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_input_active_changed));
148
149         TreeViewColumn* input_active_column = manage (new TreeViewColumn ("I", *input_active_col_renderer));
150
151         input_active_column->add_attribute(input_active_col_renderer->property_state(), _columns.is_input_active);
152         input_active_column->add_attribute (input_active_col_renderer->property_visible(), _columns.is_midi);
153
154         input_active_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
155         input_active_column->set_alignment(ALIGN_CENTER);
156         input_active_column->set_expand(false);
157         input_active_column->set_fixed_width(column_width);
158
159         // Mute enable toggle
160         CellRendererPixbufMulti* mute_col_renderer = manage (new CellRendererPixbufMulti());
161
162         mute_col_renderer->set_pixbuf (Gtkmm2ext::Off, ::get_icon("mute-disabled"));
163         mute_col_renderer->set_pixbuf (Gtkmm2ext::ImplicitActive, ::get_icon("muted-by-others"));
164         mute_col_renderer->set_pixbuf (Gtkmm2ext::ExplicitActive, ::get_icon("mute-enabled"));
165         mute_col_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_mute_enable_toggled));
166
167         TreeViewColumn* mute_state_column = manage (new TreeViewColumn("M", *mute_col_renderer));
168
169         mute_state_column->add_attribute(mute_col_renderer->property_state(), _columns.mute_state);
170         mute_state_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
171         mute_state_column->set_alignment(ALIGN_CENTER);
172         mute_state_column->set_expand(false);
173         mute_state_column->set_fixed_width(15);
174
175         // Solo enable toggle
176         CellRendererPixbufMulti* solo_col_renderer = manage (new CellRendererPixbufMulti());
177
178         solo_col_renderer->set_pixbuf (Gtkmm2ext::Off, ::get_icon("solo-disabled"));
179         solo_col_renderer->set_pixbuf (Gtkmm2ext::ExplicitActive, ::get_icon("solo-enabled"));
180         solo_col_renderer->set_pixbuf (Gtkmm2ext::ImplicitActive, ::get_icon("soloed-by-others"));
181         solo_col_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_solo_enable_toggled));
182
183         TreeViewColumn* solo_state_column = manage (new TreeViewColumn("S", *solo_col_renderer));
184
185         solo_state_column->add_attribute(solo_col_renderer->property_state(), _columns.solo_state);
186         solo_state_column->add_attribute(solo_col_renderer->property_visible(), _columns.solo_visible);
187         solo_state_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
188         solo_state_column->set_alignment(ALIGN_CENTER);
189         solo_state_column->set_expand(false);
190         solo_state_column->set_fixed_width(column_width);
191
192         // Solo isolate toggle
193         CellRendererPixbufMulti* solo_iso_renderer = manage (new CellRendererPixbufMulti());
194
195         solo_iso_renderer->set_pixbuf (0, ::get_icon("solo-isolate-disabled"));
196         solo_iso_renderer->set_pixbuf (1, ::get_icon("solo-isolate-enabled"));
197         solo_iso_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_solo_isolate_toggled));
198
199         TreeViewColumn* solo_isolate_state_column = manage (new TreeViewColumn("SI", *solo_iso_renderer));
200
201         solo_isolate_state_column->add_attribute(solo_iso_renderer->property_state(), _columns.solo_isolate_state);
202         solo_isolate_state_column->add_attribute(solo_iso_renderer->property_visible(), _columns.solo_lock_iso_visible);
203         solo_isolate_state_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
204         solo_isolate_state_column->set_alignment(ALIGN_CENTER);
205         solo_isolate_state_column->set_expand(false);
206         solo_isolate_state_column->set_fixed_width(column_width);
207
208         // Solo safe toggle
209         CellRendererPixbufMulti* solo_safe_renderer = manage (new CellRendererPixbufMulti ());
210
211         solo_safe_renderer->set_pixbuf (0, ::get_icon("solo-safe-disabled"));
212         solo_safe_renderer->set_pixbuf (1, ::get_icon("solo-safe-enabled"));
213         solo_safe_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_solo_safe_toggled));
214
215         TreeViewColumn* solo_safe_state_column = manage (new TreeViewColumn(_("SS"), *solo_safe_renderer));
216         solo_safe_state_column->add_attribute(solo_safe_renderer->property_state(), _columns.solo_safe_state);
217         solo_safe_state_column->add_attribute(solo_safe_renderer->property_visible(), _columns.solo_lock_iso_visible);
218         solo_safe_state_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
219         solo_safe_state_column->set_alignment(ALIGN_CENTER);
220         solo_safe_state_column->set_expand(false);
221         solo_safe_state_column->set_fixed_width(column_width);
222
223         _name_column = _display.append_column ("", _columns.text) - 1;
224         _visible_column = _display.append_column ("", _columns.visible) - 1;
225         _active_column = _display.append_column ("", _columns.active) - 1;
226
227         _display.append_column (*input_active_column);
228         _display.append_column (*rec_state_column);
229         _display.append_column (*rec_safe_column);
230         _display.append_column (*mute_state_column);
231         _display.append_column (*solo_state_column);
232         _display.append_column (*solo_isolate_state_column);
233         _display.append_column (*solo_safe_state_column);
234
235
236         TreeViewColumn* col;
237         Gtk::Label* l;
238
239         ColumnInfo ci[] = {
240                 { 0,  _("Name"),        _("Track/Bus Name") },
241                 { 1, S_("Visible|V"),   _("Track/Bus visible ?") },
242                 { 2, S_("Active|A"),    _("Track/Bus active ?") },
243                 { 3, S_("MidiInput|I"), _("MIDI input enabled") },
244                 { 4, S_("Rec|R"),       _("Record enabled") },
245                 { 5, S_("Rec|RS"),      _("Record Safe") },
246                 { 6, S_("Mute|M"),      _("Muted") },
247                 { 7, S_("Solo|S"),      _("Soloed") },
248                 { 8, S_("SoloIso|SI"),  _("Solo Isolated") },
249                 { 9, S_("SoloLock|SS"), _("Solo Safe (Locked)") },
250                 { -1, 0, 0 }
251         };
252
253         for (int i = 0; ci[i].index >= 0; ++i) {
254                 col = _display.get_column (ci[i].index);
255                 l = manage (new Label (ci[i].label));
256                 set_tooltip (*l, ci[i].tooltip);
257                 col->set_widget (*l);
258                 l->show ();
259         }
260
261         _display.set_headers_visible (true);
262         _display.get_selection()->set_mode (SELECTION_MULTIPLE);
263         _display.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::selection_changed));
264         _display.set_reorderable (true);
265         _display.set_name (X_("EditGroupList"));
266         _display.set_rules_hint (true);
267         _display.set_size_request (100, -1);
268
269         CellRendererText* name_cell = dynamic_cast<CellRendererText*> (_display.get_column_cell_renderer (_name_column));
270
271         assert (name_cell);
272         name_cell->signal_editing_started().connect (sigc::mem_fun (*this, &EditorRoutes::name_edit_started));
273
274         TreeViewColumn* name_column = _display.get_column (_name_column);
275
276         assert (name_column);
277
278         name_column->add_attribute (name_cell->property_editable(), _columns.name_editable);
279         name_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
280         name_column->set_expand(true);
281         name_column->set_min_width(50);
282
283         name_cell->property_editable() = true;
284         name_cell->signal_edited().connect (sigc::mem_fun (*this, &EditorRoutes::name_edit));
285
286         // Set the visible column cell renderer to radio toggle
287         CellRendererToggle* visible_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (_visible_column));
288
289         visible_cell->property_activatable() = true;
290         visible_cell->property_radio() = false;
291         visible_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRoutes::visible_changed));
292
293         TreeViewColumn* visible_col = dynamic_cast<TreeViewColumn*> (_display.get_column (_visible_column));
294         visible_col->set_expand(false);
295         visible_col->set_sizing(TREE_VIEW_COLUMN_FIXED);
296         visible_col->set_fixed_width(30);
297         visible_col->set_alignment(ALIGN_CENTER);
298
299         CellRendererToggle* active_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (_active_column));
300
301         active_cell->property_activatable() = true;
302         active_cell->property_radio() = false;
303         active_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRoutes::active_changed));
304
305         TreeViewColumn* active_col = dynamic_cast<TreeViewColumn*> (_display.get_column (_active_column));
306         active_col->set_expand (false);
307         active_col->set_sizing (TREE_VIEW_COLUMN_FIXED);
308         active_col->set_fixed_width (30);
309         active_col->set_alignment (ALIGN_CENTER);
310         active_col->add_attribute (active_cell->property_visible(), _columns.no_vca);
311
312         _model->signal_row_deleted().connect (sigc::mem_fun (*this, &EditorRoutes::row_deleted));
313         _model->signal_rows_reordered().connect (sigc::mem_fun (*this, &EditorRoutes::reordered));
314
315         _display.signal_button_press_event().connect (sigc::mem_fun (*this, &EditorRoutes::button_press), false);
316         _scroller.signal_key_press_event().connect (sigc::mem_fun(*this, &EditorRoutes::key_press), false);
317
318         _scroller.signal_focus_in_event().connect (sigc::mem_fun (*this, &EditorRoutes::focus_in), false);
319         _scroller.signal_focus_out_event().connect (sigc::mem_fun (*this, &EditorRoutes::focus_out));
320
321         _display.signal_enter_notify_event().connect (sigc::mem_fun (*this, &EditorRoutes::enter_notify), false);
322         _display.signal_leave_notify_event().connect (sigc::mem_fun (*this, &EditorRoutes::leave_notify), false);
323
324         _display.set_enable_search (false);
325
326         Route::PluginSetup.connect_same_thread (*this, boost::bind (&EditorRoutes::plugin_setup, this, _1, _2, _3));
327 }
328
329 EditorRoutes::~EditorRoutes ()
330 {
331         delete _menu;
332 }
333
334 bool
335 EditorRoutes::focus_in (GdkEventFocus*)
336 {
337         Window* win = dynamic_cast<Window*> (_scroller.get_toplevel ());
338
339         if (win) {
340                 old_focus = win->get_focus ();
341         } else {
342                 old_focus = 0;
343         }
344
345         name_editable = 0;
346
347         /* try to do nothing on focus in (doesn't work, hence selection_count nonsense) */
348         return true;
349 }
350
351 bool
352 EditorRoutes::focus_out (GdkEventFocus*)
353 {
354         if (old_focus) {
355                 old_focus->grab_focus ();
356                 old_focus = 0;
357         }
358
359         return false;
360 }
361
362 bool
363 EditorRoutes::enter_notify (GdkEventCrossing*)
364 {
365         if (name_editable) {
366                 return true;
367         }
368
369         Keyboard::magic_widget_grab_focus ();
370         return false;
371 }
372
373 bool
374 EditorRoutes::leave_notify (GdkEventCrossing*)
375 {
376         if (old_focus) {
377                 old_focus->grab_focus ();
378                 old_focus = 0;
379         }
380
381         Keyboard::magic_widget_drop_focus ();
382         return false;
383 }
384
385 void
386 EditorRoutes::set_session (Session* s)
387 {
388         SessionHandlePtr::set_session (s);
389
390         initial_display ();
391
392         if (_session) {
393                 _session->SoloChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::solo_changed_so_update_mute, this), gui_context());
394                 _session->RecordStateChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_rec_display, this), gui_context());
395
396                 /* TODO: check if these needs to be tied in with DisplaySuspender
397                  * Given that the UI is single-threaded and DisplaySuspender is only used
398                  * in loops in the UI thread all should be fine.
399                  */
400                 _session->BatchUpdateStart.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::suspend_redisplay, this), gui_context());
401                 _session->BatchUpdateEnd.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::resume_redisplay, this), gui_context());
402         }
403 }
404
405 void
406 EditorRoutes::on_input_active_changed (std::string const & path_string)
407 {
408         // Get the model row that has been toggled.
409         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
410
411         TimeAxisView* tv = row[_columns.tv];
412         RouteTimeAxisView *rtv = dynamic_cast<RouteTimeAxisView*> (tv);
413
414         if (rtv) {
415                 boost::shared_ptr<MidiTrack> mt;
416                 mt = rtv->midi_track();
417                 if (mt) {
418                         mt->set_input_active (!mt->input_active());
419                 }
420         }
421 }
422
423 void
424 EditorRoutes::on_tv_rec_enable_changed (std::string const & path_string)
425 {
426         // Get the model row that has been toggled.
427         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
428
429         TimeAxisView* tv = row[_columns.tv];
430         StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*> (tv);
431
432         if (!stv || !stv->stripable()) {
433                 return;
434         }
435
436         boost::shared_ptr<AutomationControl> ac = stv->stripable()->rec_enable_control();
437
438         if (ac) {
439                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
440         }
441 }
442
443 void
444 EditorRoutes::on_tv_rec_safe_toggled (std::string const & path_string)
445 {
446         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
447         TimeAxisView* tv = row[_columns.tv];
448         StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*> (tv);
449
450         if (!stv || !stv->stripable()) {
451                 return;
452         }
453
454         boost::shared_ptr<AutomationControl> ac (stv->stripable()->rec_safe_control());
455
456         if (ac) {
457                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
458         }
459 }
460
461 void
462 EditorRoutes::on_tv_mute_enable_toggled (std::string const & path_string)
463 {
464         // Get the model row that has been toggled.
465         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
466
467         TimeAxisView *tv = row[_columns.tv];
468         StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*> (tv);
469
470         if (!stv || !stv->stripable()) {
471                 return;
472         }
473
474         boost::shared_ptr<AutomationControl> ac (stv->stripable()->mute_control());
475
476         if (ac) {
477                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
478         }
479 }
480
481 void
482 EditorRoutes::on_tv_solo_enable_toggled (std::string const & path_string)
483 {
484         // Get the model row that has been toggled.
485         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
486
487         TimeAxisView *tv = row[_columns.tv];
488         StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*> (tv);
489
490         if (!stv || !stv->stripable()) {
491                 return;
492         }
493
494         boost::shared_ptr<AutomationControl> ac (stv->stripable()->solo_control());
495
496         if (ac) {
497                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
498         }
499 }
500
501 void
502 EditorRoutes::on_tv_solo_isolate_toggled (std::string const & path_string)
503 {
504         // Get the model row that has been toggled.
505         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
506
507         TimeAxisView *tv = row[_columns.tv];
508         StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*> (tv);
509
510         if (!stv || !stv->stripable()) {
511                 return;
512         }
513
514         boost::shared_ptr<AutomationControl> ac (stv->stripable()->solo_isolate_control());
515
516         if (ac) {
517                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
518         }
519 }
520
521 void
522 EditorRoutes::on_tv_solo_safe_toggled (std::string const & path_string)
523 {
524         // Get the model row that has been toggled.
525         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
526
527         TimeAxisView *tv = row[_columns.tv];
528         StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*> (tv);
529
530         if (!stv || !stv->stripable()) {
531                 return;
532         }
533
534         boost::shared_ptr<AutomationControl> ac (stv->stripable()->solo_safe_control());
535
536         if (ac) {
537                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
538         }
539 }
540
541 void
542 EditorRoutes::build_menu ()
543 {
544         using namespace Menu_Helpers;
545         using namespace Gtk;
546
547         _menu = new Menu;
548
549         MenuList& items = _menu->items();
550         _menu->set_name ("ArdourContextMenu");
551
552         items.push_back (MenuElem (_("Show All"), sigc::mem_fun (*this, &EditorRoutes::show_all_routes)));
553         items.push_back (MenuElem (_("Hide All"), sigc::mem_fun (*this, &EditorRoutes::hide_all_routes)));
554         items.push_back (MenuElem (_("Show All Audio Tracks"), sigc::mem_fun (*this, &EditorRoutes::show_all_audiotracks)));
555         items.push_back (MenuElem (_("Hide All Audio Tracks"), sigc::mem_fun (*this, &EditorRoutes::hide_all_audiotracks)));
556         items.push_back (MenuElem (_("Show All Midi Tracks"), sigc::mem_fun (*this, &EditorRoutes::show_all_miditracks)));
557         items.push_back (MenuElem (_("Hide All Midi Tracks"), sigc::mem_fun (*this, &EditorRoutes::hide_all_miditracks)));
558         items.push_back (MenuElem (_("Show All Busses"), sigc::mem_fun (*this, &EditorRoutes::show_all_audiobus)));
559         items.push_back (MenuElem (_("Hide All Busses"), sigc::mem_fun (*this, &EditorRoutes::hide_all_audiobus)));
560         items.push_back (MenuElem (_("Only Show Tracks with Regions Under Playhead"), sigc::mem_fun (*this, &EditorRoutes::show_tracks_with_regions_at_playhead)));
561 }
562
563 void
564 EditorRoutes::redisplay_real ()
565 {
566         TreeModel::Children rows = _model->children();
567         TreeModel::Children::iterator i;
568         uint32_t position;
569
570         /* n will be the count of tracks plus children (updated by TimeAxisView::show_at),
571          * so we will use that to know where to put things.
572          */
573         int n;
574
575         for (n = 0, position = 0, i = rows.begin(); i != rows.end(); ++i) {
576                 TimeAxisView *tv = (*i)[_columns.tv];
577
578                 if (tv == 0) {
579                         // just a "title" row
580                         continue;
581                 }
582
583                 bool visible = tv->marked_for_display ();
584
585                 /* show or hide the TimeAxisView */
586                 if (visible) {
587                         position += tv->show_at (position, n, &_editor->edit_controls_vbox);
588                 } else {
589                         tv->hide ();
590                 }
591
592                 n++;
593         }
594
595         /* whenever we go idle, update the track view list to reflect the new order.
596          * we can't do this here, because we could mess up something that is traversing
597          * the track order and has caused a redisplay of the list.
598          */
599         Glib::signal_idle().connect (sigc::mem_fun (*_editor, &Editor::sync_track_view_list_and_routes));
600
601         _editor->reset_controls_layout_height (position);
602         _editor->reset_controls_layout_width ();
603         _editor->_full_canvas_height = position;
604
605         if ((_editor->vertical_adjustment.get_value() + _editor->_visible_canvas_height) > _editor->vertical_adjustment.get_upper()) {
606                 /*
607                  * We're increasing the size of the canvas while the bottom is visible.
608                  * We scroll down to keep in step with the controls layout.
609                  */
610                 _editor->vertical_adjustment.set_value (_editor->_full_canvas_height - _editor->_visible_canvas_height);
611         }
612 }
613
614 void
615 EditorRoutes::redisplay ()
616 {
617         if (!_session || _session->deletion_in_progress()) {
618                 return;
619         }
620
621         if (_no_redisplay) {
622                 _redisplay_on_resume = true;
623                 return;
624         }
625
626         // model deprecated g_atomic_int_exchange_and_add(, 1)
627         g_atomic_int_inc(const_cast<gint*>(&_redisplay_active));
628         if (!g_atomic_int_compare_and_exchange (const_cast<gint*>(&_redisplay_active), 1, 1)) {
629                 /* recursive re-display can happen if redisplay shows/hides a TrackView
630                  * which has children and their display status changes as result.
631                  */
632                 return;
633         }
634
635         redisplay_real ();
636
637         while (!g_atomic_int_compare_and_exchange (const_cast<gint*>(&_redisplay_active), 1, 0)) {
638                 g_atomic_int_set(const_cast<gint*>(&_redisplay_active), 1);
639                 redisplay_real ();
640         }
641 }
642
643 void
644 EditorRoutes::row_deleted (Gtk::TreeModel::Path const &)
645 {
646         if (!_session || _session->deletion_in_progress()) {
647                 return;
648         }
649         /* this happens as the second step of a DnD within the treeview, and
650          * when a route is actually removed. we don't differentiate between
651          * the two cases.
652          *
653          * note that the sync_presentation_info_from_treeview() step may not
654          * actually change any presentation info (e.g. the last track may be
655          * removed, so all other tracks keep the same presentation info), which
656          * means that no redisplay would happen. so we have to force a
657          * redisplay.
658          */
659
660         DEBUG_TRACE (DEBUG::OrderKeys, "editor routes treeview row deleted\n");
661
662         DisplaySuspender ds;
663         sync_presentation_info_from_treeview ();
664 }
665
666 void
667 EditorRoutes::reordered (TreeModel::Path const &, TreeModel::iterator const &, int* /*what*/)
668 {
669         /* reordering implies that RID's will change, so
670            sync_presentation_info_from_treeview() will cause a redisplay.
671         */
672
673         DEBUG_TRACE (DEBUG::OrderKeys, "editor routes treeview reordered\n");
674         sync_presentation_info_from_treeview ();
675 }
676
677 void
678 EditorRoutes::visible_changed (std::string const & path)
679 {
680         if (_session && _session->deletion_in_progress()) {
681                 return;
682         }
683
684         DisplaySuspender ds;
685         TreeIter iter;
686
687         if ((iter = _model->get_iter (path))) {
688                 TimeAxisView* tv = (*iter)[_columns.tv];
689                 if (tv) {
690                         bool visible = (*iter)[_columns.visible];
691
692                         if (tv->set_marked_for_display (!visible)) {
693                                 update_visibility ();
694                         }
695                 }
696         }
697 }
698
699 void
700 EditorRoutes::active_changed (std::string const & path)
701 {
702         if (_session && _session->deletion_in_progress ()) {
703                 return;
704         }
705
706         Gtk::TreeModel::Row row = *_model->get_iter (path);
707         boost::shared_ptr<Stripable> stripable = row[_columns.stripable];
708         boost::shared_ptr<Route> route = boost::dynamic_pointer_cast<Route> (stripable);
709         if (route) {
710                 bool const active = row[_columns.active];
711                 route->set_active (!active, this);
712         }
713 }
714
715 void
716 EditorRoutes::time_axis_views_added (list<TimeAxisView*> tavs)
717 {
718         PBD::Unwinder<bool> at (_adding_routes, true);
719         bool from_scratch = (_model->children().size() == 0);
720         Gtk::TreeModel::Children::iterator insert_iter = _model->children().end();
721
722         for (Gtk::TreeModel::Children::iterator it = _model->children().begin(); it != _model->children().end(); ++it) {
723
724                 boost::shared_ptr<Stripable> r = (*it)[_columns.stripable];
725
726                 if (r->presentation_info().order() == (tavs.front()->stripable()->presentation_info().order() + tavs.size())) {
727                         insert_iter = it;
728                         break;
729                 }
730         }
731
732         {
733                 PBD::Unwinder<bool> uw (_ignore_selection_change, true);
734                 _display.set_model (Glib::RefPtr<ListStore>());
735         }
736
737         for (list<TimeAxisView*>::iterator x = tavs.begin(); x != tavs.end(); ++x) {
738
739                 VCATimeAxisView* vtav = dynamic_cast<VCATimeAxisView*> (*x);
740                 RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (*x);
741
742                 TreeModel::Row row = *(_model->insert (insert_iter));
743
744                 boost::shared_ptr<Stripable> stripable;
745                 boost::shared_ptr<MidiTrack> midi_trk;
746
747                 if (vtav) {
748
749                         stripable = vtav->vca();
750
751                         row[_columns.is_track] = false;
752                         row[_columns.is_input_active] = false;
753                         row[_columns.is_midi] = false;
754                         row[_columns.no_vca] = false;
755
756                 } else if (rtav) {
757
758                         stripable = rtav->route ();
759                         midi_trk= boost::dynamic_pointer_cast<MidiTrack> (stripable);
760
761                         row[_columns.is_track] = (boost::dynamic_pointer_cast<Track> (stripable) != 0);
762                         row[_columns.no_vca] = true;
763
764                         if (midi_trk) {
765                                 row[_columns.is_input_active] = midi_trk->input_active ();
766                                 row[_columns.is_midi] = true;
767                         } else {
768                                 row[_columns.is_input_active] = false;
769                                 row[_columns.is_midi] = false;
770                         }
771                 }
772
773                 if (!stripable) {
774                         continue;
775                 }
776
777                 row[_columns.text] = stripable->name();
778                 row[_columns.visible] = (*x)->marked_for_display();
779                 row[_columns.active] = true;
780                 row[_columns.tv] = *x;
781                 row[_columns.stripable] = stripable;
782                 row[_columns.mute_state] = RouteUI::mute_active_state (_session, stripable);
783                 row[_columns.solo_state] = RouteUI::solo_active_state (stripable);
784                 row[_columns.solo_visible] = !stripable->is_master ();
785                 row[_columns.solo_lock_iso_visible] = row[_columns.solo_visible] && row[_columns.no_vca];
786                 row[_columns.solo_isolate_state] = RouteUI::solo_isolate_active_state (stripable);
787                 row[_columns.solo_safe_state] = RouteUI::solo_safe_active_state (stripable);
788                 row[_columns.name_editable] = true;
789
790                 boost::weak_ptr<Stripable> ws (stripable);
791
792                 /* for now, we need both of these. PropertyChanged covers on
793                  * pre-defined, "global" things of interest to a
794                  * UI. gui_changed covers arbitrary, un-enumerated, un-typed
795                  * changes that may only be of interest to a particular
796                  * UI (e.g. track-height is not of any relevant to OSC)
797                  */
798
799                 stripable->gui_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::handle_gui_changes, this, _1, _2), gui_context());
800                 stripable->PropertyChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::route_property_changed, this, _1, ws), gui_context());
801                 stripable->presentation_info().PropertyChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::route_property_changed, this, _1, ws), gui_context());
802
803                 if (boost::dynamic_pointer_cast<Track> (stripable)) {
804                         boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (stripable);
805                         t->rec_enable_control()->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_rec_display, this), gui_context());
806                         t->rec_safe_control()->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_rec_display, this), gui_context());
807                 }
808
809                 if (midi_trk) {
810                         midi_trk->StepEditStatusChange.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_rec_display, this), gui_context());
811                         midi_trk->InputActiveChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_input_active_display, this), gui_context());
812                 }
813
814                 boost::shared_ptr<AutomationControl> ac;
815
816                 if ((ac = stripable->mute_control()) != 0) {
817                         ac->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_mute_display, this), gui_context());
818                 }
819                 if ((ac = stripable->solo_control()) != 0) {
820                         ac->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_solo_display, this), gui_context());
821                 }
822                 if ((ac = stripable->solo_isolate_control()) != 0) {
823                         ac->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_solo_isolate_display, this), gui_context());
824                 }
825                 if ((ac = stripable->solo_safe_control()) != 0) {
826                         ac->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_solo_safe_display, this), gui_context());
827                 }
828
829                 if (rtav) {
830                         rtav->route()->active_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_active_display, this), gui_context ());
831                 }
832         }
833
834         update_rec_display ();
835         update_mute_display ();
836         update_solo_display ();
837         update_solo_isolate_display ();
838         update_solo_safe_display ();
839         update_input_active_display ();
840         update_active_display ();
841
842         {
843                 PBD::Unwinder<bool> uw (_ignore_selection_change, true);
844                 _display.set_model (_model);
845         }
846
847         /* now update route order keys from the treeview/track display order */
848
849         if (!from_scratch) {
850                 sync_presentation_info_from_treeview ();
851         }
852
853         redisplay ();
854 }
855
856 void
857 EditorRoutes::handle_gui_changes (string const & what, void*)
858 {
859         if (_adding_routes) {
860                 return;
861         }
862
863         if (what == "track_height") {
864                 /* Optional :make tracks change height while it happens, instead
865                    of on first-idle
866                 */
867                 redisplay ();
868         }
869
870         if (what == "visible_tracks") {
871                 redisplay ();
872         }
873 }
874
875 void
876 EditorRoutes::route_removed (TimeAxisView *tv)
877 {
878         ENSURE_GUI_THREAD (*this, &EditorRoutes::route_removed, tv)
879
880         TreeModel::Children rows = _model->children();
881         TreeModel::Children::iterator ri;
882
883         PBD::Unwinder<bool> uw (_ignore_selection_change, true);
884
885         for (ri = rows.begin(); ri != rows.end(); ++ri) {
886                 if ((*ri)[_columns.tv] == tv) {
887                         PBD::Unwinder<bool> uw (_route_deletion_in_progress, true);
888                         _model->erase (ri);
889                         break;
890                 }
891         }
892 }
893
894 void
895 EditorRoutes::route_property_changed (const PropertyChange& what_changed, boost::weak_ptr<Stripable> s)
896 {
897         if (!what_changed.contains (ARDOUR::Properties::hidden) && !what_changed.contains (ARDOUR::Properties::name)) {
898                 return;
899         }
900
901         if (_adding_routes) {
902                 return;
903         }
904
905         boost::shared_ptr<Stripable> stripable = s.lock ();
906
907         if (!stripable) {
908                 return;
909         }
910
911         TreeModel::Children rows = _model->children();
912         TreeModel::Children::iterator i;
913
914         for (i = rows.begin(); i != rows.end(); ++i) {
915
916                 boost::shared_ptr<Stripable> ss = (*i)[_columns.stripable];
917
918                 if (ss == stripable) {
919
920                         if (what_changed.contains (ARDOUR::Properties::name)) {
921                                 (*i)[_columns.text] = stripable->name();
922                                 break;
923                         }
924
925                         if (what_changed.contains (ARDOUR::Properties::hidden)) {
926                                 (*i)[_columns.visible] = !stripable->presentation_info().hidden();
927                                 redisplay ();
928
929                         }
930
931                         break;
932                 }
933         }
934 }
935
936 void
937 EditorRoutes::update_active_display ()
938 {
939         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
940                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
941         }
942 }
943
944 void
945 EditorRoutes::update_visibility ()
946 {
947         TreeModel::Children rows = _model->children();
948         TreeModel::Children::iterator i;
949
950         DisplaySuspender ds;
951
952         for (i = rows.begin(); i != rows.end(); ++i) {
953                 TimeAxisView *tv = (*i)[_columns.tv];
954                 (*i)[_columns.visible] = tv->marked_for_display ();
955         }
956
957         /* force route order keys catch up with visibility changes */
958
959         sync_presentation_info_from_treeview ();
960 }
961
962 void
963 EditorRoutes::hide_track_in_display (TimeAxisView& tv)
964 {
965         TreeModel::Children rows = _model->children();
966         TreeModel::Children::iterator i;
967
968         for (i = rows.begin(); i != rows.end(); ++i) {
969                 if ((*i)[_columns.tv] == &tv) {
970                         tv.set_marked_for_display (false);
971                         (*i)[_columns.visible] = false;
972                         redisplay ();
973                         break;
974                 }
975         }
976 }
977
978 void
979 EditorRoutes::show_track_in_display (TimeAxisView& tv)
980 {
981         TreeModel::Children rows = _model->children();
982         TreeModel::Children::iterator i;
983
984
985         for (i = rows.begin(); i != rows.end(); ++i) {
986                 if ((*i)[_columns.tv] == &tv) {
987                         tv.set_marked_for_display (true);
988                         (*i)[_columns.visible] = true;
989                         redisplay ();
990                         break;
991                 }
992         }
993 }
994
995 void
996 EditorRoutes::sync_presentation_info_from_treeview ()
997 {
998         if (_ignore_reorder || !_session || _session->deletion_in_progress()) {
999                 return;
1000         }
1001
1002         TreeModel::Children rows = _model->children();
1003
1004         if (rows.empty()) {
1005                 return;
1006         }
1007
1008         DEBUG_TRACE (DEBUG::OrderKeys, "editor sync presentation info from treeview\n");
1009
1010         bool change = false;
1011         PresentationInfo::order_t order = 0;
1012
1013         PresentationInfo::ChangeSuspender cs;
1014
1015         for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri) {
1016                 boost::shared_ptr<Stripable> stripable = (*ri)[_columns.stripable];
1017                 bool visible = (*ri)[_columns.visible];
1018
1019 #ifndef NDEBUG // these should not exist in the treeview
1020                 assert (stripable);
1021                 if (stripable->is_monitor() || stripable->is_auditioner()) {
1022                         assert (0);
1023                         continue;
1024                 }
1025 #endif
1026
1027                 stripable->presentation_info().set_hidden (!visible);
1028
1029                 if (order != stripable->presentation_info().order()) {
1030                         stripable->set_presentation_order (order);
1031                         change = true;
1032                 }
1033                 ++order;
1034         }
1035
1036         change |= _session->ensure_stripable_sort_order ();
1037
1038         if (change) {
1039                 _session->set_dirty();
1040         }
1041 }
1042
1043 void
1044 EditorRoutes::sync_treeview_from_presentation_info (PropertyChange const & what_changed)
1045 {
1046         /* Some route order key(s) have been changed, make sure that
1047            we update out tree/list model and GUI to reflect the change.
1048         */
1049
1050         if (_ignore_reorder || !_session || _session->deletion_in_progress()) {
1051                 return;
1052         }
1053
1054         DEBUG_TRACE (DEBUG::OrderKeys, "editor sync model from presentation info.\n");
1055
1056         PropertyChange hidden_or_order;
1057         hidden_or_order.add (Properties::hidden);
1058         hidden_or_order.add (Properties::order);
1059
1060         TreeModel::Children rows = _model->children();
1061
1062         bool changed = false;
1063
1064         if (what_changed.contains (hidden_or_order)) {
1065                 vector<int> neworder;
1066                 uint32_t old_order = 0;
1067
1068                 if (rows.empty()) {
1069                         return;
1070                 }
1071
1072                 TreeOrderKeys sorted;
1073                 for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri, ++old_order) {
1074                         boost::shared_ptr<Stripable> stripable = (*ri)[_columns.stripable];
1075                         /* use global order */
1076                         sorted.push_back (TreeOrderKey (old_order, stripable));
1077                 }
1078
1079                 TreeOrderKeySorter cmp;
1080
1081                 sort (sorted.begin(), sorted.end(), cmp);
1082                 neworder.assign (sorted.size(), 0);
1083
1084                 uint32_t n = 0;
1085
1086                 for (TreeOrderKeys::iterator sr = sorted.begin(); sr != sorted.end(); ++sr, ++n) {
1087
1088                         neworder[n] = sr->old_display_order;
1089
1090                         if (sr->old_display_order != n) {
1091                                 changed = true;
1092                         }
1093                 }
1094
1095                 if (changed) {
1096                         Unwinder<bool> uw (_ignore_reorder, true);
1097                         /* prevent traverse_cells: assertion 'row_path != NULL'
1098                          * in case of DnD re-order: row-removed + row-inserted.
1099                          *
1100                          * The rows (stripables) are not actually removed from the model,
1101                          * but only from the display in the DnDTreeView.
1102                          * ->reorder() will fail to find the row_path.
1103                          * (re-order drag -> remove row -> sync PI from TV -> notify -> sync TV from PI -> crash)
1104                          */
1105                         Unwinder<bool> uw2 (_ignore_selection_change, true);
1106
1107                         _display.unset_model();
1108                         _model->reorder (neworder);
1109                         _display.set_model (_model);
1110                 }
1111         }
1112
1113         if (changed || what_changed.contains (Properties::selected)) {
1114                 /* by the time this is invoked, the GUI Selection model has
1115                  * already updated itself.
1116                  */
1117                 PBD::Unwinder<bool> uw (_ignore_selection_change, true);
1118
1119                 /* set the treeview model selection state */
1120                 for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri) {
1121                         boost::shared_ptr<Stripable> stripable = (*ri)[_columns.stripable];
1122                         if (stripable && stripable->is_selected()) {
1123                                 _display.get_selection()->select (*ri);
1124                         } else {
1125                                 _display.get_selection()->unselect (*ri);
1126                         }
1127                 }
1128         }
1129
1130         redisplay ();
1131 }
1132
1133 void
1134 EditorRoutes::hide_all_tracks (bool /*with_select*/)
1135 {
1136         TreeModel::Children rows = _model->children();
1137         TreeModel::Children::iterator i;
1138
1139         DisplaySuspender ds;
1140
1141         for (i = rows.begin(); i != rows.end(); ++i) {
1142
1143                 TreeModel::Row row = (*i);
1144                 TimeAxisView *tv = row[_columns.tv];
1145
1146                 if (tv == 0) {
1147                         continue;
1148                 }
1149
1150                 row[_columns.visible] = false;
1151         }
1152 }
1153
1154 void
1155 EditorRoutes::set_all_tracks_visibility (bool yn)
1156 {
1157         TreeModel::Children rows = _model->children();
1158         TreeModel::Children::iterator i;
1159
1160         DisplaySuspender ds;
1161
1162         for (i = rows.begin(); i != rows.end(); ++i) {
1163
1164                 TreeModel::Row row = (*i);
1165                 TimeAxisView* tv = row[_columns.tv];
1166
1167                 if (tv == 0) {
1168                         continue;
1169                 }
1170
1171                 tv->set_marked_for_display (yn);
1172                 (*i)[_columns.visible] = yn;
1173         }
1174
1175         /* force route order keys catch up with visibility changes
1176          */
1177
1178         sync_presentation_info_from_treeview ();
1179 }
1180
1181 void
1182 EditorRoutes::set_all_audio_midi_visibility (int tracks, bool yn)
1183 {
1184         TreeModel::Children rows = _model->children();
1185         TreeModel::Children::iterator i;
1186
1187         DisplaySuspender ds;
1188
1189         for (i = rows.begin(); i != rows.end(); ++i) {
1190
1191                 TreeModel::Row row = (*i);
1192                 TimeAxisView* tv = row[_columns.tv];
1193
1194                 AudioTimeAxisView* atv;
1195                 MidiTimeAxisView* mtv;
1196
1197                 if (tv == 0) {
1198                         continue;
1199                 }
1200
1201                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
1202                         switch (tracks) {
1203                         case 0:
1204                                 atv->set_marked_for_display (yn);
1205                                 (*i)[_columns.visible] = yn;
1206                                 break;
1207
1208                         case 1:
1209                                 if (atv->is_audio_track()) {
1210                                         atv->set_marked_for_display (yn);
1211                                         (*i)[_columns.visible] = yn;
1212                                 }
1213                                 break;
1214
1215                         case 2:
1216                                 if (!atv->is_audio_track()) {
1217                                         atv->set_marked_for_display (yn);
1218                                         (*i)[_columns.visible] = yn;
1219                                 }
1220                                 break;
1221                         }
1222                 }
1223                 else if ((mtv = dynamic_cast<MidiTimeAxisView*>(tv)) != 0) {
1224                         switch (tracks) {
1225                         case 0:
1226                                 mtv->set_marked_for_display (yn);
1227                                 (*i)[_columns.visible] = yn;
1228                                 break;
1229
1230                         case 3:
1231                                 if (mtv->is_midi_track()) {
1232                                         mtv->set_marked_for_display (yn);
1233                                         (*i)[_columns.visible] = yn;
1234                                 }
1235                                 break;
1236                         }
1237                 }
1238         }
1239
1240         /* force route order keys catch up with visibility changes
1241          */
1242
1243         sync_presentation_info_from_treeview ();
1244 }
1245
1246 void
1247 EditorRoutes::hide_all_routes ()
1248 {
1249         set_all_tracks_visibility (false);
1250 }
1251
1252 void
1253 EditorRoutes::show_all_routes ()
1254 {
1255         set_all_tracks_visibility (true);
1256 }
1257
1258 void
1259 EditorRoutes::show_all_audiotracks()
1260 {
1261         set_all_audio_midi_visibility (1, true);
1262 }
1263 void
1264 EditorRoutes::hide_all_audiotracks ()
1265 {
1266         set_all_audio_midi_visibility (1, false);
1267 }
1268
1269 void
1270 EditorRoutes::show_all_audiobus ()
1271 {
1272         set_all_audio_midi_visibility (2, true);
1273 }
1274 void
1275 EditorRoutes::hide_all_audiobus ()
1276 {
1277         set_all_audio_midi_visibility (2, false);
1278 }
1279
1280 void
1281 EditorRoutes::show_all_miditracks()
1282 {
1283         set_all_audio_midi_visibility (3, true);
1284 }
1285 void
1286 EditorRoutes::hide_all_miditracks ()
1287 {
1288         set_all_audio_midi_visibility (3, false);
1289 }
1290
1291 bool
1292 EditorRoutes::key_press (GdkEventKey* ev)
1293 {
1294         TreeViewColumn *col;
1295         boost::shared_ptr<RouteList> rl (new RouteList);
1296         TreePath path;
1297
1298         switch (ev->keyval) {
1299                 case GDK_Tab:
1300                 case GDK_ISO_Left_Tab:
1301
1302                         /* If we appear to be editing something, leave that cleanly and appropriately. */
1303                         if (name_editable) {
1304                                 name_editable->editing_done ();
1305                                 name_editable = 0;
1306                         }
1307
1308                         col = _display.get_column (_name_column); // select&focus on name column
1309
1310                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
1311                                 treeview_select_previous (_display, _model, col);
1312                         } else {
1313                                 treeview_select_next (_display, _model, col);
1314                         }
1315
1316                         return true;
1317                         break;
1318
1319                 case 'm':
1320                         if (get_relevant_routes (rl)) {
1321                                 _session->set_controls (route_list_to_control_list (rl, &Stripable::mute_control), rl->front()->muted() ? 0.0 : 1.0, Controllable::NoGroup);
1322                         }
1323                         return true;
1324                         break;
1325
1326                 case 's':
1327                         if (get_relevant_routes (rl)) {
1328                                 _session->set_controls (route_list_to_control_list (rl, &Stripable::solo_control), rl->front()->self_soloed() ? 0.0 : 1.0, Controllable::NoGroup);
1329                         }
1330                         return true;
1331                         break;
1332
1333                 case 'r':
1334                         if (get_relevant_routes (rl)) {
1335                                 for (RouteList::const_iterator r = rl->begin(); r != rl->end(); ++r) {
1336                                         boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (*r);
1337                                         if (t) {
1338                                                 _session->set_controls (route_list_to_control_list (rl, &Stripable::rec_enable_control), !t->rec_enable_control()->get_value(), Controllable::NoGroup);
1339                                                 break;
1340                                         }
1341                                 }
1342                         }
1343                         break;
1344
1345                 default:
1346                         break;
1347         }
1348
1349         return false;
1350 }
1351
1352 bool
1353 EditorRoutes::get_relevant_routes (boost::shared_ptr<RouteList> rl)
1354 {
1355         TimeAxisView* tv;
1356         RouteTimeAxisView* rtv;
1357         RefPtr<TreeSelection> selection = _display.get_selection();
1358         TreePath path;
1359         TreeIter iter;
1360
1361         if (selection->count_selected_rows() != 0) {
1362
1363                 /* use selection */
1364
1365                 RefPtr<TreeModel> tm = RefPtr<TreeModel>::cast_dynamic (_model);
1366                 iter = selection->get_selected (tm);
1367
1368         } else {
1369                 /* use mouse pointer */
1370
1371                 int x, y;
1372                 int bx, by;
1373
1374                 _display.get_pointer (x, y);
1375                 _display.convert_widget_to_bin_window_coords (x, y, bx, by);
1376
1377                 if (_display.get_path_at_pos (bx, by, path)) {
1378                         iter = _model->get_iter (path);
1379                 }
1380         }
1381
1382         if (iter) {
1383                 tv = (*iter)[_columns.tv];
1384                 if (tv) {
1385                         rtv = dynamic_cast<RouteTimeAxisView*>(tv);
1386                         if (rtv) {
1387                                 rl->push_back (rtv->route());
1388                         }
1389                 }
1390         }
1391
1392         return !rl->empty();
1393 }
1394
1395 bool
1396 EditorRoutes::button_press (GdkEventButton* ev)
1397 {
1398         if (Keyboard::is_context_menu_event (ev)) {
1399                 if (_menu == 0) {
1400                         build_menu ();
1401                 }
1402                 _menu->popup (ev->button, ev->time);
1403                 return true;
1404         }
1405
1406         TreeModel::Path path;
1407         TreeViewColumn *tvc;
1408         int cell_x;
1409         int cell_y;
1410
1411         if (!_display.get_path_at_pos ((int) ev->x, (int) ev->y, path, tvc, cell_x, cell_y)) {
1412                 /* cancel selection */
1413                 _display.get_selection()->unselect_all ();
1414                 /* end any editing by grabbing focus */
1415                 _display.grab_focus ();
1416                 return true;
1417         }
1418
1419         //Scroll editor canvas to selected track
1420         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1421
1422                 Gtk::TreeModel::Row row = *_model->get_iter (path);
1423                 TimeAxisView *tv = row[_columns.tv];
1424
1425                 if (tv) {
1426                         _editor->ensure_time_axis_view_is_visible (*tv, true);
1427                 }
1428         }
1429
1430         return false;
1431 }
1432
1433 void
1434 EditorRoutes::selection_changed ()
1435 {
1436         if (_ignore_selection_change) {
1437                 return;
1438         }
1439
1440         _editor->begin_reversible_selection_op (X_("Select Track from Route List"));
1441
1442         if (_display.get_selection()->count_selected_rows() > 0) {
1443
1444                 TreeIter iter;
1445                 TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
1446                 TrackViewList selected;
1447
1448                 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
1449
1450                         if ((iter = _model->get_iter (*i))) {
1451
1452                                 TimeAxisView* tv = (*iter)[_columns.tv];
1453                                 selected.push_back (tv);
1454                         }
1455
1456                 }
1457
1458                 _editor->get_selection().set (selected);
1459                 _editor->ensure_time_axis_view_is_visible (*(selected.front()), true);
1460
1461         } else {
1462                 _editor->get_selection().clear_tracks ();
1463         }
1464
1465         _editor->commit_reversible_selection_op ();
1466 }
1467
1468 void
1469 EditorRoutes::initial_display ()
1470 {
1471
1472         if (!_session) {
1473                 clear ();
1474                 return;
1475         }
1476
1477         DisplaySuspender ds;
1478         _model->clear ();
1479
1480         StripableList s;
1481
1482         _session->get_stripables (s);
1483         _editor->add_stripables (s);
1484
1485         sync_treeview_from_presentation_info (Properties::order);
1486 }
1487
1488 struct ViewStripable {
1489         TimeAxisView* tav;
1490         boost::shared_ptr<Stripable> stripable;
1491
1492         ViewStripable (TimeAxisView* t, boost::shared_ptr<Stripable> s)
1493                 : tav (t), stripable (s) {}
1494 };
1495
1496 void
1497 EditorRoutes::move_selected_tracks (bool up)
1498 {
1499         TimeAxisView* scroll_to = 0;
1500         StripableList sl;
1501         _session->get_stripables (sl);
1502
1503         if (sl.size() < 2) {
1504                 /* nope */
1505                 return;
1506         }
1507
1508         sl.sort (Stripable::Sorter());
1509
1510         std::list<ViewStripable> view_stripables;
1511
1512         /* build a list that includes time axis view information */
1513
1514         for (StripableList::const_iterator sli = sl.begin(); sli != sl.end(); ++sli) {
1515                 TimeAxisView* tv = _editor->time_axis_view_from_stripable (*sli);
1516                 view_stripables.push_back (ViewStripable (tv, *sli));
1517         }
1518
1519         /* for each selected stripable, move it above or below the adjacent
1520          * stripable that has a time-axis view representation here. If there's
1521          * no such representation, then
1522          */
1523
1524         list<ViewStripable>::iterator unselected_neighbour;
1525         list<ViewStripable>::iterator vsi;
1526
1527         {
1528                 PresentationInfo::ChangeSuspender cs;
1529
1530                 if (up) {
1531                         unselected_neighbour = view_stripables.end ();
1532                         vsi = view_stripables.begin();
1533
1534                         while (vsi != view_stripables.end()) {
1535
1536                                 if (vsi->stripable->is_selected()) {
1537
1538                                         if (unselected_neighbour != view_stripables.end()) {
1539
1540                                                 PresentationInfo::order_t unselected_neighbour_order = unselected_neighbour->stripable->presentation_info().order();
1541                                                 PresentationInfo::order_t my_order = vsi->stripable->presentation_info().order();
1542
1543                                                 unselected_neighbour->stripable->set_presentation_order (my_order);
1544                                                 vsi->stripable->set_presentation_order (unselected_neighbour_order);
1545
1546                                                 if (!scroll_to) {
1547                                                         scroll_to = vsi->tav;
1548                                                 }
1549                                         }
1550
1551                                 } else {
1552
1553                                         if (vsi->tav) {
1554                                                 unselected_neighbour = vsi;
1555                                         }
1556
1557                                 }
1558
1559                                 ++vsi;
1560                         }
1561
1562                 } else {
1563
1564                         unselected_neighbour = view_stripables.end();
1565                         vsi = unselected_neighbour;
1566
1567                         do {
1568
1569                                 --vsi;
1570
1571                                 if (vsi->stripable->is_selected()) {
1572
1573                                         if (unselected_neighbour != view_stripables.end()) {
1574
1575                                                 PresentationInfo::order_t unselected_neighbour_order = unselected_neighbour->stripable->presentation_info().order();
1576                                                 PresentationInfo::order_t my_order = vsi->stripable->presentation_info().order();
1577
1578                                                 unselected_neighbour->stripable->set_presentation_order (my_order);
1579                                                 vsi->stripable->set_presentation_order (unselected_neighbour_order);
1580
1581                                                 if (!scroll_to) {
1582                                                         scroll_to = vsi->tav;
1583                                                 }
1584                                         }
1585
1586                                 } else {
1587
1588                                         if (vsi->tav) {
1589                                                 unselected_neighbour = vsi;
1590                                         }
1591
1592                                 }
1593
1594                         } while (vsi != view_stripables.begin());
1595                 }
1596         }
1597
1598         if (scroll_to) {
1599                 _editor->ensure_time_axis_view_is_visible (*scroll_to, false);
1600         }
1601 }
1602
1603 void
1604 EditorRoutes::update_input_active_display ()
1605 {
1606         TreeModel::Children rows = _model->children();
1607         TreeModel::Children::iterator i;
1608
1609         for (i = rows.begin(); i != rows.end(); ++i) {
1610                 boost::shared_ptr<Stripable> stripable = (*i)[_columns.stripable];
1611
1612                 if (boost::dynamic_pointer_cast<Track> (stripable)) {
1613                         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (stripable);
1614
1615                         if (mt) {
1616                                 (*i)[_columns.is_input_active] = mt->input_active();
1617                         }
1618                 }
1619         }
1620 }
1621
1622 void
1623 EditorRoutes::update_rec_display ()
1624 {
1625         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
1626                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1627         }
1628 }
1629
1630 bool
1631 EditorRoutes::idle_update_mute_rec_solo_etc()
1632 {
1633         g_atomic_int_set (const_cast<gint*>(&_queue_tv_update), 0);
1634         TreeModel::Children rows = _model->children();
1635         TreeModel::Children::iterator i;
1636
1637         for (i = rows.begin(); i != rows.end(); ++i) {
1638                 boost::shared_ptr<Stripable> stripable = (*i)[_columns.stripable];
1639                 boost::shared_ptr<Route> route = boost::dynamic_pointer_cast<Route> (stripable);
1640                 (*i)[_columns.mute_state] = RouteUI::mute_active_state (_session, stripable);
1641                 (*i)[_columns.solo_state] = RouteUI::solo_active_state (stripable);
1642                 (*i)[_columns.solo_isolate_state] = RouteUI::solo_isolate_active_state (stripable) ? 1 : 0;
1643                 (*i)[_columns.solo_safe_state] = RouteUI::solo_safe_active_state (stripable) ? 1 : 0;
1644                 if (route) {
1645                         (*i)[_columns.active] = route->active ();
1646                 } else {
1647                         (*i)[_columns.active] = true;
1648                 }
1649
1650                 boost::shared_ptr<Track> trk (boost::dynamic_pointer_cast<Track>(route));
1651
1652                 if (trk) {
1653                         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (route);
1654
1655                         if (trk->rec_enable_control()->get_value()) {
1656                                 if (_session->record_status() == Session::Recording) {
1657                                         (*i)[_columns.rec_state] = 1;
1658                                 } else {
1659                                         (*i)[_columns.rec_state] = 2;
1660                                 }
1661                         } else if (mt && mt->step_editing()) {
1662                                 (*i)[_columns.rec_state] = 3;
1663                         } else {
1664                                 (*i)[_columns.rec_state] = 0;
1665                         }
1666
1667                         (*i)[_columns.rec_safe] = trk->rec_safe_control()->get_value();
1668                         (*i)[_columns.name_editable] = !trk->rec_enable_control()->get_value();
1669                 }
1670         }
1671
1672         return false; // do not call again (until needed)
1673 }
1674
1675
1676 void
1677 EditorRoutes::update_mute_display ()
1678 {
1679         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
1680                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1681         }
1682 }
1683
1684 void
1685 EditorRoutes::update_solo_display ()
1686 {
1687         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
1688                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1689         }
1690 }
1691
1692 void
1693 EditorRoutes::update_solo_isolate_display ()
1694 {
1695         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
1696                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1697         }
1698 }
1699
1700 void
1701 EditorRoutes::update_solo_safe_display ()
1702 {
1703         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
1704                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1705         }
1706 }
1707
1708 list<TimeAxisView*>
1709 EditorRoutes::views () const
1710 {
1711         list<TimeAxisView*> v;
1712         for (TreeModel::Children::iterator i = _model->children().begin(); i != _model->children().end(); ++i) {
1713                 v.push_back ((*i)[_columns.tv]);
1714         }
1715
1716         return v;
1717 }
1718
1719 void
1720 EditorRoutes::clear ()
1721 {
1722         PBD::Unwinder<bool> uw (_ignore_selection_change, true);
1723         _display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
1724         _model->clear ();
1725         _display.set_model (_model);
1726 }
1727
1728 void
1729 EditorRoutes::name_edit_started (CellEditable* ce, const Glib::ustring&)
1730 {
1731         name_editable = ce;
1732
1733         /* give it a special name */
1734
1735         Gtk::Entry *e = dynamic_cast<Gtk::Entry*> (ce);
1736
1737         if (e) {
1738                 e->set_name (X_("RouteNameEditorEntry"));
1739         }
1740 }
1741
1742 void
1743 EditorRoutes::name_edit (std::string const & path, std::string const & new_text)
1744 {
1745         name_editable = 0;
1746
1747         TreeIter iter = _model->get_iter (path);
1748
1749         if (!iter) {
1750                 return;
1751         }
1752
1753         boost::shared_ptr<Stripable> stripable = (*iter)[_columns.stripable];
1754
1755         if (stripable && stripable->name() != new_text) {
1756                 stripable->set_name (new_text);
1757         }
1758 }
1759
1760 void
1761 EditorRoutes::solo_changed_so_update_mute ()
1762 {
1763         update_mute_display ();
1764 }
1765
1766 void
1767 EditorRoutes::show_tracks_with_regions_at_playhead ()
1768 {
1769         boost::shared_ptr<RouteList> const r = _session->get_routes_with_regions_at (_session->transport_sample ());
1770
1771         set<TimeAxisView*> show;
1772         for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
1773                 TimeAxisView* tav = _editor->time_axis_view_from_stripable (*i);
1774                 if (tav) {
1775                         show.insert (tav);
1776                 }
1777         }
1778
1779         DisplaySuspender ds;
1780
1781         TreeModel::Children rows = _model->children ();
1782         for (TreeModel::Children::iterator i = rows.begin(); i != rows.end(); ++i) {
1783                 TimeAxisView* tv = (*i)[_columns.tv];
1784                 bool to_show = (show.find (tv) != show.end());
1785
1786                 tv->set_marked_for_display (to_show);
1787                 (*i)[_columns.visible] = to_show;
1788         }
1789
1790         /* force route order keys catch up with visibility changes
1791          */
1792
1793         sync_presentation_info_from_treeview ();
1794 }
1795
1796 int
1797 EditorRoutes::plugin_setup (boost::shared_ptr<Route> r, boost::shared_ptr<PluginInsert> pi, ARDOUR::Route::PluginSetupOptions flags)
1798 {
1799         PluginSetupDialog psd (r, pi, flags);
1800         int rv = psd.run ();
1801         return rv + (psd.fan_out() ? 4 : 0);
1802 }