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