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