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