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