solo buttons are now ArdourButtons
[ardour.git] / gtk2_ardour / mixer_strip.cc
1 /*
2     Copyright (C) 2000-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 #include <cmath>
20 #include <algorithm>
21
22 #include <sigc++/bind.h>
23
24 #include "pbd/convert.h"
25 #include "pbd/enumwriter.h"
26 #include "pbd/replace_all.h"
27 #include "pbd/stacktrace.h"
28
29 #include <gtkmm2ext/gtk_ui.h>
30 #include <gtkmm2ext/utils.h>
31 #include <gtkmm2ext/choice.h>
32 #include <gtkmm2ext/doi.h>
33 #include <gtkmm2ext/slider_controller.h>
34 #include <gtkmm2ext/bindable_button.h>
35
36 #include "ardour/ardour.h"
37 #include "ardour/amp.h"
38 #include "ardour/session.h"
39 #include "ardour/audioengine.h"
40 #include "ardour/internal_send.h"
41 #include "ardour/route.h"
42 #include "ardour/route_group.h"
43 #include "ardour/audio_track.h"
44 #include "ardour/midi_track.h"
45 #include "ardour/pannable.h"
46 #include "ardour/panner.h"
47 #include "ardour/panner_shell.h"
48 #include "ardour/send.h"
49 #include "ardour/processor.h"
50 #include "ardour/profile.h"
51 #include "ardour/ladspa_plugin.h"
52 #include "ardour/user_bundle.h"
53 #include "ardour/port.h"
54
55 #include "ardour_ui.h"
56 #include "ardour_dialog.h"
57 #include "mixer_strip.h"
58 #include "mixer_ui.h"
59 #include "keyboard.h"
60 #include "ardour_button.h"
61 #include "public_editor.h"
62 #include "send_ui.h"
63 #include "io_selector.h"
64 #include "utils.h"
65 #include "gui_thread.h"
66 #include "route_group_menu.h"
67
68 #include "i18n.h"
69
70 using namespace ARDOUR;
71 using namespace PBD;
72 using namespace Gtk;
73 using namespace Gtkmm2ext;
74 using namespace std;
75
76 sigc::signal<void,boost::shared_ptr<Route> > MixerStrip::SwitchIO;
77
78 int MixerStrip::scrollbar_height = 0;
79 PBD::Signal1<void,MixerStrip*> MixerStrip::CatchDeletion;
80
81 MixerStrip::MixerStrip (Mixer_UI& mx, Session* sess, bool in_mixer)
82         : AxisView(sess)
83         , RouteUI (sess)
84         , _mixer(mx)
85         , _mixer_owned (in_mixer)
86         , processor_box (sess, boost::bind (&MixerStrip::plugin_selector, this), mx.selection(), this, in_mixer)
87         , gpm (sess, 250)
88         , panners (sess)
89         , button_table (3, 1)
90         , rec_solo_table (2, 2)
91         , top_button_table (1, 2)
92         , middle_button_table (1, 2)
93         , bottom_button_table (1, 2)
94         , meter_point_label (_("pre"))
95         , midi_input_enable_button (0)
96         , _comment_button (_("Comments"))
97         , _visibility (X_("mixer-strip-visibility"))
98 {
99         init ();
100
101         if (!_mixer_owned) {
102                 /* the editor mixer strip: don't destroy it every time
103                    the underlying route goes away.
104                 */
105
106                 self_destruct = false;
107         }
108 }
109
110 MixerStrip::MixerStrip (Mixer_UI& mx, Session* sess, boost::shared_ptr<Route> rt, bool in_mixer)
111         : AxisView(sess)
112         , RouteUI (sess)
113         , _mixer(mx)
114         , _mixer_owned (in_mixer)
115         , processor_box (sess, sigc::mem_fun(*this, &MixerStrip::plugin_selector), mx.selection(), this, in_mixer)
116         , gpm (sess, 250)
117         , panners (sess)
118         , button_table (3, 1)
119         , middle_button_table (1, 2)
120         , bottom_button_table (1, 2)
121         , meter_point_label (_("pre"))
122         , midi_input_enable_button (0)
123         , _comment_button (_("Comments"))
124         , _visibility (X_("mixer-strip-visibility"))
125 {
126         init ();
127         set_route (rt);
128 }
129
130 void
131 MixerStrip::init ()
132 {
133         input_selector = 0;
134         output_selector = 0;
135         group_menu = 0;
136         route_ops_menu = 0;
137         ignore_comment_edit = false;
138         ignore_toggle = false;
139         comment_window = 0;
140         comment_area = 0;
141         _width_owner = 0;
142         spacer = 0;
143
144         /* the length of this string determines the width of the mixer strip when it is set to `wide' */
145         longest_label = "longest label";
146
147         Gtk::Image* img;
148
149         img = manage (new Gtk::Image (::get_icon("strip_width")));
150         img->show ();
151
152         width_button.add (*img);
153
154         img = manage (new Gtk::Image (::get_icon("hide")));
155         img->show ();
156
157         hide_button.add (*img);
158
159         input_label.set_text (_("Input"));
160         ARDOUR_UI::instance()->set_tip (&input_button, _("Button 1 to choose inputs from a port matrix, button 3 to select inputs from a menu"), "");
161         input_button.add (input_label);
162         input_button.set_name ("MixerIOButton");
163         input_label.set_name ("MixerIOButtonLabel");
164         input_button_box.pack_start (input_button, true, true);
165
166         output_label.set_text (_("Output"));
167         ARDOUR_UI::instance()->set_tip (&output_button, _("Button 1 to choose outputs from a port matrix, button 3 to select inputs from a menu"), "");
168         output_button.add (output_label);
169         output_button.set_name ("MixerIOButton");
170         output_label.set_name ("MixerIOButtonLabel");
171         Gtkmm2ext::set_size_request_to_display_given_text (output_button, longest_label.c_str(), 4, 4);
172
173         ARDOUR_UI::instance()->set_tip (&meter_point_button, _("Select metering point"), "");
174         meter_point_button.add (meter_point_label);
175         meter_point_button.set_name ("MixerStripMeterPreButton");
176         meter_point_label.set_name ("MixerStripMeterPreButton");
177
178         /* TRANSLATORS: this string should be longest of the strings
179            used to describe meter points. In english, it's "input".
180         */
181         set_size_request_to_display_given_text (meter_point_button, _("tupni"), 5, 5);
182
183         bottom_button_table.attach (meter_point_button, 1, 2, 0, 1);
184
185         meter_point_button.signal_button_press_event().connect (sigc::mem_fun (gpm, &GainMeter::meter_press), false);
186         meter_point_button.signal_button_release_event().connect (sigc::mem_fun (gpm, &GainMeter::meter_release), false);
187
188         hide_button.set_events (hide_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
189
190         monitor_input_button->set_diameter (3);
191         monitor_disk_button->set_diameter (3);
192
193         solo_isolated_led = manage (new ArdourButton (ArdourButton::led_default_elements));
194         solo_isolated_led->show ();
195         solo_isolated_led->set_diameter (3);
196         solo_isolated_led->set_no_show_all (true);
197         solo_isolated_led->set_name (X_("solo isolate"));
198         solo_isolated_led->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
199         solo_isolated_led->signal_button_release_event().connect (sigc::mem_fun (*this, &RouteUI::solo_isolate_button_release));
200         UI::instance()->set_tip (solo_isolated_led, _("Isolate Solo"), "");
201
202         solo_safe_led = manage (new ArdourButton (ArdourButton::led_default_elements));
203         solo_safe_led->show ();
204         solo_safe_led->set_diameter (3);
205         solo_safe_led->set_no_show_all (true);
206         solo_safe_led->set_name (X_("solo safe"));
207         solo_safe_led->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
208         solo_safe_led->signal_button_release_event().connect (sigc::mem_fun (*this, &RouteUI::solo_safe_button_release));
209         UI::instance()->set_tip (solo_safe_led, _("Lock Solo Status"), "");
210
211         solo_safe_led->set_text (_("lock"));
212         solo_isolated_led->set_text (_("iso"));
213
214         top_button_table.set_homogeneous (true);
215         top_button_table.set_spacings (2);
216         top_button_table.attach (*monitor_input_button, 0, 1, 0, 1);
217         top_button_table.attach (*monitor_disk_button, 1, 2, 0, 1);
218         top_button_table.show ();
219
220         rec_solo_table.set_homogeneous (false);
221         rec_solo_table.set_row_spacings (2);
222         rec_solo_table.set_col_spacings (2);
223         rec_solo_table.attach (*solo_isolated_led, 1, 2, 0, 1);
224         rec_solo_table.attach (*solo_safe_led, 1, 2, 1, 2);
225         rec_solo_table.show ();
226
227         button_table.set_homogeneous (false);
228         button_table.set_spacings (0);
229
230         button_table.attach (name_button, 0, 1, 0, 1);
231         button_table.attach (input_button_box, 0, 1, 1, 2);
232         button_table.attach (_invert_button_box, 0, 1, 2, 3);
233
234         middle_button_table.set_homogeneous (true);
235         middle_button_table.set_spacings (0);
236         middle_button_table.attach (*mute_button, 0, 1, 0, 1);
237         middle_button_table.attach (*solo_button, 1, 2, 0, 1);
238
239         bottom_button_table.set_col_spacings (0);
240         bottom_button_table.set_homogeneous (true);
241         bottom_button_table.attach (group_button, 0, 1, 0, 1);
242
243         name_button.add (name_label);
244         name_button.set_name ("MixerNameButton");
245         Gtkmm2ext::set_size_request_to_display_given_text (name_button, longest_label.c_str(), 2, 2);
246
247         name_label.set_name ("MixerNameButtonLabel");
248         ARDOUR_UI::instance()->set_tip (&group_button, _("Mix group"), "");
249         group_button.add (group_label);
250         group_button.set_name ("MixerGroupButton");
251         Gtkmm2ext::set_size_request_to_display_given_text (group_button, "Group", 2, 2);
252         group_label.set_name ("MixerGroupButtonLabel");
253
254         _comment_button.set_name (X_("MixerCommentButton"));
255         _comment_button.signal_clicked().connect (sigc::mem_fun (*this, &MixerStrip::toggle_comment));
256
257         global_vpacker.set_border_width (0);
258         global_vpacker.set_spacing (0);
259
260         width_button.set_name ("MixerWidthButton");
261         hide_button.set_name ("MixerHideButton");
262         top_event_box.set_name ("MixerTopEventBox");
263
264         width_button.signal_clicked().connect (sigc::mem_fun(*this, &MixerStrip::width_clicked));
265         hide_button.signal_clicked().connect (sigc::mem_fun(*this, &MixerStrip::hide_clicked));
266
267         width_hide_box.pack_start (width_button, false, true);
268         width_hide_box.pack_start (top_event_box, true, true);
269         width_hide_box.pack_end (hide_button, false, true);
270
271         whvbox.pack_start (width_hide_box, true, true);
272
273         global_vpacker.pack_start (whvbox, Gtk::PACK_SHRINK);
274         global_vpacker.pack_start (button_table, Gtk::PACK_SHRINK);
275         global_vpacker.pack_start (processor_box, true, true);
276         global_vpacker.pack_start (panners, Gtk::PACK_SHRINK);
277         global_vpacker.pack_start (top_button_table, Gtk::PACK_SHRINK, 2);
278         global_vpacker.pack_start (rec_solo_table, Gtk::PACK_SHRINK, 2);
279         global_vpacker.pack_start (middle_button_table, Gtk::PACK_SHRINK, 2);
280         global_vpacker.pack_start (gpm, Gtk::PACK_SHRINK);
281         global_vpacker.pack_start (bottom_button_table, Gtk::PACK_SHRINK);
282         global_vpacker.pack_start (output_button, Gtk::PACK_SHRINK);
283         global_vpacker.pack_start (_comment_button, Gtk::PACK_SHRINK);
284
285         global_frame.add (global_vpacker);
286         global_frame.set_shadow_type (Gtk::SHADOW_IN);
287         global_frame.set_name ("BaseFrame");
288
289         add (global_frame);
290
291         /* force setting of visible selected status */
292
293         _selected = true;
294         set_selected (false);
295
296         _packed = false;
297         _embedded = false;
298
299         _session->engine().Stopped.connect (*this, invalidator (*this), boost::bind (&MixerStrip::engine_stopped, this), gui_context());
300         _session->engine().Running.connect (*this, invalidator (*this), boost::bind (&MixerStrip::engine_running, this), gui_context());
301
302         input_button.signal_button_press_event().connect (sigc::mem_fun(*this, &MixerStrip::input_press), false);
303         output_button.signal_button_press_event().connect (sigc::mem_fun(*this, &MixerStrip::output_press), false);
304
305         /* we don't need this if its not an audio track, but we don't know that yet and it doesn't
306            hurt (much).
307         */
308
309         rec_enable_button->set_name ("MixerRecordEnableButton");
310
311         /* ditto for this button and busses */
312
313         name_button.signal_button_press_event().connect (sigc::mem_fun(*this, &MixerStrip::name_button_button_press), false);
314         group_button.signal_button_press_event().connect (sigc::mem_fun(*this, &MixerStrip::select_route_group), false);
315
316         _width = (Width) -1;
317
318         /* start off as a passthru strip. we'll correct this, if necessary,
319            in update_diskstream_display().
320         */
321
322         /* start off as a passthru strip. we'll correct this, if necessary,
323            in update_diskstream_display().
324         */
325
326         if (is_midi_track()) {
327                 set_name ("MidiTrackStripBase");
328         } else {
329                 set_name ("AudioTrackStripBase");
330         }
331
332         add_events (Gdk::BUTTON_RELEASE_MASK|
333                     Gdk::ENTER_NOTIFY_MASK|
334                     Gdk::LEAVE_NOTIFY_MASK|
335                     Gdk::KEY_PRESS_MASK|
336                     Gdk::KEY_RELEASE_MASK);
337
338         set_flags (get_flags() | Gtk::CAN_FOCUS);
339
340         SwitchIO.connect (sigc::mem_fun (*this, &MixerStrip::switch_io));
341
342         AudioEngine::instance()->PortConnectedOrDisconnected.connect (
343                 *this, invalidator (*this), boost::bind (&MixerStrip::port_connected_or_disconnected, this, _1, _3), gui_context ()
344                 );
345
346         /* Add the widgets under visibility control to the VisibilityGroup; the names used here
347            must be the same as those used in RCOptionEditor so that the configuration changes
348            are recognised when they occur.
349         */
350         _visibility.add (&_invert_button_box, X_("PhaseInvert"), _("Phase Invert"));
351         _visibility.add (solo_safe_led, X_("SoloSafe"), _("Solo Safe"));
352         _visibility.add (solo_isolated_led, X_("SoloIsolated"), _("Solo Isolated"));
353         _visibility.add (&_comment_button, X_("Comments"), _("Comments"));
354         _visibility.add (&group_button, X_("Group"), _("Group"));
355         _visibility.add (&meter_point_button, X_("MeterPoint"), _("Meter Point"));
356
357         parameter_changed (X_("mixer-strip-visibility"));
358
359         Config->ParameterChanged.connect (_config_connection, MISSING_INVALIDATOR, ui_bind (&MixerStrip::parameter_changed, this, _1), gui_context());
360 }
361
362 MixerStrip::~MixerStrip ()
363 {
364         CatchDeletion (this);
365
366         delete input_selector;
367         delete output_selector;
368         delete comment_window;
369 }
370
371 void
372 MixerStrip::set_route (boost::shared_ptr<Route> rt)
373 {
374         if (rec_enable_button->get_parent()) {
375                 rec_solo_table.remove (*rec_enable_button);
376         }
377
378         if (show_sends_button->get_parent()) {
379                 rec_solo_table.remove (*show_sends_button);
380         }
381
382         processor_box.set_route (rt);
383
384         RouteUI::set_route (rt);
385
386         /* map the current state */
387
388         mute_changed (0);
389         update_solo_display ();
390
391         delete input_selector;
392         input_selector = 0;
393
394         delete output_selector;
395         output_selector = 0;
396
397         revert_to_default_display ();
398
399         if (route()->is_master()) {
400                 solo_button->hide ();
401                 rec_solo_table.hide ();
402         } else {
403                 solo_button->show ();
404                 rec_solo_table.show ();
405         }
406
407         if (_mixer_owned && (route()->is_master() || route()->is_monitor())) {
408
409                 if (scrollbar_height == 0) {
410                         HScrollbar scrollbar;
411                         Gtk::Requisition requisition(scrollbar.size_request ());
412                         scrollbar_height = requisition.height;
413                 }
414
415                 spacer = manage (new EventBox);
416                 spacer->set_size_request (-1, scrollbar_height);
417                 global_vpacker.pack_start (*spacer, false, false);
418         }
419
420         if (is_track()) {
421                 monitor_input_button->show ();
422                 monitor_disk_button->show ();
423         } else {
424                 monitor_input_button->hide();
425                 monitor_disk_button->hide ();
426         }
427
428         if (is_midi_track()) {
429                 if (midi_input_enable_button == 0) {
430                         Image* img = manage (new Image (get_icon (X_("midi_socket_small"))));
431                         midi_input_enable_button = manage (new StatefulToggleButton);
432                         midi_input_enable_button->set_name ("MixerMidiInputEnableButton");
433                         midi_input_enable_button->set_image (*img);
434                         midi_input_enable_button->signal_button_press_event().connect (sigc::mem_fun (*this, &MixerStrip::input_active_button_press), false);
435                         midi_input_enable_button->signal_button_release_event().connect (sigc::mem_fun (*this, &MixerStrip::input_active_button_release), false);
436                         ARDOUR_UI::instance()->set_tip (midi_input_enable_button, _("Enable/Disable MIDI input"));
437                 } else {
438                         input_button_box.remove (*midi_input_enable_button);
439                 }
440                 /* get current state */
441                 midi_input_status_changed ();
442                 input_button_box.pack_start (*midi_input_enable_button, false, false);
443                 /* follow changes */
444                 midi_track()->InputActiveChanged.connect (route_connections, invalidator (*this), boost::bind (&MixerStrip::midi_input_status_changed, this), gui_context());
445         } else {
446                 if (midi_input_enable_button) {
447                         /* removal from the container will delete it */
448                         input_button_box.remove (*midi_input_enable_button);
449                         midi_input_enable_button = 0;
450                 }
451         }
452
453         if (is_audio_track()) {
454                 boost::shared_ptr<AudioTrack> at = audio_track();
455                 at->FreezeChange.connect (route_connections, invalidator (*this), boost::bind (&MixerStrip::map_frozen, this), gui_context());
456         }
457
458         if (has_audio_outputs ()) {
459                 panners.show_all ();
460         } else {
461                 panners.hide_all ();
462         }
463
464         if (is_track ()) {
465
466                 rec_solo_table.attach (*rec_enable_button, 0, 1, 0, 2);
467                 rec_enable_button->set_sensitive (_session->writable());
468                 rec_enable_button->show();
469
470         } else {
471
472                 /* non-master bus */
473
474                 if (!_route->is_master()) {
475                         rec_solo_table.attach (*show_sends_button, 0, 1, 0, 2);
476                         show_sends_button->show();
477                 }
478         }
479
480         meter_point_label.set_text (meter_point_string (_route->meter_point()));
481
482         delete route_ops_menu;
483         route_ops_menu = 0;
484
485         _route->meter_change.connect (route_connections, invalidator (*this), bind (&MixerStrip::meter_changed, this), gui_context());
486         _route->route_group_changed.connect (route_connections, invalidator (*this), boost::bind (&MixerStrip::route_group_changed, this), gui_context());
487
488         if (_route->panner_shell()) {
489                 _route->panner_shell()->Changed.connect (route_connections, invalidator (*this), boost::bind (&MixerStrip::connect_to_pan, this), gui_context());
490         }
491
492         if (is_audio_track()) {
493                 audio_track()->DiskstreamChanged.connect (route_connections, invalidator (*this), boost::bind (&MixerStrip::diskstream_changed, this), gui_context());
494         }
495
496         _route->comment_changed.connect (route_connections, invalidator (*this), ui_bind (&MixerStrip::comment_changed, this, _1), gui_context());
497
498         set_stuff_from_route ();
499
500         /* now force an update of all the various elements */
501
502         mute_changed (0);
503         update_solo_display ();
504         name_changed ();
505         comment_changed (0);
506         route_group_changed ();
507
508         connect_to_pan ();
509         panners.setup_pan ();
510
511         update_diskstream_display ();
512         update_input_display ();
513         update_output_display ();
514
515         add_events (Gdk::BUTTON_RELEASE_MASK);
516
517         processor_box.show ();
518
519         if (!route()->is_master() && !route()->is_monitor()) {
520                 /* we don't allow master or control routes to be hidden */
521                 hide_button.show();
522         }
523
524         width_button.show();
525         width_hide_box.show();
526         whvbox.show ();
527         global_frame.show();
528         global_vpacker.show();
529         button_table.show();
530         middle_button_table.show();
531         bottom_button_table.show();
532         gpm.show_all ();
533         gain_unit_button.show();
534         gain_unit_label.show();
535         meter_point_button.show();
536         meter_point_label.show();
537         diskstream_button.show();
538         diskstream_label.show();
539         input_button_box.show_all();
540         output_button.show();
541         output_label.show();
542         name_label.show();
543         name_button.show();
544         _comment_button.show();
545         group_button.show();
546         group_label.show();
547
548         parameter_changed ("mixer-strip-visibility");
549
550         show ();
551 }
552
553 void
554 MixerStrip::set_stuff_from_route ()
555 {
556         /* if width is not set, it will be set by the MixerUI or editor */
557
558         string str = gui_property ("strip-width");
559         if (!str.empty()) {
560                 set_width_enum (Width (string_2_enum (str, _width)), this);
561         }
562 }
563
564 void
565 MixerStrip::set_width_enum (Width w, void* owner)
566 {
567         /* always set the gpm width again, things may be hidden */
568
569         gpm.set_width (w);
570         panners.set_width (w);
571
572         boost::shared_ptr<AutomationList> gain_automation = _route->gain_control()->alist();
573
574         _width_owner = owner;
575
576         _width = w;
577
578         if (_width_owner == this) {
579                 set_gui_property ("strip-width", enum_2_string (_width));
580         }
581
582         set_button_names ();
583
584         switch (w) {
585         case Wide:
586                 if (show_sends_button)  {
587                         show_sends_button->set_text (_("Sends"));
588                 }
589
590                 ((Gtk::Label*)gpm.gain_automation_style_button.get_child())->set_text (
591                                 gpm.astyle_string(gain_automation->automation_style()));
592                 ((Gtk::Label*)gpm.gain_automation_state_button.get_child())->set_text (
593                                 gpm.astate_string(gain_automation->automation_state()));
594
595                 if (_route->panner()) {
596                         ((Gtk::Label*)panners.pan_automation_style_button.get_child())->set_text (
597                                         panners.astyle_string(_route->panner()->automation_style()));
598                         ((Gtk::Label*)panners.pan_automation_state_button.get_child())->set_text (
599                                         panners.astate_string(_route->panner()->automation_state()));
600                 }
601
602                 solo_isolated_led->set_text (_("iso"));
603                 solo_safe_led->set_text (_("lock"));
604
605                 Gtkmm2ext::set_size_request_to_display_given_text (name_button, "long", 2, 2);
606                 set_size_request (-1, -1);
607                 break;
608
609         case Narrow:
610                 if (show_sends_button) {
611                         show_sends_button->set_text (_("Snd"));
612                 }
613
614                 ((Gtk::Label*)gpm.gain_automation_style_button.get_child())->set_text (
615                                 gpm.short_astyle_string(gain_automation->automation_style()));
616                 ((Gtk::Label*)gpm.gain_automation_state_button.get_child())->set_text (
617                                 gpm.short_astate_string(gain_automation->automation_state()));
618
619                 if (_route->panner()) {
620                         ((Gtk::Label*)panners.pan_automation_style_button.get_child())->set_text (
621                         panners.short_astyle_string(_route->panner()->automation_style()));
622                         ((Gtk::Label*)panners.pan_automation_state_button.get_child())->set_text (
623                         panners.short_astate_string(_route->panner()->automation_state()));
624                 }
625                 
626                 solo_isolated_led->set_text ("");
627                 solo_safe_led->set_text ("");
628
629                 Gtkmm2ext::set_size_request_to_display_given_text (name_button, longest_label.c_str(), 2, 2);
630                 set_size_request (max (50, gpm.get_gm_width()), -1);
631                 break;
632         }
633
634         processor_box.set_width (w);
635
636         update_input_display ();
637         update_output_display ();
638         setup_comment_button ();
639         route_group_changed ();
640         name_changed ();
641         WidthChanged ();
642 }
643
644 void
645 MixerStrip::set_packed (bool yn)
646 {
647         _packed = yn;
648
649         if (_packed) {
650                 set_gui_property ("visible", true);
651         } else {
652                 set_gui_property ("visible", false);
653         }
654 }
655
656
657 struct RouteCompareByName {
658         bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
659                 return a->name().compare (b->name()) < 0;
660         }
661 };
662
663 gint
664 MixerStrip::output_press (GdkEventButton *ev)
665 {
666         using namespace Menu_Helpers;
667         if (!_session->engine().connected()) {
668                 MessageDialog msg (_("Not connected to JACK - no I/O changes are possible"));
669                 msg.run ();
670                 return true;
671         }
672
673         MenuList& citems = output_menu.items();
674         switch (ev->button) {
675
676         case 1:
677                 edit_output_configuration ();
678                 break;
679
680         case 3:
681         {
682                 output_menu.set_name ("ArdourContextMenu");
683                 citems.clear ();
684                 output_menu_bundles.clear ();
685
686                 citems.push_back (MenuElem (_("Disconnect"), sigc::mem_fun (*(static_cast<RouteUI*>(this)), &RouteUI::disconnect_output)));
687                 citems.push_back (SeparatorElem());
688
689                 ARDOUR::BundleList current = _route->output()->bundles_connected ();
690
691                 boost::shared_ptr<ARDOUR::BundleList> b = _session->bundles ();
692
693                 /* give user bundles first chance at being in the menu */
694
695                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
696                         if (boost::dynamic_pointer_cast<UserBundle> (*i)) {
697                                 maybe_add_bundle_to_output_menu (*i, current);
698                         }
699                 }
700
701                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
702                         if (boost::dynamic_pointer_cast<UserBundle> (*i) == 0) {
703                                 maybe_add_bundle_to_output_menu (*i, current);
704                         }
705                 }
706
707                 boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
708                 RouteList copy = *routes;
709                 copy.sort (RouteCompareByName ());
710                 for (ARDOUR::RouteList::const_iterator i = copy.begin(); i != copy.end(); ++i) {
711                         maybe_add_bundle_to_output_menu ((*i)->input()->bundle(), current);
712                 }
713
714                 if (citems.size() == 2) {
715                         /* no routes added; remove the separator */
716                         citems.pop_back ();
717                 }
718
719                 output_menu.popup (1, ev->time);
720                 break;
721         }
722
723         default:
724                 break;
725         }
726         return TRUE;
727 }
728
729 void
730 MixerStrip::edit_output_configuration ()
731 {
732         if (output_selector == 0) {
733
734                 boost::shared_ptr<Send> send;
735                 boost::shared_ptr<IO> output;
736
737                 if ((send = boost::dynamic_pointer_cast<Send>(_current_delivery)) != 0) {
738                         if (!boost::dynamic_pointer_cast<InternalSend>(send)) {
739                                 output = send->output();
740                         } else {
741                                 output = _route->output ();
742                         }
743                 } else {
744                         output = _route->output ();
745                 }
746
747                 output_selector = new IOSelectorWindow (_session, output);
748         }
749
750         if (output_selector->is_visible()) {
751                 output_selector->get_toplevel()->get_window()->raise();
752         } else {
753                 output_selector->present ();
754         }
755 }
756
757 void
758 MixerStrip::edit_input_configuration ()
759 {
760         if (input_selector == 0) {
761                 input_selector = new IOSelectorWindow (_session, _route->input());
762         }
763
764         if (input_selector->is_visible()) {
765                 input_selector->get_toplevel()->get_window()->raise();
766         } else {
767                 input_selector->present ();
768         }
769 }
770
771 gint
772 MixerStrip::input_press (GdkEventButton *ev)
773 {
774         using namespace Menu_Helpers;
775
776         MenuList& citems = input_menu.items();
777         input_menu.set_name ("ArdourContextMenu");
778         citems.clear();
779
780         if (!_session->engine().connected()) {
781                 MessageDialog msg (_("Not connected to JACK - no I/O changes are possible"));
782                 msg.run ();
783                 return true;
784         }
785
786         if (_session->actively_recording() && _route->record_enabled())
787                 return true;
788
789         switch (ev->button) {
790
791         case 1:
792                 edit_input_configuration ();
793                 break;
794
795         case 3:
796         {
797                 citems.push_back (MenuElem (_("Disconnect"), sigc::mem_fun (*(static_cast<RouteUI*>(this)), &RouteUI::disconnect_input)));
798                 citems.push_back (SeparatorElem());
799                 input_menu_bundles.clear ();
800
801                 ARDOUR::BundleList current = _route->input()->bundles_connected ();
802
803                 boost::shared_ptr<ARDOUR::BundleList> b = _session->bundles ();
804
805                 /* give user bundles first chance at being in the menu */
806
807                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
808                         if (boost::dynamic_pointer_cast<UserBundle> (*i)) {
809                                 maybe_add_bundle_to_input_menu (*i, current);
810                         }
811                 }
812
813                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
814                         if (boost::dynamic_pointer_cast<UserBundle> (*i) == 0) {
815                                 maybe_add_bundle_to_input_menu (*i, current);
816                         }
817                 }
818
819                 boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
820                 RouteList copy = *routes;
821                 copy.sort (RouteCompareByName ());
822                 for (ARDOUR::RouteList::const_iterator i = copy.begin(); i != copy.end(); ++i) {
823                         maybe_add_bundle_to_input_menu ((*i)->output()->bundle(), current);
824                 }
825
826                 if (citems.size() == 2) {
827                         /* no routes added; remove the separator */
828                         citems.pop_back ();
829                 }
830
831                 input_menu.popup (1, ev->time);
832                 break;
833         }
834         default:
835                 break;
836         }
837         return TRUE;
838 }
839
840 void
841 MixerStrip::bundle_input_toggled (boost::shared_ptr<ARDOUR::Bundle> c)
842 {
843         if (ignore_toggle) {
844                 return;
845         }
846
847         ARDOUR::BundleList current = _route->input()->bundles_connected ();
848
849         if (std::find (current.begin(), current.end(), c) == current.end()) {
850                 _route->input()->connect_ports_to_bundle (c, this);
851         } else {
852                 _route->input()->disconnect_ports_from_bundle (c, this);
853         }
854 }
855
856 void
857 MixerStrip::bundle_output_toggled (boost::shared_ptr<ARDOUR::Bundle> c)
858 {
859         if (ignore_toggle) {
860                 return;
861         }
862
863         ARDOUR::BundleList current = _route->output()->bundles_connected ();
864
865         if (std::find (current.begin(), current.end(), c) == current.end()) {
866                 _route->output()->connect_ports_to_bundle (c, this);
867         } else {
868                 _route->output()->disconnect_ports_from_bundle (c, this);
869         }
870 }
871
872 void
873 MixerStrip::maybe_add_bundle_to_input_menu (boost::shared_ptr<Bundle> b, ARDOUR::BundleList const & current)
874 {
875         using namespace Menu_Helpers;
876
877         if (b->ports_are_outputs() == false || b->nchannels() != _route->n_inputs()) {
878                 return;
879         }
880
881         list<boost::shared_ptr<Bundle> >::iterator i = input_menu_bundles.begin ();
882         while (i != input_menu_bundles.end() && b->has_same_ports (*i) == false) {
883                 ++i;
884         }
885
886         if (i != input_menu_bundles.end()) {
887                 return;
888         }
889
890         input_menu_bundles.push_back (b);
891
892         MenuList& citems = input_menu.items();
893
894         std::string n = b->name ();
895         replace_all (n, "_", " ");
896
897         citems.push_back (CheckMenuElem (n, sigc::bind (sigc::mem_fun(*this, &MixerStrip::bundle_input_toggled), b)));
898
899         if (std::find (current.begin(), current.end(), b) != current.end()) {
900                 ignore_toggle = true;
901                 dynamic_cast<CheckMenuItem *> (&citems.back())->set_active (true);
902                 ignore_toggle = false;
903         }
904 }
905
906 void
907 MixerStrip::maybe_add_bundle_to_output_menu (boost::shared_ptr<Bundle> b, ARDOUR::BundleList const & current)
908 {
909         using namespace Menu_Helpers;
910
911         if (b->ports_are_inputs() == false || b->nchannels() != _route->n_outputs()) {
912                 return;
913         }
914
915         list<boost::shared_ptr<Bundle> >::iterator i = output_menu_bundles.begin ();
916         while (i != output_menu_bundles.end() && b->has_same_ports (*i) == false) {
917                 ++i;
918         }
919
920         if (i != output_menu_bundles.end()) {
921                 return;
922         }
923
924         output_menu_bundles.push_back (b);
925
926         MenuList& citems = output_menu.items();
927
928         std::string n = b->name ();
929         replace_all (n, "_", " ");
930
931         citems.push_back (CheckMenuElem (n, sigc::bind (sigc::mem_fun(*this, &MixerStrip::bundle_output_toggled), b)));
932
933         if (std::find (current.begin(), current.end(), b) != current.end()) {
934                 ignore_toggle = true;
935                 dynamic_cast<CheckMenuItem *> (&citems.back())->set_active (true);
936                 ignore_toggle = false;
937         }
938 }
939
940 void
941 MixerStrip::update_diskstream_display ()
942 {
943         if (is_track()) {
944
945                 if (input_selector) {
946                         input_selector->hide_all ();
947                 }
948
949                 route_color_changed ();
950
951         } else {
952
953                 show_passthru_color ();
954         }
955 }
956
957 void
958 MixerStrip::connect_to_pan ()
959 {
960         ENSURE_GUI_THREAD (*this, &MixerStrip::connect_to_pan)
961
962         panstate_connection.disconnect ();
963         panstyle_connection.disconnect ();
964
965         if (!_route->panner()) {
966                 return;
967         }
968
969         boost::shared_ptr<Pannable> p = _route->pannable ();
970
971         p->automation_state_changed.connect (panstate_connection, invalidator (*this), boost::bind (&PannerUI::pan_automation_state_changed, &panners), gui_context());
972         p->automation_style_changed.connect (panstyle_connection, invalidator (*this), boost::bind (&PannerUI::pan_automation_style_changed, &panners), gui_context());
973
974         panners.panshell_changed ();
975 }
976
977
978 /*
979  * Output port labelling
980  * =====================
981  *
982  * Case 1: Each output has one connection, all connections are to system:playback_%i
983  *   out 1 -> system:playback_1
984  *   out 2 -> system:playback_2
985  *   out 3 -> system:playback_3
986  *   Display as: 1/2/3
987  *
988  * Case 2: Each output has one connection, all connections are to ardour:track_x/in 1
989  *   out 1 -> ardour:track_x/in 1
990  *   out 2 -> ardour:track_x/in 2
991  *   Display as: track_x
992  *
993  * Case 3: Each output has one connection, all connections are to Jack client "program x"
994  *   out 1 -> program x:foo
995  *   out 2 -> program x:foo
996  *   Display as: program x
997  *
998  * Case 4: No connections (Disconnected)
999  *   Display as: -
1000  *
1001  * Default case (unusual routing):
1002  *   Display as: *number of connections*
1003  *
1004  * Tooltips
1005  * ========
1006  * .-----------------------------------------------.
1007  * | Mixdown                                       |
1008  * | out 1 -> ardour:master/in 1, jamin:input/in 1 |
1009  * | out 2 -> ardour:master/in 2, jamin:input/in 2 |
1010  * '-----------------------------------------------'
1011  * .-----------------------------------------------.
1012  * | Guitar SM58                                   |
1013  * | Disconnected                                  |
1014  * '-----------------------------------------------'
1015  */
1016
1017 void
1018 MixerStrip::update_io_button (boost::shared_ptr<ARDOUR::Route> route, Width width, bool for_input)
1019 {
1020         uint32_t io_count;
1021         uint32_t io_index;
1022         boost::shared_ptr<Port> port;
1023         vector<string> port_connections;
1024
1025         uint32_t total_connection_count = 0;
1026         uint32_t io_connection_count = 0;
1027         uint32_t ardour_connection_count = 0;
1028         uint32_t system_connection_count = 0;
1029         uint32_t other_connection_count = 0;
1030
1031         ostringstream label;
1032         string label_string;
1033
1034         bool have_label = false;
1035         bool each_io_has_one_connection = true;
1036
1037         string connection_name;
1038         string ardour_track_name;
1039         string other_connection_type;
1040         string system_ports;
1041         string system_port;
1042
1043         ostringstream tooltip;
1044         char * tooltip_cstr;
1045
1046         tooltip << route->name();
1047
1048         if (for_input) {
1049                 io_count = route->n_inputs().n_total();
1050         } else {
1051                 io_count = route->n_outputs().n_total();
1052         }
1053
1054         for (io_index = 0; io_index < io_count; ++io_index) {
1055                 if (for_input) {
1056                         port = route->input()->nth (io_index);
1057                 } else {
1058                         port = route->output()->nth (io_index);
1059                 }
1060
1061                 port_connections.clear ();
1062                 port->get_connections(port_connections);
1063                 io_connection_count = 0;
1064
1065                 if (!port_connections.empty()) {
1066                         for (vector<string>::iterator i = port_connections.begin(); i != port_connections.end(); ++i) {
1067                                 string& connection_name (*i);
1068
1069                                 if (io_connection_count == 0) {
1070                                         tooltip << endl << port->name().substr(port->name().find("/") + 1) << " -> " << connection_name;
1071                                 } else {
1072                                         tooltip << ", " << connection_name;
1073                                 }
1074
1075                                 if (connection_name.find("ardour:") == 0) {
1076                                         if (ardour_track_name.empty()) {
1077                                                 // "ardour:Master/in 1" -> "ardour:Master/"
1078                                                 string::size_type slash = connection_name.find("/");
1079                                                 if (slash != string::npos) {
1080                                                         ardour_track_name = connection_name.substr(0, slash + 1);
1081                                                 }
1082                                         }
1083
1084                                         if (connection_name.find(ardour_track_name) == 0) {
1085                                                 ++ardour_connection_count;
1086                                         }
1087                                 } else if (connection_name.find("system:") == 0) {
1088                                         if (for_input) {
1089                                                 // "system:capture_123" -> "123"
1090                                                 system_port = connection_name.substr(15);
1091                                         } else {
1092                                                 // "system:playback_123" -> "123"
1093                                                 system_port = connection_name.substr(16);
1094                                         }
1095
1096                                         if (system_ports.empty()) {
1097                                                 system_ports += system_port;
1098                                         } else {
1099                                                 system_ports += "/" + system_port;
1100                                         }
1101
1102                                         ++system_connection_count;
1103                                 } else {
1104                                         if (other_connection_type.empty()) {
1105                                                 // "jamin:in 1" -> "jamin:"
1106                                                 other_connection_type = connection_name.substr(0, connection_name.find(":") + 1);
1107                                         }
1108
1109                                         if (connection_name.find(other_connection_type) == 0) {
1110                                                 ++other_connection_count;
1111                                         }
1112                                 }
1113
1114                                 ++total_connection_count;
1115                                 ++io_connection_count;
1116                         }
1117                 }
1118
1119                 if (io_connection_count != 1) {
1120                         each_io_has_one_connection = false;
1121                 }
1122         }
1123
1124         if (total_connection_count == 0) {
1125                 tooltip << endl << _("Disconnected");
1126         }
1127
1128         tooltip_cstr = new char[tooltip.str().size() + 1];
1129         strcpy(tooltip_cstr, tooltip.str().c_str());
1130
1131         if (for_input) {
1132                 ARDOUR_UI::instance()->set_tip (&input_button, tooltip_cstr, "");
1133         } else {
1134                 ARDOUR_UI::instance()->set_tip (&output_button, tooltip_cstr, "");
1135         }
1136
1137         if (each_io_has_one_connection) {
1138                 if ((total_connection_count == ardour_connection_count)) {
1139                         // all connections are to the same track in ardour
1140                         // "ardour:Master/" -> "Master"
1141                         string::size_type slash = ardour_track_name.find("/");
1142                         if (slash != string::npos) {
1143                                 label << ardour_track_name.substr(7, slash - 7);
1144                                 have_label = true;
1145                         }
1146                 }
1147                 else if (total_connection_count == system_connection_count) {
1148                         // all connections are to system ports
1149                         label << system_ports;
1150                         have_label = true;
1151                 }
1152                 else if (total_connection_count == other_connection_count) {
1153                         // all connections are to the same external program eg jamin
1154                         // "jamin:" -> "jamin"
1155                         label << other_connection_type.substr(0, other_connection_type.size() - 1);
1156                         have_label = true;
1157                 }
1158         }
1159
1160         if (!have_label) {
1161                 if (total_connection_count == 0) {
1162                         // Disconnected
1163                         label << "-";
1164                 } else {
1165                         // Odd configuration
1166                         label << "*" << total_connection_count << "*";
1167                 }
1168         }
1169
1170         switch (width) {
1171         case Wide:
1172                 label_string = label.str().substr(0, 6);
1173                 break;
1174         case Narrow:
1175                 label_string = label.str().substr(0, 3);
1176                 break;
1177         }
1178
1179         if (for_input) {
1180                 input_label.set_text (label_string);
1181         } else {
1182                 output_label.set_text (label_string);
1183         }
1184 }
1185
1186 void
1187 MixerStrip::update_input_display ()
1188 {
1189         update_io_button (_route, _width, true);
1190         panners.setup_pan ();
1191 }
1192
1193 void
1194 MixerStrip::update_output_display ()
1195 {
1196         update_io_button (_route, _width, false);
1197         gpm.setup_meters ();
1198         panners.setup_pan ();
1199 }
1200
1201 void
1202 MixerStrip::fast_update ()
1203 {
1204         gpm.update_meters ();
1205 }
1206
1207 void
1208 MixerStrip::diskstream_changed ()
1209 {
1210         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&MixerStrip::update_diskstream_display, this));
1211 }
1212
1213 void
1214 MixerStrip::port_connected_or_disconnected (boost::weak_ptr<Port> wa, boost::weak_ptr<Port> wb)
1215 {
1216         boost::shared_ptr<Port> a = wa.lock ();
1217         boost::shared_ptr<Port> b = wb.lock ();
1218
1219         if ((a && _route->input()->has_port (a)) || (b && _route->input()->has_port (b))) {
1220                 update_input_display ();
1221                 set_width_enum (_width, this);
1222         }
1223
1224         if ((a && _route->output()->has_port (a)) || (b && _route->output()->has_port (b))) {
1225                 update_output_display ();
1226                 set_width_enum (_width, this);
1227         }
1228 }
1229
1230 void
1231 MixerStrip::setup_comment_button ()
1232 {
1233         switch (_width) {
1234
1235         case Wide:
1236                 if (_route->comment().empty ()) {
1237                         _comment_button.unset_bg (STATE_NORMAL);
1238                         ((Gtk::Label *) _comment_button.get_child ())->set_text (_("Comments"));
1239                 } else {
1240                         _comment_button.modify_bg (STATE_NORMAL, color ());
1241                         ((Gtk::Label *) _comment_button.get_child ())->set_text (_("*Comments*"));
1242                 }
1243                 break;
1244
1245         case Narrow:
1246                 if (_route->comment().empty ()) {
1247                         _comment_button.unset_bg (STATE_NORMAL);
1248                         ((Gtk::Label *) _comment_button.get_child ())->set_text (_("Cmt"));
1249                 } else {
1250                         _comment_button.modify_bg (STATE_NORMAL, color ());
1251                         ((Gtk::Label *) _comment_button.get_child ())->set_text (_("*Cmt*"));
1252                 }
1253                 break;
1254         }
1255
1256         ARDOUR_UI::instance()->set_tip (
1257                 _comment_button, _route->comment().empty() ? _("Click to Add/Edit Comments") : _route->comment()
1258                 );
1259 }
1260
1261 void
1262 MixerStrip::comment_editor_done_editing ()
1263 {
1264         string const str = comment_area->get_buffer()->get_text();
1265         if (str == _route->comment ()) {
1266                 return;
1267         }
1268
1269         _route->set_comment (str, this);
1270         setup_comment_button ();
1271 }
1272
1273 void
1274 MixerStrip::toggle_comment ()
1275 {
1276         if (ignore_toggle) {
1277                 return;
1278         }
1279
1280         if (comment_window == 0) {
1281                 setup_comment_editor ();
1282         }
1283
1284         if (comment_window->is_visible ()) {
1285                 comment_window->hide ();
1286                 return;
1287         }
1288
1289         string title;
1290         title = _route->name();
1291         title += _(": comment editor");
1292
1293         comment_window->set_title (title);
1294         comment_window->present();
1295 }
1296
1297 void
1298 MixerStrip::setup_comment_editor ()
1299 {
1300         comment_window = new ArdourDialog ("", false); // title will be reset to show route
1301         comment_window->set_position (Gtk::WIN_POS_MOUSE);
1302         comment_window->set_skip_taskbar_hint (true);
1303         comment_window->signal_hide().connect (sigc::mem_fun(*this, &MixerStrip::comment_editor_done_editing));
1304         comment_window->set_default_size (400, 200);
1305
1306         comment_area = manage (new TextView());
1307         comment_area->set_name ("MixerTrackCommentArea");
1308         comment_area->set_wrap_mode (WRAP_WORD);
1309         comment_area->set_editable (true);
1310         comment_area->get_buffer()->set_text (_route->comment());
1311         comment_area->show ();
1312
1313         comment_window->get_vbox()->pack_start (*comment_area);
1314         comment_window->get_action_area()->hide();
1315 }
1316
1317 void
1318 MixerStrip::comment_changed (void *src)
1319 {
1320         ENSURE_GUI_THREAD (*this, &MixerStrip::comment_changed, src)
1321
1322         if (src != this) {
1323                 ignore_comment_edit = true;
1324                 if (comment_area) {
1325                         comment_area->get_buffer()->set_text (_route->comment());
1326                 }
1327                 ignore_comment_edit = false;
1328         }
1329 }
1330
1331 bool
1332 MixerStrip::select_route_group (GdkEventButton *ev)
1333 {
1334         using namespace Menu_Helpers;
1335
1336         if (ev->button == 1) {
1337
1338                 if (group_menu == 0) {
1339
1340                         PropertyList* plist = new PropertyList();
1341
1342                         plist->add (Properties::gain, true);
1343                         plist->add (Properties::mute, true);
1344                         plist->add (Properties::solo, true);
1345
1346                         group_menu = new RouteGroupMenu (_session, plist);
1347                 }
1348
1349                 WeakRouteList r;
1350                 r.push_back (route ());
1351                 group_menu->build (r);
1352                 group_menu->menu()->popup (1, ev->time);
1353         }
1354
1355         return true;
1356 }
1357
1358 void
1359 MixerStrip::route_group_changed ()
1360 {
1361         ENSURE_GUI_THREAD (*this, &MixerStrip::route_group_changed)
1362
1363         RouteGroup *rg = _route->route_group();
1364
1365         if (rg) {
1366                 group_label.set_text (PBD::short_version (rg->name(), 5));
1367         } else {
1368                 switch (_width) {
1369                 case Wide:
1370                         group_label.set_text (_("Grp"));
1371                         break;
1372                 case Narrow:
1373                         group_label.set_text (_("~G"));
1374                         break;
1375                 }
1376         }
1377 }
1378
1379 void
1380 MixerStrip::route_color_changed ()
1381 {
1382         name_button.modify_bg (STATE_NORMAL, color());
1383         top_event_box.modify_bg (STATE_NORMAL, color());
1384         reset_strip_style ();
1385 }
1386
1387 void
1388 MixerStrip::show_passthru_color ()
1389 {
1390         reset_strip_style ();
1391 }
1392
1393 void
1394 MixerStrip::build_route_ops_menu ()
1395 {
1396         using namespace Menu_Helpers;
1397         route_ops_menu = new Menu;
1398         route_ops_menu->set_name ("ArdourContextMenu");
1399
1400         MenuList& items = route_ops_menu->items();
1401
1402         items.push_back (CheckMenuElem (_("Comments..."), sigc::mem_fun (*this, &MixerStrip::toggle_comment)));
1403         CheckMenuItem* i = dynamic_cast<CheckMenuItem*> (&items.back ());
1404         i->set_active (comment_window && comment_window->is_visible ());
1405         items.push_back (MenuElem (_("Save As Template..."), sigc::mem_fun(*this, &RouteUI::save_as_template)));
1406         items.push_back (MenuElem (_("Rename..."), sigc::mem_fun(*this, &RouteUI::route_rename)));
1407         rename_menu_item = &items.back();
1408
1409         items.push_back (SeparatorElem());
1410         items.push_back (CheckMenuElem (_("Active")));
1411         i = dynamic_cast<CheckMenuItem *> (&items.back());
1412         i->set_active (_route->active());
1413         i->signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &RouteUI::set_route_active), !_route->active(), false));
1414
1415         items.push_back (SeparatorElem());
1416
1417         items.push_back (MenuElem (_("Adjust Latency..."), sigc::mem_fun (*this, &RouteUI::adjust_latency)));
1418
1419         items.push_back (SeparatorElem());
1420         items.push_back (CheckMenuElem (_("Protect Against Denormals"), sigc::mem_fun (*this, &RouteUI::toggle_denormal_protection)));
1421         denormal_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
1422         denormal_menu_item->set_active (_route->denormal_protection());
1423
1424         if (!Profile->get_sae()) {
1425                 items.push_back (SeparatorElem());
1426                 items.push_back (MenuElem (_("Remote Control ID..."), sigc::mem_fun (*this, &RouteUI::open_remote_control_id_dialog)));
1427         }
1428
1429         items.push_back (SeparatorElem());
1430         items.push_back (MenuElem (_("Remove"), sigc::bind (sigc::mem_fun(*this, &RouteUI::remove_this_route), false)));
1431 }
1432
1433 gboolean
1434 MixerStrip::name_button_button_press (GdkEventButton* ev)
1435 {
1436         if (ev->button == 3) {
1437                 list_route_operations ();
1438
1439                 /* do not allow rename if the track is record-enabled */
1440                 rename_menu_item->set_sensitive (!_route->record_enabled());
1441                 route_ops_menu->popup (1, ev->time);
1442
1443         } else if (ev->button == 1) {
1444                 revert_to_default_display ();
1445         }
1446
1447         return false;
1448 }
1449
1450 void
1451 MixerStrip::list_route_operations ()
1452 {
1453         delete route_ops_menu;
1454         build_route_ops_menu ();
1455 }
1456
1457 void
1458 MixerStrip::set_selected (bool yn)
1459 {
1460         AxisView::set_selected (yn);
1461         if (_selected) {
1462                 global_frame.set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
1463                 global_frame.set_name ("MixerStripSelectedFrame");
1464         } else {
1465                 global_frame.set_shadow_type (Gtk::SHADOW_IN);
1466                 global_frame.set_name ("MixerStripFrame");
1467         }
1468         global_frame.queue_draw ();
1469 }
1470
1471 void
1472 MixerStrip::name_changed ()
1473 {
1474         switch (_width) {
1475         case Wide:
1476                 RouteUI::property_changed (PropertyChange (ARDOUR::Properties::name));
1477                 break;
1478         case Narrow:
1479                 name_label.set_text (PBD::short_version (_route->name(), 5));
1480                 break;
1481         }
1482 }
1483
1484 void
1485 MixerStrip::width_clicked ()
1486 {
1487         switch (_width) {
1488         case Wide:
1489                 set_width_enum (Narrow, this);
1490                 break;
1491         case Narrow:
1492                 set_width_enum (Wide, this);
1493                 break;
1494         }
1495 }
1496
1497 void
1498 MixerStrip::hide_clicked ()
1499 {
1500         // LAME fix to reset the button status for when it is redisplayed (part 1)
1501         hide_button.set_sensitive(false);
1502
1503         if (_embedded) {
1504                 Hiding(); /* EMIT_SIGNAL */
1505         } else {
1506                 _mixer.hide_strip (this);
1507         }
1508
1509         // (part 2)
1510         hide_button.set_sensitive(true);
1511 }
1512
1513 void
1514 MixerStrip::set_embedded (bool yn)
1515 {
1516         _embedded = yn;
1517 }
1518
1519 void
1520 MixerStrip::map_frozen ()
1521 {
1522         ENSURE_GUI_THREAD (*this, &MixerStrip::map_frozen)
1523
1524         boost::shared_ptr<AudioTrack> at = audio_track();
1525
1526         if (at) {
1527                 switch (at->freeze_state()) {
1528                 case AudioTrack::Frozen:
1529                         processor_box.set_sensitive (false);
1530                         hide_redirect_editors ();
1531                         break;
1532                 default:
1533                         processor_box.set_sensitive (true);
1534                         // XXX need some way, maybe, to retoggle redirect editors
1535                         break;
1536                 }
1537         }
1538 }
1539
1540 void
1541 MixerStrip::hide_redirect_editors ()
1542 {
1543         _route->foreach_processor (sigc::mem_fun (*this, &MixerStrip::hide_processor_editor));
1544 }
1545
1546 void
1547 MixerStrip::hide_processor_editor (boost::weak_ptr<Processor> p)
1548 {
1549         boost::shared_ptr<Processor> processor (p.lock ());
1550         if (!processor) {
1551                 return;
1552         }
1553
1554         Gtk::Window* w = processor_box.get_processor_ui (processor);
1555
1556         if (w) {
1557                 w->hide ();
1558         }
1559 }
1560
1561 void
1562 MixerStrip::reset_strip_style ()
1563 {
1564         if (_current_delivery && boost::dynamic_pointer_cast<Send>(_current_delivery)) {
1565
1566                 gpm.set_fader_name ("SendStripBase");
1567
1568         } else {
1569
1570                 if (is_midi_track()) {
1571                         if (_route->active()) {
1572                                 set_name ("MidiTrackStripBase");
1573                                 gpm.set_meter_strip_name ("MidiTrackMetrics");
1574                         } else {
1575                                 set_name ("MidiTrackStripBaseInactive");
1576                                 gpm.set_meter_strip_name ("MidiTrackMetricsInactive");
1577                         }
1578                         gpm.set_fader_name ("MidiTrackFader");
1579                 } else if (is_audio_track()) {
1580                         if (_route->active()) {
1581                                 set_name ("AudioTrackStripBase");
1582                                 gpm.set_meter_strip_name ("AudioTrackMetrics");
1583                         } else {
1584                                 set_name ("AudioTrackStripBaseInactive");
1585                                 gpm.set_meter_strip_name ("AudioTrackMetricsInactive");
1586                         }
1587                         gpm.set_fader_name ("AudioTrackFader");
1588                 } else {
1589                         if (_route->active()) {
1590                                 set_name ("AudioBusStripBase");
1591                                 gpm.set_meter_strip_name ("AudioBusMetrics");
1592                         } else {
1593                                 set_name ("AudioBusStripBaseInactive");
1594                                 gpm.set_meter_strip_name ("AudioBusMetricsInactive");
1595                         }
1596                         gpm.set_fader_name ("AudioBusFader");
1597
1598                         /* (no MIDI busses yet) */
1599                 }
1600         }
1601 }
1602
1603 RouteGroup*
1604 MixerStrip::route_group() const
1605 {
1606         return _route->route_group();
1607 }
1608
1609 void
1610 MixerStrip::engine_stopped ()
1611 {
1612 }
1613
1614 void
1615 MixerStrip::engine_running ()
1616 {
1617 }
1618
1619 string
1620 MixerStrip::meter_point_string (MeterPoint mp)
1621 {
1622         switch (mp) {
1623         case MeterInput:
1624                 return _("in");
1625                 break;
1626
1627         case MeterPreFader:
1628                 return _("pre");
1629                 break;
1630
1631         case MeterPostFader:
1632                 return _("post");
1633                 break;
1634
1635         case MeterOutput:
1636                 return _("out");
1637                 break;
1638
1639         case MeterCustom:
1640         default:
1641                 return _("custom");
1642                 break;
1643         }
1644 }
1645
1646 /** Called when the metering point has changed */
1647 void
1648 MixerStrip::meter_changed ()
1649 {
1650         meter_point_label.set_text (meter_point_string (_route->meter_point()));
1651         gpm.setup_meters ();
1652         // reset peak when meter point changes
1653         gpm.reset_peak_display();
1654 }
1655
1656 void
1657 MixerStrip::switch_io (boost::shared_ptr<Route> target)
1658 {
1659         /* don't respond to switch IO signal outside of the mixer window */
1660
1661         if (!_mixer_owned) {
1662                 return;
1663         }
1664
1665         if (_route == target || _route->is_master()) {
1666                 /* don't change the display for the target or the master bus */
1667                 return;
1668         } else if (!is_track() && show_sends_button) {
1669                 /* make sure our show sends button is inactive, and we no longer blink,
1670                    since we're not the target.
1671                 */
1672                 send_blink_connection.disconnect ();
1673                 show_sends_button->unset_active_state ();
1674         }
1675
1676         if (!target) {
1677                 /* switch back to default */
1678                 revert_to_default_display ();
1679                 return;
1680         }
1681
1682         boost::shared_ptr<Send> send = _route->internal_send_for (target);
1683
1684         if (send) {
1685                 show_send (send);
1686         } else {
1687                 revert_to_default_display ();
1688         }
1689 }
1690
1691 void
1692 MixerStrip::drop_send ()
1693 {
1694         boost::shared_ptr<Send> current_send;
1695
1696         if (_current_delivery && (current_send = boost::dynamic_pointer_cast<Send>(_current_delivery))) {
1697                 current_send->set_metering (false);
1698         }
1699
1700         send_gone_connection.disconnect ();
1701         input_button.set_sensitive (true);
1702         output_button.set_sensitive (true);
1703         group_button.set_sensitive (true);
1704         set_invert_sensitive (true);
1705         meter_point_button.set_sensitive (true);
1706         mute_button->set_sensitive (true);
1707         solo_button->set_sensitive (true);
1708         rec_enable_button->set_sensitive (true);
1709         solo_isolated_led->set_sensitive (true);
1710         solo_safe_led->set_sensitive (true);
1711         monitor_input_button->set_sensitive (true);
1712         monitor_disk_button->set_sensitive (true);
1713         _comment_button.set_sensitive (true);
1714 }
1715
1716 void
1717 MixerStrip::set_current_delivery (boost::shared_ptr<Delivery> d)
1718 {
1719         _current_delivery = d;
1720         DeliveryChanged (_current_delivery);
1721 }
1722
1723 void
1724 MixerStrip::show_send (boost::shared_ptr<Send> send)
1725 {
1726         assert (send != 0);
1727
1728         drop_send ();
1729
1730         set_current_delivery (send);
1731
1732         send->set_metering (true);
1733         _current_delivery->DropReferences.connect (send_gone_connection, invalidator (*this), boost::bind (&MixerStrip::revert_to_default_display, this), gui_context());
1734
1735         gain_meter().set_controls (_route, send->meter(), send->amp());
1736         gain_meter().setup_meters ();
1737
1738         panner_ui().set_panner (_current_delivery->panner_shell(), _current_delivery->panner());
1739         panner_ui().setup_pan ();
1740
1741         input_button.set_sensitive (false);
1742         group_button.set_sensitive (false);
1743         set_invert_sensitive (false);
1744         meter_point_button.set_sensitive (false);
1745         mute_button->set_sensitive (false);
1746         solo_button->set_sensitive (false);
1747         rec_enable_button->set_sensitive (false);
1748         solo_isolated_led->set_sensitive (false);
1749         solo_safe_led->set_sensitive (false);
1750         monitor_input_button->set_sensitive (false);
1751         monitor_disk_button->set_sensitive (false);
1752         _comment_button.set_sensitive (false);
1753
1754         if (boost::dynamic_pointer_cast<InternalSend>(send)) {
1755                 output_button.set_sensitive (false);
1756         }
1757
1758         reset_strip_style ();
1759 }
1760
1761 void
1762 MixerStrip::revert_to_default_display ()
1763 {
1764         if (show_sends_button) {
1765                 show_sends_button->unset_active_state ();
1766         }
1767
1768         drop_send ();
1769
1770         set_current_delivery (_route->main_outs ());
1771
1772         gain_meter().set_controls (_route, _route->shared_peak_meter(), _route->amp());
1773         gain_meter().setup_meters ();
1774
1775         panner_ui().set_panner (_route->main_outs()->panner_shell(), _route->main_outs()->panner());
1776         panner_ui().setup_pan ();
1777
1778         reset_strip_style ();
1779 }
1780
1781 void
1782 MixerStrip::set_button_names ()
1783 {
1784         switch (_width) {
1785         case Wide:
1786                 rec_enable_button_label.set_text (_("Rec"));
1787                 mute_button->set_text (_("Mute"));
1788                 monitor_input_button->set_text (_("In"));
1789                 monitor_disk_button->set_text (_("Disk"));
1790
1791                 if (_route && _route->solo_safe()) {
1792                         if (solo_safe_pixbuf == 0) {
1793                                 solo_safe_pixbuf = ::get_icon("solo-safe-icon");
1794                         }
1795                         solo_button->set_image (solo_safe_pixbuf);
1796                         solo_button->set_text (string());
1797                 } else {
1798                         solo_button->set_image (Glib::RefPtr<Gdk::Pixbuf>());
1799                         if (!Config->get_solo_control_is_listen_control()) {
1800                                 solo_button->set_text (_("Solo"));
1801                         } else {
1802                                 switch (Config->get_listen_position()) {
1803                                 case AfterFaderListen:
1804                                         solo_button->set_text (_("AFL"));
1805                                         break;
1806                                 case PreFaderListen:
1807                                         solo_button->set_text (_("PFL"));
1808                                         break;
1809                                 }
1810                         }
1811                 }
1812                 break;
1813
1814         default:
1815                 rec_enable_button_label.set_text (_("R"));
1816                 mute_button->set_text (_("M"));
1817                 monitor_input_button->set_text (_("I"));
1818                 monitor_disk_button->set_text (_("D"));
1819                 if (_route && _route->solo_safe()) {
1820                         solo_button->remove ();
1821                         if (solo_safe_pixbuf == 0) {
1822                                 solo_safe_pixbuf =::get_icon("solo-safe-icon");
1823                         }
1824                         solo_button->set_image (solo_safe_pixbuf);
1825                         solo_button->set_text (string());
1826                 } else {
1827                         solo_button->set_image (Glib::RefPtr<Gdk::Pixbuf>());
1828                         if (!Config->get_solo_control_is_listen_control()) {
1829                                 solo_button->set_text (_("S"));
1830                         } else {
1831                                 switch (Config->get_listen_position()) {
1832                                 case AfterFaderListen:
1833                                         solo_button->set_text (_("A"));
1834                                         break;
1835                                 case PreFaderListen:
1836                                         solo_button->set_text (_("P"));
1837                                         break;
1838                                 }
1839                         }
1840                 }
1841                 break;
1842
1843         }
1844 }
1845
1846 bool
1847 MixerStrip::on_key_press_event (GdkEventKey* ev)
1848 {
1849         GdkEventButton fake;
1850         fake.type = GDK_BUTTON_PRESS;
1851         fake.button = 1;
1852         fake.state = ev->state;
1853
1854         switch (ev->keyval) {
1855         case GDK_m:
1856                 mute_press (&fake);
1857                 return true;
1858                 break;
1859
1860         case GDK_s:
1861                 solo_press (&fake);
1862                 return true;
1863                 break;
1864
1865         case GDK_r:
1866                 rec_enable_press (&fake);
1867                 return true;
1868                 break;
1869
1870         case GDK_e:
1871                 show_sends_press (&fake);
1872                 return true;
1873                 break;
1874
1875         case GDK_g:
1876                 if (ev->state & Keyboard::PrimaryModifier) {
1877                         step_gain_down ();
1878                 } else {
1879                         step_gain_up ();
1880                 }
1881                 return true;
1882                 break;
1883
1884         case GDK_0:
1885                 if (_route) {
1886                         _route->set_gain (1.0, this);
1887                 }
1888                 return true;
1889
1890         default:
1891                 break;
1892         }
1893
1894         return false;
1895 }
1896
1897
1898 bool
1899 MixerStrip::on_key_release_event (GdkEventKey* ev)
1900 {
1901         GdkEventButton fake;
1902         fake.type = GDK_BUTTON_RELEASE;
1903         fake.button = 1;
1904         fake.state = ev->state;
1905
1906         switch (ev->keyval) {
1907         case GDK_m:
1908                 mute_release (&fake);
1909                 return true;
1910                 break;
1911
1912         case GDK_s:
1913                 solo_release (&fake);
1914                 return true;
1915                 break;
1916
1917         case GDK_r:
1918                 rec_enable_release (&fake);
1919                 return true;
1920                 break;
1921
1922         case GDK_e:
1923                 show_sends_release (&fake);
1924                 return true;
1925                 break;
1926
1927         case GDK_g:
1928                 return true;
1929                 break;
1930
1931         default:
1932                 break;
1933         }
1934
1935         return false;
1936 }
1937
1938 bool
1939 MixerStrip::on_enter_notify_event (GdkEventCrossing*)
1940 {
1941         Keyboard::magic_widget_grab_focus ();
1942         return false;
1943 }
1944
1945 bool
1946 MixerStrip::on_leave_notify_event (GdkEventCrossing* ev)
1947 {
1948         switch (ev->detail) {
1949         case GDK_NOTIFY_INFERIOR:
1950                 break;
1951         default:
1952                 Keyboard::magic_widget_drop_focus ();
1953         }
1954
1955         return false;
1956 }
1957
1958 PluginSelector*
1959 MixerStrip::plugin_selector()
1960 {
1961         return _mixer.plugin_selector();
1962 }
1963
1964 void
1965 MixerStrip::hide_things ()
1966 {
1967         processor_box.hide_things ();
1968 }
1969
1970 bool
1971 MixerStrip::input_active_button_press (GdkEventButton*)
1972 {
1973         /* nothing happens on press */
1974         return true;
1975 }
1976
1977 bool
1978 MixerStrip::input_active_button_release (GdkEventButton* ev)
1979 {
1980         boost::shared_ptr<MidiTrack> mt = midi_track ();
1981
1982         if (!mt) {
1983                 return true;
1984         }
1985
1986         if (mt->input_active()) {
1987                 if (Keyboard::modifier_state_contains (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::SecondaryModifier))) {
1988                         /* turn all other tracks using this input off */
1989                         _session->set_exclusive_input_active (mt, false);
1990                 } else {
1991                         mt->set_input_active (false);
1992                 }
1993
1994         } else {
1995                 if (Keyboard::modifier_state_contains (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::SecondaryModifier))) {
1996                         /* turn all other tracks using this input on */
1997                         _session->set_exclusive_input_active (mt, true);
1998                 } else {
1999                         mt->set_input_active (true);
2000                 }
2001         }
2002
2003         return true;
2004 }
2005
2006 void
2007 MixerStrip::midi_input_status_changed ()
2008 {
2009         if (midi_input_enable_button) {
2010                 boost::shared_ptr<MidiTrack> mt = midi_track ();
2011                 assert (mt);
2012                 midi_input_enable_button->set_active (mt->input_active ());
2013         }
2014 }
2015
2016 string
2017 MixerStrip::state_id () const
2018 {
2019         return string_compose ("strip %1", _route->id().to_s());
2020 }
2021
2022 void
2023 MixerStrip::parameter_changed (string p)
2024 {
2025         if (p == _visibility.get_state_name()) {
2026                 /* The user has made changes to the mixer strip visibility, so get
2027                    our VisibilityGroup to reflect these changes in our widgets.
2028                 */
2029                 _visibility.set_state (Config->get_mixer_strip_visibility ());
2030         }
2031 }