From 2abf9c66a4fbf06e06b7ecc092448a921a2e97ca Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Sat, 30 Jan 2021 02:18:56 +0100 Subject: [PATCH] Fix size-request that rely on Pango::Layout A Pango::Layout created by Gtk::Widget::get_pango_context () does not have any font set. The Font is inherited from the layout's context. The actual font is set when the layout is used in on_expose_event() via get_window ()->create_cairo_context (). In some cases we rely on the font-size to calculate the widget's size before rendering. At this point in time there is no drawing context from which to inherit the style. Furthermore, even after a call to `ensure_style()` in the c'tor of a Widget get_font() simply returns the default value. All Widgets that rely an Pango::Layout font size during size-requests have to explicitly set the font. This fixes various overlarge buttons with the initial default layout. --- libs/widgets/ardour_button.cc | 19 +++++++++++++++---- libs/widgets/ardour_fader.cc | 14 ++++++++------ libs/widgets/widgets/ardour_button.h | 4 +++- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/libs/widgets/ardour_button.cc b/libs/widgets/ardour_button.cc index cbe38bb3b0..8f0eb9bf02 100644 --- a/libs/widgets/ardour_button.cc +++ b/libs/widgets/ardour_button.cc @@ -66,6 +66,7 @@ ArdourButton::ArdourButton (Element e, bool toggle) , _char_pixel_width (0) , _char_pixel_height (0) , _char_avg_pixel_width (0) + , _custom_font_set (false) , _text_width (0) , _text_height (0) , _diameter (0) @@ -113,6 +114,7 @@ ArdourButton::ArdourButton (const std::string& str, Element e, bool toggle) , _char_pixel_width (0) , _char_pixel_height (0) , _char_avg_pixel_width (0) + , _custom_font_set (false) , _text_width (0) , _text_height (0) , _diameter (0) @@ -179,6 +181,7 @@ ArdourButton::set_layout_font (const Pango::FontDescription& fd) queue_resize (); _char_pixel_width = 0; _char_pixel_height = 0; + _custom_font_set = true; } } @@ -974,7 +977,7 @@ ArdourButton::on_size_allocate (Allocation& alloc) setup_led_rect (); if (_layout) { /* re-center text */ - _layout->get_pixel_size (_text_width, _text_height); + //_layout->get_pixel_size (_text_width, _text_height); } } @@ -1047,13 +1050,20 @@ ArdourButton::action_toggled () } void -ArdourButton::on_style_changed (const RefPtr&) +ArdourButton::on_style_changed (const RefPtr& style) { - _update_colors = true; + CairoWidget::on_style_changed (style); + Glib::RefPtr const& new_style = get_style(); + CairoWidget::set_dirty (); + _update_colors = true; _char_pixel_width = 0; _char_pixel_height = 0; - if (is_realized()) { + + if (!_custom_font_set && _layout && _layout->get_font_description () != new_style->get_font ()) { + _layout->set_font_description (new_style->get_font ()); + queue_resize (); + } else if (is_realized()) { queue_resize (); } } @@ -1288,6 +1298,7 @@ ArdourButton::ensure_layout () if (!_layout) { ensure_style (); _layout = Pango::Layout::create (get_pango_context()); + _layout->set_font_description (get_style()->get_font()); _layout->set_ellipsize(_ellipsis); if (_layout_ellipsize_width > 3 * PANGO_SCALE) { _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE); diff --git a/libs/widgets/ardour_fader.cc b/libs/widgets/ardour_fader.cc index 42891310e4..21e4b3da3e 100644 --- a/libs/widgets/ardour_fader.cc +++ b/libs/widgets/ardour_fader.cc @@ -713,13 +713,15 @@ ArdourFader::on_state_changed (Gtk::StateType old_state) } void -ArdourFader::on_style_changed (const Glib::RefPtr&) +ArdourFader::on_style_changed (const Glib::RefPtr& style) { - if (_layout) { - std::string txt = _layout->get_text(); - _layout.clear (); // drop reference to existing layout - _text = ""; - set_text (txt, _centered_text, false); + CairoWidget::on_style_changed (style); + Glib::RefPtr const& new_style = get_style (); + if (_layout && (_layout->get_font_description ().gobj () == 0 || _layout->get_font_description () != new_style->get_font ())) { + _layout->set_font_description (new_style->get_font ()); + queue_resize (); + } else if (is_realized ()) { + queue_resize (); } /* patterns are cached and re-created as needed * during 'expose' in the GUI thread */ diff --git a/libs/widgets/widgets/ardour_button.h b/libs/widgets/widgets/ardour_button.h index 75690c4da6..8a8b03c356 100644 --- a/libs/widgets/widgets/ardour_button.h +++ b/libs/widgets/widgets/ardour_button.h @@ -165,9 +165,11 @@ class LIBWIDGETS_API ArdourButton : public CairoWidget , public Gtkmm2ext::Activ void set_text_internal (); void recalc_char_pixel_geometry (); + unsigned int _char_pixel_width; unsigned int _char_pixel_height; - float _char_avg_pixel_width; + float _char_avg_pixel_width; + bool _custom_font_set; int _text_width; int _text_height;