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