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