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