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