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