Implement per-channel phase invert. Fixes #3392. Breaks loading of existing 3.0...
[ardour.git] / gtk2_ardour / mixer_strip.cc
1 /*
2     Copyright (C) 2000-2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cmath>
20 #include <algorithm>
21
22 #include <sigc++/bind.h>
23
24 #include "pbd/convert.h"
25 #include "pbd/enumwriter.h"
26 #include "pbd/replace_all.h"
27
28 #include <gtkmm2ext/gtk_ui.h>
29 #include <gtkmm2ext/utils.h>
30 #include <gtkmm2ext/choice.h>
31 #include <gtkmm2ext/doi.h>
32 #include <gtkmm2ext/slider_controller.h>
33 #include <gtkmm2ext/bindable_button.h>
34
35 #include "ardour/ardour.h"
36 #include "ardour/amp.h"
37 #include "ardour/session.h"
38 #include "ardour/audioengine.h"
39 #include "ardour/internal_send.h"
40 #include "ardour/route.h"
41 #include "ardour/route_group.h"
42 #include "ardour/audio_track.h"
43 #include "ardour/panner.h"
44 #include "ardour/send.h"
45 #include "ardour/processor.h"
46 #include "ardour/profile.h"
47 #include "ardour/ladspa_plugin.h"
48 #include "ardour/user_bundle.h"
49 #include "ardour/port.h"
50
51 #include "ardour_ui.h"
52 #include "ardour_dialog.h"
53 #include "mixer_strip.h"
54 #include "mixer_ui.h"
55 #include "keyboard.h"
56 #include "led.h"
57 #include "public_editor.h"
58 #include "send_ui.h"
59 #include "io_selector.h"
60 #include "utils.h"
61 #include "gui_thread.h"
62 #include "route_group_menu.h"
63
64 #include "i18n.h"
65
66 using namespace ARDOUR;
67 using namespace PBD;
68 using namespace Gtk;
69 using namespace Gtkmm2ext;
70 using namespace std;
71
72 sigc::signal<void,boost::shared_ptr<Route> > MixerStrip::SwitchIO;
73
74 int MixerStrip::scrollbar_height = 0;
75 PBD::Signal1<void,MixerStrip*> MixerStrip::CatchDeletion;
76
77 MixerStrip::MixerStrip (Mixer_UI& mx, Session* sess, bool in_mixer)
78         : AxisView(sess)
79         , RouteUI (sess)
80         , _mixer(mx)
81         , _mixer_owned (in_mixer)
82         , processor_box (sess, boost::bind (&MixerStrip::plugin_selector, this), mx.selection(), this, in_mixer)
83         , gpm (sess, 250)
84         , panners (sess)
85         , _mono_button (_("Mono"))
86         , button_table (4, 2)
87         , solo_led_table (2, 2)
88         , middle_button_table (1, 2)
89         , bottom_button_table (1, 2)
90         , meter_point_label (_("pre"))
91         , comment_button (_("Comments"))
92 {
93         init ();
94
95         if (!_mixer_owned) {
96                 /* the editor mixer strip: don't destroy it every time
97                    the underlying route goes away.
98                 */
99
100                 self_destruct = false;
101         }
102 }
103
104 MixerStrip::MixerStrip (Mixer_UI& mx, Session* sess, boost::shared_ptr<Route> rt, bool in_mixer)
105         : AxisView(sess)
106         , RouteUI (sess)
107         , _mixer(mx)
108         , _mixer_owned (in_mixer)
109         , processor_box (sess, sigc::mem_fun(*this, &MixerStrip::plugin_selector), mx.selection(), this, in_mixer)
110         , gpm (sess, 250)
111         , panners (sess)
112         , button_table (3, 2)
113         , middle_button_table (1, 2)
114         , bottom_button_table (1, 2)
115         , meter_point_label (_("pre"))
116         , comment_button (_("Comments"))
117 {
118         init ();
119         set_route (rt);
120 }
121
122 void
123 MixerStrip::init ()
124 {
125         input_selector = 0;
126         output_selector = 0;
127         group_menu = 0;
128         _marked_for_display = false;
129         route_ops_menu = 0;
130         ignore_comment_edit = false;
131         ignore_toggle = false;
132         comment_window = 0;
133         comment_area = 0;
134         _width_owner = 0;
135         spacer = 0;
136
137         /* the length of this string determines the width of the mixer strip when it is set to `wide' */
138         longest_label = "longest label";
139
140         Gtk::Image* img;
141
142         img = manage (new Gtk::Image (::get_icon("strip_width")));
143         img->show ();
144
145         width_button.add (*img);
146
147         img = manage (new Gtk::Image (::get_icon("hide")));
148         img->show ();
149
150         hide_button.add (*img);
151
152         input_label.set_text (_("Input"));
153         ARDOUR_UI::instance()->set_tip (&input_button, _("Button 1 to choose inputs from a port matrix, button 3 to select inputs from a menu"), "");
154         input_button.add (input_label);
155         input_button.set_name ("MixerIOButton");
156         input_label.set_name ("MixerIOButtonLabel");
157
158         Gtkmm2ext::set_size_request_to_display_given_text (input_button, longest_label.c_str(), 4, 4);
159
160         output_label.set_text (_("Output"));
161         ARDOUR_UI::instance()->set_tip (&output_button, _("Button 1 to choose outputs from a port matrix, button 3 to select inputs from a menu"), "");
162         output_button.add (output_label);
163         output_button.set_name ("MixerIOButton");
164         output_label.set_name ("MixerIOButtonLabel");
165         Gtkmm2ext::set_size_request_to_display_given_text (output_button, longest_label.c_str(), 4, 4);
166
167         ARDOUR_UI::instance()->set_tip (&meter_point_button, _("Select metering point"), "");
168         meter_point_button.add (meter_point_label);
169         meter_point_button.set_name ("MixerStripMeterPreButton");
170         meter_point_label.set_name ("MixerStripMeterPreButton");
171
172         /* TRANSLATORS: this string should be longest of the strings
173            used to describe meter points. In english, it's "input".
174         */
175         set_size_request_to_display_given_text (meter_point_button, _("tupni"), 5, 5);
176
177         bottom_button_table.attach (meter_point_button, 1, 2, 0, 1);
178
179         meter_point_button.signal_button_press_event().connect (sigc::mem_fun (gpm, &GainMeter::meter_press), false);
180         meter_point_button.signal_button_release_event().connect (sigc::mem_fun (gpm, &GainMeter::meter_release), false);
181
182         hide_button.set_events (hide_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
183
184         mute_button->set_name ("MixerMuteButton");
185         solo_button->set_name ("MixerSoloButton");
186
187         solo_isolated_led = manage (new LED);
188         solo_isolated_led->show ();
189         solo_isolated_led->set_diameter (6);
190         solo_isolated_led->set_no_show_all (true);
191         solo_isolated_led->set_name (X_("SoloIsolatedLED"));
192         solo_isolated_led->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
193         solo_isolated_led->signal_button_release_event().connect (sigc::mem_fun (*this, &RouteUI::solo_isolate_button_release));
194         UI::instance()->set_tip (solo_isolated_led, _("Isolate Solo"), "");
195
196         solo_safe_led = manage (new LED);
197         solo_safe_led->show ();
198         solo_safe_led->set_diameter (6);
199         solo_safe_led->set_no_show_all (true);
200         solo_safe_led->set_name (X_("SoloSafeLED"));
201         solo_safe_led->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
202         solo_safe_led->signal_button_release_event().connect (sigc::mem_fun (*this, &RouteUI::solo_safe_button_release));
203         UI::instance()->set_tip (solo_safe_led, _("Lock Solo Status"), "");
204
205         Label* iso_label = manage (new Label (_("iso")));
206         Label* safe_label = manage (new Label (_("lock")));
207         
208         iso_label->set_name (X_("SoloLEDLabel"));
209         safe_label->set_name (X_("SoloLEDLabel"));
210
211         iso_label->show ();
212         safe_label->show (); 
213
214         solo_led_table.set_spacings (0);
215         solo_led_table.set_border_width (1);
216         solo_led_table.attach (*iso_label, 0, 1, 0, 1, Gtk::FILL, Gtk::FILL);
217         solo_led_table.attach (*solo_isolated_led, 1, 2, 0, 1, Gtk::FILL, Gtk::FILL);
218         solo_led_table.attach (*safe_label, 0, 1, 1, 2, Gtk::FILL, Gtk::FILL);
219         solo_led_table.attach (*solo_safe_led, 1, 2, 1, 2, Gtk::FILL, Gtk::FILL);
220
221         solo_led_table.show ();
222         solo_led_box.pack_end (solo_led_table, false, false);
223         solo_led_box.show ();
224
225         button_table.set_homogeneous (true);
226         button_table.set_spacings (0);
227
228         button_table.attach (name_button, 0, 2, 0, 1);
229         button_table.attach (input_button, 0, 2, 1, 2);
230         button_table.attach (_invert_button_box, 0, 2, 3, 4);
231
232         middle_button_table.set_homogeneous (true);
233         middle_button_table.set_spacings (0);
234         middle_button_table.attach (*mute_button, 0, 1, 0, 1);
235         middle_button_table.attach (*solo_button, 1, 2, 0, 1);
236
237         bottom_button_table.set_col_spacings (0);
238         bottom_button_table.set_homogeneous (true);
239         bottom_button_table.attach (group_button, 0, 1, 0, 1);
240
241         name_button.add (name_label);
242         name_button.set_name ("MixerNameButton");
243         Gtkmm2ext::set_size_request_to_display_given_text (name_button, longest_label.c_str(), 2, 2);
244
245         name_label.set_name ("MixerNameButtonLabel");
246         ARDOUR_UI::instance()->set_tip (&group_button, _("Mix group"), "");
247         group_button.add (group_label);
248         group_button.set_name ("MixerGroupButton");
249         Gtkmm2ext::set_size_request_to_display_given_text (group_button, "Group", 2, 2);
250         group_label.set_name ("MixerGroupButtonLabel");
251
252         comment_button.set_name ("MixerCommentButton");
253         comment_button.signal_clicked().connect (sigc::mem_fun(*this, &MixerStrip::comment_button_clicked));
254
255         _mono_button.set_name ("MixerMonoButton");
256         _mono_button.signal_clicked().connect (sigc::mem_fun (*this, &MixerStrip::mono_button_clicked));
257
258         global_vpacker.set_border_width (0);
259         global_vpacker.set_spacing (0);
260
261         width_button.set_name ("MixerWidthButton");
262         hide_button.set_name ("MixerHideButton");
263         top_event_box.set_name ("MixerTopEventBox");
264
265         width_button.signal_clicked().connect (sigc::mem_fun(*this, &MixerStrip::width_clicked));
266         hide_button.signal_clicked().connect (sigc::mem_fun(*this, &MixerStrip::hide_clicked));
267
268         width_hide_box.pack_start (width_button, false, true);
269         width_hide_box.pack_start (top_event_box, true, true);
270         width_hide_box.pack_end (hide_button, false, true);
271         gain_meter_alignment.set_padding(0, 4, 0, 0);
272         gain_meter_alignment.add(gpm);
273
274         whvbox.pack_start (width_hide_box, true, true);
275
276         global_vpacker.pack_start (whvbox, Gtk::PACK_SHRINK);
277         global_vpacker.pack_start (button_table,Gtk::PACK_SHRINK);
278         global_vpacker.pack_start (processor_box, true, true);
279         global_vpacker.pack_start (solo_led_box,Gtk::PACK_SHRINK);
280         global_vpacker.pack_start (middle_button_table,Gtk::PACK_SHRINK);
281         global_vpacker.pack_start (gain_meter_alignment,Gtk::PACK_SHRINK);
282         global_vpacker.pack_start (bottom_button_table,Gtk::PACK_SHRINK);
283         global_vpacker.pack_start (panners, Gtk::PACK_SHRINK);
284         global_vpacker.pack_start (_mono_button, Gtk::PACK_SHRINK);
285         global_vpacker.pack_start (output_button, Gtk::PACK_SHRINK);
286         global_vpacker.pack_start (comment_button, Gtk::PACK_SHRINK);
287
288         global_frame.add (global_vpacker);
289         global_frame.set_shadow_type (Gtk::SHADOW_IN);
290         global_frame.set_name ("BaseFrame");
291
292         add (global_frame);
293
294         /* force setting of visible selected status */
295
296         _selected = true;
297         set_selected (false);
298
299         _packed = false;
300         _embedded = false;
301
302         _session->engine().Stopped.connect (*this, invalidator (*this), boost::bind (&MixerStrip::engine_stopped, this), gui_context());
303         _session->engine().Running.connect (*this, invalidator (*this), boost::bind (&MixerStrip::engine_running, this), gui_context());
304
305         input_button.signal_button_press_event().connect (sigc::mem_fun(*this, &MixerStrip::input_press), false);
306         output_button.signal_button_press_event().connect (sigc::mem_fun(*this, &MixerStrip::output_press), false);
307
308         /* we don't need this if its not an audio track, but we don't know that yet and it doesn't
309            hurt (much).
310         */
311
312         rec_enable_button->set_name ("MixerRecordEnableButton");
313
314         /* ditto for this button and busses */
315
316         name_button.signal_button_press_event().connect (sigc::mem_fun(*this, &MixerStrip::name_button_button_press), false);
317         group_button.signal_button_press_event().connect (sigc::mem_fun(*this, &MixerStrip::select_route_group), false);
318
319         _width = (Width) -1;
320
321         /* start off as a passthru strip. we'll correct this, if necessary,
322            in update_diskstream_display().
323         */
324
325         /* start off as a passthru strip. we'll correct this, if necessary,
326            in update_diskstream_display().
327         */
328
329         if (is_midi_track())
330                 set_name ("MidiTrackStripBase");
331         else
332                 set_name ("AudioTrackStripBase");
333
334         add_events (Gdk::BUTTON_RELEASE_MASK|
335                     Gdk::ENTER_NOTIFY_MASK|
336                     Gdk::LEAVE_NOTIFY_MASK|
337                     Gdk::KEY_PRESS_MASK|
338                     Gdk::KEY_RELEASE_MASK);
339
340         set_flags (get_flags() | Gtk::CAN_FOCUS);
341
342         SwitchIO.connect (sigc::mem_fun (*this, &MixerStrip::switch_io));
343 }
344
345 MixerStrip::~MixerStrip ()
346 {
347         CatchDeletion (this);
348
349         delete input_selector;
350         delete output_selector;
351         delete comment_window;
352 }
353
354 void
355 MixerStrip::set_route (boost::shared_ptr<Route> rt)
356 {
357         if (rec_enable_button->get_parent()) {
358                 button_table.remove (*rec_enable_button);
359         }
360
361         if (show_sends_button->get_parent()) {
362                 button_table.remove (*show_sends_button);
363         }
364
365         RouteUI::set_route (rt);
366
367         /* map the current state */
368
369         mute_changed (0);
370         update_solo_display ();
371
372         delete input_selector;
373         input_selector = 0;
374
375         delete output_selector;
376         output_selector = 0;
377
378         revert_to_default_display ();
379
380         processor_box.set_route (rt);
381
382         if (set_color_from_route()) {
383                 set_color (unique_random_color());
384         }
385
386         if (route()->is_master()) {
387                 solo_button->hide ();
388                 solo_led_box.hide ();
389         } else {
390                 solo_button->show ();
391                 solo_led_box.show ();
392         }
393
394         if (_mixer_owned && (route()->is_master() || route()->is_monitor())) {
395
396                 if (scrollbar_height == 0) {
397                         HScrollbar scrollbar;
398                         Gtk::Requisition requisition(scrollbar.size_request ());
399                         scrollbar_height = requisition.height;
400                 }
401
402                 spacer = manage (new EventBox);
403                 spacer->set_size_request (-1, scrollbar_height);
404                 global_vpacker.pack_start (*spacer, false, false);
405         }
406
407         if (is_audio_track()) {
408                 boost::shared_ptr<AudioTrack> at = audio_track();
409                 at->FreezeChange.connect (route_connections, invalidator (*this), boost::bind (&MixerStrip::map_frozen, this), gui_context());
410         }
411
412         if (has_audio_outputs ()) {
413                 _mono_button.show ();
414                 panners.show_all ();
415         } else {
416                 _mono_button.hide ();
417                 panners.hide_all ();
418         }
419
420         if (is_track ()) {
421                 
422                 button_table.attach (*rec_enable_button, 0, 2, 2, 3);
423                 rec_enable_button->set_sensitive (_session->writable());
424                 rec_enable_button->show();
425
426         } else {
427                 
428                 /* non-master bus */
429
430                 if (!_route->is_master()) {
431                         button_table.attach (*show_sends_button, 0, 2, 2, 3);
432                         show_sends_button->show();
433                 }
434         }
435
436         switch (_route->meter_point()) {
437         case MeterInput:
438                 meter_point_label.set_text (_("input"));
439                 break;
440
441         case MeterPreFader:
442                 meter_point_label.set_text (_("pre"));
443                 break;
444
445         case MeterPostFader:
446                 meter_point_label.set_text (_("post"));
447                 break;
448
449         case MeterCustom:
450                 meter_point_label.set_text (_("custom"));
451                 break;
452         }
453
454         delete route_ops_menu;
455         route_ops_menu = 0;
456
457         ARDOUR_UI::instance()->set_tip (comment_button, _route->comment().empty() ?
458                                                    _("Click to Add/Edit Comments"):
459                                                    _route->comment());
460
461         _route->meter_change.connect (route_connections, invalidator (*this), bind (&MixerStrip::meter_changed, this), gui_context());
462         _route->input()->changed.connect (route_connections, invalidator (*this), ui_bind (&MixerStrip::input_changed, this, _1, _2), gui_context());
463         _route->output()->changed.connect (route_connections, invalidator (*this), ui_bind (&MixerStrip::output_changed, this, _1, _2), gui_context());
464         _route->route_group_changed.connect (route_connections, invalidator (*this), boost::bind (&MixerStrip::route_group_changed, this), gui_context());
465
466         if (_route->panner()) {
467                 _route->panner()->Changed.connect (route_connections, invalidator (*this), boost::bind (&MixerStrip::connect_to_pan, this), gui_context());
468         }
469
470         if (is_audio_track()) {
471                 audio_track()->DiskstreamChanged.connect (route_connections, invalidator (*this), boost::bind (&MixerStrip::diskstream_changed, this), gui_context());
472         }
473
474         _route->comment_changed.connect (route_connections, invalidator (*this), ui_bind (&MixerStrip::comment_changed, this, _1), gui_context());
475         _route->gui_changed.connect (route_connections, invalidator (*this), ui_bind (&MixerStrip::route_gui_changed, this, _1, _2), gui_context());
476
477         set_stuff_from_route ();
478
479         /* now force an update of all the various elements */
480
481         mute_changed (0);
482         update_solo_display ();
483         name_changed ();
484         comment_changed (0);
485         route_group_changed ();
486
487         connect_to_pan ();
488
489         panners.setup_pan ();
490
491         update_diskstream_display ();
492         update_input_display ();
493         update_output_display ();
494
495         add_events (Gdk::BUTTON_RELEASE_MASK);
496
497         processor_box.show();
498
499         if (!route()->is_master() && !route()->is_monitor()) {
500                 /* we don't allow master or control routes to be hidden */
501                 hide_button.show();
502         }
503
504         width_button.show();
505         width_hide_box.show();
506         whvbox.show ();
507         global_frame.show();
508         global_vpacker.show();
509         button_table.show();
510         middle_button_table.show();
511         bottom_button_table.show();
512         processor_box.show_all ();
513         gpm.show_all ();
514         gain_meter_alignment.show ();
515         gain_unit_button.show();
516         gain_unit_label.show();
517         meter_point_button.show();
518         meter_point_label.show();
519         diskstream_button.show();
520         diskstream_label.show();
521         input_button.show();
522         input_label.show();
523         output_button.show();
524         output_label.show();
525         name_label.show();
526         name_button.show();
527         comment_button.show();
528         group_button.show();
529         group_label.show();
530
531         show ();
532 }
533
534 void
535 MixerStrip::set_stuff_from_route ()
536 {
537         XMLProperty *prop;
538
539         ensure_xml_node ();
540
541         /* if width is not set, it will be set by the MixerUI or editor */
542
543         if ((prop = xml_node->property ("strip-width")) != 0) {
544                 set_width_enum (Width (string_2_enum (prop->value(), _width)), this);
545         }
546
547         if ((prop = xml_node->property ("shown-mixer")) != 0) {
548                 if (prop->value() == "no") {
549                         _marked_for_display = false;
550                 } else {
551                         _marked_for_display = true;
552                 }
553         } else {
554                 /* backwards compatibility */
555                 _marked_for_display = true;
556         }
557 }
558
559 void
560 MixerStrip::set_width_enum (Width w, void* owner)
561 {
562         /* always set the gpm width again, things may be hidden */
563
564         gpm.set_width (w);
565         panners.set_width (w);
566
567         boost::shared_ptr<AutomationList> gain_automation = _route->gain_control()->alist();
568
569         _width_owner = owner;
570
571         ensure_xml_node ();
572
573         _width = w;
574
575         if (_width_owner == this) {
576                 xml_node->add_property ("strip-width", enum_2_string (_width));
577         }
578
579         set_button_names ();
580
581         switch (w) {
582         case Wide:
583                 if (show_sends_button)  {
584                         ((Gtk::Label*)show_sends_button->get_child())->set_text (_("Sends"));
585                 }
586
587                 if (_route->comment() == "") {
588                         comment_button.unset_bg (STATE_NORMAL);
589                         ((Gtk::Label*)comment_button.get_child())->set_text (_("Comments"));
590                 } else {
591                         comment_button.modify_bg (STATE_NORMAL, color());
592                         ((Gtk::Label*)comment_button.get_child())->set_text (_("*Comments*"));
593                 }
594
595                 ((Gtk::Label*)gpm.gain_automation_style_button.get_child())->set_text (
596                                 gpm.astyle_string(gain_automation->automation_style()));
597                 ((Gtk::Label*)gpm.gain_automation_state_button.get_child())->set_text (
598                                 gpm.astate_string(gain_automation->automation_state()));
599
600                 if (_route->panner()) {
601                         ((Gtk::Label*)panners.pan_automation_style_button.get_child())->set_text (
602                                         panners.astyle_string(_route->panner()->automation_style()));
603                         ((Gtk::Label*)panners.pan_automation_state_button.get_child())->set_text (
604                                         panners.astate_string(_route->panner()->automation_state()));
605                 }
606
607                 Gtkmm2ext::set_size_request_to_display_given_text (name_button, "long", 2, 2);
608                 set_size_request (-1, -1);
609                 break;
610
611         case Narrow:
612                 if (show_sends_button) {
613                         ((Gtk::Label*)show_sends_button->get_child())->set_text (_("Snd"));
614                 }
615
616                 if (_route->comment() == "") {
617                        comment_button.unset_bg (STATE_NORMAL);
618                        ((Gtk::Label*)comment_button.get_child())->set_text (_("Cmt"));
619                 } else {
620                        comment_button.modify_bg (STATE_NORMAL, color());
621                        ((Gtk::Label*)comment_button.get_child())->set_text (_("*Cmt*"));
622                 }
623
624                 ((Gtk::Label*)gpm.gain_automation_style_button.get_child())->set_text (
625                                 gpm.short_astyle_string(gain_automation->automation_style()));
626                 ((Gtk::Label*)gpm.gain_automation_state_button.get_child())->set_text (
627                                 gpm.short_astate_string(gain_automation->automation_state()));
628
629                 if (_route->panner()) {
630                         ((Gtk::Label*)panners.pan_automation_style_button.get_child())->set_text (
631                         panners.short_astyle_string(_route->panner()->automation_style()));
632                         ((Gtk::Label*)panners.pan_automation_state_button.get_child())->set_text (
633                         panners.short_astate_string(_route->panner()->automation_state()));
634                 }
635
636                 Gtkmm2ext::set_size_request_to_display_given_text (name_button, longest_label.c_str(), 2, 2);
637                 set_size_request (max (50, gpm.get_gm_width()), -1);
638                 break;
639         }
640
641         processor_box.set_width (w);
642         
643         update_input_display ();
644         update_output_display ();
645         route_group_changed ();
646         name_changed ();
647         WidthChanged ();
648 }
649
650 void
651 MixerStrip::set_packed (bool yn)
652 {
653         _packed = yn;
654
655         ensure_xml_node ();
656
657         if (_packed) {
658                 xml_node->add_property ("shown-mixer", "yes");
659         } else {
660                 xml_node->add_property ("shown-mixer", "no");
661         }
662 }
663
664
665 gint
666 MixerStrip::output_press (GdkEventButton *ev)
667 {
668         using namespace Menu_Helpers;
669         if (!_session->engine().connected()) {
670                 MessageDialog msg (_("Not connected to JACK - no I/O changes are possible"));
671                 msg.run ();
672                 return true;
673         }
674
675         MenuList& citems = output_menu.items();
676         switch (ev->button) {
677
678         case 1:
679                 edit_output_configuration ();
680                 break;
681
682         case 3:
683         {
684                 output_menu.set_name ("ArdourContextMenu");
685                 citems.clear ();
686                 output_menu_bundles.clear ();
687
688                 citems.push_back (MenuElem (_("Disconnect"), sigc::mem_fun (*(static_cast<RouteUI*>(this)), &RouteUI::disconnect_output)));
689                 citems.push_back (SeparatorElem());
690
691                 ARDOUR::BundleList current = _route->output()->bundles_connected ();
692
693                 boost::shared_ptr<ARDOUR::BundleList> b = _session->bundles ();
694
695                 /* give user bundles first chance at being in the menu */
696                 
697                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
698                         if (boost::dynamic_pointer_cast<UserBundle> (*i)) {
699                                 maybe_add_bundle_to_output_menu (*i, current);
700                         }
701                 }
702
703                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
704                         if (boost::dynamic_pointer_cast<UserBundle> (*i) == 0) {
705                                 maybe_add_bundle_to_output_menu (*i, current);
706                         }
707                 }
708                 
709                 boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
710                 for (ARDOUR::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
711                         maybe_add_bundle_to_output_menu ((*i)->input()->bundle(), current);
712                 }
713
714                 if (citems.size() == 2) {
715                         /* no routes added; remove the separator */
716                         citems.pop_back ();
717                 }
718
719                 output_menu.popup (1, ev->time);
720                 break;
721         }
722
723         default:
724                 break;
725         }
726         return TRUE;
727 }
728
729 void
730 MixerStrip::edit_output_configuration ()
731 {
732         if (output_selector == 0) {
733                 
734                 boost::shared_ptr<Send> send;
735                 boost::shared_ptr<IO> output;
736
737                 if ((send = boost::dynamic_pointer_cast<Send>(_current_delivery)) != 0) {
738                         if (!boost::dynamic_pointer_cast<InternalSend>(send)) {
739                                 output = send->output();
740                         } else {
741                                 output = _route->output ();
742                         } 
743                 } else {
744                         output = _route->output ();
745                 }
746                 
747                 output_selector = new IOSelectorWindow (_session, output);
748         }
749
750         if (output_selector->is_visible()) {
751                 output_selector->get_toplevel()->get_window()->raise();
752         } else {
753                 output_selector->present ();
754         }
755 }
756
757 void
758 MixerStrip::edit_input_configuration ()
759 {
760         if (input_selector == 0) {
761                 input_selector = new IOSelectorWindow (_session, _route->input());
762         }
763
764         if (input_selector->is_visible()) {
765                 input_selector->get_toplevel()->get_window()->raise();
766         } else {
767                 input_selector->present ();
768         }
769 }
770
771 gint
772 MixerStrip::input_press (GdkEventButton *ev)
773 {
774         using namespace Menu_Helpers;
775
776         MenuList& citems = input_menu.items();
777         input_menu.set_name ("ArdourContextMenu");
778         citems.clear();
779
780         if (!_session->engine().connected()) {
781                 MessageDialog msg (_("Not connected to JACK - no I/O changes are possible"));
782                 msg.run ();
783                 return true;
784         }
785
786         switch (ev->button) {
787
788         case 1:
789                 edit_input_configuration ();
790                 break;
791
792         case 3:
793         {
794                 citems.push_back (MenuElem (_("Disconnect"), sigc::mem_fun (*(static_cast<RouteUI*>(this)), &RouteUI::disconnect_input)));
795                 citems.push_back (SeparatorElem());
796                 input_menu_bundles.clear ();
797
798                 ARDOUR::BundleList current = _route->input()->bundles_connected ();
799
800                 boost::shared_ptr<ARDOUR::BundleList> b = _session->bundles ();
801
802                 /* give user bundles first chance at being in the menu */
803                 
804                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
805                         if (boost::dynamic_pointer_cast<UserBundle> (*i)) {
806                                 maybe_add_bundle_to_input_menu (*i, current);
807                         }
808                 }
809
810                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
811                         if (boost::dynamic_pointer_cast<UserBundle> (*i) == 0) {
812                                 maybe_add_bundle_to_input_menu (*i, current);
813                         }
814                 }
815                 
816                 boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
817                 for (ARDOUR::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
818                         maybe_add_bundle_to_input_menu ((*i)->output()->bundle(), current);
819                 }
820
821                 if (citems.size() == 2) {
822                         /* no routes added; remove the separator */
823                         citems.pop_back ();
824                 }
825
826                 input_menu.popup (1, ev->time);
827                 break;
828         }
829         default:
830                 break;
831         }
832         return TRUE;
833 }
834
835 void
836 MixerStrip::bundle_input_toggled (boost::shared_ptr<ARDOUR::Bundle> c)
837 {
838         if (ignore_toggle) {
839                 return;
840         }
841
842         ARDOUR::BundleList current = _route->input()->bundles_connected ();
843
844         if (std::find (current.begin(), current.end(), c) == current.end()) {
845                 _route->input()->connect_ports_to_bundle (c, this);
846         } else {
847                 _route->input()->disconnect_ports_from_bundle (c, this);
848         }
849 }
850
851 void
852 MixerStrip::bundle_output_toggled (boost::shared_ptr<ARDOUR::Bundle> c)
853 {
854         if (ignore_toggle) {
855                 return;
856         }
857
858         ARDOUR::BundleList current = _route->output()->bundles_connected ();
859
860         if (std::find (current.begin(), current.end(), c) == current.end()) {
861                 _route->output()->connect_ports_to_bundle (c, this);
862         } else {
863                 _route->output()->disconnect_ports_from_bundle (c, this);
864         }
865 }
866
867 void
868 MixerStrip::maybe_add_bundle_to_input_menu (boost::shared_ptr<Bundle> b, ARDOUR::BundleList const & current)
869 {
870         using namespace Menu_Helpers;
871
872         if (b->ports_are_outputs() == false || b->nchannels() != _route->n_inputs()) {
873                 return;
874         }
875
876         list<boost::shared_ptr<Bundle> >::iterator i = input_menu_bundles.begin ();
877         while (i != input_menu_bundles.end() && b->has_same_ports (*i) == false) {
878                 ++i;
879         }
880
881         if (i != input_menu_bundles.end()) {
882                 return;
883         }
884
885         input_menu_bundles.push_back (b);
886
887         MenuList& citems = input_menu.items();
888
889         std::string n = b->name ();
890         replace_all (n, "_", " ");
891
892         citems.push_back (CheckMenuElem (n, sigc::bind (sigc::mem_fun(*this, &MixerStrip::bundle_input_toggled), b)));
893
894         if (std::find (current.begin(), current.end(), b) != current.end()) {
895                 ignore_toggle = true;
896                 dynamic_cast<CheckMenuItem *> (&citems.back())->set_active (true);
897                 ignore_toggle = false;
898         }
899 }
900
901 void
902 MixerStrip::maybe_add_bundle_to_output_menu (boost::shared_ptr<Bundle> b, ARDOUR::BundleList const & current)
903 {
904         using namespace Menu_Helpers;
905
906         if (b->ports_are_inputs() == false || b->nchannels() != _route->n_outputs()) {
907                 return;
908         }
909
910         list<boost::shared_ptr<Bundle> >::iterator i = output_menu_bundles.begin ();
911         while (i != output_menu_bundles.end() && b->has_same_ports (*i) == false) {
912                 ++i;
913         }
914
915         if (i != output_menu_bundles.end()) {
916                 return;
917         }
918
919         output_menu_bundles.push_back (b);
920         
921         MenuList& citems = output_menu.items();
922
923         std::string n = b->name ();
924         replace_all (n, "_", " ");
925
926         citems.push_back (CheckMenuElem (n, sigc::bind (sigc::mem_fun(*this, &MixerStrip::bundle_output_toggled), b)));
927
928         if (std::find (current.begin(), current.end(), b) != current.end()) {
929                 ignore_toggle = true;
930                 dynamic_cast<CheckMenuItem *> (&citems.back())->set_active (true);
931                 ignore_toggle = false;
932         }
933 }
934
935 void
936 MixerStrip::update_diskstream_display ()
937 {
938         if (is_track()) {
939
940                 if (input_selector) {
941                         input_selector->hide_all ();
942                 }
943
944                 show_route_color ();
945
946         } else {
947
948                 show_passthru_color ();
949         }
950 }
951
952 void
953 MixerStrip::connect_to_pan ()
954 {
955         ENSURE_GUI_THREAD (*this, &MixerStrip::connect_to_pan)
956
957         panstate_connection.disconnect ();
958         panstyle_connection.disconnect ();
959
960         if (!_route->panner()) {
961                 return;
962         }
963
964         boost::shared_ptr<ARDOUR::AutomationControl> pan_control
965                 = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(
966                                 _route->panner()->control(Evoral::Parameter(PanAutomation)));
967
968         if (pan_control) {
969                 pan_control->alist()->automation_state_changed.connect (panstate_connection, invalidator (*this), boost::bind (&PannerUI::pan_automation_state_changed, &panners), gui_context());
970                 pan_control->alist()->automation_style_changed.connect (panstyle_connection, invalidator (*this), boost::bind (&PannerUI::pan_automation_style_changed, &panners), gui_context());
971         }
972
973         panners.pan_changed (this);
974 }
975
976
977 /*
978  * Output port labelling
979  * =====================
980  *
981  * Case 1: Each output has one connection, all connections are to system:playback_%i
982  *   out 1 -> system:playback_1
983  *   out 2 -> system:playback_2
984  *   out 3 -> system:playback_3
985  *   Display as: 1/2/3
986  *
987  * Case 2: Each output has one connection, all connections are to ardour:track_x/in 1
988  *   out 1 -> ardour:track_x/in 1
989  *   out 2 -> ardour:track_x/in 2
990  *   Display as: track_x
991  *
992  * Case 3: Each output has one connection, all connections are to Jack client "program x"
993  *   out 1 -> program x:foo
994  *   out 2 -> program x:foo
995  *   Display as: program x
996  *
997  * Case 4: No connections (Disconnected)
998  *   Display as: -
999  *
1000  * Default case (unusual routing):
1001  *   Display as: *number of connections*
1002  *
1003  * Tooltips
1004  * ========
1005  * .-----------------------------------------------.
1006  * | Mixdown                                       |
1007  * | out 1 -> ardour:master/in 1, jamin:input/in 1 |
1008  * | out 2 -> ardour:master/in 2, jamin:input/in 2 |
1009  * '-----------------------------------------------'
1010  * .-----------------------------------------------.
1011  * | Guitar SM58                                   |
1012  * | Disconnected                                  |
1013  * '-----------------------------------------------'
1014  */
1015
1016 void
1017 MixerStrip::update_io_button (boost::shared_ptr<ARDOUR::Route> route, Width width, bool for_input)
1018 {
1019         uint32_t io_count;
1020         uint32_t io_index;
1021         Port *port;
1022         vector<string> port_connections;
1023
1024         uint32_t total_connection_count = 0;
1025         uint32_t io_connection_count = 0;
1026         uint32_t ardour_connection_count = 0;
1027         uint32_t system_connection_count = 0;
1028         uint32_t other_connection_count = 0;
1029
1030         ostringstream label;
1031         string label_string;
1032         char * label_cstr;
1033
1034         bool have_label = false;
1035         bool each_io_has_one_connection = true;
1036
1037         string connection_name;
1038         string ardour_track_name;
1039         string other_connection_type;
1040         string system_ports;
1041         string system_port;
1042
1043         ostringstream tooltip;
1044         char * tooltip_cstr;
1045
1046         tooltip << route->name();
1047
1048         if (for_input) {
1049                 io_count = route->n_inputs().n_total();
1050         } else {
1051                 io_count = route->n_outputs().n_total();
1052         }
1053
1054         for (io_index = 0; io_index < io_count; ++io_index) {
1055                 if (for_input) {
1056                         port = route->input()->nth (io_index);
1057                 } else {
1058                         port = route->output()->nth (io_index);
1059                 }
1060                 
1061                 port_connections.clear ();
1062                 port->get_connections(port_connections);
1063                 io_connection_count = 0;
1064
1065                 if (!port_connections.empty()) {
1066                         for (vector<string>::iterator i = port_connections.begin(); i != port_connections.end(); ++i) {
1067                                 string& connection_name (*i);
1068
1069                                 if (io_connection_count == 0) {
1070                                         tooltip << endl << port->name().substr(port->name().find("/") + 1) << " -> " << connection_name;
1071                                 } else {
1072                                         tooltip << ", " << connection_name;
1073                                 }
1074
1075                                 if (connection_name.find("ardour:") == 0) {
1076                                         if (ardour_track_name.empty()) {
1077                                                 // "ardour:Master/in 1" -> "ardour:Master/"
1078                                                 string::size_type slash = connection_name.find("/");
1079                                                 if (slash != string::npos) {
1080                                                         ardour_track_name = connection_name.substr(0, slash + 1);
1081                                                 }
1082                                         }
1083
1084                                         if (connection_name.find(ardour_track_name) == 0) {
1085                                                 ++ardour_connection_count;
1086                                         }
1087                                 } else if (connection_name.find("system:") == 0) {
1088                                         if (for_input) {
1089                                                 // "system:capture_123" -> "123"
1090                                                 system_port = connection_name.substr(15);
1091                                         } else {
1092                                                 // "system:playback_123" -> "123"
1093                                                 system_port = connection_name.substr(16);
1094                                         }
1095
1096                                         if (system_ports.empty()) {
1097                                                 system_ports += system_port;
1098                                         } else {
1099                                                 system_ports += "/" + system_port;
1100                                         }
1101
1102                                         ++system_connection_count;
1103                                 } else {
1104                                         if (other_connection_type.empty()) {
1105                                                 // "jamin:in 1" -> "jamin:"
1106                                                 other_connection_type = connection_name.substr(0, connection_name.find(":") + 1);
1107                                         }
1108
1109                                         if (connection_name.find(other_connection_type) == 0) {
1110                                                 ++other_connection_count;
1111                                         }
1112                                 }
1113
1114                                 ++total_connection_count;
1115                                 ++io_connection_count;
1116                         }
1117                 }
1118
1119                 if (io_connection_count != 1) {
1120                         each_io_has_one_connection = false;
1121                 }
1122         }
1123
1124         if (total_connection_count == 0) {
1125                 tooltip << endl << _("Disconnected");
1126         }
1127
1128         tooltip_cstr = new char[tooltip.str().size() + 1];
1129         strcpy(tooltip_cstr, tooltip.str().c_str());
1130
1131         if (for_input) {
1132                 ARDOUR_UI::instance()->set_tip (&input_button, tooltip_cstr, "");
1133         } else {
1134                 ARDOUR_UI::instance()->set_tip (&output_button, tooltip_cstr, "");
1135         }
1136
1137         if (each_io_has_one_connection) {
1138                 if ((total_connection_count == ardour_connection_count)) {
1139                         // all connections are to the same track in ardour
1140                         // "ardour:Master/" -> "Master"
1141                         string::size_type slash = ardour_track_name.find("/");
1142                         if (slash != string::npos) {
1143                                 label << ardour_track_name.substr(7, slash - 7);
1144                                 have_label = true;
1145                         }
1146                 }
1147                 else if (total_connection_count == system_connection_count) {
1148                         // all connections are to system ports
1149                         label << system_ports;
1150                         have_label = true;
1151                 }
1152                 else if (total_connection_count == other_connection_count) {
1153                         // all connections are to the same external program eg jamin
1154                         // "jamin:" -> "jamin"
1155                         label << other_connection_type.substr(0, other_connection_type.size() - 1);
1156                         have_label = true;
1157                 }
1158         }
1159
1160         if (!have_label) {
1161                 if (total_connection_count == 0) {
1162                         // Disconnected
1163                         label << "-";
1164                 } else {
1165                         // Odd configuration
1166                         label << "*" << total_connection_count << "*";
1167                 }
1168         }
1169
1170         switch (width) {
1171         case Wide:
1172                 label_string = label.str().substr(0, 6);
1173                 break;
1174         case Narrow:
1175                 label_string = label.str().substr(0, 3);
1176                 break;
1177         }
1178
1179         label_cstr = new char[label_string.size() + 1];
1180         strcpy(label_cstr, label_string.c_str());
1181
1182         if (for_input) {
1183                 input_label.set_text (label_cstr);
1184         } else {
1185                 output_label.set_text (label_cstr);
1186         }
1187 }
1188
1189 void
1190 MixerStrip::update_input_display ()
1191 {
1192         update_io_button (_route, _width, true);
1193         panners.setup_pan ();
1194 }
1195
1196 void
1197 MixerStrip::update_output_display ()
1198 {
1199         update_io_button (_route, _width, false);
1200         gpm.setup_meters ();
1201         panners.setup_pan ();
1202 }
1203
1204 void
1205 MixerStrip::fast_update ()
1206 {
1207         gpm.update_meters ();
1208 }
1209
1210 void
1211 MixerStrip::diskstream_changed ()
1212 {
1213         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&MixerStrip::update_diskstream_display, this));
1214 }
1215
1216 void
1217 MixerStrip::input_changed (IOChange /*change*/, void */*src*/)
1218 {
1219         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&MixerStrip::update_input_display, this));
1220         set_width_enum (_width, this);
1221 }
1222
1223 void
1224 MixerStrip::output_changed (IOChange /*change*/, void */*src*/)
1225 {
1226         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&MixerStrip::update_output_display, this));
1227         set_width_enum (_width, this);
1228 }
1229
1230
1231 void
1232 MixerStrip::comment_editor_done_editing()
1233 {
1234         string str =  comment_area->get_buffer()->get_text();
1235         if (_route->comment() != str) {
1236                 _route->set_comment (str, this);
1237
1238                 switch (_width) {
1239
1240                 case Wide:
1241                         if (! str.empty()) {
1242                                 comment_button.modify_bg (STATE_NORMAL, color());
1243                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("*Comments*"));
1244                         } else {
1245                                 comment_button.unset_bg (STATE_NORMAL);
1246                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("Comments"));
1247                         }
1248                         break;
1249
1250                 case Narrow:
1251                         if (! str.empty()) {
1252                                 comment_button.modify_bg (STATE_NORMAL, color());
1253                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("*Cmt*"));
1254                         } else {
1255                                 comment_button.unset_bg (STATE_NORMAL);
1256                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("Cmt"));
1257                         }
1258                         break;
1259                 }
1260
1261                 ARDOUR_UI::instance()->set_tip (comment_button,
1262                                 str.empty() ? _("Click to Add/Edit Comments") : str);
1263         }
1264
1265 }
1266
1267 void
1268 MixerStrip::comment_button_clicked ()
1269 {
1270         if (comment_window == 0) {
1271                 setup_comment_editor ();
1272         }
1273
1274     int x, y, cw_width, cw_height;
1275
1276         if (comment_window->is_visible()) {
1277                 comment_window->hide ();
1278                 return;
1279         }
1280
1281         comment_window->get_size (cw_width, cw_height);
1282         comment_window->get_position(x, y);
1283         comment_window->move(x, y - (cw_height / 2) - 45);
1284         /*
1285            half the dialog height minus the comments button height
1286            with some window decoration fudge thrown in.
1287         */
1288
1289         comment_window->show();
1290         comment_window->present();
1291 }
1292
1293 void
1294 MixerStrip::setup_comment_editor ()
1295 {
1296         string title;
1297         title = _route->name();
1298         title += _(": comment editor");
1299
1300         comment_window = new ArdourDialog (title, false);
1301         comment_window->set_position (Gtk::WIN_POS_MOUSE);
1302         comment_window->set_skip_taskbar_hint (true);
1303         comment_window->signal_hide().connect (sigc::mem_fun(*this, &MixerStrip::comment_editor_done_editing));
1304
1305         comment_area = manage (new TextView());
1306         comment_area->set_name ("MixerTrackCommentArea");
1307         comment_area->set_size_request (110, 178);
1308         comment_area->set_wrap_mode (WRAP_WORD);
1309         comment_area->set_editable (true);
1310         comment_area->get_buffer()->set_text (_route->comment());
1311         comment_area->show ();
1312
1313         comment_window->get_vbox()->pack_start (*comment_area);
1314         comment_window->get_action_area()->hide();
1315 }
1316
1317 void
1318 MixerStrip::comment_changed (void *src)
1319 {
1320         ENSURE_GUI_THREAD (*this, &MixerStrip::comment_changed, src)
1321
1322         if (src != this) {
1323                 ignore_comment_edit = true;
1324                 if (comment_area) {
1325                         comment_area->get_buffer()->set_text (_route->comment());
1326                 }
1327                 ignore_comment_edit = false;
1328         }
1329 }
1330
1331 void
1332 MixerStrip::set_route_group (RouteGroup *rg)
1333 {
1334         rg->add (_route);
1335 }
1336
1337 bool
1338 MixerStrip::select_route_group (GdkEventButton *ev)
1339 {
1340         using namespace Menu_Helpers;
1341
1342         if (ev->button == 1) {
1343
1344                 if (group_menu == 0) {
1345
1346                         PropertyList* plist = new PropertyList();
1347
1348                         plist->add (Properties::gain, true);
1349                         plist->add (Properties::mute, true);
1350                         plist->add (Properties::solo, true);
1351
1352                         group_menu = new RouteGroupMenu (_session, plist);
1353                         group_menu->GroupSelected.connect (sigc::mem_fun (*this, &MixerStrip::set_route_group));
1354                 }
1355
1356                 group_menu->popup (1, ev->time);
1357         }
1358
1359         return true;
1360 }
1361
1362 void
1363 MixerStrip::route_group_changed ()
1364 {
1365         ENSURE_GUI_THREAD (*this, &MixerStrip::route_group_changed)
1366
1367         RouteGroup *rg = _route->route_group();
1368
1369         if (rg) {
1370                 group_label.set_text (PBD::short_version (rg->name(), 5));
1371         } else {
1372                 switch (_width) {
1373                 case Wide:
1374                         group_label.set_text (_("Grp"));
1375                         break;
1376                 case Narrow:
1377                         group_label.set_text (_("~G"));
1378                         break;
1379                 }
1380         }
1381 }
1382
1383
1384 void
1385 MixerStrip::route_gui_changed (string what_changed, void* ignored)
1386 {
1387         ENSURE_GUI_THREAD (*this, &MixerStrip::route_gui_changed, what_changed, ignored)
1388
1389         if (what_changed == "color") {
1390                 if (set_color_from_route () == 0) {
1391                         show_route_color ();
1392                 }
1393         }
1394 }
1395
1396 void
1397 MixerStrip::show_route_color ()
1398 {
1399         name_button.modify_bg (STATE_NORMAL, color());
1400         top_event_box.modify_bg (STATE_NORMAL, color());
1401         route_active_changed ();
1402 }
1403
1404 void
1405 MixerStrip::show_passthru_color ()
1406 {
1407         route_active_changed ();
1408 }
1409
1410 void
1411 MixerStrip::build_route_ops_menu ()
1412 {
1413         using namespace Menu_Helpers;
1414         route_ops_menu = new Menu;
1415         route_ops_menu->set_name ("ArdourContextMenu");
1416
1417         MenuList& items = route_ops_menu->items();
1418
1419         items.push_back (MenuElem (_("Save As Template..."), sigc::mem_fun(*this, &RouteUI::save_as_template)));
1420         items.push_back (MenuElem (_("Rename..."), sigc::mem_fun(*this, &RouteUI::route_rename)));
1421         rename_menu_item = &items.back();
1422         items.push_back (SeparatorElem());
1423         items.push_back (CheckMenuElem (_("Active"), sigc::mem_fun (*this, &RouteUI::toggle_route_active)));
1424         route_active_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
1425         route_active_menu_item->set_active (_route->active());
1426
1427         items.push_back (SeparatorElem());
1428
1429         items.push_back (MenuElem (_("Adjust Latency..."), sigc::mem_fun (*this, &RouteUI::adjust_latency)));
1430
1431         items.push_back (SeparatorElem());
1432         items.push_back (CheckMenuElem (_("Protect Against Denormals"), sigc::mem_fun (*this, &RouteUI::toggle_denormal_protection)));
1433         denormal_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
1434         denormal_menu_item->set_active (_route->denormal_protection());
1435
1436         if (!Profile->get_sae()) {
1437                 items.push_back (SeparatorElem());
1438                 items.push_back (MenuElem (_("Remote Control ID..."), sigc::mem_fun (*this, &RouteUI::open_remote_control_id_dialog)));
1439         }
1440
1441         items.push_back (SeparatorElem());
1442         items.push_back (MenuElem (_("Remove"), sigc::mem_fun(*this, &RouteUI::remove_this_route)));
1443 }
1444
1445 gboolean
1446 MixerStrip::name_button_button_press (GdkEventButton* ev)
1447 {
1448         if (ev->button == 3) {
1449                 list_route_operations ();
1450
1451                 /* do not allow rename if the track is record-enabled */
1452                 rename_menu_item->set_sensitive (!_route->record_enabled());
1453                 route_ops_menu->popup (1, ev->time);
1454
1455         } else if (ev->button == 1) {
1456                 revert_to_default_display ();
1457         }
1458
1459         return false;
1460 }
1461
1462 void
1463 MixerStrip::list_route_operations ()
1464 {
1465         if (route_ops_menu == 0) {
1466                 build_route_ops_menu ();
1467         }
1468 }
1469
1470 void
1471 MixerStrip::set_selected (bool yn)
1472 {
1473         AxisView::set_selected (yn);
1474         if (_selected) {
1475                 global_frame.set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
1476                 global_frame.set_name ("MixerStripSelectedFrame");
1477         } else {
1478                 global_frame.set_shadow_type (Gtk::SHADOW_IN);
1479                 global_frame.set_name ("MixerStripFrame");
1480         }
1481         global_frame.queue_draw ();
1482 }
1483
1484 void
1485 MixerStrip::name_changed ()
1486 {
1487         switch (_width) {
1488         case Wide:
1489                 RouteUI::property_changed (PropertyChange (ARDOUR::Properties::name));
1490                 break;
1491         case Narrow:
1492                 name_label.set_text (PBD::short_version (_route->name(), 5));
1493                 break;
1494         }
1495 }
1496
1497 void
1498 MixerStrip::width_clicked ()
1499 {
1500         switch (_width) {
1501         case Wide:
1502                 set_width_enum (Narrow, this);
1503                 break;
1504         case Narrow:
1505                 set_width_enum (Wide, this);
1506                 break;
1507         }
1508 }
1509
1510 void
1511 MixerStrip::hide_clicked ()
1512 {
1513         // LAME fix to reset the button status for when it is redisplayed (part 1)
1514         hide_button.set_sensitive(false);
1515
1516         if (_embedded) {
1517                 Hiding(); /* EMIT_SIGNAL */
1518         } else {
1519                 _mixer.hide_strip (this);
1520         }
1521
1522         // (part 2)
1523         hide_button.set_sensitive(true);
1524 }
1525
1526 void
1527 MixerStrip::set_embedded (bool yn)
1528 {
1529         _embedded = yn;
1530 }
1531
1532 void
1533 MixerStrip::map_frozen ()
1534 {
1535         ENSURE_GUI_THREAD (*this, &MixerStrip::map_frozen)
1536
1537         boost::shared_ptr<AudioTrack> at = audio_track();
1538
1539         if (at) {
1540                 switch (at->freeze_state()) {
1541                 case AudioTrack::Frozen:
1542                         processor_box.set_sensitive (false);
1543                         break;
1544                 default:
1545                         processor_box.set_sensitive (true);
1546                         // XXX need some way, maybe, to retoggle redirect editors
1547                         break;
1548                 }
1549         }
1550
1551         hide_redirect_editors ();
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         void* gui = processor->get_gui ();
1569
1570         if (gui) {
1571                 static_cast<Gtk::Widget*>(gui)->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 }