Fix crash on save.
[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_window.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 ArdourWindow (""); // 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->add (*comment_area);
1368 }
1369
1370 void
1371 MixerStrip::comment_changed (void *src)
1372 {
1373         ENSURE_GUI_THREAD (*this, &MixerStrip::comment_changed, src)
1374
1375         if (src != this) {
1376                 ignore_comment_edit = true;
1377                 if (comment_area) {
1378                         comment_area->get_buffer()->set_text (_route->comment());
1379                 }
1380                 ignore_comment_edit = false;
1381         }
1382 }
1383
1384 bool
1385 MixerStrip::select_route_group (GdkEventButton *ev)
1386 {
1387         using namespace Menu_Helpers;
1388
1389         if (ev->button == 1) {
1390
1391                 if (group_menu == 0) {
1392
1393                         PropertyList* plist = new PropertyList();
1394
1395                         plist->add (Properties::gain, true);
1396                         plist->add (Properties::mute, true);
1397                         plist->add (Properties::solo, true);
1398
1399                         group_menu = new RouteGroupMenu (_session, plist);
1400                 }
1401
1402                 WeakRouteList r;
1403                 r.push_back (route ());
1404                 group_menu->build (r);
1405                 group_menu->menu()->popup (1, ev->time);
1406         }
1407
1408         return true;
1409 }
1410
1411 void
1412 MixerStrip::route_group_changed ()
1413 {
1414         ENSURE_GUI_THREAD (*this, &MixerStrip::route_group_changed)
1415
1416         RouteGroup *rg = _route->route_group();
1417
1418         if (rg) {
1419                 group_label.set_text (PBD::short_version (rg->name(), 5));
1420         } else {
1421                 switch (_width) {
1422                 case Wide:
1423                         group_label.set_text (_("Grp"));
1424                         break;
1425                 case Narrow:
1426                         group_label.set_text (_("~G"));
1427                         break;
1428                 }
1429         }
1430 }
1431
1432 void
1433 MixerStrip::route_color_changed ()
1434 {
1435         name_button.modify_bg (STATE_NORMAL, color());
1436         top_event_box.modify_bg (STATE_NORMAL, color());
1437         reset_strip_style ();
1438 }
1439
1440 void
1441 MixerStrip::show_passthru_color ()
1442 {
1443         reset_strip_style ();
1444 }
1445
1446 void
1447 MixerStrip::build_route_ops_menu ()
1448 {
1449         using namespace Menu_Helpers;
1450         route_ops_menu = new Menu;
1451         route_ops_menu->set_name ("ArdourContextMenu");
1452
1453         MenuList& items = route_ops_menu->items();
1454
1455         items.push_back (MenuElem (_("Comments..."), sigc::mem_fun (*this, &MixerStrip::open_comment_editor)));
1456         items.push_back (MenuElem (_("Save As Template..."), sigc::mem_fun(*this, &RouteUI::save_as_template)));
1457         items.push_back (MenuElem (_("Rename..."), sigc::mem_fun(*this, &RouteUI::route_rename)));
1458         rename_menu_item = &items.back();
1459
1460         items.push_back (SeparatorElem());
1461         items.push_back (CheckMenuElem (_("Active")));
1462         CheckMenuItem* i = dynamic_cast<CheckMenuItem *> (&items.back());
1463         i->set_active (_route->active());
1464         i->signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &RouteUI::set_route_active), !_route->active(), false));
1465
1466         items.push_back (SeparatorElem());
1467
1468         items.push_back (MenuElem (_("Adjust Latency..."), sigc::mem_fun (*this, &RouteUI::adjust_latency)));
1469
1470         items.push_back (SeparatorElem());
1471         items.push_back (CheckMenuElem (_("Protect Against Denormals"), sigc::mem_fun (*this, &RouteUI::toggle_denormal_protection)));
1472         denormal_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
1473         denormal_menu_item->set_active (_route->denormal_protection());
1474
1475         if (!Profile->get_sae()) {
1476                 items.push_back (SeparatorElem());
1477                 items.push_back (MenuElem (_("Remote Control ID..."), sigc::mem_fun (*this, &RouteUI::open_remote_control_id_dialog)));
1478         }
1479
1480         items.push_back (SeparatorElem());
1481         items.push_back (MenuElem (_("Remove"), sigc::bind (sigc::mem_fun(*this, &RouteUI::remove_this_route), false)));
1482 }
1483
1484 gboolean
1485 MixerStrip::name_button_button_press (GdkEventButton* ev)
1486 {
1487         if (ev->button == 3) {
1488                 list_route_operations ();
1489
1490                 /* do not allow rename if the track is record-enabled */
1491                 rename_menu_item->set_sensitive (!_route->record_enabled());
1492                 route_ops_menu->popup (1, ev->time);
1493
1494         } else if (ev->button == 1) {
1495                 revert_to_default_display ();
1496         }
1497
1498         return false;
1499 }
1500
1501 void
1502 MixerStrip::list_route_operations ()
1503 {
1504         delete route_ops_menu;
1505         build_route_ops_menu ();
1506 }
1507
1508 void
1509 MixerStrip::set_selected (bool yn)
1510 {
1511         AxisView::set_selected (yn);
1512         if (_selected) {
1513                 global_frame.set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
1514                 global_frame.set_name ("MixerStripSelectedFrame");
1515         } else {
1516                 global_frame.set_shadow_type (Gtk::SHADOW_IN);
1517                 global_frame.set_name ("MixerStripFrame");
1518         }
1519         global_frame.queue_draw ();
1520 }
1521
1522 void
1523 MixerStrip::name_changed ()
1524 {
1525         switch (_width) {
1526         case Wide:
1527                 RouteUI::property_changed (PropertyChange (ARDOUR::Properties::name));
1528                 break;
1529         case Narrow:
1530                 name_label.set_text (PBD::short_version (_route->name(), 5));
1531                 break;
1532         }
1533 }
1534
1535 void
1536 MixerStrip::width_clicked ()
1537 {
1538         switch (_width) {
1539         case Wide:
1540                 set_width_enum (Narrow, this);
1541                 break;
1542         case Narrow:
1543                 set_width_enum (Wide, this);
1544                 break;
1545         }
1546 }
1547
1548 void
1549 MixerStrip::hide_clicked ()
1550 {
1551         // LAME fix to reset the button status for when it is redisplayed (part 1)
1552         hide_button.set_sensitive(false);
1553
1554         if (_embedded) {
1555                 Hiding(); /* EMIT_SIGNAL */
1556         } else {
1557                 _mixer.hide_strip (this);
1558         }
1559
1560         // (part 2)
1561         hide_button.set_sensitive(true);
1562 }
1563
1564 void
1565 MixerStrip::set_embedded (bool yn)
1566 {
1567         _embedded = yn;
1568 }
1569
1570 void
1571 MixerStrip::map_frozen ()
1572 {
1573         ENSURE_GUI_THREAD (*this, &MixerStrip::map_frozen)
1574
1575         boost::shared_ptr<AudioTrack> at = audio_track();
1576
1577         if (at) {
1578                 switch (at->freeze_state()) {
1579                 case AudioTrack::Frozen:
1580                         processor_box.set_sensitive (false);
1581                         hide_redirect_editors ();
1582                         break;
1583                 default:
1584                         processor_box.set_sensitive (true);
1585                         // XXX need some way, maybe, to retoggle redirect editors
1586                         break;
1587                 }
1588         }
1589 }
1590
1591 void
1592 MixerStrip::hide_redirect_editors ()
1593 {
1594         _route->foreach_processor (sigc::mem_fun (*this, &MixerStrip::hide_processor_editor));
1595 }
1596
1597 void
1598 MixerStrip::hide_processor_editor (boost::weak_ptr<Processor> p)
1599 {
1600         boost::shared_ptr<Processor> processor (p.lock ());
1601         if (!processor) {
1602                 return;
1603         }
1604
1605         Gtk::Window* w = processor_box.get_processor_ui (processor);
1606
1607         if (w) {
1608                 w->hide ();
1609         }
1610 }
1611
1612 void
1613 MixerStrip::reset_strip_style ()
1614 {
1615         if (_current_delivery && boost::dynamic_pointer_cast<Send>(_current_delivery)) {
1616
1617                 gpm.set_fader_name ("SendStripBase");
1618
1619         } else {
1620
1621                 if (is_midi_track()) {
1622                         if (_route->active()) {
1623                                 set_name ("MidiTrackStripBase");
1624                                 gpm.set_meter_strip_name ("MidiTrackMetrics");
1625                         } else {
1626                                 set_name ("MidiTrackStripBaseInactive");
1627                                 gpm.set_meter_strip_name ("MidiTrackMetricsInactive");
1628                         }
1629                         gpm.set_fader_name ("MidiTrackFader");
1630                 } else if (is_audio_track()) {
1631                         if (_route->active()) {
1632                                 set_name ("AudioTrackStripBase");
1633                                 gpm.set_meter_strip_name ("AudioTrackMetrics");
1634                         } else {
1635                                 set_name ("AudioTrackStripBaseInactive");
1636                                 gpm.set_meter_strip_name ("AudioTrackMetricsInactive");
1637                         }
1638                         gpm.set_fader_name ("AudioTrackFader");
1639                 } else {
1640                         if (_route->active()) {
1641                                 set_name ("AudioBusStripBase");
1642                                 gpm.set_meter_strip_name ("AudioBusMetrics");
1643                         } else {
1644                                 set_name ("AudioBusStripBaseInactive");
1645                                 gpm.set_meter_strip_name ("AudioBusMetricsInactive");
1646                         }
1647                         gpm.set_fader_name ("AudioBusFader");
1648
1649                         /* (no MIDI busses yet) */
1650                 }
1651         }
1652 }
1653
1654
1655 void
1656 MixerStrip::engine_stopped ()
1657 {
1658 }
1659
1660 void
1661 MixerStrip::engine_running ()
1662 {
1663 }
1664
1665 string
1666 MixerStrip::meter_point_string (MeterPoint mp)
1667 {
1668         switch (mp) {
1669         case MeterInput:
1670                 return _("in");
1671                 break;
1672
1673         case MeterPreFader:
1674                 return _("pre");
1675                 break;
1676
1677         case MeterPostFader:
1678                 return _("post");
1679                 break;
1680
1681         case MeterOutput:
1682                 return _("out");
1683                 break;
1684
1685         case MeterCustom:
1686         default:
1687                 return _("custom");
1688                 break;
1689         }
1690 }
1691
1692 /** Called when the metering point has changed */
1693 void
1694 MixerStrip::meter_changed ()
1695 {
1696         meter_point_label.set_text (meter_point_string (_route->meter_point()));
1697         gpm.setup_meters ();
1698         // reset peak when meter point changes
1699         gpm.reset_peak_display();
1700 }
1701
1702 /** The bus that we are displaying sends to has changed, or been turned off.
1703  *  @param send_to New bus that we are displaying sends to, or 0.
1704  */
1705 void
1706 MixerStrip::bus_send_display_changed (boost::shared_ptr<Route> send_to)
1707 {
1708         RouteUI::bus_send_display_changed (send_to);
1709
1710         if (send_to) {
1711                 boost::shared_ptr<Send> send = _route->internal_send_for (send_to);
1712                 if (send) {
1713                         show_send (send);
1714                 } else {
1715                         revert_to_default_display ();
1716                 }
1717         } else {
1718                 revert_to_default_display ();
1719         }
1720 }
1721
1722 void
1723 MixerStrip::drop_send ()
1724 {
1725         boost::shared_ptr<Send> current_send;
1726
1727         if (_current_delivery && (current_send = boost::dynamic_pointer_cast<Send>(_current_delivery))) {
1728                 current_send->set_metering (false);
1729         }
1730
1731         send_gone_connection.disconnect ();
1732         input_button.set_sensitive (true);
1733         output_button.set_sensitive (true);
1734         group_button.set_sensitive (true);
1735         set_invert_sensitive (true);
1736         meter_point_button.set_sensitive (true);
1737         mute_button->set_sensitive (true);
1738         solo_button->set_sensitive (true);
1739         rec_enable_button->set_sensitive (true);
1740         solo_isolated_led->set_sensitive (true);
1741         solo_safe_led->set_sensitive (true);
1742         monitor_input_button->set_sensitive (true);
1743         monitor_disk_button->set_sensitive (true);
1744         _comment_button.set_sensitive (true);
1745 }
1746
1747 void
1748 MixerStrip::set_current_delivery (boost::shared_ptr<Delivery> d)
1749 {
1750         _current_delivery = d;
1751         DeliveryChanged (_current_delivery);
1752 }
1753
1754 void
1755 MixerStrip::show_send (boost::shared_ptr<Send> send)
1756 {
1757         assert (send != 0);
1758
1759         drop_send ();
1760
1761         set_current_delivery (send);
1762
1763         send->set_metering (true);
1764         _current_delivery->DropReferences.connect (send_gone_connection, invalidator (*this), boost::bind (&MixerStrip::revert_to_default_display, this), gui_context());
1765
1766         gain_meter().set_controls (_route, send->meter(), send->amp());
1767         gain_meter().setup_meters ();
1768
1769         panner_ui().set_panner (_current_delivery->panner_shell(), _current_delivery->panner());
1770         panner_ui().setup_pan ();
1771
1772         input_button.set_sensitive (false);
1773         group_button.set_sensitive (false);
1774         set_invert_sensitive (false);
1775         meter_point_button.set_sensitive (false);
1776         mute_button->set_sensitive (false);
1777         solo_button->set_sensitive (false);
1778         rec_enable_button->set_sensitive (false);
1779         solo_isolated_led->set_sensitive (false);
1780         solo_safe_led->set_sensitive (false);
1781         monitor_input_button->set_sensitive (false);
1782         monitor_disk_button->set_sensitive (false);
1783         _comment_button.set_sensitive (false);
1784
1785         if (boost::dynamic_pointer_cast<InternalSend>(send)) {
1786                 output_button.set_sensitive (false);
1787         }
1788
1789         reset_strip_style ();
1790 }
1791
1792 void
1793 MixerStrip::revert_to_default_display ()
1794 {
1795         drop_send ();
1796
1797         set_current_delivery (_route->main_outs ());
1798
1799         gain_meter().set_controls (_route, _route->shared_peak_meter(), _route->amp());
1800         gain_meter().setup_meters ();
1801
1802         panner_ui().set_panner (_route->main_outs()->panner_shell(), _route->main_outs()->panner());
1803         panner_ui().setup_pan ();
1804
1805         reset_strip_style ();
1806 }
1807
1808 void
1809 MixerStrip::set_button_names ()
1810 {
1811         switch (_width) {
1812         case Wide:
1813                 rec_enable_button->set_text (_("Rec"));
1814                 mute_button->set_text (_("Mute"));
1815                 monitor_input_button->set_text (_("In"));
1816                 monitor_disk_button->set_text (_("Disk"));
1817
1818                 if (_route && _route->solo_safe()) {
1819                         if (solo_safe_pixbuf == 0) {
1820                                 solo_safe_pixbuf = ::get_icon("solo-safe-icon");
1821                         }
1822                         solo_button->set_image (solo_safe_pixbuf);
1823                         solo_button->set_text (string());
1824                 } else {
1825                         solo_button->set_image (Glib::RefPtr<Gdk::Pixbuf>());
1826                         if (!Config->get_solo_control_is_listen_control()) {
1827                                 solo_button->set_text (_("Solo"));
1828                         } else {
1829                                 switch (Config->get_listen_position()) {
1830                                 case AfterFaderListen:
1831                                         solo_button->set_text (_("AFL"));
1832                                         break;
1833                                 case PreFaderListen:
1834                                         solo_button->set_text (_("PFL"));
1835                                         break;
1836                                 }
1837                         }
1838                 }
1839                 break;
1840
1841         default:
1842                 rec_enable_button->set_text (_("R"));
1843                 mute_button->set_text (_("M"));
1844                 monitor_input_button->set_text (_("I"));
1845                 monitor_disk_button->set_text (_("D"));
1846                 if (_route && _route->solo_safe()) {
1847                         solo_button->remove ();
1848                         if (solo_safe_pixbuf == 0) {
1849                                 solo_safe_pixbuf =::get_icon("solo-safe-icon");
1850                         }
1851                         solo_button->set_image (solo_safe_pixbuf);
1852                         solo_button->set_text (string());
1853                 } else {
1854                         solo_button->set_image (Glib::RefPtr<Gdk::Pixbuf>());
1855                         if (!Config->get_solo_control_is_listen_control()) {
1856                                 solo_button->set_text (_("S"));
1857                         } else {
1858                                 switch (Config->get_listen_position()) {
1859                                 case AfterFaderListen:
1860                                         solo_button->set_text (_("A"));
1861                                         break;
1862                                 case PreFaderListen:
1863                                         solo_button->set_text (_("P"));
1864                                         break;
1865                                 }
1866                         }
1867                 }
1868                 break;
1869
1870         }
1871 }
1872
1873
1874 PluginSelector*
1875 MixerStrip::plugin_selector()
1876 {
1877         return _mixer.plugin_selector();
1878 }
1879
1880 void
1881 MixerStrip::hide_things ()
1882 {
1883         processor_box.hide_things ();
1884 }
1885
1886 bool
1887 MixerStrip::input_active_button_press (GdkEventButton*)
1888 {
1889         /* nothing happens on press */
1890         return true;
1891 }
1892
1893 bool
1894 MixerStrip::input_active_button_release (GdkEventButton* ev)
1895 {
1896         boost::shared_ptr<MidiTrack> mt = midi_track ();
1897
1898         if (!mt) {
1899                 return true;
1900         }
1901
1902         if (mt->input_active()) {
1903                 if (Keyboard::modifier_state_contains (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::SecondaryModifier))) {
1904                         /* turn all other tracks using this input off */
1905                         _session->set_exclusive_input_active (mt, false);
1906                 } else {
1907                         mt->set_input_active (false);
1908                 }
1909
1910         } else {
1911                 if (Keyboard::modifier_state_contains (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::SecondaryModifier))) {
1912                         /* turn all other tracks using this input on */
1913                         _session->set_exclusive_input_active (mt, true);
1914                 } else {
1915                         mt->set_input_active (true);
1916                 }
1917         }
1918
1919         return true;
1920 }
1921
1922 void
1923 MixerStrip::midi_input_status_changed ()
1924 {
1925         if (midi_input_enable_button) {
1926                 boost::shared_ptr<MidiTrack> mt = midi_track ();
1927                 assert (mt);
1928                 midi_input_enable_button->set_active (mt->input_active ());
1929         }
1930 }
1931
1932 string
1933 MixerStrip::state_id () const
1934 {
1935         return string_compose ("strip %1", _route->id().to_s());
1936 }
1937
1938 void
1939 MixerStrip::parameter_changed (string p)
1940 {
1941         if (p == _visibility.get_state_name()) {
1942                 /* The user has made changes to the mixer strip visibility, so get
1943                    our VisibilityGroup to reflect these changes in our widgets.
1944                 */
1945                 _visibility.set_state (Config->get_mixer_strip_visibility ());
1946         }
1947 }
1948
1949 /** Called to decide whether the solo isolate / solo lock button visibility should
1950  *  be overridden from that configured by the user.  We do this for the master bus.
1951  *
1952  *  @return optional value that is present if visibility state should be overridden.
1953  */
1954 boost::optional<bool>
1955 MixerStrip::override_solo_visibility () const
1956 {
1957         if (_route && _route->is_master ()) {
1958                 return boost::optional<bool> (false);
1959         }
1960         
1961         return boost::optional<bool> ();
1962 }
1963
1964 void
1965 MixerStrip::add_input_port (DataType t)
1966 {
1967         _route->input()->add_port ("", this, t);
1968 }
1969
1970 void
1971 MixerStrip::add_output_port (DataType t)
1972 {
1973         _route->output()->add_port ("", this, t);
1974 }
1975
1976 void
1977 MixerStrip::route_active_changed ()
1978 {
1979         reset_strip_style ();
1980 }
1981
1982 void
1983 MixerStrip::copy_processors ()
1984 {
1985         processor_box.processor_operation (ProcessorBox::ProcessorsCopy);
1986 }
1987
1988 void
1989 MixerStrip::cut_processors ()
1990 {
1991         processor_box.processor_operation (ProcessorBox::ProcessorsCut);
1992 }
1993
1994 void
1995 MixerStrip::paste_processors ()
1996 {
1997         processor_box.processor_operation (ProcessorBox::ProcessorsPaste);
1998 }
1999
2000 void
2001 MixerStrip::select_all_processors ()
2002 {
2003         processor_box.processor_operation (ProcessorBox::ProcessorsSelectAll);
2004 }
2005
2006 void
2007 MixerStrip::delete_processors ()
2008 {
2009         processor_box.processor_operation (ProcessorBox::ProcessorsDelete);
2010 }
2011
2012 void
2013 MixerStrip::toggle_processors ()
2014 {
2015         processor_box.processor_operation (ProcessorBox::ProcessorsToggleActive);
2016 }
2017
2018 void
2019 MixerStrip::ab_plugins ()
2020 {
2021         processor_box.processor_operation (ProcessorBox::ProcessorsAB);
2022 }
2023
2024 bool
2025 MixerStrip::level_meter_button_press (GdkEventButton* ev)
2026 {
2027         if (ev->button == 3) {
2028                 popup_level_meter_menu (ev);
2029                 return true;
2030         }
2031
2032         return false;
2033 }
2034
2035 void
2036 MixerStrip::popup_level_meter_menu (GdkEventButton* ev)
2037 {
2038         using namespace Gtk::Menu_Helpers;
2039
2040         Gtk::Menu* m = manage (new Menu);
2041         MenuList& items = m->items ();
2042
2043         RadioMenuItem::Group group;
2044
2045         add_level_meter_item (items, group, _("Input"), MeterInput);
2046         add_level_meter_item (items, group, _("Pre-fader"), MeterPreFader);
2047         add_level_meter_item (items, group, _("Post-fader"), MeterPostFader);
2048         add_level_meter_item (items, group, _("Output"), MeterOutput);
2049         add_level_meter_item (items, group, _("Custom"), MeterCustom);
2050
2051         m->popup (ev->button, ev->time);
2052 }
2053
2054 void
2055 MixerStrip::add_level_meter_item (Menu_Helpers::MenuList& items, RadioMenuItem::Group& group, string const & name, MeterPoint point)
2056 {
2057         using namespace Menu_Helpers;
2058         
2059         items.push_back (RadioMenuElem (group, name, sigc::bind (sigc::mem_fun (*this, &MixerStrip::set_meter_point), point)));
2060         RadioMenuItem* i = dynamic_cast<RadioMenuItem *> (&items.back ());
2061         i->set_active (_route->meter_point() == point);
2062 }
2063
2064 void
2065 MixerStrip::set_meter_point (MeterPoint p)
2066 {
2067         _route->set_meter_point (p);
2068 }