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