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