Another stab at the logic for selecting regions for operations, to ensure that edit...
[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/panner.h"
44 #include "ardour/send.h"
45 #include "ardour/processor.h"
46 #include "ardour/profile.h"
47 #include "ardour/ladspa_plugin.h"
48 #include "ardour/user_bundle.h"
49 #include "ardour/port.h"
50
51 #include "ardour_ui.h"
52 #include "ardour_dialog.h"
53 #include "mixer_strip.h"
54 #include "mixer_ui.h"
55 #include "keyboard.h"
56 #include "led.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 PBD::Signal1<void,MixerStrip*> MixerStrip::CatchDeletion;
76
77 MixerStrip::MixerStrip (Mixer_UI& mx, Session* sess, bool in_mixer)
78         : AxisView(sess)
79         , RouteUI (sess)
80         , _mixer(mx)
81         , _mixer_owned (in_mixer)
82         , processor_box (sess, boost::bind (&MixerStrip::plugin_selector, this), mx.selection(), this, in_mixer)
83         , gpm (sess, 250)
84         , panners (sess)
85         , _mono_button (_("Mono"))
86         , button_table (4, 2)
87         , solo_led_table (2, 2)
88         , middle_button_table (1, 2)
89         , bottom_button_table (1, 2)
90         , meter_point_label (_("pre"))
91         , comment_button (_("Comments"))
92 {
93         init ();
94
95         if (!_mixer_owned) {
96                 /* the editor mixer strip: don't destroy it every time
97                    the underlying route goes away.
98                 */
99
100                 self_destruct = false;
101         }
102 }
103
104 MixerStrip::MixerStrip (Mixer_UI& mx, Session* sess, boost::shared_ptr<Route> rt, bool in_mixer)
105         : AxisView(sess)
106         , RouteUI (sess)
107         , _mixer(mx)
108         , _mixer_owned (in_mixer)
109         , processor_box (sess, sigc::mem_fun(*this, &MixerStrip::plugin_selector), mx.selection(), this, in_mixer)
110         , gpm (sess, 250)
111         , panners (sess)
112         , _mono_button (_("Mono"))
113         , button_table (3, 2)
114         , middle_button_table (1, 2)
115         , bottom_button_table (1, 2)
116         , meter_point_label (_("pre"))
117         , comment_button (_("Comments"))
118 {
119         init ();
120         set_route (rt);
121 }
122
123 void
124 MixerStrip::init ()
125 {
126         input_selector = 0;
127         output_selector = 0;
128         group_menu = 0;
129         _marked_for_display = false;
130         route_ops_menu = 0;
131         ignore_comment_edit = false;
132         ignore_toggle = false;
133         comment_window = 0;
134         comment_area = 0;
135         _width_owner = 0;
136         spacer = 0;
137
138         /* the length of this string determines the width of the mixer strip when it is set to `wide' */
139         longest_label = "longest label";
140
141         Gtk::Image* img;
142
143         img = manage (new Gtk::Image (::get_icon("strip_width")));
144         img->show ();
145
146         width_button.add (*img);
147
148         img = manage (new Gtk::Image (::get_icon("hide")));
149         img->show ();
150
151         hide_button.add (*img);
152
153         input_label.set_text (_("Input"));
154         ARDOUR_UI::instance()->set_tip (&input_button, _("Button 1 to choose inputs from a port matrix, button 3 to select inputs from a menu"), "");
155         input_button.add (input_label);
156         input_button.set_name ("MixerIOButton");
157         input_label.set_name ("MixerIOButtonLabel");
158
159         Gtkmm2ext::set_size_request_to_display_given_text (input_button, longest_label.c_str(), 4, 4);
160
161         output_label.set_text (_("Output"));
162         ARDOUR_UI::instance()->set_tip (&output_button, _("Button 1 to choose outputs from a port matrix, button 3 to select inputs from a menu"), "");
163         output_button.add (output_label);
164         output_button.set_name ("MixerIOButton");
165         output_label.set_name ("MixerIOButtonLabel");
166         Gtkmm2ext::set_size_request_to_display_given_text (output_button, longest_label.c_str(), 4, 4);
167
168         ARDOUR_UI::instance()->set_tip (&meter_point_button, _("Select metering point"), "");
169         meter_point_button.add (meter_point_label);
170         meter_point_button.set_name ("MixerStripMeterPreButton");
171         meter_point_label.set_name ("MixerStripMeterPreButton");
172
173         /* TRANSLATORS: this string should be longest of the strings
174            used to describe meter points. In english, it's "input".
175         */
176         set_size_request_to_display_given_text (meter_point_button, _("tupni"), 5, 5);
177
178         bottom_button_table.attach (meter_point_button, 1, 2, 0, 1);
179
180         meter_point_button.signal_button_press_event().connect (sigc::mem_fun (gpm, &GainMeter::meter_press), false);
181         meter_point_button.signal_button_release_event().connect (sigc::mem_fun (gpm, &GainMeter::meter_release), false);
182
183         hide_button.set_events (hide_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
184
185         mute_button->set_name ("MixerMuteButton");
186         solo_button->set_name ("MixerSoloButton");
187
188         solo_isolated_led = manage (new LED);
189         solo_isolated_led->show ();
190         solo_isolated_led->set_diameter (6);
191         solo_isolated_led->set_no_show_all (true);
192         solo_isolated_led->set_name (X_("SoloIsolatedLED"));
193         solo_isolated_led->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
194         solo_isolated_led->signal_button_release_event().connect (sigc::mem_fun (*this, &RouteUI::solo_isolate_button_release));
195         UI::instance()->set_tip (solo_isolated_led, _("Isolate Solo"), "");
196
197         solo_safe_led = manage (new LED);
198         solo_safe_led->show ();
199         solo_safe_led->set_diameter (6);
200         solo_safe_led->set_no_show_all (true);
201         solo_safe_led->set_name (X_("SoloSafeLED"));
202         solo_safe_led->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
203         solo_safe_led->signal_button_release_event().connect (sigc::mem_fun (*this, &RouteUI::solo_safe_button_release));
204         UI::instance()->set_tip (solo_safe_led, _("Lock Solo Status"), "");
205
206         Label* iso_label = manage (new Label (_("iso")));
207         Label* safe_label = manage (new Label (_("lock")));
208         
209         iso_label->set_name (X_("SoloLEDLabel"));
210         safe_label->set_name (X_("SoloLEDLabel"));
211
212         iso_label->show ();
213         safe_label->show (); 
214
215         solo_led_table.set_spacings (0);
216         solo_led_table.set_border_width (1);
217         solo_led_table.attach (*iso_label, 0, 1, 0, 1, Gtk::FILL, Gtk::FILL);
218         solo_led_table.attach (*solo_isolated_led, 1, 2, 0, 1, Gtk::FILL, Gtk::FILL);
219         solo_led_table.attach (*safe_label, 0, 1, 1, 2, Gtk::FILL, Gtk::FILL);
220         solo_led_table.attach (*solo_safe_led, 1, 2, 1, 2, Gtk::FILL, Gtk::FILL);
221
222         solo_led_table.show ();
223         solo_led_box.pack_end (solo_led_table, false, false);
224         solo_led_box.show ();
225
226         button_table.set_homogeneous (true);
227         button_table.set_spacings (0);
228
229         button_table.attach (name_button, 0, 2, 0, 1);
230         button_table.attach (input_button, 0, 2, 1, 2);
231         button_table.attach (_invert_button_box, 0, 2, 3, 4);
232
233         middle_button_table.set_homogeneous (true);
234         middle_button_table.set_spacings (0);
235         middle_button_table.attach (*mute_button, 0, 1, 0, 1);
236         middle_button_table.attach (*solo_button, 1, 2, 0, 1);
237
238         bottom_button_table.set_col_spacings (0);
239         bottom_button_table.set_homogeneous (true);
240         bottom_button_table.attach (group_button, 0, 1, 0, 1);
241
242         name_button.add (name_label);
243         name_button.set_name ("MixerNameButton");
244         Gtkmm2ext::set_size_request_to_display_given_text (name_button, longest_label.c_str(), 2, 2);
245
246         name_label.set_name ("MixerNameButtonLabel");
247         ARDOUR_UI::instance()->set_tip (&group_button, _("Mix group"), "");
248         group_button.add (group_label);
249         group_button.set_name ("MixerGroupButton");
250         Gtkmm2ext::set_size_request_to_display_given_text (group_button, "Group", 2, 2);
251         group_label.set_name ("MixerGroupButtonLabel");
252
253         comment_button.set_name ("MixerCommentButton");
254         comment_button.signal_clicked().connect (sigc::mem_fun(*this, &MixerStrip::comment_button_clicked));
255
256         _mono_button.set_name ("MixerMonoButton");
257         _mono_button.signal_clicked().connect (sigc::mem_fun (*this, &MixerStrip::mono_button_clicked));
258
259         global_vpacker.set_border_width (0);
260         global_vpacker.set_spacing (0);
261
262         width_button.set_name ("MixerWidthButton");
263         hide_button.set_name ("MixerHideButton");
264         top_event_box.set_name ("MixerTopEventBox");
265
266         width_button.signal_clicked().connect (sigc::mem_fun(*this, &MixerStrip::width_clicked));
267         hide_button.signal_clicked().connect (sigc::mem_fun(*this, &MixerStrip::hide_clicked));
268
269         width_hide_box.pack_start (width_button, false, true);
270         width_hide_box.pack_start (top_event_box, true, true);
271         width_hide_box.pack_end (hide_button, false, true);
272         gain_meter_alignment.set_padding(0, 4, 0, 0);
273         gain_meter_alignment.add(gpm);
274
275         whvbox.pack_start (width_hide_box, true, true);
276
277         global_vpacker.pack_start (whvbox, Gtk::PACK_SHRINK);
278         global_vpacker.pack_start (button_table,Gtk::PACK_SHRINK);
279         global_vpacker.pack_start (processor_box, true, true);
280         global_vpacker.pack_start (panners, Gtk::PACK_SHRINK);
281         global_vpacker.pack_start (solo_led_box,Gtk::PACK_SHRINK);
282         global_vpacker.pack_start (middle_button_table,Gtk::PACK_SHRINK);
283         global_vpacker.pack_start (gain_meter_alignment,Gtk::PACK_SHRINK);
284         global_vpacker.pack_start (bottom_button_table,Gtk::PACK_SHRINK);
285         global_vpacker.pack_start (_mono_button, Gtk::PACK_SHRINK);
286         global_vpacker.pack_start (output_button, Gtk::PACK_SHRINK);
287         global_vpacker.pack_start (comment_button, Gtk::PACK_SHRINK);
288
289         global_frame.add (global_vpacker);
290         global_frame.set_shadow_type (Gtk::SHADOW_IN);
291         global_frame.set_name ("BaseFrame");
292
293         add (global_frame);
294
295         /* force setting of visible selected status */
296
297         _selected = true;
298         set_selected (false);
299
300         _packed = false;
301         _embedded = false;
302
303         _session->engine().Stopped.connect (*this, invalidator (*this), boost::bind (&MixerStrip::engine_stopped, this), gui_context());
304         _session->engine().Running.connect (*this, invalidator (*this), boost::bind (&MixerStrip::engine_running, this), gui_context());
305
306         input_button.signal_button_press_event().connect (sigc::mem_fun(*this, &MixerStrip::input_press), false);
307         output_button.signal_button_press_event().connect (sigc::mem_fun(*this, &MixerStrip::output_press), false);
308
309         /* we don't need this if its not an audio track, but we don't know that yet and it doesn't
310            hurt (much).
311         */
312
313         rec_enable_button->set_name ("MixerRecordEnableButton");
314
315         /* ditto for this button and busses */
316
317         name_button.signal_button_press_event().connect (sigc::mem_fun(*this, &MixerStrip::name_button_button_press), false);
318         group_button.signal_button_press_event().connect (sigc::mem_fun(*this, &MixerStrip::select_route_group), false);
319
320         _width = (Width) -1;
321
322         /* start off as a passthru strip. we'll correct this, if necessary,
323            in update_diskstream_display().
324         */
325
326         /* start off as a passthru strip. we'll correct this, if necessary,
327            in update_diskstream_display().
328         */
329
330         if (is_midi_track())
331                 set_name ("MidiTrackStripBase");
332         else
333                 set_name ("AudioTrackStripBase");
334
335         add_events (Gdk::BUTTON_RELEASE_MASK|
336                     Gdk::ENTER_NOTIFY_MASK|
337                     Gdk::LEAVE_NOTIFY_MASK|
338                     Gdk::KEY_PRESS_MASK|
339                     Gdk::KEY_RELEASE_MASK);
340
341         set_flags (get_flags() | Gtk::CAN_FOCUS);
342
343         SwitchIO.connect (sigc::mem_fun (*this, &MixerStrip::switch_io));
344
345         AudioEngine::instance()->PortConnectedOrDisconnected.connect (
346                 *this, invalidator (*this), boost::bind (&MixerStrip::port_connected_or_disconnected, this, _1, _2), gui_context ()
347                 );
348 }
349
350 MixerStrip::~MixerStrip ()
351 {
352         CatchDeletion (this);
353
354         delete input_selector;
355         delete output_selector;
356         delete comment_window;
357 }
358
359 void
360 MixerStrip::set_route (boost::shared_ptr<Route> rt)
361 {
362         if (rec_enable_button->get_parent()) {
363                 button_table.remove (*rec_enable_button);
364         }
365
366         if (show_sends_button->get_parent()) {
367                 button_table.remove (*show_sends_button);
368         }
369
370         processor_box.set_route (rt);
371
372         RouteUI::set_route (rt);
373
374         /* map the current state */
375
376         mute_changed (0);
377         update_solo_display ();
378
379         delete input_selector;
380         input_selector = 0;
381
382         delete output_selector;
383         output_selector = 0;
384
385         revert_to_default_display ();
386
387         if (set_color_from_route()) {
388                 set_color (unique_random_color());
389         }
390
391         if (route()->is_master()) {
392                 solo_button->hide ();
393                 solo_led_box.hide ();
394         } else {
395                 solo_button->show ();
396                 solo_led_box.show ();
397         }
398
399         if (_mixer_owned && (route()->is_master() || route()->is_monitor())) {
400
401                 if (scrollbar_height == 0) {
402                         HScrollbar scrollbar;
403                         Gtk::Requisition requisition(scrollbar.size_request ());
404                         scrollbar_height = requisition.height;
405                 }
406
407                 spacer = manage (new EventBox);
408                 spacer->set_size_request (-1, scrollbar_height);
409                 global_vpacker.pack_start (*spacer, false, false);
410         }
411
412         if (is_audio_track()) {
413                 boost::shared_ptr<AudioTrack> at = audio_track();
414                 at->FreezeChange.connect (route_connections, invalidator (*this), boost::bind (&MixerStrip::map_frozen, this), gui_context());
415         }
416
417         if (has_audio_outputs ()) {
418                 _mono_button.show ();
419                 panners.show_all ();
420         } else {
421                 _mono_button.hide ();
422                 panners.hide_all ();
423         }
424
425         if (is_track ()) {
426                 
427                 button_table.attach (*rec_enable_button, 0, 2, 2, 3);
428                 rec_enable_button->set_sensitive (_session->writable());
429                 rec_enable_button->show();
430
431         } else {
432                 
433                 /* non-master bus */
434
435                 if (!_route->is_master()) {
436                         button_table.attach (*show_sends_button, 0, 2, 2, 3);
437                         show_sends_button->show();
438                 }
439         }
440
441         switch (_route->meter_point()) {
442         case MeterInput:
443                 meter_point_label.set_text (_("input"));
444                 break;
445
446         case MeterPreFader:
447                 meter_point_label.set_text (_("pre"));
448                 break;
449
450         case MeterPostFader:
451                 meter_point_label.set_text (_("post"));
452                 break;
453
454         case MeterCustom:
455                 meter_point_label.set_text (_("custom"));
456                 break;
457         }
458
459         delete route_ops_menu;
460         route_ops_menu = 0;
461
462         ARDOUR_UI::instance()->set_tip (comment_button, _route->comment().empty() ?
463                                                    _("Click to Add/Edit Comments"):
464                                                    _route->comment());
465
466         _route->meter_change.connect (route_connections, invalidator (*this), bind (&MixerStrip::meter_changed, this), gui_context());
467         _route->route_group_changed.connect (route_connections, invalidator (*this), boost::bind (&MixerStrip::route_group_changed, this), gui_context());
468
469         if (_route->panner()) {
470                 _route->panner()->Changed.connect (route_connections, invalidator (*this), boost::bind (&MixerStrip::connect_to_pan, this), gui_context());
471         }
472
473         if (is_audio_track()) {
474                 audio_track()->DiskstreamChanged.connect (route_connections, invalidator (*this), boost::bind (&MixerStrip::diskstream_changed, this), gui_context());
475         }
476
477         _route->comment_changed.connect (route_connections, invalidator (*this), ui_bind (&MixerStrip::comment_changed, this, _1), gui_context());
478         _route->gui_changed.connect (route_connections, invalidator (*this), ui_bind (&MixerStrip::route_gui_changed, this, _1, _2), gui_context());
479
480         set_stuff_from_route ();
481
482         /* now force an update of all the various elements */
483
484         mute_changed (0);
485         update_solo_display ();
486         name_changed ();
487         comment_changed (0);
488         route_group_changed ();
489
490         connect_to_pan ();
491
492         panners.setup_pan ();
493
494         update_diskstream_display ();
495         update_input_display ();
496         update_output_display ();
497
498         add_events (Gdk::BUTTON_RELEASE_MASK);
499
500         processor_box.show();
501
502         if (!route()->is_master() && !route()->is_monitor()) {
503                 /* we don't allow master or control routes to be hidden */
504                 hide_button.show();
505         }
506
507         width_button.show();
508         width_hide_box.show();
509         whvbox.show ();
510         global_frame.show();
511         global_vpacker.show();
512         button_table.show();
513         middle_button_table.show();
514         bottom_button_table.show();
515         processor_box.show_all ();
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<ARDOUR::AutomationControl> pan_control
971                 = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(
972                                 _route->panner()->control(Evoral::Parameter(PanAutomation)));
973
974         if (pan_control) {
975                 pan_control->alist()->automation_state_changed.connect (panstate_connection, invalidator (*this), boost::bind (&PannerUI::pan_automation_state_changed, &panners), gui_context());
976                 pan_control->alist()->automation_style_changed.connect (panstyle_connection, invalidator (*this), boost::bind (&PannerUI::pan_automation_style_changed, &panners), gui_context());
977         }
978
979         panners.panner_changed (this);
980 }
981
982
983 /*
984  * Output port labelling
985  * =====================
986  *
987  * Case 1: Each output has one connection, all connections are to system:playback_%i
988  *   out 1 -> system:playback_1
989  *   out 2 -> system:playback_2
990  *   out 3 -> system:playback_3
991  *   Display as: 1/2/3
992  *
993  * Case 2: Each output has one connection, all connections are to ardour:track_x/in 1
994  *   out 1 -> ardour:track_x/in 1
995  *   out 2 -> ardour:track_x/in 2
996  *   Display as: track_x
997  *
998  * Case 3: Each output has one connection, all connections are to Jack client "program x"
999  *   out 1 -> program x:foo
1000  *   out 2 -> program x:foo
1001  *   Display as: program x
1002  *
1003  * Case 4: No connections (Disconnected)
1004  *   Display as: -
1005  *
1006  * Default case (unusual routing):
1007  *   Display as: *number of connections*
1008  *
1009  * Tooltips
1010  * ========
1011  * .-----------------------------------------------.
1012  * | Mixdown                                       |
1013  * | out 1 -> ardour:master/in 1, jamin:input/in 1 |
1014  * | out 2 -> ardour:master/in 2, jamin:input/in 2 |
1015  * '-----------------------------------------------'
1016  * .-----------------------------------------------.
1017  * | Guitar SM58                                   |
1018  * | Disconnected                                  |
1019  * '-----------------------------------------------'
1020  */
1021
1022 void
1023 MixerStrip::update_io_button (boost::shared_ptr<ARDOUR::Route> route, Width width, bool for_input)
1024 {
1025         uint32_t io_count;
1026         uint32_t io_index;
1027         Port *port;
1028         vector<string> port_connections;
1029
1030         uint32_t total_connection_count = 0;
1031         uint32_t io_connection_count = 0;
1032         uint32_t ardour_connection_count = 0;
1033         uint32_t system_connection_count = 0;
1034         uint32_t other_connection_count = 0;
1035
1036         ostringstream label;
1037         string label_string;
1038
1039         bool have_label = false;
1040         bool each_io_has_one_connection = true;
1041
1042         string connection_name;
1043         string ardour_track_name;
1044         string other_connection_type;
1045         string system_ports;
1046         string system_port;
1047
1048         ostringstream tooltip;
1049         char * tooltip_cstr;
1050
1051         tooltip << route->name();
1052
1053         if (for_input) {
1054                 io_count = route->n_inputs().n_total();
1055         } else {
1056                 io_count = route->n_outputs().n_total();
1057         }
1058
1059         for (io_index = 0; io_index < io_count; ++io_index) {
1060                 if (for_input) {
1061                         port = route->input()->nth (io_index);
1062                 } else {
1063                         port = route->output()->nth (io_index);
1064                 }
1065                 
1066                 port_connections.clear ();
1067                 port->get_connections(port_connections);
1068                 io_connection_count = 0;
1069
1070                 if (!port_connections.empty()) {
1071                         for (vector<string>::iterator i = port_connections.begin(); i != port_connections.end(); ++i) {
1072                                 string& connection_name (*i);
1073
1074                                 if (io_connection_count == 0) {
1075                                         tooltip << endl << port->name().substr(port->name().find("/") + 1) << " -> " << connection_name;
1076                                 } else {
1077                                         tooltip << ", " << connection_name;
1078                                 }
1079
1080                                 if (connection_name.find("ardour:") == 0) {
1081                                         if (ardour_track_name.empty()) {
1082                                                 // "ardour:Master/in 1" -> "ardour:Master/"
1083                                                 string::size_type slash = connection_name.find("/");
1084                                                 if (slash != string::npos) {
1085                                                         ardour_track_name = connection_name.substr(0, slash + 1);
1086                                                 }
1087                                         }
1088
1089                                         if (connection_name.find(ardour_track_name) == 0) {
1090                                                 ++ardour_connection_count;
1091                                         }
1092                                 } else if (connection_name.find("system:") == 0) {
1093                                         if (for_input) {
1094                                                 // "system:capture_123" -> "123"
1095                                                 system_port = connection_name.substr(15);
1096                                         } else {
1097                                                 // "system:playback_123" -> "123"
1098                                                 system_port = connection_name.substr(16);
1099                                         }
1100
1101                                         if (system_ports.empty()) {
1102                                                 system_ports += system_port;
1103                                         } else {
1104                                                 system_ports += "/" + system_port;
1105                                         }
1106
1107                                         ++system_connection_count;
1108                                 } else {
1109                                         if (other_connection_type.empty()) {
1110                                                 // "jamin:in 1" -> "jamin:"
1111                                                 other_connection_type = connection_name.substr(0, connection_name.find(":") + 1);
1112                                         }
1113
1114                                         if (connection_name.find(other_connection_type) == 0) {
1115                                                 ++other_connection_count;
1116                                         }
1117                                 }
1118
1119                                 ++total_connection_count;
1120                                 ++io_connection_count;
1121                         }
1122                 }
1123
1124                 if (io_connection_count != 1) {
1125                         each_io_has_one_connection = false;
1126                 }
1127         }
1128
1129         if (total_connection_count == 0) {
1130                 tooltip << endl << _("Disconnected");
1131         }
1132
1133         tooltip_cstr = new char[tooltip.str().size() + 1];
1134         strcpy(tooltip_cstr, tooltip.str().c_str());
1135
1136         if (for_input) {
1137                 ARDOUR_UI::instance()->set_tip (&input_button, tooltip_cstr, "");
1138         } else {
1139                 ARDOUR_UI::instance()->set_tip (&output_button, tooltip_cstr, "");
1140         }
1141
1142         if (each_io_has_one_connection) {
1143                 if ((total_connection_count == ardour_connection_count)) {
1144                         // all connections are to the same track in ardour
1145                         // "ardour:Master/" -> "Master"
1146                         string::size_type slash = ardour_track_name.find("/");
1147                         if (slash != string::npos) {
1148                                 label << ardour_track_name.substr(7, slash - 7);
1149                                 have_label = true;
1150                         }
1151                 }
1152                 else if (total_connection_count == system_connection_count) {
1153                         // all connections are to system ports
1154                         label << system_ports;
1155                         have_label = true;
1156                 }
1157                 else if (total_connection_count == other_connection_count) {
1158                         // all connections are to the same external program eg jamin
1159                         // "jamin:" -> "jamin"
1160                         label << other_connection_type.substr(0, other_connection_type.size() - 1);
1161                         have_label = true;
1162                 }
1163         }
1164
1165         if (!have_label) {
1166                 if (total_connection_count == 0) {
1167                         // Disconnected
1168                         label << "-";
1169                 } else {
1170                         // Odd configuration
1171                         label << "*" << total_connection_count << "*";
1172                 }
1173         }
1174
1175         switch (width) {
1176         case Wide:
1177                 label_string = label.str().substr(0, 6);
1178                 break;
1179         case Narrow:
1180                 label_string = label.str().substr(0, 3);
1181                 break;
1182         }
1183
1184         if (for_input) {
1185                 input_label.set_text (label_string);
1186         } else {
1187                 output_label.set_text (label_string);
1188         }
1189 }
1190
1191 void
1192 MixerStrip::update_input_display ()
1193 {
1194         update_io_button (_route, _width, true);
1195         panners.setup_pan ();
1196 }
1197
1198 void
1199 MixerStrip::update_output_display ()
1200 {
1201         update_io_button (_route, _width, false);
1202         gpm.setup_meters ();
1203         panners.setup_pan ();
1204 }
1205
1206 void
1207 MixerStrip::fast_update ()
1208 {
1209         gpm.update_meters ();
1210 }
1211
1212 void
1213 MixerStrip::diskstream_changed ()
1214 {
1215         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&MixerStrip::update_diskstream_display, this));
1216 }
1217
1218 void
1219 MixerStrip::port_connected_or_disconnected (Port* a, Port* b)
1220 {
1221         if (_route->input()->has_port (a) || _route->input()->has_port (b)) {
1222                 update_input_display ();
1223                 set_width_enum (_width, this);
1224         }
1225
1226         if (_route->output()->has_port (a) || _route->output()->has_port (b)) {
1227                 update_output_display ();
1228                 set_width_enum (_width, this);
1229         }
1230 }
1231
1232 void
1233 MixerStrip::comment_editor_done_editing()
1234 {
1235         string str =  comment_area->get_buffer()->get_text();
1236         if (_route->comment() != str) {
1237                 _route->set_comment (str, this);
1238
1239                 switch (_width) {
1240
1241                 case Wide:
1242                         if (! str.empty()) {
1243                                 comment_button.modify_bg (STATE_NORMAL, color());
1244                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("*Comments*"));
1245                         } else {
1246                                 comment_button.unset_bg (STATE_NORMAL);
1247                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("Comments"));
1248                         }
1249                         break;
1250
1251                 case Narrow:
1252                         if (! str.empty()) {
1253                                 comment_button.modify_bg (STATE_NORMAL, color());
1254                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("*Cmt*"));
1255                         } else {
1256                                 comment_button.unset_bg (STATE_NORMAL);
1257                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("Cmt"));
1258                         }
1259                         break;
1260                 }
1261
1262                 ARDOUR_UI::instance()->set_tip (comment_button,
1263                                 str.empty() ? _("Click to Add/Edit Comments") : str);
1264         }
1265
1266 }
1267
1268 void
1269 MixerStrip::comment_button_clicked ()
1270 {
1271         if (comment_window == 0) {
1272                 setup_comment_editor ();
1273         }
1274
1275     int x, y, cw_width, cw_height;
1276
1277         if (comment_window->is_visible()) {
1278                 comment_window->hide ();
1279                 return;
1280         }
1281
1282         comment_window->get_size (cw_width, cw_height);
1283         comment_window->get_position(x, y);
1284         comment_window->move(x, y - (cw_height / 2) - 45);
1285         /*
1286            half the dialog height minus the comments button height
1287            with some window decoration fudge thrown in.
1288         */
1289
1290         comment_window->show();
1291         comment_window->present();
1292 }
1293
1294 void
1295 MixerStrip::setup_comment_editor ()
1296 {
1297         string title;
1298         title = _route->name();
1299         title += _(": comment editor");
1300
1301         comment_window = new ArdourDialog (title, false);
1302         comment_window->set_position (Gtk::WIN_POS_MOUSE);
1303         comment_window->set_skip_taskbar_hint (true);
1304         comment_window->signal_hide().connect (sigc::mem_fun(*this, &MixerStrip::comment_editor_done_editing));
1305
1306         comment_area = manage (new TextView());
1307         comment_area->set_name ("MixerTrackCommentArea");
1308         comment_area->set_size_request (110, 178);
1309         comment_area->set_wrap_mode (WRAP_WORD);
1310         comment_area->set_editable (true);
1311         comment_area->get_buffer()->set_text (_route->comment());
1312         comment_area->show ();
1313
1314         comment_window->get_vbox()->pack_start (*comment_area);
1315         comment_window->get_action_area()->hide();
1316 }
1317
1318 void
1319 MixerStrip::comment_changed (void *src)
1320 {
1321         ENSURE_GUI_THREAD (*this, &MixerStrip::comment_changed, src)
1322
1323         if (src != this) {
1324                 ignore_comment_edit = true;
1325                 if (comment_area) {
1326                         comment_area->get_buffer()->set_text (_route->comment());
1327                 }
1328                 ignore_comment_edit = false;
1329         }
1330 }
1331
1332 void
1333 MixerStrip::set_route_group (RouteGroup *rg)
1334 {
1335         rg->add (_route);
1336 }
1337
1338 bool
1339 MixerStrip::select_route_group (GdkEventButton *ev)
1340 {
1341         using namespace Menu_Helpers;
1342
1343         if (ev->button == 1) {
1344
1345                 if (group_menu == 0) {
1346
1347                         PropertyList* plist = new PropertyList();
1348
1349                         plist->add (Properties::gain, true);
1350                         plist->add (Properties::mute, true);
1351                         plist->add (Properties::solo, true);
1352
1353                         group_menu = new RouteGroupMenu (_session, plist);
1354                         group_menu->GroupSelected.connect (sigc::mem_fun (*this, &MixerStrip::set_route_group));
1355                 }
1356
1357                 group_menu->popup (1, ev->time);
1358         }
1359
1360         return true;
1361 }
1362
1363 void
1364 MixerStrip::route_group_changed ()
1365 {
1366         ENSURE_GUI_THREAD (*this, &MixerStrip::route_group_changed)
1367
1368         RouteGroup *rg = _route->route_group();
1369
1370         if (rg) {
1371                 group_label.set_text (PBD::short_version (rg->name(), 5));
1372         } else {
1373                 switch (_width) {
1374                 case Wide:
1375                         group_label.set_text (_("Grp"));
1376                         break;
1377                 case Narrow:
1378                         group_label.set_text (_("~G"));
1379                         break;
1380                 }
1381         }
1382 }
1383
1384
1385 void
1386 MixerStrip::route_gui_changed (string what_changed, void*)
1387 {
1388         ENSURE_GUI_THREAD (*this, &MixerStrip::route_gui_changed, what_changed, ignored)
1389
1390         if (what_changed == "color") {
1391                 if (set_color_from_route () == 0) {
1392                         show_route_color ();
1393                 }
1394         }
1395 }
1396
1397 void
1398 MixerStrip::show_route_color ()
1399 {
1400         name_button.modify_bg (STATE_NORMAL, color());
1401         top_event_box.modify_bg (STATE_NORMAL, color());
1402         route_active_changed ();
1403 }
1404
1405 void
1406 MixerStrip::show_passthru_color ()
1407 {
1408         route_active_changed ();
1409 }
1410
1411 void
1412 MixerStrip::build_route_ops_menu ()
1413 {
1414         using namespace Menu_Helpers;
1415         route_ops_menu = new Menu;
1416         route_ops_menu->set_name ("ArdourContextMenu");
1417
1418         MenuList& items = route_ops_menu->items();
1419
1420         items.push_back (MenuElem (_("Save As Template..."), sigc::mem_fun(*this, &RouteUI::save_as_template)));
1421         items.push_back (MenuElem (_("Rename..."), sigc::mem_fun(*this, &RouteUI::route_rename)));
1422         rename_menu_item = &items.back();
1423         items.push_back (SeparatorElem());
1424         items.push_back (CheckMenuElem (_("Active"), sigc::mem_fun (*this, &RouteUI::toggle_route_active)));
1425         route_active_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
1426         route_active_menu_item->set_active (_route->active());
1427
1428         items.push_back (SeparatorElem());
1429
1430         items.push_back (MenuElem (_("Adjust Latency..."), sigc::mem_fun (*this, &RouteUI::adjust_latency)));
1431
1432         items.push_back (SeparatorElem());
1433         items.push_back (CheckMenuElem (_("Protect Against Denormals"), sigc::mem_fun (*this, &RouteUI::toggle_denormal_protection)));
1434         denormal_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
1435         denormal_menu_item->set_active (_route->denormal_protection());
1436
1437         if (!Profile->get_sae()) {
1438                 items.push_back (SeparatorElem());
1439                 items.push_back (MenuElem (_("Remote Control ID..."), sigc::mem_fun (*this, &RouteUI::open_remote_control_id_dialog)));
1440         }
1441
1442         items.push_back (SeparatorElem());
1443         items.push_back (MenuElem (_("Remove"), sigc::mem_fun(*this, &RouteUI::remove_this_route)));
1444 }
1445
1446 gboolean
1447 MixerStrip::name_button_button_press (GdkEventButton* ev)
1448 {
1449         if (ev->button == 3) {
1450                 list_route_operations ();
1451
1452                 /* do not allow rename if the track is record-enabled */
1453                 rename_menu_item->set_sensitive (!_route->record_enabled());
1454                 route_ops_menu->popup (1, ev->time);
1455
1456         } else if (ev->button == 1) {
1457                 revert_to_default_display ();
1458         }
1459
1460         return false;
1461 }
1462
1463 void
1464 MixerStrip::list_route_operations ()
1465 {
1466         if (route_ops_menu == 0) {
1467                 build_route_ops_menu ();
1468         }
1469 }
1470
1471 void
1472 MixerStrip::set_selected (bool yn)
1473 {
1474         AxisView::set_selected (yn);
1475         if (_selected) {
1476                 global_frame.set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
1477                 global_frame.set_name ("MixerStripSelectedFrame");
1478         } else {
1479                 global_frame.set_shadow_type (Gtk::SHADOW_IN);
1480                 global_frame.set_name ("MixerStripFrame");
1481         }
1482         global_frame.queue_draw ();
1483 }
1484
1485 void
1486 MixerStrip::name_changed ()
1487 {
1488         switch (_width) {
1489         case Wide:
1490                 RouteUI::property_changed (PropertyChange (ARDOUR::Properties::name));
1491                 break;
1492         case Narrow:
1493                 name_label.set_text (PBD::short_version (_route->name(), 5));
1494                 break;
1495         }
1496 }
1497
1498 void
1499 MixerStrip::width_clicked ()
1500 {
1501         switch (_width) {
1502         case Wide:
1503                 set_width_enum (Narrow, this);
1504                 break;
1505         case Narrow:
1506                 set_width_enum (Wide, this);
1507                 break;
1508         }
1509 }
1510
1511 void
1512 MixerStrip::hide_clicked ()
1513 {
1514         // LAME fix to reset the button status for when it is redisplayed (part 1)
1515         hide_button.set_sensitive(false);
1516
1517         if (_embedded) {
1518                 Hiding(); /* EMIT_SIGNAL */
1519         } else {
1520                 _mixer.hide_strip (this);
1521         }
1522
1523         // (part 2)
1524         hide_button.set_sensitive(true);
1525 }
1526
1527 void
1528 MixerStrip::set_embedded (bool yn)
1529 {
1530         _embedded = yn;
1531 }
1532
1533 void
1534 MixerStrip::map_frozen ()
1535 {
1536         ENSURE_GUI_THREAD (*this, &MixerStrip::map_frozen)
1537
1538         boost::shared_ptr<AudioTrack> at = audio_track();
1539
1540         if (at) {
1541                 switch (at->freeze_state()) {
1542                 case AudioTrack::Frozen:
1543                         processor_box.set_sensitive (false);
1544                         hide_redirect_editors ();
1545                         break;
1546                 default:
1547                         processor_box.set_sensitive (true);
1548                         // XXX need some way, maybe, to retoggle redirect editors
1549                         break;
1550                 }
1551         }
1552 }
1553
1554 void
1555 MixerStrip::hide_redirect_editors ()
1556 {
1557         _route->foreach_processor (sigc::mem_fun (*this, &MixerStrip::hide_processor_editor));
1558 }
1559
1560 void
1561 MixerStrip::hide_processor_editor (boost::weak_ptr<Processor> p)
1562 {
1563         boost::shared_ptr<Processor> processor (p.lock ());
1564         if (!processor) {
1565                 return;
1566         }
1567
1568         Gtk::Window* w = processor_box.get_processor_ui (processor);
1569
1570         if (w) {
1571                 w->hide ();
1572         }
1573 }
1574
1575 void
1576 MixerStrip::route_active_changed ()
1577 {
1578         RouteUI::route_active_changed ();
1579         reset_strip_style ();
1580 }
1581
1582 void
1583 MixerStrip::reset_strip_style ()
1584 {
1585         if (_current_delivery && boost::dynamic_pointer_cast<Send>(_current_delivery)) {
1586
1587                 gpm.set_fader_name ("SendStripBase");
1588
1589         } else {
1590                 
1591                 if (is_midi_track()) {
1592                         if (_route->active()) {
1593                                 set_name ("MidiTrackStripBase");
1594                                 gpm.set_meter_strip_name ("MidiTrackStripBase");
1595                         } else {
1596                                 set_name ("MidiTrackStripBaseInactive");
1597                                 gpm.set_meter_strip_name ("MidiTrackStripBaseInactive");
1598                         }
1599                         gpm.set_fader_name ("MidiTrackFader");
1600                 } else if (is_audio_track()) {
1601                         if (_route->active()) {
1602                                 set_name ("AudioTrackStripBase");
1603                                 gpm.set_meter_strip_name ("AudioTrackMetrics");
1604                         } else {
1605                                 set_name ("AudioTrackStripBaseInactive");
1606                                 gpm.set_meter_strip_name ("AudioTrackMetricsInactive");
1607                         }
1608                         gpm.set_fader_name ("AudioTrackFader");
1609                 } else {
1610                         if (_route->active()) {
1611                                 set_name ("AudioBusStripBase");
1612                                 gpm.set_meter_strip_name ("AudioBusMetrics");
1613                         } else {
1614                                 set_name ("AudioBusStripBaseInactive");
1615                                 gpm.set_meter_strip_name ("AudioBusMetricsInactive");
1616                         }
1617                         gpm.set_fader_name ("AudioBusFader");
1618                         
1619                         /* (no MIDI busses yet) */
1620                 }
1621         }
1622 }
1623
1624 RouteGroup*
1625 MixerStrip::route_group() const
1626 {
1627         return _route->route_group();
1628 }
1629
1630 void
1631 MixerStrip::engine_stopped ()
1632 {
1633 }
1634
1635 void
1636 MixerStrip::engine_running ()
1637 {
1638 }
1639
1640 /** Called when the metering point has changed */
1641 void
1642 MixerStrip::meter_changed ()
1643 {
1644         ENSURE_GUI_THREAD (*this, &MixerStrip::meter_changed)
1645
1646         switch (_route->meter_point()) {
1647         case MeterInput:
1648                 meter_point_label.set_text (_("input"));
1649                 break;
1650
1651         case MeterPreFader:
1652                 meter_point_label.set_text (_("pre"));
1653                 break;
1654
1655         case MeterPostFader:
1656                 meter_point_label.set_text (_("post"));
1657                 break;
1658
1659         case MeterCustom:
1660                 meter_point_label.set_text (_("custom"));
1661                 break;
1662         }
1663
1664         gpm.setup_meters ();
1665         // reset peak when meter point changes
1666         gpm.reset_peak_display();
1667 }
1668
1669 void
1670 MixerStrip::switch_io (boost::shared_ptr<Route> target)
1671 {
1672         /* don't respond to switch IO signal outside of the mixer window */
1673
1674         if (!_mixer_owned) {
1675                 return;
1676         }
1677
1678         if (_route == target || _route->is_master()) {
1679                 /* don't change the display for the target or the master bus */
1680                 return;
1681         } else if (!is_track() && show_sends_button) {
1682                 /* make sure our show sends button is inactive, and we no longer blink,
1683                    since we're not the target.
1684                 */
1685                 send_blink_connection.disconnect ();
1686                 show_sends_button->set_active (false);
1687                 show_sends_button->set_state (STATE_NORMAL);
1688         }
1689
1690         if (!target) {
1691                 /* switch back to default */
1692                 revert_to_default_display ();
1693                 return;
1694         }
1695
1696         boost::shared_ptr<Send> send = _route->internal_send_for (target);
1697
1698         if (send) {
1699                 show_send (send);
1700         } else {
1701                 revert_to_default_display ();
1702         }
1703 }
1704
1705 void
1706 MixerStrip::drop_send ()
1707 {
1708         boost::shared_ptr<Send> current_send;
1709
1710         if (_current_delivery && (current_send = boost::dynamic_pointer_cast<Send>(_current_delivery))) {
1711                 current_send->set_metering (false);
1712         }
1713
1714         send_gone_connection.disconnect ();
1715         input_button.set_sensitive (true);
1716         output_button.set_sensitive (true);
1717 }
1718
1719 void
1720 MixerStrip::show_send (boost::shared_ptr<Send> send)
1721 {
1722         assert (send != 0);
1723
1724         drop_send ();
1725
1726         _current_delivery = send;
1727
1728         send->set_metering (true);
1729         _current_delivery->DropReferences.connect (send_gone_connection, invalidator (*this), boost::bind (&MixerStrip::revert_to_default_display, this), gui_context());
1730
1731         gain_meter().set_controls (_route, send->meter(), send->amp());
1732         gain_meter().setup_meters ();
1733
1734         panner_ui().set_panner (_current_delivery->panner());
1735         panner_ui().setup_pan ();
1736
1737         input_button.set_sensitive (false);
1738
1739         if (boost::dynamic_pointer_cast<InternalSend>(send)) {
1740                 output_button.set_sensitive (false);
1741         }
1742
1743         reset_strip_style ();
1744 }
1745
1746 void
1747 MixerStrip::revert_to_default_display ()
1748 {
1749         if (show_sends_button) {
1750                 show_sends_button->set_active (false);
1751         }
1752
1753         drop_send ();
1754
1755         _current_delivery = _route->main_outs();
1756
1757         gain_meter().set_controls (_route, _route->shared_peak_meter(), _route->amp());
1758         gain_meter().setup_meters ();
1759
1760         panner_ui().set_panner (_route->main_outs()->panner());
1761         panner_ui().setup_pan ();
1762
1763         reset_strip_style ();
1764 }
1765
1766 void
1767 MixerStrip::set_button_names ()
1768 {
1769         switch (_width) {
1770         case Wide:
1771                 rec_enable_button_label.set_text (_("Rec"));
1772                 mute_button_label.set_text (_("Mute"));
1773                 if (_route && _route->solo_safe()) {
1774                         solo_button_label.set_text (X_("!"));
1775                 } else {
1776                         if (!Config->get_solo_control_is_listen_control()) {
1777                                 solo_button_label.set_text (_("Solo"));
1778                         } else {
1779                                 switch (Config->get_listen_position()) {
1780                                 case AfterFaderListen:
1781                                         solo_button_label.set_text (_("AFL"));
1782                                         break;
1783                                 case PreFaderListen:
1784                                         solo_button_label.set_text (_("PFL"));
1785                                         break;
1786                                 }
1787                         }
1788                 }
1789                 break;
1790
1791         default:
1792                 rec_enable_button_label.set_text (_("R"));
1793                 mute_button_label.set_text (_("M"));
1794                 if (_route && _route->solo_safe()) {
1795                         solo_button_label.set_text (X_("!"));
1796                         if (!Config->get_solo_control_is_listen_control()) {
1797                                 solo_button_label.set_text (_("S"));
1798                         } else {
1799                                 switch (Config->get_listen_position()) {
1800                                 case AfterFaderListen:
1801                                         solo_button_label.set_text (_("A"));
1802                                         break;
1803                                 case PreFaderListen:
1804                                         solo_button_label.set_text (_("P"));
1805                                         break;
1806                                 }
1807                         }
1808                 }
1809                 break;
1810
1811         }
1812 }
1813
1814 bool
1815 MixerStrip::on_key_press_event (GdkEventKey* ev)
1816 {
1817         GdkEventButton fake;
1818         fake.type = GDK_BUTTON_PRESS;
1819         fake.button = 1;
1820         fake.state = ev->state;
1821
1822         switch (ev->keyval) {
1823         case GDK_m:
1824                 mute_press (&fake);
1825                 return true;
1826                 break;
1827
1828         case GDK_s:
1829                 solo_press (&fake);
1830                 return true;
1831                 break;
1832
1833         case GDK_r:
1834                 cerr << "Stole that r\n";
1835                 rec_enable_press (&fake);
1836                 return true;
1837                 break;
1838
1839         case GDK_e:
1840                 show_sends_press (&fake);
1841                 return true;
1842                 break;
1843
1844         case GDK_g:
1845                 if (ev->state & Keyboard::PrimaryModifier) {
1846                         step_gain_down ();
1847                 } else {
1848                         step_gain_up ();
1849                 }
1850                 return true;
1851                 break;
1852
1853         case GDK_0:
1854                 if (_route) {
1855                         _route->set_gain (1.0, this);
1856                 }
1857                 return true;
1858
1859         default:
1860                 break;
1861         }
1862
1863         return false;
1864 }
1865
1866
1867 bool
1868 MixerStrip::on_key_release_event (GdkEventKey* ev)
1869 {
1870         GdkEventButton fake;
1871         fake.type = GDK_BUTTON_RELEASE;
1872         fake.button = 1;
1873         fake.state = ev->state;
1874
1875         switch (ev->keyval) {
1876         case GDK_m:
1877                 mute_release (&fake);
1878                 return true;
1879                 break;
1880
1881         case GDK_s:
1882                 solo_release (&fake);
1883                 return true;
1884                 break;
1885
1886         case GDK_r:
1887                 cerr << "Stole that r\n";
1888                 rec_enable_release (&fake);
1889                 return true;
1890                 break;
1891
1892         case GDK_e:
1893                 show_sends_release (&fake);
1894                 return true;
1895                 break;
1896
1897         case GDK_g:
1898                 return true;
1899                 break;
1900
1901         default:
1902                 break;
1903         }
1904
1905         return false;
1906 }
1907
1908 bool
1909 MixerStrip::on_enter_notify_event (GdkEventCrossing*)
1910 {
1911         Keyboard::magic_widget_grab_focus ();
1912
1913         if (!panners._bar_spinner_active) {
1914                 /* This next grab_focus() causes a focus-out event to be sent to, amongst
1915                  * other things, panner BarControllers.  When they receive it, they abort
1916                  * the use of any SpinButton that might be in use to change pan settings.
1917                  * Hence we have this horrific hack which stops the grab_focus () call
1918                  * happening if a bar spinner is active.
1919                  */
1920                 grab_focus ();
1921         }
1922         return false;
1923 }
1924
1925 bool
1926 MixerStrip::on_leave_notify_event (GdkEventCrossing* ev)
1927 {
1928         switch (ev->detail) {
1929         case GDK_NOTIFY_INFERIOR:
1930                 break;
1931         default:
1932                 Keyboard::magic_widget_drop_focus ();
1933         }
1934
1935         return false;
1936 }
1937
1938 void
1939 MixerStrip::mono_button_clicked ()
1940 {
1941         panners.set_mono (_mono_button.get_active ());
1942 }
1943
1944 PluginSelector*
1945 MixerStrip::plugin_selector()
1946 {
1947         return _mixer.plugin_selector();
1948 }