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