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