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