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