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