fix up file renaming code a little bit
[ArdourMidi.git] / gtk2_ardour / gain_meter.cc
blobb8a6ad2df9c07d899b1c4b036a0d27f509687147
1 /*
2 Copyright (C) 2002 Paul Davis
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.
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.
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.
20 #include <limits.h>
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"
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"
41 #include "ardour_ui.h"
42 #include "gain_meter.h"
43 #include "logmeter.h"
44 #include "gui_thread.h"
45 #include "keyboard.h"
46 #include "public_editor.h"
47 #include "utils.h"
49 #include "ardour/session.h"
50 #include "ardour/route.h"
51 #include "ardour/meter.h"
53 #include "i18n.h"
55 using namespace ARDOUR;
56 using namespace PBD;
57 using namespace Gtkmm2ext;
58 using namespace Gtk;
59 using namespace std;
60 using Gtkmm2ext::Keyboard;
62 sigc::signal<void> GainMeterBase::ResetAllPeakDisplays;
63 sigc::signal<void,RouteGroup*> GainMeterBase::ResetGroupPeakDisplays;
65 map<string,Glib::RefPtr<Gdk::Pixmap> > GainMeter::metric_pixmaps;
66 Glib::RefPtr<Gdk::Pixbuf> GainMeter::slider;
69 void
70 GainMeter::setup_slider_pix ()
72 if ((slider = ::get_icon ("fader_belt")) == 0) {
73 throw failed_constructor();
77 GainMeterBase::GainMeterBase (Session* s,
78 const Glib::RefPtr<Gdk::Pixbuf>& pix,
79 bool horizontal,
80 int fader_length)
81 : 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.
82 , gain_automation_style_button ("")
83 , gain_automation_state_button ("")
84 , dpi_changed (false)
85 , _is_midi (false)
88 using namespace Menu_Helpers;
90 set_session (s);
92 ignore_toggle = false;
93 meter_menu = 0;
94 next_release_selects = false;
95 style_changed = true;
96 _width = Wide;
98 if (horizontal) {
99 gain_slider = manage (new HSliderController (pix,
100 &gain_adjustment,
101 fader_length,
102 false));
103 } else {
104 gain_slider = manage (new VSliderController (pix,
105 &gain_adjustment,
106 fader_length,
107 false));
110 level_meter = new LevelMeter(_session);
112 gain_slider->signal_button_press_event().connect (sigc::mem_fun(*this, &GainMeter::start_gain_touch));
113 gain_slider->signal_button_release_event().connect (sigc::mem_fun(*this, &GainMeter::end_gain_touch));
114 gain_slider->set_name ("GainFader");
116 gain_display.set_name ("MixerStripGainDisplay");
117 gain_display.set_has_frame (false);
118 set_size_request_to_display_given_text (gain_display, "-80.g", 2, 6); /* note the descender */
119 gain_display.signal_activate().connect (sigc::mem_fun (*this, &GainMeter::gain_activated));
120 gain_display.signal_focus_in_event().connect (sigc::mem_fun (*this, &GainMeter::gain_focused), false);
121 gain_display.signal_focus_out_event().connect (sigc::mem_fun (*this, &GainMeter::gain_focused), false);
123 peak_display.set_name ("MixerStripPeakDisplay");
124 // peak_display.set_has_frame (false);
125 // peak_display.set_editable (false);
126 set_size_request_to_display_given_text (peak_display, "-80.g", 2, 6); /* note the descender */
127 max_peak = minus_infinity();
128 peak_display.set_label (_("-inf"));
129 peak_display.unset_flags (Gtk::CAN_FOCUS);
131 gain_automation_style_button.set_name ("MixerAutomationModeButton");
132 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
134 ARDOUR_UI::instance()->set_tip (gain_automation_state_button, _("Fader automation mode"));
135 ARDOUR_UI::instance()->set_tip (gain_automation_style_button, _("Fader automation type"));
137 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
138 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
140 gain_automation_state_button.set_size_request(15, 15);
141 gain_automation_style_button.set_size_request(15, 15);
143 gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
144 gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
146 gain_astate_menu.set_name ("ArdourContextMenu");
147 gain_astyle_menu.set_name ("ArdourContextMenu");
149 gain_adjustment.signal_value_changed().connect (sigc::mem_fun(*this, &GainMeterBase::gain_adjusted));
150 peak_display.signal_button_release_event().connect (sigc::mem_fun(*this, &GainMeterBase::peak_button_release), false);
151 gain_display.signal_key_press_event().connect (sigc::mem_fun(*this, &GainMeterBase::gain_key_press), false);
153 ResetAllPeakDisplays.connect (sigc::mem_fun(*this, &GainMeterBase::reset_peak_display));
154 ResetGroupPeakDisplays.connect (sigc::mem_fun(*this, &GainMeterBase::reset_group_peak_display));
156 UI::instance()->theme_changed.connect (sigc::mem_fun(*this, &GainMeterBase::on_theme_changed));
157 ColorsChanged.connect (sigc::bind(sigc::mem_fun (*this, &GainMeterBase::color_handler), false));
158 DPIReset.connect (sigc::bind(sigc::mem_fun (*this, &GainMeterBase::color_handler), true));
161 GainMeterBase::~GainMeterBase ()
163 delete meter_menu;
164 delete level_meter;
167 void
168 GainMeterBase::set_controls (boost::shared_ptr<Route> r,
169 boost::shared_ptr<PeakMeter> pm,
170 boost::shared_ptr<Amp> amp)
172 connections.clear ();
173 model_connections.drop_connections ();
175 if (!pm && !amp) {
176 level_meter->set_meter (0);
177 gain_slider->set_controllable (boost::shared_ptr<PBD::Controllable>());
178 _meter.reset ();
179 _amp.reset ();
180 _route.reset ();
181 return;
184 _meter = pm;
185 _amp = amp;
186 _route = r;
188 level_meter->set_meter (pm.get());
189 gain_slider->set_controllable (amp->gain_control());
191 if (!_route || _route->output()->n_ports().n_midi() == 0) {
192 _is_midi = false;
193 gain_adjustment.set_lower (0.0);
194 gain_adjustment.set_upper (1.0);
195 gain_adjustment.set_step_increment (0.01);
196 gain_adjustment.set_page_increment (0.1);
197 } else {
198 _is_midi = true;
199 gain_adjustment.set_lower (0.0);
200 gain_adjustment.set_upper (2.0);
201 gain_adjustment.set_step_increment (0.05);
202 gain_adjustment.set_page_increment (0.1);
205 if (!_route || !_route->is_hidden()) {
207 using namespace Menu_Helpers;
209 gain_astate_menu.items().clear ();
211 gain_astate_menu.items().push_back (MenuElem (_("Manual"),
212 sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
213 Evoral::Parameter(GainAutomation), (AutoState) Off)));
214 gain_astate_menu.items().push_back (MenuElem (_("Play"),
215 sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
216 Evoral::Parameter(GainAutomation), (AutoState) Play)));
217 gain_astate_menu.items().push_back (MenuElem (_("Write"),
218 sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
219 Evoral::Parameter(GainAutomation), (AutoState) Write)));
220 gain_astate_menu.items().push_back (MenuElem (_("Touch"),
221 sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
222 Evoral::Parameter(GainAutomation), (AutoState) Touch)));
224 connections.push_back (gain_automation_style_button.signal_button_press_event().connect (sigc::mem_fun(*this, &GainMeterBase::gain_automation_style_button_event), false));
225 connections.push_back (gain_automation_state_button.signal_button_press_event().connect (sigc::mem_fun(*this, &GainMeterBase::gain_automation_state_button_event), false));
227 boost::shared_ptr<AutomationControl> gc = amp->gain_control();
229 gc->alist()->automation_state_changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeter::gain_automation_state_changed, this), gui_context());
230 gc->alist()->automation_style_changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeter::gain_automation_style_changed, this), gui_context());
232 gain_automation_state_changed ();
235 amp->gain_control()->Changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeterBase::gain_changed, this), gui_context());
237 gain_changed ();
238 show_gain ();
239 update_gain_sensitive ();
242 void
243 GainMeterBase::hide_all_meters ()
245 level_meter->hide_meters();
248 void
249 GainMeter::hide_all_meters ()
251 bool remove_metric_area = false;
253 GainMeterBase::hide_all_meters ();
255 if (remove_metric_area) {
256 if (meter_metric_area.get_parent()) {
257 level_meter->remove (meter_metric_area);
262 void
263 GainMeterBase::setup_meters (int len)
265 level_meter->setup_meters(len, 5);
268 void
269 GainMeter::setup_meters (int len)
271 if (!meter_metric_area.get_parent()) {
272 level_meter->pack_end (meter_metric_area, false, false);
273 meter_metric_area.show_all ();
275 GainMeterBase::setup_meters (len);
278 bool
279 GainMeterBase::gain_key_press (GdkEventKey* ev)
281 if (key_is_legal_for_numeric_entry (ev->keyval)) {
282 /* drop through to normal handling */
283 return false;
285 /* illegal key for gain entry */
286 return true;
289 bool
290 GainMeterBase::peak_button_release (GdkEventButton* ev)
292 /* reset peak label */
294 if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) {
295 ResetAllPeakDisplays ();
296 } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
297 if (_route) {
298 ResetGroupPeakDisplays (_route->route_group());
300 } else {
301 reset_peak_display ();
304 return true;
307 void
308 GainMeterBase::reset_peak_display ()
310 _meter->reset_max();
311 level_meter->clear_meters();
312 max_peak = -INFINITY;
313 peak_display.set_label (_("-Inf"));
314 peak_display.set_name ("MixerStripPeakDisplay");
317 void
318 GainMeterBase::reset_group_peak_display (RouteGroup* group)
320 if (_route && group == _route->route_group()) {
321 reset_peak_display ();
325 void
326 GainMeterBase::popup_meter_menu (GdkEventButton *ev)
328 using namespace Menu_Helpers;
330 if (meter_menu == 0) {
331 meter_menu = new Gtk::Menu;
332 MenuList& items = meter_menu->items();
334 items.push_back (MenuElem ("-inf .. +0dBFS"));
335 items.push_back (MenuElem ("-10dB .. +0dBFS"));
336 items.push_back (MenuElem ("-4 .. +0dBFS"));
337 items.push_back (SeparatorElem());
338 items.push_back (MenuElem ("-inf .. -2dBFS"));
339 items.push_back (MenuElem ("-10dB .. -2dBFS"));
340 items.push_back (MenuElem ("-4 .. -2dBFS"));
343 meter_menu->popup (1, ev->time);
346 bool
347 GainMeterBase::gain_focused (GdkEventFocus* ev)
349 if (ev->in) {
350 gain_display.select_region (0, -1);
351 } else {
352 gain_display.select_region (0, 0);
354 return false;
357 void
358 GainMeterBase::gain_activated ()
360 float f;
362 if (sscanf (gain_display.get_text().c_str(), "%f", &f) == 1) {
364 /* clamp to displayable values */
366 f = min (f, 6.0f);
368 _amp->set_gain (dB_to_coefficient(f), this);
370 if (gain_display.has_focus()) {
371 PublicEditor::instance().reset_focus();
376 void
377 GainMeterBase::show_gain ()
379 char buf[32];
381 float v = gain_adjustment.get_value();
383 if (!_is_midi) {
384 if (v == 0.0) {
385 strcpy (buf, _("-inf"));
386 } else {
387 snprintf (buf, sizeof (buf), "%.1f", accurate_coefficient_to_dB (slider_position_to_gain (v)));
389 } else {
390 snprintf (buf, sizeof (buf), "%.1f", v);
393 gain_display.set_text (buf);
396 void
397 GainMeterBase::gain_adjusted ()
399 if (!ignore_toggle) {
400 if (_route) {
401 if (_route->amp() == _amp) {
402 if (_is_midi) {
403 _route->set_gain (gain_adjustment.get_value(), this);
404 } else {
405 _route->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
407 } else {
408 _amp->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
413 show_gain ();
416 void
417 GainMeterBase::effective_gain_display ()
419 gfloat value;
421 if (!_route || _route->output()->n_ports().n_midi() == 0) {
422 value = gain_to_slider_position (_amp->gain());
423 } else {
424 value = _amp->gain ();
427 //cerr << this << " for " << _io->name() << " EGAIN = " << value
428 // << " AGAIN = " << gain_adjustment.get_value () << endl;
429 // stacktrace (cerr, 20);
431 if (gain_adjustment.get_value() != value) {
432 ignore_toggle = true;
433 gain_adjustment.set_value (value);
434 ignore_toggle = false;
438 void
439 GainMeterBase::gain_changed ()
441 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&GainMeterBase::effective_gain_display, this));
444 void
445 GainMeterBase::set_meter_strip_name (const char * name)
447 meter_metric_area.set_name (name);
450 void
451 GainMeterBase::set_fader_name (const char * name)
453 gain_slider->set_name (name);
456 void
457 GainMeterBase::update_gain_sensitive ()
459 bool x = !(_amp->gain_control()->alist()->automation_state() & Play);
460 static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (x);
463 static MeterPoint
464 next_meter_point (MeterPoint mp)
466 switch (mp) {
467 case MeterInput:
468 return MeterPreFader;
469 break;
471 case MeterPreFader:
472 return MeterPostFader;
473 break;
475 case MeterPostFader:
476 return MeterCustom;
477 break;
479 case MeterCustom:
480 return MeterInput;
481 break;
484 /*NOTREACHED*/
485 return MeterInput;
488 gint
489 GainMeterBase::meter_press(GdkEventButton* ev)
491 wait_for_release = false;
493 if (!_route) {
494 return FALSE;
497 if (!ignore_toggle) {
499 if (Keyboard::is_context_menu_event (ev)) {
501 // no menu at this time.
503 } else {
505 if (Keyboard::is_button2_event(ev)) {
507 // Primary-button2 click is the midi binding click
508 // button2-click is "momentary"
510 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier))) {
511 wait_for_release = true;
512 old_meter_point = _route->meter_point ();
516 if (_route && (ev->button == 1 || Keyboard::is_button2_event (ev))) {
518 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
520 /* Primary+Tertiary-click applies change to all routes */
522 _session->foreach_route (this, &GainMeterBase::set_meter_point, next_meter_point (_route->meter_point()));
525 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
527 /* Primary-click: solo mix group.
528 NOTE: Primary-button2 is MIDI learn.
531 if (ev->button == 1) {
532 set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
535 } else {
537 /* click: change just this route */
539 // XXX no undo yet
541 _route->set_meter_point (next_meter_point (_route->meter_point()));
547 return true;
551 gint
552 GainMeterBase::meter_release(GdkEventButton*)
554 if(!ignore_toggle){
555 if (wait_for_release){
556 wait_for_release = false;
558 if (_route) {
559 set_meter_point (*_route, old_meter_point);
564 return true;
567 void
568 GainMeterBase::set_meter_point (Route& route, MeterPoint mp)
570 route.set_meter_point (mp);
573 void
574 GainMeterBase::set_mix_group_meter_point (Route& route, MeterPoint mp)
576 RouteGroup* mix_group;
578 if((mix_group = route.route_group()) != 0){
579 mix_group->apply (&Route::set_meter_point, mp);
580 } else {
581 route.set_meter_point (mp);
585 void
586 GainMeterBase::meter_point_clicked ()
588 if (_route) {
589 /* WHAT? */
593 gint
594 GainMeterBase::start_gain_touch (GdkEventButton*)
596 _amp->gain_control()->start_touch ();
597 return FALSE;
600 gint
601 GainMeterBase::end_gain_touch (GdkEventButton*)
603 _amp->gain_control()->stop_touch ();
604 return FALSE;
607 gint
608 GainMeterBase::gain_automation_state_button_event (GdkEventButton *ev)
610 if (ev->type == GDK_BUTTON_RELEASE) {
611 return TRUE;
614 switch (ev->button) {
615 case 1:
616 gain_astate_menu.popup (1, ev->time);
617 break;
618 default:
619 break;
622 return TRUE;
625 gint
626 GainMeterBase::gain_automation_style_button_event (GdkEventButton *ev)
628 if (ev->type == GDK_BUTTON_RELEASE) {
629 return TRUE;
632 switch (ev->button) {
633 case 1:
634 gain_astyle_menu.popup (1, ev->time);
635 break;
636 default:
637 break;
639 return TRUE;
642 string
643 GainMeterBase::astate_string (AutoState state)
645 return _astate_string (state, false);
648 string
649 GainMeterBase::short_astate_string (AutoState state)
651 return _astate_string (state, true);
654 string
655 GainMeterBase::_astate_string (AutoState state, bool shrt)
657 string sstr;
659 switch (state) {
660 case Off:
661 sstr = (shrt ? "M" : _("M"));
662 break;
663 case Play:
664 sstr = (shrt ? "P" : _("P"));
665 break;
666 case Touch:
667 sstr = (shrt ? "T" : _("T"));
668 break;
669 case Write:
670 sstr = (shrt ? "W" : _("W"));
671 break;
674 return sstr;
677 string
678 GainMeterBase::astyle_string (AutoStyle style)
680 return _astyle_string (style, false);
683 string
684 GainMeterBase::short_astyle_string (AutoStyle style)
686 return _astyle_string (style, true);
689 string
690 GainMeterBase::_astyle_string (AutoStyle style, bool shrt)
692 if (style & Trim) {
693 return _("Trim");
694 } else {
695 /* XXX it might different in different languages */
697 return (shrt ? _("Abs") : _("Abs"));
701 void
702 GainMeterBase::gain_automation_style_changed ()
704 switch (_width) {
705 case Wide:
706 gain_automation_style_button.set_label (astyle_string(_amp->gain_control()->alist()->automation_style()));
707 break;
708 case Narrow:
709 gain_automation_style_button.set_label (short_astyle_string(_amp->gain_control()->alist()->automation_style()));
710 break;
714 void
715 GainMeterBase::gain_automation_state_changed ()
717 ENSURE_GUI_THREAD (*this, &GainMeterBase::gain_automation_state_changed)
719 bool x;
721 switch (_width) {
722 case Wide:
723 gain_automation_state_button.set_label (astate_string(_amp->gain_control()->alist()->automation_state()));
724 break;
725 case Narrow:
726 gain_automation_state_button.set_label (short_astate_string(_amp->gain_control()->alist()->automation_state()));
727 break;
730 x = (_amp->gain_control()->alist()->automation_state() != Off);
732 if (gain_automation_state_button.get_active() != x) {
733 ignore_toggle = true;
734 gain_automation_state_button.set_active (x);
735 ignore_toggle = false;
738 update_gain_sensitive ();
740 /* start watching automation so that things move */
742 gain_watching.disconnect();
744 if (x) {
745 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (sigc::mem_fun (*this, &GainMeterBase::effective_gain_display));
749 void
750 GainMeterBase::update_meters()
752 char buf[32];
753 float mpeak = level_meter->update_meters();
755 if (mpeak > max_peak) {
756 max_peak = mpeak;
757 if (mpeak <= -200.0f) {
758 peak_display.set_label (_("-inf"));
759 } else {
760 snprintf (buf, sizeof(buf), "%.1f", mpeak);
761 peak_display.set_label (buf);
764 if (mpeak >= 0.0f) {
765 peak_display.set_name ("MixerStripPeakDisplayPeak");
770 void GainMeterBase::color_handler(bool dpi)
772 color_changed = true;
773 dpi_changed = (dpi) ? true : false;
774 setup_meters();
777 void
778 GainMeterBase::set_width (Width w, int len)
780 _width = w;
781 level_meter->setup_meters (len);
785 void
786 GainMeterBase::on_theme_changed()
788 style_changed = true;
791 GainMeter::GainMeter (Session* s, int fader_length)
792 : GainMeterBase (s, slider, false, fader_length)
794 gain_display_box.set_homogeneous (true);
795 gain_display_box.set_spacing (2);
796 gain_display_box.pack_start (gain_display, true, true);
798 meter_metric_area.set_name ("AudioTrackMetrics");
799 set_size_request_to_display_given_text (meter_metric_area, "-50", 0, 0);
801 gain_automation_style_button.set_name ("MixerAutomationModeButton");
802 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
804 ARDOUR_UI::instance()->set_tip (gain_automation_state_button, _("Fader automation mode"));
805 ARDOUR_UI::instance()->set_tip (gain_automation_style_button, _("Fader automation type"));
807 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
808 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
810 gain_automation_state_button.set_size_request(15, 15);
811 gain_automation_style_button.set_size_request(15, 15);
813 HBox* fader_centering_box = manage (new HBox);
814 fader_centering_box->pack_start (*gain_slider, true, false);
816 fader_vbox = manage (new Gtk::VBox());
817 fader_vbox->set_spacing (0);
818 fader_vbox->pack_start (*fader_centering_box, false, false, 0);
820 hbox.set_spacing (2);
821 hbox.pack_start (*fader_vbox, true, true);
823 set_spacing (2);
825 pack_start (gain_display_box, Gtk::PACK_SHRINK);
826 pack_start (hbox, Gtk::PACK_SHRINK);
828 meter_metric_area.signal_expose_event().connect (sigc::mem_fun(*this, &GainMeter::meter_metrics_expose));
831 void
832 GainMeter::set_controls (boost::shared_ptr<Route> r,
833 boost::shared_ptr<PeakMeter> meter,
834 boost::shared_ptr<Amp> amp)
836 if (level_meter->get_parent()) {
837 hbox.remove (*level_meter);
840 if (peak_display.get_parent()) {
841 gain_display_box.remove (peak_display);
844 if (gain_automation_state_button.get_parent()) {
845 fader_vbox->remove (gain_automation_state_button);
848 GainMeterBase::set_controls (r, meter, amp);
851 if we have a non-hidden route (ie. we're not the click or the auditioner),
852 pack some route-dependent stuff.
855 gain_display_box.pack_end (peak_display, true, true);
856 hbox.pack_end (*level_meter, true, true);
858 if (r && !r->is_hidden()) {
859 fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
862 setup_meters ();
863 hbox.show_all ();
867 GainMeter::get_gm_width ()
869 Gtk::Requisition sz;
870 hbox.size_request (sz);
871 return sz.width;
874 Glib::RefPtr<Gdk::Pixmap>
875 GainMeter::render_metrics (Gtk::Widget& w)
877 Glib::RefPtr<Gdk::Window> win (w.get_window());
878 Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
879 Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
880 gint width, height;
881 int db_points[] = { -50, -40, -20, -30, -10, -3, 0, 4 };
882 char buf[32];
884 win->get_size (width, height);
886 Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
888 metric_pixmaps[w.get_name()] = pixmap;
890 pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
892 Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout("");
894 for (uint32_t i = 0; i < sizeof (db_points)/sizeof (db_points[0]); ++i) {
896 float fraction = log_meter (db_points[i]);
897 gint pos = height - (gint) floor (height * fraction);
899 snprintf (buf, sizeof (buf), "%d", abs (db_points[i]));
901 layout->set_text (buf);
903 /* we want logical extents, not ink extents here */
905 int width, height;
906 layout->get_pixel_size (width, height);
908 pixmap->draw_line (fg_gc, 0, pos, 4, pos);
909 pixmap->draw_layout (fg_gc, 6, pos - (height/2), layout);
912 return pixmap;
915 gint
916 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
918 static Glib::RefPtr<Gtk::Style> meter_style;
919 if (style_changed) {
920 meter_style = meter_metric_area.get_style();
922 Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
923 Glib::RefPtr<Gdk::GC> bg_gc (meter_style->get_bg_gc (Gtk::STATE_INSENSITIVE));
924 GdkRectangle base_rect;
925 GdkRectangle draw_rect;
926 gint width, height;
928 win->get_size (width, height);
930 base_rect.width = width;
931 base_rect.height = height;
932 base_rect.x = 0;
933 base_rect.y = 0;
935 Glib::RefPtr<Gdk::Pixmap> pixmap;
936 std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
938 if (i == metric_pixmaps.end() || style_changed || dpi_changed) {
939 pixmap = render_metrics (meter_metric_area);
940 } else {
941 pixmap = i->second;
944 gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
945 win->draw_drawable (bg_gc, pixmap, draw_rect.x, draw_rect.y, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
946 style_changed = false;
947 return true;
950 boost::shared_ptr<PBD::Controllable>
951 GainMeterBase::get_controllable()
953 if (_amp) {
954 return _amp->gain_control();
955 } else {
956 return boost::shared_ptr<PBD::Controllable>();