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