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