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