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