4222b40a93c9bb6bdf82da37a943dd3bad5d722d
[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/choice.h>
22 #include <gtkmm2ext/doi.h>
23 #include <gtkmm2ext/bindable_button.h>
24 #include <gtkmm2ext/barcontroller.h>
25 #include <gtkmm2ext/gtk_ui.h>
26
27 #include "ardour/route_group.h"
28 #include "ardour/dB.h"
29 #include "pbd/memento_command.h"
30 #include "pbd/stacktrace.h"
31 #include "pbd/controllable.h"
32 #include "pbd/enumwriter.h"
33
34 #include "ardour_ui.h"
35 #include "editor.h"
36 #include "route_ui.h"
37 #include "led.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/midi_track.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 Gtk;
61 using namespace Gtkmm2ext;
62 using namespace ARDOUR;
63 using namespace PBD;
64
65 uint32_t RouteUI::_max_invert_buttons = 4;
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         delete record_menu;
89         delete _invert_menu;
90 }
91
92 void
93 RouteUI::init ()
94 {
95         self_destruct = true;
96         xml_node = 0;
97         mute_menu = 0;
98         solo_menu = 0;
99         sends_menu = 0;
100         record_menu = 0;
101         _invert_menu = 0;
102         pre_fader_mute_check = 0;
103         post_fader_mute_check = 0;
104         listen_mute_check = 0;
105         main_mute_check = 0;
106         solo_safe_check = 0;
107         solo_isolated_check = 0;
108         solo_isolated_led = 0;
109         solo_safe_led = 0;
110         _solo_release = 0;
111         _mute_release = 0;
112         route_active_menu_item = 0;
113         denormal_menu_item = 0;
114         step_edit_item = 0;
115         multiple_mute_change = false;
116         multiple_solo_change = false;
117         _i_am_the_modifier = 0;
118
119         setup_invert_buttons ();
120
121         mute_button = manage (new BindableToggleButton ());
122         // mute_button->set_self_managed (true);
123         mute_button->set_name ("MuteButton");
124         mute_button->add (mute_button_label);
125         mute_button_label.show ();
126         UI::instance()->set_tip (mute_button, _("Mute this track"), "");
127
128         solo_button = manage (new BindableToggleButton ());
129         // solo_button->set_self_managed (true);
130         solo_button->set_name ("SoloButton");
131         solo_button->add (solo_button_label);
132         solo_button_label.show ();
133         UI::instance()->set_tip (solo_button, _("Mute other (non-soloed) tracks"), "");
134         solo_button->set_no_show_all (true);
135
136         rec_enable_button = manage (new BindableToggleButton ());
137         rec_enable_button->set_name ("RecordEnableButton");
138         // rec_enable_button->set_self_managed (true);
139         rec_enable_button->add (rec_enable_button_label);
140         rec_enable_button_label.show ();
141         UI::instance()->set_tip (rec_enable_button, _("Enable recording on this track"), "");
142
143         show_sends_button = manage (new BindableToggleButton (""));
144         show_sends_button->set_name ("SendAlert");
145         // show_sends_button->set_self_managed (true);
146         UI::instance()->set_tip (show_sends_button, _("make mixer strips show sends to this bus"), "");
147
148         _session->SoloChanged.connect (_session_connections, invalidator (*this), boost::bind (&RouteUI::solo_changed_so_update_mute, this), gui_context());
149         _session->TransportStateChange.connect (_session_connections, invalidator (*this), boost::bind (&RouteUI::check_rec_enable_sensitivity, this), gui_context());
150         _session->RecordStateChanged.connect (_session_connections, invalidator (*this), boost::bind (&RouteUI::session_rec_enable_changed, this), gui_context());
151
152         Config->ParameterChanged.connect (*this, invalidator (*this), ui_bind (&RouteUI::parameter_changed, this, _1), gui_context());
153
154         rec_enable_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::rec_enable_press), false);
155         rec_enable_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::rec_enable_release), false);
156
157         show_sends_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::show_sends_press), false);
158         show_sends_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::show_sends_release));
159
160         solo_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::solo_press), false);
161         solo_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::solo_release), false);
162         mute_button->signal_button_press_event().connect (sigc::mem_fun(*this, &RouteUI::mute_press), false);
163         mute_button->signal_button_release_event().connect (sigc::mem_fun(*this, &RouteUI::mute_release), false);
164 }
165
166 void
167 RouteUI::reset ()
168 {
169         route_connections.drop_connections ();
170
171         delete solo_menu;
172         solo_menu = 0;
173
174         delete mute_menu;
175         mute_menu = 0;
176
177         if (xml_node) {
178                 /* do not delete the node - its owned by the route */
179                 xml_node = 0;
180         }
181
182         route_active_menu_item = 0;
183         denormal_menu_item = 0;
184 }
185
186 void
187 RouteUI::self_delete ()
188 {
189         /* This may be called from a non-GUI thread. Keep it safe */
190
191         delete_when_idle (this);
192 }
193
194 void
195 RouteUI::set_route (boost::shared_ptr<Route> rp)
196 {
197         reset ();
198
199         _route = rp;
200
201         if (set_color_from_route()) {
202                 set_color (unique_random_color());
203         }
204
205         if (self_destruct) {
206                 rp->DropReferences.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::self_delete, this), gui_context());
207         }
208         
209         mute_button->set_controllable (_route->mute_control());
210         solo_button->set_controllable (_route->solo_control());
211
212         _route->active_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::route_active_changed, this), gui_context());
213         _route->mute_changed.connect (route_connections, invalidator (*this), ui_bind (&RouteUI::mute_changed, this, _1), gui_context());
214
215         _route->solo_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::update_solo_display, this), gui_context());
216         _route->solo_safe_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::update_solo_display, this), gui_context());
217         _route->listen_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::update_solo_display, this), gui_context());
218         _route->solo_isolated_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::update_solo_display, this), gui_context());
219
220         _route->phase_invert_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::polarity_changed, this), gui_context());
221         _route->PropertyChanged.connect (route_connections, invalidator (*this), ui_bind (&RouteUI::property_changed, this, _1), gui_context());
222
223         _route->io_changed.connect (route_connections, invalidator (*this), ui_bind (&RouteUI::setup_invert_buttons, this), gui_context ());
224
225         if (_session->writable() && is_track()) {
226                 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track>(_route);
227
228                 t->RecordEnableChanged.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::route_rec_enable_changed, this), gui_context());
229
230                 rec_enable_button->show();
231                 rec_enable_button->set_controllable (t->rec_enable_control());
232
233                 update_rec_display ();
234
235                 if (is_midi_track()) {
236                         midi_track()->StepEditStatusChange.connect (route_connections, invalidator (*this),
237                                                                     ui_bind (&RouteUI::step_edit_changed, this, _1), gui_context());
238                 }
239
240         }
241
242         mute_button->unset_flags (Gtk::CAN_FOCUS);
243         solo_button->unset_flags (Gtk::CAN_FOCUS);
244
245         mute_button->show();
246
247         if (_route->is_monitor()) {
248                 solo_button->hide ();
249         } else {
250                 solo_button->show();
251         }
252
253         map_frozen ();
254
255         setup_invert_buttons ();
256         set_invert_button_state ();
257 }
258
259 void
260 RouteUI::polarity_changed ()
261 {
262         if (!_route) {
263                 return;
264         }
265
266         set_invert_button_state ();
267 }
268
269 bool
270 RouteUI::mute_press (GdkEventButton* ev)
271 {
272         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS ) {
273                 return true;
274         }
275
276         multiple_mute_change = false;
277
278         if (!_i_am_the_modifier) {
279
280                 if (Keyboard::is_context_menu_event (ev)) {
281
282                         if (mute_menu == 0){
283                                 build_mute_menu();
284                         }
285
286                         mute_menu->popup(0,ev->time);
287
288                 } else {
289
290                         if (Keyboard::is_button2_event (ev)) {
291                                 // Primary-button2 click is the midi binding click
292                                 // button2-click is "momentary"
293
294
295                                 if (mute_button->on_button_press_event (ev)) {
296                                         return true;
297                                 }
298
299                                 _mute_release = new SoloMuteRelease (_route->muted ());
300                         }
301
302                         if (ev->button == 1 || Keyboard::is_button2_event (ev)) {
303
304                                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
305
306                                         if (_mute_release) {
307                                                 _mute_release->routes = _session->get_routes ();
308                                         }
309
310                                         _session->set_mute (_session->get_routes(), !_route->muted());
311
312                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
313
314                                         /* Primary-button1 applies change to the mix group even if it is not active
315                                            NOTE: Primary-button2 is MIDI learn.
316                                         */
317
318                                         if (ev->button == 1 && _route->route_group()) {
319                                                 if (_mute_release) {
320                                                         _mute_release->routes = _session->get_routes ();
321                                                 }
322                                                                 
323                                                 _session->set_mute (_session->get_routes(), !_route->muted(), Session::rt_cleanup, true);
324                                         }
325
326                                 } else {
327
328                                         /* plain click applies change to this route */
329                                         
330                                         boost::shared_ptr<RouteList> rl (new RouteList);
331                                         rl->push_back (_route);
332
333                                         if (_mute_release) {
334                                                 _mute_release->routes = rl;
335                                         }
336
337                                         _session->set_mute (rl, !_route->muted());
338
339                                 }
340                         }
341                 }
342
343         }
344
345         return true;
346 }
347
348 bool
349 RouteUI::mute_release (GdkEventButton*)
350 {
351         if (!_i_am_the_modifier) {
352                 if (_mute_release){
353                         _session->set_mute (_mute_release->routes, _mute_release->active, Session::rt_cleanup, true);
354                         delete _mute_release;
355                         _mute_release = 0;
356                 }
357         }
358
359         return true;
360 }
361
362 bool
363 RouteUI::solo_press(GdkEventButton* ev)
364 {
365         /* ignore double/triple clicks */
366
367         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS ) {
368                 return true;
369         }
370         
371         multiple_solo_change = false;
372
373         if (!_i_am_the_modifier) {
374                 
375                 if (Keyboard::is_context_menu_event (ev)) {
376                         
377                         if (!solo_isolated_led) {
378
379                                 if (solo_menu == 0) {
380                                         build_solo_menu ();
381                                 }
382                                 
383                                 solo_menu->popup (1, ev->time);
384                         }
385                         
386                 } else {
387                         
388                         if (Keyboard::is_button2_event (ev)) {
389                                 
390                                 // Primary-button2 click is the midi binding click
391                                 // button2-click is "momentary"
392                                 
393                                 if (solo_button->on_button_press_event (ev)) {
394                                         return true;
395                                 }
396
397                                 _solo_release = new SoloMuteRelease (_route->self_soloed());
398                         }
399                         
400                         if (ev->button == 1 || Keyboard::is_button2_event (ev)) {
401                                 
402                                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
403                                         
404                                         /* Primary-Tertiary-click applies change to all routes */
405
406                                         if (_solo_release) {
407                                                 _solo_release->routes = _session->get_routes ();
408                                         }
409                                         
410                                         if (Config->get_solo_control_is_listen_control()) {
411                                                 _session->set_listen (_session->get_routes(), !_route->listening(),  Session::rt_cleanup, true);
412                                         } else {
413                                                 _session->set_solo (_session->get_routes(), !_route->self_soloed(),  Session::rt_cleanup, true);
414                                         }
415                                         
416                                 } else if (Keyboard::modifier_state_contains (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::SecondaryModifier))) {
417                                         
418                                         // Primary-Secondary-click: exclusively solo this track
419
420                                         if (_solo_release) {
421                                                 _solo_release->exclusive = true;
422
423                                                 boost::shared_ptr<RouteList> routes = _session->get_routes();
424
425                                                 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
426                                                         if ((*i)->soloed ()) {
427                                                                 _solo_release->routes_on->push_back (*i);
428                                                         } else {
429                                                                 _solo_release->routes_off->push_back (*i);
430                                                         }
431                                                 }
432                                         }
433                                         
434                                         if (Config->get_solo_control_is_listen_control()) {
435                                                 /* ??? we need a just_one_listen() method */
436                                         } else {
437                                                 _session->set_just_one_solo (_route, true);
438                                         }
439
440                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
441                                         
442                                         // shift-click: toggle solo isolated status
443                                         
444                                         _route->set_solo_isolated (!_route->solo_isolated(), this);
445                                         delete _solo_release;
446                                         _solo_release = 0;
447                                         
448                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
449                                         
450                                         /* Primary-button1: solo mix group.
451                                            NOTE: Primary-button2 is MIDI learn.
452                                         */
453                                         
454                                         if (ev->button == 1 && _route->route_group()) {
455
456                                                 if (_solo_release) {
457                                                         _solo_release->routes = _route->route_group()->route_list();
458                                                 }
459                                         
460                                                 if (Config->get_solo_control_is_listen_control()) {
461                                                         _session->set_listen (_route->route_group()->route_list(), !_route->listening(),  Session::rt_cleanup, true);
462                                                 } else {
463                                                         _session->set_solo (_route->route_group()->route_list(), !_route->self_soloed(),  Session::rt_cleanup, true);
464                                                 }
465                                         }
466                                         
467                                 } else {
468                                         
469                                         /* click: solo this route */
470                                         
471                                         boost::shared_ptr<RouteList> rl (new RouteList);
472                                         rl->push_back (route());
473
474                                         if (_solo_release) {
475                                                 _solo_release->routes = rl;
476                                         }
477
478                                         if (Config->get_solo_control_is_listen_control()) {
479                                                 _session->set_listen (rl, !_route->listening());
480                                         } else {
481                                                 _session->set_solo (rl, !_route->self_soloed());
482                                         }
483                                 }
484                         }
485                 }
486         }
487
488         return true;
489 }
490
491 bool
492 RouteUI::solo_release (GdkEventButton*)
493 {
494         if (!_i_am_the_modifier) {
495                 
496                 if (_solo_release) {
497
498                         if (_solo_release->exclusive) {
499
500                         } else {
501                                 if (Config->get_solo_control_is_listen_control()) {
502                                         _session->set_listen (_solo_release->routes, _solo_release->active, Session::rt_cleanup, true);
503                                 } else {
504                                         _session->set_solo (_solo_release->routes, _solo_release->active, Session::rt_cleanup, true);
505                                 }
506                         }
507
508                         delete _solo_release;
509                         _solo_release = 0;
510                 }
511         }
512
513         return true;
514 }
515
516 bool
517 RouteUI::rec_enable_press(GdkEventButton* ev)
518 {
519         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS ) {
520                 return true;
521         }
522
523         if (!_session->engine().connected()) {
524                 MessageDialog msg (_("Not connected to JACK - cannot engage record"));
525                 msg.run ();
526                 return true;
527         }
528
529         if (is_midi_track()) {
530
531                 /* cannot rec-enable while step-editing */
532
533                 if (midi_track()->step_editing()) {
534                         return true;
535                 } 
536         }
537
538         if (!_i_am_the_modifier && is_track() && rec_enable_button) {
539
540                 if (Keyboard::is_button2_event (ev)) {
541
542                         // do nothing on midi sigc::bind event
543                         return rec_enable_button->on_button_press_event (ev);
544
545                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
546
547                         _session->set_record_enabled (_session->get_routes(), !rec_enable_button->get_active());
548
549                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
550
551                         /* Primary-button1 applies change to the route group (even if it is not active)
552                            NOTE: Primary-button2 is MIDI learn.
553                         */
554                         if (ev->button == 1 && _route->route_group()) {
555                                 _session->set_record_enabled (_route->route_group()->route_list(), !rec_enable_button->get_active(), Session::rt_cleanup, true);
556                         }
557
558                 } else if (Keyboard::is_context_menu_event (ev)) {
559
560                         /* do this on release */
561
562                 } else {
563
564                         boost::shared_ptr<RouteList> rl (new RouteList);
565                         rl->push_back (route());
566                         _session->set_record_enabled (rl, !rec_enable_button->get_active());
567                 }
568         }
569
570         return true;
571 }
572
573 void
574 RouteUI::build_record_menu ()
575 {
576         if (record_menu) {
577                 return;
578         }
579
580         /* no rec-button context menu for non-MIDI tracks 
581          */
582
583         if (is_midi_track()) {
584                 record_menu = new Menu;
585                 record_menu->set_name ("ArdourContextMenu");
586                 
587                 using namespace Menu_Helpers;
588                 MenuList& items = record_menu->items();
589                 
590                 items.push_back (CheckMenuElem (_("Step Edit"), sigc::mem_fun (*this, &RouteUI::toggle_step_edit)));
591                 step_edit_item = dynamic_cast<CheckMenuItem*> (&items.back());
592
593                 if (_route->record_enabled()) {
594                         step_edit_item->set_sensitive (false);
595                 }
596
597                 step_edit_item->set_active (midi_track()->step_editing());
598         }
599 }
600
601 void
602 RouteUI::toggle_step_edit ()
603 {
604         if (!is_midi_track() || _route->record_enabled()) {
605                 return;
606         }
607
608         midi_track()->set_step_editing (step_edit_item->get_active());
609 }
610
611 void
612 RouteUI::step_edit_changed (bool yn)
613 {
614         if (yn) {
615                 if (rec_enable_button) {
616                         rec_enable_button->set_visual_state (3);
617                 } 
618
619                 start_step_editing ();
620
621                 if (step_edit_item) {
622                         step_edit_item->set_active (true);
623                 }
624
625         } else {
626
627                 if (rec_enable_button) {
628                         rec_enable_button->set_visual_state (0);
629                 } 
630
631                 stop_step_editing ();
632
633                 if (step_edit_item) {
634                         step_edit_item->set_active (false);
635                 }
636         }
637 }
638
639 bool
640 RouteUI::rec_enable_release (GdkEventButton* ev)
641 {
642         if (Keyboard::is_context_menu_event (ev)) {
643                 build_record_menu ();
644                 if (record_menu) {
645                         record_menu->popup (1, ev->time);
646                 }
647                 return true;
648         }
649
650         return true;
651 }
652
653 void
654 RouteUI::build_sends_menu ()
655 {
656         using namespace Menu_Helpers;
657
658         sends_menu = new Menu;
659         sends_menu->set_name ("ArdourContextMenu");
660         MenuList& items = sends_menu->items();
661
662         items.push_back (MenuElem(_("Assign all tracks (prefader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_sends), PreFader)));
663         items.push_back (MenuElem(_("Assign all tracks (postfader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_sends), PostFader)));
664         items.push_back (MenuElem(_("Assign selected tracks (prefader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_selected_sends), PreFader)));
665         items.push_back (MenuElem(_("Assign selected tracks (postfader)"), sigc::bind (sigc::mem_fun (*this, &RouteUI::create_selected_sends), PostFader)));
666         items.push_back (MenuElem(_("Copy track gains to sends"), sigc::mem_fun (*this, &RouteUI::set_sends_gain_from_track)));
667         items.push_back (MenuElem(_("Set sends gain to -inf"), sigc::mem_fun (*this, &RouteUI::set_sends_gain_to_zero)));
668         items.push_back (MenuElem(_("Set sends gain to 0dB"), sigc::mem_fun (*this, &RouteUI::set_sends_gain_to_unity)));
669
670 }
671
672 void
673 RouteUI::create_sends (Placement p)
674 {
675         _session->globally_add_internal_sends (_route, p);
676 }
677
678 void
679 RouteUI::create_selected_sends (Placement p)
680 {
681         boost::shared_ptr<RouteList> rlist (new RouteList);
682         TrackSelection& selected_tracks (ARDOUR_UI::instance()->the_editor().get_selection().tracks);
683
684         for (TrackSelection::iterator i = selected_tracks.begin(); i != selected_tracks.end(); ++i) {
685                 RouteTimeAxisView* rtv;
686                 RouteUI* rui;
687                 if ((rtv = dynamic_cast<RouteTimeAxisView*>(*i)) != 0) {
688                         if ((rui = dynamic_cast<RouteUI*>(rtv)) != 0) {
689                                 if (boost::dynamic_pointer_cast<AudioTrack>(rui->route())) {
690                                         rlist->push_back (rui->route());
691                                 }
692                         }
693                 }
694         }
695         
696         _session->add_internal_sends (_route, p, rlist);
697 }
698
699 void
700 RouteUI::set_sends_gain_from_track ()
701 {
702         _session->globally_set_send_gains_from_track (_route);
703 }
704
705 void
706 RouteUI::set_sends_gain_to_zero ()
707 {
708         _session->globally_set_send_gains_to_zero (_route);
709 }
710
711 void
712 RouteUI::set_sends_gain_to_unity ()
713 {
714         _session->globally_set_send_gains_to_unity (_route);
715 }
716
717 bool
718 RouteUI::show_sends_press(GdkEventButton* ev)
719 {
720         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS ) {
721                 return true;
722         }
723
724         if (!_i_am_the_modifier && !is_track() && show_sends_button) {
725
726                 if (Keyboard::is_button2_event (ev) && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
727
728                         // do nothing on midi sigc::bind event
729                         return false;
730
731                 } else if (Keyboard::is_context_menu_event (ev)) {
732
733                         if (sends_menu == 0) {
734                                 build_sends_menu ();
735                         }
736
737                         sends_menu->popup (0, ev->time);
738
739                 } else {
740
741                         /* change button state */
742
743                         show_sends_button->set_active (!show_sends_button->get_active());
744
745                         /* start blinking */
746
747                         if (show_sends_button->get_active()) {
748                                 /* show sends to this bus */
749                                 MixerStrip::SwitchIO (_route);
750                                 send_blink_connection = ARDOUR_UI::instance()->Blink.connect (sigc::mem_fun(*this, &RouteUI::send_blink));
751                         } else {
752                                 /* everybody back to normal */
753                                 send_blink_connection.disconnect ();
754                                 MixerStrip::SwitchIO (boost::shared_ptr<Route>());
755                         }
756
757                 }
758         }
759
760         return true;
761 }
762
763 bool
764 RouteUI::show_sends_release (GdkEventButton*)
765 {
766         return true;
767 }
768
769 void
770 RouteUI::send_blink (bool onoff)
771 {
772         if (!show_sends_button) {
773                 return;
774         }
775
776         if (onoff) {
777                 show_sends_button->set_state (STATE_ACTIVE);
778         } else {
779                 show_sends_button->set_state (STATE_NORMAL);
780         }
781 }
782
783 int
784 RouteUI::solo_visual_state (boost::shared_ptr<Route> r)
785 {
786         if (r->is_master() || r->is_monitor()) {
787                 return 0;
788         }
789         
790         if (Config->get_solo_control_is_listen_control()) {
791
792                 if (r->listening()) {
793                         return 1;
794                 } else {
795                         return 0;
796                 }
797
798         } 
799         
800         if (r->soloed()) {
801                 if (!r->self_soloed()) {
802                         return 3;
803                 } else {
804                         return 1;
805                 }
806         } else {
807                 return 0;
808         }
809 }
810
811 int
812 RouteUI::solo_visual_state_with_isolate (boost::shared_ptr<Route> r)
813 {
814         if (r->is_master() || r->is_monitor()) {
815                 return 0;
816         }
817         
818         if (Config->get_solo_control_is_listen_control()) {
819
820                 if (r->listening()) {
821                         return 1;
822                 } else {
823                         return 0;
824                 }
825
826         } 
827         
828         if (r->solo_isolated()) {
829                 return 2;
830         } else if (r->soloed()) {
831                 if (!r->self_soloed()) {
832                         return 3;
833                 } else {
834                         return 1;
835                 }
836         } else {
837                 return 0;
838         }
839 }
840
841 int
842 RouteUI::solo_isolate_visual_state (boost::shared_ptr<Route> r)
843 {
844         if (r->is_master() || r->is_monitor()) {
845                 return 0;
846         }
847         
848         if (r->solo_isolated()) {
849                 return 1;
850         } else {
851                 return 0;
852         }
853 }
854
855 int
856 RouteUI::solo_safe_visual_state (boost::shared_ptr<Route> r)
857 {
858         if (r->is_master() || r->is_monitor()) {
859                 return 0;
860         }
861         
862         if (r->solo_safe()) {
863                 return 1;
864         } else {
865                 return 0;
866         }
867 }
868
869 void
870 RouteUI::update_solo_display ()
871 {
872         bool x;
873
874         if (Config->get_solo_control_is_listen_control()) {
875
876                 if (solo_button->get_active() != (x = _route->listening())) {
877                         ++_i_am_the_modifier;
878                         solo_button->set_active(x);
879                         --_i_am_the_modifier;
880                 }
881
882         } else {
883
884                 if (solo_button->get_active() != (x = _route->soloed())) {
885                         ++_i_am_the_modifier;
886                         solo_button->set_active (x);
887                         --_i_am_the_modifier;
888                 }
889
890         }
891
892         bool yn = _route->solo_safe ();
893
894         if (solo_safe_check && solo_safe_check->get_active() != yn) {
895                 solo_safe_check->set_active (yn);
896         }
897
898         yn = _route->solo_isolated ();
899
900         if (solo_isolated_check && solo_isolated_check->get_active() != yn) {
901                 solo_isolated_check->set_active (yn);
902         }
903
904         set_button_names ();
905
906         if (solo_isolated_led) {
907                 solo_isolated_led->set_visual_state (_route->solo_isolated() ? 1 : 0);
908         }
909
910         if (solo_safe_led) {
911                 solo_safe_led->set_visual_state (_route->solo_safe() ? 1 : 0);
912         }
913
914         solo_button->set_visual_state (solo_visual_state (_route));
915         
916         /* some changes to solo status can affect mute display, so catch up 
917          */
918
919         update_mute_display ();
920 }
921
922 void
923 RouteUI::solo_changed_so_update_mute ()
924 {
925         update_mute_display ();
926 }
927
928 void
929 RouteUI::mute_changed(void* /*src*/)
930 {
931         update_mute_display ();
932 }
933
934 int
935 RouteUI::mute_visual_state (Session* s, boost::shared_ptr<Route> r)
936 {
937         if (r->is_master() || r->is_monitor()) {
938                 return 0;
939         }
940
941         if (Config->get_show_solo_mutes()) {
942
943                 if (r->muted ()) {
944                         /* full mute */
945                         return 2;
946                 } else if (s->soloing() && !r->soloed() && !r->solo_isolated()) {
947                         return 1;
948                 } else {
949                         /* no mute at all */
950                         return 0;
951                 }
952
953         } else {
954
955                 if (r->muted()) {
956                         /* full mute */
957                         return 2;
958                 } else {
959                         /* no mute at all */
960                         return 0;
961                 }
962         }
963
964         return 0;
965 }
966
967 void
968 RouteUI::update_mute_display ()
969 {
970         if (!_route) {
971                 return;
972         }
973
974         bool model = _route->muted();
975         bool view = mute_button->get_active();
976
977         /* first make sure the button's "depressed" visual
978            is correct.
979         */
980
981         if (model != view) {
982                 ++_i_am_the_modifier;
983                 mute_button->set_active (model);
984                 --_i_am_the_modifier;
985         }
986
987         mute_button->set_visual_state (mute_visual_state (_session, _route));
988 }
989
990 void
991 RouteUI::route_rec_enable_changed ()
992 {
993         update_rec_display ();
994 }
995
996 void
997 RouteUI::session_rec_enable_changed ()
998 {
999         update_rec_display ();
1000 }
1001
1002 void
1003 RouteUI::update_rec_display ()
1004 {
1005         if (!rec_enable_button || !_route) {
1006                 return;
1007         }
1008                         
1009         bool model = _route->record_enabled();
1010         bool view = rec_enable_button->get_active();
1011
1012         /* first make sure the button's "depressed" visual
1013            is correct.
1014         */
1015
1016         if (model != view) {
1017                 ++_i_am_the_modifier;
1018                 rec_enable_button->set_active (model);
1019                 --_i_am_the_modifier;
1020         }
1021
1022         /* now make sure its color state is correct */
1023
1024         if (model) {
1025                 switch (_session->record_status ()) {
1026                 case Session::Recording:
1027                         rec_enable_button->set_visual_state (1);
1028                         break;
1029                         
1030                 case Session::Disabled:
1031                 case Session::Enabled:
1032                         rec_enable_button->set_visual_state (2);
1033                         break;
1034                         
1035                 }
1036
1037                 if (step_edit_item) {
1038                         step_edit_item->set_sensitive (false);
1039                 }
1040
1041         } else {
1042                 rec_enable_button->set_visual_state (0);
1043
1044                 if (step_edit_item) {
1045                         step_edit_item->set_sensitive (true);
1046                 }
1047         }
1048         
1049
1050         check_rec_enable_sensitivity ();
1051 }
1052
1053 void
1054 RouteUI::build_solo_menu (void)
1055 {
1056         using namespace Menu_Helpers;
1057
1058         solo_menu = new Menu;
1059         solo_menu->set_name ("ArdourContextMenu");
1060         MenuList& items = solo_menu->items();
1061         CheckMenuItem* check;
1062
1063         check = new CheckMenuItem(_("Solo Isolate"));
1064         check->set_active (_route->solo_isolated());
1065         check->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_solo_isolated), check));
1066         items.push_back (CheckMenuElem(*check));
1067         solo_isolated_check = dynamic_cast<CheckMenuItem*>(&items.back());
1068         check->show_all();
1069
1070         check = new CheckMenuItem(_("Solo Safe"));
1071         check->set_active (_route->solo_safe());
1072         check->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_solo_safe), check));
1073         items.push_back (CheckMenuElem(*check));
1074         solo_safe_check = dynamic_cast<CheckMenuItem*>(&items.back());
1075         check->show_all();
1076
1077         //items.push_back (SeparatorElem());
1078         // items.push_back (MenuElem (_("MIDI Bind"), sigc::mem_fun (*mute_button, &BindableToggleButton::midi_learn)));
1079
1080 }
1081
1082 void
1083 RouteUI::build_mute_menu(void)
1084 {
1085         using namespace Menu_Helpers;
1086
1087         mute_menu = new Menu;
1088         mute_menu->set_name ("ArdourContextMenu");
1089
1090         MenuList& items = mute_menu->items();
1091
1092         pre_fader_mute_check = manage (new CheckMenuItem(_("Pre Fader")));
1093         init_mute_menu(MuteMaster::PreFader, pre_fader_mute_check);
1094         pre_fader_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::PreFader, pre_fader_mute_check));
1095         items.push_back (CheckMenuElem(*pre_fader_mute_check));
1096         pre_fader_mute_check->show_all();
1097
1098         post_fader_mute_check = manage (new CheckMenuItem(_("Post Fader")));
1099         init_mute_menu(MuteMaster::PostFader, post_fader_mute_check);
1100         post_fader_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::PostFader, post_fader_mute_check));
1101         items.push_back (CheckMenuElem(*post_fader_mute_check));
1102         post_fader_mute_check->show_all();
1103
1104         listen_mute_check = manage (new CheckMenuItem(_("Control Outs")));
1105         init_mute_menu(MuteMaster::Listen, listen_mute_check);
1106         listen_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::Listen, listen_mute_check));
1107         items.push_back (CheckMenuElem(*listen_mute_check));
1108         listen_mute_check->show_all();
1109
1110         main_mute_check = manage (new CheckMenuItem(_("Main Outs")));
1111         init_mute_menu(MuteMaster::Main, main_mute_check);
1112         main_mute_check->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_mute_menu), MuteMaster::Main, main_mute_check));
1113         items.push_back (CheckMenuElem(*main_mute_check));
1114         main_mute_check->show_all();
1115
1116         //items.push_back (SeparatorElem());
1117         // items.push_back (MenuElem (_("MIDI Bind"), sigc::mem_fun (*mute_button, &BindableToggleButton::midi_learn)));
1118
1119         _route->mute_points_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::muting_change, this), gui_context());
1120 }
1121
1122 void
1123 RouteUI::init_mute_menu(MuteMaster::MutePoint mp, CheckMenuItem* check)
1124 {
1125         check->set_active (_route->mute_points() & mp);
1126 }
1127
1128 void
1129 RouteUI::toggle_mute_menu(MuteMaster::MutePoint mp, Gtk::CheckMenuItem* check)
1130 {
1131         if (check->get_active()) {
1132                 _route->set_mute_points (MuteMaster::MutePoint (_route->mute_points() | mp));
1133         } else {
1134                 _route->set_mute_points (MuteMaster::MutePoint (_route->mute_points() & ~mp));
1135         }
1136 }
1137
1138 void
1139 RouteUI::muting_change ()
1140 {
1141         ENSURE_GUI_THREAD (*this, &RouteUI::muting_change)
1142
1143         bool yn;
1144         MuteMaster::MutePoint current = _route->mute_points ();
1145
1146         yn = (current & MuteMaster::PreFader);
1147
1148         if (pre_fader_mute_check->get_active() != yn) {
1149                 pre_fader_mute_check->set_active (yn);
1150         }
1151
1152         yn = (current & MuteMaster::PostFader);
1153
1154         if (post_fader_mute_check->get_active() != yn) {
1155                 post_fader_mute_check->set_active (yn);
1156         }
1157
1158         yn = (current & MuteMaster::Listen);
1159
1160         if (listen_mute_check->get_active() != yn) {
1161                 listen_mute_check->set_active (yn);
1162         }
1163
1164         yn = (current & MuteMaster::Main);
1165
1166         if (main_mute_check->get_active() != yn) {
1167                 main_mute_check->set_active (yn);
1168         }
1169 }
1170
1171 bool
1172 RouteUI::solo_isolate_button_release (GdkEventButton* ev)
1173 {
1174         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS) {
1175                 return true;
1176         }
1177
1178         bool view = (solo_isolated_led->visual_state() != 0);
1179         bool model = _route->solo_isolated();
1180
1181         /* called BEFORE the view has changed */
1182
1183         if (ev->button == 1) {
1184                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
1185
1186                         if (model) {
1187                                 /* disable isolate for all routes */
1188                                 _session->set_solo_isolated (_session->get_routes(), false, Session::rt_cleanup, true);
1189                         }
1190
1191                 } else {
1192                         if (model == view) {
1193
1194                                 /* flip just this route */
1195
1196                                 boost::shared_ptr<RouteList> rl (new RouteList);
1197                                 rl->push_back (_route);
1198                                 _session->set_solo_isolated (rl, !view, Session::rt_cleanup, true);
1199                         }
1200                 }
1201         }
1202
1203         return true;
1204 }
1205
1206 bool
1207 RouteUI::solo_safe_button_release (GdkEventButton*)
1208 {
1209         _route->set_solo_safe (!(solo_safe_led->visual_state() > 0), this);
1210         return true;
1211 }
1212
1213 void
1214 RouteUI::toggle_solo_isolated (Gtk::CheckMenuItem* check)
1215 {
1216         bool view = check->get_active();
1217         bool model = _route->solo_isolated();
1218
1219         /* called AFTER the view has changed */
1220
1221         if (model != view) {
1222                 _route->set_solo_isolated (view, this);
1223         }
1224 }
1225
1226 void
1227 RouteUI::toggle_solo_safe (Gtk::CheckMenuItem* check)
1228 {
1229         _route->set_solo_safe (check->get_active(), this);
1230 }
1231
1232 bool
1233 RouteUI::choose_color()
1234 {
1235         bool picked;
1236         Gdk::Color color;
1237
1238         color = Gtkmm2ext::UI::instance()->get_color (_("Color Selection"), picked, &_color);
1239
1240         if (picked) {
1241                 set_color (color);
1242         }
1243
1244         return picked;
1245 }
1246
1247 void
1248 RouteUI::set_color (const Gdk::Color & c)
1249 {
1250         char buf[64];
1251
1252         _color = c;
1253
1254         ensure_xml_node ();
1255         snprintf (buf, sizeof (buf), "%d:%d:%d", c.get_red(), c.get_green(), c.get_blue());
1256         xml_node->add_property ("color", buf);
1257
1258         _route->gui_changed ("color", (void *) 0); /* EMIT_SIGNAL */
1259 }
1260
1261
1262 void
1263 RouteUI::ensure_xml_node ()
1264 {
1265         if (xml_node == 0) {
1266                 if ((xml_node = _route->extra_xml ("GUI")) == 0) {
1267                         xml_node = new XMLNode ("GUI");
1268                         _route->add_extra_xml (*xml_node);
1269                 }
1270         }
1271 }
1272
1273 XMLNode*
1274 RouteUI::get_automation_child_xml_node (Evoral::Parameter param, int version)
1275 {
1276         ensure_xml_node ();
1277
1278         XMLNodeList kids = xml_node->children();
1279         XMLNodeConstIterator iter;
1280
1281         const string sym = ARDOUR::EventTypeMap::instance().to_symbol(param);
1282
1283         for (iter = kids.begin(); iter != kids.end(); ++iter) {
1284
1285                 if (version < 3000) {
1286                         if ((*iter)->name() == sym) {
1287                                 return *iter;
1288                         }
1289                 } else {
1290                         if ((*iter)->name() == AutomationTimeAxisView::state_node_name) {
1291                                 XMLProperty* type = (*iter)->property("automation-id");
1292                                 if (type && type->value() == sym)
1293                                         return *iter;
1294                         }
1295                 }
1296         }
1297
1298         // Didn't find it, make a new one
1299         XMLNode* child = new XMLNode (AutomationTimeAxisView::state_node_name);
1300         child->add_property("automation-id", sym);
1301         xml_node->add_child_nocopy (*child);
1302
1303         return child;
1304 }
1305
1306 int
1307 RouteUI::set_color_from_route ()
1308 {
1309         XMLProperty *prop;
1310
1311         RouteUI::ensure_xml_node ();
1312
1313         if ((prop = xml_node->property ("color")) != 0) {
1314                 int r, g, b;
1315                 sscanf (prop->value().c_str(), "%d:%d:%d", &r, &g, &b);
1316                 _color.set_red(r);
1317                 _color.set_green(g);
1318                 _color.set_blue(b);
1319                 return 0;
1320         }
1321         return 1;
1322 }
1323
1324 void
1325 RouteUI::remove_this_route ()
1326 {
1327         if ((route()->is_master() || route()->is_monitor()) &&
1328             !Config->get_allow_special_bus_removal()) {
1329                 MessageDialog msg (_("That would be bad news ...."),
1330                                    false,
1331                                    Gtk::MESSAGE_INFO,
1332                                    Gtk::BUTTONS_OK);
1333                 msg.set_secondary_text (string_compose (_(
1334 "Removing the master or monitor bus is such a bad idea\n\
1335 that %1 is not going to allow it.\n\
1336 \n\
1337 If you really want to do this sort of thing\n\
1338 edit your ardour.rc file to set the\n\
1339 \"allow-special-bus-removal\" option to be \"yes\""), PROGRAM_NAME));
1340
1341                 msg.present ();
1342                 msg.run ();
1343                 return;
1344         }
1345
1346         vector<string> choices;
1347         string prompt;
1348
1349         if (is_track()) {
1350                 prompt  = string_compose (_("Do you really want to remove track \"%1\" ?\n\nYou may also lose the playlist used by this track.\n\n(This action cannot be undone, and the session file will be overwritten)"), _route->name());
1351         } else {
1352                 prompt  = string_compose (_("Do you really want to remove bus \"%1\" ?\n\nYou may also lose the playlist used by this track.\n\n(This action cannot be undone, and the session file will be overwritten)"), _route->name());
1353         }
1354
1355         choices.push_back (_("No, do nothing."));
1356         choices.push_back (_("Yes, remove it."));
1357
1358         string title;
1359         if (is_track()) {
1360                 title = _("Remove track");
1361         } else {
1362                 title = _("Remove bus");
1363         }
1364
1365         Choice prompter (title, prompt, choices);
1366
1367         if (prompter.run () == 1) {
1368                 Glib::signal_idle().connect (sigc::bind (sigc::ptr_fun (&RouteUI::idle_remove_this_route), this));
1369         }
1370 }
1371
1372 gint
1373 RouteUI::idle_remove_this_route (RouteUI *rui)
1374 {
1375         rui->_session->remove_route (rui->route());
1376         return false;
1377 }
1378
1379 void
1380 RouteUI::route_rename ()
1381 {
1382         ArdourPrompter name_prompter (true);
1383         string result;
1384         if (is_track()) {
1385                 name_prompter.set_title (_("Rename Track"));
1386         } else {
1387                 name_prompter.set_title (_("Rename Bus"));
1388         }
1389         name_prompter.set_prompt (_("New name:"));
1390         name_prompter.set_initial_text (_route->name());
1391         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
1392         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
1393         name_prompter.show_all ();
1394
1395         switch (name_prompter.run ()) {
1396
1397         case Gtk::RESPONSE_ACCEPT:
1398         name_prompter.get_result (result);
1399         if (result.length()) {
1400                         _route->set_name (result);
1401                 }
1402                 break;
1403         }
1404
1405         return;
1406
1407 }
1408
1409 void
1410 RouteUI::property_changed (const PropertyChange& what_changed)
1411 {
1412         if (what_changed.contains (ARDOUR::Properties::name)) {
1413                 name_label.set_text (_route->name());
1414         }
1415 }
1416
1417 void
1418 RouteUI::toggle_route_active ()
1419 {
1420         bool yn;
1421
1422         if (route_active_menu_item) {
1423                 if (route_active_menu_item->get_active() != (yn = _route->active())) {
1424                         _route->set_active (!yn);
1425                 }
1426         }
1427 }
1428
1429 void
1430 RouteUI::route_active_changed ()
1431 {
1432         if (route_active_menu_item) {
1433                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&CheckMenuItem::set_active, route_active_menu_item, _route->active()));
1434         }
1435 }
1436
1437
1438 void
1439 RouteUI::toggle_denormal_protection ()
1440 {
1441         if (denormal_menu_item) {
1442
1443                 bool x;
1444
1445                 ENSURE_GUI_THREAD (*this, &RouteUI::toggle_denormal_protection)
1446
1447                 if ((x = denormal_menu_item->get_active()) != _route->denormal_protection()) {
1448                         _route->set_denormal_protection (x);
1449                 }
1450         }
1451 }
1452
1453 void
1454 RouteUI::denormal_protection_changed ()
1455 {
1456         if (denormal_menu_item) {
1457                 denormal_menu_item->set_active (_route->denormal_protection());
1458         }
1459 }
1460
1461 void
1462 RouteUI::disconnect_input ()
1463 {
1464         _route->input()->disconnect (this);
1465 }
1466
1467 void
1468 RouteUI::disconnect_output ()
1469 {
1470         _route->output()->disconnect (this);
1471 }
1472
1473 bool
1474 RouteUI::is_track () const
1475 {
1476         return boost::dynamic_pointer_cast<Track>(_route) != 0;
1477 }
1478
1479 boost::shared_ptr<Track>
1480 RouteUI::track() const
1481 {
1482         return boost::dynamic_pointer_cast<Track>(_route);
1483 }
1484
1485 bool
1486 RouteUI::is_audio_track () const
1487 {
1488         return boost::dynamic_pointer_cast<AudioTrack>(_route) != 0;
1489 }
1490
1491 boost::shared_ptr<AudioTrack>
1492 RouteUI::audio_track() const
1493 {
1494         return boost::dynamic_pointer_cast<AudioTrack>(_route);
1495 }
1496
1497 bool
1498 RouteUI::is_midi_track () const
1499 {
1500         return boost::dynamic_pointer_cast<MidiTrack>(_route) != 0;
1501 }
1502
1503 boost::shared_ptr<MidiTrack>
1504 RouteUI::midi_track() const
1505 {
1506         return boost::dynamic_pointer_cast<MidiTrack>(_route);
1507 }
1508
1509 bool
1510 RouteUI::has_audio_outputs () const
1511 {
1512         return (_route->n_outputs().n_audio() > 0);
1513 }
1514
1515 string
1516 RouteUI::name() const
1517 {
1518         return _route->name();
1519 }
1520
1521 void
1522 RouteUI::map_frozen ()
1523 {
1524         ENSURE_GUI_THREAD (*this, &RouteUI::map_frozen)
1525
1526         AudioTrack* at = dynamic_cast<AudioTrack*>(_route.get());
1527
1528         if (at) {
1529                 switch (at->freeze_state()) {
1530                 case AudioTrack::Frozen:
1531                         rec_enable_button->set_sensitive (false);
1532                         break;
1533                 default:
1534                         rec_enable_button->set_sensitive (true);
1535                         break;
1536                 }
1537         }
1538 }
1539
1540 void
1541 RouteUI::adjust_latency ()
1542 {
1543         LatencyDialog dialog (_route->name() + _(" latency"), *(_route->output()), _session->frame_rate(), _session->engine().frames_per_cycle());
1544 }
1545
1546 void
1547 RouteUI::save_as_template ()
1548 {
1549         sys::path path;
1550         Glib::ustring safe_name;
1551         string name;
1552
1553         path = ARDOUR::user_route_template_directory ();
1554
1555         if (g_mkdir_with_parents (path.to_string().c_str(), 0755)) {
1556                 error << string_compose (_("Cannot create route template directory %1"), path.to_string()) << endmsg;
1557                 return;
1558         }
1559
1560         Prompter p (true); // modal
1561
1562         p.set_title (_("Save As Template"));
1563         p.set_prompt (_("Template name:"));
1564         switch (p.run()) {
1565         case RESPONSE_ACCEPT:
1566                 break;
1567         default:
1568                 return;
1569         }
1570
1571         p.hide ();
1572         p.get_result (name, true);
1573
1574         safe_name = legalize_for_path (name);
1575         safe_name += template_suffix;
1576
1577         path /= safe_name;
1578
1579         _route->save_as_template (path.to_string(), name);
1580 }
1581
1582 void
1583 RouteUI::check_rec_enable_sensitivity ()
1584 {
1585         if (_session->transport_rolling() && rec_enable_button->get_active() && Config->get_disable_disarm_during_roll()) {
1586                 rec_enable_button->set_sensitive (false);
1587         } else {
1588                 rec_enable_button->set_sensitive (true);
1589         }
1590 }
1591
1592 void
1593 RouteUI::parameter_changed (string const & p)
1594 {
1595         if (p == "disable-disarm-during-roll") {
1596                 check_rec_enable_sensitivity ();
1597         } else if (p == "solo-control-is-listen-control") {
1598                 set_button_names ();
1599         } else if (p == "listen-position") {
1600                 set_button_names ();
1601         }
1602 }
1603
1604 void
1605 RouteUI::step_gain_up ()
1606 {
1607         _route->set_gain (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) + 0.1), this);
1608 }
1609
1610 void
1611 RouteUI::page_gain_up ()
1612 {
1613         _route->set_gain (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) + 0.5), this);
1614 }
1615
1616 void
1617 RouteUI::step_gain_down ()
1618 {
1619         _route->set_gain (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) - 0.1), this);
1620 }
1621
1622 void
1623 RouteUI::page_gain_down ()
1624 {
1625         _route->set_gain (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) - 0.5), this);
1626 }
1627
1628 void
1629 RouteUI::open_remote_control_id_dialog ()
1630 {
1631         ArdourDialog dialog (_("Remote Control ID"));
1632
1633         uint32_t const limit = _session->ntracks() + _session->nbusses () + 4;
1634
1635         HBox* hbox = manage (new HBox);
1636         hbox->set_spacing (6);
1637         hbox->pack_start (*manage (new Label (_("Remote control ID:"))));
1638         SpinButton* spin = manage (new SpinButton);
1639         spin->set_digits (0);
1640         spin->set_increments (1, 10);
1641         spin->set_range (0, limit);
1642         spin->set_value (_route->remote_control_id());
1643         hbox->pack_start (*spin);
1644         dialog.get_vbox()->pack_start (*hbox);
1645
1646         dialog.add_button (Stock::CANCEL, RESPONSE_CANCEL);
1647         dialog.add_button (Stock::APPLY, RESPONSE_ACCEPT);
1648
1649         dialog.show_all ();
1650         int const r = dialog.run ();
1651
1652         if (r == RESPONSE_ACCEPT) {
1653                 _route->set_remote_control_id (spin->get_value_as_int ());
1654         }
1655 }
1656
1657 void
1658 RouteUI::setup_invert_buttons ()
1659 {
1660         /* remove old invert buttons */
1661         for (list<BindableToggleButton*>::iterator i = _invert_buttons.begin(); i != _invert_buttons.end(); ++i) {
1662                 _invert_button_box.remove (**i);
1663         }
1664
1665         _invert_buttons.clear ();
1666
1667         if (!_route || !_route->input()) {
1668                 return;
1669         }
1670
1671         uint32_t const N = _route->input()->n_ports().n_audio ();
1672
1673         uint32_t const to_add = (N <= _max_invert_buttons) ? N : 1;
1674
1675         for (uint32_t i = 0; i < to_add; ++i) {
1676                 BindableToggleButton* b = manage (new BindableToggleButton);
1677                 b->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &RouteUI::invert_toggled), i, b));
1678                 b->signal_button_press_event().connect (sigc::mem_fun (*this, &RouteUI::invert_press));
1679                 
1680                 b->set_name (X_("MixerInvertButton"));
1681                 if (to_add == 1) {
1682                         b->add (*manage (new Label (X_("Ø"))));
1683                 } else {
1684                         b->add (*manage (new Label (string_compose (X_("Ø%1"), i + 1))));
1685                 }
1686
1687                 if (N <= 4) {
1688                         UI::instance()->set_tip (*b, string_compose (_("Left-click to invert (phase reverse) channel %1 of this track.  Right-click to show menu."), i + 1));
1689                 } else {
1690                         UI::instance()->set_tip (*b, string_compose (_("Left-click to invert (phase reverse) all channels of this track.  Right-click to show menu."), i + 1));
1691                 }
1692                 
1693                 _invert_buttons.push_back (b);
1694                 _invert_button_box.pack_start (*b);
1695         }
1696         
1697         _invert_button_box.show_all ();
1698 }
1699
1700 void
1701 RouteUI::set_invert_button_state ()
1702 {
1703         ++_i_am_the_modifier;
1704         
1705         uint32_t const N = _route->input()->n_ports().n_audio();
1706         if (N > _max_invert_buttons) {
1707                 _invert_buttons.front()->set_active (_route->phase_invert().any());
1708                 --_i_am_the_modifier;
1709                 return;
1710         }
1711
1712         int j = 0;
1713         for (list<BindableToggleButton*>::iterator i = _invert_buttons.begin(); i != _invert_buttons.end(); ++i, ++j) {
1714                 (*i)->set_active (_route->phase_invert (j));
1715         }
1716
1717         --_i_am_the_modifier;
1718 }
1719
1720 void
1721 RouteUI::invert_toggled (uint32_t i, BindableToggleButton* b)
1722 {
1723         if (_i_am_the_modifier) {
1724                 return;
1725         }
1726         
1727         uint32_t const N = _route->input()->n_ports().n_audio();
1728         if (N <= _max_invert_buttons) {
1729                 _route->set_phase_invert (i, b->get_active ());
1730         } else {
1731                 boost::dynamic_bitset<> p (N);
1732                 if (b->get_active ()) {
1733                         p.set ();
1734                 }
1735                 _route->set_phase_invert (p);
1736         }
1737 }
1738
1739 bool
1740 RouteUI::invert_press (GdkEventButton* ev)
1741 {
1742         using namespace Menu_Helpers;
1743
1744         if (ev->button != 3) {
1745                 return true;
1746         }
1747
1748         delete _invert_menu;
1749         _invert_menu = new Menu;
1750         _invert_menu->set_name ("ArdourContextMenu");
1751         MenuList& items = _invert_menu->items ();
1752
1753         uint32_t const N = _route->input()->n_ports().n_audio();
1754         for (uint32_t i = 0; i < N; ++i) {
1755                 items.push_back (CheckMenuElem (string_compose (X_("Ø%1"), i + 1), sigc::bind (sigc::mem_fun (*this, &RouteUI::invert_menu_toggled), i)));
1756                 CheckMenuItem* e = dynamic_cast<CheckMenuItem*> (&items.back ());
1757                 ++_i_am_the_modifier;
1758                 e->set_active (_route->phase_invert (i));
1759                 --_i_am_the_modifier;
1760         }
1761
1762         _invert_menu->popup (0, ev->time);
1763
1764         return false;
1765 }
1766
1767 void
1768 RouteUI::invert_menu_toggled (uint32_t c)
1769 {
1770         if (_i_am_the_modifier) {
1771                 return;
1772         }
1773         
1774         _route->set_phase_invert (c, !_route->phase_invert (c));
1775 }