increase threshold for drag-playhead-does-vertical-zoom
[ardour2.git] / gtk2_ardour / gain_meter.cc
blobf0a8ceb5489812a37647b5690033055081a1004c
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 "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"
50 #include "ardour/session.h"
51 #include "ardour/route.h"
52 #include "ardour/meter.h"
54 #include "i18n.h"
56 using namespace ARDOUR;
57 using namespace PBD;
58 using namespace Gtkmm2ext;
59 using namespace Gtk;
60 using namespace std;
61 using Gtkmm2ext::Keyboard;
63 sigc::signal<void> GainMeterBase::ResetAllPeakDisplays;
64 sigc::signal<void,RouteGroup*> GainMeterBase::ResetGroupPeakDisplays;
66 map<string,Glib::RefPtr<Gdk::Pixmap> > GainMeter::metric_pixmaps;
67 Glib::RefPtr<Gdk::Pixbuf> GainMeter::slider;
70 void
71 GainMeter::setup_slider_pix ()
73 if ((slider = ::get_icon ("fader_belt")) == 0) {
74 throw failed_constructor();
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)
90 using namespace Menu_Helpers;
92 set_session (s);
94 ignore_toggle = false;
95 meter_menu = 0;
96 next_release_selects = false;
97 _width = Wide;
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));
111 level_meter = new LevelMeter(_session);
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");
117 gain_display.set_name ("MixerStripGainDisplay");
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 set_size_request_to_display_given_text (peak_display, "-80.g", 2, 6); /* note the descender */
125 max_peak = minus_infinity();
126 peak_display.set_label (_("-inf"));
127 peak_display.unset_flags (Gtk::CAN_FOCUS);
129 gain_automation_style_button.set_name ("MixerAutomationModeButton");
130 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
132 ARDOUR_UI::instance()->set_tip (gain_automation_state_button, _("Fader automation mode"));
133 ARDOUR_UI::instance()->set_tip (gain_automation_style_button, _("Fader automation type"));
135 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
136 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
138 gain_automation_state_button.set_size_request(15, 15);
139 gain_automation_style_button.set_size_request(15, 15);
141 gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
142 gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
144 gain_astate_menu.set_name ("ArdourContextMenu");
145 gain_astyle_menu.set_name ("ArdourContextMenu");
147 gain_adjustment.signal_value_changed().connect (sigc::mem_fun(*this, &GainMeterBase::gain_adjusted));
148 peak_display.signal_button_release_event().connect (sigc::mem_fun(*this, &GainMeterBase::peak_button_release), false);
149 gain_display.signal_key_press_event().connect (sigc::mem_fun(*this, &GainMeterBase::gain_key_press), false);
151 ResetAllPeakDisplays.connect (sigc::mem_fun(*this, &GainMeterBase::reset_peak_display));
152 ResetGroupPeakDisplays.connect (sigc::mem_fun(*this, &GainMeterBase::reset_group_peak_display));
154 UI::instance()->theme_changed.connect (sigc::mem_fun(*this, &GainMeterBase::on_theme_changed));
155 ColorsChanged.connect (sigc::bind(sigc::mem_fun (*this, &GainMeterBase::color_handler), false));
156 DPIReset.connect (sigc::bind(sigc::mem_fun (*this, &GainMeterBase::color_handler), true));
159 GainMeterBase::~GainMeterBase ()
161 delete meter_menu;
162 delete level_meter;
165 void
166 GainMeterBase::set_controls (boost::shared_ptr<Route> r,
167 boost::shared_ptr<PeakMeter> pm,
168 boost::shared_ptr<Amp> amp)
170 connections.clear ();
171 model_connections.drop_connections ();
173 if (!pm && !amp) {
174 level_meter->set_meter (0);
175 gain_slider->set_controllable (boost::shared_ptr<PBD::Controllable>());
176 _meter.reset ();
177 _amp.reset ();
178 _route.reset ();
179 return;
182 _meter = pm;
183 _amp = amp;
184 _route = r;
186 level_meter->set_meter (pm.get());
187 gain_slider->set_controllable (amp->gain_control());
189 if (!_route || _route->output()->n_ports().n_midi() == 0) {
190 _is_midi = false;
191 gain_adjustment.set_lower (0.0);
192 gain_adjustment.set_upper (1.0);
193 gain_adjustment.set_step_increment (0.01);
194 gain_adjustment.set_page_increment (0.1);
195 } else {
196 _is_midi = true;
197 gain_adjustment.set_lower (0.0);
198 gain_adjustment.set_upper (2.0);
199 gain_adjustment.set_step_increment (0.05);
200 gain_adjustment.set_page_increment (0.1);
203 if (!_route || !_route->is_hidden()) {
205 using namespace Menu_Helpers;
207 gain_astate_menu.items().clear ();
209 gain_astate_menu.items().push_back (MenuElem (_("Manual"),
210 sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
211 Evoral::Parameter(GainAutomation), (AutoState) Off)));
212 gain_astate_menu.items().push_back (MenuElem (_("Play"),
213 sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
214 Evoral::Parameter(GainAutomation), (AutoState) Play)));
215 gain_astate_menu.items().push_back (MenuElem (_("Write"),
216 sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
217 Evoral::Parameter(GainAutomation), (AutoState) Write)));
218 gain_astate_menu.items().push_back (MenuElem (_("Touch"),
219 sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
220 Evoral::Parameter(GainAutomation), (AutoState) Touch)));
222 connections.push_back (gain_automation_style_button.signal_button_press_event().connect (sigc::mem_fun(*this, &GainMeterBase::gain_automation_style_button_event), false));
223 connections.push_back (gain_automation_state_button.signal_button_press_event().connect (sigc::mem_fun(*this, &GainMeterBase::gain_automation_state_button_event), false));
225 boost::shared_ptr<AutomationControl> gc = amp->gain_control();
227 gc->alist()->automation_state_changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeter::gain_automation_state_changed, this), gui_context());
228 gc->alist()->automation_style_changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeter::gain_automation_style_changed, this), gui_context());
230 gain_automation_state_changed ();
233 amp->gain_control()->Changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeterBase::gain_changed, this), gui_context());
235 gain_changed ();
236 show_gain ();
237 update_gain_sensitive ();
240 void
241 GainMeterBase::hide_all_meters ()
243 level_meter->hide_meters();
246 void
247 GainMeter::hide_all_meters ()
249 bool remove_metric_area = false;
251 GainMeterBase::hide_all_meters ();
253 if (remove_metric_area) {
254 if (meter_metric_area.get_parent()) {
255 level_meter->remove (meter_metric_area);
260 void
261 GainMeterBase::setup_meters (int len)
263 level_meter->setup_meters(len, 5);
266 void
267 GainMeter::setup_meters (int len)
269 if (!meter_metric_area.get_parent()) {
270 level_meter->pack_end (meter_metric_area, false, false);
271 meter_metric_area.show_all ();
273 GainMeterBase::setup_meters (len);
276 bool
277 GainMeterBase::gain_key_press (GdkEventKey* ev)
279 if (key_is_legal_for_numeric_entry (ev->keyval)) {
280 /* drop through to normal handling */
281 return false;
283 /* illegal key for gain entry */
284 return true;
287 bool
288 GainMeterBase::peak_button_release (GdkEventButton* ev)
290 /* reset peak label */
292 if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) {
293 ResetAllPeakDisplays ();
294 } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
295 if (_route) {
296 ResetGroupPeakDisplays (_route->route_group());
298 } else {
299 reset_peak_display ();
302 return true;
305 void
306 GainMeterBase::reset_peak_display ()
308 _meter->reset_max();
309 level_meter->clear_meters();
310 max_peak = -INFINITY;
311 peak_display.set_label (_("-Inf"));
312 peak_display.set_name ("MixerStripPeakDisplay");
315 void
316 GainMeterBase::reset_group_peak_display (RouteGroup* group)
318 if (_route && group == _route->route_group()) {
319 reset_peak_display ();
323 void
324 GainMeterBase::popup_meter_menu (GdkEventButton *ev)
326 using namespace Menu_Helpers;
328 if (meter_menu == 0) {
329 meter_menu = new Gtk::Menu;
330 MenuList& items = meter_menu->items();
332 items.push_back (MenuElem ("-inf .. +0dBFS"));
333 items.push_back (MenuElem ("-10dB .. +0dBFS"));
334 items.push_back (MenuElem ("-4 .. +0dBFS"));
335 items.push_back (SeparatorElem());
336 items.push_back (MenuElem ("-inf .. -2dBFS"));
337 items.push_back (MenuElem ("-10dB .. -2dBFS"));
338 items.push_back (MenuElem ("-4 .. -2dBFS"));
341 meter_menu->popup (1, ev->time);
344 bool
345 GainMeterBase::gain_focused (GdkEventFocus* ev)
347 if (ev->in) {
348 gain_display.select_region (0, -1);
349 } else {
350 gain_display.select_region (0, 0);
352 return false;
355 void
356 GainMeterBase::gain_activated ()
358 float f;
360 if (sscanf (gain_display.get_text().c_str(), "%f", &f) == 1) {
362 /* clamp to displayable values */
364 f = min (f, 6.0f);
366 _amp->set_gain (dB_to_coefficient(f), this);
368 if (gain_display.has_focus()) {
369 PublicEditor::instance().reset_focus();
374 void
375 GainMeterBase::show_gain ()
377 char buf[32];
379 float v = gain_adjustment.get_value();
381 if (!_is_midi) {
382 if (v == 0.0) {
383 strcpy (buf, _("-inf"));
384 } else {
385 snprintf (buf, sizeof (buf), "%.1f", accurate_coefficient_to_dB (slider_position_to_gain (v)));
387 } else {
388 snprintf (buf, sizeof (buf), "%.1f", v);
391 gain_display.set_text (buf);
394 void
395 GainMeterBase::gain_adjusted ()
397 if (!ignore_toggle) {
398 if (_route && _route->amp() == _amp) {
399 if (_is_midi) {
400 _route->set_gain (gain_adjustment.get_value(), this);
401 } else {
402 _route->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
404 } else {
405 _amp->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
409 show_gain ();
412 void
413 GainMeterBase::effective_gain_display ()
415 gfloat value;
417 if (!_route || _route->output()->n_ports().n_midi() == 0) {
418 value = gain_to_slider_position (_amp->gain());
419 } else {
420 value = _amp->gain ();
423 //cerr << this << " for " << _io->name() << " EGAIN = " << value
424 // << " AGAIN = " << gain_adjustment.get_value () << endl;
425 // stacktrace (cerr, 20);
427 if (gain_adjustment.get_value() != value) {
428 ignore_toggle = true;
429 gain_adjustment.set_value (value);
430 ignore_toggle = false;
434 void
435 GainMeterBase::gain_changed ()
437 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&GainMeterBase::effective_gain_display, this));
440 void
441 GainMeterBase::set_meter_strip_name (const char * name)
443 meter_metric_area.set_name (name);
446 void
447 GainMeterBase::set_fader_name (const char * name)
449 uint32_t rgb_active = rgba_from_style (name, 0xff, 0, 0xff, 0, "bg", STATE_ACTIVE, false);
450 uint32_t rgb_normal = rgba_from_style (name, 0xff, 0xff, 0, 0, "bg", STATE_NORMAL, false);
452 gain_slider->set_border_colors (rgb_normal, rgb_active);
455 void
456 GainMeterBase::update_gain_sensitive ()
458 bool x = !(_amp->gain_control()->alist()->automation_state() & Play);
459 static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (x);
462 static MeterPoint
463 next_meter_point (MeterPoint mp)
465 switch (mp) {
466 case MeterInput:
467 return MeterPreFader;
468 break;
470 case MeterPreFader:
471 return MeterPostFader;
472 break;
474 case MeterPostFader:
475 return MeterOutput;
476 break;
478 case MeterOutput:
479 return MeterCustom;
480 break;
482 case MeterCustom:
483 return MeterInput;
484 break;
487 /*NOTREACHED*/
488 return MeterInput;
491 gint
492 GainMeterBase::meter_press(GdkEventButton* ev)
494 wait_for_release = false;
496 if (!_route) {
497 return FALSE;
500 if (!ignore_toggle) {
502 if (Keyboard::is_context_menu_event (ev)) {
504 // no menu at this time.
506 } else {
508 if (Keyboard::is_button2_event(ev)) {
510 // Primary-button2 click is the midi binding click
511 // button2-click is "momentary"
513 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier))) {
514 wait_for_release = true;
515 old_meter_point = _route->meter_point ();
519 if (_route && (ev->button == 1 || Keyboard::is_button2_event (ev))) {
521 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
523 /* Primary+Tertiary-click applies change to all routes */
525 _session->foreach_route (this, &GainMeterBase::set_meter_point, next_meter_point (_route->meter_point()));
528 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
530 /* Primary-click: solo mix group.
531 NOTE: Primary-button2 is MIDI learn.
534 if (ev->button == 1) {
535 set_route_group_meter_point (*_route, next_meter_point (_route->meter_point()));
538 } else {
540 /* click: change just this route */
542 // XXX no undo yet
544 _route->set_meter_point (next_meter_point (_route->meter_point()));
550 return true;
554 gint
555 GainMeterBase::meter_release(GdkEventButton*)
557 if (!ignore_toggle) {
558 if (wait_for_release) {
559 wait_for_release = false;
561 if (_route) {
562 set_meter_point (*_route, old_meter_point);
567 return true;
570 void
571 GainMeterBase::set_meter_point (Route& route, MeterPoint mp)
573 route.set_meter_point (mp);
576 void
577 GainMeterBase::set_route_group_meter_point (Route& route, MeterPoint mp)
579 RouteGroup* route_group;
581 if ((route_group = route.route_group ()) != 0) {
582 route_group->foreach_route (boost::bind (&Route::set_meter_point, _1, mp, false));
583 } else {
584 route.set_meter_point (mp);
588 void
589 GainMeterBase::meter_point_clicked ()
591 if (_route) {
592 /* WHAT? */
596 bool
597 GainMeterBase::gain_slider_button_press (GdkEventButton* ev)
599 switch (ev->type) {
600 case GDK_BUTTON_PRESS:
601 _amp->gain_control()->start_touch (_amp->session().transport_frame());
602 break;
603 default:
604 return false;
607 return true;
610 bool
611 GainMeterBase::gain_slider_button_release (GdkEventButton* ev)
613 _amp->gain_control()->stop_touch (false, _amp->session().transport_frame());
614 return true;
617 gint
618 GainMeterBase::gain_automation_state_button_event (GdkEventButton *ev)
620 if (ev->type == GDK_BUTTON_RELEASE) {
621 return TRUE;
624 switch (ev->button) {
625 case 1:
626 gain_astate_menu.popup (1, ev->time);
627 break;
628 default:
629 break;
632 return TRUE;
635 gint
636 GainMeterBase::gain_automation_style_button_event (GdkEventButton *ev)
638 if (ev->type == GDK_BUTTON_RELEASE) {
639 return TRUE;
642 switch (ev->button) {
643 case 1:
644 gain_astyle_menu.popup (1, ev->time);
645 break;
646 default:
647 break;
649 return TRUE;
652 string
653 GainMeterBase::astate_string (AutoState state)
655 return _astate_string (state, false);
658 string
659 GainMeterBase::short_astate_string (AutoState state)
661 return _astate_string (state, true);
664 string
665 GainMeterBase::_astate_string (AutoState state, bool shrt)
667 string sstr;
669 switch (state) {
670 case Off:
671 sstr = (shrt ? "M" : _("M"));
672 break;
673 case Play:
674 sstr = (shrt ? "P" : _("P"));
675 break;
676 case Touch:
677 sstr = (shrt ? "T" : _("T"));
678 break;
679 case Write:
680 sstr = (shrt ? "W" : _("W"));
681 break;
684 return sstr;
687 string
688 GainMeterBase::astyle_string (AutoStyle style)
690 return _astyle_string (style, false);
693 string
694 GainMeterBase::short_astyle_string (AutoStyle style)
696 return _astyle_string (style, true);
699 string
700 GainMeterBase::_astyle_string (AutoStyle style, bool shrt)
702 if (style & Trim) {
703 return _("Trim");
704 } else {
705 /* XXX it might different in different languages */
707 return (shrt ? _("Abs") : _("Abs"));
711 void
712 GainMeterBase::gain_automation_style_changed ()
714 switch (_width) {
715 case Wide:
716 gain_automation_style_button.set_label (astyle_string(_amp->gain_control()->alist()->automation_style()));
717 break;
718 case Narrow:
719 gain_automation_style_button.set_label (short_astyle_string(_amp->gain_control()->alist()->automation_style()));
720 break;
724 void
725 GainMeterBase::gain_automation_state_changed ()
727 ENSURE_GUI_THREAD (*this, &GainMeterBase::gain_automation_state_changed)
729 bool x;
731 switch (_width) {
732 case Wide:
733 gain_automation_state_button.set_label (astate_string(_amp->gain_control()->alist()->automation_state()));
734 break;
735 case Narrow:
736 gain_automation_state_button.set_label (short_astate_string(_amp->gain_control()->alist()->automation_state()));
737 break;
740 x = (_amp->gain_control()->alist()->automation_state() != Off);
742 if (gain_automation_state_button.get_active() != x) {
743 ignore_toggle = true;
744 gain_automation_state_button.set_active (x);
745 ignore_toggle = false;
748 update_gain_sensitive ();
750 /* start watching automation so that things move */
752 gain_watching.disconnect();
754 if (x) {
755 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (sigc::mem_fun (*this, &GainMeterBase::effective_gain_display));
759 void
760 GainMeterBase::update_meters()
762 char buf[32];
763 float mpeak = level_meter->update_meters();
765 if (mpeak > max_peak) {
766 max_peak = mpeak;
767 if (mpeak <= -200.0f) {
768 peak_display.set_label (_("-inf"));
769 } else {
770 snprintf (buf, sizeof(buf), "%.1f", mpeak);
771 peak_display.set_label (buf);
774 if (mpeak >= 0.0f) {
775 peak_display.set_name ("MixerStripPeakDisplayPeak");
780 void GainMeterBase::color_handler(bool dpi)
782 color_changed = true;
783 dpi_changed = (dpi) ? true : false;
784 setup_meters();
787 void
788 GainMeterBase::set_width (Width w, int len)
790 _width = w;
791 level_meter->setup_meters (len);
795 void
796 GainMeterBase::on_theme_changed()
798 style_changed = true;
801 GainMeter::GainMeter (Session* s, int fader_length)
802 : GainMeterBase (s, slider, false, fader_length)
803 , gain_display_box(true, 0)
804 , hbox(true, 2)
806 gain_display_box.pack_start (gain_display, true, true);
808 meter_metric_area.set_name ("AudioTrackMetrics");
809 set_size_request_to_display_given_text (meter_metric_area, "-127", 0, 0);
811 gain_automation_style_button.set_name ("MixerAutomationModeButton");
812 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
814 ARDOUR_UI::instance()->set_tip (gain_automation_state_button, _("Fader automation mode"));
815 ARDOUR_UI::instance()->set_tip (gain_automation_style_button, _("Fader automation type"));
817 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
818 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
820 gain_automation_state_button.set_size_request(15, 15);
821 gain_automation_style_button.set_size_request(15, 15);
823 fader_vbox = manage (new Gtk::VBox());
824 fader_vbox->set_spacing (0);
825 fader_vbox->pack_start (*gain_slider, true, true);
827 fader_alignment.set (0.5, 0.5, 0.0, 1.0);
828 fader_alignment.add (*fader_vbox);
830 hbox.pack_start (fader_alignment, true, true);
832 set_spacing (2);
834 pack_start (gain_display_box, Gtk::PACK_SHRINK);
835 pack_start (hbox, Gtk::PACK_SHRINK);
837 meter_alignment.set (0.5, 0.5, 0.0, 1.0);
838 meter_alignment.add (*level_meter);
840 meter_metric_area.signal_expose_event().connect (
841 sigc::mem_fun(*this, &GainMeter::meter_metrics_expose));
844 void
845 GainMeter::set_controls (boost::shared_ptr<Route> r,
846 boost::shared_ptr<PeakMeter> meter,
847 boost::shared_ptr<Amp> amp)
849 if (meter_alignment.get_parent()) {
850 hbox.remove (meter_alignment);
853 if (peak_display.get_parent()) {
854 gain_display_box.remove (peak_display);
857 if (gain_automation_state_button.get_parent()) {
858 fader_vbox->remove (gain_automation_state_button);
861 GainMeterBase::set_controls (r, meter, amp);
863 if (_meter) {
864 _meter->ConfigurationChanged.connect (
865 model_connections, invalidator (*this), ui_bind (&GainMeter::meter_configuration_changed, this, _1), gui_context()
868 meter_configuration_changed (_meter->input_streams ());
873 if we have a non-hidden route (ie. we're not the click or the auditioner),
874 pack some route-dependent stuff.
877 gain_display_box.pack_end (peak_display, true, true);
878 hbox.pack_start (meter_alignment, true, true);
880 if (r && !r->is_hidden()) {
881 fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
884 setup_meters ();
885 hbox.show_all ();
889 GainMeter::get_gm_width ()
891 Gtk::Requisition sz;
892 hbox.size_request (sz);
893 return sz.width;
896 Glib::RefPtr<Gdk::Pixmap>
897 GainMeter::render_metrics (Gtk::Widget& w, vector<DataType> types)
899 Glib::RefPtr<Gdk::Window> win (w.get_window());
900 Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
902 gint width, height;
903 win->get_size (width, height);
905 Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
907 metric_pixmaps[w.get_name()] = pixmap;
909 pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
911 Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout ("");
913 for (vector<DataType>::const_iterator i = types.begin(); i != types.end(); ++i) {
915 Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
917 if (types.size() > 1) {
918 /* we're overlaying more than 1 set of marks, so use different colours */
919 Gdk::Color c;
920 switch (*i) {
921 case DataType::AUDIO:
922 c.set_rgb_p (1, 1, 1);
923 break;
924 case DataType::MIDI:
925 c.set_rgb_p (0.2, 0.2, 0.5);
926 break;
929 fg_gc->set_rgb_fg_color (c);
932 vector<int> points;
934 switch (*i) {
935 case DataType::AUDIO:
936 points.push_back (-50);
937 points.push_back (-40);
938 points.push_back (-30);
939 points.push_back (-20);
940 points.push_back (-10);
941 points.push_back (-3);
942 points.push_back (0);
943 points.push_back (4);
944 break;
946 case DataType::MIDI:
947 points.push_back (0);
948 if (types.size() == 1) {
949 points.push_back (32);
950 } else {
951 /* tweak so as not to overlay the -30dB mark */
952 points.push_back (48);
954 points.push_back (64);
955 points.push_back (96);
956 points.push_back (127);
957 break;
960 char buf[32];
962 for (vector<int>::const_iterator j = points.begin(); j != points.end(); ++j) {
964 float fraction = 0;
965 switch (*i) {
966 case DataType::AUDIO:
967 fraction = log_meter (*j);
968 break;
969 case DataType::MIDI:
970 fraction = *j / 127.0;
971 break;
974 gint const pos = height - (gint) floor (height * fraction);
976 snprintf (buf, sizeof (buf), "%d", abs (*j));
978 layout->set_text (buf);
980 /* we want logical extents, not ink extents here */
982 int tw, th;
983 layout->get_pixel_size (tw, th);
985 pixmap->draw_line (fg_gc, 0, pos, 4, pos);
987 int p = pos - (th / 2);
988 p = min (p, height - th);
989 p = max (p, 0);
991 pixmap->draw_layout (fg_gc, 6, p, layout);
995 return pixmap;
998 gint
999 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
1001 Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
1002 Glib::RefPtr<Gdk::GC> bg_gc (meter_metric_area.get_style()->get_bg_gc (Gtk::STATE_INSENSITIVE));
1003 GdkRectangle base_rect;
1004 GdkRectangle draw_rect;
1005 gint width, height;
1007 win->get_size (width, height);
1009 base_rect.width = width;
1010 base_rect.height = height;
1011 base_rect.x = 0;
1012 base_rect.y = 0;
1014 Glib::RefPtr<Gdk::Pixmap> pixmap;
1015 std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
1017 if (i == metric_pixmaps.end() || style_changed || dpi_changed) {
1018 pixmap = render_metrics (meter_metric_area, _types);
1019 } else {
1020 pixmap = i->second;
1023 gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
1024 win->draw_drawable (bg_gc, pixmap, draw_rect.x, draw_rect.y, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
1025 style_changed = false;
1026 return true;
1029 boost::shared_ptr<PBD::Controllable>
1030 GainMeterBase::get_controllable()
1032 if (_amp) {
1033 return _amp->gain_control();
1034 } else {
1035 return boost::shared_ptr<PBD::Controllable>();
1039 void
1040 GainMeter::meter_configuration_changed (ChanCount c)
1042 _types.clear ();
1044 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
1045 if (c.get (*i) > 0) {
1046 _types.push_back (*i);
1050 style_changed = true;
1051 meter_metric_area.queue_draw ();