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