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