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