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