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