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