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