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