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