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