4efc2b0340bd51a9baaa773c3d040dd5f5825b2a
[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/bundle.h>
46 #include <ardour/session_bundle.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                 boost::shared_ptr<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         if (!is_midi_track())
260                 global_vpacker.pack_start (panners, Gtk::PACK_SHRINK);
261         global_vpacker.pack_start (output_button, Gtk::PACK_SHRINK);
262         global_vpacker.pack_start (comment_button, Gtk::PACK_SHRINK);
263
264         if (route()->master() || route()->control()) {
265                 
266                 if (scrollbar_height == 0) {
267                         HScrollbar scrollbar;
268                         Gtk::Requisition requisition(scrollbar.size_request ());
269                         scrollbar_height = requisition.height;
270                 }
271
272                 EventBox* spacer = manage (new EventBox);
273                 spacer->set_size_request (-1, scrollbar_height);
274                 global_vpacker.pack_start (*spacer, false, false);
275         }
276
277         global_frame.add (global_vpacker);
278         global_frame.set_shadow_type (Gtk::SHADOW_IN);
279         global_frame.set_name ("BaseFrame");
280
281         add (global_frame);
282
283         /* force setting of visible selected status */
284
285         _selected = true;
286         set_selected (false);
287
288         _packed = false;
289         _embedded = false;
290
291         _session.engine().Stopped.connect (mem_fun(*this, &MixerStrip::engine_stopped));
292         _session.engine().Running.connect (mem_fun(*this, &MixerStrip::engine_running));
293         _route->input_changed.connect (mem_fun(*this, &MixerStrip::input_changed));
294         _route->output_changed.connect (mem_fun(*this, &MixerStrip::output_changed));
295         _route->mute_changed.connect (mem_fun(*this, &RouteUI::mute_changed));
296         _route->solo_changed.connect (mem_fun(*this, &RouteUI::solo_changed));
297         _route->solo_safe_changed.connect (mem_fun(*this, &RouteUI::solo_changed));
298         _route->mix_group_changed.connect (mem_fun(*this, &MixerStrip::mix_group_changed));
299         _route->panner().Changed.connect (mem_fun(*this, &MixerStrip::connect_to_pan));
300
301         if (is_audio_track()) {
302                 audio_track()->DiskstreamChanged.connect (mem_fun(*this, &MixerStrip::diskstream_changed));
303                 get_diskstream()->SpeedChanged.connect (mem_fun(*this, &MixerStrip::speed_changed));
304         }
305
306         _route->name_changed.connect (mem_fun(*this, &RouteUI::name_changed));
307         _route->comment_changed.connect (mem_fun(*this, &MixerStrip::comment_changed));
308         _route->gui_changed.connect (mem_fun(*this, &MixerStrip::route_gui_changed));
309
310         input_button.signal_button_press_event().connect (mem_fun(*this, &MixerStrip::input_press), false);
311         output_button.signal_button_press_event().connect (mem_fun(*this, &MixerStrip::output_press), false);
312
313         solo_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::solo_press), false);
314         solo_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::solo_release), false);
315         mute_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::mute_press), false);
316         mute_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::mute_release), false);
317
318         name_button.signal_button_press_event().connect (mem_fun(*this, &MixerStrip::name_button_button_press), false);
319         group_button.signal_button_press_event().connect (mem_fun(*this, &MixerStrip::select_mix_group), false);
320
321         _width = (Width) -1;
322         set_stuff_from_route ();
323
324         /* start off as a passthru strip. we'll correct this, if necessary,
325            in update_diskstream_display().
326         */
327
328         if (is_midi_track())
329                 set_name ("MidiTrackStripBase");
330         else
331                 set_name ("AudioTrackStripBase");
332
333         /* now force an update of all the various elements */
334
335         pre_redirect_box.update();
336         post_redirect_box.update();
337         mute_changed (0);
338         solo_changed (0);
339         name_changed (0);
340         comment_changed (0);
341         mix_group_changed (0);
342
343         connect_to_pan ();
344
345         panners.setup_pan ();
346
347         if (is_audio_track()) {
348                 speed_changed ();
349         }
350
351         update_diskstream_display ();
352         update_input_display ();
353         update_output_display ();
354
355         add_events (Gdk::BUTTON_RELEASE_MASK);
356 }
357
358 MixerStrip::~MixerStrip ()
359 {
360         GoingAway(); /* EMIT_SIGNAL */
361
362         if (input_selector) {
363                 delete input_selector;
364         }
365
366         if (output_selector) {
367                 delete output_selector;
368         }
369 }
370
371 void
372 MixerStrip::set_stuff_from_route ()
373 {
374         XMLProperty *prop;
375
376         ensure_xml_node ();
377
378         /* if width is not set, it will be set by the MixerUI or editor */
379
380         if ((prop = xml_node->property ("strip_width")) != 0) {
381                 set_width (Width (string_2_enum (prop->value(), _width)), this);
382         }
383
384         if ((prop = xml_node->property ("shown_mixer")) != 0) {
385                 if (prop->value() == "no") {
386                         _marked_for_display = false;
387                 } else {
388                         _marked_for_display = true;
389                 }
390         } else {
391                 /* backwards compatibility */
392                 _marked_for_display = true;
393         }
394 }
395
396 void
397 MixerStrip::set_width (Width w, void* owner)
398 {
399         /* always set the gpm width again, things may be hidden */
400
401         gpm.set_width (w);
402         panners.set_width (w);
403         pre_redirect_box.set_width (w);
404         post_redirect_box.set_width (w);
405
406         _width_owner = owner;
407
408         if (_width == w) {
409                 return;
410         }
411
412         ensure_xml_node ();
413         
414         _width = w;
415
416         if (_width_owner == this) {
417                 xml_node->add_property ("strip_width", enum_2_string (_width));
418         }
419
420         switch (w) {
421         case Wide:
422                 set_size_request (-1, -1);
423                 
424                 if (rec_enable_button)  {
425                         ((Gtk::Label*)rec_enable_button->get_child())->set_text (_("record"));
426                 }
427                 ((Gtk::Label*)mute_button->get_child())->set_text  (_("Mute"));
428                 ((Gtk::Label*)solo_button->get_child())->set_text (_("Solo"));
429
430                 if (_route->comment() == "") {
431                        comment_button.unset_bg (STATE_NORMAL);
432                        ((Gtk::Label*)comment_button.get_child())->set_text (_("comments"));
433                 } else {
434                        comment_button.modify_bg (STATE_NORMAL, color());
435                        ((Gtk::Label*)comment_button.get_child())->set_text (_("*comments*"));
436                 }
437
438                 ((Gtk::Label*)gpm.gain_automation_style_button.get_child())->set_text (gpm.astyle_string(_route->gain_automation_curve().automation_style()));
439                 ((Gtk::Label*)gpm.gain_automation_state_button.get_child())->set_text (gpm.astate_string(_route->gain_automation_curve().automation_state()));
440                 ((Gtk::Label*)panners.pan_automation_style_button.get_child())->set_text (panners.astyle_string(_route->panner().automation_style()));
441                 ((Gtk::Label*)panners.pan_automation_state_button.get_child())->set_text (panners.astate_string(_route->panner().automation_state()));
442                 Gtkmm2ext::set_size_request_to_display_given_text (name_button, "long", 2, 2);
443                 break;
444
445         case Narrow:
446                 if (rec_enable_button) {
447                         ((Gtk::Label*)rec_enable_button->get_child())->set_text (_("Rec"));
448                 }
449                 ((Gtk::Label*)mute_button->get_child())->set_text (_("M"));
450                 ((Gtk::Label*)solo_button->get_child())->set_text (_("S"));
451
452                 if (_route->comment() == "") {
453                        comment_button.unset_bg (STATE_NORMAL);
454                        ((Gtk::Label*)comment_button.get_child())->set_text (_("Cmt"));
455                 } else {
456                        comment_button.modify_bg (STATE_NORMAL, color());
457                        ((Gtk::Label*)comment_button.get_child())->set_text (_("*Cmt*"));
458                 }
459
460                 ((Gtk::Label*)gpm.gain_automation_style_button.get_child())->set_text (gpm.short_astyle_string(_route->gain_automation_curve().automation_style()));
461                 ((Gtk::Label*)gpm.gain_automation_state_button.get_child())->set_text (gpm.short_astate_string(_route->gain_automation_curve().automation_state()));
462                 ((Gtk::Label*)panners.pan_automation_style_button.get_child())->set_text (panners.short_astyle_string(_route->panner().automation_style()));
463                 ((Gtk::Label*)panners.pan_automation_state_button.get_child())->set_text (panners.short_astate_string(_route->panner().automation_state()));
464                 Gtkmm2ext::set_size_request_to_display_given_text (name_button, "longest label", 2, 2);
465                 set_size_request (max (50, gpm.get_gm_width()), -1);
466                 break;
467         }
468
469         update_input_display ();
470         update_output_display ();
471         mix_group_changed (0);
472         name_changed (0);
473
474 }
475
476 void
477 MixerStrip::set_packed (bool yn)
478 {
479         _packed = yn;
480
481         ensure_xml_node ();
482
483         if (_packed) {
484                 xml_node->add_property ("shown_mixer", "yes");
485         } else {
486                 xml_node->add_property ("shown_mixer", "no");
487         }
488 }
489
490
491 gint
492 MixerStrip::output_press (GdkEventButton *ev)
493 {
494         using namespace Menu_Helpers;
495         if (!_session.engine().connected()) {
496                 MessageDialog msg (_("Not connected to JACK - no I/O changes are possible"));
497                 msg.run ();
498                 return true;
499         }
500
501         MenuList& citems = output_menu.items();
502         switch (ev->button) {
503
504         case 1:
505                 output_menu.set_name ("ArdourContextMenu");
506                 citems.clear();
507                 
508                 citems.push_back (MenuElem (_("Edit"), mem_fun(*this, &MixerStrip::edit_output_configuration)));
509                 citems.push_back (SeparatorElem());
510                 citems.push_back (MenuElem (_("Disconnect"), mem_fun (*(static_cast<RouteUI*>(this)), &RouteUI::disconnect_output)));
511                 citems.push_back (SeparatorElem());
512                 
513                 _session.foreach_bundle (this, &MixerStrip::add_bundle_to_output_menu);
514
515                 output_menu.popup (1, ev->time);
516                 break;
517                 
518         default:
519                 break;
520         }
521         return TRUE;
522 }
523
524 void
525 MixerStrip::edit_output_configuration ()
526 {
527         if (output_selector == 0) {
528                 output_selector = new IOSelectorWindow (_session, _route, false);
529         } 
530
531         if (output_selector->is_visible()) {
532                 output_selector->get_toplevel()->get_window()->raise();
533         } else {
534                 output_selector->show_all ();
535         }
536 }
537
538 void
539 MixerStrip::edit_input_configuration ()
540 {
541         if (input_selector == 0) {
542                 input_selector = new IOSelectorWindow (_session, _route, true);
543         } 
544
545         if (input_selector->is_visible()) {
546                 input_selector->get_toplevel()->get_window()->raise();
547         } else {
548                 input_selector->show_all ();
549         }
550 }
551
552 gint
553 MixerStrip::input_press (GdkEventButton *ev)
554 {
555         using namespace Menu_Helpers;
556
557         MenuList& citems = input_menu.items();
558         input_menu.set_name ("ArdourContextMenu");
559         citems.clear();
560         
561         if (!_session.engine().connected()) {
562                 MessageDialog msg (_("Not connected to JACK - no I/O changes are possible"));
563                 msg.run ();
564                 return true;
565         }
566
567         switch (ev->button) {
568
569         case 1:
570                 citems.push_back (MenuElem (_("Edit"), mem_fun(*this, &MixerStrip::edit_input_configuration)));
571                 citems.push_back (SeparatorElem());
572                 citems.push_back (MenuElem (_("Disconnect"), mem_fun (*(static_cast<RouteUI*>(this)), &RouteUI::disconnect_input)));
573                 citems.push_back (SeparatorElem());
574                 
575                 _session.foreach_bundle (this, &MixerStrip::add_bundle_to_input_menu);
576
577                 input_menu.popup (1, ev->time);
578                 break;
579                 
580         default:
581                 break;
582         }
583         return TRUE;
584 }
585
586 void
587 MixerStrip::bundle_input_chosen (ARDOUR::Bundle *c)
588 {
589         if (!ignore_toggle) {
590
591                 try { 
592                         _route->use_input_bundle (*c, this);
593                 }
594
595                 catch (AudioEngine::PortRegistrationFailure& err) {
596                         error << _("could not register new ports required for that bundle")
597                               << endmsg;
598                 }
599         }
600 }
601
602 void
603 MixerStrip::bundle_output_chosen (ARDOUR::Bundle *c)
604 {
605         if (!ignore_toggle) {
606
607                 try { 
608                         _route->use_output_bundle (*c, this);
609                 }
610
611                 catch (AudioEngine::PortRegistrationFailure& err) {
612                         error << _("could not register new ports required for that bundle")
613                               << endmsg;
614                 }
615         }
616 }
617
618 void
619 MixerStrip::add_bundle_to_input_menu (ARDOUR::Bundle* c)
620 {
621         using namespace Menu_Helpers;
622
623         if (dynamic_cast<InputBundle *> (c) == 0) {
624                 return;
625         }
626
627         MenuList& citems = input_menu.items();
628         
629         if (c->nchannels() == _route->n_inputs().n_total()) {
630
631                 citems.push_back (CheckMenuElem (c->name(), bind (mem_fun(*this, &MixerStrip::bundle_input_chosen), c)));
632                 
633                 ARDOUR::Bundle *current = _route->input_bundle();
634                 
635                 if (current == c) {
636                         ignore_toggle = true;
637                         dynamic_cast<CheckMenuItem *> (&citems.back())->set_active (true);
638                         ignore_toggle = false;
639                 }
640         }
641 }
642
643 void
644 MixerStrip::add_bundle_to_output_menu (ARDOUR::Bundle* c)
645 {
646         using namespace Menu_Helpers;
647
648         if (dynamic_cast<OutputBundle *> (c) == 0) {
649                 return;
650         }
651
652         if (c->nchannels() == _route->n_outputs().n_total()) {
653
654                 MenuList& citems = output_menu.items();
655                 citems.push_back (CheckMenuElem (c->name(), bind (mem_fun(*this, &MixerStrip::bundle_output_chosen), c)));
656                 
657                 ARDOUR::Bundle *current = _route->output_bundle();
658                 
659                 if (current == c) {
660                         ignore_toggle = true;
661                         dynamic_cast<CheckMenuItem *> (&citems.back())->set_active (true);
662                         ignore_toggle = false;
663                 }
664         }
665 }
666
667 void
668 MixerStrip::update_diskstream_display ()
669 {
670         if (is_audio_track()) {
671
672                 map_frozen ();
673
674                 update_input_display ();
675
676                 if (input_selector) {
677                         input_selector->hide_all ();
678                 }
679
680                 show_route_color ();
681
682         } else {
683
684                 map_frozen ();
685
686                 update_input_display ();
687                 show_passthru_color ();
688         }
689 }
690
691 void
692 MixerStrip::connect_to_pan ()
693 {
694         ENSURE_GUI_THREAD(mem_fun(*this, &MixerStrip::connect_to_pan));
695
696         panstate_connection.disconnect ();
697         panstyle_connection.disconnect ();
698
699         if (!_route->panner().empty()) {
700                 StreamPanner* sp = _route->panner().front();
701
702                 panstate_connection = sp->automation().automation_state_changed.connect (mem_fun(panners, &PannerUI::pan_automation_state_changed));
703                 panstyle_connection = sp->automation().automation_style_changed.connect (mem_fun(panners, &PannerUI::pan_automation_style_changed));
704         }
705
706         panners.pan_changed (this);
707 }
708
709 void
710 MixerStrip::update_input_display ()
711 {
712         ARDOUR::Bundle *c;
713
714         if ((c = _route->input_bundle()) != 0) {
715                 input_label.set_text (c->name());
716         } else {
717                 switch (_width) {
718                 case Wide:
719                         input_label.set_text (_(" Input"));
720                         break;
721                 case Narrow:
722                         input_label.set_text (_("I"));
723                         break;
724                 }
725         }
726         panners.setup_pan ();
727 }
728
729 void
730 MixerStrip::update_output_display ()
731 {
732         ARDOUR::Bundle *c;
733
734         if ((c = _route->output_bundle()) != 0) {
735                 output_label.set_text (c->name());
736         } else {
737                 switch (_width) {
738                 case Wide:
739                         output_label.set_text (_("Output"));
740                         break;
741                 case Narrow:
742                         output_label.set_text (_("O"));
743                         break;
744                 }
745         }
746         gpm.setup_meters ();
747         panners.setup_pan ();
748 }
749
750 void
751 MixerStrip::fast_update ()
752 {
753         gpm.update_meters ();
754 }
755
756 void
757 MixerStrip::diskstream_changed ()
758 {
759         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_diskstream_display));
760 }       
761
762 void
763 MixerStrip::input_changed (IOChange change, void *src)
764 {
765         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_input_display));
766 }
767
768 void
769 MixerStrip::output_changed (IOChange change, void *src)
770 {
771         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_output_display));
772 }
773
774
775 void 
776 MixerStrip::comment_editor_done_editing() 
777 {
778         string str =  comment_area->get_buffer()->get_text();
779         if (_route->comment() != str) {
780                 _route->set_comment (str, this);
781
782                 switch (_width) {
783                    
784                 case Wide:
785                         if (! str.empty()) {
786                                 comment_button.modify_bg (STATE_NORMAL, color());
787                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("*Comments*"));
788                         } else {
789                                 comment_button.unset_bg (STATE_NORMAL);
790                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("Comments"));
791                         }
792                         break;
793                    
794                 case Narrow:
795                         if (! str.empty()) {
796                                 comment_button.modify_bg (STATE_NORMAL, color());
797                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("*Cmt*"));
798                         } else {
799                                 comment_button.unset_bg (STATE_NORMAL);
800                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("Cmt"));
801                         } 
802                         break;
803                 }
804                  
805                 ARDOUR_UI::instance()->tooltips().set_tip (comment_button, 
806                                 str.empty() ? _("Click to Add/Edit Comments") : str);
807         }
808
809 }
810
811 void
812 MixerStrip::comment_button_clicked ()
813 {
814         if (comment_window == 0) {
815                 setup_comment_editor ();
816         }
817
818     int x, y, cw_width, cw_height;
819
820         if (comment_window->is_visible()) {
821                 comment_window->hide ();
822                 return;
823         }
824
825         comment_window->get_size (cw_width, cw_height);
826         comment_window->get_position(x, y);
827         comment_window->move(x, y - (cw_height / 2) - 45);
828         /* 
829            half the dialog height minus the comments button height 
830            with some window decoration fudge thrown in.
831         */
832
833         comment_window->show();
834         comment_window->present();
835 }
836
837 void
838 MixerStrip::setup_comment_editor ()
839 {
840         string title;
841         title = _route->name();
842         title += _(": comment editor");
843
844         comment_window = new ArdourDialog (title, false);
845         comment_window->set_position (Gtk::WIN_POS_MOUSE);
846         comment_window->set_skip_taskbar_hint (true);
847         comment_window->signal_hide().connect (mem_fun(*this, &MixerStrip::comment_editor_done_editing));
848
849         comment_area = manage (new TextView());
850         comment_area->set_name ("MixerTrackCommentArea");
851         comment_area->set_size_request (110, 178);
852         comment_area->set_wrap_mode (WRAP_WORD);
853         comment_area->set_editable (true);
854         comment_area->get_buffer()->set_text (_route->comment());
855         comment_area->show ();
856
857         comment_window->get_vbox()->pack_start (*comment_area);
858         comment_window->get_action_area()->hide();
859 }
860
861 void
862 MixerStrip::comment_changed (void *src)
863 {
864         ENSURE_GUI_THREAD(bind (mem_fun(*this, &MixerStrip::comment_changed), src));
865         
866         if (src != this) {
867                 ignore_comment_edit = true;
868                 if (comment_area) {
869                         comment_area->get_buffer()->set_text (_route->comment());
870                 }
871                 ignore_comment_edit = false;
872         }
873 }
874
875 void
876 MixerStrip::set_mix_group (RouteGroup *rg)
877 {
878         _route->set_mix_group (rg, this);
879 }
880
881 void
882 MixerStrip::add_mix_group_to_menu (RouteGroup *rg, RadioMenuItem::Group* group)
883 {
884         using namespace Menu_Helpers;
885
886         MenuList& items = group_menu->items();
887
888         items.push_back (RadioMenuElem (*group, rg->name(), bind (mem_fun(*this, &MixerStrip::set_mix_group), rg)));
889
890         if (_route->mix_group() == rg) {
891                 static_cast<RadioMenuItem*>(&items.back())->set_active ();
892         }
893 }
894
895 bool
896 MixerStrip::select_mix_group (GdkEventButton *ev)
897 {
898         using namespace Menu_Helpers;
899
900         if (group_menu == 0) {
901                 group_menu = new Menu;
902         } 
903         group_menu->set_name ("ArdourContextMenu");
904         MenuList& items = group_menu->items();
905         RadioMenuItem::Group group;
906
907         switch (ev->button) {
908         case 1:
909
910                 items.clear ();
911                 items.push_back (RadioMenuElem (group, _("No group"), bind (mem_fun(*this, &MixerStrip::set_mix_group), (RouteGroup *) 0)));
912
913                 _session.foreach_mix_group (bind (mem_fun (*this, &MixerStrip::add_mix_group_to_menu), &group));
914
915                 group_menu->popup (1, ev->time);
916                 break;
917
918         default:
919                 break;
920         }
921         
922         return true;
923 }       
924
925 void
926 MixerStrip::mix_group_changed (void *ignored)
927 {
928         ENSURE_GUI_THREAD(bind (mem_fun(*this, &MixerStrip::mix_group_changed), ignored));
929         
930         RouteGroup *rg = _route->mix_group();
931         
932         if (rg) {
933                 group_label.set_text (rg->name());
934         } else {
935                 switch (_width) {
936                 case Wide:
937                         group_label.set_text (_("Grp"));
938                         break;
939                 case Narrow:
940                         group_label.set_text (_("~G"));
941                         break;
942                 }
943         }
944 }
945
946
947 void 
948 MixerStrip::route_gui_changed (string what_changed, void* ignored)
949 {
950         ENSURE_GUI_THREAD(bind (mem_fun(*this, &MixerStrip::route_gui_changed), what_changed, ignored));
951         
952         if (what_changed == "color") {
953                 if (set_color_from_route () == 0) {
954                         show_route_color ();
955                 }
956         }
957 }
958
959 void
960 MixerStrip::show_route_color ()
961 {
962         name_button.modify_bg (STATE_NORMAL, color());
963         top_event_box.modify_bg (STATE_NORMAL, color());
964         route_active_changed ();
965 }
966
967 void
968 MixerStrip::show_passthru_color ()
969 {
970         route_active_changed ();
971 }
972
973 void
974 MixerStrip::build_route_ops_menu ()
975 {
976         using namespace Menu_Helpers;
977         route_ops_menu = manage (new Menu);
978         route_ops_menu->set_name ("ArdourContextMenu");
979
980         MenuList& items = route_ops_menu->items();
981
982         items.push_back (MenuElem (_("Rename"), mem_fun(*this, &RouteUI::route_rename)));
983         items.push_back (SeparatorElem());
984         items.push_back (CheckMenuElem (_("Active"), mem_fun (*this, &RouteUI::toggle_route_active)));
985         route_active_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
986         route_active_menu_item->set_active (_route->active());
987         items.push_back (SeparatorElem());
988         items.push_back (CheckMenuElem (_("Invert Polarity"), mem_fun (*this, &RouteUI::toggle_polarity)));
989         polarity_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
990         polarity_menu_item->set_active (_route->phase_invert());
991         items.push_back (CheckMenuElem (_("Protect against denormals"), mem_fun (*this, &RouteUI::toggle_denormal_protection)));
992         denormal_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
993         denormal_menu_item->set_active (_route->denormal_protection());
994
995         build_remote_control_menu ();
996         
997         items.push_back (SeparatorElem());
998         items.push_back (MenuElem (_("Remote Control ID"), *remote_control_menu));
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_midi_track()) {
1171                 if (_route->active()) {
1172                         set_name ("MidiTrackStripBase");
1173                         gpm.set_meter_strip_name ("MidiTrackStripBase");
1174                 } else {
1175                         set_name ("MidiTrackStripBaseInactive");
1176                         gpm.set_meter_strip_name ("MidiTrackStripBaseInactive");
1177                 }
1178                 gpm.set_fader_name ("MidiTrackFader");
1179         } else if (is_audio_track()) {
1180                 if (_route->active()) {
1181                         set_name ("AudioTrackStripBase");
1182                         gpm.set_meter_strip_name ("AudioTrackStripBase");
1183                 } else {
1184                         set_name ("AudioTrackStripBaseInactive");
1185                         gpm.set_meter_strip_name ("AudioTrackStripBaseInactive");
1186                 }
1187                 gpm.set_fader_name ("AudioTrackFader");
1188         } else {
1189                 if (_route->active()) {
1190                         set_name ("AudioBusStripBase");
1191                         gpm.set_meter_strip_name ("AudioBusStripBase");
1192                 } else {
1193                         set_name ("AudioBusStripBaseInactive");
1194                         gpm.set_meter_strip_name ("AudioBusStripBaseInactive");
1195                 }
1196                 gpm.set_fader_name ("AudioBusFader");
1197                 
1198                 /* (no MIDI busses yet) */
1199         }
1200 }
1201
1202 RouteGroup*
1203 MixerStrip::mix_group() const
1204 {
1205         return _route->mix_group();
1206 }
1207
1208 void
1209 MixerStrip::engine_stopped ()
1210 {
1211 }
1212
1213 void
1214 MixerStrip::engine_running ()
1215 {
1216 }
1217
1218 void
1219 MixerStrip::meter_changed (void *src)
1220 {
1221
1222         ENSURE_GUI_THREAD (bind (mem_fun(*this, &MixerStrip::meter_changed), src));
1223
1224         switch (_route->meter_point()) {
1225                 case MeterInput:
1226                         meter_point_label.set_text (_("input"));
1227                         break;
1228
1229                 case MeterPreFader:
1230                         meter_point_label.set_text (_("pre"));
1231                         break;
1232
1233                 case MeterPostFader:
1234                         meter_point_label.set_text (_("post"));
1235                         break;
1236         }
1237
1238         gpm.setup_meters ();
1239 }
1240