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