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