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