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