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