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