Re-fix #3685 having read the request properly.
[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         , _mono_button (_("Mono"))
113         , button_table (3, 2)
114         , middle_button_table (1, 2)
115         , bottom_button_table (1, 2)
116         , meter_point_label (_("pre"))
117         , comment_button (_("Comments"))
118 {
119         init ();
120         set_route (rt);
121 }
122
123 void
124 MixerStrip::init ()
125 {
126         input_selector = 0;
127         output_selector = 0;
128         group_menu = 0;
129         _marked_for_display = false;
130         route_ops_menu = 0;
131         ignore_comment_edit = false;
132         ignore_toggle = false;
133         comment_window = 0;
134         comment_area = 0;
135         _width_owner = 0;
136         spacer = 0;
137
138         /* the length of this string determines the width of the mixer strip when it is set to `wide' */
139         longest_label = "longest label";
140
141         Gtk::Image* img;
142
143         img = manage (new Gtk::Image (::get_icon("strip_width")));
144         img->show ();
145
146         width_button.add (*img);
147
148         img = manage (new Gtk::Image (::get_icon("hide")));
149         img->show ();
150
151         hide_button.add (*img);
152
153         input_label.set_text (_("Input"));
154         ARDOUR_UI::instance()->set_tip (&input_button, _("Button 1 to choose inputs from a port matrix, button 3 to select inputs from a menu"), "");
155         input_button.add (input_label);
156         input_button.set_name ("MixerIOButton");
157         input_label.set_name ("MixerIOButtonLabel");
158
159         Gtkmm2ext::set_size_request_to_display_given_text (input_button, longest_label.c_str(), 4, 4);
160
161         output_label.set_text (_("Output"));
162         ARDOUR_UI::instance()->set_tip (&output_button, _("Button 1 to choose outputs from a port matrix, button 3 to select inputs from a menu"), "");
163         output_button.add (output_label);
164         output_button.set_name ("MixerIOButton");
165         output_label.set_name ("MixerIOButtonLabel");
166         Gtkmm2ext::set_size_request_to_display_given_text (output_button, longest_label.c_str(), 4, 4);
167
168         ARDOUR_UI::instance()->set_tip (&meter_point_button, _("Select metering point"), "");
169         meter_point_button.add (meter_point_label);
170         meter_point_button.set_name ("MixerStripMeterPreButton");
171         meter_point_label.set_name ("MixerStripMeterPreButton");
172
173         /* TRANSLATORS: this string should be longest of the strings
174            used to describe meter points. In english, it's "input".
175         */
176         set_size_request_to_display_given_text (meter_point_button, _("tupni"), 5, 5);
177
178         bottom_button_table.attach (meter_point_button, 1, 2, 0, 1);
179
180         meter_point_button.signal_button_press_event().connect (sigc::mem_fun (gpm, &GainMeter::meter_press), false);
181         meter_point_button.signal_button_release_event().connect (sigc::mem_fun (gpm, &GainMeter::meter_release), false);
182
183         hide_button.set_events (hide_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
184
185         mute_button->set_name ("MixerMuteButton");
186         solo_button->set_name ("MixerSoloButton");
187
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_box, 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 (panners, Gtk::PACK_SHRINK);
281         global_vpacker.pack_start (solo_led_box,Gtk::PACK_SHRINK);
282         global_vpacker.pack_start (middle_button_table,Gtk::PACK_SHRINK);
283         global_vpacker.pack_start (gain_meter_alignment,Gtk::PACK_SHRINK);
284         global_vpacker.pack_start (bottom_button_table,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         AudioEngine::instance()->PortConnectedOrDisconnected.connect (
346                 *this, invalidator (*this), boost::bind (&MixerStrip::port_connected_or_disconnected, this, _1, _2), gui_context ()
347                 );
348 }
349
350 MixerStrip::~MixerStrip ()
351 {
352         CatchDeletion (this);
353
354         delete input_selector;
355         delete output_selector;
356         delete comment_window;
357 }
358
359 void
360 MixerStrip::set_route (boost::shared_ptr<Route> rt)
361 {
362         if (rec_enable_button->get_parent()) {
363                 button_table.remove (*rec_enable_button);
364         }
365
366         if (show_sends_button->get_parent()) {
367                 button_table.remove (*show_sends_button);
368         }
369
370         processor_box.set_route (rt);
371
372         RouteUI::set_route (rt);
373
374         /* map the current state */
375
376         mute_changed (0);
377         update_solo_display ();
378
379         delete input_selector;
380         input_selector = 0;
381
382         delete output_selector;
383         output_selector = 0;
384
385         revert_to_default_display ();
386
387         if (set_color_from_route()) {
388                 set_color (unique_random_color());
389         }
390
391         if (route()->is_master()) {
392                 solo_button->hide ();
393                 solo_led_box.hide ();
394         } else {
395                 solo_button->show ();
396                 solo_led_box.show ();
397         }
398
399         if (_mixer_owned && (route()->is_master() || route()->is_monitor())) {
400
401                 if (scrollbar_height == 0) {
402                         HScrollbar scrollbar;
403                         Gtk::Requisition requisition(scrollbar.size_request ());
404                         scrollbar_height = requisition.height;
405                 }
406
407                 spacer = manage (new EventBox);
408                 spacer->set_size_request (-1, scrollbar_height);
409                 global_vpacker.pack_start (*spacer, false, false);
410         }
411
412         if (is_audio_track()) {
413                 boost::shared_ptr<AudioTrack> at = audio_track();
414                 at->FreezeChange.connect (route_connections, invalidator (*this), boost::bind (&MixerStrip::map_frozen, this), gui_context());
415         }
416
417         if (has_audio_outputs ()) {
418                 _mono_button.show ();
419                 panners.show_all ();
420         } else {
421                 _mono_button.hide ();
422                 panners.hide_all ();
423         }
424
425         if (is_track ()) {
426                 
427                 button_table.attach (*rec_enable_button, 0, 2, 2, 3);
428                 rec_enable_button->set_sensitive (_session->writable());
429                 rec_enable_button->show();
430
431         } else {
432                 
433                 /* non-master bus */
434
435                 if (!_route->is_master()) {
436                         button_table.attach (*show_sends_button, 0, 2, 2, 3);
437                         show_sends_button->show();
438                 }
439         }
440
441         switch (_route->meter_point()) {
442         case MeterInput:
443                 meter_point_label.set_text (_("input"));
444                 break;
445
446         case MeterPreFader:
447                 meter_point_label.set_text (_("pre"));
448                 break;
449
450         case MeterPostFader:
451                 meter_point_label.set_text (_("post"));
452                 break;
453
454         case MeterCustom:
455                 meter_point_label.set_text (_("custom"));
456                 break;
457         }
458
459         delete route_ops_menu;
460         route_ops_menu = 0;
461
462         ARDOUR_UI::instance()->set_tip (comment_button, _route->comment().empty() ?
463                                                    _("Click to Add/Edit Comments"):
464                                                    _route->comment());
465
466         _route->meter_change.connect (route_connections, invalidator (*this), bind (&MixerStrip::meter_changed, this), 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         if (_session->actively_recording() && _route->record_enabled()) 
790                 return true;
791
792         switch (ev->button) {
793
794         case 1:
795                 edit_input_configuration ();
796                 break;
797
798         case 3:
799         {
800                 citems.push_back (MenuElem (_("Disconnect"), sigc::mem_fun (*(static_cast<RouteUI*>(this)), &RouteUI::disconnect_input)));
801                 citems.push_back (SeparatorElem());
802                 input_menu_bundles.clear ();
803
804                 ARDOUR::BundleList current = _route->input()->bundles_connected ();
805
806                 boost::shared_ptr<ARDOUR::BundleList> b = _session->bundles ();
807
808                 /* give user bundles first chance at being in the menu */
809                 
810                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
811                         if (boost::dynamic_pointer_cast<UserBundle> (*i)) {
812                                 maybe_add_bundle_to_input_menu (*i, current);
813                         }
814                 }
815
816                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
817                         if (boost::dynamic_pointer_cast<UserBundle> (*i) == 0) {
818                                 maybe_add_bundle_to_input_menu (*i, current);
819                         }
820                 }
821                 
822                 boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
823                 for (ARDOUR::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
824                         maybe_add_bundle_to_input_menu ((*i)->output()->bundle(), current);
825                 }
826
827                 if (citems.size() == 2) {
828                         /* no routes added; remove the separator */
829                         citems.pop_back ();
830                 }
831
832                 input_menu.popup (1, ev->time);
833                 break;
834         }
835         default:
836                 break;
837         }
838         return TRUE;
839 }
840
841 void
842 MixerStrip::bundle_input_toggled (boost::shared_ptr<ARDOUR::Bundle> c)
843 {
844         if (ignore_toggle) {
845                 return;
846         }
847
848         ARDOUR::BundleList current = _route->input()->bundles_connected ();
849
850         if (std::find (current.begin(), current.end(), c) == current.end()) {
851                 _route->input()->connect_ports_to_bundle (c, this);
852         } else {
853                 _route->input()->disconnect_ports_from_bundle (c, this);
854         }
855 }
856
857 void
858 MixerStrip::bundle_output_toggled (boost::shared_ptr<ARDOUR::Bundle> c)
859 {
860         if (ignore_toggle) {
861                 return;
862         }
863
864         ARDOUR::BundleList current = _route->output()->bundles_connected ();
865
866         if (std::find (current.begin(), current.end(), c) == current.end()) {
867                 _route->output()->connect_ports_to_bundle (c, this);
868         } else {
869                 _route->output()->disconnect_ports_from_bundle (c, this);
870         }
871 }
872
873 void
874 MixerStrip::maybe_add_bundle_to_input_menu (boost::shared_ptr<Bundle> b, ARDOUR::BundleList const & current)
875 {
876         using namespace Menu_Helpers;
877
878         if (b->ports_are_outputs() == false || b->nchannels() != _route->n_inputs()) {
879                 return;
880         }
881
882         list<boost::shared_ptr<Bundle> >::iterator i = input_menu_bundles.begin ();
883         while (i != input_menu_bundles.end() && b->has_same_ports (*i) == false) {
884                 ++i;
885         }
886
887         if (i != input_menu_bundles.end()) {
888                 return;
889         }
890
891         input_menu_bundles.push_back (b);
892
893         MenuList& citems = input_menu.items();
894
895         std::string n = b->name ();
896         replace_all (n, "_", " ");
897
898         citems.push_back (CheckMenuElem (n, sigc::bind (sigc::mem_fun(*this, &MixerStrip::bundle_input_toggled), b)));
899
900         if (std::find (current.begin(), current.end(), b) != current.end()) {
901                 ignore_toggle = true;
902                 dynamic_cast<CheckMenuItem *> (&citems.back())->set_active (true);
903                 ignore_toggle = false;
904         }
905 }
906
907 void
908 MixerStrip::maybe_add_bundle_to_output_menu (boost::shared_ptr<Bundle> b, ARDOUR::BundleList const & current)
909 {
910         using namespace Menu_Helpers;
911
912         if (b->ports_are_inputs() == false || b->nchannels() != _route->n_outputs()) {
913                 return;
914         }
915
916         list<boost::shared_ptr<Bundle> >::iterator i = output_menu_bundles.begin ();
917         while (i != output_menu_bundles.end() && b->has_same_ports (*i) == false) {
918                 ++i;
919         }
920
921         if (i != output_menu_bundles.end()) {
922                 return;
923         }
924
925         output_menu_bundles.push_back (b);
926         
927         MenuList& citems = output_menu.items();
928
929         std::string n = b->name ();
930         replace_all (n, "_", " ");
931
932         citems.push_back (CheckMenuElem (n, sigc::bind (sigc::mem_fun(*this, &MixerStrip::bundle_output_toggled), b)));
933
934         if (std::find (current.begin(), current.end(), b) != current.end()) {
935                 ignore_toggle = true;
936                 dynamic_cast<CheckMenuItem *> (&citems.back())->set_active (true);
937                 ignore_toggle = false;
938         }
939 }
940
941 void
942 MixerStrip::update_diskstream_display ()
943 {
944         if (is_track()) {
945
946                 if (input_selector) {
947                         input_selector->hide_all ();
948                 }
949
950                 show_route_color ();
951
952         } else {
953
954                 show_passthru_color ();
955         }
956 }
957
958 void
959 MixerStrip::connect_to_pan ()
960 {
961         ENSURE_GUI_THREAD (*this, &MixerStrip::connect_to_pan)
962
963         panstate_connection.disconnect ();
964         panstyle_connection.disconnect ();
965
966         if (!_route->panner()) {
967                 return;
968         }
969
970         boost::shared_ptr<ARDOUR::AutomationControl> pan_control
971                 = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(
972                                 _route->panner()->control(Evoral::Parameter(PanAutomation)));
973
974         if (pan_control) {
975                 pan_control->alist()->automation_state_changed.connect (panstate_connection, invalidator (*this), boost::bind (&PannerUI::pan_automation_state_changed, &panners), gui_context());
976                 pan_control->alist()->automation_style_changed.connect (panstyle_connection, invalidator (*this), boost::bind (&PannerUI::pan_automation_style_changed, &panners), gui_context());
977         }
978
979         panners.panner_changed (this);
980 }
981
982
983 /*
984  * Output port labelling
985  * =====================
986  *
987  * Case 1: Each output has one connection, all connections are to system:playback_%i
988  *   out 1 -> system:playback_1
989  *   out 2 -> system:playback_2
990  *   out 3 -> system:playback_3
991  *   Display as: 1/2/3
992  *
993  * Case 2: Each output has one connection, all connections are to ardour:track_x/in 1
994  *   out 1 -> ardour:track_x/in 1
995  *   out 2 -> ardour:track_x/in 2
996  *   Display as: track_x
997  *
998  * Case 3: Each output has one connection, all connections are to Jack client "program x"
999  *   out 1 -> program x:foo
1000  *   out 2 -> program x:foo
1001  *   Display as: program x
1002  *
1003  * Case 4: No connections (Disconnected)
1004  *   Display as: -
1005  *
1006  * Default case (unusual routing):
1007  *   Display as: *number of connections*
1008  *
1009  * Tooltips
1010  * ========
1011  * .-----------------------------------------------.
1012  * | Mixdown                                       |
1013  * | out 1 -> ardour:master/in 1, jamin:input/in 1 |
1014  * | out 2 -> ardour:master/in 2, jamin:input/in 2 |
1015  * '-----------------------------------------------'
1016  * .-----------------------------------------------.
1017  * | Guitar SM58                                   |
1018  * | Disconnected                                  |
1019  * '-----------------------------------------------'
1020  */
1021
1022 void
1023 MixerStrip::update_io_button (boost::shared_ptr<ARDOUR::Route> route, Width width, bool for_input)
1024 {
1025         uint32_t io_count;
1026         uint32_t io_index;
1027         Port *port;
1028         vector<string> port_connections;
1029
1030         uint32_t total_connection_count = 0;
1031         uint32_t io_connection_count = 0;
1032         uint32_t ardour_connection_count = 0;
1033         uint32_t system_connection_count = 0;
1034         uint32_t other_connection_count = 0;
1035
1036         ostringstream label;
1037         string label_string;
1038
1039         bool have_label = false;
1040         bool each_io_has_one_connection = true;
1041
1042         string connection_name;
1043         string ardour_track_name;
1044         string other_connection_type;
1045         string system_ports;
1046         string system_port;
1047
1048         ostringstream tooltip;
1049         char * tooltip_cstr;
1050
1051         tooltip << route->name();
1052
1053         if (for_input) {
1054                 io_count = route->n_inputs().n_total();
1055         } else {
1056                 io_count = route->n_outputs().n_total();
1057         }
1058
1059         for (io_index = 0; io_index < io_count; ++io_index) {
1060                 if (for_input) {
1061                         port = route->input()->nth (io_index);
1062                 } else {
1063                         port = route->output()->nth (io_index);
1064                 }
1065                 
1066                 port_connections.clear ();
1067                 port->get_connections(port_connections);
1068                 io_connection_count = 0;
1069
1070                 if (!port_connections.empty()) {
1071                         for (vector<string>::iterator i = port_connections.begin(); i != port_connections.end(); ++i) {
1072                                 string& connection_name (*i);
1073
1074                                 if (io_connection_count == 0) {
1075                                         tooltip << endl << port->name().substr(port->name().find("/") + 1) << " -> " << connection_name;
1076                                 } else {
1077                                         tooltip << ", " << connection_name;
1078                                 }
1079
1080                                 if (connection_name.find("ardour:") == 0) {
1081                                         if (ardour_track_name.empty()) {
1082                                                 // "ardour:Master/in 1" -> "ardour:Master/"
1083                                                 string::size_type slash = connection_name.find("/");
1084                                                 if (slash != string::npos) {
1085                                                         ardour_track_name = connection_name.substr(0, slash + 1);
1086                                                 }
1087                                         }
1088
1089                                         if (connection_name.find(ardour_track_name) == 0) {
1090                                                 ++ardour_connection_count;
1091                                         }
1092                                 } else if (connection_name.find("system:") == 0) {
1093                                         if (for_input) {
1094                                                 // "system:capture_123" -> "123"
1095                                                 system_port = connection_name.substr(15);
1096                                         } else {
1097                                                 // "system:playback_123" -> "123"
1098                                                 system_port = connection_name.substr(16);
1099                                         }
1100
1101                                         if (system_ports.empty()) {
1102                                                 system_ports += system_port;
1103                                         } else {
1104                                                 system_ports += "/" + system_port;
1105                                         }
1106
1107                                         ++system_connection_count;
1108                                 } else {
1109                                         if (other_connection_type.empty()) {
1110                                                 // "jamin:in 1" -> "jamin:"
1111                                                 other_connection_type = connection_name.substr(0, connection_name.find(":") + 1);
1112                                         }
1113
1114                                         if (connection_name.find(other_connection_type) == 0) {
1115                                                 ++other_connection_count;
1116                                         }
1117                                 }
1118
1119                                 ++total_connection_count;
1120                                 ++io_connection_count;
1121                         }
1122                 }
1123
1124                 if (io_connection_count != 1) {
1125                         each_io_has_one_connection = false;
1126                 }
1127         }
1128
1129         if (total_connection_count == 0) {
1130                 tooltip << endl << _("Disconnected");
1131         }
1132
1133         tooltip_cstr = new char[tooltip.str().size() + 1];
1134         strcpy(tooltip_cstr, tooltip.str().c_str());
1135
1136         if (for_input) {
1137                 ARDOUR_UI::instance()->set_tip (&input_button, tooltip_cstr, "");
1138         } else {
1139                 ARDOUR_UI::instance()->set_tip (&output_button, tooltip_cstr, "");
1140         }
1141
1142         if (each_io_has_one_connection) {
1143                 if ((total_connection_count == ardour_connection_count)) {
1144                         // all connections are to the same track in ardour
1145                         // "ardour:Master/" -> "Master"
1146                         string::size_type slash = ardour_track_name.find("/");
1147                         if (slash != string::npos) {
1148                                 label << ardour_track_name.substr(7, slash - 7);
1149                                 have_label = true;
1150                         }
1151                 }
1152                 else if (total_connection_count == system_connection_count) {
1153                         // all connections are to system ports
1154                         label << system_ports;
1155                         have_label = true;
1156                 }
1157                 else if (total_connection_count == other_connection_count) {
1158                         // all connections are to the same external program eg jamin
1159                         // "jamin:" -> "jamin"
1160                         label << other_connection_type.substr(0, other_connection_type.size() - 1);
1161                         have_label = true;
1162                 }
1163         }
1164
1165         if (!have_label) {
1166                 if (total_connection_count == 0) {
1167                         // Disconnected
1168                         label << "-";
1169                 } else {
1170                         // Odd configuration
1171                         label << "*" << total_connection_count << "*";
1172                 }
1173         }
1174
1175         switch (width) {
1176         case Wide:
1177                 label_string = label.str().substr(0, 6);
1178                 break;
1179         case Narrow:
1180                 label_string = label.str().substr(0, 3);
1181                 break;
1182         }
1183
1184         if (for_input) {
1185                 input_label.set_text (label_string);
1186         } else {
1187                 output_label.set_text (label_string);
1188         }
1189 }
1190
1191 void
1192 MixerStrip::update_input_display ()
1193 {
1194         update_io_button (_route, _width, true);
1195         panners.setup_pan ();
1196 }
1197
1198 void
1199 MixerStrip::update_output_display ()
1200 {
1201         update_io_button (_route, _width, false);
1202         gpm.setup_meters ();
1203         panners.setup_pan ();
1204 }
1205
1206 void
1207 MixerStrip::fast_update ()
1208 {
1209         gpm.update_meters ();
1210 }
1211
1212 void
1213 MixerStrip::diskstream_changed ()
1214 {
1215         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&MixerStrip::update_diskstream_display, this));
1216 }
1217
1218 void
1219 MixerStrip::port_connected_or_disconnected (Port* a, Port* b)
1220 {
1221         if (_route->input()->has_port (a) || _route->input()->has_port (b)) {
1222                 update_input_display ();
1223                 set_width_enum (_width, this);
1224         }
1225
1226         if (_route->output()->has_port (a) || _route->output()->has_port (b)) {
1227                 update_output_display ();
1228                 set_width_enum (_width, this);
1229         }
1230 }
1231
1232 void
1233 MixerStrip::comment_editor_done_editing()
1234 {
1235         string str =  comment_area->get_buffer()->get_text();
1236         if (_route->comment() != str) {
1237                 _route->set_comment (str, this);
1238
1239                 switch (_width) {
1240
1241                 case Wide:
1242                         if (! str.empty()) {
1243                                 comment_button.modify_bg (STATE_NORMAL, color());
1244                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("*Comments*"));
1245                         } else {
1246                                 comment_button.unset_bg (STATE_NORMAL);
1247                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("Comments"));
1248                         }
1249                         break;
1250
1251                 case Narrow:
1252                         if (! str.empty()) {
1253                                 comment_button.modify_bg (STATE_NORMAL, color());
1254                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("*Cmt*"));
1255                         } else {
1256                                 comment_button.unset_bg (STATE_NORMAL);
1257                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("Cmt"));
1258                         }
1259                         break;
1260                 }
1261
1262                 ARDOUR_UI::instance()->set_tip (comment_button,
1263                                 str.empty() ? _("Click to Add/Edit Comments") : str);
1264         }
1265
1266 }
1267
1268 void
1269 MixerStrip::comment_button_clicked ()
1270 {
1271         if (comment_window == 0) {
1272                 setup_comment_editor ();
1273         }
1274
1275     int x, y, cw_width, cw_height;
1276
1277         if (comment_window->is_visible()) {
1278                 comment_window->hide ();
1279                 return;
1280         }
1281
1282         comment_window->get_size (cw_width, cw_height);
1283         comment_window->get_position(x, y);
1284         comment_window->move(x, y - (cw_height / 2) - 45);
1285         /*
1286            half the dialog height minus the comments button height
1287            with some window decoration fudge thrown in.
1288         */
1289
1290         comment_window->show();
1291         comment_window->present();
1292 }
1293
1294 void
1295 MixerStrip::setup_comment_editor ()
1296 {
1297         string title;
1298         title = _route->name();
1299         title += _(": comment editor");
1300
1301         comment_window = new ArdourDialog (title, false);
1302         comment_window->set_position (Gtk::WIN_POS_MOUSE);
1303         comment_window->set_skip_taskbar_hint (true);
1304         comment_window->signal_hide().connect (sigc::mem_fun(*this, &MixerStrip::comment_editor_done_editing));
1305
1306         comment_area = manage (new TextView());
1307         comment_area->set_name ("MixerTrackCommentArea");
1308         comment_area->set_size_request (110, 178);
1309         comment_area->set_wrap_mode (WRAP_WORD);
1310         comment_area->set_editable (true);
1311         comment_area->get_buffer()->set_text (_route->comment());
1312         comment_area->show ();
1313
1314         comment_window->get_vbox()->pack_start (*comment_area);
1315         comment_window->get_action_area()->hide();
1316 }
1317
1318 void
1319 MixerStrip::comment_changed (void *src)
1320 {
1321         ENSURE_GUI_THREAD (*this, &MixerStrip::comment_changed, src)
1322
1323         if (src != this) {
1324                 ignore_comment_edit = true;
1325                 if (comment_area) {
1326                         comment_area->get_buffer()->set_text (_route->comment());
1327                 }
1328                 ignore_comment_edit = false;
1329         }
1330 }
1331
1332 /** Set the route group for this strip's route, or remove it from its current group.
1333  *  @param rg New RouteGroup, or 0.
1334  */
1335 void
1336 MixerStrip::set_route_group (RouteGroup *rg)
1337 {
1338         if (rg) {
1339                 rg->add (_route);
1340         } else if (_route->route_group ()) {
1341                 _route->route_group()->remove (_route);
1342         }
1343 }
1344
1345 bool
1346 MixerStrip::select_route_group (GdkEventButton *ev)
1347 {
1348         using namespace Menu_Helpers;
1349
1350         if (ev->button == 1) {
1351
1352                 if (group_menu == 0) {
1353
1354                         PropertyList* plist = new PropertyList();
1355
1356                         plist->add (Properties::gain, true);
1357                         plist->add (Properties::mute, true);
1358                         plist->add (Properties::solo, true);
1359
1360                         group_menu = new RouteGroupMenu (_session, plist);
1361                         group_menu->GroupSelected.connect (sigc::mem_fun (*this, &MixerStrip::set_route_group));
1362                 }
1363
1364                 group_menu->popup (1, ev->time);
1365         }
1366
1367         return true;
1368 }
1369
1370 void
1371 MixerStrip::route_group_changed ()
1372 {
1373         ENSURE_GUI_THREAD (*this, &MixerStrip::route_group_changed)
1374
1375         RouteGroup *rg = _route->route_group();
1376
1377         if (rg) {
1378                 group_label.set_text (PBD::short_version (rg->name(), 5));
1379         } else {
1380                 switch (_width) {
1381                 case Wide:
1382                         group_label.set_text (_("Grp"));
1383                         break;
1384                 case Narrow:
1385                         group_label.set_text (_("~G"));
1386                         break;
1387                 }
1388         }
1389 }
1390
1391
1392 void
1393 MixerStrip::route_gui_changed (string what_changed, void*)
1394 {
1395         ENSURE_GUI_THREAD (*this, &MixerStrip::route_gui_changed, what_changed, ignored)
1396
1397         if (what_changed == "color") {
1398                 if (set_color_from_route () == 0) {
1399                         show_route_color ();
1400                 }
1401         }
1402 }
1403
1404 void
1405 MixerStrip::show_route_color ()
1406 {
1407         name_button.modify_bg (STATE_NORMAL, color());
1408         top_event_box.modify_bg (STATE_NORMAL, color());
1409         route_active_changed ();
1410 }
1411
1412 void
1413 MixerStrip::show_passthru_color ()
1414 {
1415         route_active_changed ();
1416 }
1417
1418 void
1419 MixerStrip::build_route_ops_menu ()
1420 {
1421         using namespace Menu_Helpers;
1422         route_ops_menu = new Menu;
1423         route_ops_menu->set_name ("ArdourContextMenu");
1424
1425         MenuList& items = route_ops_menu->items();
1426
1427         items.push_back (MenuElem (_("Save As Template..."), sigc::mem_fun(*this, &RouteUI::save_as_template)));
1428         items.push_back (MenuElem (_("Rename..."), sigc::mem_fun(*this, &RouteUI::route_rename)));
1429         rename_menu_item = &items.back();
1430         items.push_back (SeparatorElem());
1431         items.push_back (CheckMenuElem (_("Active"), sigc::mem_fun (*this, &RouteUI::toggle_route_active)));
1432         route_active_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
1433         route_active_menu_item->set_active (_route->active());
1434
1435         items.push_back (SeparatorElem());
1436
1437         items.push_back (MenuElem (_("Adjust Latency..."), sigc::mem_fun (*this, &RouteUI::adjust_latency)));
1438
1439         items.push_back (SeparatorElem());
1440         items.push_back (CheckMenuElem (_("Protect Against Denormals"), sigc::mem_fun (*this, &RouteUI::toggle_denormal_protection)));
1441         denormal_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
1442         denormal_menu_item->set_active (_route->denormal_protection());
1443
1444         if (!Profile->get_sae()) {
1445                 items.push_back (SeparatorElem());
1446                 items.push_back (MenuElem (_("Remote Control ID..."), sigc::mem_fun (*this, &RouteUI::open_remote_control_id_dialog)));
1447         }
1448
1449         items.push_back (SeparatorElem());
1450         items.push_back (MenuElem (_("Remove"), sigc::mem_fun(*this, &RouteUI::remove_this_route)));
1451 }
1452
1453 gboolean
1454 MixerStrip::name_button_button_press (GdkEventButton* ev)
1455 {
1456         if (ev->button == 3) {
1457                 list_route_operations ();
1458
1459                 /* do not allow rename if the track is record-enabled */
1460                 rename_menu_item->set_sensitive (!_route->record_enabled());
1461                 route_ops_menu->popup (1, ev->time);
1462
1463         } else if (ev->button == 1) {
1464                 revert_to_default_display ();
1465         }
1466
1467         return false;
1468 }
1469
1470 void
1471 MixerStrip::list_route_operations ()
1472 {
1473         if (route_ops_menu == 0) {
1474                 build_route_ops_menu ();
1475         }
1476 }
1477
1478 void
1479 MixerStrip::set_selected (bool yn)
1480 {
1481         AxisView::set_selected (yn);
1482         if (_selected) {
1483                 global_frame.set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
1484                 global_frame.set_name ("MixerStripSelectedFrame");
1485         } else {
1486                 global_frame.set_shadow_type (Gtk::SHADOW_IN);
1487                 global_frame.set_name ("MixerStripFrame");
1488         }
1489         global_frame.queue_draw ();
1490 }
1491
1492 void
1493 MixerStrip::name_changed ()
1494 {
1495         switch (_width) {
1496         case Wide:
1497                 RouteUI::property_changed (PropertyChange (ARDOUR::Properties::name));
1498                 break;
1499         case Narrow:
1500                 name_label.set_text (PBD::short_version (_route->name(), 5));
1501                 break;
1502         }
1503 }
1504
1505 void
1506 MixerStrip::width_clicked ()
1507 {
1508         switch (_width) {
1509         case Wide:
1510                 set_width_enum (Narrow, this);
1511                 break;
1512         case Narrow:
1513                 set_width_enum (Wide, this);
1514                 break;
1515         }
1516 }
1517
1518 void
1519 MixerStrip::hide_clicked ()
1520 {
1521         // LAME fix to reset the button status for when it is redisplayed (part 1)
1522         hide_button.set_sensitive(false);
1523
1524         if (_embedded) {
1525                 Hiding(); /* EMIT_SIGNAL */
1526         } else {
1527                 _mixer.hide_strip (this);
1528         }
1529
1530         // (part 2)
1531         hide_button.set_sensitive(true);
1532 }
1533
1534 void
1535 MixerStrip::set_embedded (bool yn)
1536 {
1537         _embedded = yn;
1538 }
1539
1540 void
1541 MixerStrip::map_frozen ()
1542 {
1543         ENSURE_GUI_THREAD (*this, &MixerStrip::map_frozen)
1544
1545         boost::shared_ptr<AudioTrack> at = audio_track();
1546
1547         if (at) {
1548                 switch (at->freeze_state()) {
1549                 case AudioTrack::Frozen:
1550                         processor_box.set_sensitive (false);
1551                         hide_redirect_editors ();
1552                         break;
1553                 default:
1554                         processor_box.set_sensitive (true);
1555                         // XXX need some way, maybe, to retoggle redirect editors
1556                         break;
1557                 }
1558         }
1559 }
1560
1561 void
1562 MixerStrip::hide_redirect_editors ()
1563 {
1564         _route->foreach_processor (sigc::mem_fun (*this, &MixerStrip::hide_processor_editor));
1565 }
1566
1567 void
1568 MixerStrip::hide_processor_editor (boost::weak_ptr<Processor> p)
1569 {
1570         boost::shared_ptr<Processor> processor (p.lock ());
1571         if (!processor) {
1572                 return;
1573         }
1574
1575         Gtk::Window* w = processor_box.get_processor_ui (processor);
1576
1577         if (w) {
1578                 w->hide ();
1579         }
1580 }
1581
1582 void
1583 MixerStrip::route_active_changed ()
1584 {
1585         RouteUI::route_active_changed ();
1586         reset_strip_style ();
1587 }
1588
1589 void
1590 MixerStrip::reset_strip_style ()
1591 {
1592         if (_current_delivery && boost::dynamic_pointer_cast<Send>(_current_delivery)) {
1593
1594                 gpm.set_fader_name ("SendStripBase");
1595
1596         } else {
1597                 
1598                 if (is_midi_track()) {
1599                         if (_route->active()) {
1600                                 set_name ("MidiTrackStripBase");
1601                                 gpm.set_meter_strip_name ("MidiTrackStripBase");
1602                         } else {
1603                                 set_name ("MidiTrackStripBaseInactive");
1604                                 gpm.set_meter_strip_name ("MidiTrackStripBaseInactive");
1605                         }
1606                         gpm.set_fader_name ("MidiTrackFader");
1607                 } else if (is_audio_track()) {
1608                         if (_route->active()) {
1609                                 set_name ("AudioTrackStripBase");
1610                                 gpm.set_meter_strip_name ("AudioTrackMetrics");
1611                         } else {
1612                                 set_name ("AudioTrackStripBaseInactive");
1613                                 gpm.set_meter_strip_name ("AudioTrackMetricsInactive");
1614                         }
1615                         gpm.set_fader_name ("AudioTrackFader");
1616                 } else {
1617                         if (_route->active()) {
1618                                 set_name ("AudioBusStripBase");
1619                                 gpm.set_meter_strip_name ("AudioBusMetrics");
1620                         } else {
1621                                 set_name ("AudioBusStripBaseInactive");
1622                                 gpm.set_meter_strip_name ("AudioBusMetricsInactive");
1623                         }
1624                         gpm.set_fader_name ("AudioBusFader");
1625                         
1626                         /* (no MIDI busses yet) */
1627                 }
1628         }
1629 }
1630
1631 RouteGroup*
1632 MixerStrip::route_group() const
1633 {
1634         return _route->route_group();
1635 }
1636
1637 void
1638 MixerStrip::engine_stopped ()
1639 {
1640 }
1641
1642 void
1643 MixerStrip::engine_running ()
1644 {
1645 }
1646
1647 /** Called when the metering point has changed */
1648 void
1649 MixerStrip::meter_changed ()
1650 {
1651         ENSURE_GUI_THREAD (*this, &MixerStrip::meter_changed)
1652
1653         switch (_route->meter_point()) {
1654         case MeterInput:
1655                 meter_point_label.set_text (_("input"));
1656                 break;
1657
1658         case MeterPreFader:
1659                 meter_point_label.set_text (_("pre"));
1660                 break;
1661
1662         case MeterPostFader:
1663                 meter_point_label.set_text (_("post"));
1664                 break;
1665
1666         case MeterCustom:
1667                 meter_point_label.set_text (_("custom"));
1668                 break;
1669         }
1670
1671         gpm.setup_meters ();
1672         // reset peak when meter point changes
1673         gpm.reset_peak_display();
1674 }
1675
1676 void
1677 MixerStrip::switch_io (boost::shared_ptr<Route> target)
1678 {
1679         /* don't respond to switch IO signal outside of the mixer window */
1680
1681         if (!_mixer_owned) {
1682                 return;
1683         }
1684
1685         if (_route == target || _route->is_master()) {
1686                 /* don't change the display for the target or the master bus */
1687                 return;
1688         } else if (!is_track() && show_sends_button) {
1689                 /* make sure our show sends button is inactive, and we no longer blink,
1690                    since we're not the target.
1691                 */
1692                 send_blink_connection.disconnect ();
1693                 show_sends_button->set_active (false);
1694                 show_sends_button->set_state (STATE_NORMAL);
1695         }
1696
1697         if (!target) {
1698                 /* switch back to default */
1699                 revert_to_default_display ();
1700                 return;
1701         }
1702
1703         boost::shared_ptr<Send> send = _route->internal_send_for (target);
1704
1705         if (send) {
1706                 show_send (send);
1707         } else {
1708                 revert_to_default_display ();
1709         }
1710 }
1711
1712 void
1713 MixerStrip::drop_send ()
1714 {
1715         boost::shared_ptr<Send> current_send;
1716
1717         if (_current_delivery && (current_send = boost::dynamic_pointer_cast<Send>(_current_delivery))) {
1718                 current_send->set_metering (false);
1719         }
1720
1721         send_gone_connection.disconnect ();
1722         input_button.set_sensitive (true);
1723         output_button.set_sensitive (true);
1724 }
1725
1726 void
1727 MixerStrip::set_current_delivery (boost::shared_ptr<Delivery> d)
1728 {
1729         _current_delivery = d;
1730         DeliveryChanged (_current_delivery);
1731 }
1732
1733 void
1734 MixerStrip::show_send (boost::shared_ptr<Send> send)
1735 {
1736         assert (send != 0);
1737
1738         drop_send ();
1739
1740         set_current_delivery (send);
1741
1742         send->set_metering (true);
1743         _current_delivery->DropReferences.connect (send_gone_connection, invalidator (*this), boost::bind (&MixerStrip::revert_to_default_display, this), gui_context());
1744
1745         gain_meter().set_controls (_route, send->meter(), send->amp());
1746         gain_meter().setup_meters ();
1747
1748         panner_ui().set_panner (_current_delivery->panner());
1749         panner_ui().setup_pan ();
1750
1751         input_button.set_sensitive (false);
1752
1753         if (boost::dynamic_pointer_cast<InternalSend>(send)) {
1754                 output_button.set_sensitive (false);
1755         }
1756
1757         reset_strip_style ();
1758 }
1759
1760 void
1761 MixerStrip::revert_to_default_display ()
1762 {
1763         if (show_sends_button) {
1764                 show_sends_button->set_active (false);
1765         }
1766
1767         drop_send ();
1768
1769         set_current_delivery (_route->main_outs ());
1770
1771         gain_meter().set_controls (_route, _route->shared_peak_meter(), _route->amp());
1772         gain_meter().setup_meters ();
1773
1774         panner_ui().set_panner (_route->main_outs()->panner());
1775         panner_ui().setup_pan ();
1776
1777         reset_strip_style ();
1778 }
1779
1780 void
1781 MixerStrip::set_button_names ()
1782 {
1783         switch (_width) {
1784         case Wide:
1785                 rec_enable_button_label.set_text (_("Rec"));
1786                 mute_button_label.set_text (_("Mute"));
1787                 if (_route && _route->solo_safe()) {
1788                         solo_button_label.set_text (X_("!"));
1789                 } else {
1790                         if (!Config->get_solo_control_is_listen_control()) {
1791                                 solo_button_label.set_text (_("Solo"));
1792                         } else {
1793                                 switch (Config->get_listen_position()) {
1794                                 case AfterFaderListen:
1795                                         solo_button_label.set_text (_("AFL"));
1796                                         break;
1797                                 case PreFaderListen:
1798                                         solo_button_label.set_text (_("PFL"));
1799                                         break;
1800                                 }
1801                         }
1802                 }
1803                 break;
1804
1805         default:
1806                 rec_enable_button_label.set_text (_("R"));
1807                 mute_button_label.set_text (_("M"));
1808                 if (_route && _route->solo_safe()) {
1809                         solo_button_label.set_text (X_("!"));
1810                         if (!Config->get_solo_control_is_listen_control()) {
1811                                 solo_button_label.set_text (_("S"));
1812                         } else {
1813                                 switch (Config->get_listen_position()) {
1814                                 case AfterFaderListen:
1815                                         solo_button_label.set_text (_("A"));
1816                                         break;
1817                                 case PreFaderListen:
1818                                         solo_button_label.set_text (_("P"));
1819                                         break;
1820                                 }
1821                         }
1822                 }
1823                 break;
1824
1825         }
1826 }
1827
1828 bool
1829 MixerStrip::on_key_press_event (GdkEventKey* ev)
1830 {
1831         GdkEventButton fake;
1832         fake.type = GDK_BUTTON_PRESS;
1833         fake.button = 1;
1834         fake.state = ev->state;
1835
1836         switch (ev->keyval) {
1837         case GDK_m:
1838                 mute_press (&fake);
1839                 return true;
1840                 break;
1841
1842         case GDK_s:
1843                 solo_press (&fake);
1844                 return true;
1845                 break;
1846
1847         case GDK_r:
1848                 cerr << "Stole that r\n";
1849                 rec_enable_press (&fake);
1850                 return true;
1851                 break;
1852
1853         case GDK_e:
1854                 show_sends_press (&fake);
1855                 return true;
1856                 break;
1857
1858         case GDK_g:
1859                 if (ev->state & Keyboard::PrimaryModifier) {
1860                         step_gain_down ();
1861                 } else {
1862                         step_gain_up ();
1863                 }
1864                 return true;
1865                 break;
1866
1867         case GDK_0:
1868                 if (_route) {
1869                         _route->set_gain (1.0, this);
1870                 }
1871                 return true;
1872
1873         default:
1874                 break;
1875         }
1876
1877         return false;
1878 }
1879
1880
1881 bool
1882 MixerStrip::on_key_release_event (GdkEventKey* ev)
1883 {
1884         GdkEventButton fake;
1885         fake.type = GDK_BUTTON_RELEASE;
1886         fake.button = 1;
1887         fake.state = ev->state;
1888
1889         switch (ev->keyval) {
1890         case GDK_m:
1891                 mute_release (&fake);
1892                 return true;
1893                 break;
1894
1895         case GDK_s:
1896                 solo_release (&fake);
1897                 return true;
1898                 break;
1899
1900         case GDK_r:
1901                 cerr << "Stole that r\n";
1902                 rec_enable_release (&fake);
1903                 return true;
1904                 break;
1905
1906         case GDK_e:
1907                 show_sends_release (&fake);
1908                 return true;
1909                 break;
1910
1911         case GDK_g:
1912                 return true;
1913                 break;
1914
1915         default:
1916                 break;
1917         }
1918
1919         return false;
1920 }
1921
1922 bool
1923 MixerStrip::on_enter_notify_event (GdkEventCrossing*)
1924 {
1925         Keyboard::magic_widget_grab_focus ();
1926         return false;
1927 }
1928
1929 bool
1930 MixerStrip::on_leave_notify_event (GdkEventCrossing* ev)
1931 {
1932         switch (ev->detail) {
1933         case GDK_NOTIFY_INFERIOR:
1934                 break;
1935         default:
1936                 Keyboard::magic_widget_drop_focus ();
1937         }
1938
1939         return false;
1940 }
1941
1942 void
1943 MixerStrip::mono_button_clicked ()
1944 {
1945         panners.set_mono (_mono_button.get_active ());
1946 }
1947
1948 PluginSelector*
1949 MixerStrip::plugin_selector()
1950 {
1951         return _mixer.plugin_selector();
1952 }