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