debug the impossible
[ardour.git] / gtk2_ardour / route_ui.cc
1 /*
2     Copyright (C) 2002-2006 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 <gtkmm2ext/gtk_ui.h>
21 #include <gtkmm2ext/choice.h>
22 #include <gtkmm2ext/doi.h>
23 #include <gtkmm2ext/bindable_button.h>
24 #include <gtkmm2ext/barcontroller.h>
25 #include <gtkmm2ext/gtk_ui.h>
26
27 #include "ardour/route_group.h"
28 #include "ardour/dB.h"
29 #include "pbd/memento_command.h"
30 #include "pbd/stacktrace.h"
31 #include "pbd/controllable.h"
32 #include "pbd/enumwriter.h"
33
34 #include "ardour_ui.h"
35 #include "editor.h"
36 #include "route_ui.h"
37 #include "led.h"
38 #include "keyboard.h"
39 #include "utils.h"
40 #include "prompter.h"
41 #include "gui_thread.h"
42 #include "ardour_dialog.h"
43 #include "latency_gui.h"
44 #include "mixer_strip.h"
45 #include "automation_time_axis.h"
46 #include "route_time_axis.h"
47 #include "group_tabs.h"
48
49 #include "ardour/route.h"
50 #include "ardour/event_type_map.h"
51 #include "ardour/session.h"
52 #include "ardour/audioengine.h"
53 #include "ardour/audio_track.h"
54 #include "ardour/midi_track.h"
55 #include "ardour/template_utils.h"
56 #include "ardour/filename_extensions.h"
57 #include "ardour/directory_names.h"
58 #include "ardour/profile.h"
59
60 #include "i18n.h"
61 using namespace Gtk;
62 using namespace Gtkmm2ext;
63 using namespace ARDOUR;
64 using namespace PBD;
65
66 uint32_t RouteUI::_max_invert_buttons = 3;
67
68 RouteUI::RouteUI (ARDOUR::Session* sess)
69         : AxisView(sess)
70 {
71         init ();
72 }
73
74 RouteUI::~RouteUI()
75 {
76         _route.reset (); /* drop reference to route, so that it can be cleaned up */
77         route_connections.drop_connections ();
78
79         delete solo_menu;
80         delete mute_menu;
81         delete sends_menu;
82         delete record_menu;
83         delete _invert_menu;
84 }
85
86 void
87 RouteUI::init ()
88 {
89         self_destruct = true;
90         mute_menu = 0;
91         solo_menu = 0;
92         sends_menu = 0;
93         record_menu = 0;
94         _invert_menu = 0;
95         pre_fader_mute_check = 0;
96         post_fader_mute_check = 0;
97         listen_mute_check = 0;
98         main_mute_check = 0;
99         solo_safe_check = 0;
100         solo_isolated_check = 0;
101         solo_isolated_led = 0;
102         solo_safe_led = 0;
103         _solo_release = 0;
104         _mute_release = 0;
105         denormal_menu_item = 0;
106         step_edit_item = 0;
107         multiple_mute_change = false;
108         multiple_solo_change = false;
109         _i_am_the_modifier = 0;
110
111         setup_invert_buttons ();
112
113         mute_button = manage (new BindableToggleButton ());
114         // mute_button->set_self_managed (true);
115         mute_button->set_name ("MuteButton");
116         mute_button->add (mute_button_label);
117         mute_button_label.show ();
118         UI::instance()->set_tip (mute_button, _("Mute this track"), "");
119
120         solo_button = manage (new BindableToggleButton ());
121         // solo_button->set_self_managed (true);
122         solo_button->set_name ("SoloButton");
123         solo_button->add (solo_button_label);
124         solo_button_label.show ();
125         UI::instance()->set_tip (solo_button, _("Mute other (non-soloed) tracks"), "");
126         solo_button->set_no_show_all (true);
127
128         rec_enable_button = manage (new BindableToggleButton ());
129         rec_enable_button->set_name ("RecordEnableButton");
130         // rec_enable_button->set_self_managed (true);
131         rec_enable_button->add (rec_enable_button_label);
132         rec_enable_button_label.show ();
133         UI::instance()->set_tip (rec_enable_button, _("Enable recording on this track"), "");
134
135         show_sends_button = manage (new BindableToggleButton (""));
136         show_sends_button->set_name ("SendAlert");
137         // show_sends_button->set_self_managed (true);
138         UI::instance()->set_tip (show_sends_button, _("make mixer strips show sends to this bus"), "");
139
140         _session->SoloChanged.connect (_session_connections, invalidator (*this), boost::bind (&RouteUI::solo_changed_so_update_mute, this), gui_context());
141         _session->TransportStateChange.connect (_session_connections, invalidator (*this), boost::bind (&RouteUI::check_rec_enable_sensitivity, this), gui_context());
142         _session->RecordStateChanged.connect (_session_connections, invalidator (*this), boost::bind (&RouteUI::session_rec_enable_changed, this), gui_context());
143
144         Config->ParameterChanged.connect (*this, invalidator (*this), ui_bind (&RouteUI::parameter_changed, this, _1), gui_context());
145
146         rec_enable_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::rec_enable_press), false);
147         rec_enable_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::rec_enable_release), false);
148
149         show_sends_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::show_sends_press), false);
150         show_sends_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::show_sends_release));
151
152         solo_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::solo_press), false);
153         solo_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::solo_release), false);
154         mute_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::mute_press), false);
155         mute_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::mute_release), false);
156 }
157
158 void
159 RouteUI::reset ()
160 {
161         route_connections.drop_connections ();
162
163         delete solo_menu;
164         solo_menu = 0;
165
166         delete mute_menu;
167         mute_menu = 0;
168
169         denormal_menu_item = 0;
170 }
171
172 void
173 RouteUI::self_delete ()
174 {
175         delete this;
176 }
177
178 void
179 RouteUI::set_route (boost::shared_ptr<Route> rp)
180 {
181         reset ();
182
183         _route = rp;
184
185         if (set_color_from_route()) {
186                 set_color (unique_random_color());
187         }
188
189         if (self_destruct) {
190                 rp->DropReferences.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::self_delete, this), gui_context());
191         }
192
193         mute_button->set_controllable (_route->mute_control());
194         solo_button->set_controllable (_route->solo_control());
195
196         _route->active_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::route_active_changed, this), gui_context());
197         _route->mute_changed.connect (route_connections, invalidator (*this), ui_bind (&RouteUI::mute_changed, this, _1), gui_context());
198
199         _route->solo_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::update_solo_display, this), gui_context());
200         _route->solo_safe_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::update_solo_display, this), gui_context());
201         _route->listen_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::update_solo_display, this), gui_context());
202         _route->solo_isolated_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::update_solo_display, this), gui_context());
203
204         _route->phase_invert_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::polarity_changed, this), gui_context());
205         _route->PropertyChanged.connect (route_connections, invalidator (*this), ui_bind (&RouteUI::property_changed, this, _1), gui_context());
206
207         _route->io_changed.connect (route_connections, invalidator (*this), ui_bind (&RouteUI::setup_invert_buttons, this), gui_context ());
208         _route->gui_changed.connect (route_connections, invalidator (*this), ui_bind (&RouteUI::route_gui_changed, this, _1), gui_context ());
209
210         if (_session->writable() && is_track()) {
211                 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track>(_route);
212
213                 t->RecordEnableChanged.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::route_rec_enable_changed, this), gui_context());
214
215                 rec_enable_button->show();
216                 rec_enable_button->set_controllable (t->rec_enable_control());
217
218                 update_rec_display ();
219
220                 if (is_midi_track()) {
221                         midi_track()->StepEditStatusChange.connect (route_connections, invalidator (*this),
222                                                                     ui_bind (&RouteUI::step_edit_changed, this, _1), gui_context());
223                 }
224
225         }
226
227         mute_button->unset_flags (Gtk::CAN_FOCUS);
228         solo_button->unset_flags (Gtk::CAN_FOCUS);
229
230         mute_button->show();
231
232         if (_route->is_monitor()) {
233                 solo_button->hide ();
234         } else {
235                 solo_button->show();
236         }
237
238         map_frozen ();
239
240         setup_invert_buttons ();
241         set_invert_button_state ();
242 }
243
244 void
245 RouteUI::polarity_changed ()
246 {
247         if (!_route) {
248                 return;
249         }
250
251         set_invert_button_state ();
252 }
253
254 bool
255 RouteUI::mute_press (GdkEventButton* ev)
256 {
257         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS ) {
258                 return true;
259         }
260
261         multiple_mute_change = false;
262
263         if (!_i_am_the_modifier) {
264
265                 if (Keyboard::is_context_menu_event (ev)) {
266
267                         if (mute_menu == 0){
268                                 build_mute_menu();
269                         }
270
271                         mute_menu->popup(0,ev->time);
272
273                 } else {
274
275                         if (Keyboard::is_button2_event (ev)) {
276                                 // Primary-button2 click is the midi binding click
277                                 // button2-click is "momentary"
278
279
280                                 if (mute_button->on_button_press_event (ev)) {
281                                         return true;
282                                 }
283
284                                 _mute_release = new SoloMuteRelease (_route->muted ());
285                         }
286
287                         if (ev->button == 1 || Keyboard::is_button2_event (ev)) {
288
289                                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
290
291                                         if (_mute_release) {
292                                                 _mute_release->routes = _session->get_routes ();
293                                         }
294
295                                         _session->set_mute (_session->get_routes(), !_route->muted());
296
297                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
298
299                                         /* Primary-button1 applies change to the mix group even if it is not active
300                                            NOTE: Primary-button2 is MIDI learn.
301                                         */
302
303                                         if (ev->button == 1 && _route->route_group()) {
304                                                 if (_mute_release) {
305                                                         _mute_release->routes = _session->get_routes ();
306                                                 }
307
308                                                 _session->set_mute (_session->get_routes(), !_route->muted(), Session::rt_cleanup, true);
309                                         }
310
311                                 } else {
312
313                                         /* plain click applies change to this route */
314
315                                         boost::shared_ptr<RouteList> rl (new RouteList);
316                                         rl->push_back (_route);
317
318                                         if (_mute_release) {
319                                                 _mute_release->routes = rl;
320                                         }
321
322                                         _session->set_mute (rl, !_route->muted());
323
324                                 }
325                         }
326                 }
327
328         }
329
330         return true;
331 }
332
333 bool
334 RouteUI::mute_release (GdkEventButton*)
335 {
336         if (!_i_am_the_modifier) {
337                 if (_mute_release){
338                         _session->set_mute (_mute_release->routes, _mute_release->active, Session::rt_cleanup, true);
339                         delete _mute_release;
340                         _mute_release = 0;
341                 }
342         }
343
344         return true;
345 }
346
347 bool
348 RouteUI::solo_press(GdkEventButton* ev)
349 {
350         /* ignore double/triple clicks */
351
352         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS ) {
353                 return true;
354         }
355
356         multiple_solo_change = false;
357
358         if (!_i_am_the_modifier) {
359
360                 if (Keyboard::is_context_menu_event (ev)) {
361
362                         if (!solo_isolated_led) {
363
364                                 if (solo_menu == 0) {
365                                         build_solo_menu ();
366                                 }
367
368                                 solo_menu->popup (1, ev->time);
369                         }
370
371                 } else {
372
373                         if (Keyboard::is_button2_event (ev)) {
374
375                                 // Primary-button2 click is the midi binding click
376                                 // button2-click is "momentary"
377
378                                 if (solo_button->on_button_press_event (ev)) {
379                                         return true;
380                                 }
381
382                                 _solo_release = new SoloMuteRelease (_route->self_soloed());
383                         }
384
385                         if (ev->button == 1 || Keyboard::is_button2_event (ev)) {
386
387                                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
388
389                                         /* Primary-Tertiary-click applies change to all routes */
390
391                                         if (_solo_release) {
392                                                 _solo_release->routes = _session->get_routes ();
393                                         }
394
395                                         if (Config->get_solo_control_is_listen_control()) {
396                                                 _session->set_listen (_session->get_routes(), !_route->listening_via_monitor(),  Session::rt_cleanup, true);
397                                         } else {
398                                                 _session->set_solo (_session->get_routes(), !_route->self_soloed(),  Session::rt_cleanup, true);
399                                         }
400
401                                 } else if (Keyboard::modifier_state_contains (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::SecondaryModifier))) {
402
403                                         // Primary-Secondary-click: exclusively solo this track
404
405                                         if (_solo_release) {
406                                                 _solo_release->exclusive = true;
407
408                                                 boost::shared_ptr<RouteList> routes = _session->get_routes();
409
410                                                 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
411                                                         if ((*i)->soloed ()) {
412                                                                 _solo_release->routes_on->push_back (*i);
413                                                         } else {
414                                                                 _solo_release->routes_off->push_back (*i);
415                                                         }
416                                                 }
417                                         }
418
419                                         if (Config->get_solo_control_is_listen_control()) {
420                                                 /* ??? we need a just_one_listen() method */
421                                         } else {
422                                                 _session->set_just_one_solo (_route, true);
423                                         }
424
425                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
426
427                                         // shift-click: toggle solo isolated status
428
429                                         _route->set_solo_isolated (!_route->solo_isolated(), this);
430                                         delete _solo_release;
431                                         _solo_release = 0;
432
433                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
434
435                                         /* Primary-button1: solo mix group.
436                                            NOTE: Primary-button2 is MIDI learn.
437                                         */
438
439                                         if (ev->button == 1 && _route->route_group()) {
440
441                                                 if (_solo_release) {
442                                                         _solo_release->routes = _route->route_group()->route_list();
443                                                 }
444
445                                                 if (Config->get_solo_control_is_listen_control()) {
446                                                         _session->set_listen (_route->route_group()->route_list(), !_route->listening_via_monitor(),  Session::rt_cleanup, true);
447                                                 } else {
448                                                         _session->set_solo (_route->route_group()->route_list(), !_route->self_soloed(),  Session::rt_cleanup, true);
449                                                 }
450                                         }
451
452                                 } else {
453
454                                         /* click: solo this route */
455
456                                         boost::shared_ptr<RouteList> rl (new RouteList);
457                                         rl->push_back (route());
458
459                                         if (_solo_release) {
460                                                 _solo_release->routes = rl;
461                                         }
462
463                                         if (Config->get_solo_control_is_listen_control()) {
464                                                 _session->set_listen (rl, !_route->listening_via_monitor());
465                                         } else {
466                                                 _session->set_solo (rl, !_route->self_soloed());
467                                         }
468                                 }
469                         }
470                 }
471         }
472
473         return true;
474 }
475
476 bool
477 RouteUI::solo_release (GdkEventButton*)
478 {
479         if (!_i_am_the_modifier) {
480
481                 if (_solo_release) {
482
483                         if (_solo_release->exclusive) {
484
485                         } else {
486                                 if (Config->get_solo_control_is_listen_control()) {
487                                         _session->set_listen (_solo_release->routes, _solo_release->active, Session::rt_cleanup, true);
488                                 } else {
489                                         _session->set_solo (_solo_release->routes, _solo_release->active, Session::rt_cleanup, true);
490                                 }
491                         }
492
493                         delete _solo_release;
494                         _solo_release = 0;
495                 }
496         }
497
498         return true;
499 }
500
501 bool
502 RouteUI::rec_enable_press(GdkEventButton* ev)
503 {
504         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS ) {
505                 return true;
506         }
507
508         if (!_session->engine().connected()) {
509                 MessageDialog msg (_("Not connected to JACK - cannot engage record"));
510                 msg.run ();
511                 return true;
512         }
513
514         if (is_midi_track()) {
515
516                 /* cannot rec-enable while step-editing */
517
518                 if (midi_track()->step_editing()) {
519                         return true;
520                 }
521         }
522
523         if (!_i_am_the_modifier && is_track() && rec_enable_button) {
524
525                 if (Keyboard::is_button2_event (ev)) {
526
527                         // do nothing on midi sigc::bind event
528                         return rec_enable_button->on_button_press_event (ev);
529
530                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
531
532                         _session->set_record_enabled (_session->get_routes(), !rec_enable_button->get_active());
533
534                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
535
536                         /* Primary-button1 applies change to the route group (even if it is not active)
537                            NOTE: Primary-button2 is MIDI learn.
538                         */
539                         if (ev->button == 1 && _route->route_group()) {
540                                 _session->set_record_enabled (_route->route_group()->route_list(), !rec_enable_button->get_active(), Session::rt_cleanup, true);
541                         }
542
543                 } else if (Keyboard::is_context_menu_event (ev)) {
544
545                         /* do this on release */
546
547                 } else {
548
549                         boost::shared_ptr<RouteList> rl (new RouteList);
550                         rl->push_back (route());
551                         _session->set_record_enabled (rl, !rec_enable_button->get_active());
552                 }
553         }
554
555         return true;
556 }
557
558 void
559 RouteUI::build_record_menu ()
560 {
561         if (record_menu) {
562                 return;
563         }
564
565         /* no rec-button context menu for non-MIDI tracks
566          */
567
568         if (is_midi_track()) {
569                 record_menu = new Menu;
570                 record_menu->set_name ("ArdourContextMenu");
571
572                 using namespace Menu_Helpers;
573                 MenuList& items = record_menu->items();
574
575                 items.push_back (CheckMenuElem (_("Step Entry"), sigc::mem_fun (*this, &RouteUI::toggle_step_edit)));
576                 step_edit_item = dynamic_cast<CheckMenuItem*> (&items.back());
577
578                 if (_route->record_enabled()) {
579                         step_edit_item->set_sensitive (false);
580                 }
581
582                 step_edit_item->set_active (midi_track()->step_editing());
583         }
584 }
585
586 void
587 RouteUI::toggle_step_edit ()
588 {
589         if (!is_midi_track() || _route->record_enabled()) {
590                 return;
591         }
592
593         midi_track()->set_step_editing (step_edit_item->get_active());
594 }
595
596 void
597 RouteUI::step_edit_changed (bool yn)
598 {
599         if (yn) {
600                 if (rec_enable_button) {
601                         rec_enable_button->set_visual_state (3);
602                 }
603
604                 start_step_editing ();
605
606                 if (step_edit_item) {
607                         step_edit_item->set_active (true);
608                 }
609
610         } else {
611
612                 if (rec_enable_button) {
613                         rec_enable_button->set_visual_state (0);
614                 }
615
616                 stop_step_editing ();
617
618                 if (step_edit_item) {
619                         step_edit_item->set_active (false);
620                 }
621         }
622 }
623
624 bool
625 RouteUI::rec_enable_release (GdkEventButton* ev)
626 {
627         if (Keyboard::is_context_menu_event (ev)) {
628                 build_record_menu ();
629                 if (record_menu) {
630                         record_menu->popup (1, ev->time);
631                 }
632                 return true;
633         }
634
635         return true;
636 }
637
638 void
639 RouteUI::build_sends_menu ()
640 {
641         using namespace Menu_Helpers;
642
643         sends_menu = new Menu;
644         sends_menu->set_name ("ArdourContextMenu");
645         MenuList& items = sends_menu->items();
646
647         items.push_back (
648                 MenuElem(_("Assign all tracks (prefader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_sends), PreFader, false))
649                 );
650
651         items.push_back (
652                 MenuElem(_("Assign all tracks and buses (prefader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_sends), PreFader, true))
653                 );
654
655         items.push_back (
656                 MenuElem(_("Assign all tracks (postfader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_sends), PostFader, false))
657                 );
658
659         items.push_back (
660                 MenuElem(_("Assign all tracks and buses (postfader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_sends), PostFader, true))
661                 );
662
663         items.push_back (
664                 MenuElem(_("Assign selected tracks (prefader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_selected_sends), PreFader, false))
665                 );
666
667         items.push_back (
668                 MenuElem(_("Assign selected tracks and buses (prefader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_selected_sends), PreFader, true)));
669
670         items.push_back (
671                 MenuElem(_("Assign selected tracks (postfader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_selected_sends), PostFader, false))
672                 );
673
674         items.push_back (
675                 MenuElem(_("Assign selected tracks and buses (postfader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_selected_sends), PostFader, true))
676                 );
677
678         items.push_back (MenuElem(_("Copy track/bus gains to sends"), sigc::mem_fun (*this, &RouteUI::set_sends_gain_from_track)));
679         items.push_back (MenuElem(_("Set sends gain to -inf"), sigc::mem_fun (*this, &RouteUI::set_sends_gain_to_zero)));
680         items.push_back (MenuElem(_("Set sends gain to 0dB"), sigc::mem_fun (*this, &RouteUI::set_sends_gain_to_unity)));
681
682 }
683
684 void
685 RouteUI::create_sends (Placement p, bool include_buses)
686 {
687         _session->globally_add_internal_sends (_route, p, include_buses);
688 }
689
690 void
691 RouteUI::create_selected_sends (Placement p, bool include_buses)
692 {
693         boost::shared_ptr<RouteList> rlist (new RouteList);
694         TrackSelection& selected_tracks (ARDOUR_UI::instance()->the_editor().get_selection().tracks);
695
696         for (TrackSelection::iterator i = selected_tracks.begin(); i != selected_tracks.end(); ++i) {
697                 RouteTimeAxisView* rtv;
698                 RouteUI* rui;
699                 if ((rtv = dynamic_cast<RouteTimeAxisView*>(*i)) != 0) {
700                         if ((rui = dynamic_cast<RouteUI*>(rtv)) != 0) {
701                                 if (include_buses || boost::dynamic_pointer_cast<AudioTrack>(rui->route())) {
702                                         rlist->push_back (rui->route());
703                                 }
704                         }
705                 }
706         }
707
708         _session->add_internal_sends (_route, p, rlist);
709 }
710
711 void
712 RouteUI::set_sends_gain_from_track ()
713 {
714         _session->globally_set_send_gains_from_track (_route);
715 }
716
717 void
718 RouteUI::set_sends_gain_to_zero ()
719 {
720         _session->globally_set_send_gains_to_zero (_route);
721 }
722
723 void
724 RouteUI::set_sends_gain_to_unity ()
725 {
726         _session->globally_set_send_gains_to_unity (_route);
727 }
728
729 bool
730 RouteUI::show_sends_press(GdkEventButton* ev)
731 {
732         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS ) {
733                 return true;
734         }
735
736         if (!_i_am_the_modifier && !is_track() && show_sends_button) {
737
738                 if (Keyboard::is_button2_event (ev) && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
739
740                         // do nothing on midi sigc::bind event
741                         return false;
742
743                 } else if (Keyboard::is_context_menu_event (ev)) {
744
745                         if (sends_menu == 0) {
746                                 build_sends_menu ();
747                         }
748
749                         sends_menu->popup (0, ev->time);
750
751                 } else {
752
753                         /* change button state */
754
755                         show_sends_button->set_active (!show_sends_button->get_active());
756
757                         /* start blinking */
758
759                         if (show_sends_button->get_active()) {
760                                 /* show sends to this bus */
761                                 MixerStrip::SwitchIO (_route);
762                                 send_blink_connection = ARDOUR_UI::instance()->Blink.connect (sigc::mem_fun(*this, &RouteUI::send_blink));
763                         } else {
764                                 /* everybody back to normal */
765                                 send_blink_connection.disconnect ();
766                                 MixerStrip::SwitchIO (boost::shared_ptr<Route>());
767                         }
768
769                 }
770         }
771
772         return true;
773 }
774
775 bool
776 RouteUI::show_sends_release (GdkEventButton*)
777 {
778         return true;
779 }
780
781 void
782 RouteUI::send_blink (bool onoff)
783 {
784         if (!show_sends_button) {
785                 return;
786         }
787
788         if (onoff) {
789                 show_sends_button->set_state (STATE_ACTIVE);
790         } else {
791                 show_sends_button->set_state (STATE_NORMAL);
792         }
793 }
794
795 int
796 RouteUI::solo_visual_state (boost::shared_ptr<Route> r)
797 {
798         if (r->is_master() || r->is_monitor()) {
799                 return 0;
800         }
801
802         if (Config->get_solo_control_is_listen_control()) {
803
804                 if (r->listening_via_monitor()) {
805                         return 1;
806                 } else {
807                         return 0;
808                 }
809
810         }
811
812         if (r->soloed()) {
813                 if (!r->self_soloed()) {
814                         return 3;
815                 } else {
816                         return 1;
817                 }
818         } else {
819                 return 0;
820         }
821 }
822
823 int
824 RouteUI::solo_visual_state_with_isolate (boost::shared_ptr<Route> r)
825 {
826         if (r->is_master() || r->is_monitor()) {
827                 return 0;
828         }
829
830         if (Config->get_solo_control_is_listen_control()) {
831
832                 if (r->listening_via_monitor()) {
833                         return 1;
834                 } else {
835                         return 0;
836                 }
837
838         }
839
840         if (r->solo_isolated()) {
841                 return 2;
842         } else if (r->soloed()) {
843                 if (!r->self_soloed()) {
844                         return 3;
845                 } else {
846                         return 1;
847                 }
848         } else {
849                 return 0;
850         }
851 }
852
853 int
854 RouteUI::solo_isolate_visual_state (boost::shared_ptr<Route> r)
855 {
856         if (r->is_master() || r->is_monitor()) {
857                 return 0;
858         }
859
860         if (r->solo_isolated()) {
861                 return 1;
862         } else {
863                 return 0;
864         }
865 }
866
867 int
868 RouteUI::solo_safe_visual_state (boost::shared_ptr<Route> r)
869 {
870         if (r->is_master() || r->is_monitor()) {
871                 return 0;
872         }
873
874         if (r->solo_safe()) {
875                 return 1;
876         } else {
877                 return 0;
878         }
879 }
880
881 void
882 RouteUI::update_solo_display ()
883 {
884         bool x;
885
886         if (Config->get_solo_control_is_listen_control()) {
887
888                 if (solo_button->get_active() != (x = _route->listening_via_monitor())) {
889                         ++_i_am_the_modifier;
890                         solo_button->set_active(x);
891                         --_i_am_the_modifier;
892                 }
893
894         } else {
895
896                 if (solo_button->get_active() != (x = _route->soloed())) {
897                         ++_i_am_the_modifier;
898                         solo_button->set_active (x);
899                         --_i_am_the_modifier;
900                 }
901
902         }
903
904         bool yn = _route->solo_safe ();
905
906         if (solo_safe_check && solo_safe_check->get_active() != yn) {
907                 solo_safe_check->set_active (yn);
908         }
909
910         yn = _route->solo_isolated ();
911
912         if (solo_isolated_check && solo_isolated_check->get_active() != yn) {
913                 solo_isolated_check->set_active (yn);
914         }
915
916         set_button_names ();
917
918         if (solo_isolated_led) {
919                 solo_isolated_led->set_visual_state (_route->solo_isolated() ? 1 : 0);
920         }
921
922         if (solo_safe_led) {
923                 solo_safe_led->set_visual_state (_route->solo_safe() ? 1 : 0);
924         }
925
926         solo_button->set_visual_state (solo_visual_state (_route));
927
928         /* some changes to solo status can affect mute display, so catch up
929          */
930
931         update_mute_display ();
932 }
933
934 void
935 RouteUI::solo_changed_so_update_mute ()
936 {
937         update_mute_display ();
938 }
939
940 void
941 RouteUI::mute_changed(void* /*src*/)
942 {
943         update_mute_display ();
944 }
945
946 int
947 RouteUI::mute_visual_state (Session* s, boost::shared_ptr<Route> r)
948 {
949         if (r->is_master() || r->is_monitor()) {
950                 return 0;
951         }
952
953
954         if (Config->get_show_solo_mutes() && !Config->get_solo_control_is_listen_control ()) {
955
956                 if (r->muted ()) {
957                         /* full mute */
958                         return 2;
959                 } else if (s->soloing() && !r->soloed() && !r->solo_isolated()) {
960                         return 1;
961                 } else {
962                         /* no mute at all */
963                         return 0;
964                 }
965
966         } else {
967
968                 if (r->muted()) {
969                         /* full mute */
970                         return 2;
971                 } else {
972                         /* no mute at all */
973                         return 0;
974                 }
975         }
976
977         return 0;
978 }
979
980 void
981 RouteUI::update_mute_display ()
982 {
983         if (!_route) {
984                 return;
985         }
986
987         bool model = _route->muted();
988         bool view = mute_button->get_active();
989
990         /* first make sure the button's "depressed" visual
991            is correct.
992         */
993
994         if (model != view) {
995                 ++_i_am_the_modifier;
996                 mute_button->set_active (model);
997                 --_i_am_the_modifier;
998         }
999
1000         mute_button->set_visual_state (mute_visual_state (_session, _route));
1001 }
1002
1003 void
1004 RouteUI::route_rec_enable_changed ()
1005 {
1006         update_rec_display ();
1007 }
1008
1009 void
1010 RouteUI::session_rec_enable_changed ()
1011 {
1012         update_rec_display ();
1013 }
1014
1015 void
1016 RouteUI::update_rec_display ()
1017 {
1018         if (!rec_enable_button || !_route) {
1019                 return;
1020         }
1021
1022         bool model = _route->record_enabled();
1023         bool view = rec_enable_button->get_active();
1024
1025         /* first make sure the button's "depressed" visual
1026            is correct.
1027         */
1028
1029         if (model != view) {
1030                 ++_i_am_the_modifier;
1031                 rec_enable_button->set_active (model);
1032                 --_i_am_the_modifier;
1033         }
1034
1035         /* now make sure its color state is correct */
1036
1037         if (model) {
1038                 switch (_session->record_status ()) {
1039                 case Session::Recording:
1040                         rec_enable_button->set_visual_state (1);
1041                         break;
1042
1043                 case Session::Disabled:
1044                 case Session::Enabled:
1045                         rec_enable_button->set_visual_state (2);
1046                         break;
1047
1048                 }
1049
1050                 if (step_edit_item) {
1051                         step_edit_item->set_sensitive (false);
1052                 }
1053
1054         } else {
1055                 rec_enable_button->set_visual_state (0);
1056
1057                 if (step_edit_item) {
1058                         step_edit_item->set_sensitive (true);
1059                 }
1060         }
1061
1062
1063         check_rec_enable_sensitivity ();
1064 }
1065
1066 void
1067 RouteUI::build_solo_menu (void)
1068 {
1069         using namespace Menu_Helpers;
1070
1071         solo_menu = new Menu;
1072         solo_menu->set_name ("ArdourContextMenu");
1073         MenuList& items = solo_menu->items();
1074         CheckMenuItem* check;
1075
1076         check = new CheckMenuItem(_("Solo Isolate"));
1077         check->set_active (_route->solo_isolated());
1078         check->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_solo_isolated), check));
1079         items.push_back (CheckMenuElem(*check));
1080         solo_isolated_check = dynamic_cast<CheckMenuItem*>(&items.back());
1081         check->show_all();
1082
1083         check = new CheckMenuItem(_("Solo Safe"));
1084         check->set_active (_route->solo_safe());
1085         check->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_solo_safe), check));
1086         items.push_back (CheckMenuElem(*check));
1087         solo_safe_check = dynamic_cast<CheckMenuItem*>(&items.back());
1088         check->show_all();
1089
1090         //items.push_back (SeparatorElem());
1091         // items.push_back (MenuElem (_("MIDI Bind"), sigc::mem_fun (*mute_button, &BindableToggleButton::midi_learn)));
1092
1093 }
1094
1095 void
1096 RouteUI::build_mute_menu(void)
1097 {
1098         using namespace Menu_Helpers;
1099
1100         mute_menu = new Menu;
1101         mute_menu->set_name ("ArdourContextMenu");
1102
1103         MenuList& items = mute_menu->items();
1104
1105         pre_fader_mute_check = manage (new CheckMenuItem(_("Pre Fader")));
1106         init_mute_menu(MuteMaster::PreFader, pre_fader_mute_check);
1107         pre_fader_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::PreFader, pre_fader_mute_check));
1108         items.push_back (CheckMenuElem(*pre_fader_mute_check));
1109         pre_fader_mute_check->show_all();
1110
1111         post_fader_mute_check = manage (new CheckMenuItem(_("Post Fader")));
1112         init_mute_menu(MuteMaster::PostFader, post_fader_mute_check);
1113         post_fader_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::PostFader, post_fader_mute_check));
1114         items.push_back (CheckMenuElem(*post_fader_mute_check));
1115         post_fader_mute_check->show_all();
1116
1117         listen_mute_check = manage (new CheckMenuItem(_("Control Outs")));
1118         init_mute_menu(MuteMaster::Listen, listen_mute_check);
1119         listen_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::Listen, listen_mute_check));
1120         items.push_back (CheckMenuElem(*listen_mute_check));
1121         listen_mute_check->show_all();
1122
1123         main_mute_check = manage (new CheckMenuItem(_("Main Outs")));
1124         init_mute_menu(MuteMaster::Main, main_mute_check);
1125         main_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::Main, main_mute_check));
1126         items.push_back (CheckMenuElem(*main_mute_check));
1127         main_mute_check->show_all();
1128
1129         //items.push_back (SeparatorElem());
1130         // items.push_back (MenuElem (_("MIDI Bind"), sigc::mem_fun (*mute_button, &BindableToggleButton::midi_learn)));
1131
1132         _route->mute_points_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::muting_change, this), gui_context());
1133 }
1134
1135 void
1136 RouteUI::init_mute_menu(MuteMaster::MutePoint mp, CheckMenuItem* check)
1137 {
1138         check->set_active (_route->mute_points() & mp);
1139 }
1140
1141 void
1142 RouteUI::toggle_mute_menu(MuteMaster::MutePoint mp, Gtk::CheckMenuItem* check)
1143 {
1144         if (check->get_active()) {
1145                 _route->set_mute_points (MuteMaster::MutePoint (_route->mute_points() | mp));
1146         } else {
1147                 _route->set_mute_points (MuteMaster::MutePoint (_route->mute_points() & ~mp));
1148         }
1149 }
1150
1151 void
1152 RouteUI::muting_change ()
1153 {
1154         ENSURE_GUI_THREAD (*this, &RouteUI::muting_change)
1155
1156         bool yn;
1157         MuteMaster::MutePoint current = _route->mute_points ();
1158
1159         yn = (current & MuteMaster::PreFader);
1160
1161         if (pre_fader_mute_check->get_active() != yn) {
1162                 pre_fader_mute_check->set_active (yn);
1163         }
1164
1165         yn = (current & MuteMaster::PostFader);
1166
1167         if (post_fader_mute_check->get_active() != yn) {
1168                 post_fader_mute_check->set_active (yn);
1169         }
1170
1171         yn = (current & MuteMaster::Listen);
1172
1173         if (listen_mute_check->get_active() != yn) {
1174                 listen_mute_check->set_active (yn);
1175         }
1176
1177         yn = (current & MuteMaster::Main);
1178
1179         if (main_mute_check->get_active() != yn) {
1180                 main_mute_check->set_active (yn);
1181         }
1182 }
1183
1184 bool
1185 RouteUI::solo_isolate_button_release (GdkEventButton* ev)
1186 {
1187         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS) {
1188                 return true;
1189         }
1190
1191         bool view = (solo_isolated_led->visual_state() != 0);
1192         bool model = _route->solo_isolated();
1193
1194         /* called BEFORE the view has changed */
1195
1196         if (ev->button == 1) {
1197                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
1198
1199                         if (model) {
1200                                 /* disable isolate for all routes */
1201                                 _session->set_solo_isolated (_session->get_routes(), false, Session::rt_cleanup, true);
1202                         }
1203
1204                 } else {
1205                         if (model == view) {
1206
1207                                 /* flip just this route */
1208
1209                                 boost::shared_ptr<RouteList> rl (new RouteList);
1210                                 rl->push_back (_route);
1211                                 _session->set_solo_isolated (rl, !view, Session::rt_cleanup, true);
1212                         }
1213                 }
1214         }
1215
1216         return true;
1217 }
1218
1219 bool
1220 RouteUI::solo_safe_button_release (GdkEventButton*)
1221 {
1222         _route->set_solo_safe (!(solo_safe_led->visual_state() > 0), this);
1223         return true;
1224 }
1225
1226 void
1227 RouteUI::toggle_solo_isolated (Gtk::CheckMenuItem* check)
1228 {
1229         bool view = check->get_active();
1230         bool model = _route->solo_isolated();
1231
1232         /* called AFTER the view has changed */
1233
1234         if (model != view) {
1235                 _route->set_solo_isolated (view, this);
1236         }
1237 }
1238
1239 void
1240 RouteUI::toggle_solo_safe (Gtk::CheckMenuItem* check)
1241 {
1242         _route->set_solo_safe (check->get_active(), this);
1243 }
1244
1245 /** Ask the user to choose a colour, and then set all selected tracks
1246  *  to that colour.
1247  */
1248 void
1249 RouteUI::choose_color ()
1250 {
1251         bool picked;
1252         Gdk::Color const color = Gtkmm2ext::UI::instance()->get_color (_("Color Selection"), picked, &_color);
1253
1254         if (picked) {
1255                 ARDOUR_UI::instance()->the_editor().get_selection().tracks.foreach_route_ui (
1256                         boost::bind (&RouteUI::set_color, _1, color)
1257                         );
1258         }
1259 }
1260
1261 /** Set the route's own color.  This may not be used for display if
1262  *  the route is in a group which shares its color with its routes.
1263  */
1264 void
1265 RouteUI::set_color (const Gdk::Color & c)
1266 {
1267         char buf[64];
1268
1269         _color = c;
1270
1271         snprintf (buf, sizeof (buf), "%d:%d:%d", c.get_red(), c.get_green(), c.get_blue());
1272
1273         /* note: we use the route state ID here so that color is the same for both
1274            the time axis view and the mixer strip
1275         */
1276         
1277         gui_object_state().set<string> (route_state_id(), X_("color"), buf);
1278         _route->gui_changed ("color", (void *) 0); /* EMIT_SIGNAL */
1279 }
1280
1281 /** @return GUI state ID for things that are common to the route in all its representations */
1282 string
1283 RouteUI::route_state_id () const
1284 {
1285         return string_compose (X_("route %1"), _route->id().to_s());
1286 }
1287
1288 int
1289 RouteUI::set_color_from_route ()
1290 {
1291         const string str = gui_object_state().get_string (route_state_id(), X_("color"));
1292
1293         if (str.empty()) {
1294                 return 1;
1295         }
1296
1297         int r, g, b;
1298
1299         sscanf (str.c_str(), "%d:%d:%d", &r, &g, &b);
1300
1301         _color.set_red (r);
1302         _color.set_green (g);
1303         _color.set_blue (b);
1304
1305         return 0;
1306 }
1307
1308 void
1309 RouteUI::remove_this_route (bool apply_to_selection)
1310 {
1311         if (apply_to_selection) {
1312                 ARDOUR_UI::instance()->the_editor().get_selection().tracks.foreach_route_ui (boost::bind (&RouteUI::remove_this_route, _1, false));
1313         } else {
1314                 if ((route()->is_master() || route()->is_monitor()) &&
1315                     !Config->get_allow_special_bus_removal()) {
1316                         MessageDialog msg (_("That would be bad news ...."),
1317                                            false,
1318                                            Gtk::MESSAGE_INFO,
1319                                    Gtk::BUTTONS_OK);
1320                         msg.set_secondary_text (string_compose (_(
1321 "Removing the master or monitor bus is such a bad idea\n\
1322 that %1 is not going to allow it.\n\
1323 \n\
1324 If you really want to do this sort of thing\n\
1325 edit your ardour.rc file to set the\n\
1326 \"allow-special-bus-removal\" option to be \"yes\""), PROGRAM_NAME));
1327
1328                         msg.present ();
1329                         msg.run ();
1330                         return;
1331                 }
1332
1333                 vector<string> choices;
1334                 string prompt;
1335
1336                 if (is_track()) {
1337                         prompt  = string_compose (_("Do you really want to remove track \"%1\" ?\n\nYou may also lose the playlist used by this track.\n\n(This action cannot be undone, and the session file will be overwritten)"), _route->name());
1338                 } else {
1339                         prompt  = string_compose (_("Do you really want to remove bus \"%1\" ?\n\n(This action cannot be undone, and the session file will be overwritten)"), _route->name());
1340                 }
1341
1342                 choices.push_back (_("No, do nothing."));
1343                 choices.push_back (_("Yes, remove it."));
1344
1345                 string title;
1346                 if (is_track()) {
1347                         title = _("Remove track");
1348                 } else {
1349                         title = _("Remove bus");
1350                 }
1351
1352                 Choice prompter (title, prompt, choices);
1353
1354                 if (prompter.run () == 1) {
1355                         Glib::signal_idle().connect (sigc::bind (sigc::ptr_fun (&RouteUI::idle_remove_this_route), this));
1356                 }
1357         }
1358 }
1359
1360 gint
1361 RouteUI::idle_remove_this_route (RouteUI *rui)
1362 {
1363         rui->_session->remove_route (rui->route());
1364         return false;
1365 }
1366
1367 /** @return true if this name should be used for the route, otherwise false */
1368 bool
1369 RouteUI::verify_new_route_name (const std::string& name)
1370 {
1371         string::size_type colon = name.find (':');
1372
1373         if (colon == string::npos) {
1374                 return true;
1375         }
1376         
1377         cerr << "There is a colon in " << name << " at pos " << colon << endl;
1378
1379         MessageDialog colon_msg (
1380                 _("The use of colons (':') is discouraged in track and bus names.\nDo you want to use this new name?"),
1381                 false, MESSAGE_QUESTION, BUTTONS_NONE
1382                 );
1383         
1384         colon_msg.add_button (_("Use the new name"), Gtk::RESPONSE_ACCEPT);
1385         colon_msg.add_button (_("Re-edit the name"), Gtk::RESPONSE_CANCEL);
1386
1387         return (colon_msg.run () == Gtk::RESPONSE_ACCEPT);
1388 }
1389
1390 void
1391 RouteUI::route_rename ()
1392 {
1393         ArdourPrompter name_prompter (true);
1394         string result;
1395         bool done = false;
1396
1397         if (is_track()) {
1398                 name_prompter.set_title (_("Rename Track"));
1399         } else {
1400                 name_prompter.set_title (_("Rename Bus"));
1401         }
1402         name_prompter.set_prompt (_("New name:"));
1403         name_prompter.set_initial_text (_route->name());
1404         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
1405         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
1406         name_prompter.show_all ();
1407
1408         while (!done) {
1409                 switch (name_prompter.run ()) {
1410                 case Gtk::RESPONSE_ACCEPT:
1411                         name_prompter.get_result (result);
1412                         name_prompter.hide ();
1413                         if (result.length()) {
1414                                 if (verify_new_route_name (result)) {
1415                                         _route->set_name (result);
1416                                         done = true;
1417                                 } else {
1418                                         /* back to name prompter */
1419                                 }
1420
1421                         } else {
1422                                 /* nothing entered, just get out of here */
1423                                 done = true;
1424                         }
1425                         break;
1426                 }
1427         }
1428
1429         return;
1430
1431 }
1432
1433 void
1434 RouteUI::property_changed (const PropertyChange& what_changed)
1435 {
1436         if (what_changed.contains (ARDOUR::Properties::name)) {
1437                 name_label.set_text (_route->name());
1438         }
1439 }
1440
1441 void
1442 RouteUI::set_route_active (bool a, bool apply_to_selection)
1443 {
1444         if (apply_to_selection) {
1445                 ARDOUR_UI::instance()->the_editor().get_selection().tracks.foreach_route_ui (boost::bind (&RouteTimeAxisView::set_route_active, _1, a, false));
1446         } else {
1447                 _route->set_active (a, this);
1448         }
1449 }
1450
1451 void
1452 RouteUI::toggle_denormal_protection ()
1453 {
1454         if (denormal_menu_item) {
1455
1456                 bool x;
1457
1458                 ENSURE_GUI_THREAD (*this, &RouteUI::toggle_denormal_protection)
1459
1460                 if ((x = denormal_menu_item->get_active()) != _route->denormal_protection()) {
1461                         _route->set_denormal_protection (x);
1462                 }
1463         }
1464 }
1465
1466 void
1467 RouteUI::denormal_protection_changed ()
1468 {
1469         if (denormal_menu_item) {
1470                 denormal_menu_item->set_active (_route->denormal_protection());
1471         }
1472 }
1473
1474 void
1475 RouteUI::disconnect_input ()
1476 {
1477         _route->input()->disconnect (this);
1478 }
1479
1480 void
1481 RouteUI::disconnect_output ()
1482 {
1483         _route->output()->disconnect (this);
1484 }
1485
1486 bool
1487 RouteUI::is_track () const
1488 {
1489         return boost::dynamic_pointer_cast<Track>(_route) != 0;
1490 }
1491
1492 boost::shared_ptr<Track>
1493 RouteUI::track() const
1494 {
1495         return boost::dynamic_pointer_cast<Track>(_route);
1496 }
1497
1498 bool
1499 RouteUI::is_audio_track () const
1500 {
1501         return boost::dynamic_pointer_cast<AudioTrack>(_route) != 0;
1502 }
1503
1504 boost::shared_ptr<AudioTrack>
1505 RouteUI::audio_track() const
1506 {
1507         return boost::dynamic_pointer_cast<AudioTrack>(_route);
1508 }
1509
1510 bool
1511 RouteUI::is_midi_track () const
1512 {
1513         return boost::dynamic_pointer_cast<MidiTrack>(_route) != 0;
1514 }
1515
1516 boost::shared_ptr<MidiTrack>
1517 RouteUI::midi_track() const
1518 {
1519         return boost::dynamic_pointer_cast<MidiTrack>(_route);
1520 }
1521
1522 bool
1523 RouteUI::has_audio_outputs () const
1524 {
1525         return (_route->n_outputs().n_audio() > 0);
1526 }
1527
1528 string
1529 RouteUI::name() const
1530 {
1531         return _route->name();
1532 }
1533
1534 void
1535 RouteUI::map_frozen ()
1536 {
1537         ENSURE_GUI_THREAD (*this, &RouteUI::map_frozen)
1538
1539         AudioTrack* at = dynamic_cast<AudioTrack*>(_route.get());
1540
1541         if (at) {
1542                 switch (at->freeze_state()) {
1543                 case AudioTrack::Frozen:
1544                         rec_enable_button->set_sensitive (false);
1545                         break;
1546                 default:
1547                         rec_enable_button->set_sensitive (true);
1548                         break;
1549                 }
1550         }
1551 }
1552
1553 void
1554 RouteUI::adjust_latency ()
1555 {
1556         LatencyDialog dialog (_route->name() + _(" latency"), *(_route->output()), _session->frame_rate(), _session->engine().frames_per_cycle());
1557 }
1558
1559 void
1560 RouteUI::save_as_template ()
1561 {
1562         sys::path path;
1563         std::string safe_name;
1564         string name;
1565
1566         path = ARDOUR::user_route_template_directory ();
1567
1568         if (g_mkdir_with_parents (path.to_string().c_str(), 0755)) {
1569                 error << string_compose (_("Cannot create route template directory %1"), path.to_string()) << endmsg;
1570                 return;
1571         }
1572
1573         Prompter p (true); // modal
1574
1575         p.set_title (_("Save As Template"));
1576         p.set_prompt (_("Template name:"));
1577         p.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
1578         switch (p.run()) {
1579         case RESPONSE_ACCEPT:
1580                 break;
1581         default:
1582                 return;
1583         }
1584
1585         p.hide ();
1586         p.get_result (name, true);
1587
1588         safe_name = legalize_for_path (name);
1589         safe_name += template_suffix;
1590
1591         path /= safe_name;
1592
1593         _route->save_as_template (path.to_string(), name);
1594 }
1595
1596 void
1597 RouteUI::check_rec_enable_sensitivity ()
1598 {
1599         if (_session->transport_rolling() && rec_enable_button->get_active() && Config->get_disable_disarm_during_roll()) {
1600                 rec_enable_button->set_sensitive (false);
1601         } else {
1602                 rec_enable_button->set_sensitive (true);
1603         }
1604 }
1605
1606 void
1607 RouteUI::parameter_changed (string const & p)
1608 {
1609         if (p == "disable-disarm-during-roll") {
1610                 check_rec_enable_sensitivity ();
1611         } else if (p == "use-monitor-bus" || p == "solo-control-is-listen-control" || p == "listen-position") {
1612                 set_button_names ();
1613         }
1614 }
1615
1616 void
1617 RouteUI::step_gain_up ()
1618 {
1619         _route->set_gain (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) + 0.1), this);
1620 }
1621
1622 void
1623 RouteUI::page_gain_up ()
1624 {
1625         _route->set_gain (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) + 0.5), this);
1626 }
1627
1628 void
1629 RouteUI::step_gain_down ()
1630 {
1631         _route->set_gain (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) - 0.1), this);
1632 }
1633
1634 void
1635 RouteUI::page_gain_down ()
1636 {
1637         _route->set_gain (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) - 0.5), this);
1638 }
1639
1640 void
1641 RouteUI::open_remote_control_id_dialog ()
1642 {
1643         ArdourDialog dialog (_("Remote Control ID"));
1644
1645         uint32_t const limit = _session->ntracks() + _session->nbusses () + 4;
1646
1647         HBox* hbox = manage (new HBox);
1648         hbox->set_spacing (6);
1649         hbox->pack_start (*manage (new Label (_("Remote control ID:"))));
1650         SpinButton* spin = manage (new SpinButton);
1651         spin->set_digits (0);
1652         spin->set_increments (1, 10);
1653         spin->set_range (0, limit);
1654         spin->set_value (_route->remote_control_id());
1655         hbox->pack_start (*spin);
1656         dialog.get_vbox()->pack_start (*hbox);
1657
1658         dialog.add_button (Stock::CANCEL, RESPONSE_CANCEL);
1659         dialog.add_button (Stock::APPLY, RESPONSE_ACCEPT);
1660
1661         dialog.show_all ();
1662         int const r = dialog.run ();
1663
1664         if (r == RESPONSE_ACCEPT) {
1665                 _route->set_remote_control_id (spin->get_value_as_int ());
1666         }
1667 }
1668
1669 void
1670 RouteUI::setup_invert_buttons ()
1671 {
1672         /* remove old invert buttons */
1673         for (list<BindableToggleButton*>::iterator i = _invert_buttons.begin(); i != _invert_buttons.end(); ++i) {
1674                 _invert_button_box.remove (**i);
1675         }
1676
1677         _invert_buttons.clear ();
1678
1679         if (!_route || !_route->input()) {
1680                 return;
1681         }
1682
1683         uint32_t const N = _route->input()->n_ports().n_audio ();
1684
1685         uint32_t const to_add = (N <= _max_invert_buttons) ? N : 1;
1686
1687         for (uint32_t i = 0; i < to_add; ++i) {
1688                 BindableToggleButton* b = manage (new BindableToggleButton);
1689                 b->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &RouteUI::invert_toggled), i, b));
1690                 b->signal_button_press_event().connect (sigc::mem_fun (*this, &RouteUI::invert_press));
1691
1692                 b->set_name (X_("MixerInvertButton"));
1693                 if (to_add == 1) {
1694                         b->add (*manage (new Label (X_("Ø"))));
1695                 } else {
1696                         b->add (*manage (new Label (string_compose (X_("Ø%1"), i + 1))));
1697                 }
1698
1699                 if (N <= 4) {
1700                         UI::instance()->set_tip (*b, string_compose (_("Left-click to invert (phase reverse) channel %1 of this track.  Right-click to show menu."), i + 1));
1701                 } else {
1702                         UI::instance()->set_tip (*b, string_compose (_("Left-click to invert (phase reverse) all channels of this track.  Right-click to show menu."), i + 1));
1703                 }
1704
1705                 _invert_buttons.push_back (b);
1706                 _invert_button_box.pack_start (*b);
1707         }
1708
1709         _invert_button_box.show_all ();
1710 }
1711
1712 void
1713 RouteUI::set_invert_button_state ()
1714 {
1715         ++_i_am_the_modifier;
1716
1717         uint32_t const N = _route->input()->n_ports().n_audio();
1718         if (N > _max_invert_buttons) {
1719                 _invert_buttons.front()->set_active (_route->phase_invert().any());
1720                 --_i_am_the_modifier;
1721                 return;
1722         }
1723
1724         int j = 0;
1725         for (list<BindableToggleButton*>::iterator i = _invert_buttons.begin(); i != _invert_buttons.end(); ++i, ++j) {
1726                 (*i)->set_active (_route->phase_invert (j));
1727         }
1728
1729         --_i_am_the_modifier;
1730 }
1731
1732 void
1733 RouteUI::invert_toggled (uint32_t i, BindableToggleButton* b)
1734 {
1735         if (_i_am_the_modifier) {
1736                 return;
1737         }
1738
1739         uint32_t const N = _route->input()->n_ports().n_audio();
1740         if (N <= _max_invert_buttons) {
1741                 _route->set_phase_invert (i, b->get_active ());
1742         } else {
1743                 boost::dynamic_bitset<> p (N);
1744                 if (b->get_active ()) {
1745                         p.set ();
1746                 }
1747                 _route->set_phase_invert (p);
1748         }
1749 }
1750
1751 bool
1752 RouteUI::invert_press (GdkEventButton* ev)
1753 {
1754         using namespace Menu_Helpers;
1755
1756         if (ev->button != 3) {
1757                 return true;
1758         }
1759
1760         delete _invert_menu;
1761         _invert_menu = new Menu;
1762         _invert_menu->set_name ("ArdourContextMenu");
1763         MenuList& items = _invert_menu->items ();
1764
1765         uint32_t const N = _route->input()->n_ports().n_audio();
1766         for (uint32_t i = 0; i < N; ++i) {
1767                 items.push_back (CheckMenuElem (string_compose (X_("Ø%1"), i + 1), sigc::bind (sigc::mem_fun (*this, &RouteUI::invert_menu_toggled), i)));
1768                 CheckMenuItem* e = dynamic_cast<CheckMenuItem*> (&items.back ());
1769                 ++_i_am_the_modifier;
1770                 e->set_active (_route->phase_invert (i));
1771                 --_i_am_the_modifier;
1772         }
1773
1774         _invert_menu->popup (0, ev->time);
1775
1776         return false;
1777 }
1778
1779 void
1780 RouteUI::invert_menu_toggled (uint32_t c)
1781 {
1782         if (_i_am_the_modifier) {
1783                 return;
1784         }
1785
1786         _route->set_phase_invert (c, !_route->phase_invert (c));
1787 }
1788
1789 void
1790 RouteUI::set_invert_sensitive (bool yn)
1791 {
1792         for (list<BindableToggleButton*>::iterator b = _invert_buttons.begin(); b != _invert_buttons.end(); ++b) {
1793                 (*b)->set_sensitive (yn);
1794         }
1795 }
1796
1797 void
1798 RouteUI::request_redraw ()
1799 {
1800         if (_route) {
1801                 _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1802         }
1803 }
1804
1805 /** The Route's gui_changed signal has been emitted */
1806 void
1807 RouteUI::route_gui_changed (string what_changed)
1808 {
1809         if (what_changed == "color") {
1810                 if (set_color_from_route () == 0) {
1811                         route_color_changed ();
1812                 }
1813         }
1814 }
1815
1816 /** @return the color that this route should use; it maybe its own,
1817     or it maybe that of its route group.
1818 */
1819 Gdk::Color
1820 RouteUI::color () const
1821 {
1822         RouteGroup* g = _route->route_group ();
1823         
1824         if (g && g->is_color()) {
1825                 return GroupTabs::group_color (g);
1826         }
1827
1828         return _color;
1829 }