Removed unused variable (current_user_level) from FastMeter.
[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 */
19
20 #include <limits.h>
21
22 #include <ardour/io.h>
23 #include <ardour/route.h>
24 #include <ardour/route_group.h>
25 #include <ardour/session.h>
26 #include <ardour/session_route.h>
27 #include <ardour/dB.h>
28
29 #include <gtkmm2ext/utils.h>
30 #include <gtkmm2ext/fastmeter.h>
31 #include <gtkmm2ext/stop_signal.h>
32 #include <gtkmm2ext/barcontroller.h>
33 #include <midi++/manager.h>
34 #include <pbd/fastlog.h>
35
36 #include "ardour_ui.h"
37 #include "gain_meter.h"
38 #include "utils.h"
39 #include "logmeter.h"
40 #include "gui_thread.h"
41 #include "keyboard.h"
42 #include "public_editor.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_label (_("-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 (_("Manual"), 
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 width, height;
230         int  db_points[] = { -50, -40, -20, -30, -10, -3, 0, 4 };
231         char buf[32];
232
233         win->get_size (width, height);
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 width, height;
273
274         win->get_size (width, height);
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, mpeak;
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));
323
324                         mpeak = _io->max_peak_power(n);
325                         
326                         if (mpeak > max_peak) {
327                                 max_peak = mpeak;
328                                 /* set peak display */
329                                 if (max_peak <= -200.0f) {
330                                         peak_display.set_label (_("-inf"));
331                                 } else {
332                                         snprintf (buf, sizeof(buf), "%.1f", max_peak);
333                                         peak_display.set_label (buf);
334                                 }
335
336                                 if (max_peak >= 0.0f) {
337                                         peak_display.set_name ("MixerStripPeakDisplayPeak");
338                                 }
339                         }
340                 }
341         }
342 }
343
344 void
345 GainMeter::parameter_changed(const char* parameter_name)
346 {
347 #define PARAM_IS(x) (!strcmp (parameter_name, (x)))
348
349         ENSURE_GUI_THREAD (bind (mem_fun(*this, &GainMeter::parameter_changed), parameter_name));
350
351         if (PARAM_IS ("meter-hold")) {
352         
353                 vector<MeterInfo>::iterator i;
354                 uint32_t n;
355                 
356                 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
357                         
358                         (*i).meter->set_hold_count ((uint32_t) floor(Config->get_meter_hold()));
359                 }
360         }
361
362 #undef PARAM_IS
363 }
364
365 void
366 GainMeter::hide_all_meters ()
367 {
368         bool remove_metric_area = false;
369
370         for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); ++i) {
371                 if ((*i).packed) {
372                         remove_metric_area = true;
373                         meter_packer.remove (*((*i).meter));
374                         (*i).packed = false;
375                 }
376         }
377
378         if (remove_metric_area) {
379                 if (meter_metric_area.get_parent()) {
380                         meter_packer.remove (meter_metric_area);
381                 }
382         }
383 }
384
385 void
386 GainMeter::setup_meters ()
387 {
388         uint32_t nmeters = _io->n_outputs();
389         guint16 width;
390
391         hide_all_meters ();
392
393         Route* r;
394
395         if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
396
397                 switch (r->meter_point()) {
398                 case MeterPreFader:
399                 case MeterInput:
400                         nmeters = r->n_inputs();
401                         break;
402                 case MeterPostFader:
403                         nmeters = r->n_outputs();
404                         break;
405                 }
406
407         } else {
408
409                 nmeters = _io->n_outputs();
410
411         }
412
413         if (nmeters == 0) {
414                 return;
415         }
416
417         if (nmeters <= 2) {
418                 width = regular_meter_width;
419         } else {
420                 width = thin_meter_width;
421         }
422
423         while (meters.size() < nmeters) {
424                 meters.push_back (MeterInfo());
425         }
426
427         /* pack them backwards */
428
429         if (_width == Wide) {
430                 meter_packer.pack_end (meter_metric_area, false, false);
431                 meter_metric_area.show_all ();
432         }
433
434         for (int32_t n = nmeters-1; nmeters && n >= 0 ; --n) {
435                 if (meters[n].width != width) {
436                         delete meters[n].meter;
437                         meters[n].meter = new FastMeter ((uint32_t) floor (Config->get_meter_hold()), width, FastMeter::Vertical);
438                         meters[n].width = width;
439
440                         meters[n].meter->add_events (Gdk::BUTTON_RELEASE_MASK);
441                         meters[n].meter->signal_button_release_event().connect (bind (mem_fun(*this, &GainMeter::meter_button_release), n));
442                 }
443
444                 meter_packer.pack_end (*meters[n].meter, false, false);
445                 meters[n].meter->show_all ();
446                 meters[n].packed = true;
447         }
448 }       
449
450 int
451 GainMeter::get_gm_width ()
452 {
453         Gtk::Requisition sz;
454         hbox.size_request (sz);
455         return sz.width;
456 }
457
458 bool
459 GainMeter::gain_key_press (GdkEventKey* ev)
460 {
461         if (key_is_legal_for_numeric_entry (ev->keyval)) {
462                 /* drop through to normal handling */
463                 return false;
464         }
465         /* illegal key for gain entry */
466         return true;
467 }
468
469 bool
470 GainMeter::peak_button_release (GdkEventButton* ev)
471 {
472         /* reset peak label */
473
474         if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control|Keyboard::Shift)) {
475                 ResetAllPeakDisplays ();
476         } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
477                 Route* r;
478                 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
479                         ResetGroupPeakDisplays (r->mix_group());
480                 }
481         } else {
482                 reset_peak_display ();
483         }
484
485         return true;
486 }
487
488 void
489 GainMeter::reset_peak_display ()
490 {
491         Route * r;
492         if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
493                 r->reset_max_peak_meters();
494         }
495
496         max_peak = -INFINITY;
497         peak_display.set_label (_("-Inf"));
498         peak_display.set_name ("MixerStripPeakDisplay");
499 }
500
501 void
502 GainMeter::reset_group_peak_display (RouteGroup* group)
503 {
504         Route* r;
505         if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
506                 if (group == r->mix_group()) {
507                         reset_peak_display ();
508                 }
509         }
510 }
511
512 gint
513 GainMeter::meter_button_release (GdkEventButton* ev, uint32_t which)
514 {
515         switch (ev->button) {
516         case 1:
517                 meters[which].meter->clear();
518                 max_peak = minus_infinity();
519                 peak_display.set_label (_("-inf"));
520                 peak_display.set_name ("MixerStripPeakDisplay");
521                 break;
522
523         case 3:
524                 // popup_meter_menu (ev);
525                 break;
526         };
527
528         return TRUE;
529 }
530
531 void
532 GainMeter::popup_meter_menu (GdkEventButton *ev)
533 {
534         using namespace Menu_Helpers;
535
536         if (meter_menu == 0) {
537                 meter_menu = new Gtk::Menu;
538                 MenuList& items = meter_menu->items();
539
540                 items.push_back (MenuElem ("-inf .. +0dBFS"));
541                 items.push_back (MenuElem ("-10dB .. +0dBFS"));
542                 items.push_back (MenuElem ("-4 .. +0dBFS"));
543                 items.push_back (SeparatorElem());
544                 items.push_back (MenuElem ("-inf .. -2dBFS"));
545                 items.push_back (MenuElem ("-10dB .. -2dBFS"));
546                 items.push_back (MenuElem ("-4 .. -2dBFS"));
547         }
548
549         meter_menu->popup (1, ev->time);
550 }
551
552 bool
553 GainMeter::gain_focused (GdkEventFocus* ev)
554 {
555         if (ev->in) {
556                 gain_display.select_region (0, -1);
557         } else {
558                 gain_display.select_region (0, 0);
559         }
560         return false;
561 }
562
563 void
564 GainMeter::gain_activated ()
565 {
566         float f;
567
568         if (sscanf (gain_display.get_text().c_str(), "%f", &f) == 1) {
569
570                 /* clamp to displayable values */
571
572                 f = min (f, 6.0f);
573
574                 _io->set_gain (dB_to_coefficient(f), this);
575
576                 if (gain_display.has_focus()) {
577                         PublicEditor::instance().reset_focus();
578                 }
579         }
580 }
581
582 void
583 GainMeter::show_gain ()
584 {
585         char buf[32];
586
587         float v = gain_adjustment.get_value();
588         
589         if (v == 0.0) {
590                 strcpy (buf, _("-inf"));
591         } else {
592                 snprintf (buf, 32, "%.1f", coefficient_to_dB (slider_position_to_gain (v)));
593         }
594         
595         gain_display.set_text (buf);
596 }
597
598 void
599 GainMeter::gain_adjusted ()
600 {
601         if (!ignore_toggle) {
602                 _io->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
603         }
604         show_gain ();
605 }
606
607 void
608 GainMeter::effective_gain_display ()
609 {
610         gfloat value = gain_to_slider_position (_io->effective_gain());
611         
612         if (gain_adjustment.get_value() != value) {
613                 ignore_toggle = true; 
614                 gain_adjustment.set_value (value);
615                 ignore_toggle = false;
616         }
617 }
618
619 void
620 GainMeter::gain_changed (void *src)
621 {
622         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &GainMeter::effective_gain_display));
623 }
624
625 void
626 GainMeter::set_meter_strip_name (const char * name)
627 {
628         meter_metric_area.set_name (name);
629 }
630
631 void
632 GainMeter::set_fader_name (const char * name)
633 {
634         gain_slider->set_name (name);
635 }
636
637 void
638 GainMeter::update_gain_sensitive ()
639 {
640         static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (!(_io->gain_automation_state() & Play));
641 }
642
643
644 static MeterPoint
645 next_meter_point (MeterPoint mp)
646 {
647         switch (mp) {
648         case MeterInput:
649                 return MeterPreFader;
650                 break;
651                 
652         case MeterPreFader:
653                 return MeterPostFader;
654                 break;
655                 
656         case MeterPostFader:
657                 return MeterInput;
658                 break;
659         }
660         /*NOTREACHED*/
661         return MeterInput;
662 }
663
664 gint
665 GainMeter::meter_press(GdkEventButton* ev)
666 {
667         Route* _route;
668
669         wait_for_release = false;
670
671         if ((_route = dynamic_cast<Route*>(_io.get())) == 0) {
672                 return FALSE;
673         }
674
675         if (!ignore_toggle) {
676
677                 if (Keyboard::is_context_menu_event (ev)) {
678                         
679                         // no menu at this time.
680
681                 } else {
682
683                         if (ev->button == 2) {
684
685                                 // ctrl-button2 click is the midi binding click
686                                 // button2-click is "momentary"
687                                 
688                                 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control))) {
689                                         wait_for_release = true;
690                                         old_meter_point = _route->meter_point ();
691                                 }
692                         }
693
694                         if (ev->button == 1 || ev->button == 2) {
695
696                                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control|Keyboard::Shift))) {
697
698                                         /* ctrl-shift-click applies change to all routes */
699
700                                         _session.begin_reversible_command (_("meter point change"));
701                                         Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
702                                         _session.foreach_route (this, &GainMeter::set_meter_point, next_meter_point (_route->meter_point()));
703                                         cmd->mark();
704                                         _session.add_command (cmd);
705                                         _session.commit_reversible_command ();
706                                         
707                                         
708                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
709
710                                         /* ctrl-click: solo mix group.
711                                            ctrl-button2 is MIDI learn.
712                                         */
713                                         
714                                         if (ev->button == 1) {
715                                                 _session.begin_reversible_command (_("meter point change"));
716                                                 Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
717                                                 set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
718                                                 cmd->mark();
719                                                 _session.add_command (cmd);
720                                                 _session.commit_reversible_command ();
721                                         }
722                                         
723                                 } else {
724                                         
725                                         /* click: change just this route */
726
727                                         // XXX no undo yet
728                                         
729                                         _route->set_meter_point (next_meter_point (_route->meter_point()), this);
730                                 }
731                         }
732                 }
733         }
734
735         return true;
736
737 }
738
739 gint
740 GainMeter::meter_release(GdkEventButton* ev)
741 {
742
743         if(!ignore_toggle){
744                 if (wait_for_release){
745                         wait_for_release = false;
746                         set_meter_point (*(dynamic_cast<Route*>(_io.get())), old_meter_point);
747                 }
748         }
749         return true;
750 }
751
752 void
753 GainMeter::set_meter_point (Route& route, MeterPoint mp)
754 {
755         route.set_meter_point (mp, this);
756 }
757
758 void
759 GainMeter::set_mix_group_meter_point (Route& route, MeterPoint mp)
760 {
761         RouteGroup* mix_group;
762
763         if((mix_group = route.mix_group()) != 0){
764                 mix_group->apply (&Route::set_meter_point, mp, this);
765         } else {
766                 route.set_meter_point (mp, this);
767         }
768 }
769
770 void
771 GainMeter::meter_point_clicked ()
772 {
773         Route* r;
774
775         if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
776
777         }
778 }
779
780 gint
781 GainMeter::start_gain_touch (GdkEventButton* ev)
782 {
783         _io->start_gain_touch ();
784         return FALSE;
785 }
786
787 gint
788 GainMeter::end_gain_touch (GdkEventButton* ev)
789 {
790         _io->end_gain_touch ();
791         return FALSE;
792 }
793
794 gint
795 GainMeter::gain_automation_state_button_event (GdkEventButton *ev)
796 {
797         if (ev->type == GDK_BUTTON_RELEASE) {
798                 return TRUE;
799         }
800         
801         switch (ev->button) {
802                 case 1:
803                         gain_astate_menu.popup (1, ev->time);
804                         break;
805                 default:
806                         break;
807         }
808
809         return TRUE;
810 }
811
812 gint
813 GainMeter::gain_automation_style_button_event (GdkEventButton *ev)
814 {
815         if (ev->type == GDK_BUTTON_RELEASE) {
816                 return TRUE;
817         }
818
819         switch (ev->button) {
820         case 1:
821                 gain_astyle_menu.popup (1, ev->time);
822                 break;
823         default:
824                 break;
825         }
826         return TRUE;
827 }
828
829 string
830 GainMeter::astate_string (AutoState state)
831 {
832         return _astate_string (state, false);
833 }
834
835 string
836 GainMeter::short_astate_string (AutoState state)
837 {
838         return _astate_string (state, true);
839 }
840
841 string
842 GainMeter::_astate_string (AutoState state, bool shrt)
843 {
844         string sstr;
845
846         switch (state) {
847         case Off:
848                 sstr = (shrt ? "M" : _("M"));
849                 break;
850         case Play:
851                 sstr = (shrt ? "P" : _("P"));
852                 break;
853         case Touch:
854                 sstr = (shrt ? "T" : _("T"));
855                 break;
856         case Write:
857                 sstr = (shrt ? "W" : _("W"));
858                 break;
859         }
860
861         return sstr;
862 }
863
864 string
865 GainMeter::astyle_string (AutoStyle style)
866 {
867         return _astyle_string (style, false);
868 }
869
870 string
871 GainMeter::short_astyle_string (AutoStyle style)
872 {
873         return _astyle_string (style, true);
874 }
875
876 string
877 GainMeter::_astyle_string (AutoStyle style, bool shrt)
878 {
879         if (style & Trim) {
880                 return _("Trim");
881         } else {
882                 /* XXX it might different in different languages */
883
884                 return (shrt ? _("Abs") : _("Abs"));
885         }
886 }
887
888 void
889 GainMeter::gain_automation_style_changed ()
890 {
891   // Route* _route = dynamic_cast<Route*>(&_io);
892         switch (_width) {
893         case Wide:
894                 gain_automation_style_button.set_label (astyle_string(_io->gain_automation_curve().automation_style()));
895                 break;
896         case Narrow:
897                 gain_automation_style_button.set_label  (short_astyle_string(_io->gain_automation_curve().automation_style()));
898                 break;
899         }
900 }
901
902 void
903 GainMeter::gain_automation_state_changed ()
904 {
905         ENSURE_GUI_THREAD(mem_fun(*this, &GainMeter::gain_automation_state_changed));
906         //Route* _route = dynamic_cast<Route*>(&_io);
907         
908         bool x;
909
910         switch (_width) {
911         case Wide:
912                 gain_automation_state_button.set_label (astate_string(_io->gain_automation_curve().automation_state()));
913                 break;
914         case Narrow:
915                 gain_automation_state_button.set_label (short_astate_string(_io->gain_automation_curve().automation_state()));
916                 break;
917         }
918
919         x = (_io->gain_automation_state() != Off);
920         
921         if (gain_automation_state_button.get_active() != x) {
922                 ignore_toggle = true;
923                 gain_automation_state_button.set_active (x);
924                 ignore_toggle = false;
925         }
926
927         update_gain_sensitive ();
928         
929         /* start watching automation so that things move */
930         
931         gain_watching.disconnect();
932
933         if (x) {
934                 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (mem_fun (*this, &GainMeter::effective_gain_display));
935         }
936 }