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