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