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