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