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