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