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