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