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