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