led\'s for solo-safe and solo-isolate, rather than a context menu (mixer strip only...
[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 "led.h"
39 #include "keyboard.h"
40 #include "utils.h"
41 #include "prompter.h"
42 #include "gui_thread.h"
43 #include "ardour_dialog.h"
44 #include "latency_gui.h"
45 #include "mixer_strip.h"
46 #include "automation_time_axis.h"
47 #include "route_time_axis.h"
48
49 #include "ardour/route.h"
50 #include "ardour/event_type_map.h"
51 #include "ardour/session.h"
52 #include "ardour/audioengine.h"
53 #include "ardour/audio_track.h"
54 #include "ardour/midi_track.h"
55 #include "ardour/template_utils.h"
56 #include "ardour/filename_extensions.h"
57 #include "ardour/directory_names.h"
58 #include "ardour/profile.h"
59
60 #include "i18n.h"
61 using namespace Gtk;
62 using namespace Gtkmm2ext;
63 using namespace ARDOUR;
64 using namespace PBD;
65
66 RouteUI::RouteUI (ARDOUR::Session* sess)
67         : AxisView(sess)
68 {
69         init ();
70 }
71
72 RouteUI::RouteUI (boost::shared_ptr<ARDOUR::Route> rt, ARDOUR::Session* sess)
73         : AxisView(sess)
74 {
75         init ();
76         set_route (rt);
77 }
78
79 RouteUI::~RouteUI()
80 {
81         _route.reset (); /* drop reference to route, so that it can be cleaned up */
82         route_connections.drop_connections ();
83
84         delete solo_menu;
85         delete mute_menu;
86         delete sends_menu;
87 }
88
89 void
90 RouteUI::init ()
91 {
92         self_destruct = true;
93         xml_node = 0;
94         mute_menu = 0;
95         solo_menu = 0;
96         sends_menu = 0;
97         pre_fader_mute_check = 0;
98         post_fader_mute_check = 0;
99         listen_mute_check = 0;
100         main_mute_check = 0;
101         solo_safe_check = 0;
102         solo_isolated_check = 0;
103         solo_isolated_led = 0;
104         solo_safe_led = 0;
105         ignore_toggle = false;
106         _solo_release = 0;
107         _mute_release = 0;
108         route_active_menu_item = 0;
109         denormal_menu_item = 0;
110         multiple_mute_change = false;
111         multiple_solo_change = false;
112
113         invert_button = manage (new BindableToggleButton ());
114         // mute_button->set_self_managed (true);
115         invert_button->set_name ("InvertButton");
116         invert_button->add (invert_button_label);
117         invert_button_label.show ();
118         UI::instance()->set_tip (invert_button, _("Invert (Phase reverse) this track"), "");
119
120         mute_button = manage (new BindableToggleButton ());
121         // mute_button->set_self_managed (true);
122         mute_button->set_name ("MuteButton");
123         mute_button->add (mute_button_label);
124         mute_button_label.show ();
125         UI::instance()->set_tip (mute_button, _("Mute this track"), "");
126
127         solo_button = manage (new BindableToggleButton ());
128         // solo_button->set_self_managed (true);
129         solo_button->set_name ("SoloButton");
130         solo_button->add (solo_button_label);
131         solo_button_label.show ();
132         UI::instance()->set_tip (solo_button, _("Mute other (non-soloed) tracks"), "");
133         solo_button->set_no_show_all (true);
134
135         rec_enable_button = manage (new BindableToggleButton ());
136         rec_enable_button->set_name ("RecordEnableButton");
137         // rec_enable_button->set_self_managed (true);
138         rec_enable_button->add (rec_enable_button_label);
139         rec_enable_button_label.show ();
140         UI::instance()->set_tip (rec_enable_button, _("Enable recording on this track"), "");
141
142         show_sends_button = manage (new BindableToggleButton (""));
143         show_sends_button->set_name ("SendAlert");
144         // show_sends_button->set_self_managed (true);
145         UI::instance()->set_tip (show_sends_button, _("make mixer strips show sends to this bus"), "");
146
147         _session->SoloChanged.connect (_session_connections, invalidator (*this), boost::bind (&RouteUI::solo_changed_so_update_mute, this), gui_context());
148         _session->TransportStateChange.connect (_session_connections, invalidator (*this), boost::bind (&RouteUI::check_rec_enable_sensitivity, this), gui_context());
149         _session->RecordStateChanged.connect (_session_connections, invalidator (*this), boost::bind (&RouteUI::session_rec_enable_changed, this), gui_context());
150
151         Config->ParameterChanged.connect (*this, invalidator (*this), ui_bind (&RouteUI::parameter_changed, this, _1), gui_context());
152
153         rec_enable_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::rec_enable_press), false);
154         rec_enable_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::rec_enable_release), false);
155
156         show_sends_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::show_sends_press), false);
157         show_sends_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::show_sends_release));
158
159         solo_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::solo_press), false);
160         solo_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::solo_release), false);
161         mute_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::mute_press), false);
162         mute_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::mute_release), false);
163         invert_button->signal_toggled().connect (sigc::mem_fun(*this, &RouteUI::invert_toggled), false);
164
165 }
166
167 void
168 RouteUI::reset ()
169 {
170         route_connections.drop_connections ();
171
172         delete solo_menu;
173         solo_menu = 0;
174
175         delete mute_menu;
176         mute_menu = 0;
177
178         if (xml_node) {
179                 /* do not delete the node - its owned by the route */
180                 xml_node = 0;
181         }
182
183         route_active_menu_item = 0;
184         denormal_menu_item = 0;
185 }
186
187 void
188 RouteUI::self_delete ()
189 {
190         /* This may be called from a non-GUI thread. Keep it safe */
191
192         delete_when_idle (this);
193 }
194
195 void
196 RouteUI::set_route (boost::shared_ptr<Route> rp)
197 {
198         reset ();
199
200         _route = rp;
201
202         if (set_color_from_route()) {
203                 set_color (unique_random_color());
204         }
205
206         if (self_destruct) {
207                 rp->DropReferences.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::self_delete, this), gui_context());
208         }
209         
210         mute_button->set_controllable (_route->mute_control());
211         solo_button->set_controllable (_route->solo_control());
212
213         _route->active_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::route_active_changed, this), gui_context());
214         _route->mute_changed.connect (route_connections, invalidator (*this), ui_bind (&RouteUI::mute_changed, this, _1), gui_context());
215
216         _route->solo_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::update_solo_display, this), gui_context());
217         _route->solo_safe_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::update_solo_display, this), gui_context());
218         _route->listen_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::update_solo_display, this), gui_context());
219         _route->solo_isolated_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::update_solo_display, this), gui_context());
220
221         _route->phase_invert_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::polarity_changed, this), gui_context());
222         _route->PropertyChanged.connect (route_connections, invalidator (*this), ui_bind (&RouteUI::property_changed, this, _1), gui_context());
223
224         if (_session->writable() && is_track()) {
225                 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track>(_route);
226
227                 t->RecordEnableChanged.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::route_rec_enable_changed, this), gui_context());
228
229                 rec_enable_button->show();
230                 rec_enable_button->set_controllable (t->rec_enable_control());
231
232                 update_rec_display ();
233         }
234
235         mute_button->unset_flags (Gtk::CAN_FOCUS);
236         solo_button->unset_flags (Gtk::CAN_FOCUS);
237
238         mute_button->show();
239         invert_button->show ();
240
241         if (_route->is_monitor()) {
242                 solo_button->hide ();
243         } else {
244                 solo_button->show();
245         }
246
247         map_frozen ();
248 }
249
250 void
251 RouteUI::invert_toggled ()
252 {
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_isolated_led) {
379
380                                 if (solo_menu == 0) {
381                                         build_solo_menu ();
382                                 }
383                                 
384                                 solo_menu->popup (1, ev->time);
385                         }
386                         
387                 } else {
388                         
389                         if (Keyboard::is_button2_event (ev)) {
390                                 
391                                 // Primary-button2 click is the midi binding click
392                                 // button2-click is "momentary"
393                                 
394                                 if (solo_button->on_button_press_event (ev)) {
395                                         return true;
396                                 }
397
398                                 _solo_release = new SoloMuteRelease (_route->self_soloed());
399                         }
400                         
401                         if (ev->button == 1 || Keyboard::is_button2_event (ev)) {
402                                 
403                                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
404                                         
405                                         /* Primary-Tertiary-click applies change to all routes */
406
407                                         if (_solo_release) {
408                                                 _solo_release->routes = _session->get_routes ();
409                                         }
410                                         
411                                         if (Config->get_solo_control_is_listen_control()) {
412                                                 _session->set_listen (_session->get_routes(), !_route->listening(),  Session::rt_cleanup, true);
413                                         } else {
414                                                 _session->set_solo (_session->get_routes(), !_route->self_soloed(),  Session::rt_cleanup, true);
415                                         }
416                                         
417                                 } else if (Keyboard::modifier_state_contains (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::SecondaryModifier))) {
418                                         
419                                         // Primary-Secondary-click: exclusively solo this track
420
421                                         if (_solo_release) {
422                                                 _solo_release->exclusive = true;
423
424                                                 boost::shared_ptr<RouteList> routes = _session->get_routes();
425
426                                                 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
427                                                         if ((*i)->soloed ()) {
428                                                                 _solo_release->routes_on->push_back (*i);
429                                                         } else {
430                                                                 _solo_release->routes_off->push_back (*i);
431                                                         }
432                                                 }
433                                         }
434                                         
435                                         if (Config->get_solo_control_is_listen_control()) {
436                                                 /* ??? we need a just_one_listen() method */
437                                         } else {
438                                                 _session->set_just_one_solo (_route, true);
439                                         }
440
441                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
442                                         
443                                         // shift-click: toggle solo isolated status
444                                         
445                                         _route->set_solo_isolated (!_route->solo_isolated(), this);
446                                         delete _solo_release;
447                                         _solo_release = 0;
448                                         
449                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
450                                         
451                                         /* Primary-button1: solo mix group.
452                                            NOTE: Primary-button2 is MIDI learn.
453                                         */
454                                         
455                                         if (ev->button == 1 && _route->route_group()) {
456
457                                                 if (_solo_release) {
458                                                         _solo_release->routes = _route->route_group()->route_list();
459                                                 }
460                                         
461                                                 if (Config->get_solo_control_is_listen_control()) {
462                                                         _session->set_listen (_route->route_group()->route_list(), !_route->listening(),  Session::rt_cleanup, true);
463                                                 } else {
464                                                         _session->set_solo (_route->route_group()->route_list(), !_route->self_soloed(),  Session::rt_cleanup, true);
465                                                 }
466                                         }
467                                         
468                                 } else {
469                                         
470                                         /* click: solo this route */
471                                         
472                                         boost::shared_ptr<RouteList> rl (new RouteList);
473                                         rl->push_back (route());
474
475                                         if (_solo_release) {
476                                                 _solo_release->routes = rl;
477                                         }
478
479                                         if (Config->get_solo_control_is_listen_control()) {
480                                                 _session->set_listen (rl, !_route->listening());
481                                         } else {
482                                                 _session->set_solo (rl, !_route->self_soloed());
483                                         }
484                                 }
485                         }
486                 }
487         }
488
489         return true;
490 }
491
492 bool
493 RouteUI::solo_release (GdkEventButton*)
494 {
495         if (!ignore_toggle) {
496                 
497                 if (_solo_release) {
498
499                         if (_solo_release->exclusive) {
500
501                         } else {
502                                 if (Config->get_solo_control_is_listen_control()) {
503                                         _session->set_listen (_solo_release->routes, _solo_release->active, Session::rt_cleanup, true);
504                                 } else {
505                                         _session->set_solo (_solo_release->routes, _solo_release->active, Session::rt_cleanup, true);
506                                 }
507                         }
508
509                         delete _solo_release;
510                         _solo_release = 0;
511                 }
512         }
513
514         return true;
515 }
516
517 bool
518 RouteUI::rec_enable_press(GdkEventButton* ev)
519 {
520         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS ) {
521                 return true;
522         }
523
524         if (!_session->engine().connected()) {
525                 MessageDialog msg (_("Not connected to JACK - cannot engage record"));
526                 msg.run ();
527                 return true;
528         }
529
530         if (!ignore_toggle && is_track() && rec_enable_button) {
531
532                 if (Keyboard::is_button2_event (ev)) {
533
534                         // do nothing on midi sigc::bind event
535                         return rec_enable_button->on_button_press_event (ev);
536
537                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
538
539                         _session->set_record_enable (_session->get_routes(), !rec_enable_button->get_active());
540
541                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
542
543                         /* Primary-button1 applies change to the route group (even if it is not active)
544                            NOTE: Primary-button2 is MIDI learn.
545                         */
546                         if (ev->button == 1 && _route->route_group()) {
547                                 _session->set_record_enable (_route->route_group()->route_list(), !rec_enable_button->get_active(), Session::rt_cleanup, true);
548                         }
549
550                 } else if (Keyboard::is_context_menu_event (ev)) {
551
552                         /* do this on release */
553
554                 } else {
555
556                         boost::shared_ptr<RouteList> rl (new RouteList);
557                         rl->push_back (route());
558                         _session->set_record_enable (rl, !rec_enable_button->get_active());
559                 }
560         }
561
562         return true;
563 }
564
565 bool
566 RouteUI::rec_enable_release (GdkEventButton*)
567 {
568         return true;
569 }
570
571 void
572 RouteUI::build_sends_menu ()
573 {
574         using namespace Menu_Helpers;
575
576         sends_menu = new Menu;
577         sends_menu->set_name ("ArdourContextMenu");
578         MenuList& items = sends_menu->items();
579
580         items.push_back (MenuElem(_("Assign all tracks (prefader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_sends), PreFader)));
581         items.push_back (MenuElem(_("Assign all tracks (postfader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_sends), PostFader)));
582         items.push_back (MenuElem(_("Assign selected tracks (prefader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_selected_sends), PreFader)));
583         items.push_back (MenuElem(_("Assign selected tracks (postfader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_selected_sends), PostFader)));
584         items.push_back (MenuElem(_("Copy track gains to sends"), sigc::mem_fun (*this, &RouteUI::set_sends_gain_from_track)));
585         items.push_back (MenuElem(_("Set sends gain to -inf"), sigc::mem_fun (*this, &RouteUI::set_sends_gain_to_zero)));
586         items.push_back (MenuElem(_("Set sends gain to 0dB"), sigc::mem_fun (*this, &RouteUI::set_sends_gain_to_unity)));
587
588 }
589
590 void
591 RouteUI::create_sends (Placement p)
592 {
593         _session->globally_add_internal_sends (_route, p);
594 }
595
596 void
597 RouteUI::create_selected_sends (Placement p)
598 {
599         boost::shared_ptr<RouteList> rlist (new RouteList);
600         TrackSelection& selected_tracks (ARDOUR_UI::instance()->the_editor().get_selection().tracks);
601
602         for (TrackSelection::iterator i = selected_tracks.begin(); i != selected_tracks.end(); ++i) {
603                 RouteTimeAxisView* rtv;
604                 RouteUI* rui;
605                 if ((rtv = dynamic_cast<RouteTimeAxisView*>(*i)) != 0) {
606                         if ((rui = dynamic_cast<RouteUI*>(rtv)) != 0) {
607                                 if (boost::dynamic_pointer_cast<AudioTrack>(rui->route())) {
608                                         rlist->push_back (rui->route());
609                                 }
610                         }
611                 }
612         }
613         
614         _session->add_internal_sends (_route, p, rlist);
615 }
616
617 void
618 RouteUI::set_sends_gain_from_track ()
619 {
620         _session->globally_set_send_gains_from_track (_route);
621 }
622
623 void
624 RouteUI::set_sends_gain_to_zero ()
625 {
626         _session->globally_set_send_gains_to_zero (_route);
627 }
628
629 void
630 RouteUI::set_sends_gain_to_unity ()
631 {
632         _session->globally_set_send_gains_to_unity (_route);
633 }
634
635 bool
636 RouteUI::show_sends_press(GdkEventButton* ev)
637 {
638         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS ) {
639                 return true;
640         }
641
642         if (!ignore_toggle && !is_track() && show_sends_button) {
643
644                 if (Keyboard::is_button2_event (ev) && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
645
646                         // do nothing on midi sigc::bind event
647                         return false;
648
649                 } else if (Keyboard::is_context_menu_event (ev)) {
650
651                         if (sends_menu == 0) {
652                                 build_sends_menu ();
653                         }
654
655                         sends_menu->popup (0, ev->time);
656
657                 } else {
658
659                         /* change button state */
660
661                         show_sends_button->set_active (!show_sends_button->get_active());
662
663                         /* start blinking */
664
665                         if (show_sends_button->get_active()) {
666                                 /* show sends to this bus */
667                                 MixerStrip::SwitchIO (_route);
668                                 send_blink_connection = ARDOUR_UI::instance()->Blink.connect (sigc::mem_fun(*this, &RouteUI::send_blink));
669                         } else {
670                                 /* everybody back to normal */
671                                 send_blink_connection.disconnect ();
672                                 MixerStrip::SwitchIO (boost::shared_ptr<Route>());
673                         }
674
675                 }
676         }
677
678         return true;
679 }
680
681 bool
682 RouteUI::show_sends_release (GdkEventButton*)
683 {
684         return true;
685 }
686
687 void
688 RouteUI::send_blink (bool onoff)
689 {
690         if (!show_sends_button) {
691                 return;
692         }
693
694         if (onoff) {
695                 show_sends_button->set_state (STATE_ACTIVE);
696         } else {
697                 show_sends_button->set_state (STATE_NORMAL);
698         }
699 }
700
701 int
702 RouteUI::solo_visual_state (boost::shared_ptr<Route> r)
703 {
704         if (r->is_master() || r->is_monitor()) {
705                 return 0;
706         }
707         
708         if (Config->get_solo_control_is_listen_control()) {
709
710                 if (r->listening()) {
711                         return 1;
712                 } else {
713                         return 0;
714                 }
715
716         } 
717         
718         if (r->soloed()) {
719                 return 1;
720         } else {
721                 return 0;
722         }
723 }
724
725 int
726 RouteUI::solo_visual_state_with_isolate (boost::shared_ptr<Route> r)
727 {
728         if (r->is_master() || r->is_monitor()) {
729                 return 0;
730         }
731         
732         if (Config->get_solo_control_is_listen_control()) {
733
734                 if (r->listening()) {
735                         return 1;
736                 } else {
737                         return 0;
738                 }
739
740         } 
741         
742         if (r->solo_isolated()) {
743                 return 2;
744         } else if (r->soloed()) {
745                 if (!r->self_soloed()) {
746                         return 3;
747                 } else {
748                         return 1;
749                 }
750         } else {
751                 return 0;
752         }
753 }
754
755 int
756 RouteUI::solo_isolate_visual_state (boost::shared_ptr<Route> r)
757 {
758         if (r->is_master() || r->is_monitor()) {
759                 return 0;
760         }
761         
762         if (r->solo_isolated()) {
763                 return 1;
764         } else {
765                 return 0;
766         }
767 }
768
769 int
770 RouteUI::solo_safe_visual_state (boost::shared_ptr<Route> r)
771 {
772         if (r->is_master() || r->is_monitor()) {
773                 return 0;
774         }
775         
776         if (r->solo_safe()) {
777                 return 1;
778         } else {
779                 return 0;
780         }
781 }
782
783 void
784 RouteUI::update_solo_display ()
785 {
786         bool x;
787
788         if (Config->get_solo_control_is_listen_control()) {
789
790                 if (solo_button->get_active() != (x = _route->listening())) {
791                         ignore_toggle = true;
792                         solo_button->set_active(x);
793                         ignore_toggle = false;
794                 }
795
796         } else {
797
798                 if (solo_button->get_active() != (x = _route->soloed())) {
799                         ignore_toggle = true;
800                         solo_button->set_active (x);
801                         ignore_toggle = false;
802                 }
803
804         }
805
806         bool yn = _route->solo_safe ();
807
808         if (solo_safe_check && solo_safe_check->get_active() != yn) {
809                 solo_safe_check->set_active (yn);
810         }
811
812         yn = _route->solo_isolated ();
813
814         if (solo_isolated_check && solo_isolated_check->get_active() != yn) {
815                 solo_isolated_check->set_active (yn);
816         }
817
818         set_button_names ();
819
820         if (solo_isolated_led) {
821                 cerr << _route->name() << " reset iso vis = " << (_route->solo_isolated() ? 1 : 0) << endl;
822                 solo_isolated_led->set_visual_state (_route->solo_isolated() ? 1 : 0);
823         }
824
825         if (solo_safe_led) {
826                 solo_safe_led->set_visual_state (_route->solo_safe() ? 1 : 0);
827         }
828
829         solo_button->set_visual_state (solo_visual_state (_route));
830 }
831
832 void
833 RouteUI::solo_changed_so_update_mute ()
834 {
835         update_mute_display ();
836 }
837
838 void
839 RouteUI::mute_changed(void* /*src*/)
840 {
841         update_mute_display ();
842 }
843
844 int
845 RouteUI::mute_visual_state (Session* s, boost::shared_ptr<Route> r)
846 {
847         if (r->is_master() || r->is_monitor()) {
848                 return 0;
849         }
850
851         if (Config->get_show_solo_mutes()) {
852
853                 if (r->muted ()) {
854                         /* full mute */
855                         return 2;
856                 } else if (s->soloing() && !r->soloed() && !r->solo_isolated()) {
857                         return 1;
858                 } else {
859                         /* no mute at all */
860                         return 0;
861                 }
862
863         } else {
864
865                 if (r->muted()) {
866                         /* full mute */
867                         return 2;
868                 } else {
869                         /* no mute at all */
870                         return 0;
871                 }
872         }
873
874         return 0;
875 }
876
877 void
878 RouteUI::update_mute_display ()
879 {
880         if (!_route) {
881                 return;
882         }
883
884         bool model = _route->muted();
885         bool view = mute_button->get_active();
886
887         /* first make sure the button's "depressed" visual
888            is correct.
889         */
890
891         if (model != view) {
892                 ignore_toggle = true;
893                 mute_button->set_active (model);
894                 ignore_toggle = false;
895         }
896
897         mute_button->set_visual_state (mute_visual_state (_session, _route));
898 }
899
900 void
901 RouteUI::route_rec_enable_changed ()
902 {
903         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&RouteUI::update_rec_display, this));
904 }
905
906 void
907 RouteUI::session_rec_enable_changed ()
908 {
909         update_rec_display ();
910 }
911
912 void
913 RouteUI::update_rec_display ()
914 {
915         if (!rec_enable_button || !_route) {
916                 return;
917         }
918                         
919         bool model = _route->record_enabled();
920         bool view = rec_enable_button->get_active();
921
922         /* first make sure the button's "depressed" visual
923            is correct.
924         */
925
926         if (model != view) {
927                 ignore_toggle = true;
928                 rec_enable_button->set_active (model);
929                 ignore_toggle = false;
930         }
931
932         /* now make sure its color state is correct */
933
934         if (model) {
935
936                 switch (_session->record_status ()) {
937                 case Session::Recording:
938                         rec_enable_button->set_visual_state (1);
939                         break;
940
941                 case Session::Disabled:
942                 case Session::Enabled:
943                         rec_enable_button->set_visual_state (2);
944                         break;
945
946                 }
947
948         } else {
949                 rec_enable_button->set_visual_state (0);
950         }
951
952         check_rec_enable_sensitivity ();
953 }
954
955 void
956 RouteUI::build_solo_menu (void)
957 {
958         using namespace Menu_Helpers;
959
960         solo_menu = new Menu;
961         solo_menu->set_name ("ArdourContextMenu");
962         MenuList& items = solo_menu->items();
963         CheckMenuItem* check;
964
965         check = new CheckMenuItem(_("Solo Isolate"));
966         check->set_active (_route->solo_isolated());
967         check->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_solo_isolated), check));
968         items.push_back (CheckMenuElem(*check));
969         solo_isolated_check = dynamic_cast<CheckMenuItem*>(&items.back());
970         check->show_all();
971
972         check = new CheckMenuItem(_("Solo Safe"));
973         check->set_active (_route->solo_safe());
974         check->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_solo_safe), check));
975         items.push_back (CheckMenuElem(*check));
976         solo_safe_check = dynamic_cast<CheckMenuItem*>(&items.back());
977         check->show_all();
978
979         //items.push_back (SeparatorElem());
980         // items.push_back (MenuElem (_("MIDI Bind"), sigc::mem_fun (*mute_button, &BindableToggleButton::midi_learn)));
981
982 }
983
984 void
985 RouteUI::build_mute_menu(void)
986 {
987         using namespace Menu_Helpers;
988
989         mute_menu = new Menu;
990         mute_menu->set_name ("ArdourContextMenu");
991
992         MenuList& items = mute_menu->items();
993
994         pre_fader_mute_check = manage (new CheckMenuItem(_("Pre Fader")));
995         init_mute_menu(MuteMaster::PreFader, pre_fader_mute_check);
996         pre_fader_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::PreFader, pre_fader_mute_check));
997         items.push_back (CheckMenuElem(*pre_fader_mute_check));
998         pre_fader_mute_check->show_all();
999
1000         post_fader_mute_check = manage (new CheckMenuItem(_("Post Fader")));
1001         init_mute_menu(MuteMaster::PostFader, post_fader_mute_check);
1002         post_fader_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::PostFader, post_fader_mute_check));
1003         items.push_back (CheckMenuElem(*post_fader_mute_check));
1004         post_fader_mute_check->show_all();
1005
1006         listen_mute_check = manage (new CheckMenuItem(_("Control Outs")));
1007         init_mute_menu(MuteMaster::Listen, listen_mute_check);
1008         listen_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::Listen, listen_mute_check));
1009         items.push_back (CheckMenuElem(*listen_mute_check));
1010         listen_mute_check->show_all();
1011
1012         main_mute_check = manage (new CheckMenuItem(_("Main Outs")));
1013         init_mute_menu(MuteMaster::Main, main_mute_check);
1014         main_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::Main, main_mute_check));
1015         items.push_back (CheckMenuElem(*main_mute_check));
1016         main_mute_check->show_all();
1017
1018         //items.push_back (SeparatorElem());
1019         // items.push_back (MenuElem (_("MIDI Bind"), sigc::mem_fun (*mute_button, &BindableToggleButton::midi_learn)));
1020
1021         _route->mute_points_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::muting_change, this), gui_context());
1022 }
1023
1024 void
1025 RouteUI::init_mute_menu(MuteMaster::MutePoint mp, CheckMenuItem* check)
1026 {
1027         check->set_active (_route->mute_points() & mp);
1028 }
1029
1030 void
1031 RouteUI::toggle_mute_menu(MuteMaster::MutePoint mp, Gtk::CheckMenuItem* check)
1032 {
1033         if (check->get_active()) {
1034                 _route->set_mute_points (MuteMaster::MutePoint (_route->mute_points() | mp));
1035         } else {
1036                 _route->set_mute_points (MuteMaster::MutePoint (_route->mute_points() & ~mp));
1037         }
1038 }
1039
1040 void
1041 RouteUI::muting_change ()
1042 {
1043         ENSURE_GUI_THREAD (*this, &RouteUI::muting_change)
1044
1045         bool yn;
1046         MuteMaster::MutePoint current = _route->mute_points ();
1047
1048         yn = (current & MuteMaster::PreFader);
1049
1050         if (pre_fader_mute_check->get_active() != yn) {
1051                 pre_fader_mute_check->set_active (yn);
1052         }
1053
1054         yn = (current & MuteMaster::PostFader);
1055
1056         if (post_fader_mute_check->get_active() != yn) {
1057                 post_fader_mute_check->set_active (yn);
1058         }
1059
1060         yn = (current & MuteMaster::Listen);
1061
1062         if (listen_mute_check->get_active() != yn) {
1063                 listen_mute_check->set_active (yn);
1064         }
1065
1066         yn = (current & MuteMaster::Main);
1067
1068         if (main_mute_check->get_active() != yn) {
1069                 main_mute_check->set_active (yn);
1070         }
1071 }
1072
1073 bool
1074 RouteUI::solo_isolate_button_release (GdkEventButton* ev)
1075 {
1076         bool view = (solo_isolated_led->visual_state() != 0);
1077         cerr << _route->name() << "button release,  view is " << view << " set to " << !view << endl;
1078         _route->set_solo_isolated (!view, this);
1079         cerr << "DONE with SSI\n";
1080         return true;
1081 }
1082
1083 bool
1084 RouteUI::solo_safe_button_release (GdkEventButton* ev)
1085 {
1086         _route->set_solo_safe (!(solo_safe_led->visual_state() > 0), this);
1087         return true;
1088 }
1089
1090 void
1091 RouteUI::toggle_solo_isolated (Gtk::CheckMenuItem* check)
1092 {
1093         _route->set_solo_isolated (check->get_active(), this);
1094 }
1095
1096 void
1097 RouteUI::toggle_solo_safe (Gtk::CheckMenuItem* check)
1098 {
1099         _route->set_solo_safe (check->get_active(), this);
1100 }
1101
1102 bool
1103 RouteUI::choose_color()
1104 {
1105         bool picked;
1106         Gdk::Color color;
1107
1108         color = Gtkmm2ext::UI::instance()->get_color (_("Color Selection"), picked, &_color);
1109
1110         if (picked) {
1111                 set_color (color);
1112         }
1113
1114         return picked;
1115 }
1116
1117 void
1118 RouteUI::set_color (const Gdk::Color & c)
1119 {
1120         char buf[64];
1121
1122         _color = c;
1123
1124         ensure_xml_node ();
1125         snprintf (buf, sizeof (buf), "%d:%d:%d", c.get_red(), c.get_green(), c.get_blue());
1126         xml_node->add_property ("color", buf);
1127
1128         _route->gui_changed ("color", (void *) 0); /* EMIT_SIGNAL */
1129 }
1130
1131
1132 void
1133 RouteUI::ensure_xml_node ()
1134 {
1135         if (xml_node == 0) {
1136                 if ((xml_node = _route->extra_xml ("GUI")) == 0) {
1137                         xml_node = new XMLNode ("GUI");
1138                         _route->add_extra_xml (*xml_node);
1139                 }
1140         }
1141 }
1142
1143 XMLNode*
1144 RouteUI::get_automation_child_xml_node (Evoral::Parameter param)
1145 {
1146         ensure_xml_node ();
1147
1148         XMLNodeList kids = xml_node->children();
1149         XMLNodeConstIterator iter;
1150
1151         const string sym = ARDOUR::EventTypeMap::instance().to_symbol(param);
1152
1153         for (iter = kids.begin(); iter != kids.end(); ++iter) {
1154                 if ((*iter)->name() == AutomationTimeAxisView::state_node_name) {
1155                         XMLProperty* type = (*iter)->property("automation-id");
1156                         if (type && type->value() == sym)
1157                                 return *iter;
1158                 }
1159         }
1160
1161         // Didn't find it, make a new one
1162         XMLNode* child = new XMLNode (AutomationTimeAxisView::state_node_name);
1163         child->add_property("automation-id", sym);
1164         xml_node->add_child_nocopy (*child);
1165
1166         return child;
1167 }
1168
1169 int
1170 RouteUI::set_color_from_route ()
1171 {
1172         XMLProperty *prop;
1173
1174         RouteUI::ensure_xml_node ();
1175
1176         if ((prop = xml_node->property ("color")) != 0) {
1177                 int r, g, b;
1178                 sscanf (prop->value().c_str(), "%d:%d:%d", &r, &g, &b);
1179                 _color.set_red(r);
1180                 _color.set_green(g);
1181                 _color.set_blue(b);
1182                 return 0;
1183         }
1184         return 1;
1185 }
1186
1187 void
1188 RouteUI::remove_this_route ()
1189 {
1190         vector<string> choices;
1191         string prompt;
1192
1193         if (is_track()) {
1194                 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());
1195         } else {
1196                 prompt  = string_compose (_("Do you really want to remove bus \"%1\" ?\n(this cannot be undone)"), _route->name());
1197         }
1198
1199         choices.push_back (_("No, do nothing."));
1200         choices.push_back (_("Yes, remove it."));
1201
1202         string title;
1203         if (is_track()) {
1204                 title = _("Remove track");
1205         } else {
1206                 title = _("Remove bus");
1207         }
1208
1209         Choice prompter (title, prompt, choices);
1210
1211         if (prompter.run () == 1) {
1212                 Glib::signal_idle().connect (sigc::bind (sigc::ptr_fun (&RouteUI::idle_remove_this_route), this));
1213         }
1214 }
1215
1216 gint
1217 RouteUI::idle_remove_this_route (RouteUI *rui)
1218 {
1219         rui->_session->remove_route (rui->_route);
1220         return false;
1221 }
1222
1223 void
1224 RouteUI::route_rename ()
1225 {
1226         ArdourPrompter name_prompter (true);
1227         string result;
1228         name_prompter.set_prompt (_("New Name: "));
1229         name_prompter.set_initial_text (_route->name());
1230         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
1231         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
1232         name_prompter.show_all ();
1233
1234         switch (name_prompter.run ()) {
1235
1236         case Gtk::RESPONSE_ACCEPT:
1237         name_prompter.get_result (result);
1238         if (result.length()) {
1239                         _route->set_name (result);
1240                 }
1241                 break;
1242         }
1243
1244         return;
1245
1246 }
1247
1248 void
1249 RouteUI::property_changed (const PropertyChange& what_changed)
1250 {
1251         if (what_changed.contains (ARDOUR::Properties::name)) {
1252                 name_label.set_text (_route->name());
1253         }
1254 }
1255
1256 void
1257 RouteUI::toggle_route_active ()
1258 {
1259         bool yn;
1260
1261         if (route_active_menu_item) {
1262                 if (route_active_menu_item->get_active() != (yn = _route->active())) {
1263                         _route->set_active (!yn);
1264                 }
1265         }
1266 }
1267
1268 void
1269 RouteUI::route_active_changed ()
1270 {
1271         if (route_active_menu_item) {
1272                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&CheckMenuItem::set_active, route_active_menu_item, _route->active()));
1273         }
1274 }
1275
1276
1277 void
1278 RouteUI::toggle_denormal_protection ()
1279 {
1280         if (denormal_menu_item) {
1281
1282                 bool x;
1283
1284                 ENSURE_GUI_THREAD (*this, &RouteUI::toggle_denormal_protection)
1285
1286                 if ((x = denormal_menu_item->get_active()) != _route->denormal_protection()) {
1287                         _route->set_denormal_protection (x);
1288                 }
1289         }
1290 }
1291
1292 void
1293 RouteUI::denormal_protection_changed ()
1294 {
1295         if (denormal_menu_item) {
1296                 denormal_menu_item->set_active (_route->denormal_protection());
1297         }
1298 }
1299
1300 void
1301 RouteUI::disconnect_input ()
1302 {
1303         _route->input()->disconnect (this);
1304 }
1305
1306 void
1307 RouteUI::disconnect_output ()
1308 {
1309         _route->output()->disconnect (this);
1310 }
1311
1312 bool
1313 RouteUI::is_track () const
1314 {
1315         return boost::dynamic_pointer_cast<Track>(_route) != 0;
1316 }
1317
1318 boost::shared_ptr<Track>
1319 RouteUI::track() const
1320 {
1321         return boost::dynamic_pointer_cast<Track>(_route);
1322 }
1323
1324 bool
1325 RouteUI::is_audio_track () const
1326 {
1327         return boost::dynamic_pointer_cast<AudioTrack>(_route) != 0;
1328 }
1329
1330 boost::shared_ptr<AudioTrack>
1331 RouteUI::audio_track() const
1332 {
1333         return boost::dynamic_pointer_cast<AudioTrack>(_route);
1334 }
1335
1336 bool
1337 RouteUI::is_midi_track () const
1338 {
1339         return boost::dynamic_pointer_cast<MidiTrack>(_route) != 0;
1340 }
1341
1342 boost::shared_ptr<MidiTrack>
1343 RouteUI::midi_track() const
1344 {
1345         return boost::dynamic_pointer_cast<MidiTrack>(_route);
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 }