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