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