monitor section: make sip/afl/pfl buttons work, add rude solo, mono function, rearrange
[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/stop_signal.h>
22 #include <gtkmm2ext/choice.h>
23 #include <gtkmm2ext/doi.h>
24 #include <gtkmm2ext/bindable_button.h>
25 #include <gtkmm2ext/barcontroller.h>
26 #include <gtkmm2ext/gtk_ui.h>
27
28 #include "ardour/route_group.h"
29 #include "ardour/dB.h"
30 #include "pbd/memento_command.h"
31 #include "pbd/stacktrace.h"
32 #include "pbd/controllable.h"
33 #include "pbd/enumwriter.h"
34
35 #include "ardour_ui.h"
36 #include "editor.h"
37 #include "route_ui.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
48 #include "ardour/route.h"
49 #include "ardour/event_type_map.h"
50 #include "ardour/session.h"
51 #include "ardour/audioengine.h"
52 #include "ardour/audio_track.h"
53 #include "ardour/audio_diskstream.h"
54 #include "ardour/midi_track.h"
55 #include "ardour/midi_diskstream.h"
56 #include "ardour/template_utils.h"
57 #include "ardour/filename_extensions.h"
58 #include "ardour/directory_names.h"
59 #include "ardour/profile.h"
60
61 #include "i18n.h"
62 using namespace Gtk;
63 using namespace Gtkmm2ext;
64 using namespace ARDOUR;
65 using namespace PBD;
66
67 RouteUI::RouteUI (ARDOUR::Session* sess)
68         : AxisView(sess)
69 {
70         init ();
71 }
72
73 RouteUI::RouteUI (boost::shared_ptr<ARDOUR::Route> rt, ARDOUR::Session* sess)
74         : AxisView(sess)
75 {
76         init ();
77         set_route (rt);
78 }
79
80 RouteUI::~RouteUI()
81 {
82         _route.reset (); /* drop reference to route, so that it can be cleaned up */
83         route_connections.drop_connections ();
84
85         delete solo_menu;
86         delete mute_menu;
87         delete sends_menu;
88 }
89
90 void
91 RouteUI::init ()
92 {
93         self_destruct = true;
94         xml_node = 0;
95         mute_menu = 0;
96         solo_menu = 0;
97         sends_menu = 0;
98         pre_fader_mute_check = 0;
99         post_fader_mute_check = 0;
100         listen_mute_check = 0;
101         main_mute_check = 0;
102         ignore_toggle = false;
103         _solo_release = 0;
104         _mute_release = 0;
105         route_active_menu_item = 0;
106         polarity_menu_item = 0;
107         denormal_menu_item = 0;
108         multiple_mute_change = false;
109         multiple_solo_change = false;
110
111         mute_button = manage (new BindableToggleButton ());
112         mute_button->set_self_managed (true);
113         mute_button->set_name ("MuteButton");
114         mute_button->add (mute_button_label);
115         mute_button_label.show ();
116         UI::instance()->set_tip (mute_button, _("Mute this track"), "");
117
118         solo_button = manage (new BindableToggleButton ());
119         solo_button->set_self_managed (true);
120         solo_button->set_name ("SoloButton");
121         solo_button->add (solo_button_label);
122         solo_button_label.show ();
123         UI::instance()->set_tip (solo_button, _("Mute other (non-soloed) tracks"), "");
124         solo_button->set_no_show_all (true);
125
126         rec_enable_button = manage (new BindableToggleButton ());
127         rec_enable_button->set_name ("RecordEnableButton");
128         rec_enable_button->set_self_managed (true);
129         rec_enable_button->add (rec_enable_button_label);
130         rec_enable_button_label.show ();
131         UI::instance()->set_tip (rec_enable_button, _("Enable recording on this track"), "");
132
133         show_sends_button = manage (new BindableToggleButton (""));
134         show_sends_button->set_name ("SendAlert");
135         show_sends_button->set_self_managed (true);
136         UI::instance()->set_tip (show_sends_button, _("make mixer strips show sends to this bus"), "");
137
138         _session->SoloChanged.connect (_session_connections, boost::bind (&RouteUI::solo_changed_so_update_mute, this), gui_context());
139         _session->TransportStateChange.connect (_session_connections, boost::bind (&RouteUI::check_rec_enable_sensitivity, this), gui_context());
140         _session->RecordStateChanged.connect (_session_connections, boost::bind (&RouteUI::session_rec_enable_changed, this), gui_context());
141
142         Config->ParameterChanged.connect (*this, ui_bind (&RouteUI::parameter_changed, this, _1), gui_context());
143
144         rec_enable_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::rec_enable_press), false);
145         rec_enable_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::rec_enable_release), false);
146
147         show_sends_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::show_sends_press), false);
148         show_sends_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::show_sends_release));
149
150         solo_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::solo_press), false);
151         solo_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::solo_release), false);
152         mute_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::mute_press), false);
153         mute_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::mute_release), false);
154 }
155
156 void
157 RouteUI::reset ()
158 {
159         route_connections.drop_connections ();
160
161         delete solo_menu;
162         solo_menu = 0;
163
164         delete mute_menu;
165         mute_menu = 0;
166
167         if (xml_node) {
168                 /* do not delete the node - its owned by the route */
169                 xml_node = 0;
170         }
171
172         route_active_menu_item = 0;
173         polarity_menu_item = 0;
174         denormal_menu_item = 0;
175 }
176
177 void
178 RouteUI::self_delete ()
179 {
180         /* This may be called from a non-GUI thread. Keep it safe */
181
182         delete_when_idle (this);
183 }
184
185 void
186 RouteUI::set_route (boost::shared_ptr<Route> rp)
187 {
188         reset ();
189
190         _route = rp;
191
192         if (set_color_from_route()) {
193                 set_color (unique_random_color());
194         }
195
196         if (self_destruct) {
197                 rp->DropReferences.connect (route_connections, boost::bind (&RouteUI::self_delete, this), gui_context());
198         }
199         
200         mute_button->set_controllable (_route->mute_control());
201         solo_button->set_controllable (_route->solo_control());
202
203         _route->active_changed.connect (route_connections, boost::bind (&RouteUI::route_active_changed, this), gui_context());
204         _route->mute_changed.connect (route_connections, ui_bind (&RouteUI::mute_changed, this, _1), gui_context());
205         _route->solo_changed.connect (route_connections, ui_bind (&RouteUI::solo_changed, this, _1), gui_context());
206         _route->listen_changed.connect (route_connections, ui_bind (&RouteUI::listen_changed, this, _1), gui_context());
207         _route->solo_isolated_changed.connect (route_connections, ui_bind (&RouteUI::solo_changed, this, _1), gui_context());
208         _route->PropertyChanged.connect (route_connections, ui_bind (&RouteUI::property_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->diskstream()->RecordEnableChanged.connect (route_connections, 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
221         mute_button->unset_flags (Gtk::CAN_FOCUS);
222         solo_button->unset_flags (Gtk::CAN_FOCUS);
223
224         mute_button->show();
225
226         if (_route->is_control()) {
227                 solo_button->hide ();
228         } else {
229                 solo_button->show();
230         }
231
232         /* map the current state */
233
234         mute_changed (0);
235         solo_changed (0);
236
237         map_frozen ();
238 }
239
240 bool
241 RouteUI::mute_press (GdkEventButton* ev)
242 {
243         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS ) {
244                 return true;
245         }
246
247         multiple_mute_change = false;
248
249         if (!ignore_toggle) {
250
251                 if (Keyboard::is_context_menu_event (ev)) {
252
253                         if (mute_menu == 0){
254                                 build_mute_menu();
255                         }
256
257                         mute_menu->popup(0,ev->time);
258
259                 } else {
260
261                         if (Keyboard::is_button2_event (ev)) {
262                                 // Primary-button2 click is the midi binding click
263                                 // button2-click is "momentary"
264
265                                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier))) {
266                                         return false;
267                                 }
268
269                                 _mute_release = new SoloMuteRelease (_route->muted ());
270                         }
271
272                         if (ev->button == 1 || Keyboard::is_button2_event (ev)) {
273
274                                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
275
276                                         if (_mute_release) {
277                                                 _mute_release->routes = _session->get_routes ();
278                                         }
279
280                                         _session->set_mute (_session->get_routes(), !_route->muted());
281
282                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
283
284                                         /* Primary-button1 applies change to the mix group even if it is not active
285                                            NOTE: Primary-button2 is MIDI learn.
286                                         */
287
288                                         if (ev->button == 1 && _route->route_group()) {
289                                                 if (_mute_release) {
290                                                         _mute_release->routes = _session->get_routes ();
291                                                 }
292                                                                 
293                                                 _session->set_mute (_session->get_routes(), !_route->muted(), Session::rt_cleanup, true);
294                                         }
295
296                                 } else {
297
298                                         /* plain click applies change to this route */
299                                         
300                                         boost::shared_ptr<RouteList> rl (new RouteList);
301                                         rl->push_back (_route);
302
303                                         if (_mute_release) {
304                                                 _mute_release->routes = rl;
305                                         }
306
307                                         _session->set_mute (rl, !_route->muted());
308
309                                 }
310                         }
311                 }
312
313         }
314
315         return true;
316 }
317
318 bool
319 RouteUI::mute_release (GdkEventButton*)
320 {
321         if (!ignore_toggle) {
322                 if (_mute_release){
323                         _session->set_mute (_mute_release->routes, _mute_release->active, Session::rt_cleanup, true);
324                         delete _mute_release;
325                         _mute_release = 0;
326                 }
327         }
328
329         return true;
330 }
331
332 bool
333 RouteUI::solo_press(GdkEventButton* ev)
334 {
335         /* ignore double/triple clicks */
336
337         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS ) {
338                 return true;
339         }
340         
341         multiple_solo_change = false;
342
343         if (!ignore_toggle) {
344                 
345                 if (Keyboard::is_context_menu_event (ev)) {
346                         
347                         if (solo_menu == 0) {
348                                 build_solo_menu ();
349                         }
350                         
351                         solo_menu->popup (1, ev->time);
352                         
353                 } else {
354                         
355                         if (Keyboard::is_button2_event (ev)) {
356                                 
357                                 // Primary-button2 click is the midi binding click
358                                 // button2-click is "momentary"
359                                 
360                                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier))) {
361                                         return false;
362                                 }
363
364                                 _solo_release = new SoloMuteRelease (_route->soloed());
365                         }
366                         
367                         if (ev->button == 1 || Keyboard::is_button2_event (ev)) {
368                                 
369                                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
370                                         
371                                         /* Primary-Tertiary-click applies change to all routes */
372
373                                         if (_solo_release) {
374                                                 _solo_release->routes = _session->get_routes ();
375                                         }
376                                         
377                                         if (Config->get_solo_control_is_listen_control()) {
378                                                 _session->set_listen (_session->get_routes(), !_route->listening(),  Session::rt_cleanup, true);
379                                         } else {
380                                                 _session->set_solo (_session->get_routes(), !_route->soloed(),  Session::rt_cleanup, true);
381                                         }
382                                         
383                                 } else if (Keyboard::modifier_state_contains (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::SecondaryModifier))) {
384                                         
385                                         // Primary-Secondary-click: exclusively solo this track
386
387                                         if (_solo_release) {
388                                                 _solo_release->exclusive = true;
389
390                                                 boost::shared_ptr<RouteList> routes = _session->get_routes();
391
392                                                 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
393                                                         if ((*i)->soloed ()) {
394                                                                 _solo_release->routes_on->push_back (*i);
395                                                         } else {
396                                                                 _solo_release->routes_off->push_back (*i);
397                                                         }
398                                                 }
399                                         }
400                                         
401                                         if (Config->get_solo_control_is_listen_control()) {
402                                                 /* ??? we need a just_one_listen() method */
403                                         } else {
404                                                 _session->set_just_one_solo (_route, true);
405                                         }
406
407                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
408                                         
409                                         // shift-click: toggle solo isolated status
410                                         
411                                         _route->set_solo_isolated (!_route->solo_isolated(), this);
412                                         delete _solo_release;
413                                         _solo_release = 0;
414                                         
415                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
416                                         
417                                         /* Primary-button1: solo mix group.
418                                            NOTE: Primary-button2 is MIDI learn.
419                                         */
420                                         
421                                         if (ev->button == 1 && _route->route_group()) {
422
423                                                 if (_solo_release) {
424                                                         _solo_release->routes = _route->route_group()->route_list();
425                                                 }
426                                         
427                                                 if (Config->get_solo_control_is_listen_control()) {
428                                                         _session->set_listen (_route->route_group()->route_list(), !_route->listening(),  Session::rt_cleanup, true);
429                                                 } else {
430                                                         _session->set_solo (_route->route_group()->route_list(), !_route->soloed(),  Session::rt_cleanup, true);
431                                                 }
432                                         }
433                                         
434                                 } else {
435                                         
436                                         /* click: solo this route */
437                                         
438                                         boost::shared_ptr<RouteList> rl (new RouteList);
439                                         rl->push_back (route());
440
441                                         if (_solo_release) {
442                                                 _solo_release->routes = rl;
443                                         }
444
445                                         if (Config->get_solo_control_is_listen_control()) {
446                                                 _session->set_listen (rl, !_route->listening());
447                                         } else {
448                                                 _session->set_solo (rl, !_route->soloed());
449                                         }
450                                 }
451                         }
452                 }
453         }
454
455         return true;
456 }
457
458 bool
459 RouteUI::solo_release (GdkEventButton*)
460 {
461         if (!ignore_toggle) {
462                 
463                 if (_solo_release) {
464
465                         if (_solo_release->exclusive) {
466
467                         } else {
468                                 _session->set_solo (_solo_release->routes, _solo_release->active, Session::rt_cleanup, true);
469                         }
470
471                         delete _solo_release;
472                         _solo_release = 0;
473                 }
474         }
475
476         return true;
477 }
478
479 bool
480 RouteUI::rec_enable_press(GdkEventButton* ev)
481 {
482         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS ) {
483                 return true;
484         }
485
486         if (!_session->engine().connected()) {
487                 MessageDialog msg (_("Not connected to JACK - cannot engage record"));
488                 msg.run ();
489                 return true;
490         }
491
492         if (!ignore_toggle && is_track() && rec_enable_button) {
493
494                 if (Keyboard::is_button2_event (ev) && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
495
496                         // do nothing on midi sigc::bind event
497                         return false;
498
499                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
500
501                         _session->set_record_enable (_session->get_routes(), !rec_enable_button->get_active());
502
503                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
504
505                         /* Primary-button1 applies change to the route group (even if it is not active)
506                            NOTE: Primary-button2 is MIDI learn.
507                         */
508                         if (ev->button == 1 && _route->route_group()) {
509                                 _session->set_record_enable (_route->route_group()->route_list(), !rec_enable_button->get_active(), Session::rt_cleanup, true);
510                         }
511
512                 } else if (Keyboard::is_context_menu_event (ev)) {
513
514                         /* do this on release */
515
516                 } else {
517
518                         boost::shared_ptr<RouteList> rl (new RouteList);
519                         rl->push_back (route());
520                         _session->set_record_enable (rl, !rec_enable_button->get_active());
521                 }
522         }
523
524         return true;
525 }
526
527 bool
528 RouteUI::rec_enable_release (GdkEventButton*)
529 {
530         return true;
531 }
532
533 void
534 RouteUI::build_sends_menu ()
535 {
536         using namespace Menu_Helpers;
537
538         sends_menu = new Menu;
539         sends_menu->set_name ("ArdourContextMenu");
540         MenuList& items = sends_menu->items();
541
542         items.push_back (MenuElem(_("Assign all tracks (prefader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_sends), PreFader)));
543         items.push_back (MenuElem(_("Assign all tracks (postfader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_sends), PostFader)));
544         items.push_back (MenuElem(_("Assign selected tracks (prefader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_selected_sends), PreFader)));
545         items.push_back (MenuElem(_("Assign selected tracks (postfader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_selected_sends), PostFader)));
546         items.push_back (MenuElem(_("Copy track gains to sends"), sigc::mem_fun (*this, &RouteUI::set_sends_gain_from_track)));
547         items.push_back (MenuElem(_("Set sends gain to -inf"), sigc::mem_fun (*this, &RouteUI::set_sends_gain_to_zero)));
548         items.push_back (MenuElem(_("Set sends gain to 0dB"), sigc::mem_fun (*this, &RouteUI::set_sends_gain_to_unity)));
549
550 }
551
552 void
553 RouteUI::create_sends (Placement p)
554 {
555         _session->globally_add_internal_sends (_route, p);
556 }
557
558 void
559 RouteUI::create_selected_sends (Placement p)
560 {
561         boost::shared_ptr<RouteList> rlist (new RouteList);
562         TrackSelection& selected_tracks (ARDOUR_UI::instance()->the_editor().get_selection().tracks);
563
564         for (TrackSelection::iterator i = selected_tracks.begin(); i != selected_tracks.end(); ++i) {
565                 RouteTimeAxisView* rtv;
566                 RouteUI* rui;
567                 if ((rtv = dynamic_cast<RouteTimeAxisView*>(*i)) != 0) {
568                         if ((rui = dynamic_cast<RouteUI*>(rtv)) != 0) {
569                                 if (boost::dynamic_pointer_cast<AudioTrack>(rui->route())) {
570                                         rlist->push_back (rui->route());
571                                 }
572                         }
573                 }
574         }
575         
576         _session->add_internal_sends (_route, p, rlist);
577 }
578
579 void
580 RouteUI::set_sends_gain_from_track ()
581 {
582         _session->globally_set_send_gains_from_track (_route);
583 }
584
585 void
586 RouteUI::set_sends_gain_to_zero ()
587 {
588         _session->globally_set_send_gains_to_zero (_route);
589 }
590
591 void
592 RouteUI::set_sends_gain_to_unity ()
593 {
594         _session->globally_set_send_gains_to_unity (_route);
595 }
596
597 bool
598 RouteUI::show_sends_press(GdkEventButton* ev)
599 {
600         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS ) {
601                 return true;
602         }
603
604         if (!ignore_toggle && !is_track() && show_sends_button) {
605
606                 if (Keyboard::is_button2_event (ev) && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
607
608                         // do nothing on midi sigc::bind event
609                         return false;
610
611                 } else if (Keyboard::is_context_menu_event (ev)) {
612
613                         if (sends_menu == 0) {
614                                 build_sends_menu ();
615                         }
616
617                         sends_menu->popup (0, ev->time);
618
619                 } else {
620
621                         /* change button state */
622
623                         show_sends_button->set_active (!show_sends_button->get_active());
624
625                         /* start blinking */
626
627                         if (show_sends_button->get_active()) {
628                                 /* show sends to this bus */
629                                 MixerStrip::SwitchIO (_route);
630                                 send_blink_connection = ARDOUR_UI::instance()->Blink.connect (sigc::mem_fun(*this, &RouteUI::send_blink));
631                         } else {
632                                 /* everybody back to normal */
633                                 send_blink_connection.disconnect ();
634                                 MixerStrip::SwitchIO (boost::shared_ptr<Route>());
635                         }
636
637                 }
638         }
639
640         return true;
641 }
642
643 bool
644 RouteUI::show_sends_release (GdkEventButton*)
645 {
646         return true;
647 }
648
649 void
650 RouteUI::send_blink (bool onoff)
651 {
652         if (!show_sends_button) {
653                 return;
654         }
655
656         if (onoff) {
657                 show_sends_button->set_state (STATE_ACTIVE);
658         } else {
659                 show_sends_button->set_state (STATE_NORMAL);
660         }
661 }
662
663 void
664 RouteUI::solo_changed(void* /*src*/)
665 {
666         Gtkmm2ext::UI::instance()->call_slot (boost::bind (&RouteUI::update_solo_display, this));
667 }
668
669
670 void
671 RouteUI::listen_changed(void* /*src*/)
672 {
673         Gtkmm2ext::UI::instance()->call_slot (boost::bind (&RouteUI::update_solo_display, this));
674 }
675
676 int
677 RouteUI::solo_visual_state (boost::shared_ptr<Route> r)
678 {
679         if (r->is_master() || r->is_control()) {
680                 return 0;
681         }
682         
683         if (Config->get_solo_control_is_listen_control()) {
684
685                 if (r->listening()) {
686                         return 1;
687                 } else {
688                         return 0;
689                 }
690
691         } 
692         
693         if (r->soloed()) {
694                 return 1;
695         } else {
696                 return 0;
697         }
698 }
699
700 int
701 RouteUI::solo_visual_state_with_isolate (boost::shared_ptr<Route> r)
702 {
703         if (r->is_master() || r->is_control()) {
704                 return 0;
705         }
706         
707         if (Config->get_solo_control_is_listen_control()) {
708
709                 if (r->listening()) {
710                         return 1;
711                 } else {
712                         return 0;
713                 }
714
715         } 
716         
717         if (r->solo_isolated()) {
718                 return 2;
719         } else if (r->soloed()) {
720                 return 1;
721         } else {
722                 return 0;
723         }
724 }
725
726 int
727 RouteUI::solo_isolate_visual_state (boost::shared_ptr<Route> r)
728 {
729         if (r->is_master() || r->is_control()) {
730                 return 0;
731         }
732         
733         if (r->solo_isolated()) {
734                         return 1;
735         } else {
736                 return 0;
737         }
738 }
739
740 void
741 RouteUI::update_solo_display ()
742 {
743         bool x;
744
745         if (Config->get_solo_control_is_listen_control()) {
746
747                 if (solo_button->get_active() != (x = _route->listening())) {
748                         ignore_toggle = true;
749                         solo_button->set_active(x);
750                         ignore_toggle = false;
751                 }
752
753         } else {
754
755                 if (solo_button->get_active() != (x = _route->soloed())) {
756                         ignore_toggle = true;
757                         solo_button->set_active (x);
758                         ignore_toggle = false;
759                 }
760
761         }
762
763         solo_button->set_visual_state (solo_visual_state_with_isolate (_route));
764 }
765
766 void
767 RouteUI::solo_changed_so_update_mute ()
768 {
769         update_mute_display ();
770 }
771
772 void
773 RouteUI::mute_changed(void* /*src*/)
774 {
775         update_mute_display ();
776 }
777
778 int
779 RouteUI::mute_visual_state (Session* s, boost::shared_ptr<Route> r)
780 {
781         if (r->is_master() || r->is_control()) {
782                 return 0;
783         }
784         
785         if (Config->get_show_solo_mutes()) {
786                 
787                 if (r->muted ()) {
788                         /* full mute */
789                         return 2;
790                 } else if (s->soloing() && !r->soloed() && !r->solo_isolated()) {
791                         /* mute-because-not-soloed */
792                         return 1;
793                 } else {
794                         /* no mute at all */
795                         return 0;
796                 }
797
798         } else {
799
800                 if (r->muted()) {
801                         /* full mute */
802                         return 2;
803                 } else {
804                         /* no mute at all */
805                         return 0;
806                 }
807         }
808
809         return 0;
810 }
811
812 void
813 RouteUI::update_mute_display ()
814 {
815         if (!_route) {
816                 return;
817         }
818
819         bool model = _route->muted();
820         bool view = mute_button->get_active();
821
822         /* first make sure the button's "depressed" visual
823            is correct.
824         */
825
826         if (model != view) {
827                 ignore_toggle = true;
828                 mute_button->set_active (model);
829                 ignore_toggle = false;
830         }
831
832         mute_button->set_visual_state (mute_visual_state (_session, _route));
833 }
834
835 void
836 RouteUI::route_rec_enable_changed ()
837 {
838         Gtkmm2ext::UI::instance()->call_slot (boost::bind (&RouteUI::update_rec_display, this));
839 }
840
841 void
842 RouteUI::session_rec_enable_changed ()
843 {
844         update_rec_display ();
845 }
846
847 void
848 RouteUI::update_rec_display ()
849 {
850         if (!rec_enable_button || !_route) {
851                 return;
852         }
853                         
854         bool model = _route->record_enabled();
855         bool view = rec_enable_button->get_active();
856
857         /* first make sure the button's "depressed" visual
858            is correct.
859         */
860
861         if (model != view) {
862                 ignore_toggle = true;
863                 rec_enable_button->set_active (model);
864                 ignore_toggle = false;
865         }
866
867         /* now make sure its color state is correct */
868
869         if (model) {
870
871                 switch (_session->record_status ()) {
872                 case Session::Recording:
873                         rec_enable_button->set_visual_state (1);
874                         break;
875
876                 case Session::Disabled:
877                 case Session::Enabled:
878                         rec_enable_button->set_visual_state (2);
879                         break;
880
881                 }
882
883         } else {
884                 rec_enable_button->set_visual_state (0);
885         }
886
887         check_rec_enable_sensitivity ();
888 }
889
890 void
891 RouteUI::build_solo_menu (void)
892 {
893         using namespace Menu_Helpers;
894
895         solo_menu = new Menu;
896         solo_menu->set_name ("ArdourContextMenu");
897         MenuList& items = solo_menu->items();
898         CheckMenuItem* check;
899
900         check = new CheckMenuItem(_("Solo Isolate"));
901         check->set_active (_route->solo_isolated());
902         check->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_solo_isolated), check));
903         _route->solo_isolated_changed.connect (route_connections, ui_bind (&RouteUI::solo_isolated_toggle, this, _1, check), gui_context());
904         items.push_back (CheckMenuElem(*check));
905         check->show_all();
906
907         check = new CheckMenuItem(_("Solo Safe"));
908         check->set_active (_route->solo_safe());
909         check->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_solo_safe), check));
910         _route->solo_safe_changed.connect (route_connections, ui_bind (&RouteUI::solo_safe_toggle, this, _1, check), gui_context());
911         items.push_back (CheckMenuElem(*check));
912         check->show_all();
913
914         //items.push_back (SeparatorElem());
915         // items.push_back (MenuElem (_("MIDI Bind"), sigc::mem_fun (*mute_button, &BindableToggleButton::midi_learn)));
916
917 }
918
919 void
920 RouteUI::build_mute_menu(void)
921 {
922         using namespace Menu_Helpers;
923
924         mute_menu = new Menu;
925         mute_menu->set_name ("ArdourContextMenu");
926
927         MenuList& items = mute_menu->items();
928
929         pre_fader_mute_check = manage (new CheckMenuItem(_("Pre Fader")));
930         init_mute_menu(MuteMaster::PreFader, pre_fader_mute_check);
931         pre_fader_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::PreFader, pre_fader_mute_check));
932         items.push_back (CheckMenuElem(*pre_fader_mute_check));
933         pre_fader_mute_check->show_all();
934
935         post_fader_mute_check = manage (new CheckMenuItem(_("Post Fader")));
936         init_mute_menu(MuteMaster::PostFader, post_fader_mute_check);
937         post_fader_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::PostFader, post_fader_mute_check));
938         items.push_back (CheckMenuElem(*post_fader_mute_check));
939         post_fader_mute_check->show_all();
940
941         listen_mute_check = manage (new CheckMenuItem(_("Control Outs")));
942         init_mute_menu(MuteMaster::Listen, listen_mute_check);
943         listen_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::Listen, listen_mute_check));
944         items.push_back (CheckMenuElem(*listen_mute_check));
945         listen_mute_check->show_all();
946
947         main_mute_check = manage (new CheckMenuItem(_("Main Outs")));
948         init_mute_menu(MuteMaster::Main, main_mute_check);
949         main_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::Main, main_mute_check));
950         items.push_back (CheckMenuElem(*main_mute_check));
951         main_mute_check->show_all();
952
953         //items.push_back (SeparatorElem());
954         // items.push_back (MenuElem (_("MIDI Bind"), sigc::mem_fun (*mute_button, &BindableToggleButton::midi_learn)));
955
956         _route->mute_points_changed.connect (route_connections, boost::bind (&RouteUI::muting_change, this), gui_context());
957 }
958
959 void
960 RouteUI::init_mute_menu(MuteMaster::MutePoint mp, CheckMenuItem* check)
961 {
962         check->set_active (_route->mute_points() & mp);
963 }
964
965 void
966 RouteUI::toggle_mute_menu(MuteMaster::MutePoint mp, Gtk::CheckMenuItem* check)
967 {
968         if (check->get_active()) {
969                 _route->set_mute_points (MuteMaster::MutePoint (_route->mute_points() | mp));
970         } else {
971                 _route->set_mute_points (MuteMaster::MutePoint (_route->mute_points() & ~mp));
972         }
973 }
974
975 void
976 RouteUI::muting_change ()
977 {
978         ENSURE_GUI_THREAD (*this, &RouteUI::muting_change)
979
980         bool yn;
981         MuteMaster::MutePoint current = _route->mute_points ();
982
983         yn = (current & MuteMaster::PreFader);
984
985         if (pre_fader_mute_check->get_active() != yn) {
986                 pre_fader_mute_check->set_active (yn);
987         }
988
989         yn = (current & MuteMaster::PostFader);
990
991         if (post_fader_mute_check->get_active() != yn) {
992                 post_fader_mute_check->set_active (yn);
993         }
994
995         yn = (current & MuteMaster::Listen);
996
997         if (listen_mute_check->get_active() != yn) {
998                 listen_mute_check->set_active (yn);
999         }
1000
1001         yn = (current & MuteMaster::Main);
1002
1003         if (main_mute_check->get_active() != yn) {
1004                 main_mute_check->set_active (yn);
1005         }
1006 }
1007
1008 void
1009 RouteUI::toggle_solo_isolated (Gtk::CheckMenuItem* check)
1010 {
1011         _route->set_solo_isolated (check->get_active(), this);
1012 }
1013
1014 void
1015 RouteUI::toggle_solo_safe (Gtk::CheckMenuItem* check)
1016 {
1017         _route->set_solo_safe (check->get_active(), this);
1018 }
1019
1020 bool
1021 RouteUI::choose_color()
1022 {
1023         bool picked;
1024         Gdk::Color color;
1025
1026         color = Gtkmm2ext::UI::instance()->get_color (_("ardour: color selection"), picked, &_color);
1027
1028         if (picked) {
1029                 set_color (color);
1030         }
1031
1032         return picked;
1033 }
1034
1035 void
1036 RouteUI::set_color (const Gdk::Color & c)
1037 {
1038         char buf[64];
1039
1040         _color = c;
1041
1042         ensure_xml_node ();
1043         snprintf (buf, sizeof (buf), "%d:%d:%d", c.get_red(), c.get_green(), c.get_blue());
1044         xml_node->add_property ("color", buf);
1045
1046         _route->gui_changed ("color", (void *) 0); /* EMIT_SIGNAL */
1047 }
1048
1049
1050 void
1051 RouteUI::ensure_xml_node ()
1052 {
1053         if (xml_node == 0) {
1054                 if ((xml_node = _route->extra_xml ("GUI")) == 0) {
1055                         xml_node = new XMLNode ("GUI");
1056                         _route->add_extra_xml (*xml_node);
1057                 }
1058         }
1059 }
1060
1061 XMLNode*
1062 RouteUI::get_automation_child_xml_node (Evoral::Parameter param)
1063 {
1064         ensure_xml_node ();
1065
1066         XMLNodeList kids = xml_node->children();
1067         XMLNodeConstIterator iter;
1068
1069         const string sym = ARDOUR::EventTypeMap::instance().to_symbol(param);
1070
1071         for (iter = kids.begin(); iter != kids.end(); ++iter) {
1072                 if ((*iter)->name() == AutomationTimeAxisView::state_node_name) {
1073                         XMLProperty* type = (*iter)->property("automation-id");
1074                         if (type && type->value() == sym)
1075                                 return *iter;
1076                 }
1077         }
1078
1079         // Didn't find it, make a new one
1080         XMLNode* child = new XMLNode (AutomationTimeAxisView::state_node_name);
1081         child->add_property("automation-id", sym);
1082         xml_node->add_child_nocopy (*child);
1083
1084         return child;
1085 }
1086
1087 int
1088 RouteUI::set_color_from_route ()
1089 {
1090         XMLProperty *prop;
1091
1092         RouteUI::ensure_xml_node ();
1093
1094         if ((prop = xml_node->property ("color")) != 0) {
1095                 int r, g, b;
1096                 sscanf (prop->value().c_str(), "%d:%d:%d", &r, &g, &b);
1097                 _color.set_red(r);
1098                 _color.set_green(g);
1099                 _color.set_blue(b);
1100                 return 0;
1101         }
1102         return 1;
1103 }
1104
1105 void
1106 RouteUI::remove_this_route ()
1107 {
1108         vector<string> choices;
1109         string prompt;
1110
1111         if (is_track()) {
1112                 prompt  = string_compose (_("Do you really want to remove track \"%1\" ?\n\nYou may also lose the playlist used by this track.\n(this cannot be undone)"), _route->name());
1113         } else {
1114                 prompt  = string_compose (_("Do you really want to remove bus \"%1\" ?\n(this cannot be undone)"), _route->name());
1115         }
1116
1117         choices.push_back (_("No, do nothing."));
1118         choices.push_back (_("Yes, remove it."));
1119
1120         string title;
1121         if (is_track()) {
1122                 title = _("Remove track");
1123         } else {
1124                 title = _("Remove bus");
1125         }
1126
1127         Choice prompter (title, prompt, choices);
1128
1129         if (prompter.run () == 1) {
1130                 Glib::signal_idle().connect (sigc::bind (sigc::ptr_fun (&RouteUI::idle_remove_this_route), this));
1131         }
1132 }
1133
1134 gint
1135 RouteUI::idle_remove_this_route (RouteUI *rui)
1136 {
1137         rui->_session->remove_route (rui->_route);
1138         return false;
1139 }
1140
1141 void
1142 RouteUI::route_rename ()
1143 {
1144         ArdourPrompter name_prompter (true);
1145         string result;
1146         name_prompter.set_prompt (_("New Name: "));
1147         name_prompter.set_initial_text (_route->name());
1148         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
1149         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
1150         name_prompter.show_all ();
1151
1152         switch (name_prompter.run ()) {
1153
1154         case Gtk::RESPONSE_ACCEPT:
1155         name_prompter.get_result (result);
1156         if (result.length()) {
1157                         _route->set_name (result);
1158                 }
1159                 break;
1160         }
1161
1162         return;
1163
1164 }
1165
1166 void
1167 RouteUI::property_changed (const PropertyChange& what_changed)
1168 {
1169         if (what_changed.contains (ARDOUR::Properties::name)) {
1170                 name_label.set_text (_route->name());
1171         }
1172 }
1173
1174 void
1175 RouteUI::toggle_route_active ()
1176 {
1177         bool yn;
1178
1179         if (route_active_menu_item) {
1180                 if (route_active_menu_item->get_active() != (yn = _route->active())) {
1181                         _route->set_active (!yn);
1182                 }
1183         }
1184 }
1185
1186 void
1187 RouteUI::route_active_changed ()
1188 {
1189         if (route_active_menu_item) {
1190                 Gtkmm2ext::UI::instance()->call_slot (boost::bind (&CheckMenuItem::set_active, route_active_menu_item, _route->active()));
1191         }
1192 }
1193
1194 void
1195 RouteUI::toggle_polarity ()
1196 {
1197         if (polarity_menu_item) {
1198
1199                 bool x;
1200
1201                 ENSURE_GUI_THREAD (*this, &RouteUI::toggle_polarity)
1202
1203                 if ((x = polarity_menu_item->get_active()) != _route->phase_invert()) {
1204                         _route->set_phase_invert (x);
1205                         if (x) {
1206                                 name_label.set_text (X_("Ø ") + name_label.get_text());
1207                         } else {
1208                                 name_label.set_text (_route->name());
1209                         }
1210                 }
1211         }
1212 }
1213
1214 void
1215 RouteUI::polarity_changed ()
1216 {
1217         if (_route->phase_invert()) {
1218                 name_label.set_text (X_("Ø ") + name_label.get_text());
1219         } else {
1220                 name_label.set_text (_route->name());
1221         }
1222 }
1223
1224 void
1225 RouteUI::toggle_denormal_protection ()
1226 {
1227         if (denormal_menu_item) {
1228
1229                 bool x;
1230
1231                 ENSURE_GUI_THREAD (*this, &RouteUI::toggle_denormal_protection)
1232
1233                 if ((x = denormal_menu_item->get_active()) != _route->denormal_protection()) {
1234                         _route->set_denormal_protection (x);
1235                 }
1236         }
1237 }
1238
1239 void
1240 RouteUI::denormal_protection_changed ()
1241 {
1242         if (denormal_menu_item) {
1243                 denormal_menu_item->set_active (_route->denormal_protection());
1244         }
1245 }
1246
1247 void
1248 RouteUI::solo_isolated_toggle(void* /*src*/, Gtk::CheckMenuItem* check)
1249 {
1250         bool yn = _route->solo_isolated ();
1251
1252         if (check->get_active() != yn) {
1253                 check->set_active (yn);
1254         }
1255 }
1256
1257
1258 void
1259 RouteUI::solo_safe_toggle(void* /*src*/, Gtk::CheckMenuItem* check)
1260 {
1261         bool yn = _route->solo_safe ();
1262
1263         if (check->get_active() != yn) {
1264                 check->set_active (yn);
1265         }
1266 }
1267
1268 void
1269 RouteUI::disconnect_input ()
1270 {
1271         _route->input()->disconnect (this);
1272 }
1273
1274 void
1275 RouteUI::disconnect_output ()
1276 {
1277         _route->output()->disconnect (this);
1278 }
1279
1280 bool
1281 RouteUI::is_track () const
1282 {
1283         return boost::dynamic_pointer_cast<Track>(_route) != 0;
1284 }
1285
1286 boost::shared_ptr<Track>
1287 RouteUI::track() const
1288 {
1289         return boost::dynamic_pointer_cast<Track>(_route);
1290 }
1291
1292 bool
1293 RouteUI::is_audio_track () const
1294 {
1295         return boost::dynamic_pointer_cast<AudioTrack>(_route) != 0;
1296 }
1297
1298 boost::shared_ptr<AudioTrack>
1299 RouteUI::audio_track() const
1300 {
1301         return boost::dynamic_pointer_cast<AudioTrack>(_route);
1302 }
1303
1304 bool
1305 RouteUI::is_midi_track () const
1306 {
1307         return boost::dynamic_pointer_cast<MidiTrack>(_route) != 0;
1308 }
1309
1310 boost::shared_ptr<MidiTrack>
1311 RouteUI::midi_track() const
1312 {
1313         return boost::dynamic_pointer_cast<MidiTrack>(_route);
1314 }
1315
1316 boost::shared_ptr<Diskstream>
1317 RouteUI::get_diskstream () const
1318 {
1319         boost::shared_ptr<Track> t;
1320
1321         if ((t = boost::dynamic_pointer_cast<Track>(_route)) != 0) {
1322                 return t->diskstream();
1323         } else {
1324                 return boost::shared_ptr<Diskstream> ((Diskstream*) 0);
1325         }
1326 }
1327
1328 string
1329 RouteUI::name() const
1330 {
1331         return _route->name();
1332 }
1333
1334 void
1335 RouteUI::map_frozen ()
1336 {
1337         ENSURE_GUI_THREAD (*this, &RouteUI::map_frozen)
1338
1339         AudioTrack* at = dynamic_cast<AudioTrack*>(_route.get());
1340
1341         if (at) {
1342                 switch (at->freeze_state()) {
1343                 case AudioTrack::Frozen:
1344                         rec_enable_button->set_sensitive (false);
1345                         break;
1346                 default:
1347                         rec_enable_button->set_sensitive (true);
1348                         break;
1349                 }
1350         }
1351 }
1352
1353 void
1354 RouteUI::adjust_latency ()
1355 {
1356         LatencyDialog dialog (_route->name() + _(" latency"), *(_route->output()), _session->frame_rate(), _session->engine().frames_per_cycle());
1357 }
1358
1359 void
1360 RouteUI::save_as_template ()
1361 {
1362         sys::path path;
1363         Glib::ustring safe_name;
1364         string name;
1365
1366         path = ARDOUR::user_route_template_directory ();
1367
1368         if (g_mkdir_with_parents (path.to_string().c_str(), 0755)) {
1369                 error << string_compose (_("Cannot create route template directory %1"), path.to_string()) << endmsg;
1370                 return;
1371         }
1372
1373         Prompter p (true); // modal
1374
1375         p.set_prompt (_("Template name:"));
1376         switch (p.run()) {
1377         case RESPONSE_ACCEPT:
1378                 break;
1379         default:
1380                 return;
1381         }
1382
1383         p.hide ();
1384         p.get_result (name, true);
1385
1386         safe_name = legalize_for_path (name);
1387         safe_name += template_suffix;
1388
1389         path /= safe_name;
1390
1391         _route->save_as_template (path.to_string(), name);
1392 }
1393
1394 void
1395 RouteUI::check_rec_enable_sensitivity ()
1396 {
1397         if (_session->transport_rolling() && rec_enable_button->get_active() && Config->get_disable_disarm_during_roll()) {
1398                 rec_enable_button->set_sensitive (false);
1399         } else {
1400                 rec_enable_button->set_sensitive (true);
1401         }
1402 }
1403
1404 void
1405 RouteUI::parameter_changed (string const & p)
1406 {
1407         if (p == "disable-disarm-during-roll") {
1408                 check_rec_enable_sensitivity ();
1409         } else if (p == "solo-control-is-listen-control") {
1410                 set_button_names ();
1411         } else if (p == "listen-position") {
1412                 set_button_names ();
1413         }
1414 }
1415
1416 void
1417 RouteUI::step_gain_up ()
1418 {
1419         _route->set_gain (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) + 0.1), this);
1420 }
1421
1422 void
1423 RouteUI::page_gain_up ()
1424 {
1425         _route->set_gain (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) + 0.5), this);
1426 }
1427
1428 void
1429 RouteUI::step_gain_down ()
1430 {
1431         _route->set_gain (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) - 0.1), this);
1432 }
1433
1434 void
1435 RouteUI::page_gain_down ()
1436 {
1437         _route->set_gain (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) - 0.5), this);
1438 }
1439
1440 void
1441 RouteUI::open_remote_control_id_dialog ()
1442 {
1443         ArdourDialog dialog (_("Remote Control ID"));
1444
1445         uint32_t const limit = _session->ntracks() + _session->nbusses () + 4;
1446
1447         HBox* hbox = manage (new HBox);
1448         hbox->set_spacing (6);
1449         hbox->pack_start (*manage (new Label (_("Remote control ID:"))));
1450         SpinButton* spin = manage (new SpinButton);
1451         spin->set_digits (0);
1452         spin->set_increments (1, 10);
1453         spin->set_range (0, limit);
1454         spin->set_value (_route->remote_control_id());
1455         hbox->pack_start (*spin);
1456         dialog.get_vbox()->pack_start (*hbox);
1457
1458         dialog.add_button (Stock::CANCEL, RESPONSE_CANCEL);
1459         dialog.add_button (Stock::APPLY, RESPONSE_ACCEPT);
1460
1461         dialog.show_all ();
1462         int const r = dialog.run ();
1463
1464         if (r == RESPONSE_ACCEPT) {
1465                 _route->set_remote_control_id (spin->get_value_as_int ());
1466         }
1467 }