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