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