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