Merged with trunk R1761
[ardour.git] / gtk2_ardour / mixer_strip.cc
1 /*
2     Copyright (C) 2000-2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cmath>
20 #include <algorithm>
21
22 #include <sigc++/bind.h>
23
24 #include <pbd/convert.h>
25 #include <pbd/enumwriter.h>
26
27 #include <gtkmm2ext/gtk_ui.h>
28 #include <gtkmm2ext/utils.h>
29 #include <gtkmm2ext/choice.h>
30 #include <gtkmm2ext/stop_signal.h>
31 #include <gtkmm2ext/doi.h>
32 #include <gtkmm2ext/slider_controller.h>
33 #include <gtkmm2ext/bindable_button.h>
34
35 #include <ardour/ardour.h>
36 #include <ardour/session.h>
37 #include <ardour/audioengine.h>
38 #include <ardour/route.h>
39 #include <ardour/audio_track.h>
40 #include <ardour/audio_diskstream.h>
41 #include <ardour/panner.h>
42 #include <ardour/send.h>
43 #include <ardour/insert.h>
44 #include <ardour/ladspa_plugin.h>
45 #include <ardour/connection.h>
46 #include <ardour/session_connection.h>
47
48 #include "ardour_ui.h"
49 #include "ardour_dialog.h"
50 #include "mixer_strip.h"
51 #include "mixer_ui.h"
52 #include "keyboard.h"
53 #include "public_editor.h"
54 #include "send_ui.h"
55 #include "io_selector.h"
56 #include "utils.h"
57 #include "gui_thread.h"
58
59 #include "i18n.h"
60
61 using namespace sigc;
62 using namespace ARDOUR;
63 using namespace PBD;
64 using namespace Gtk;
65 using namespace Gtkmm2ext;
66 using namespace std;
67
68 int MixerStrip::scrollbar_height = 0;
69
70 #ifdef VARISPEED_IN_MIXER_STRIP
71 static void 
72 speed_printer (char buf[32], Gtk::Adjustment& adj, void* arg)
73 {
74         float val = adj.get_value ();
75
76         if (val == 1.0) {
77                 strcpy (buf, "1");
78         } else {
79                 snprintf (buf, 32, "%.3f", val);
80         }
81 }
82 #endif 
83
84 MixerStrip::MixerStrip (Mixer_UI& mx, Session& sess, boost::shared_ptr<Route> rt, bool in_mixer)
85         : AxisView(sess),
86           RouteUI (rt, sess, _("Mute"), _("Solo"), _("Record")),
87           _mixer(mx),
88           pre_redirect_box (PreFader, sess, rt, mx.plugin_selector(), mx.selection(), in_mixer),
89           post_redirect_box (PostFader, sess, rt, mx.plugin_selector(), mx.selection(), in_mixer),
90           gpm (_route, sess),
91           panners (_route, sess),
92           button_table (3, 2),
93           middle_button_table (1, 2),
94           bottom_button_table (1, 2),
95           meter_point_label (_("pre")),
96           comment_button (_("Comments")),
97           speed_adjustment (1.0, 0.001, 4.0, 0.001, 0.1),
98           speed_spinner (&speed_adjustment, "MixerStripSpeedBase", true)
99
100 {
101         if (set_color_from_route()) {
102                 set_color (unique_random_color());
103         }
104
105         input_selector = 0;
106         output_selector = 0;
107         group_menu = 0;
108         _marked_for_display = false;
109         route_ops_menu = 0;
110         ignore_comment_edit = false;
111         ignore_toggle = false;
112         ignore_speed_adjustment = false;
113         comment_window = 0;
114         comment_area = 0;
115         _width_owner = 0;
116
117         width_button.add (*(manage (new Gtk::Image (::get_icon("strip_width")))));
118         hide_button.add (*(manage (new Gtk::Image (::get_icon("hide")))));
119
120         input_label.set_text (_("Input"));
121         input_button.add (input_label);
122         input_button.set_name ("MixerIOButton");
123         input_label.set_name ("MixerIOButtonLabel");
124
125         output_label.set_text (_("Output"));
126         output_button.add (output_label);
127         output_button.set_name ("MixerIOButton");
128         output_label.set_name ("MixerIOButtonLabel");
129
130         _route->meter_change.connect (mem_fun(*this, &MixerStrip::meter_changed));
131         meter_point_button.add (meter_point_label);
132         meter_point_button.set_name ("MixerStripMeterPreButton");
133         meter_point_label.set_name ("MixerStripMeterPreButton");
134         
135         switch (_route->meter_point()) {
136         case MeterInput:
137                 meter_point_label.set_text (_("input"));
138                 break;
139                 
140         case MeterPreFader:
141                 meter_point_label.set_text (_("pre"));
142                 break;
143                 
144         case MeterPostFader:
145                 meter_point_label.set_text (_("post"));
146                 break;
147         }
148         
149         /* TRANSLATORS: this string should be longest of the strings
150            used to describe meter points. In english, it's "input".
151         */
152         set_size_request_to_display_given_text (meter_point_button, _("tupni"), 5, 5);
153     
154         bottom_button_table.attach (meter_point_button, 1, 2, 0, 1);
155     
156         meter_point_button.signal_button_press_event().connect (mem_fun (gpm, &GainMeter::meter_press), false);
157         /* XXX what is this meant to do? */
158         //meter_point_button.signal_button_release_event().connect (mem_fun (gpm, &GainMeter::meter_release), false);
159
160         hide_button.set_events (hide_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
161
162         mute_button->set_name ("MixerMuteButton");
163         solo_button->set_name ("MixerSoloButton");
164
165         button_table.set_homogeneous (true);
166         button_table.set_spacings (0);
167
168         button_table.attach (name_button, 0, 2, 0, 1);
169         button_table.attach (input_button, 0, 2, 1, 2);
170
171         middle_button_table.set_homogeneous (true);
172         middle_button_table.set_spacings (0);
173         middle_button_table.attach (*mute_button, 0, 1, 0, 1);
174         middle_button_table.attach (*solo_button, 1, 2, 0, 1);
175
176         bottom_button_table.set_col_spacings (0);
177         bottom_button_table.set_homogeneous (true);
178         bottom_button_table.attach (group_button, 0, 1, 0, 1);
179         
180         if (is_audio_track()) {
181                 
182                 rec_enable_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::rec_enable_press), false);
183                 rec_enable_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::rec_enable_release));
184
185                 rec_enable_button->set_name ("MixerRecordEnableButton");
186
187                 AudioTrack* at = audio_track();
188
189                 at->FreezeChange.connect (mem_fun(*this, &MixerStrip::map_frozen));
190
191 #ifdef VARISPEED_IN_MIXER_STRIP
192                 speed_adjustment.signal_value_changed().connect (mem_fun(*this, &MixerStrip::speed_adjustment_changed));
193                 
194                 speed_frame.set_name ("BaseFrame");
195                 speed_frame.set_shadow_type (Gtk::SHADOW_IN);
196                 speed_frame.add (speed_spinner);
197                 
198                 speed_spinner.set_print_func (speed_printer, 0);
199
200                 ARDOUR_UI::instance()->tooltips().set_tip (speed_spinner, _("Varispeed"));
201
202                 button_table.attach (speed_frame, 0, 2, 5, 6);
203 #endif /* VARISPEED_IN_MIXER_STRIP */
204
205                 button_table.attach (*rec_enable_button, 0, 2, 2, 3);
206         }
207
208         name_button.add (name_label);
209         name_button.set_name ("MixerNameButton");
210         Gtkmm2ext::set_size_request_to_display_given_text (name_button, "longest label", 2, 2);
211
212         name_label.set_name ("MixerNameButtonLabel");
213         if (_route->phase_invert()) {
214                 name_label.set_text (X_("Ø ") + name_label.get_text());
215         } else {
216                 name_label.set_text (_route->name());
217         }
218
219         group_button.add (group_label);
220         group_button.set_name ("MixerGroupButton");
221         group_label.set_name ("MixerGroupButtonLabel");
222
223         comment_button.set_name ("MixerCommentButton");
224
225         ARDOUR_UI::instance()->tooltips().set_tip (comment_button, _route->comment()==""        ?
226                                                         _("Click to Add/Edit Comments"):
227                                                         _route->comment());
228
229         comment_button.signal_clicked().connect (mem_fun(*this, &MixerStrip::comment_button_clicked));
230         
231         global_vpacker.set_border_width (0);
232         global_vpacker.set_spacing (0);
233
234         VBox *whvbox = manage (new VBox);
235
236         width_button.set_name ("MixerWidthButton");
237         hide_button.set_name ("MixerHideButton");
238         top_event_box.set_name ("MixerTopEventBox");
239
240         width_button.signal_clicked().connect (mem_fun(*this, &MixerStrip::width_clicked));
241         hide_button.signal_clicked().connect (mem_fun(*this, &MixerStrip::hide_clicked));
242
243         width_hide_box.pack_start (width_button, false, true);
244         width_hide_box.pack_start (top_event_box, true, true);
245         width_hide_box.pack_end (hide_button, false, true);
246         Gtk::Alignment *gain_meter_alignment = Gtk::manage(new Gtk::Alignment());
247         gain_meter_alignment->set_padding(0, 4, 0, 0);
248         gain_meter_alignment->add(gpm);
249
250         whvbox->pack_start (width_hide_box, true, true);
251
252         global_vpacker.pack_start (*whvbox, Gtk::PACK_SHRINK);
253         global_vpacker.pack_start (button_table,Gtk::PACK_SHRINK);
254         global_vpacker.pack_start (pre_redirect_box, true, true);
255         global_vpacker.pack_start (middle_button_table,Gtk::PACK_SHRINK);
256         global_vpacker.pack_start (*gain_meter_alignment,Gtk::PACK_SHRINK);
257         global_vpacker.pack_start (bottom_button_table,Gtk::PACK_SHRINK);
258         global_vpacker.pack_start (post_redirect_box, true, true);
259         global_vpacker.pack_start (panners, Gtk::PACK_SHRINK);
260         global_vpacker.pack_start (output_button, Gtk::PACK_SHRINK);
261         global_vpacker.pack_start (comment_button, Gtk::PACK_SHRINK);
262
263         if (route()->master() || route()->control()) {
264                 
265                 if (scrollbar_height == 0) {
266                         HScrollbar scrollbar;
267                         Gtk::Requisition requisition(scrollbar.size_request ());
268                         scrollbar_height = requisition.height;
269                 }
270
271                 EventBox* spacer = manage (new EventBox);
272                 spacer->set_size_request (-1, scrollbar_height);
273                 global_vpacker.pack_start (*spacer, false, false);
274         }
275
276         global_frame.add (global_vpacker);
277         global_frame.set_shadow_type (Gtk::SHADOW_IN);
278         global_frame.set_name ("BaseFrame");
279
280         add (global_frame);
281
282         /* force setting of visible selected status */
283
284         _selected = true;
285         set_selected (false);
286
287         _packed = false;
288         _embedded = false;
289
290         _session.engine().Stopped.connect (mem_fun(*this, &MixerStrip::engine_stopped));
291         _session.engine().Running.connect (mem_fun(*this, &MixerStrip::engine_running));
292         _route->input_changed.connect (mem_fun(*this, &MixerStrip::input_changed));
293         _route->output_changed.connect (mem_fun(*this, &MixerStrip::output_changed));
294         _route->mute_changed.connect (mem_fun(*this, &RouteUI::mute_changed));
295         _route->solo_changed.connect (mem_fun(*this, &RouteUI::solo_changed));
296         _route->solo_safe_changed.connect (mem_fun(*this, &RouteUI::solo_changed));
297         _route->mix_group_changed.connect (mem_fun(*this, &MixerStrip::mix_group_changed));
298         _route->panner().Changed.connect (mem_fun(*this, &MixerStrip::connect_to_pan));
299
300         if (is_audio_track()) {
301                 audio_track()->DiskstreamChanged.connect (mem_fun(*this, &MixerStrip::diskstream_changed));
302                 get_diskstream()->SpeedChanged.connect (mem_fun(*this, &MixerStrip::speed_changed));
303         }
304
305         _route->name_changed.connect (mem_fun(*this, &RouteUI::name_changed));
306         _route->comment_changed.connect (mem_fun(*this, &MixerStrip::comment_changed));
307         _route->gui_changed.connect (mem_fun(*this, &MixerStrip::route_gui_changed));
308
309         input_button.signal_button_press_event().connect (mem_fun(*this, &MixerStrip::input_press), false);
310         output_button.signal_button_press_event().connect (mem_fun(*this, &MixerStrip::output_press), false);
311
312         solo_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::solo_press), false);
313         solo_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::solo_release), false);
314         mute_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::mute_press), false);
315         mute_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::mute_release), false);
316
317         name_button.signal_button_press_event().connect (mem_fun(*this, &MixerStrip::name_button_button_press), false);
318         group_button.signal_button_press_event().connect (mem_fun(*this, &MixerStrip::select_mix_group), false);
319
320         _width = (Width) -1;
321         set_stuff_from_route ();
322
323         /* start off as a passthru strip. we'll correct this, if necessary,
324            in update_diskstream_display().
325         */
326
327         set_name ("AudioTrackStripBase");
328
329         /* now force an update of all the various elements */
330
331         pre_redirect_box.update();
332         post_redirect_box.update();
333         mute_changed (0);
334         solo_changed (0);
335         name_changed (0);
336         comment_changed (0);
337         mix_group_changed (0);
338
339         connect_to_pan ();
340
341         panners.setup_pan ();
342
343         if (is_audio_track()) {
344                 speed_changed ();
345         }
346
347         update_diskstream_display ();
348         update_input_display ();
349         update_output_display ();
350
351         add_events (Gdk::BUTTON_RELEASE_MASK);
352 }
353
354 MixerStrip::~MixerStrip ()
355 {
356         GoingAway(); /* EMIT_SIGNAL */
357
358         if (input_selector) {
359                 delete input_selector;
360         }
361
362         if (output_selector) {
363                 delete output_selector;
364         }
365 }
366
367 void
368 MixerStrip::set_stuff_from_route ()
369 {
370         XMLProperty *prop;
371
372         ensure_xml_node ();
373
374         /* if width is not set, it will be set by the MixerUI or editor */
375
376         if ((prop = xml_node->property ("strip_width")) != 0) {
377                 set_width (Width (string_2_enum (prop->value(), _width)), this);
378         }
379
380         if ((prop = xml_node->property ("shown_mixer")) != 0) {
381                 if (prop->value() == "no") {
382                         _marked_for_display = false;
383                 } else {
384                         _marked_for_display = true;
385                 }
386         } else {
387                 /* backwards compatibility */
388                 _marked_for_display = true;
389         }
390 }
391
392 void
393 MixerStrip::set_width (Width w, void* owner)
394 {
395         /* always set the gpm width again, things may be hidden */
396
397         gpm.set_width (w);
398         panners.set_width (w);
399         pre_redirect_box.set_width (w);
400         post_redirect_box.set_width (w);
401
402         _width_owner = owner;
403
404         if (_width == w) {
405                 return;
406         }
407
408         ensure_xml_node ();
409         
410         _width = w;
411
412         if (_width_owner == this) {
413                 xml_node->add_property ("strip_width", enum_2_string (_width));
414         }
415
416         switch (w) {
417         case Wide:
418                 set_size_request (-1, -1);
419                 
420                 if (rec_enable_button)  {
421                         ((Gtk::Label*)rec_enable_button->get_child())->set_text (_("record"));
422                 }
423                 ((Gtk::Label*)mute_button->get_child())->set_text  (_("Mute"));
424                 ((Gtk::Label*)solo_button->get_child())->set_text (_("Solo"));
425
426                 if (_route->comment() == "") {
427                        comment_button.unset_bg (STATE_NORMAL);
428                        ((Gtk::Label*)comment_button.get_child())->set_text (_("comments"));
429                 } else {
430                        comment_button.modify_bg (STATE_NORMAL, color());
431                        ((Gtk::Label*)comment_button.get_child())->set_text (_("*comments*"));
432                 }
433
434                 ((Gtk::Label*)gpm.gain_automation_style_button.get_child())->set_text (gpm.astyle_string(_route->gain_automation_curve().automation_style()));
435                 ((Gtk::Label*)gpm.gain_automation_state_button.get_child())->set_text (gpm.astate_string(_route->gain_automation_curve().automation_state()));
436                 ((Gtk::Label*)panners.pan_automation_style_button.get_child())->set_text (panners.astyle_string(_route->panner().automation_style()));
437                 ((Gtk::Label*)panners.pan_automation_state_button.get_child())->set_text (panners.astate_string(_route->panner().automation_state()));
438                 Gtkmm2ext::set_size_request_to_display_given_text (name_button, "long", 2, 2);
439                 break;
440
441         case Narrow:
442                 if (rec_enable_button) {
443                         ((Gtk::Label*)rec_enable_button->get_child())->set_text (_("Rec"));
444                 }
445                 ((Gtk::Label*)mute_button->get_child())->set_text (_("M"));
446                 ((Gtk::Label*)solo_button->get_child())->set_text (_("S"));
447
448                 if (_route->comment() == "") {
449                        comment_button.unset_bg (STATE_NORMAL);
450                        ((Gtk::Label*)comment_button.get_child())->set_text (_("Cmt"));
451                 } else {
452                        comment_button.modify_bg (STATE_NORMAL, color());
453                        ((Gtk::Label*)comment_button.get_child())->set_text (_("*Cmt*"));
454                 }
455
456                 ((Gtk::Label*)gpm.gain_automation_style_button.get_child())->set_text (gpm.short_astyle_string(_route->gain_automation_curve().automation_style()));
457                 ((Gtk::Label*)gpm.gain_automation_state_button.get_child())->set_text (gpm.short_astate_string(_route->gain_automation_curve().automation_state()));
458                 ((Gtk::Label*)panners.pan_automation_style_button.get_child())->set_text (panners.short_astyle_string(_route->panner().automation_style()));
459                 ((Gtk::Label*)panners.pan_automation_state_button.get_child())->set_text (panners.short_astate_string(_route->panner().automation_state()));
460                 Gtkmm2ext::set_size_request_to_display_given_text (name_button, "longest label", 2, 2);
461                 set_size_request (max (50, gpm.get_gm_width()), -1);
462                 break;
463         }
464
465         update_input_display ();
466         update_output_display ();
467         mix_group_changed (0);
468         name_changed (0);
469
470 }
471
472 void
473 MixerStrip::set_packed (bool yn)
474 {
475         _packed = yn;
476
477         ensure_xml_node ();
478
479         if (_packed) {
480                 xml_node->add_property ("shown_mixer", "yes");
481         } else {
482                 xml_node->add_property ("shown_mixer", "no");
483         }
484 }
485
486
487 gint
488 MixerStrip::output_press (GdkEventButton *ev)
489 {
490         using namespace Menu_Helpers;
491         if (!_session.engine().connected()) {
492                 MessageDialog msg (_("Not connected to JACK - no I/O changes are possible"));
493                 msg.run ();
494                 return true;
495         }
496
497         MenuList& citems = output_menu.items();
498         switch (ev->button) {
499
500         case 1:
501                 output_menu.set_name ("ArdourContextMenu");
502                 citems.clear();
503                 
504                 citems.push_back (MenuElem (_("Edit"), mem_fun(*this, &MixerStrip::edit_output_configuration)));
505                 citems.push_back (SeparatorElem());
506                 citems.push_back (MenuElem (_("Disconnect"), mem_fun (*(static_cast<RouteUI*>(this)), &RouteUI::disconnect_output)));
507                 citems.push_back (SeparatorElem());
508                 
509                 _session.foreach_connection (this, &MixerStrip::add_connection_to_output_menu);
510
511                 output_menu.popup (1, ev->time);
512                 break;
513                 
514         default:
515                 break;
516         }
517         return TRUE;
518 }
519
520 void
521 MixerStrip::edit_output_configuration ()
522 {
523         if (output_selector == 0) {
524                 output_selector = new IOSelectorWindow (_session, _route, false);
525         } 
526
527         if (output_selector->is_visible()) {
528                 output_selector->get_toplevel()->get_window()->raise();
529         } else {
530                 output_selector->show_all ();
531         }
532 }
533
534 void
535 MixerStrip::edit_input_configuration ()
536 {
537         if (input_selector == 0) {
538                 input_selector = new IOSelectorWindow (_session, _route, true);
539         } 
540
541         if (input_selector->is_visible()) {
542                 input_selector->get_toplevel()->get_window()->raise();
543         } else {
544                 input_selector->show_all ();
545         }
546 }
547
548 gint
549 MixerStrip::input_press (GdkEventButton *ev)
550 {
551         using namespace Menu_Helpers;
552
553         MenuList& citems = input_menu.items();
554         input_menu.set_name ("ArdourContextMenu");
555         citems.clear();
556         
557         if (!_session.engine().connected()) {
558                 MessageDialog msg (_("Not connected to JACK - no I/O changes are possible"));
559                 msg.run ();
560                 return true;
561         }
562
563         switch (ev->button) {
564
565         case 1:
566                 citems.push_back (MenuElem (_("Edit"), mem_fun(*this, &MixerStrip::edit_input_configuration)));
567                 citems.push_back (SeparatorElem());
568                 citems.push_back (MenuElem (_("Disconnect"), mem_fun (*(static_cast<RouteUI*>(this)), &RouteUI::disconnect_input)));
569                 citems.push_back (SeparatorElem());
570                 
571                 _session.foreach_connection (this, &MixerStrip::add_connection_to_input_menu);
572
573                 input_menu.popup (1, ev->time);
574                 break;
575                 
576         default:
577                 break;
578         }
579         return TRUE;
580 }
581
582 void
583 MixerStrip::connection_input_chosen (ARDOUR::Connection *c)
584 {
585         if (!ignore_toggle) {
586
587                 try { 
588                         _route->use_input_connection (*c, this);
589                 }
590
591                 catch (AudioEngine::PortRegistrationFailure& err) {
592                         error << _("could not register new ports required for that connection")
593                               << endmsg;
594                 }
595         }
596 }
597
598 void
599 MixerStrip::connection_output_chosen (ARDOUR::Connection *c)
600 {
601         if (!ignore_toggle) {
602
603                 try { 
604                         _route->use_output_connection (*c, this);
605                 }
606
607                 catch (AudioEngine::PortRegistrationFailure& err) {
608                         error << _("could not register new ports required for that connection")
609                               << endmsg;
610                 }
611         }
612 }
613
614 void
615 MixerStrip::add_connection_to_input_menu (ARDOUR::Connection* c)
616 {
617         using namespace Menu_Helpers;
618
619         if (dynamic_cast<InputConnection *> (c) == 0) {
620                 return;
621         }
622
623         MenuList& citems = input_menu.items();
624         
625         if (c->nports() == _route->n_inputs().get_total()) {
626
627                 citems.push_back (CheckMenuElem (c->name(), bind (mem_fun(*this, &MixerStrip::connection_input_chosen), c)));
628                 
629                 ARDOUR::Connection *current = _route->input_connection();
630                 
631                 if (current == c) {
632                         ignore_toggle = true;
633                         dynamic_cast<CheckMenuItem *> (&citems.back())->set_active (true);
634                         ignore_toggle = false;
635                 }
636         }
637 }
638
639 void
640 MixerStrip::add_connection_to_output_menu (ARDOUR::Connection* c)
641 {
642         using namespace Menu_Helpers;
643
644         if (dynamic_cast<OutputConnection *> (c) == 0) {
645                 return;
646         }
647
648         if (c->nports() == _route->n_outputs().get_total()) {
649
650                 MenuList& citems = output_menu.items();
651                 citems.push_back (CheckMenuElem (c->name(), bind (mem_fun(*this, &MixerStrip::connection_output_chosen), c)));
652                 
653                 ARDOUR::Connection *current = _route->output_connection();
654                 
655                 if (current == c) {
656                         ignore_toggle = true;
657                         dynamic_cast<CheckMenuItem *> (&citems.back())->set_active (true);
658                         ignore_toggle = false;
659                 }
660         }
661 }
662
663 void
664 MixerStrip::update_diskstream_display ()
665 {
666         if (is_audio_track()) {
667
668                 map_frozen ();
669
670                 update_input_display ();
671
672                 if (input_selector) {
673                         input_selector->hide_all ();
674                 }
675
676                 show_route_color ();
677
678         } else {
679
680                 map_frozen ();
681
682                 update_input_display ();
683                 show_passthru_color ();
684         }
685 }
686
687 void
688 MixerStrip::connect_to_pan ()
689 {
690         ENSURE_GUI_THREAD(mem_fun(*this, &MixerStrip::connect_to_pan));
691
692         panstate_connection.disconnect ();
693         panstyle_connection.disconnect ();
694
695         if (!_route->panner().empty()) {
696                 StreamPanner* sp = _route->panner().front();
697
698                 panstate_connection = sp->automation().automation_state_changed.connect (mem_fun(panners, &PannerUI::pan_automation_state_changed));
699                 panstyle_connection = sp->automation().automation_style_changed.connect (mem_fun(panners, &PannerUI::pan_automation_style_changed));
700         }
701
702         panners.pan_changed (this);
703 }
704
705 void
706 MixerStrip::update_input_display ()
707 {
708         ARDOUR::Connection *c;
709
710         if ((c = _route->input_connection()) != 0) {
711                 input_label.set_text (c->name());
712         } else {
713                 switch (_width) {
714                 case Wide:
715                         input_label.set_text (_(" Input"));
716                         break;
717                 case Narrow:
718                         input_label.set_text (_("I"));
719                         break;
720                 }
721         }
722         panners.setup_pan ();
723 }
724
725 void
726 MixerStrip::update_output_display ()
727 {
728         ARDOUR::Connection *c;
729
730         if ((c = _route->output_connection()) != 0) {
731                 output_label.set_text (c->name());
732         } else {
733                 switch (_width) {
734                 case Wide:
735                         output_label.set_text (_("Output"));
736                         break;
737                 case Narrow:
738                         output_label.set_text (_("O"));
739                         break;
740                 }
741         }
742         gpm.setup_meters ();
743         panners.setup_pan ();
744 }
745
746 void
747 MixerStrip::fast_update ()
748 {
749         gpm.update_meters ();
750 }
751
752 void
753 MixerStrip::diskstream_changed ()
754 {
755         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_diskstream_display));
756 }       
757
758 void
759 MixerStrip::input_changed (IOChange change, void *src)
760 {
761         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_input_display));
762 }
763
764 void
765 MixerStrip::output_changed (IOChange change, void *src)
766 {
767         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_output_display));
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
988         build_remote_control_menu ();
989         
990         items.push_back (SeparatorElem());
991         items.push_back (MenuElem (_("Remote Control ID"), *remote_control_menu));
992
993         items.push_back (SeparatorElem());
994         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &RouteUI::remove_this_route)));
995 }
996
997 gint
998 MixerStrip::name_button_button_press (GdkEventButton* ev)
999 {
1000         if (ev->button == 1) {
1001                 list_route_operations ();
1002
1003                 Menu_Helpers::MenuList& items = route_ops_menu->items();
1004                 /* do not allow rename if the track is record-enabled */
1005                 static_cast<MenuItem*> (&items.front())->set_sensitive (!_route->record_enabled());
1006
1007                 route_ops_menu->popup (1, ev->time);
1008         }
1009         return FALSE;
1010 }
1011
1012 void
1013 MixerStrip::list_route_operations ()
1014 {
1015         if (route_ops_menu == 0) {
1016                 build_route_ops_menu ();
1017         }
1018         
1019         refresh_remote_control_menu();
1020 }
1021
1022
1023 void
1024 MixerStrip::speed_adjustment_changed ()
1025 {
1026         /* since there is a usable speed adjustment, there has to be a diskstream */
1027         if (!ignore_speed_adjustment) {
1028                 get_diskstream()->set_speed (speed_adjustment.get_value());
1029         }
1030 }
1031
1032 void
1033 MixerStrip::speed_changed ()
1034 {
1035         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_speed_display));
1036 }
1037
1038 void
1039 MixerStrip::update_speed_display ()
1040 {
1041         float val;
1042         
1043         val = get_diskstream()->speed();
1044
1045         if (val != 1.0) {
1046                 speed_spinner.set_name ("MixerStripSpeedBaseNotOne");
1047         } else {
1048                 speed_spinner.set_name ("MixerStripSpeedBase");
1049         }
1050
1051         if (speed_adjustment.get_value() != val) {
1052                 ignore_speed_adjustment = true;
1053                 speed_adjustment.set_value (val);
1054                 ignore_speed_adjustment = false;
1055         }
1056 }                       
1057
1058
1059 void
1060 MixerStrip::set_selected (bool yn)
1061 {
1062         AxisView::set_selected (yn);
1063         if (_selected) {
1064                 global_frame.set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
1065                 global_frame.set_name ("MixerStripSelectedFrame");
1066         } else {
1067                 global_frame.set_shadow_type (Gtk::SHADOW_IN);
1068                 global_frame.set_name ("MixerStripFrame");
1069         }
1070         global_frame.queue_draw ();
1071 }
1072
1073 void
1074 MixerStrip::name_changed (void *src)
1075 {
1076         switch (_width) {
1077         case Wide:
1078                 RouteUI::name_changed (src);
1079                 break;
1080         case Narrow:
1081                 name_label.set_text (PBD::short_version (_route->name(), 5));
1082                 break;
1083         }
1084         if (_route->phase_invert()) {
1085                 name_label.set_text (X_("Ø ") + name_label.get_text());
1086         }
1087 }
1088
1089 void
1090 MixerStrip::width_clicked ()
1091 {
1092         switch (_width) {
1093         case Wide:
1094                 set_width (Narrow, this);
1095                 break;
1096         case Narrow:
1097                 set_width (Wide, this);
1098                 break;
1099         }
1100 }
1101
1102 void
1103 MixerStrip::hide_clicked ()
1104 {
1105     // LAME fix to reset the button status for when it is redisplayed (part 1)
1106     hide_button.set_sensitive(false);
1107     
1108         if (_embedded) {
1109                  Hiding(); /* EMIT_SIGNAL */
1110         } else {
1111                 _mixer.hide_strip (this);
1112         }
1113         
1114     // (part 2)
1115         hide_button.set_sensitive(true);
1116 }
1117
1118 void
1119 MixerStrip::set_embedded (bool yn)
1120 {
1121         _embedded = yn;
1122 }
1123
1124 void
1125 MixerStrip::map_frozen ()
1126 {
1127         ENSURE_GUI_THREAD (mem_fun(*this, &MixerStrip::map_frozen));
1128
1129         AudioTrack* at = audio_track();
1130
1131         if (at) {
1132                 switch (at->freeze_state()) {
1133                 case AudioTrack::Frozen:
1134                         pre_redirect_box.set_sensitive (false);
1135                         post_redirect_box.set_sensitive (false);
1136                         speed_spinner.set_sensitive (false);
1137                         break;
1138                 default:
1139                         pre_redirect_box.set_sensitive (true);
1140                         post_redirect_box.set_sensitive (true);
1141                         speed_spinner.set_sensitive (true);
1142                         break;
1143                 }
1144         }
1145         _route->foreach_redirect (this, &MixerStrip::hide_redirect_editor);
1146 }
1147
1148 void
1149 MixerStrip::hide_redirect_editor (boost::shared_ptr<Redirect> redirect)
1150 {
1151         void* gui = redirect->get_gui ();
1152         
1153         if (gui) {
1154                 static_cast<Gtk::Widget*>(gui)->hide ();
1155         }
1156 }
1157
1158 void
1159 MixerStrip::route_active_changed ()
1160 {
1161         RouteUI::route_active_changed ();
1162
1163         if (is_midi_track()) {
1164                 if (_route->active()) {
1165                         set_name ("MidiTrackStripBase");
1166                         gpm.set_meter_strip_name ("MidiTrackStripBase");
1167                 } else {
1168                         set_name ("MidiTrackStripBaseInactive");
1169                         gpm.set_meter_strip_name ("MidiTrackStripBaseInactive");
1170                 }
1171         } else if (is_audio_track()) {
1172                 if (_route->active()) {
1173                         set_name ("AudioTrackStripBase");
1174                         gpm.set_meter_strip_name ("AudioTrackStripBase");
1175                 } else {
1176                         set_name ("AudioTrackStripBaseInactive");
1177                         gpm.set_meter_strip_name ("AudioTrackStripBaseInactive");
1178                 }
1179                 gpm.set_fader_name ("AudioTrackFader");
1180         } else {
1181                 if (_route->active()) {
1182                         set_name ("AudioBusStripBase");
1183                         gpm.set_meter_strip_name ("AudioBusStripBase");
1184                 } else {
1185                         set_name ("AudioBusStripBaseInactive");
1186                         gpm.set_meter_strip_name ("AudioBusStripBaseInactive");
1187                 }
1188                 gpm.set_fader_name ("AudioBusFader");
1189         }
1190 }
1191
1192 RouteGroup*
1193 MixerStrip::mix_group() const
1194 {
1195         return _route->mix_group();
1196 }
1197
1198 void
1199 MixerStrip::engine_stopped ()
1200 {
1201 }
1202
1203 void
1204 MixerStrip::engine_running ()
1205 {
1206 }
1207
1208 void
1209 MixerStrip::meter_changed (void *src)
1210 {
1211
1212         ENSURE_GUI_THREAD (bind (mem_fun(*this, &MixerStrip::meter_changed), src));
1213
1214                 switch (_route->meter_point()) {
1215                 case MeterInput:
1216                         meter_point_label.set_text (_("input"));
1217                         break;
1218                         
1219                 case MeterPreFader:
1220                         meter_point_label.set_text (_("pre"));
1221                         break;
1222                         
1223                 case MeterPostFader:
1224                         meter_point_label.set_text (_("post"));
1225                         break;
1226                 }
1227
1228                 gpm.setup_meters ();
1229 }
1230