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