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