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