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