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