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