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