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