the BIG CONFIG patch
[ardour.git] / gtk2_ardour / gain_meter.cc
1 /*
2   Copyright (C) 2002 Paul Davis
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18   $Id$
19 */
20
21 #include <limits.h>
22
23 #include <ardour/io.h>
24 #include <ardour/route.h>
25 #include <ardour/route_group.h>
26 #include <ardour/session.h>
27 #include <ardour/session_route.h>
28 #include <ardour/dB.h>
29
30 #include <gtkmm2ext/utils.h>
31 #include <gtkmm2ext/fastmeter.h>
32 #include <gtkmm2ext/stop_signal.h>
33 #include <gtkmm2ext/barcontroller.h>
34 #include <midi++/manager.h>
35 #include <pbd/fastlog.h>
36
37 #include "ardour_ui.h"
38 #include "gain_meter.h"
39 #include "utils.h"
40 #include "logmeter.h"
41 #include "gui_thread.h"
42 #include "keyboard.h"
43
44 #include <ardour/session.h>
45 #include <ardour/route.h>
46
47 #include "i18n.h"
48
49 using namespace ARDOUR;
50 using namespace PBD;
51 using namespace Gtkmm2ext;
52 using namespace Gtk;
53 using namespace sigc;
54 using namespace std;
55
56 sigc::signal<void> GainMeter::ResetAllPeakDisplays;
57 sigc::signal<void,RouteGroup*> GainMeter::ResetGroupPeakDisplays;
58 Glib::RefPtr<Gdk::Pixbuf> GainMeter::slider;
59 Glib::RefPtr<Gdk::Pixbuf> GainMeter::rail;
60 map<string,Glib::RefPtr<Gdk::Pixmap> > GainMeter::metric_pixmaps;
61
62 int
63 GainMeter::setup_slider_pix ()
64 {
65         string path = ARDOUR::find_data_file("vslider02_slider.xpm", "pixmaps");
66         if (path.empty()) {
67                 error << _("cannot find images for fader slider") << endmsg;
68                 return -1;
69         }
70         slider = Gdk::Pixbuf::create_from_file (path);
71
72         path = ARDOUR::find_data_file("vslider02_rail.xpm", "pixmaps");
73         if (path.empty()) {
74                 error << _("cannot find images for fader rail") << endmsg;
75                 return -1;
76         }
77         rail = Gdk::Pixbuf::create_from_file (path);
78
79         return 0;
80 }
81
82 GainMeter::GainMeter (boost::shared_ptr<IO> io, Session& s)
83         : _io (io),
84           _session (s),
85           gain_slider (0),
86           // 0.781787 is the value needed for gain to be set to 0.
87           gain_adjustment (0.781787, 0.0, 1.0, 0.01, 0.1),
88           gain_display (&gain_adjustment, "MixerStripGainDisplay"),
89           gain_automation_style_button (""),
90           gain_automation_state_button ("")
91         
92 {
93         if (slider == 0) {
94                 setup_slider_pix ();
95         }
96
97         ignore_toggle = false;
98         meter_menu = 0;
99         
100         gain_slider = manage (new VSliderController (slider, rail,
101                                                      &gain_adjustment,
102                                                      _io->gain_control(),
103                                                      false));
104
105         gain_slider->signal_button_press_event().connect (mem_fun(*this, &GainMeter::start_gain_touch));
106         gain_slider->signal_button_release_event().connect (mem_fun(*this, &GainMeter::end_gain_touch));
107         gain_slider->set_name ("MixerGainMeter");
108
109         gain_display.set_print_func (_gain_printer, this);
110
111         gain_display_box.set_spacing (2);
112         set_size_request_to_display_given_text (gain_display_frame, "-86.g", 2, 6); /* note the descender */
113         gain_display_frame.set_shadow_type (Gtk::SHADOW_IN);
114         gain_display_frame.set_name ("BaseFrame");
115         gain_display_frame.add (gain_display);
116         gain_display_box.pack_start (gain_display_frame,  Gtk::PACK_SHRINK);
117
118         peak_display.set_name ("MixerStripPeakDisplay");
119         peak_display.add (peak_display_label);
120         set_size_request_to_display_given_text (peak_display_frame, "-86.g", 2, 6); /* note the descender */
121         peak_display_frame.set_shadow_type (Gtk::SHADOW_IN);
122         peak_display_frame.set_name ("BaseFrame");
123         peak_display_frame.add (peak_display);
124         max_peak = minus_infinity();
125         peak_display_label.set_text (_("-inf"));
126
127         meter_metric_area.set_size_request (25, -1);
128         meter_metric_area.set_name ("MeterMetricsStrip");
129
130         meter_packer.set_spacing (2);
131
132         gain_automation_style_button.set_name ("MixerAutomationModeButton");
133         gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
134
135         ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_state_button, _("Fader automation mode"));
136         ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_style_button, _("Fader automation type"));
137
138         gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
139         gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
140
141         gain_automation_state_button.set_size_request(15, 15);
142         gain_automation_style_button.set_size_request(15, 15);
143
144         fader_vbox = manage (new Gtk::VBox());
145         fader_vbox->set_spacing (0);
146         fader_vbox->pack_start (*gain_slider, false, false, 0);
147
148         hbox.set_spacing (0);
149         hbox.pack_start (*fader_vbox, false, false, 2);
150
151         set_width(Narrow);
152
153         Route* r;
154
155         if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
156                 /* 
157                    if we don't have a route (if we're the click), 
158                    pack some route-dependent stuff.
159                 */
160
161                 gain_display_box.pack_end (peak_display_frame,  Gtk::PACK_SHRINK);
162
163                 hbox.pack_start (meter_packer, true, false);
164
165                 using namespace Menu_Helpers;
166         
167                 gain_astate_menu.items().push_back (MenuElem (_("Off"), 
168                                                       bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Off)));
169                 gain_astate_menu.items().push_back (MenuElem (_("Play"),
170                                                       bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Play)));
171                 gain_astate_menu.items().push_back (MenuElem (_("Write"),
172                                                       bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Write)));
173                 gain_astate_menu.items().push_back (MenuElem (_("Touch"),
174                                                       bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Touch)));
175         
176                 gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
177                 gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
178         
179                 gain_astate_menu.set_name ("ArdourContextMenu");
180                 gain_astyle_menu.set_name ("ArdourContextMenu");
181
182                 gain_automation_style_button.signal_button_press_event().connect (mem_fun(*this, &GainMeter::gain_automation_style_button_event), false);
183                 gain_automation_state_button.signal_button_press_event().connect (mem_fun(*this, &GainMeter::gain_automation_state_button_event), false);
184                 
185                 r->gain_automation_curve().automation_state_changed.connect (mem_fun(*this, &GainMeter::gain_automation_state_changed));
186                 r->gain_automation_curve().automation_style_changed.connect (mem_fun(*this, &GainMeter::gain_automation_style_changed));
187                 fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
188
189                 gain_automation_state_changed ();
190         }
191
192
193         set_spacing (4);
194
195         pack_start (gain_display_box,  Gtk::PACK_SHRINK);
196         pack_start (hbox,  Gtk::PACK_SHRINK);
197
198         _io->gain_changed.connect (mem_fun(*this, &GainMeter::gain_changed));
199
200         meter_metric_area.signal_expose_event().connect (mem_fun(*this, &GainMeter::meter_metrics_expose));
201         gain_adjustment.signal_value_changed().connect (mem_fun(*this, &GainMeter::gain_adjusted));
202         peak_display.signal_button_release_event().connect (mem_fun(*this, &GainMeter::peak_button_release));
203
204         Config->ParameterChanged.connect (mem_fun (*this, &GainMeter::parameter_changed));
205
206         gain_changed (0);
207         update_gain_sensitive ();
208
209         ResetAllPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_peak_display));
210         ResetGroupPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_group_peak_display));
211 }
212
213 void
214 GainMeter::set_width (Width w)
215 {
216         switch (w) {
217         case Wide:
218                 peak_display_frame.show_all();
219                 break;
220         case Narrow:
221                 peak_display_frame.hide_all();
222                 break;
223         }
224
225         _width = w;
226         setup_meters ();
227 }
228
229 Glib::RefPtr<Gdk::Pixmap>
230 GainMeter::render_metrics (Gtk::Widget& w)
231 {
232         Glib::RefPtr<Gdk::Window> win (w.get_window());
233         Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
234         Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
235         gint x, y, width, height, depth;
236         int  db_points[] = { -50, -40, -20, -30, -10, -3, 0, 4 };
237         char buf[32];
238
239         win->get_geometry (x, y, width, height, depth);
240         
241         Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
242
243         metric_pixmaps[w.get_name()] = pixmap;
244
245         pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
246
247         Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout("");
248
249         for (uint32_t i = 0; i < sizeof (db_points)/sizeof (db_points[0]); ++i) {
250
251                 float fraction = log_meter (db_points[i]);
252                 gint pos = height - (gint) floor (height * fraction);
253
254                 snprintf (buf, sizeof (buf), "%d", abs (db_points[i]));
255
256                 layout->set_text (buf);
257
258                 /* we want logical extents, not ink extents here */
259
260                 int width, height;
261                 layout->get_pixel_size (width, height);
262
263                 pixmap->draw_line (fg_gc, 0, pos, 4, pos);
264                 pixmap->draw_layout (fg_gc, 6, pos - (height/2), layout);
265         }
266
267         return pixmap;
268 }
269
270 gint
271 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
272 {
273         Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
274         Glib::RefPtr<Gdk::GC> fg_gc (meter_metric_area.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
275         Glib::RefPtr<Gdk::GC> bg_gc (meter_metric_area.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
276         GdkRectangle base_rect;
277         GdkRectangle draw_rect;
278         gint x, y, width, height, depth;
279
280         win->get_geometry (x, y, width, height, depth);
281         
282         base_rect.width = width;
283         base_rect.height = height;
284         base_rect.x = 0;
285         base_rect.y = 0;
286
287         Glib::RefPtr<Gdk::Pixmap> pixmap;
288         std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
289
290         if (i == metric_pixmaps.end()) {
291                 pixmap = render_metrics (meter_metric_area);
292         } else {
293                 pixmap = i->second;
294         }
295
296         gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
297         win->draw_rectangle (bg_gc, true, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
298         win->draw_drawable (bg_gc, pixmap, draw_rect.x, draw_rect.y, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
299         
300         return true;
301 }
302
303 GainMeter::~GainMeter ()
304 {
305         if (meter_menu) {
306                 delete meter_menu;
307         }
308
309         for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); i++) {
310                 if ((*i).meter) {
311                         delete (*i).meter;
312                 }
313         }
314 }
315
316 void
317 GainMeter::update_meters ()
318 {
319         vector<MeterInfo>::iterator i;
320         uint32_t n;
321         float peak;
322         char buf[32];
323         
324         for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
325                 if ((*i).packed) {
326                         peak = _io->peak_input_power (n);
327
328                         (*i).meter->set (log_meter (peak), peak);
329                                                 
330                         if (peak > max_peak) {
331                 max_peak = peak;
332                 /* set peak display */
333                                 if (max_peak <= -200.0f) {
334                                         peak_display_label.set_text (_("-inf"));
335                                 } else {
336                                         snprintf (buf, sizeof(buf), "%.1f", max_peak);
337                                         peak_display_label.set_text (buf);
338                                 }
339
340                                 if (max_peak >= 0.0f) {
341                                         peak_display.set_name ("MixerStripPeakDisplayPeak");
342                                 }
343                         }
344                 }
345         }
346 }
347
348 void
349 GainMeter::parameter_changed(const char* parameter_name)
350 {
351 #define PARAM_IS(x) (!strcmp (parameter_name, (x)))
352
353         ENSURE_GUI_THREAD (bind (mem_fun(*this, &GainMeter::parameter_changed), parameter_name));
354
355         if (PARAM_IS ("meter-hold")) {
356         
357                 vector<MeterInfo>::iterator i;
358                 uint32_t n;
359                 
360                 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
361                         
362                         (*i).meter->set_hold_count ((uint32_t) floor(Config->get_meter_hold()));
363                 }
364         }
365
366 #undef PARAM_IS
367 }
368
369 void
370 GainMeter::hide_all_meters ()
371 {
372         bool remove_metric_area = false;
373
374         for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); ++i) {
375                 if ((*i).packed) {
376                         remove_metric_area = true;
377                         meter_packer.remove (*((*i).meter));
378                         (*i).packed = false;
379                 }
380         }
381
382         if (remove_metric_area) {
383                 if (meter_metric_area.get_parent()) {
384                         meter_packer.remove (meter_metric_area);
385                 }
386         }
387 }
388
389 void
390 GainMeter::setup_meters ()
391 {
392         uint32_t nmeters = _io->n_outputs();
393         guint16 width;
394
395         hide_all_meters ();
396
397         Route* r;
398
399         if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
400
401                 switch (r->meter_point()) {
402                 case MeterPreFader:
403                 case MeterInput:
404                         nmeters = r->n_inputs();
405                         break;
406                 case MeterPostFader:
407                         nmeters = r->n_outputs();
408                         break;
409                 }
410
411         } else {
412
413                 nmeters = _io->n_outputs();
414
415         }
416
417         if (nmeters == 0) {
418                 return;
419         }
420
421         if (nmeters <= 2) {
422                 width = regular_meter_width;
423         } else {
424                 width = thin_meter_width;
425         }
426
427         while (meters.size() < nmeters) {
428                 meters.push_back (MeterInfo());
429         }
430
431         for (uint32_t n = 0; n < nmeters; ++n) {
432                 if (meters[n].width != width) {
433                         delete meters[n].meter;
434                         meters[n].meter = new FastMeter ((uint32_t) floor (Config->get_meter_hold()), width, FastMeter::Vertical);
435                         meters[n].width = width;
436
437                         meters[n].meter->add_events (Gdk::BUTTON_RELEASE_MASK);
438                         meters[n].meter->signal_button_release_event().connect (bind (mem_fun(*this, &GainMeter::meter_button_release), n));
439                 }
440
441                 meter_packer.pack_start (*meters[n].meter, Gtk::PACK_SHRINK);
442                 meters[n].meter->show_all ();
443                 meters[n].packed = true;
444         }
445
446         if (_width == Wide) {
447                 meter_packer.pack_start (meter_metric_area, Gtk::PACK_SHRINK);
448                 meter_metric_area.show_all ();
449         }
450 }       
451
452 gint
453 GainMeter::peak_button_release (GdkEventButton* ev)
454 {
455         /* reset peak label */
456
457         if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control|Keyboard::Shift)) {
458                 ResetAllPeakDisplays ();
459         } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
460                 Route* r;
461                 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
462                         ResetGroupPeakDisplays (r->mix_group());
463                 }
464         } else {
465                 reset_peak_display ();
466         }
467         return TRUE;
468 }
469
470 void
471 GainMeter::reset_peak_display ()
472 {
473         max_peak = minus_infinity();
474         peak_display_label.set_text (_("-Inf"));
475         peak_display.set_name ("MixerStripPeakDisplay");
476 }
477
478 void
479 GainMeter::reset_group_peak_display (RouteGroup* group)
480 {
481         Route* r;
482         if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
483                 if (group == r->mix_group()) {
484                         reset_peak_display ();
485                 }
486         }
487 }
488
489 gint
490 GainMeter::meter_button_release (GdkEventButton* ev, uint32_t which)
491 {
492         switch (ev->button) {
493         case 1:
494                 meters[which].meter->clear();
495                 max_peak = minus_infinity();
496                 peak_display_label.set_text (_("-inf"));
497                 peak_display.set_name ("MixerStripPeakDisplay");
498                 break;
499
500         case 3:
501                 // popup_meter_menu (ev);
502                 break;
503         };
504
505         return TRUE;
506 }
507
508 void
509 GainMeter::popup_meter_menu (GdkEventButton *ev)
510 {
511         using namespace Menu_Helpers;
512
513         if (meter_menu == 0) {
514                 meter_menu = new Gtk::Menu;
515                 MenuList& items = meter_menu->items();
516
517                 items.push_back (MenuElem ("-inf .. +0dBFS"));
518                 items.push_back (MenuElem ("-10dB .. +0dBFS"));
519                 items.push_back (MenuElem ("-4 .. +0dBFS"));
520                 items.push_back (SeparatorElem());
521                 items.push_back (MenuElem ("-inf .. -2dBFS"));
522                 items.push_back (MenuElem ("-10dB .. -2dBFS"));
523                 items.push_back (MenuElem ("-4 .. -2dBFS"));
524         }
525
526         meter_menu->popup (1, ev->time);
527 }
528
529 void
530 GainMeter::_gain_printer (char buf[32], Gtk::Adjustment& adj, void *arg)
531 {
532         static_cast<GainMeter *>(arg)->gain_printer (buf, adj);
533 }
534
535 void
536 GainMeter::gain_printer (char buf[32], Gtk::Adjustment& adj)
537 {
538         float v = adj.get_value();
539
540         if (v == 0.0) {
541                 strcpy (buf, _("-inf"));
542         } else {
543                 snprintf (buf, 32, "%.1f", coefficient_to_dB (slider_position_to_gain (v)));
544         }
545 }
546
547 void
548 GainMeter::gain_adjusted ()
549 {
550         if (!ignore_toggle) {
551                 _io->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
552         }
553 }
554
555 void
556 GainMeter::effective_gain_display ()
557 {
558         gfloat value = gain_to_slider_position (_io->effective_gain());
559         
560         if (gain_adjustment.get_value() != value) {
561                 ignore_toggle = true;
562                 gain_adjustment.set_value (value);
563                 ignore_toggle = false;
564         }
565 }
566
567 void
568 GainMeter::gain_changed (void *src)
569 {
570         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &GainMeter::effective_gain_display));
571 }
572
573 void
574 GainMeter::set_meter_strip_name (const char * name)
575 {
576         meter_metric_area.set_name (name);
577 }
578
579 void
580 GainMeter::set_fader_name (const char * name)
581 {
582         gain_slider->set_name (name);
583 }
584
585 void
586 GainMeter::update_gain_sensitive ()
587 {
588         static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (!(_io->gain_automation_state() & Play));
589 }
590
591
592 static MeterPoint
593 next_meter_point (MeterPoint mp)
594 {
595         switch (mp) {
596         case MeterInput:
597                 return MeterPreFader;
598                 break;
599                 
600         case MeterPreFader:
601                 return MeterPostFader;
602                 break;
603                 
604         case MeterPostFader:
605                 return MeterInput;
606                 break;
607         }
608         /*NOTREACHED*/
609         return MeterInput;
610 }
611
612 gint
613 GainMeter::meter_press(GdkEventButton* ev)
614 {
615         Route* _route;
616
617         wait_for_release = false;
618
619         if ((_route = dynamic_cast<Route*>(_io.get())) == 0) {
620                 return FALSE;
621         }
622
623         if (!ignore_toggle) {
624
625                 if (Keyboard::is_context_menu_event (ev)) {
626                         
627                         // no menu at this time.
628
629                 } else {
630
631                         if (ev->button == 2) {
632
633                                 // ctrl-button2 click is the midi binding click
634                                 // button2-click is "momentary"
635                                 
636                                 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control))) {
637                                         wait_for_release = true;
638                                         old_meter_point = _route->meter_point ();
639                                 }
640                         }
641
642                         if (ev->button == 1 || ev->button == 2) {
643
644                                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control|Keyboard::Shift))) {
645
646                                         /* ctrl-shift-click applies change to all routes */
647
648                                         _session.foreach_route (this, &GainMeter::set_meter_point, next_meter_point (_route->meter_point()));
649                                         
650                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
651
652                                         /* ctrl-click: solo mix group.
653                                            ctrl-button2 is MIDI learn.
654                                         */
655                                         
656                                         if (ev->button == 1) {
657                                                 set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
658                                         }
659                                         
660                                 } else {
661                                         
662                                         /* click: solo this route */
663                                         
664                                         _route->set_meter_point (next_meter_point (_route->meter_point()), this);
665                                 }
666                         }
667                 }
668         }
669
670         return true;
671
672 }
673
674 gint
675 GainMeter::meter_release(GdkEventButton* ev)
676 {
677
678         if(!ignore_toggle){
679                 if (wait_for_release){
680                         wait_for_release = false;
681                         set_meter_point (*(dynamic_cast<Route*>(_io.get())), old_meter_point);
682                 }
683         }
684         return true;
685 }
686
687 void
688 GainMeter::set_meter_point (Route& route, MeterPoint mp)
689 {
690         route.set_meter_point (mp, this);
691 }
692
693 void
694 GainMeter::set_mix_group_meter_point (Route& route, MeterPoint mp)
695 {
696         RouteGroup* mix_group;
697
698         if((mix_group = route.mix_group()) != 0){
699                 mix_group->apply (&Route::set_meter_point, mp, this);
700         } else {
701                 route.set_meter_point (mp, this);
702         }
703 }
704
705 void
706 GainMeter::meter_point_clicked ()
707 {
708         Route* r;
709
710         if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
711
712         }
713 }
714
715 gint
716 GainMeter::start_gain_touch (GdkEventButton* ev)
717 {
718         _io->start_gain_touch ();
719         return FALSE;
720 }
721
722 gint
723 GainMeter::end_gain_touch (GdkEventButton* ev)
724 {
725         _io->end_gain_touch ();
726         return FALSE;
727 }
728
729 gint
730 GainMeter::gain_automation_state_button_event (GdkEventButton *ev)
731 {
732         if (ev->type == GDK_BUTTON_RELEASE) {
733                 return TRUE;
734         }
735         
736         switch (ev->button) {
737                 case 1:
738                         gain_astate_menu.popup (1, ev->time);
739                         break;
740                 default:
741                         break;
742         }
743
744         return TRUE;
745 }
746
747 gint
748 GainMeter::gain_automation_style_button_event (GdkEventButton *ev)
749 {
750         if (ev->type == GDK_BUTTON_RELEASE) {
751                 return TRUE;
752         }
753
754         switch (ev->button) {
755         case 1:
756                 gain_astyle_menu.popup (1, ev->time);
757                 break;
758         default:
759                 break;
760         }
761         return TRUE;
762 }
763
764 string
765 GainMeter::astate_string (AutoState state)
766 {
767         return _astate_string (state, false);
768 }
769
770 string
771 GainMeter::short_astate_string (AutoState state)
772 {
773         return _astate_string (state, true);
774 }
775
776 string
777 GainMeter::_astate_string (AutoState state, bool shrt)
778 {
779         string sstr;
780
781         switch (state) {
782         case Off:
783                 sstr = (shrt ? "O" : _("O"));
784                 break;
785         case Play:
786                 sstr = (shrt ? "P" : _("P"));
787                 break;
788         case Touch:
789                 sstr = (shrt ? "T" : _("T"));
790                 break;
791         case Write:
792                 sstr = (shrt ? "W" : _("W"));
793                 break;
794         }
795
796         return sstr;
797 }
798
799 string
800 GainMeter::astyle_string (AutoStyle style)
801 {
802         return _astyle_string (style, false);
803 }
804
805 string
806 GainMeter::short_astyle_string (AutoStyle style)
807 {
808         return _astyle_string (style, true);
809 }
810
811 string
812 GainMeter::_astyle_string (AutoStyle style, bool shrt)
813 {
814         if (style & Trim) {
815                 return _("Trim");
816         } else {
817                 /* XXX it might different in different languages */
818
819                 return (shrt ? _("Abs") : _("Abs"));
820         }
821 }
822
823 void
824 GainMeter::gain_automation_style_changed ()
825 {
826   // Route* _route = dynamic_cast<Route*>(&_io);
827         switch (_width) {
828         case Wide:
829                 gain_automation_style_button.set_label (astyle_string(_io->gain_automation_curve().automation_style()));
830                 break;
831         case Narrow:
832                 gain_automation_style_button.set_label  (short_astyle_string(_io->gain_automation_curve().automation_style()));
833                 break;
834         }
835 }
836
837 void
838 GainMeter::gain_automation_state_changed ()
839 {
840         ENSURE_GUI_THREAD(mem_fun(*this, &GainMeter::gain_automation_state_changed));
841         //Route* _route = dynamic_cast<Route*>(&_io);
842         
843         bool x;
844
845         switch (_width) {
846         case Wide:
847                 gain_automation_state_button.set_label (astate_string(_io->gain_automation_curve().automation_state()));
848                 break;
849         case Narrow:
850                 gain_automation_state_button.set_label (short_astate_string(_io->gain_automation_curve().automation_state()));
851                 break;
852         }
853
854         x = (_io->gain_automation_state() != Off);
855         
856         if (gain_automation_state_button.get_active() != x) {
857                 ignore_toggle = true;
858                 gain_automation_state_button.set_active (x);
859                 ignore_toggle = false;
860         }
861
862         update_gain_sensitive ();
863         
864         /* start watching automation so that things move */
865         
866         gain_watching.disconnect();
867
868         if (x) {
869                 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (mem_fun (*this, &GainMeter::effective_gain_display));
870         }
871 }