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