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