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