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