Ignore triple clicks for mute/solo/rec-enable (fixes long-standing insensitive to...
[ardour.git] / gtk2_ardour / mixer_strip.cc
1 /*
2     Copyright (C) 2000-2002 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
20 #include <cmath>
21 #include <algorithm>
22
23 #include <sigc++/bind.h>
24
25 #include <pbd/convert.h>
26 #include <pbd/enumwriter.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/session.h>
38 #include <ardour/audioengine.h>
39 #include <ardour/route.h>
40 #include <ardour/audio_track.h>
41 #include <ardour/audio_diskstream.h>
42 #include <ardour/panner.h>
43 #include <ardour/send.h>
44 #include <ardour/insert.h>
45 #include <ardour/profile.h>
46 #include <ardour/ladspa_plugin.h>
47 #include <ardour/connection.h>
48 #include <ardour/session_connection.h>
49
50 #include "ardour_ui.h"
51 #include "ardour_dialog.h"
52 #include "mixer_strip.h"
53 #include "mixer_ui.h"
54 #include "keyboard.h"
55 #include "public_editor.h"
56 #include "send_ui.h"
57 #include "io_selector.h"
58 #include "utils.h"
59 #include "gui_thread.h"
60
61 #include "i18n.h"
62
63 using namespace sigc;
64 using namespace ARDOUR;
65 using namespace PBD;
66 using namespace Gtk;
67 using namespace Gtkmm2ext;
68 using namespace std;
69
70 int MixerStrip::scrollbar_height = 0;
71
72 #ifdef VARISPEED_IN_MIXER_STRIP
73 static void 
74 speed_printer (char buf[32], Gtk::Adjustment& adj, void* arg)
75 {
76         float val = adj.get_value ();
77
78         if (val == 1.0) {
79                 strcpy (buf, "1");
80         } else {
81                 snprintf (buf, 32, "%.3f", val);
82         }
83 }
84 #endif 
85
86 MixerStrip::MixerStrip (Mixer_UI& mx, Session& sess, bool in_mixer)
87         : AxisView(sess),
88           RouteUI (sess, _("Mute"), _("Solo"), _("Record")),
89           _mixer(mx),
90           _mixer_owned (in_mixer),
91           pre_redirect_box (PreFader, sess, mx.plugin_selector(), mx.selection(), in_mixer),
92           post_redirect_box (PostFader, sess, mx.plugin_selector(), mx.selection(), in_mixer),
93           gpm (sess),
94           panners (sess),
95           button_table (3, 2),
96           middle_button_table (1, 2),
97           bottom_button_table (1, 2),
98           meter_point_label (_("pre")),
99           comment_button (_("Comments")),
100           speed_adjustment (1.0, 0.001, 4.0, 0.001, 0.1),
101           speed_spinner (&speed_adjustment, "MixerStripSpeedBase", true)
102
103 {
104         init ();
105 }
106
107 MixerStrip::MixerStrip (Mixer_UI& mx, Session& sess, boost::shared_ptr<Route> rt, bool in_mixer)
108         : AxisView(sess),
109           RouteUI (sess, _("Mute"), _("Solo"), _("Record")),
110           _mixer(mx),
111           _mixer_owned (in_mixer),
112           pre_redirect_box (PreFader, sess, mx.plugin_selector(), mx.selection(), in_mixer),
113           post_redirect_box (PostFader, sess, mx.plugin_selector(), mx.selection(), in_mixer),
114           gpm (sess),
115           panners (sess),
116           button_table (3, 2),
117           middle_button_table (1, 2),
118           bottom_button_table (1, 2),
119           meter_point_label (_("pre")),
120           comment_button (_("Comments")),
121           speed_adjustment (1.0, 0.001, 4.0, 0.001, 0.1),
122           speed_spinner (&speed_adjustment, "MixerStripSpeedBase", true)
123
124 {
125         init ();
126         set_route (rt);
127 }
128
129 void
130 MixerStrip::init ()
131 {
132         input_selector = 0;
133         output_selector = 0;
134         group_menu = 0;
135         _marked_for_display = false;
136         route_ops_menu = 0;
137         ignore_comment_edit = false;
138         ignore_toggle = false;
139         ignore_speed_adjustment = false;
140         comment_window = 0;
141         comment_area = 0;
142         _width_owner = 0;
143
144         width_button.add (*(manage (new Gtk::Image (::get_icon("strip_width")))));
145         hide_button.add (*(manage (new Gtk::Image (::get_icon("hide")))));
146
147         input_label.set_text (_("Input"));
148         input_button.add (input_label);
149         input_button.set_name ("MixerIOButton");
150         input_label.set_name ("MixerIOButtonLabel");
151
152         output_label.set_text (_("Output"));
153         output_button.add (output_label);
154         output_button.set_name ("MixerIOButton");
155         output_label.set_name ("MixerIOButtonLabel");
156
157         meter_point_button.add (meter_point_label);
158         meter_point_button.set_name ("MixerStripMeterPreButton");
159         meter_point_label.set_name ("MixerStripMeterPreButton");
160         
161         /* TRANSLATORS: this string should be longest of the strings
162            used to describe meter points. In english, it's "input".
163         */
164         set_size_request_to_display_given_text (meter_point_button, _("tupni"), 5, 5);
165     
166         bottom_button_table.attach (meter_point_button, 1, 2, 0, 1);
167     
168         meter_point_button.signal_button_press_event().connect (mem_fun (gpm, &GainMeter::meter_press), false);
169         /* XXX what is this meant to do? */
170         //meter_point_button.signal_button_release_event().connect (mem_fun (gpm, &GainMeter::meter_release), false);
171
172         hide_button.set_events (hide_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
173
174         mute_button->set_name ("MixerMuteButton");
175         solo_button->set_name ("MixerSoloButton");
176
177         button_table.set_homogeneous (true);
178         button_table.set_spacings (0);
179
180         button_table.attach (name_button, 0, 2, 0, 1);
181         button_table.attach (input_button, 0, 2, 1, 2);
182
183         middle_button_table.set_homogeneous (true);
184         middle_button_table.set_spacings (0);
185         middle_button_table.attach (*mute_button, 0, 1, 0, 1);
186         middle_button_table.attach (*solo_button, 1, 2, 0, 1);
187
188         bottom_button_table.set_col_spacings (0);
189         bottom_button_table.set_homogeneous (true);
190         bottom_button_table.attach (group_button, 0, 1, 0, 1);
191         
192         name_button.add (name_label);
193         name_button.set_name ("MixerNameButton");
194         Gtkmm2ext::set_size_request_to_display_given_text (name_button, "longest label", 2, 2);
195
196         name_label.set_name ("MixerNameButtonLabel");
197         group_button.add (group_label);
198         group_button.set_name ("MixerGroupButton");
199         group_label.set_name ("MixerGroupButtonLabel");
200
201         comment_button.set_name ("MixerCommentButton");
202
203         comment_button.signal_clicked().connect (mem_fun(*this, &MixerStrip::comment_button_clicked));
204         
205         global_vpacker.set_border_width (0);
206         global_vpacker.set_spacing (0);
207
208         VBox *whvbox = manage (new VBox);
209
210         width_button.set_name ("MixerWidthButton");
211         hide_button.set_name ("MixerHideButton");
212         top_event_box.set_name ("MixerTopEventBox");
213
214         width_button.signal_clicked().connect (mem_fun(*this, &MixerStrip::width_clicked));
215         hide_button.signal_clicked().connect (mem_fun(*this, &MixerStrip::hide_clicked));
216
217         width_hide_box.pack_start (width_button, false, true);
218         width_hide_box.pack_start (top_event_box, true, true);
219         width_hide_box.pack_end (hide_button, false, true);
220         Gtk::Alignment *gain_meter_alignment = Gtk::manage(new Gtk::Alignment());
221         gain_meter_alignment->set_padding(0, 4, 0, 0);
222         gain_meter_alignment->add(gpm);
223
224         whvbox->pack_start (width_hide_box, true, true);
225
226         global_vpacker.pack_start (*whvbox, Gtk::PACK_SHRINK);
227         global_vpacker.pack_start (button_table,Gtk::PACK_SHRINK);
228         global_vpacker.pack_start (pre_redirect_box, true, true);
229         global_vpacker.pack_start (middle_button_table,Gtk::PACK_SHRINK);
230         global_vpacker.pack_start (*gain_meter_alignment,Gtk::PACK_SHRINK);
231         global_vpacker.pack_start (bottom_button_table,Gtk::PACK_SHRINK);
232         global_vpacker.pack_start (post_redirect_box, true, true);
233         global_vpacker.pack_start (panners, Gtk::PACK_SHRINK);
234         global_vpacker.pack_start (output_button, Gtk::PACK_SHRINK);
235         global_vpacker.pack_start (comment_button, Gtk::PACK_SHRINK);
236
237         global_frame.add (global_vpacker);
238         global_frame.set_shadow_type (Gtk::SHADOW_IN);
239         global_frame.set_name ("BaseFrame");
240
241         add (global_frame);
242
243         /* force setting of visible selected status */
244
245         _selected = true;
246         set_selected (false);
247
248         _packed = false;
249         _embedded = false;
250
251         _session.engine().Stopped.connect (mem_fun(*this, &MixerStrip::engine_stopped));
252         _session.engine().Running.connect (mem_fun(*this, &MixerStrip::engine_running));
253
254         input_button.signal_button_press_event().connect (mem_fun(*this, &MixerStrip::input_press), false);
255         output_button.signal_button_press_event().connect (mem_fun(*this, &MixerStrip::output_press), false);
256
257         solo_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::solo_press), false);
258         solo_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::solo_release), false);
259         mute_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::mute_press), false);
260         mute_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::mute_release), false);
261
262         /* we don't need this if its not an audio track, but we don't know that yet and it doesn't
263            hurt (much).
264         */
265
266         rec_enable_button->set_name ("MixerRecordEnableButton");
267         rec_enable_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::rec_enable_press), false);
268         rec_enable_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::rec_enable_release));
269
270         name_button.signal_button_press_event().connect (mem_fun(*this, &MixerStrip::name_button_button_press), false);
271         group_button.signal_button_press_event().connect (mem_fun(*this, &MixerStrip::select_mix_group), false);
272
273         _width = (Width) -1;
274
275         /* start off as a passthru strip. we'll correct this, if necessary,
276            in update_diskstream_display().
277         */
278
279         set_name ("AudioTrackStripBase");
280
281         add_events (Gdk::BUTTON_RELEASE_MASK);
282 }
283
284 MixerStrip::~MixerStrip ()
285 {
286         GoingAway(); /* EMIT_SIGNAL */
287
288         if (input_selector) {
289                 delete input_selector;
290         }
291
292         if (output_selector) {
293                 delete output_selector;
294         }
295 }
296
297 void
298 MixerStrip::set_route (boost::shared_ptr<Route> rt)
299 {
300         if (rec_enable_button->get_parent()) {
301                 button_table.remove (*rec_enable_button);
302         }
303
304 #ifdef VARISPEED_IN_MIXER_STRIP
305         if (speed_frame->get_parent()) {
306                 button_table.remove (*speed_frame);
307         }
308 #endif
309
310         RouteUI::set_route (rt);
311
312         if (input_selector) {
313                 delete input_selector;
314                 input_selector = 0;
315         }
316
317         if (output_selector) {
318                 delete output_selector;
319                 output_selector = 0;
320         }
321
322         panners.set_io (rt);
323         gpm.set_io (rt);
324         pre_redirect_box.set_route (rt);
325         post_redirect_box.set_route (rt);
326
327         if (set_color_from_route()) {
328                 set_color (unique_random_color());
329         }
330
331         if (_mixer_owned && (route()->master() || route()->control())) {
332                 
333                 if (scrollbar_height == 0) {
334                         HScrollbar scrollbar;
335                         Gtk::Requisition requisition(scrollbar.size_request ());
336                         scrollbar_height = requisition.height;
337                 }
338
339                 EventBox* spacer = manage (new EventBox);
340                 spacer->set_size_request (-1, scrollbar_height);
341                 global_vpacker.pack_start (*spacer, false, false);
342         }
343
344         if (is_audio_track()) {
345
346                 boost::shared_ptr<AudioTrack> at = audio_track();
347
348                 connections.push_back (at->FreezeChange.connect (mem_fun(*this, &MixerStrip::map_frozen)));
349
350 #ifdef VARISPEED_IN_MIXER_STRIP
351                 speed_adjustment.signal_value_changed().connect (mem_fun(*this, &MixerStrip::speed_adjustment_changed));
352                 
353                 speed_frame.set_name ("BaseFrame");
354                 speed_frame.set_shadow_type (Gtk::SHADOW_IN);
355                 speed_frame.add (speed_spinner);
356                 
357                 speed_spinner.set_print_func (speed_printer, 0);
358
359                 ARDOUR_UI::instance()->tooltips().set_tip (speed_spinner, _("Varispeed"));
360
361                 button_table.attach (speed_frame, 0, 2, 5, 6);
362 #endif /* VARISPEED_IN_MIXER_STRIP */
363
364                 button_table.attach (*rec_enable_button, 0, 2, 2, 3);
365                 rec_enable_button->show();
366         }
367
368         if (_route->phase_invert()) {
369                 name_label.set_text (X_("Ø ") + name_label.get_text());
370         } else {
371                 name_label.set_text (_route->name());
372         }
373
374         switch (_route->meter_point()) {
375         case MeterInput:
376                 meter_point_label.set_text (_("input"));
377                 break;
378                 
379         case MeterPreFader:
380                 meter_point_label.set_text (_("pre"));
381                 break;
382                 
383         case MeterPostFader:
384                 meter_point_label.set_text (_("post"));
385                 break;
386         }
387         
388         ARDOUR_UI::instance()->tooltips().set_tip (comment_button, _route->comment().empty() ?
389                                                    _("Click to Add/Edit Comments"):
390                                                    _route->comment());
391
392         connections.push_back (_route->meter_change.connect (mem_fun(*this, &MixerStrip::meter_changed)));
393         connections.push_back (_route->input_changed.connect (mem_fun(*this, &MixerStrip::input_changed)));
394         connections.push_back (_route->output_changed.connect (mem_fun(*this, &MixerStrip::output_changed)));
395         connections.push_back (_route->mute_changed.connect (mem_fun(*this, &RouteUI::mute_changed)));
396         connections.push_back (_route->solo_changed.connect (mem_fun(*this, &RouteUI::solo_changed)));
397         connections.push_back (_route->solo_safe_changed.connect (mem_fun(*this, &RouteUI::solo_changed)));
398         connections.push_back (_route->mix_group_changed.connect (mem_fun(*this, &MixerStrip::mix_group_changed)));
399         connections.push_back (_route->panner().Changed.connect (mem_fun(*this, &MixerStrip::connect_to_pan)));
400
401         if (is_audio_track()) {
402                 connections.push_back (audio_track()->DiskstreamChanged.connect (mem_fun(*this, &MixerStrip::diskstream_changed)));
403                 connections.push_back (get_diskstream()->SpeedChanged.connect (mem_fun(*this, &MixerStrip::speed_changed)));
404         }
405
406         connections.push_back (_route->name_changed.connect (mem_fun(*this, &RouteUI::name_changed)));
407         connections.push_back (_route->comment_changed.connect (mem_fun(*this, &MixerStrip::comment_changed)));
408         connections.push_back (_route->gui_changed.connect (mem_fun(*this, &MixerStrip::route_gui_changed)));
409
410         set_stuff_from_route ();
411
412         /* now force an update of all the various elements */
413
414         pre_redirect_box.update();
415         post_redirect_box.update();
416         mute_changed (0);
417         solo_changed (0);
418         name_changed (0);
419         comment_changed (0);
420         mix_group_changed (0);
421
422         connect_to_pan ();
423
424         panners.setup_pan ();
425
426         if (is_audio_track()) {
427                 speed_changed ();
428         }
429
430         update_diskstream_display ();
431         update_input_display ();
432         update_output_display ();
433 }
434
435 void
436 MixerStrip::set_stuff_from_route ()
437 {
438         XMLProperty *prop;
439
440         ensure_xml_node ();
441
442         /* if width is not set, it will be set by the MixerUI or editor */
443
444         if ((prop = xml_node->property ("strip_width")) != 0) {
445                 set_width (Width (string_2_enum (prop->value(), _width)), this);
446         }
447
448         if ((prop = xml_node->property ("shown_mixer")) != 0) {
449                 if (prop->value() == "no") {
450                         _marked_for_display = false;
451                 } else {
452                         _marked_for_display = true;
453                 }
454         } else {
455                 /* backwards compatibility */
456                 _marked_for_display = true;
457         }
458 }
459
460 void
461 MixerStrip::set_width (Width w, void* owner)
462 {
463         /* always set the gpm width again, things may be hidden */
464
465         gpm.set_width (w);
466         panners.set_width (w);
467         pre_redirect_box.set_width (w);
468         post_redirect_box.set_width (w);
469
470         _width_owner = owner;
471
472         ensure_xml_node ();
473         
474         _width = w;
475
476         if (_width_owner == this) {
477                 xml_node->add_property ("strip_width", enum_2_string (_width));
478         }
479
480         switch (w) {
481         case Wide:
482
483                 if (rec_enable_button)  {
484                         ((Gtk::Label*)rec_enable_button->get_child())->set_text (_("Record"));
485                 }
486                 ((Gtk::Label*)mute_button->get_child())->set_text  (_("Mute"));
487                 ((Gtk::Label*)solo_button->get_child())->set_text (_("Solo"));
488
489                 if (_route->comment() == "") {
490                        comment_button.unset_bg (STATE_NORMAL);
491                        ((Gtk::Label*)comment_button.get_child())->set_text (_("Comments"));
492                 } else {
493                        comment_button.modify_bg (STATE_NORMAL, color());
494                        ((Gtk::Label*)comment_button.get_child())->set_text (_("*Comments*"));
495                 }
496
497                 ((Gtk::Label*)gpm.gain_automation_style_button.get_child())->set_text (gpm.astyle_string(_route->gain_automation_curve().automation_style()));
498                 ((Gtk::Label*)gpm.gain_automation_state_button.get_child())->set_text (gpm.astate_string(_route->gain_automation_curve().automation_state()));
499                 ((Gtk::Label*)panners.pan_automation_style_button.get_child())->set_text (panners.astyle_string(_route->panner().automation_style()));
500                 ((Gtk::Label*)panners.pan_automation_state_button.get_child())->set_text (panners.astate_string(_route->panner().automation_state()));
501                 Gtkmm2ext::set_size_request_to_display_given_text (name_button, "long", 2, 2);
502                 set_size_request (-1, -1);
503 #ifdef GTKOSX
504                 WidthChanged();
505 #endif
506                 break;
507
508         case Narrow:
509                 if (rec_enable_button) {
510                         ((Gtk::Label*)rec_enable_button->get_child())->set_text (_("Rec"));
511                 }
512                 ((Gtk::Label*)mute_button->get_child())->set_text (_("M"));
513                 ((Gtk::Label*)solo_button->get_child())->set_text (_("S"));
514
515                 if (_route->comment() == "") {
516                        comment_button.unset_bg (STATE_NORMAL);
517                        ((Gtk::Label*)comment_button.get_child())->set_text (_("Cmt"));
518                 } else {
519                        comment_button.modify_bg (STATE_NORMAL, color());
520                        ((Gtk::Label*)comment_button.get_child())->set_text (_("*Cmt*"));
521                 }
522
523                 ((Gtk::Label*)gpm.gain_automation_style_button.get_child())->set_text (gpm.short_astyle_string(_route->gain_automation_curve().automation_style()));
524                 ((Gtk::Label*)gpm.gain_automation_state_button.get_child())->set_text (gpm.short_astate_string(_route->gain_automation_curve().automation_state()));
525                 ((Gtk::Label*)panners.pan_automation_style_button.get_child())->set_text (panners.short_astyle_string(_route->panner().automation_style()));
526                 ((Gtk::Label*)panners.pan_automation_state_button.get_child())->set_text (panners.short_astate_string(_route->panner().automation_state()));
527                 Gtkmm2ext::set_size_request_to_display_given_text (name_button, "longest label", 2, 2);
528                 set_size_request (max (50, gpm.get_gm_width()), -1);
529                 break;
530         }
531         update_input_display ();
532         update_output_display ();
533         mix_group_changed (0);
534         name_changed (0);
535 }
536
537 void
538 MixerStrip::set_packed (bool yn)
539 {
540         _packed = yn;
541
542         ensure_xml_node ();
543
544         if (_packed) {
545                 xml_node->add_property ("shown_mixer", "yes");
546         } else {
547                 xml_node->add_property ("shown_mixer", "no");
548         }
549 }
550
551
552 gint
553 MixerStrip::output_press (GdkEventButton *ev)
554 {
555         using namespace Menu_Helpers;
556         if (!_session.engine().connected()) {
557                 MessageDialog msg (_("Not connected to JACK - no I/O changes are possible"));
558                 msg.run ();
559                 return true;
560         }
561
562         MenuList& citems = output_menu.items();
563         switch (ev->button) {
564
565         case 1:
566                 output_menu.set_name ("ArdourContextMenu");
567                 citems.clear();
568                 
569                 citems.push_back (MenuElem (_("Edit"), mem_fun(*this, &MixerStrip::edit_output_configuration)));
570                 citems.push_back (SeparatorElem());
571                 citems.push_back (MenuElem (_("Disconnect"), mem_fun (*(static_cast<RouteUI*>(this)), &RouteUI::disconnect_output)));
572                 citems.push_back (SeparatorElem());
573                 
574                 _session.foreach_connection (this, &MixerStrip::add_connection_to_output_menu);
575
576                 output_menu.popup (1, ev->time);
577                 break;
578                 
579         default:
580                 break;
581         }
582         return TRUE;
583 }
584
585 void
586 MixerStrip::edit_output_configuration ()
587 {
588         if (output_selector == 0) {
589                 output_selector = new IOSelectorWindow (_session, _route, false);
590         } 
591
592         if (output_selector->is_visible()) {
593                 output_selector->get_toplevel()->get_window()->raise();
594         } else {
595                 output_selector->show_all ();
596         }
597 }
598
599 void
600 MixerStrip::edit_input_configuration ()
601 {
602         if (input_selector == 0) {
603                 input_selector = new IOSelectorWindow (_session, _route, true);
604         } 
605
606         if (input_selector->is_visible()) {
607                 input_selector->get_toplevel()->get_window()->raise();
608         } else {
609                 input_selector->show_all ();
610         }
611 }
612
613 gint
614 MixerStrip::input_press (GdkEventButton *ev)
615 {
616         using namespace Menu_Helpers;
617
618         MenuList& citems = input_menu.items();
619         input_menu.set_name ("ArdourContextMenu");
620         citems.clear();
621         
622         if (!_session.engine().connected()) {
623                 MessageDialog msg (_("Not connected to JACK - no I/O changes are possible"));
624                 msg.run ();
625                 return true;
626         }
627
628         switch (ev->button) {
629
630         case 1:
631                 citems.push_back (MenuElem (_("Edit"), mem_fun(*this, &MixerStrip::edit_input_configuration)));
632                 citems.push_back (SeparatorElem());
633                 citems.push_back (MenuElem (_("Disconnect"), mem_fun (*(static_cast<RouteUI*>(this)), &RouteUI::disconnect_input)));
634                 citems.push_back (SeparatorElem());
635                 
636                 _session.foreach_connection (this, &MixerStrip::add_connection_to_input_menu);
637
638                 input_menu.popup (1, ev->time);
639                 break;
640                 
641         default:
642                 break;
643         }
644         return TRUE;
645 }
646
647 void
648 MixerStrip::connection_input_chosen (ARDOUR::Connection *c)
649 {
650         if (!ignore_toggle) {
651
652                 try { 
653                         _route->use_input_connection (*c, this);
654                 }
655
656                 catch (AudioEngine::PortRegistrationFailure& err) {
657                         error << _("could not register new ports required for that connection")
658                               << endmsg;
659                 }
660         }
661 }
662
663 void
664 MixerStrip::connection_output_chosen (ARDOUR::Connection *c)
665 {
666         if (!ignore_toggle) {
667
668                 try { 
669                         _route->use_output_connection (*c, this);
670                 }
671
672                 catch (AudioEngine::PortRegistrationFailure& err) {
673                         error << _("could not register new ports required for that connection")
674                               << endmsg;
675                 }
676         }
677 }
678
679 void
680 MixerStrip::add_connection_to_input_menu (ARDOUR::Connection* c)
681 {
682         using namespace Menu_Helpers;
683
684         if (dynamic_cast<InputConnection *> (c) == 0) {
685                 return;
686         }
687
688         MenuList& citems = input_menu.items();
689         
690         if (c->nports() == _route->n_inputs()) {
691
692                 citems.push_back (CheckMenuElem (c->name(), bind (mem_fun(*this, &MixerStrip::connection_input_chosen), c)));
693                 
694                 ARDOUR::Connection *current = _route->input_connection();
695                 
696                 if (current == c) {
697                         ignore_toggle = true;
698                         dynamic_cast<CheckMenuItem *> (&citems.back())->set_active (true);
699                         ignore_toggle = false;
700                 }
701         }
702 }
703
704 void
705 MixerStrip::add_connection_to_output_menu (ARDOUR::Connection* c)
706 {
707         using namespace Menu_Helpers;
708
709         if (dynamic_cast<OutputConnection *> (c) == 0) {
710                 return;
711         }
712
713         if (c->nports() == _route->n_outputs()) {
714
715                 MenuList& citems = output_menu.items();
716                 citems.push_back (CheckMenuElem (c->name(), bind (mem_fun(*this, &MixerStrip::connection_output_chosen), c)));
717                 
718                 ARDOUR::Connection *current = _route->output_connection();
719                 
720                 if (current == c) {
721                         ignore_toggle = true;
722                         dynamic_cast<CheckMenuItem *> (&citems.back())->set_active (true);
723                         ignore_toggle = false;
724                 }
725         }
726 }
727
728 void
729 MixerStrip::update_diskstream_display ()
730 {
731         map_frozen ();
732         update_input_display ();
733
734         if (is_audio_track()) {
735
736                 if (input_selector) {
737                         input_selector->hide_all ();
738                 }
739
740                 show_route_color ();
741
742         } else {
743
744                 show_passthru_color ();
745         }
746 }
747
748 void
749 MixerStrip::connect_to_pan ()
750 {
751         ENSURE_GUI_THREAD(mem_fun(*this, &MixerStrip::connect_to_pan));
752
753         panstate_connection.disconnect ();
754         panstyle_connection.disconnect ();
755
756         if (!_route->panner().empty()) {
757                 StreamPanner* sp = _route->panner().front();
758
759                 panstate_connection = sp->automation().automation_state_changed.connect (mem_fun(panners, &PannerUI::pan_automation_state_changed));
760                 panstyle_connection = sp->automation().automation_style_changed.connect (mem_fun(panners, &PannerUI::pan_automation_style_changed));
761         }
762
763         panners.pan_changed (this);
764 }
765
766 void
767 MixerStrip::update_input_display ()
768 {
769         ARDOUR::Connection *c;
770
771         if ((c = _route->input_connection()) != 0) {
772                 input_label.set_text (c->name());
773         } else {
774                 switch (_width) {
775                 case Wide:
776                         input_label.set_text (_(" Input"));
777                         break;
778                 case Narrow:
779                         input_label.set_text (_("I"));
780                         break;
781                 }
782         }
783         panners.setup_pan ();
784 }
785
786 void
787 MixerStrip::update_output_display ()
788 {
789         ARDOUR::Connection *c;
790
791         if ((c = _route->output_connection()) != 0) {
792                 output_label.set_text (c->name());
793         } else {
794                 switch (_width) {
795                 case Wide:
796                         output_label.set_text (_("Output"));
797                         break;
798                 case Narrow:
799                         output_label.set_text (_("O"));
800                         break;
801                 }
802         }
803
804         gpm.setup_meters ();
805         panners.setup_pan ();
806 }
807
808 void
809 MixerStrip::fast_update ()
810 {
811         gpm.update_meters ();
812 }
813
814 void
815 MixerStrip::diskstream_changed ()
816 {
817         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_diskstream_display));
818 }       
819
820 void
821 MixerStrip::input_changed (IOChange change, void *src)
822 {
823         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_input_display));
824         set_width(_width, this);
825 }
826
827 void
828 MixerStrip::output_changed (IOChange change, void *src)
829 {
830         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_output_display));
831         set_width(_width, this);
832 }
833
834
835 void 
836 MixerStrip::comment_editor_done_editing() 
837 {
838         string str =  comment_area->get_buffer()->get_text();
839         if (_route->comment() != str) {
840                 _route->set_comment (str, this);
841
842                 switch (_width) {
843                    
844                 case Wide:
845                         if (! str.empty()) {
846                                 comment_button.modify_bg (STATE_NORMAL, color());
847                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("*Comments*"));
848                         } else {
849                                 comment_button.unset_bg (STATE_NORMAL);
850                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("Comments"));
851                         }
852                         break;
853                    
854                 case Narrow:
855                         if (! str.empty()) {
856                                 comment_button.modify_bg (STATE_NORMAL, color());
857                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("*Cmt*"));
858                         } else {
859                                 comment_button.unset_bg (STATE_NORMAL);
860                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("Cmt"));
861                         } 
862                         break;
863                 }
864                  
865                 ARDOUR_UI::instance()->tooltips().set_tip (comment_button, 
866                                 str.empty() ? _("Click to Add/Edit Comments") : str);
867         }
868
869 }
870
871 void
872 MixerStrip::comment_button_clicked ()
873 {
874         if (comment_window == 0) {
875                 setup_comment_editor ();
876         }
877
878     int x, y, cw_width, cw_height;
879
880         if (comment_window->is_visible()) {
881                 comment_window->hide ();
882                 return;
883         }
884
885         comment_window->get_size (cw_width, cw_height);
886         comment_window->get_position(x, y);
887         comment_window->move(x, y - (cw_height / 2) - 45);
888         /* 
889            half the dialog height minus the comments button height 
890            with some window decoration fudge thrown in.
891         */
892
893         comment_window->show();
894         comment_window->present();
895 }
896
897 void
898 MixerStrip::setup_comment_editor ()
899 {
900         string title;
901         title = _route->name();
902         title += _(": comment editor");
903
904         comment_window = new ArdourDialog (title, false);
905         comment_window->set_position (Gtk::WIN_POS_MOUSE);
906         comment_window->set_skip_taskbar_hint (true);
907         comment_window->signal_hide().connect (mem_fun(*this, &MixerStrip::comment_editor_done_editing));
908
909         comment_area = manage (new TextView());
910         comment_area->set_name ("MixerTrackCommentArea");
911         comment_area->set_size_request (110, 178);
912         comment_area->set_wrap_mode (WRAP_WORD);
913         comment_area->set_editable (true);
914         comment_area->get_buffer()->set_text (_route->comment());
915         comment_area->show ();
916
917         comment_window->get_vbox()->pack_start (*comment_area);
918         comment_window->get_action_area()->hide();
919 }
920
921 void
922 MixerStrip::comment_changed (void *src)
923 {
924         ENSURE_GUI_THREAD(bind (mem_fun(*this, &MixerStrip::comment_changed), src));
925         
926         if (src != this) {
927                 ignore_comment_edit = true;
928                 if (comment_area) {
929                         comment_area->get_buffer()->set_text (_route->comment());
930                 }
931                 ignore_comment_edit = false;
932         }
933 }
934
935 void
936 MixerStrip::set_mix_group (RouteGroup *rg)
937 {
938         _route->set_mix_group (rg, this);
939 }
940
941 void
942 MixerStrip::add_mix_group_to_menu (RouteGroup *rg, RadioMenuItem::Group* group)
943 {
944         using namespace Menu_Helpers;
945
946         MenuList& items = group_menu->items();
947
948         items.push_back (RadioMenuElem (*group, rg->name(), bind (mem_fun(*this, &MixerStrip::set_mix_group), rg)));
949
950         if (_route->mix_group() == rg) {
951                 static_cast<RadioMenuItem*>(&items.back())->set_active ();
952         }
953 }
954
955 bool
956 MixerStrip::select_mix_group (GdkEventButton *ev)
957 {
958         using namespace Menu_Helpers;
959
960         if (group_menu == 0) {
961                 group_menu = new Menu;
962         } 
963         group_menu->set_name ("ArdourContextMenu");
964         MenuList& items = group_menu->items();
965         RadioMenuItem::Group group;
966
967         switch (ev->button) {
968         case 1:
969
970                 items.clear ();
971                 items.push_back (RadioMenuElem (group, _("No group"), bind (mem_fun(*this, &MixerStrip::set_mix_group), (RouteGroup *) 0)));
972
973                 _session.foreach_mix_group (bind (mem_fun (*this, &MixerStrip::add_mix_group_to_menu), &group));
974
975                 group_menu->popup (1, ev->time);
976                 break;
977
978         default:
979                 break;
980         }
981         
982         return true;
983 }       
984
985 void
986 MixerStrip::mix_group_changed (void *ignored)
987 {
988         ENSURE_GUI_THREAD(bind (mem_fun(*this, &MixerStrip::mix_group_changed), ignored));
989         
990         RouteGroup *rg = _route->mix_group();
991         
992         if (rg) {
993                 group_label.set_text (rg->name());
994         } else {
995                 switch (_width) {
996                 case Wide:
997                         group_label.set_text (_("Grp"));
998                         break;
999                 case Narrow:
1000                         group_label.set_text (_("~G"));
1001                         break;
1002                 }
1003         }
1004 }
1005
1006
1007 void 
1008 MixerStrip::route_gui_changed (string what_changed, void* ignored)
1009 {
1010         ENSURE_GUI_THREAD(bind (mem_fun(*this, &MixerStrip::route_gui_changed), what_changed, ignored));
1011         
1012         if (what_changed == "color") {
1013                 if (set_color_from_route () == 0) {
1014                         show_route_color ();
1015                 }
1016         }
1017 }
1018
1019 void
1020 MixerStrip::show_route_color ()
1021 {
1022         name_button.modify_bg (STATE_NORMAL, color());
1023         top_event_box.modify_bg (STATE_NORMAL, color());
1024         route_active_changed ();
1025 }
1026
1027 void
1028 MixerStrip::show_passthru_color ()
1029 {
1030         route_active_changed ();
1031 }
1032
1033 void
1034 MixerStrip::build_route_ops_menu ()
1035 {
1036         using namespace Menu_Helpers;
1037         route_ops_menu = manage (new Menu);
1038         route_ops_menu->set_name ("ArdourContextMenu");
1039
1040         MenuList& items = route_ops_menu->items();
1041
1042         items.push_back (MenuElem (_("Rename"), mem_fun(*this, &RouteUI::route_rename)));
1043         items.push_back (SeparatorElem());
1044         items.push_back (CheckMenuElem (_("Active"), mem_fun (*this, &RouteUI::toggle_route_active)));
1045         route_active_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
1046         route_active_menu_item->set_active (_route->active());
1047         items.push_back (SeparatorElem());
1048         items.push_back (CheckMenuElem (_("Invert Polarity"), mem_fun (*this, &RouteUI::toggle_polarity)));
1049         polarity_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
1050         polarity_menu_item->set_active (_route->phase_invert());
1051         items.push_back (CheckMenuElem (_("Protect against denormals"), mem_fun (*this, &RouteUI::toggle_denormal_protection)));
1052         denormal_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
1053         denormal_menu_item->set_active (_route->denormal_protection());
1054
1055         build_remote_control_menu ();
1056         
1057         items.push_back (SeparatorElem());
1058         if (!Profile->get_sae()) {
1059               items.push_back (MenuElem (_("Remote Control ID"), *remote_control_menu));
1060         }
1061
1062         items.push_back (SeparatorElem());
1063         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &RouteUI::remove_this_route)));
1064 }
1065
1066 gint
1067 MixerStrip::name_button_button_press (GdkEventButton* ev)
1068 {
1069         if (ev->button == 1) {
1070                 list_route_operations ();
1071
1072                 Menu_Helpers::MenuList& items = route_ops_menu->items();
1073                 /* do not allow rename if the track is record-enabled */
1074                 static_cast<MenuItem*> (&items.front())->set_sensitive (!_route->record_enabled());
1075
1076                 route_ops_menu->popup (1, ev->time);
1077         }
1078         return FALSE;
1079 }
1080
1081 void
1082 MixerStrip::list_route_operations ()
1083 {
1084         if (route_ops_menu == 0) {
1085                 build_route_ops_menu ();
1086         }
1087         
1088         refresh_remote_control_menu();
1089 }
1090
1091
1092 void
1093 MixerStrip::speed_adjustment_changed ()
1094 {
1095         /* since there is a usable speed adjustment, there has to be a diskstream */
1096         if (!ignore_speed_adjustment) {
1097                 get_diskstream()->set_speed (speed_adjustment.get_value());
1098         }
1099 }
1100
1101 void
1102 MixerStrip::speed_changed ()
1103 {
1104         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_speed_display));
1105 }
1106
1107 void
1108 MixerStrip::update_speed_display ()
1109 {
1110         float val;
1111         
1112         val = get_diskstream()->speed();
1113
1114         if (val != 1.0) {
1115                 speed_spinner.set_name ("MixerStripSpeedBaseNotOne");
1116         } else {
1117                 speed_spinner.set_name ("MixerStripSpeedBase");
1118         }
1119
1120         if (speed_adjustment.get_value() != val) {
1121                 ignore_speed_adjustment = true;
1122                 speed_adjustment.set_value (val);
1123                 ignore_speed_adjustment = false;
1124         }
1125 }                       
1126
1127
1128 void
1129 MixerStrip::set_selected (bool yn)
1130 {
1131         AxisView::set_selected (yn);
1132         if (_selected) {
1133                 global_frame.set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
1134                 global_frame.set_name ("MixerStripSelectedFrame");
1135         } else {
1136                 global_frame.set_shadow_type (Gtk::SHADOW_IN);
1137                 global_frame.set_name ("MixerStripFrame");
1138         }
1139         global_frame.queue_draw ();
1140 }
1141
1142 void
1143 MixerStrip::name_changed (void *src)
1144 {
1145         switch (_width) {
1146         case Wide:
1147                 RouteUI::name_changed (src);
1148                 break;
1149         case Narrow:
1150                 name_label.set_text (PBD::short_version (_route->name(), 5));
1151                 break;
1152         }
1153         if (_route->phase_invert()) {
1154                 name_label.set_text (X_("Ø ") + name_label.get_text());
1155         }
1156 }
1157
1158 void
1159 MixerStrip::width_clicked ()
1160 {
1161         switch (_width) {
1162         case Wide:
1163                 set_width (Narrow, this);
1164                 break;
1165         case Narrow:
1166                 set_width (Wide, this);
1167                 break;
1168         }
1169 }
1170
1171 void
1172 MixerStrip::hide_clicked ()
1173 {
1174     // LAME fix to reset the button status for when it is redisplayed (part 1)
1175     hide_button.set_sensitive(false);
1176     
1177         if (_embedded) {
1178                  Hiding(); /* EMIT_SIGNAL */
1179         } else {
1180                 _mixer.hide_strip (this);
1181         }
1182         
1183     // (part 2)
1184         hide_button.set_sensitive(true);
1185 }
1186
1187 void
1188 MixerStrip::set_embedded (bool yn)
1189 {
1190         _embedded = yn;
1191 }
1192
1193 void
1194 MixerStrip::map_frozen ()
1195 {
1196         ENSURE_GUI_THREAD (mem_fun(*this, &MixerStrip::map_frozen));
1197
1198         boost::shared_ptr<AudioTrack> at = audio_track();
1199
1200         if (at) {
1201                 switch (at->freeze_state()) {
1202                 case AudioTrack::Frozen:
1203                         pre_redirect_box.set_sensitive (false);
1204                         post_redirect_box.set_sensitive (false);
1205                         speed_spinner.set_sensitive (false);
1206                         break;
1207                 default:
1208                         pre_redirect_box.set_sensitive (true);
1209                         post_redirect_box.set_sensitive (true);
1210                         speed_spinner.set_sensitive (true);
1211                         // XXX need some way, maybe, to retoggle redirect editors
1212                         break;
1213                 }
1214         }
1215         
1216         hide_redirect_editors ();
1217 }
1218
1219 void
1220 MixerStrip::hide_redirect_editors ()
1221 {
1222         _route->foreach_redirect (this, &MixerStrip::hide_redirect_editor);
1223 }
1224
1225 void
1226 MixerStrip::hide_redirect_editor (boost::shared_ptr<Redirect> redirect)
1227 {
1228         void* gui = redirect->get_gui ();
1229         
1230         if (gui) {
1231                 static_cast<Gtk::Widget*>(gui)->hide ();
1232         }
1233 }
1234
1235 void
1236 MixerStrip::route_active_changed ()
1237 {
1238         RouteUI::route_active_changed ();
1239
1240         if (is_audio_track()) {
1241                 if (_route->active()) {
1242                         set_name ("AudioTrackStripBase");
1243                         gpm.set_meter_strip_name ("AudioTrackMetrics");
1244                 } else {
1245                         set_name ("AudioTrackStripBaseInactive");
1246                         gpm.set_meter_strip_name ("AudioTrackMetricsInactive");
1247                 }
1248                 gpm.set_fader_name ("AudioTrackFader");
1249         } else { // FIXME: assumed audio bus
1250                 if (_route->active()) {
1251                         set_name ("AudioBusStripBase");
1252                         gpm.set_meter_strip_name ("AudioBusMetrics");
1253                 } else {
1254                         set_name ("AudioBusStripBaseInactive");
1255                         gpm.set_meter_strip_name ("AudioBusMetricsInactive");
1256                 }
1257                 gpm.set_fader_name ("AudioBusFader");
1258         }
1259 }
1260
1261 RouteGroup*
1262 MixerStrip::mix_group() const
1263 {
1264         return _route->mix_group();
1265 }
1266
1267 void
1268 MixerStrip::engine_stopped ()
1269 {
1270 }
1271
1272 void
1273 MixerStrip::engine_running ()
1274 {
1275 }
1276
1277 void
1278 MixerStrip::meter_changed (void *src)
1279 {
1280
1281         ENSURE_GUI_THREAD (bind (mem_fun(*this, &MixerStrip::meter_changed), src));
1282
1283         switch (_route->meter_point()) {
1284         case MeterInput:
1285                 meter_point_label.set_text (_("input"));
1286                 break;
1287                 
1288         case MeterPreFader:
1289                 meter_point_label.set_text (_("pre"));
1290                 break;
1291                 
1292         case MeterPostFader:
1293                 meter_point_label.set_text (_("post"));
1294                 break;
1295         }
1296         
1297         gpm.setup_meters ();
1298         // reset peak when meter point changes
1299         gpm.reset_peak_display();
1300         set_width (_width, this);
1301 }
1302