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