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