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