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