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