2459ac57a83dc8862bdcff9d63b409d1cdba241c
[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         if (!_route->panner().empty()) {
749                 StreamPanner* sp = _route->panner().front();
750
751                 panstate_connection = sp->pan_control()->alist()->automation_state_changed.connect (mem_fun(panners, &PannerUI::pan_automation_state_changed));
752                 panstyle_connection = sp->pan_control()->alist()->automation_style_changed.connect (mem_fun(panners, &PannerUI::pan_automation_style_changed));
753         }
754
755         panners.pan_changed (this);
756 }
757
758 void
759 MixerStrip::update_input_display ()
760 {
761         std::vector<boost::shared_ptr<ARDOUR::Bundle> > c = _route->bundles_connected_to_inputs ();
762
763         /* XXX: how do we represent >1 connected bundle? */
764         if (c.empty() == false) {
765                 input_label.set_text (c[0]->name());
766         } else {
767                 switch (_width) {
768                 case Wide:
769                         input_label.set_text (_(" Input"));
770                         break;
771                 case Narrow:
772                         input_label.set_text (_("I"));
773                         break;
774                 }
775         }
776         panners.setup_pan ();
777 }
778
779 void
780 MixerStrip::update_output_display ()
781 {
782         std::vector<boost::shared_ptr<ARDOUR::Bundle> > c = _route->bundles_connected_to_outputs ();
783
784         /* XXX: how do we represent >1 connected bundle? */
785         if (c.empty() == false) {
786                 output_label.set_text (c[0]->name());
787         } else {
788                 switch (_width) {
789                 case Wide:
790                         output_label.set_text (_("Output"));
791                         break;
792                 case Narrow:
793                         output_label.set_text (_("O"));
794                         break;
795                 }
796         }
797         gpm.setup_meters ();
798         panners.setup_pan ();
799 }
800
801 void
802 MixerStrip::fast_update ()
803 {
804         gpm.update_meters ();
805 }
806
807 void
808 MixerStrip::diskstream_changed ()
809 {
810         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_diskstream_display));
811 }       
812
813 void
814 MixerStrip::input_changed (IOChange change, void *src)
815 {
816         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_input_display));
817         set_width(_width, this);
818 }
819
820 void
821 MixerStrip::output_changed (IOChange change, void *src)
822 {
823         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_output_display));
824         set_width(_width, this);
825 }
826
827
828 void 
829 MixerStrip::comment_editor_done_editing() 
830 {
831         string str =  comment_area->get_buffer()->get_text();
832         if (_route->comment() != str) {
833                 _route->set_comment (str, this);
834
835                 switch (_width) {
836                    
837                 case Wide:
838                         if (! str.empty()) {
839                                 comment_button.modify_bg (STATE_NORMAL, color());
840                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("*Comments*"));
841                         } else {
842                                 comment_button.unset_bg (STATE_NORMAL);
843                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("Comments"));
844                         }
845                         break;
846                    
847                 case Narrow:
848                         if (! str.empty()) {
849                                 comment_button.modify_bg (STATE_NORMAL, color());
850                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("*Cmt*"));
851                         } else {
852                                 comment_button.unset_bg (STATE_NORMAL);
853                                 ((Gtk::Label*)comment_button.get_child())->set_text (_("Cmt"));
854                         } 
855                         break;
856                 }
857                  
858                 ARDOUR_UI::instance()->tooltips().set_tip (comment_button, 
859                                 str.empty() ? _("Click to Add/Edit Comments") : str);
860         }
861
862 }
863
864 void
865 MixerStrip::comment_button_clicked ()
866 {
867         if (comment_window == 0) {
868                 setup_comment_editor ();
869         }
870
871     int x, y, cw_width, cw_height;
872
873         if (comment_window->is_visible()) {
874                 comment_window->hide ();
875                 return;
876         }
877
878         comment_window->get_size (cw_width, cw_height);
879         comment_window->get_position(x, y);
880         comment_window->move(x, y - (cw_height / 2) - 45);
881         /* 
882            half the dialog height minus the comments button height 
883            with some window decoration fudge thrown in.
884         */
885
886         comment_window->show();
887         comment_window->present();
888 }
889
890 void
891 MixerStrip::setup_comment_editor ()
892 {
893         string title;
894         title = _route->name();
895         title += _(": comment editor");
896
897         comment_window = new ArdourDialog (title, false);
898         comment_window->set_position (Gtk::WIN_POS_MOUSE);
899         comment_window->set_skip_taskbar_hint (true);
900         comment_window->signal_hide().connect (mem_fun(*this, &MixerStrip::comment_editor_done_editing));
901
902         comment_area = manage (new TextView());
903         comment_area->set_name ("MixerTrackCommentArea");
904         comment_area->set_size_request (110, 178);
905         comment_area->set_wrap_mode (WRAP_WORD);
906         comment_area->set_editable (true);
907         comment_area->get_buffer()->set_text (_route->comment());
908         comment_area->show ();
909
910         comment_window->get_vbox()->pack_start (*comment_area);
911         comment_window->get_action_area()->hide();
912 }
913
914 void
915 MixerStrip::comment_changed (void *src)
916 {
917         ENSURE_GUI_THREAD(bind (mem_fun(*this, &MixerStrip::comment_changed), src));
918         
919         if (src != this) {
920                 ignore_comment_edit = true;
921                 if (comment_area) {
922                         comment_area->get_buffer()->set_text (_route->comment());
923                 }
924                 ignore_comment_edit = false;
925         }
926 }
927
928 void
929 MixerStrip::set_mix_group (RouteGroup *rg)
930 {
931         _route->set_mix_group (rg, this);
932 }
933
934 void
935 MixerStrip::add_mix_group_to_menu (RouteGroup *rg, RadioMenuItem::Group* group)
936 {
937         using namespace Menu_Helpers;
938
939         MenuList& items = group_menu->items();
940
941         items.push_back (RadioMenuElem (*group, rg->name(), bind (mem_fun(*this, &MixerStrip::set_mix_group), rg)));
942
943         if (_route->mix_group() == rg) {
944                 static_cast<RadioMenuItem*>(&items.back())->set_active ();
945         }
946 }
947
948 bool
949 MixerStrip::select_mix_group (GdkEventButton *ev)
950 {
951         using namespace Menu_Helpers;
952
953         if (group_menu == 0) {
954                 group_menu = new Menu;
955         } 
956         group_menu->set_name ("ArdourContextMenu");
957         MenuList& items = group_menu->items();
958         RadioMenuItem::Group group;
959
960         switch (ev->button) {
961         case 1:
962
963                 items.clear ();
964                 items.push_back (RadioMenuElem (group, _("No group"), bind (mem_fun(*this, &MixerStrip::set_mix_group), (RouteGroup *) 0)));
965
966                 _session.foreach_mix_group (bind (mem_fun (*this, &MixerStrip::add_mix_group_to_menu), &group));
967
968                 group_menu->popup (1, ev->time);
969                 break;
970
971         default:
972                 break;
973         }
974         
975         return true;
976 }       
977
978 void
979 MixerStrip::mix_group_changed (void *ignored)
980 {
981         ENSURE_GUI_THREAD(bind (mem_fun(*this, &MixerStrip::mix_group_changed), ignored));
982         
983         RouteGroup *rg = _route->mix_group();
984         
985         if (rg) {
986                 group_label.set_text (rg->name());
987         } else {
988                 switch (_width) {
989                 case Wide:
990                         group_label.set_text (_("Grp"));
991                         break;
992                 case Narrow:
993                         group_label.set_text (_("~G"));
994                         break;
995                 }
996         }
997 }
998
999
1000 void 
1001 MixerStrip::route_gui_changed (string what_changed, void* ignored)
1002 {
1003         ENSURE_GUI_THREAD(bind (mem_fun(*this, &MixerStrip::route_gui_changed), what_changed, ignored));
1004         
1005         if (what_changed == "color") {
1006                 if (set_color_from_route () == 0) {
1007                         show_route_color ();
1008                 }
1009         }
1010 }
1011
1012 void
1013 MixerStrip::show_route_color ()
1014 {
1015         name_button.modify_bg (STATE_NORMAL, color());
1016         top_event_box.modify_bg (STATE_NORMAL, color());
1017         route_active_changed ();
1018 }
1019
1020 void
1021 MixerStrip::show_passthru_color ()
1022 {
1023         route_active_changed ();
1024 }
1025
1026 void
1027 MixerStrip::build_route_ops_menu ()
1028 {
1029         using namespace Menu_Helpers;
1030         route_ops_menu = manage (new Menu);
1031         route_ops_menu->set_name ("ArdourContextMenu");
1032
1033         MenuList& items = route_ops_menu->items();
1034
1035         items.push_back (MenuElem (_("Rename"), mem_fun(*this, &RouteUI::route_rename)));
1036         items.push_back (SeparatorElem());
1037         items.push_back (CheckMenuElem (_("Active"), mem_fun (*this, &RouteUI::toggle_route_active)));
1038         route_active_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
1039         route_active_menu_item->set_active (_route->active());
1040
1041         items.push_back (SeparatorElem());
1042
1043         items.push_back (MenuElem (_("Adjust latency"), mem_fun (*this, &RouteUI::adjust_latency)));
1044
1045         items.push_back (SeparatorElem());
1046         items.push_back (CheckMenuElem (_("Invert Polarity"), mem_fun (*this, &RouteUI::toggle_polarity)));
1047         polarity_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
1048         polarity_menu_item->set_active (_route->phase_invert());
1049         items.push_back (CheckMenuElem (_("Protect against denormals"), mem_fun (*this, &RouteUI::toggle_denormal_protection)));
1050         denormal_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
1051         denormal_menu_item->set_active (_route->denormal_protection());
1052
1053         build_remote_control_menu ();
1054         
1055         items.push_back (SeparatorElem());
1056         if (!Profile->get_sae()) {
1057               items.push_back (MenuElem (_("Remote Control ID"), *remote_control_menu));
1058         }
1059
1060         items.push_back (SeparatorElem());
1061         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &RouteUI::remove_this_route)));
1062 }
1063
1064 gint
1065 MixerStrip::name_button_button_press (GdkEventButton* ev)
1066 {
1067         if (ev->button == 1) {
1068                 list_route_operations ();
1069
1070                 Menu_Helpers::MenuList& items = route_ops_menu->items();
1071                 /* do not allow rename if the track is record-enabled */
1072                 static_cast<MenuItem*> (&items.front())->set_sensitive (!_route->record_enabled());
1073
1074                 route_ops_menu->popup (1, ev->time);
1075         }
1076         return FALSE;
1077 }
1078
1079 void
1080 MixerStrip::list_route_operations ()
1081 {
1082         if (route_ops_menu == 0) {
1083                 build_route_ops_menu ();
1084         }
1085         
1086         refresh_remote_control_menu();
1087 }
1088
1089
1090 void
1091 MixerStrip::speed_adjustment_changed ()
1092 {
1093         /* since there is a usable speed adjustment, there has to be a diskstream */
1094         if (!ignore_speed_adjustment) {
1095                 get_diskstream()->set_speed (speed_adjustment.get_value());
1096         }
1097 }
1098
1099 void
1100 MixerStrip::speed_changed ()
1101 {
1102         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &MixerStrip::update_speed_display));
1103 }
1104
1105 void
1106 MixerStrip::update_speed_display ()
1107 {
1108         float val;
1109         
1110         val = get_diskstream()->speed();
1111
1112         if (val != 1.0) {
1113                 speed_spinner.set_name ("MixerStripSpeedBaseNotOne");
1114         } else {
1115                 speed_spinner.set_name ("MixerStripSpeedBase");
1116         }
1117
1118         if (speed_adjustment.get_value() != val) {
1119                 ignore_speed_adjustment = true;
1120                 speed_adjustment.set_value (val);
1121                 ignore_speed_adjustment = false;
1122         }
1123 }                       
1124
1125
1126 void
1127 MixerStrip::set_selected (bool yn)
1128 {
1129         AxisView::set_selected (yn);
1130         if (_selected) {
1131                 global_frame.set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
1132                 global_frame.set_name ("MixerStripSelectedFrame");
1133         } else {
1134                 global_frame.set_shadow_type (Gtk::SHADOW_IN);
1135                 global_frame.set_name ("MixerStripFrame");
1136         }
1137         global_frame.queue_draw ();
1138 }
1139
1140 void
1141 MixerStrip::name_changed ()
1142 {
1143         switch (_width) {
1144         case Wide:
1145                 RouteUI::name_changed ();
1146                 break;
1147         case Narrow:
1148                 name_label.set_text (PBD::short_version (_route->name(), 5));
1149                 break;
1150         }
1151         if (_route->phase_invert()) {
1152                 name_label.set_text (X_("Ø ") + name_label.get_text());
1153         }
1154 }
1155
1156 void
1157 MixerStrip::width_clicked ()
1158 {
1159         switch (_width) {
1160         case Wide:
1161                 set_width (Narrow, this);
1162                 break;
1163         case Narrow:
1164                 set_width (Wide, this);
1165                 break;
1166         }
1167 }
1168
1169 void
1170 MixerStrip::hide_clicked ()
1171 {
1172     // LAME fix to reset the button status for when it is redisplayed (part 1)
1173     hide_button.set_sensitive(false);
1174     
1175         if (_embedded) {
1176                  Hiding(); /* EMIT_SIGNAL */
1177         } else {
1178                 _mixer.hide_strip (this);
1179         }
1180         
1181     // (part 2)
1182         hide_button.set_sensitive(true);
1183 }
1184
1185 void
1186 MixerStrip::set_embedded (bool yn)
1187 {
1188         _embedded = yn;
1189 }
1190
1191 void
1192 MixerStrip::map_frozen ()
1193 {
1194         ENSURE_GUI_THREAD (mem_fun(*this, &MixerStrip::map_frozen));
1195
1196         boost::shared_ptr<AudioTrack> at = audio_track();
1197
1198         if (at) {
1199                 switch (at->freeze_state()) {
1200                 case AudioTrack::Frozen:
1201                         pre_processor_box.set_sensitive (false);
1202                         post_processor_box.set_sensitive (false);
1203                         speed_spinner.set_sensitive (false);
1204                         break;
1205                 default:
1206                         pre_processor_box.set_sensitive (true);
1207                         post_processor_box.set_sensitive (true);
1208                         speed_spinner.set_sensitive (true);
1209                         // XXX need some way, maybe, to retoggle redirect editors
1210                         break;
1211                 }
1212         }
1213         
1214         hide_redirect_editors ();
1215 }
1216
1217 void
1218 MixerStrip::hide_redirect_editors ()
1219 {
1220         _route->foreach_processor (this, &MixerStrip::hide_processor_editor);
1221 }
1222
1223 void
1224 MixerStrip::hide_processor_editor (boost::shared_ptr<Processor> processor)
1225 {
1226         void* gui = processor->get_gui ();
1227         
1228         if (gui) {
1229                 static_cast<Gtk::Widget*>(gui)->hide ();
1230         }
1231 }
1232
1233 void
1234 MixerStrip::route_active_changed ()
1235 {
1236         RouteUI::route_active_changed ();
1237
1238         if (is_midi_track()) {
1239                 if (_route->active()) {
1240                         set_name ("MidiTrackStripBase");
1241                         gpm.set_meter_strip_name ("MidiTrackStripBase");
1242                 } else {
1243                         set_name ("MidiTrackStripBaseInactive");
1244                         gpm.set_meter_strip_name ("MidiTrackStripBaseInactive");
1245                 }
1246                 gpm.set_fader_name ("MidiTrackFader");
1247         } else if (is_audio_track()) {
1248                 if (_route->active()) {
1249                         set_name ("AudioTrackStripBase");
1250                         gpm.set_meter_strip_name ("AudioTrackMetrics");
1251                 } else {
1252                         set_name ("AudioTrackStripBaseInactive");
1253                         gpm.set_meter_strip_name ("AudioTrackMetricsInactive");
1254                 }
1255                 gpm.set_fader_name ("AudioTrackFader");
1256         } else {
1257                 if (_route->active()) {
1258                         set_name ("AudioBusStripBase");
1259                         gpm.set_meter_strip_name ("AudioBusMetrics");
1260                 } else {
1261                         set_name ("AudioBusStripBaseInactive");
1262                         gpm.set_meter_strip_name ("AudioBusMetricsInactive");
1263                 }
1264                 gpm.set_fader_name ("AudioBusFader");
1265                 
1266                 /* (no MIDI busses yet) */
1267         }
1268 }
1269
1270 RouteGroup*
1271 MixerStrip::mix_group() const
1272 {
1273         return _route->mix_group();
1274 }
1275
1276 void
1277 MixerStrip::engine_stopped ()
1278 {
1279 }
1280
1281 void
1282 MixerStrip::engine_running ()
1283 {
1284 }
1285
1286 void
1287 MixerStrip::meter_changed (void *src)
1288 {
1289
1290         ENSURE_GUI_THREAD (bind (mem_fun(*this, &MixerStrip::meter_changed), src));
1291
1292         switch (_route->meter_point()) {
1293                 case MeterInput:
1294                         meter_point_label.set_text (_("input"));
1295                         break;
1296
1297                 case MeterPreFader:
1298                         meter_point_label.set_text (_("pre"));
1299                         break;
1300
1301                 case MeterPostFader:
1302                         meter_point_label.set_text (_("post"));
1303                         break;
1304         }
1305
1306         gpm.setup_meters ();
1307                 // reset peak when meter point changes
1308                 gpm.reset_peak_display();
1309                 set_width(_width, this);
1310 }
1311