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