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