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