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