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