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