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