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