86a1fdbe5719d9c8a9bcfaf6276a6ceb6e2ca9ac
[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                 printf ("SKIP redisplay\n");
560                 return;
561         }
562
563         redisplay_real ();
564
565         while (!g_atomic_int_compare_and_exchange (&_redisplay_active, 1, 0)) {
566                 g_atomic_int_set(&_redisplay_active, 1);
567                 redisplay_real ();
568         }
569 }
570
571 void
572 EditorRoutes::row_deleted (Gtk::TreeModel::Path const &)
573 {
574         /* this happens as the second step of a DnD within the treeview, and
575          * when a route is actually removed. we don't differentiate between
576          * the two cases.
577          *
578          * note that the sync_orders_keys() step may not actually change any
579          * RID's (e.g. the last track may be removed, so all other tracks keep
580          * the same RID), which means that no redisplay would happen. so we
581          * have to force a redisplay.
582          */
583
584         DEBUG_TRACE (DEBUG::OrderKeys, "editor routes treeview row deleted\n");
585
586         DisplaySuspender ds;
587         sync_order_keys_from_treeview ();
588 }
589
590 void
591 EditorRoutes::reordered (TreeModel::Path const &, TreeModel::iterator const &, int* /*what*/)
592 {
593         /* reordering implies that RID's will change, so sync_order_keys() will
594            cause a redisplay.
595         */
596
597         DEBUG_TRACE (DEBUG::OrderKeys, "editor routes treeview reordered\n");
598         sync_order_keys_from_treeview ();
599 }
600
601 void
602 EditorRoutes::visible_changed (std::string const & path)
603 {
604         if (_session && _session->deletion_in_progress()) {
605                 return;
606         }
607
608         DisplaySuspender ds;
609         TreeIter iter;
610
611         if ((iter = _model->get_iter (path))) {
612                 TimeAxisView* tv = (*iter)[_columns.tv];
613                 if (tv) {
614                         bool visible = (*iter)[_columns.visible];
615
616                         if (tv->set_marked_for_display (!visible)) {
617                                 update_visibility ();
618                         }
619                 }
620         }
621 }
622
623 void
624 EditorRoutes::active_changed (std::string const & path)
625 {
626         if (_session && _session->deletion_in_progress ()) {
627                 return;
628         }
629
630         Gtk::TreeModel::Row row = *_model->get_iter (path);
631         boost::shared_ptr<Route> route = row[_columns.route];
632         bool const active = row[_columns.active];
633         route->set_active (!active, this);
634 }
635
636 void
637 EditorRoutes::routes_added (list<RouteTimeAxisView*> routes)
638 {
639         PBD::Unwinder<bool> at (_adding_routes, true);
640
641         bool from_scratch = (_model->children().size() == 0);
642         Gtk::TreeModel::Children::iterator insert_iter = _model->children().end();
643
644         for (Gtk::TreeModel::Children::iterator it = _model->children().begin(); it != _model->children().end(); ++it) {
645                 boost::shared_ptr<Route> r = (*it)[_columns.route];
646
647                 if (r->order_key() == (routes.front()->route()->order_key() + routes.size())) {
648                         insert_iter = it;
649                         break;
650                 }
651         }
652
653         if(!from_scratch) {
654                 _editor->selection->tracks.clear();
655         }
656
657         DisplaySuspender ds;
658
659         _display.set_model (Glib::RefPtr<ListStore>());
660
661         for (list<RouteTimeAxisView*>::iterator x = routes.begin(); x != routes.end(); ++x) {
662
663                 boost::shared_ptr<MidiTrack> midi_trk = boost::dynamic_pointer_cast<MidiTrack> ((*x)->route());
664
665                 TreeModel::Row row = *(_model->insert (insert_iter));
666
667                 row[_columns.text] = (*x)->route()->name();
668                 row[_columns.visible] = (*x)->marked_for_display();
669                 row[_columns.active] = (*x)->route()->active ();
670                 row[_columns.tv] = *x;
671                 row[_columns.route] = (*x)->route ();
672                 row[_columns.is_track] = (boost::dynamic_pointer_cast<Track> ((*x)->route()) != 0);
673
674                 if (midi_trk) {
675                         row[_columns.is_input_active] = midi_trk->input_active ();
676                         row[_columns.is_midi] = true;
677                 } else {
678                         row[_columns.is_input_active] = false;
679                         row[_columns.is_midi] = false;
680                 }
681
682                 row[_columns.mute_state] = (*x)->route()->muted() ? Gtkmm2ext::ExplicitActive : Gtkmm2ext::Off;
683                 row[_columns.solo_state] = RouteUI::solo_active_state ((*x)->route());
684                 row[_columns.solo_visible] = !(*x)->route()->is_master ();
685                 row[_columns.solo_isolate_state] = (*x)->route()->solo_isolated();
686                 row[_columns.solo_safe_state] = (*x)->route()->solo_safe();
687                 row[_columns.name_editable] = true;
688
689                 if (!from_scratch) {
690                         _editor->selection->add(*x);
691                 }
692
693                 boost::weak_ptr<Route> wr ((*x)->route());
694
695                 (*x)->route()->gui_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::handle_gui_changes, this, _1, _2), gui_context());
696                 (*x)->route()->PropertyChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::route_property_changed, this, _1, wr), gui_context());
697
698                 if ((*x)->is_track()) {
699                         boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> ((*x)->route());
700                         t->RecordEnableChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_rec_display, this), gui_context());
701                 }
702
703                 if ((*x)->is_midi_track()) {
704                         boost::shared_ptr<MidiTrack> t = boost::dynamic_pointer_cast<MidiTrack> ((*x)->route());
705                         t->StepEditStatusChange.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_rec_display, this), gui_context());
706                         t->InputActiveChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_input_active_display, this), gui_context());
707                 }
708
709                 (*x)->route()->mute_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_mute_display, this), gui_context());
710                 (*x)->route()->solo_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_solo_display, this, _1), gui_context());
711                 (*x)->route()->listen_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_solo_display, this, _1), gui_context());
712                 (*x)->route()->solo_isolated_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_solo_isolate_display, this), gui_context());
713                 (*x)->route()->solo_safe_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_solo_safe_display, this), gui_context());
714                 (*x)->route()->active_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_active_display, this), gui_context ());
715
716         }
717
718         update_rec_display ();
719         update_mute_display ();
720         update_solo_display (true);
721         update_solo_isolate_display ();
722         update_solo_safe_display ();
723         update_input_active_display ();
724         update_active_display ();
725
726         _display.set_model (_model);
727
728         /* now update route order keys from the treeview/track display order */
729
730         sync_order_keys_from_treeview ();
731 }
732
733 void
734 EditorRoutes::handle_gui_changes (string const & what, void*)
735 {
736         if (_adding_routes) {
737                 return;
738         }
739
740         if (what == "track_height") {
741                 /* Optional :make tracks change height while it happens, instead
742                    of on first-idle
743                 */
744                 redisplay ();
745         }
746
747         if (what == "visible_tracks") {
748                 redisplay ();
749         }
750 }
751
752 void
753 EditorRoutes::route_removed (TimeAxisView *tv)
754 {
755         ENSURE_GUI_THREAD (*this, &EditorRoutes::route_removed, tv)
756
757         TreeModel::Children rows = _model->children();
758         TreeModel::Children::iterator ri;
759
760         for (ri = rows.begin(); ri != rows.end(); ++ri) {
761                 if ((*ri)[_columns.tv] == tv) {
762                         PBD::Unwinder<bool> uw (_route_deletion_in_progress, true);
763                         _model->erase (ri);
764                         break;
765                 }
766         }
767
768         /* the deleted signal for the treeview/model will take
769            care of any updates.
770         */
771 }
772
773 void
774 EditorRoutes::route_property_changed (const PropertyChange& what_changed, boost::weak_ptr<Route> r)
775 {
776         if (!what_changed.contains (ARDOUR::Properties::name)) {
777                 return;
778         }
779
780         ENSURE_GUI_THREAD (*this, &EditorRoutes::route_name_changed, r)
781
782         boost::shared_ptr<Route> route = r.lock ();
783
784         if (!route) {
785                 return;
786         }
787
788         TreeModel::Children rows = _model->children();
789         TreeModel::Children::iterator i;
790
791         for (i = rows.begin(); i != rows.end(); ++i) {
792                 boost::shared_ptr<Route> t = (*i)[_columns.route];
793                 if (t == route) {
794                         (*i)[_columns.text] = route->name();
795                         break;
796                 }
797         }
798 }
799
800 void
801 EditorRoutes::update_active_display ()
802 {
803         if (g_atomic_int_compare_and_exchange (&_queue_tv_update, 0, 1)) {
804                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
805         }
806 }
807
808 void
809 EditorRoutes::update_visibility ()
810 {
811         TreeModel::Children rows = _model->children();
812         TreeModel::Children::iterator i;
813
814         DisplaySuspender ds ();
815
816         for (i = rows.begin(); i != rows.end(); ++i) {
817                 TimeAxisView *tv = (*i)[_columns.tv];
818                 (*i)[_columns.visible] = tv->marked_for_display ();
819         }
820
821         /* force route order keys catch up with visibility changes
822          */
823
824         sync_order_keys_from_treeview ();
825 }
826
827 void
828 EditorRoutes::hide_track_in_display (TimeAxisView& tv)
829 {
830         TreeModel::Children rows = _model->children();
831         TreeModel::Children::iterator i;
832
833         for (i = rows.begin(); i != rows.end(); ++i) {
834                 if ((*i)[_columns.tv] == &tv) {
835                         tv.set_marked_for_display (false);
836                         (*i)[_columns.visible] = false;
837                         redisplay ();
838                         break;
839                 }
840         }
841 }
842
843 void
844 EditorRoutes::show_track_in_display (TimeAxisView& tv)
845 {
846         TreeModel::Children rows = _model->children();
847         TreeModel::Children::iterator i;
848
849
850         for (i = rows.begin(); i != rows.end(); ++i) {
851                 if ((*i)[_columns.tv] == &tv) {
852                         tv.set_marked_for_display (true);
853                         (*i)[_columns.visible] = true;
854                         redisplay ();
855                         break;
856                 }
857         }
858 }
859
860 void
861 EditorRoutes::reset_remote_control_ids ()
862 {
863         if (Config->get_remote_model() == UserOrdered || !_session || _session->deletion_in_progress()) {
864                 return;
865         }
866
867         TreeModel::Children rows = _model->children();
868
869         if (rows.empty()) {
870                 return;
871         }
872
873
874         DEBUG_TRACE (DEBUG::OrderKeys, "editor reset remote control ids\n");
875
876         TreeModel::Children::iterator ri;
877         bool rid_change = false;
878         uint32_t rid = 1;
879         uint32_t invisible_key = UINT32_MAX;
880
881         for (ri = rows.begin(); ri != rows.end(); ++ri) {
882
883                 boost::shared_ptr<Route> route = (*ri)[_columns.route];
884                 bool visible = (*ri)[_columns.visible];
885
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         if (_display.get_selection()->count_selected_rows() > 0) {
1330
1331                 TreeIter iter;
1332                 TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
1333                 TrackViewList selected;
1334
1335                 _editor->get_selection().clear_regions ();
1336
1337                 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
1338
1339                         if ((iter = _model->get_iter (*i))) {
1340
1341                                 TimeAxisView* tv = (*iter)[_columns.tv];
1342                                 selected.push_back (tv);
1343                         }
1344
1345                 }
1346
1347                 _editor->get_selection().set (selected);
1348                 _editor->ensure_time_axis_view_is_visible (*(selected.front()), true);
1349
1350         } else {
1351                 _editor->get_selection().clear_tracks ();
1352         }
1353 }
1354
1355 bool
1356 EditorRoutes::selection_filter (Glib::RefPtr<TreeModel> const &, TreeModel::Path const&, bool /*selected*/)
1357 {
1358         if (selection_countdown) {
1359                 if (--selection_countdown == 0) {
1360                         return true;
1361                 } else {
1362                         /* no selection yet ... */
1363                         return false;
1364                 }
1365         }
1366         return true;
1367 }
1368
1369 struct EditorOrderRouteSorter
1370 {
1371         bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
1372                 if (a->is_master()) {
1373                         /* master before everything else */
1374                         return true;
1375                 } else if (b->is_master()) {
1376                         /* everything else before master */
1377                         return false;
1378                 }
1379                 return a->order_key () < b->order_key ();
1380         }
1381 };
1382
1383 void
1384 EditorRoutes::initial_display ()
1385 {
1386         DisplaySuspender ds;
1387         _model->clear ();
1388
1389         if (!_session) {
1390                 return;
1391         }
1392
1393         boost::shared_ptr<RouteList> routes = _session->get_routes();
1394
1395         if (ARDOUR_UI::instance()->session_is_new ()) {
1396
1397                 /* new session: stamp all routes with the right editor order
1398                  * key
1399                  */
1400
1401                 _editor->add_routes (*(routes.get()));
1402
1403         } else {
1404
1405                 /* existing session: sort a copy of the route list by
1406                  * editor-order and add its contents to the display.
1407                  */
1408
1409                 RouteList r (*routes);
1410                 EditorOrderRouteSorter sorter;
1411
1412                 r.sort (sorter);
1413                 _editor->add_routes (r);
1414
1415         }
1416 }
1417
1418 void
1419 EditorRoutes::display_drag_data_received (const RefPtr<Gdk::DragContext>& context,
1420                                              int x, int y,
1421                                              const SelectionData& data,
1422                                              guint info, guint time)
1423 {
1424         if (data.get_target() == "GTK_TREE_MODEL_ROW") {
1425                 _display.on_drag_data_received (context, x, y, data, info, time);
1426                 return;
1427         }
1428
1429         context->drag_finish (true, false, time);
1430 }
1431
1432 void
1433 EditorRoutes::move_selected_tracks (bool up)
1434 {
1435         if (_editor->selection->tracks.empty()) {
1436                 return;
1437         }
1438
1439         typedef std::pair<TimeAxisView*,boost::shared_ptr<Route> > ViewRoute;
1440         std::list<ViewRoute> view_routes;
1441         std::vector<int> neworder;
1442         TreeModel::Children rows = _model->children();
1443         TreeModel::Children::iterator ri;
1444
1445         for (ri = rows.begin(); ri != rows.end(); ++ri) {
1446                 TimeAxisView* tv = (*ri)[_columns.tv];
1447                 boost::shared_ptr<Route> route = (*ri)[_columns.route];
1448
1449                 view_routes.push_back (ViewRoute (tv, route));
1450         }
1451
1452         list<ViewRoute>::iterator trailing;
1453         list<ViewRoute>::iterator leading;
1454
1455         if (up) {
1456
1457                 trailing = view_routes.begin();
1458                 leading = view_routes.begin();
1459
1460                 ++leading;
1461
1462                 while (leading != view_routes.end()) {
1463                         if (_editor->selection->selected (leading->first)) {
1464                                 view_routes.insert (trailing, ViewRoute (leading->first, leading->second));
1465                                 leading = view_routes.erase (leading);
1466                         } else {
1467                                 ++leading;
1468                                 ++trailing;
1469                         }
1470                 }
1471
1472         } else {
1473
1474                 /* if we could use reverse_iterator in list::insert, this code
1475                    would be a beautiful reflection of the code above. but we can't
1476                    and so it looks like a bit of a mess.
1477                 */
1478
1479                 trailing = view_routes.end();
1480                 leading = view_routes.end();
1481
1482                 --leading; if (leading == view_routes.begin()) { return; }
1483                 --leading;
1484                 --trailing;
1485
1486                 while (1) {
1487
1488                         if (_editor->selection->selected (leading->first)) {
1489                                 list<ViewRoute>::iterator tmp;
1490
1491                                 /* need to insert *after* trailing, not *before* it,
1492                                    which is what insert (iter, val) normally does.
1493                                 */
1494
1495                                 tmp = trailing;
1496                                 tmp++;
1497
1498                                 view_routes.insert (tmp, ViewRoute (leading->first, leading->second));
1499
1500                                 /* can't use iter = cont.erase (iter); form here, because
1501                                    we need iter to move backwards.
1502                                 */
1503
1504                                 tmp = leading;
1505                                 --tmp;
1506
1507                                 bool done = false;
1508
1509                                 if (leading == view_routes.begin()) {
1510                                         /* the one we've just inserted somewhere else
1511                                            was the first in the list. erase this copy,
1512                                            and then break, because we're done.
1513                                         */
1514                                         done = true;
1515                                 }
1516
1517                                 view_routes.erase (leading);
1518
1519                                 if (done) {
1520                                         break;
1521                                 }
1522
1523                                 leading = tmp;
1524
1525                         } else {
1526                                 if (leading == view_routes.begin()) {
1527                                         break;
1528                                 }
1529                                 --leading;
1530                                 --trailing;
1531                         }
1532                 };
1533         }
1534
1535         for (leading = view_routes.begin(); leading != view_routes.end(); ++leading) {
1536                 uint32_t order = leading->second->order_key ();
1537                 neworder.push_back (order);
1538         }
1539
1540 #ifndef NDEBUG
1541         DEBUG_TRACE (DEBUG::OrderKeys, "New order after moving tracks:\n");
1542         for (vector<int>::iterator i = neworder.begin(); i != neworder.end(); ++i) {
1543                 DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("\t%1\n", *i));
1544         }
1545         DEBUG_TRACE (DEBUG::OrderKeys, "-------\n");
1546
1547         for (vector<int>::iterator i = neworder.begin(); i != neworder.end(); ++i) {
1548                 if (*i >= (int) neworder.size()) {
1549                         cerr << "Trying to move something to " << *i << " of " << neworder.size() << endl;
1550                 }
1551                 assert (*i < (int) neworder.size ());
1552         }
1553 #endif
1554
1555         _model->reorder (neworder);
1556 }
1557
1558 void
1559 EditorRoutes::update_input_active_display ()
1560 {
1561         TreeModel::Children rows = _model->children();
1562         TreeModel::Children::iterator i;
1563
1564         for (i = rows.begin(); i != rows.end(); ++i) {
1565                 boost::shared_ptr<Route> route = (*i)[_columns.route];
1566
1567                 if (boost::dynamic_pointer_cast<Track> (route)) {
1568                         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (route);
1569
1570                         if (mt) {
1571                                 (*i)[_columns.is_input_active] = mt->input_active();
1572                         }
1573                 }
1574         }
1575 }
1576
1577 void
1578 EditorRoutes::update_rec_display ()
1579 {
1580         if (g_atomic_int_compare_and_exchange (&_queue_tv_update, 0, 1)) {
1581                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1582         }
1583 }
1584
1585 bool
1586 EditorRoutes::idle_update_mute_rec_solo_etc()
1587 {
1588         g_atomic_int_set (&_queue_tv_update, 0);
1589         TreeModel::Children rows = _model->children();
1590         TreeModel::Children::iterator i;
1591
1592         for (i = rows.begin(); i != rows.end(); ++i) {
1593                 boost::shared_ptr<Route> route = (*i)[_columns.route];
1594                 (*i)[_columns.mute_state] = RouteUI::mute_active_state (_session, route);
1595                 (*i)[_columns.solo_state] = RouteUI::solo_active_state (route);
1596                 (*i)[_columns.solo_isolate_state] = RouteUI::solo_isolate_active_state (route) ? 1 : 0;
1597                 (*i)[_columns.solo_safe_state] = RouteUI::solo_safe_active_state (route) ? 1 : 0;
1598                 (*i)[_columns.active] = route->active ();
1599                 {
1600                         if (boost::dynamic_pointer_cast<Track> (route)) {
1601                                 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (route);
1602
1603                                 if (route->record_enabled()) {
1604                                         if (_session->record_status() == Session::Recording) {
1605                                                 (*i)[_columns.rec_state] = 1;
1606                                         } else {
1607                                                 (*i)[_columns.rec_state] = 2;
1608                                         }
1609                                 } else if (mt && mt->step_editing()) {
1610                                         (*i)[_columns.rec_state] = 3;
1611                                 } else {
1612                                         (*i)[_columns.rec_state] = 0;
1613                                 }
1614
1615                                 (*i)[_columns.name_editable] = !route->record_enabled ();
1616                         }
1617                 }
1618         }
1619         return false; // do not call again (until needed)
1620 }
1621
1622
1623 void
1624 EditorRoutes::update_mute_display ()
1625 {
1626         if (g_atomic_int_compare_and_exchange (&_queue_tv_update, 0, 1)) {
1627                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1628         }
1629 }
1630
1631 void
1632 EditorRoutes::update_solo_display (bool /* selfsoloed */)
1633 {
1634         if (g_atomic_int_compare_and_exchange (&_queue_tv_update, 0, 1)) {
1635                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1636         }
1637 }
1638
1639 void
1640 EditorRoutes::update_solo_isolate_display ()
1641 {
1642         if (g_atomic_int_compare_and_exchange (&_queue_tv_update, 0, 1)) {
1643                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1644         }
1645 }
1646
1647 void
1648 EditorRoutes::update_solo_safe_display ()
1649 {
1650         if (g_atomic_int_compare_and_exchange (&_queue_tv_update, 0, 1)) {
1651                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1652         }
1653 }
1654
1655 list<TimeAxisView*>
1656 EditorRoutes::views () const
1657 {
1658         list<TimeAxisView*> v;
1659         for (TreeModel::Children::iterator i = _model->children().begin(); i != _model->children().end(); ++i) {
1660                 v.push_back ((*i)[_columns.tv]);
1661         }
1662
1663         return v;
1664 }
1665
1666 void
1667 EditorRoutes::clear ()
1668 {
1669         _display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
1670         _model->clear ();
1671         _display.set_model (_model);
1672 }
1673
1674 void
1675 EditorRoutes::name_edit_started (CellEditable* ce, const Glib::ustring&)
1676 {
1677         name_editable = ce;
1678
1679         /* give it a special name */
1680
1681         Gtk::Entry *e = dynamic_cast<Gtk::Entry*> (ce);
1682
1683         if (e) {
1684                 e->set_name (X_("RouteNameEditorEntry"));
1685         }
1686 }
1687
1688 void
1689 EditorRoutes::name_edit (std::string const & path, std::string const & new_text)
1690 {
1691         name_editable = 0;
1692
1693         TreeIter iter = _model->get_iter (path);
1694
1695         if (!iter) {
1696                 return;
1697         }
1698
1699         boost::shared_ptr<Route> route = (*iter)[_columns.route];
1700
1701         if (route && route->name() != new_text) {
1702                 route->set_name (new_text);
1703         }
1704 }
1705
1706 void
1707 EditorRoutes::solo_changed_so_update_mute ()
1708 {
1709         update_mute_display ();
1710 }
1711
1712 void
1713 EditorRoutes::show_tracks_with_regions_at_playhead ()
1714 {
1715         boost::shared_ptr<RouteList> const r = _session->get_routes_with_regions_at (_session->transport_frame ());
1716
1717         set<TimeAxisView*> show;
1718         for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
1719                 TimeAxisView* tav = _editor->axis_view_from_route (*i);
1720                 if (tav) {
1721                         show.insert (tav);
1722                 }
1723         }
1724
1725         DisplaySuspender ds;
1726
1727         TreeModel::Children rows = _model->children ();
1728         for (TreeModel::Children::iterator i = rows.begin(); i != rows.end(); ++i) {
1729                 TimeAxisView* tv = (*i)[_columns.tv];
1730                 (*i)[_columns.visible] = (show.find (tv) != show.end());
1731         }
1732 }