more ongoing SAE-driven changes (too many too list here)
[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         if (_width == w) {
407                 return;
408         }
409
410         ensure_xml_node ();
411         
412         _width = w;
413
414         if (_width_owner == this) {
415                 xml_node->add_property ("strip_width", enum_2_string (_width));
416         }
417
418         switch (w) {
419         case Wide:
420                 set_size_request (-1, -1);
421                 
422                 if (rec_enable_button)  {
423                         ((Gtk::Label*)rec_enable_button->get_child())->set_text (_("record"));
424                 }
425                 ((Gtk::Label*)mute_button->get_child())->set_text  (_("Mute"));
426                 ((Gtk::Label*)solo_button->get_child())->set_text (_("Solo"));
427
428                 if (_route->comment() == "") {
429                        comment_button.unset_bg (STATE_NORMAL);
430                        ((Gtk::Label*)comment_button.get_child())->set_text (_("comments"));
431                 } else {
432                        comment_button.modify_bg (STATE_NORMAL, color());
433                        ((Gtk::Label*)comment_button.get_child())->set_text (_("*comments*"));
434                 }
435
436                 ((Gtk::Label*)gpm.gain_automation_style_button.get_child())->set_text (gpm.astyle_string(_route->gain_automation_curve().automation_style()));
437                 ((Gtk::Label*)gpm.gain_automation_state_button.get_child())->set_text (gpm.astate_string(_route->gain_automation_curve().automation_state()));
438                 ((Gtk::Label*)panners.pan_automation_style_button.get_child())->set_text (panners.astyle_string(_route->panner().automation_style()));
439                 ((Gtk::Label*)panners.pan_automation_state_button.get_child())->set_text (panners.astate_string(_route->panner().automation_state()));
440                 Gtkmm2ext::set_size_request_to_display_given_text (name_button, "long", 2, 2);
441                 break;
442
443         case Narrow:
444                 if (rec_enable_button) {
445                         ((Gtk::Label*)rec_enable_button->get_child())->set_text (_("Rec"));
446                 }
447                 ((Gtk::Label*)mute_button->get_child())->set_text (_("M"));
448                 ((Gtk::Label*)solo_button->get_child())->set_text (_("S"));
449
450                 if (_route->comment() == "") {
451                        comment_button.unset_bg (STATE_NORMAL);
452                        ((Gtk::Label*)comment_button.get_child())->set_text (_("Cmt"));
453                 } else {
454                        comment_button.modify_bg (STATE_NORMAL, color());
455                        ((Gtk::Label*)comment_button.get_child())->set_text (_("*Cmt*"));
456                 }
457
458                 ((Gtk::Label*)gpm.gain_automation_style_button.get_child())->set_text (gpm.short_astyle_string(_route->gain_automation_curve().automation_style()));
459                 ((Gtk::Label*)gpm.gain_automation_state_button.get_child())->set_text (gpm.short_astate_string(_route->gain_automation_curve().automation_state()));
460                 ((Gtk::Label*)panners.pan_automation_style_button.get_child())->set_text (panners.short_astyle_string(_route->panner().automation_style()));
461                 ((Gtk::Label*)panners.pan_automation_state_button.get_child())->set_text (panners.short_astate_string(_route->panner().automation_state()));
462                 Gtkmm2ext::set_size_request_to_display_given_text (name_button, "longest label", 2, 2);
463                 set_size_request (max (50, gpm.get_gm_width()), -1);
464                 break;
465         }
466
467         update_input_display ();
468         update_output_display ();
469         mix_group_changed (0);
470         name_changed (0);
471
472 }
473
474 void
475 MixerStrip::set_packed (bool yn)
476 {
477         _packed = yn;
478
479         ensure_xml_node ();
480
481         if (_packed) {
482                 xml_node->add_property ("shown_mixer", "yes");
483         } else {
484                 xml_node->add_property ("shown_mixer", "no");
485         }
486 }
487
488
489 gint
490 MixerStrip::output_press (GdkEventButton *ev)
491 {
492         using namespace Menu_Helpers;
493         if (!_session.engine().connected()) {
494                 MessageDialog msg (_("Not connected to JACK - no I/O changes are possible"));
495                 msg.run ();
496                 return true;
497         }
498
499         MenuList& citems = output_menu.items();
500         switch (ev->button) {
501
502         case 1:
503                 output_menu.set_name ("ArdourContextMenu");
504                 citems.clear();
505                 
506                 citems.push_back (MenuElem (_("Edit"), mem_fun(*this, &MixerStrip::edit_output_configuration)));
507                 citems.push_back (SeparatorElem());
508                 citems.push_back (MenuElem (_("Disconnect"), mem_fun (*(static_cast<RouteUI*>(this)), &RouteUI::disconnect_output)));
509                 citems.push_back (SeparatorElem());
510                 
511                 _session.foreach_connection (this, &MixerStrip::add_connection_to_output_menu);
512
513                 output_menu.popup (1, ev->time);
514                 break;
515                 
516         default:
517                 break;
518         }
519         return TRUE;
520 }
521
522 void
523 MixerStrip::edit_output_configuration ()
524 {
525         if (output_selector == 0) {
526                 output_selector = new IOSelectorWindow (_session, _route, false);
527         } 
528
529         if (output_selector->is_visible()) {
530                 output_selector->get_toplevel()->get_window()->raise();
531         } else {
532                 output_selector->show_all ();
533         }
534 }
535
536 void
537 MixerStrip::edit_input_configuration ()
538 {
539         if (input_selector == 0) {
540                 input_selector = new IOSelectorWindow (_session, _route, true);
541         } 
542
543         if (input_selector->is_visible()) {
544                 input_selector->get_toplevel()->get_window()->raise();
545         } else {
546                 input_selector->show_all ();
547         }
548 }
549
550 gint
551 MixerStrip::input_press (GdkEventButton *ev)
552 {
553         using namespace Menu_Helpers;
554
555         MenuList& citems = input_menu.items();
556         input_menu.set_name ("ArdourContextMenu");
557         citems.clear();
558         
559         if (!_session.engine().connected()) {
560                 MessageDialog msg (_("Not connected to JACK - no I/O changes are possible"));
561                 msg.run ();
562                 return true;
563         }
564
565         switch (ev->button) {
566
567         case 1:
568                 citems.push_back (MenuElem (_("Edit"), mem_fun(*this, &MixerStrip::edit_input_configuration)));
569                 citems.push_back (SeparatorElem());
570                 citems.push_back (MenuElem (_("Disconnect"), mem_fun (*(static_cast<RouteUI*>(this)), &RouteUI::disconnect_input)));
571                 citems.push_back (SeparatorElem());
572                 
573                 _session.foreach_connection (this, &MixerStrip::add_connection_to_input_menu);
574
575                 input_menu.popup (1, ev->time);
576                 break;
577                 
578         default:
579                 break;
580         }
581         return TRUE;
582 }
583
584 void
585 MixerStrip::connection_input_chosen (ARDOUR::Connection *c)
586 {
587         if (!ignore_toggle) {
588
589                 try { 
590                         _route->use_input_connection (*c, this);
591                 }
592
593                 catch (AudioEngine::PortRegistrationFailure& err) {
594                         error << _("could not register new ports required for that connection")
595                               << endmsg;
596                 }
597         }
598 }
599
600 void
601 MixerStrip::connection_output_chosen (ARDOUR::Connection *c)
602 {
603         if (!ignore_toggle) {
604
605                 try { 
606                         _route->use_output_connection (*c, this);
607                 }
608
609                 catch (AudioEngine::PortRegistrationFailure& err) {
610                         error << _("could not register new ports required for that connection")
611                               << endmsg;
612                 }
613         }
614 }
615
616 void
617 MixerStrip::add_connection_to_input_menu (ARDOUR::Connection* c)
618 {
619         using namespace Menu_Helpers;
620
621         if (dynamic_cast<InputConnection *> (c) == 0) {
622                 return;
623         }
624
625         MenuList& citems = input_menu.items();
626         
627         if (c->nports() == _route->n_inputs()) {
628
629                 citems.push_back (CheckMenuElem (c->name(), bind (mem_fun(*this, &MixerStrip::connection_input_chosen), c)));
630                 
631                 ARDOUR::Connection *current = _route->input_connection();
632                 
633                 if (current == c) {
634                         ignore_toggle = true;
635                         dynamic_cast<CheckMenuItem *> (&citems.back())->set_active (true);
636                         ignore_toggle = false;
637                 }
638         }
639 }
640
641 void
642 MixerStrip::add_connection_to_output_menu (ARDOUR::Connection* c)
643 {
644         using namespace Menu_Helpers;
645
646         if (dynamic_cast<OutputConnection *> (c) == 0) {
647                 return;
648         }
649
650         if (c->nports() == _route->n_outputs()) {
651
652                 MenuList& citems = output_menu.items();
653                 citems.push_back (CheckMenuElem (c->name(), bind (mem_fun(*this, &MixerStrip::connection_output_chosen), c)));
654                 
655                 ARDOUR::Connection *current = _route->output_connection();
656                 
657                 if (current == c) {
658                         ignore_toggle = true;
659                         dynamic_cast<CheckMenuItem *> (&citems.back())->set_active (true);
660                         ignore_toggle = false;
661                 }
662         }
663 }
664
665 void
666 MixerStrip::update_diskstream_display ()
667 {
668         if (is_audio_track()) {
669
670                 map_frozen ();
671
672                 update_input_display ();
673
674                 if (input_selector) {
675                         input_selector->hide_all ();
676                 }
677
678                 show_route_color ();
679
680         } else {
681
682                 map_frozen ();
683
684                 update_input_display ();
685                 show_passthru_color ();
686         }
687 }
688
689 void
690 MixerStrip::connect_to_pan ()
691 {
692         ENSURE_GUI_THREAD(mem_fun(*this, &MixerStrip::connect_to_pan));
693
694         panstate_connection.disconnect ();
695         panstyle_connection.disconnect ();
696
697         if (!_route->panner().empty()) {
698                 StreamPanner* sp = _route->panner().front();
699
700                 panstate_connection = sp->automation().automation_state_changed.connect (mem_fun(panners, &PannerUI::pan_automation_state_changed));
701                 panstyle_connection = sp->automation().automation_style_changed.connect (mem_fun(panners, &PannerUI::pan_automation_style_changed));
702         }
703
704         panners.pan_changed (this);
705 }
706
707 void
708 MixerStrip::update_input_display ()
709 {
710         ARDOUR::Connection *c;
711
712         if ((c = _route->input_connection()) != 0) {
713                 input_label.set_text (c->name());
714         } else {
715                 switch (_width) {
716                 case Wide:
717                         input_label.set_text (_(" Input"));
718                         break;
719                 case Narrow:
720                         input_label.set_text (_("I"));
721                         break;
722                 }
723         }
724         panners.setup_pan ();
725 }
726
727 void
728 MixerStrip::update_output_display ()
729 {
730         ARDOUR::Connection *c;
731
732         if ((c = _route->output_connection()) != 0) {
733                 output_label.set_text (c->name());
734         } else {
735                 switch (_width) {
736                 case Wide:
737                         output_label.set_text (_("Output"));
738                         break;
739                 case Narrow:
740                         output_label.set_text (_("O"));
741                         break;
742                 }
743         }
744         gpm.setup_meters ();
745         panners.setup_pan ();
746 }
747
748 void
749 MixerStrip::fast_update ()
750 {
751         gpm.update_meters ();
752 }
753
754 void
755 MixerStrip::diskstream_changed ()
756 {
757         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_diskstream_display));
758 }       
759
760 void
761 MixerStrip::input_changed (IOChange change, void *src)
762 {
763         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_input_display));
764 }
765
766 void
767 MixerStrip::output_changed (IOChange change, void *src)
768 {
769         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_output_display));
770 }
771
772
773 void 
774 MixerStrip::comment_editor_done_editing() 
775 {
776         string str =  comment_area->get_buffer()->get_text();
777         if (_route->comment() != str) {
778                 _route->set_comment (str, this);
779
780                 switch (_width) {
781                    
782                 case Wide:
783                         if (! str.empty()) {
784                                 comment_button.modify_bg (STATE_NORMAL, color());
785                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("*Comments*"));
786                         } else {
787                                 comment_button.unset_bg (STATE_NORMAL);
788                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("Comments"));
789                         }
790                         break;
791                    
792                 case Narrow:
793                         if (! str.empty()) {
794                                 comment_button.modify_bg (STATE_NORMAL, color());
795                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("*Cmt*"));
796                         } else {
797                                 comment_button.unset_bg (STATE_NORMAL);
798                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("Cmt"));
799                         } 
800                         break;
801                 }
802                  
803                 ARDOUR_UI::instance()->tooltips().set_tip (comment_button, 
804                                 str.empty() ? _("Click to Add/Edit Comments") : str);
805         }
806
807 }
808
809 void
810 MixerStrip::comment_button_clicked ()
811 {
812         if (comment_window == 0) {
813                 setup_comment_editor ();
814         }
815
816     int x, y, cw_width, cw_height;
817
818         if (comment_window->is_visible()) {
819                 comment_window->hide ();
820                 return;
821         }
822
823         comment_window->get_size (cw_width, cw_height);
824         comment_window->get_position(x, y);
825         comment_window->move(x, y - (cw_height / 2) - 45);
826         /* 
827            half the dialog height minus the comments button height 
828            with some window decoration fudge thrown in.
829         */
830
831         comment_window->show();
832         comment_window->present();
833 }
834
835 void
836 MixerStrip::setup_comment_editor ()
837 {
838         string title;
839         title = _route->name();
840         title += _(": comment editor");
841
842         comment_window = new ArdourDialog (title, false);
843         comment_window->set_position (Gtk::WIN_POS_MOUSE);
844         comment_window->set_skip_taskbar_hint (true);
845         comment_window->signal_hide().connect (mem_fun(*this, &MixerStrip::comment_editor_done_editing));
846
847         comment_area = manage (new TextView());
848         comment_area->set_name ("MixerTrackCommentArea");
849         comment_area->set_size_request (110, 178);
850         comment_area->set_wrap_mode (WRAP_WORD);
851         comment_area->set_editable (true);
852         comment_area->get_buffer()->set_text (_route->comment());
853         comment_area->show ();
854
855         comment_window->get_vbox()->pack_start (*comment_area);
856         comment_window->get_action_area()->hide();
857 }
858
859 void
860 MixerStrip::comment_changed (void *src)
861 {
862         ENSURE_GUI_THREAD(bind (mem_fun(*this, &MixerStrip::comment_changed), src));
863         
864         if (src != this) {
865                 ignore_comment_edit = true;
866                 if (comment_area) {
867                         comment_area->get_buffer()->set_text (_route->comment());
868                 }
869                 ignore_comment_edit = false;
870         }
871 }
872
873 void
874 MixerStrip::set_mix_group (RouteGroup *rg)
875 {
876         _route->set_mix_group (rg, this);
877 }
878
879 void
880 MixerStrip::add_mix_group_to_menu (RouteGroup *rg, RadioMenuItem::Group* group)
881 {
882         using namespace Menu_Helpers;
883
884         MenuList& items = group_menu->items();
885
886         items.push_back (RadioMenuElem (*group, rg->name(), bind (mem_fun(*this, &MixerStrip::set_mix_group), rg)));
887
888         if (_route->mix_group() == rg) {
889                 static_cast<RadioMenuItem*>(&items.back())->set_active ();
890         }
891 }
892
893 bool
894 MixerStrip::select_mix_group (GdkEventButton *ev)
895 {
896         using namespace Menu_Helpers;
897
898         if (group_menu == 0) {
899                 group_menu = new Menu;
900         } 
901         group_menu->set_name ("ArdourContextMenu");
902         MenuList& items = group_menu->items();
903         RadioMenuItem::Group group;
904
905         switch (ev->button) {
906         case 1:
907
908                 items.clear ();
909                 items.push_back (RadioMenuElem (group, _("No group"), bind (mem_fun(*this, &MixerStrip::set_mix_group), (RouteGroup *) 0)));
910
911                 _session.foreach_mix_group (bind (mem_fun (*this, &MixerStrip::add_mix_group_to_menu), &group));
912
913                 group_menu->popup (1, ev->time);
914                 break;
915
916         default:
917                 break;
918         }
919         
920         return true;
921 }       
922
923 void
924 MixerStrip::mix_group_changed (void *ignored)
925 {
926         ENSURE_GUI_THREAD(bind (mem_fun(*this, &MixerStrip::mix_group_changed), ignored));
927         
928         RouteGroup *rg = _route->mix_group();
929         
930         if (rg) {
931                 group_label.set_text (rg->name());
932         } else {
933                 switch (_width) {
934                 case Wide:
935                         group_label.set_text (_("Grp"));
936                         break;
937                 case Narrow:
938                         group_label.set_text (_("~G"));
939                         break;
940                 }
941         }
942 }
943
944
945 void 
946 MixerStrip::route_gui_changed (string what_changed, void* ignored)
947 {
948         ENSURE_GUI_THREAD(bind (mem_fun(*this, &MixerStrip::route_gui_changed), what_changed, ignored));
949         
950         if (what_changed == "color") {
951                 if (set_color_from_route () == 0) {
952                         show_route_color ();
953                 }
954         }
955 }
956
957 void
958 MixerStrip::show_route_color ()
959 {
960         name_button.modify_bg (STATE_NORMAL, color());
961         top_event_box.modify_bg (STATE_NORMAL, color());
962         route_active_changed ();
963 }
964
965 void
966 MixerStrip::show_passthru_color ()
967 {
968         route_active_changed ();
969 }
970
971 void
972 MixerStrip::build_route_ops_menu ()
973 {
974         using namespace Menu_Helpers;
975         route_ops_menu = manage (new Menu);
976         route_ops_menu->set_name ("ArdourContextMenu");
977
978         MenuList& items = route_ops_menu->items();
979
980         items.push_back (MenuElem (_("Rename"), mem_fun(*this, &RouteUI::route_rename)));
981         items.push_back (SeparatorElem());
982         items.push_back (CheckMenuElem (_("Active"), mem_fun (*this, &RouteUI::toggle_route_active)));
983         route_active_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
984         route_active_menu_item->set_active (_route->active());
985         items.push_back (SeparatorElem());
986         items.push_back (CheckMenuElem (_("Invert Polarity"), mem_fun (*this, &RouteUI::toggle_polarity)));
987         polarity_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
988         polarity_menu_item->set_active (_route->phase_invert());
989         items.push_back (CheckMenuElem (_("Protect against denormals"), mem_fun (*this, &RouteUI::toggle_denormal_protection)));
990         denormal_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
991         denormal_menu_item->set_active (_route->denormal_protection());
992
993         build_remote_control_menu ();
994         
995         items.push_back (SeparatorElem());
996         if (!Profile->get_sae()) {
997               items.push_back (MenuElem (_("Remote Control ID"), *remote_control_menu));
998         }
999
1000         items.push_back (SeparatorElem());
1001         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &RouteUI::remove_this_route)));
1002 }
1003
1004 gint
1005 MixerStrip::name_button_button_press (GdkEventButton* ev)
1006 {
1007         if (ev->button == 1) {
1008                 list_route_operations ();
1009
1010                 Menu_Helpers::MenuList& items = route_ops_menu->items();
1011                 /* do not allow rename if the track is record-enabled */
1012                 static_cast<MenuItem*> (&items.front())->set_sensitive (!_route->record_enabled());
1013
1014                 route_ops_menu->popup (1, ev->time);
1015         }
1016         return FALSE;
1017 }
1018
1019 void
1020 MixerStrip::list_route_operations ()
1021 {
1022         if (route_ops_menu == 0) {
1023                 build_route_ops_menu ();
1024         }
1025         
1026         refresh_remote_control_menu();
1027 }
1028
1029
1030 void
1031 MixerStrip::speed_adjustment_changed ()
1032 {
1033         /* since there is a usable speed adjustment, there has to be a diskstream */
1034         if (!ignore_speed_adjustment) {
1035                 get_diskstream()->set_speed (speed_adjustment.get_value());
1036         }
1037 }
1038
1039 void
1040 MixerStrip::speed_changed ()
1041 {
1042         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_speed_display));
1043 }
1044
1045 void
1046 MixerStrip::update_speed_display ()
1047 {
1048         float val;
1049         
1050         val = get_diskstream()->speed();
1051
1052         if (val != 1.0) {
1053                 speed_spinner.set_name ("MixerStripSpeedBaseNotOne");
1054         } else {
1055                 speed_spinner.set_name ("MixerStripSpeedBase");
1056         }
1057
1058         if (speed_adjustment.get_value() != val) {
1059                 ignore_speed_adjustment = true;
1060                 speed_adjustment.set_value (val);
1061                 ignore_speed_adjustment = false;
1062         }
1063 }                       
1064
1065
1066 void
1067 MixerStrip::set_selected (bool yn)
1068 {
1069         AxisView::set_selected (yn);
1070         if (_selected) {
1071                 global_frame.set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
1072                 global_frame.set_name ("MixerStripSelectedFrame");
1073         } else {
1074                 global_frame.set_shadow_type (Gtk::SHADOW_IN);
1075                 global_frame.set_name ("MixerStripFrame");
1076         }
1077         global_frame.queue_draw ();
1078 }
1079
1080 void
1081 MixerStrip::name_changed (void *src)
1082 {
1083         switch (_width) {
1084         case Wide:
1085                 RouteUI::name_changed (src);
1086                 break;
1087         case Narrow:
1088                 name_label.set_text (PBD::short_version (_route->name(), 5));
1089                 break;
1090         }
1091         if (_route->phase_invert()) {
1092                 name_label.set_text (X_("Ø ") + name_label.get_text());
1093         }
1094 }
1095
1096 void
1097 MixerStrip::width_clicked ()
1098 {
1099         switch (_width) {
1100         case Wide:
1101                 set_width (Narrow, this);
1102                 break;
1103         case Narrow:
1104                 set_width (Wide, this);
1105                 break;
1106         }
1107 }
1108
1109 void
1110 MixerStrip::hide_clicked ()
1111 {
1112     // LAME fix to reset the button status for when it is redisplayed (part 1)
1113     hide_button.set_sensitive(false);
1114     
1115         if (_embedded) {
1116                  Hiding(); /* EMIT_SIGNAL */
1117         } else {
1118                 _mixer.hide_strip (this);
1119         }
1120         
1121     // (part 2)
1122         hide_button.set_sensitive(true);
1123 }
1124
1125 void
1126 MixerStrip::set_embedded (bool yn)
1127 {
1128         _embedded = yn;
1129 }
1130
1131 void
1132 MixerStrip::map_frozen ()
1133 {
1134         ENSURE_GUI_THREAD (mem_fun(*this, &MixerStrip::map_frozen));
1135
1136         boost::shared_ptr<AudioTrack> at = audio_track();
1137
1138         if (at) {
1139                 switch (at->freeze_state()) {
1140                 case AudioTrack::Frozen:
1141                         pre_redirect_box.set_sensitive (false);
1142                         post_redirect_box.set_sensitive (false);
1143                         speed_spinner.set_sensitive (false);
1144                         break;
1145                 default:
1146                         pre_redirect_box.set_sensitive (true);
1147                         post_redirect_box.set_sensitive (true);
1148                         speed_spinner.set_sensitive (true);
1149                         break;
1150                 }
1151         }
1152         _route->foreach_redirect (this, &MixerStrip::hide_redirect_editor);
1153 }
1154
1155 void
1156 MixerStrip::hide_redirect_editor (boost::shared_ptr<Redirect> redirect)
1157 {
1158         void* gui = redirect->get_gui ();
1159         
1160         if (gui) {
1161                 static_cast<Gtk::Widget*>(gui)->hide ();
1162         }
1163 }
1164
1165 void
1166 MixerStrip::route_active_changed ()
1167 {
1168         RouteUI::route_active_changed ();
1169
1170         if (is_audio_track()) {
1171                 if (_route->active()) {
1172                         set_name ("AudioTrackStripBase");
1173                         gpm.set_meter_strip_name ("AudioTrackMetrics");
1174                 } else {
1175                         set_name ("AudioTrackStripBaseInactive");
1176                         gpm.set_meter_strip_name ("AudioTrackMetricsInactive");
1177                 }
1178                 gpm.set_fader_name ("AudioTrackFader");
1179         } else { // FIXME: assumed audio bus
1180                 if (_route->active()) {
1181                         set_name ("AudioBusStripBase");
1182                         gpm.set_meter_strip_name ("AudioBusMetrics");
1183                 } else {
1184                         set_name ("AudioBusStripBaseInactive");
1185                         gpm.set_meter_strip_name ("AudioBusMetricsInactive");
1186                 }
1187                 gpm.set_fader_name ("AudioBusFader");
1188         }
1189 }
1190
1191 RouteGroup*
1192 MixerStrip::mix_group() const
1193 {
1194         return _route->mix_group();
1195 }
1196
1197 void
1198 MixerStrip::engine_stopped ()
1199 {
1200 }
1201
1202 void
1203 MixerStrip::engine_running ()
1204 {
1205 }
1206
1207 void
1208 MixerStrip::meter_changed (void *src)
1209 {
1210
1211         ENSURE_GUI_THREAD (bind (mem_fun(*this, &MixerStrip::meter_changed), src));
1212
1213                 switch (_route->meter_point()) {
1214                 case MeterInput:
1215                         meter_point_label.set_text (_("input"));
1216                         break;
1217                         
1218                 case MeterPreFader:
1219                         meter_point_label.set_text (_("pre"));
1220                         break;
1221                         
1222                 case MeterPostFader:
1223                         meter_point_label.set_text (_("post"));
1224                         break;
1225                 }
1226
1227                 gpm.setup_meters ();
1228 }
1229