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