merged with 1697 revision of trunk (which is post-rc1 but pre-rc2
[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 = hbox.size_request ();
454         return sz.width;
455 }
456
457 bool
458 GainMeter::gain_key_press (GdkEventKey* ev)
459 {
460         if (key_is_legal_for_numeric_entry (ev->keyval)) {
461                 /* drop through to normal handling */
462                 return false;
463         }
464         /* illegal key for gain entry */
465         return true;
466 }
467
468 bool
469 GainMeter::peak_button_release (GdkEventButton* ev)
470 {
471         /* reset peak label */
472
473         if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control|Keyboard::Shift)) {
474                 ResetAllPeakDisplays ();
475         } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
476                 Route* r;
477                 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
478                         ResetGroupPeakDisplays (r->mix_group());
479                 }
480         } else {
481                 reset_peak_display ();
482         }
483
484         return true;
485 }
486
487 void
488 GainMeter::reset_peak_display ()
489 {
490         Route * r;
491         if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
492                 r->reset_max_peak_meters();
493         }
494
495         max_peak = -INFINITY;
496         peak_display.set_label (_("-Inf"));
497         peak_display.set_name ("MixerStripPeakDisplay");
498 }
499
500 void
501 GainMeter::reset_group_peak_display (RouteGroup* group)
502 {
503         Route* r;
504         if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
505                 if (group == r->mix_group()) {
506                         reset_peak_display ();
507                 }
508         }
509 }
510
511 gint
512 GainMeter::meter_button_release (GdkEventButton* ev, uint32_t which)
513 {
514         switch (ev->button) {
515         case 1:
516                 meters[which].meter->clear();
517                 max_peak = minus_infinity();
518                 peak_display.set_label (_("-inf"));
519                 peak_display.set_name ("MixerStripPeakDisplay");
520                 break;
521
522         case 3:
523                 // popup_meter_menu (ev);
524                 break;
525         };
526
527         return TRUE;
528 }
529
530 void
531 GainMeter::popup_meter_menu (GdkEventButton *ev)
532 {
533         using namespace Menu_Helpers;
534
535         if (meter_menu == 0) {
536                 meter_menu = new Gtk::Menu;
537                 MenuList& items = meter_menu->items();
538
539                 items.push_back (MenuElem ("-inf .. +0dBFS"));
540                 items.push_back (MenuElem ("-10dB .. +0dBFS"));
541                 items.push_back (MenuElem ("-4 .. +0dBFS"));
542                 items.push_back (SeparatorElem());
543                 items.push_back (MenuElem ("-inf .. -2dBFS"));
544                 items.push_back (MenuElem ("-10dB .. -2dBFS"));
545                 items.push_back (MenuElem ("-4 .. -2dBFS"));
546         }
547
548         meter_menu->popup (1, ev->time);
549 }
550
551 bool
552 GainMeter::gain_focused (GdkEventFocus* ev)
553 {
554         if (ev->in) {
555                 gain_display.select_region (0, -1);
556         } else {
557                 gain_display.select_region (0, 0);
558         }
559         return false;
560 }
561
562 void
563 GainMeter::gain_activated ()
564 {
565         float f;
566
567         if (sscanf (gain_display.get_text().c_str(), "%f", &f) == 1) {
568
569                 /* clamp to displayable values */
570
571                 f = min (f, 6.0f);
572
573                 _io->set_gain (dB_to_coefficient(f), this);
574
575                 if (gain_display.has_focus()) {
576                         PublicEditor::instance().reset_focus();
577                 }
578         }
579 }
580
581 void
582 GainMeter::show_gain ()
583 {
584         char buf[32];
585
586         float v = gain_adjustment.get_value();
587         
588         if (v == 0.0) {
589                 strcpy (buf, _("-inf"));
590         } else {
591                 snprintf (buf, 32, "%.1f", coefficient_to_dB (slider_position_to_gain (v)));
592         }
593         
594         gain_display.set_text (buf);
595 }
596
597 void
598 GainMeter::gain_adjusted ()
599 {
600         if (!ignore_toggle) {
601                 _io->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
602         }
603         show_gain ();
604 }
605
606 void
607 GainMeter::effective_gain_display ()
608 {
609         gfloat value = gain_to_slider_position (_io->effective_gain());
610         
611         if (gain_adjustment.get_value() != value) {
612                 ignore_toggle = true; 
613                 gain_adjustment.set_value (value);
614                 ignore_toggle = false;
615         }
616 }
617
618 void
619 GainMeter::gain_changed (void *src)
620 {
621         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &GainMeter::effective_gain_display));
622 }
623
624 void
625 GainMeter::set_meter_strip_name (const char * name)
626 {
627         meter_metric_area.set_name (name);
628 }
629
630 void
631 GainMeter::set_fader_name (const char * name)
632 {
633         gain_slider->set_name (name);
634 }
635
636 void
637 GainMeter::update_gain_sensitive ()
638 {
639         static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (!(_io->gain_automation_state() & Play));
640 }
641
642
643 static MeterPoint
644 next_meter_point (MeterPoint mp)
645 {
646         switch (mp) {
647         case MeterInput:
648                 return MeterPreFader;
649                 break;
650                 
651         case MeterPreFader:
652                 return MeterPostFader;
653                 break;
654                 
655         case MeterPostFader:
656                 return MeterInput;
657                 break;
658         }
659         /*NOTREACHED*/
660         return MeterInput;
661 }
662
663 gint
664 GainMeter::meter_press(GdkEventButton* ev)
665 {
666         Route* _route;
667
668         wait_for_release = false;
669
670         if ((_route = dynamic_cast<Route*>(_io.get())) == 0) {
671                 return FALSE;
672         }
673
674         if (!ignore_toggle) {
675
676                 if (Keyboard::is_context_menu_event (ev)) {
677                         
678                         // no menu at this time.
679
680                 } else {
681
682                         if (ev->button == 2) {
683
684                                 // ctrl-button2 click is the midi binding click
685                                 // button2-click is "momentary"
686                                 
687                                 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control))) {
688                                         wait_for_release = true;
689                                         old_meter_point = _route->meter_point ();
690                                 }
691                         }
692
693                         if (ev->button == 1 || ev->button == 2) {
694
695                                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control|Keyboard::Shift))) {
696
697                                         /* ctrl-shift-click applies change to all routes */
698
699                                         _session.begin_reversible_command (_("meter point change"));
700                                         Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
701                                         _session.foreach_route (this, &GainMeter::set_meter_point, next_meter_point (_route->meter_point()));
702                                         cmd->mark();
703                                         _session.add_command (cmd);
704                                         _session.commit_reversible_command ();
705                                         
706                                         
707                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
708
709                                         /* ctrl-click: solo mix group.
710                                            ctrl-button2 is MIDI learn.
711                                         */
712                                         
713                                         if (ev->button == 1) {
714                                                 _session.begin_reversible_command (_("meter point change"));
715                                                 Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
716                                                 set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
717                                                 cmd->mark();
718                                                 _session.add_command (cmd);
719                                                 _session.commit_reversible_command ();
720                                         }
721                                         
722                                 } else {
723                                         
724                                         /* click: change just this route */
725
726                                         // XXX no undo yet
727                                         
728                                         _route->set_meter_point (next_meter_point (_route->meter_point()), this);
729                                 }
730                         }
731                 }
732         }
733
734         return true;
735
736 }
737
738 gint
739 GainMeter::meter_release(GdkEventButton* ev)
740 {
741
742         if(!ignore_toggle){
743                 if (wait_for_release){
744                         wait_for_release = false;
745                         set_meter_point (*(dynamic_cast<Route*>(_io.get())), old_meter_point);
746                 }
747         }
748         return true;
749 }
750
751 void
752 GainMeter::set_meter_point (Route& route, MeterPoint mp)
753 {
754         route.set_meter_point (mp, this);
755 }
756
757 void
758 GainMeter::set_mix_group_meter_point (Route& route, MeterPoint mp)
759 {
760         RouteGroup* mix_group;
761
762         if((mix_group = route.mix_group()) != 0){
763                 mix_group->apply (&Route::set_meter_point, mp, this);
764         } else {
765                 route.set_meter_point (mp, this);
766         }
767 }
768
769 void
770 GainMeter::meter_point_clicked ()
771 {
772         Route* r;
773
774         if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
775
776         }
777 }
778
779 gint
780 GainMeter::start_gain_touch (GdkEventButton* ev)
781 {
782         _io->start_gain_touch ();
783         return FALSE;
784 }
785
786 gint
787 GainMeter::end_gain_touch (GdkEventButton* ev)
788 {
789         _io->end_gain_touch ();
790         return FALSE;
791 }
792
793 gint
794 GainMeter::gain_automation_state_button_event (GdkEventButton *ev)
795 {
796         if (ev->type == GDK_BUTTON_RELEASE) {
797                 return TRUE;
798         }
799         
800         switch (ev->button) {
801                 case 1:
802                         gain_astate_menu.popup (1, ev->time);
803                         break;
804                 default:
805                         break;
806         }
807
808         return TRUE;
809 }
810
811 gint
812 GainMeter::gain_automation_style_button_event (GdkEventButton *ev)
813 {
814         if (ev->type == GDK_BUTTON_RELEASE) {
815                 return TRUE;
816         }
817
818         switch (ev->button) {
819         case 1:
820                 gain_astyle_menu.popup (1, ev->time);
821                 break;
822         default:
823                 break;
824         }
825         return TRUE;
826 }
827
828 string
829 GainMeter::astate_string (AutoState state)
830 {
831         return _astate_string (state, false);
832 }
833
834 string
835 GainMeter::short_astate_string (AutoState state)
836 {
837         return _astate_string (state, true);
838 }
839
840 string
841 GainMeter::_astate_string (AutoState state, bool shrt)
842 {
843         string sstr;
844
845         switch (state) {
846         case Off:
847                 sstr = (shrt ? "M" : _("M"));
848                 break;
849         case Play:
850                 sstr = (shrt ? "P" : _("P"));
851                 break;
852         case Touch:
853                 sstr = (shrt ? "T" : _("T"));
854                 break;
855         case Write:
856                 sstr = (shrt ? "W" : _("W"));
857                 break;
858         }
859
860         return sstr;
861 }
862
863 string
864 GainMeter::astyle_string (AutoStyle style)
865 {
866         return _astyle_string (style, false);
867 }
868
869 string
870 GainMeter::short_astyle_string (AutoStyle style)
871 {
872         return _astyle_string (style, true);
873 }
874
875 string
876 GainMeter::_astyle_string (AutoStyle style, bool shrt)
877 {
878         if (style & Trim) {
879                 return _("Trim");
880         } else {
881                 /* XXX it might different in different languages */
882
883                 return (shrt ? _("Abs") : _("Abs"));
884         }
885 }
886
887 void
888 GainMeter::gain_automation_style_changed ()
889 {
890   // Route* _route = dynamic_cast<Route*>(&_io);
891         switch (_width) {
892         case Wide:
893                 gain_automation_style_button.set_label (astyle_string(_io->gain_automation_curve().automation_style()));
894                 break;
895         case Narrow:
896                 gain_automation_style_button.set_label  (short_astyle_string(_io->gain_automation_curve().automation_style()));
897                 break;
898         }
899 }
900
901 void
902 GainMeter::gain_automation_state_changed ()
903 {
904         ENSURE_GUI_THREAD(mem_fun(*this, &GainMeter::gain_automation_state_changed));
905         //Route* _route = dynamic_cast<Route*>(&_io);
906         
907         bool x;
908
909         switch (_width) {
910         case Wide:
911                 gain_automation_state_button.set_label (astate_string(_io->gain_automation_curve().automation_state()));
912                 break;
913         case Narrow:
914                 gain_automation_state_button.set_label (short_astate_string(_io->gain_automation_curve().automation_state()));
915                 break;
916         }
917
918         x = (_io->gain_automation_state() != Off);
919         
920         if (gain_automation_state_button.get_active() != x) {
921                 ignore_toggle = true;
922                 gain_automation_state_button.set_active (x);
923                 ignore_toggle = false;
924         }
925
926         update_gain_sensitive ();
927         
928         /* start watching automation so that things move */
929         
930         gain_watching.disconnect();
931
932         if (x) {
933                 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (mem_fun (*this, &GainMeter::effective_gain_display));
934         }
935 }