[Libreoffice-commits] core.git: Branch 'libreoffice-7-2' - vcl/unx

Caolán McNamara (via logerrit) logerrit at kemper.freedesktop.org
Thu Oct 7 11:24:53 UTC 2021


 vcl/unx/gtk3/gtkinst.cxx |  131 ++++++++++++++++++++++++++---------------------
 1 file changed, 73 insertions(+), 58 deletions(-)

New commits:
commit bb70cd3d4badb615528ff18b46ff233d8be6e2ff
Author:     Caolán McNamara <caolanm at redhat.com>
AuthorDate: Wed Oct 6 12:49:05 2021 +0100
Commit:     Michael Stahl <michael.stahl at allotropia.de>
CommitDate: Thu Oct 7 13:24:18 2021 +0200

    tdf#141633 use css instead of pango attribs for font size
    
    in GtkEntry. Rendering was using the font set via pango attribs, but
    when measuring the mininum size gtk will use the min size of the widget
    font so that has to change to allow the GtkEntry to fit the size of the
    desired font
    
    bundle together the setting-font-via-css as "WidgetFont"
    
    Change-Id: Ic00d8b84decf528016fe47fc3b142daf3439340d
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/123138
    Tested-by: Jenkins
    Reviewed-by: Caolán McNamara <caolanm at redhat.com>
    (cherry picked from commit a693009c28059435ea5bae6d89a76e2243fe7793)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/123146
    Reviewed-by: Michael Stahl <michael.stahl at allotropia.de>

diff --git a/vcl/unx/gtk3/gtkinst.cxx b/vcl/unx/gtk3/gtkinst.cxx
index f535c06871fc..b033be801ff3 100644
--- a/vcl/unx/gtk3/gtkinst.cxx
+++ b/vcl/unx/gtk3/gtkinst.cxx
@@ -8813,6 +8813,55 @@ public:
     }
 };
 
+class WidgetFont
+{
+private:
+    GtkWidget* m_pWidget;
+    GtkCssProvider* m_pFontCssProvider;
+    std::unique_ptr<vcl::Font> m_xFont;
+public:
+    WidgetFont(GtkWidget* pWidget)
+        : m_pWidget(pWidget)
+        , m_pFontCssProvider(nullptr)
+    {
+    }
+
+    void use_custom_font(const vcl::Font* pFont, std::u16string_view rCSSSelector)
+    {
+        GtkStyleContext *pWidgetContext = gtk_widget_get_style_context(m_pWidget);
+        if (m_pFontCssProvider)
+        {
+            gtk_style_context_remove_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pFontCssProvider));
+            m_pFontCssProvider = nullptr;
+        }
+
+        m_xFont.reset();
+
+        if (!pFont)
+            return;
+
+        m_xFont.reset(new vcl::Font(*pFont));
+        m_pFontCssProvider = gtk_css_provider_new();
+        OUString aBuffer = rCSSSelector + OUString::Concat(" { ") + vcl_font_to_css(*pFont) + OUString::Concat(" }");
+        OString aResult = OUStringToOString(aBuffer, RTL_TEXTENCODING_UTF8);
+        css_provider_load_from_data(m_pFontCssProvider, aResult.getStr(), aResult.getLength());
+        gtk_style_context_add_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pFontCssProvider),
+                                       GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+    }
+
+    const vcl::Font* get_custom_font() const
+    {
+        return m_xFont.get();
+    }
+
+    ~WidgetFont()
+    {
+        if (m_pFontCssProvider)
+            use_custom_font(nullptr, u"");
+        assert(!m_pFontCssProvider);
+    }
+};
+
 class GtkInstanceButton : public GtkInstanceWidget, public virtual weld::Button
 {
 private:
@@ -11829,7 +11878,7 @@ protected:
     GtkEditable* m_pEditable;
     GtkWidget* m_pDelegate;
 private:
-    std::unique_ptr<vcl::Font> m_xFont;
+    WidgetFont m_aCustomFont;
     gulong m_nChangedSignalId;
     gulong m_nInsertTextSignalId;
     gulong m_nCursorPosSignalId;
@@ -11925,6 +11974,7 @@ public:
 #else
         , m_pDelegate(pWidget)
 #endif
+        , m_aCustomFont(m_pWidget)
         , m_nChangedSignalId(g_signal_connect(m_pEditable, "changed", G_CALLBACK(signalChanged), this))
         , m_nInsertTextSignalId(g_signal_connect(m_pEditable, "insert-text", G_CALLBACK(signalInsertText), this))
         , m_nCursorPosSignalId(g_signal_connect(m_pEditable, "notify::cursor-position", G_CALLBACK(signalCursorPosition), this))
@@ -12085,18 +12135,13 @@ public:
 
     virtual void set_font(const vcl::Font& rFont) override
     {
-        m_xFont.reset(new vcl::Font(rFont));
-        PangoAttrList* pOrigList = get_attributes();
-        PangoAttrList* pAttrList = pOrigList ? pango_attr_list_copy(pOrigList) : pango_attr_list_new();
-        update_attr_list(pAttrList, rFont);
-        set_attributes(pAttrList);
-        pango_attr_list_unref(pAttrList);
+        m_aCustomFont.use_custom_font(&rFont, u"entry");
     }
 
     virtual vcl::Font get_font() override
     {
-        if (m_xFont)
-            return *m_xFont;
+        if (const vcl::Font* pFont = m_aCustomFont.get_custom_font())
+            return *pFont;
         return GtkInstanceWidget::get_font();
     }
 
@@ -16140,8 +16185,7 @@ private:
     GtkTextBuffer* m_pTextBuffer;
     GtkAdjustment* m_pVAdjustment;
     GtkCssProvider* m_pFgCssProvider;
-    GtkCssProvider* m_pFontCssProvider;
-    std::optional<vcl::Font> m_xFont;
+    WidgetFont m_aCustomFont;
     int m_nMaxTextLength;
     gulong m_nChangedSignalId; // we don't disable/enable this one, it's to implement max-length
     gulong m_nInsertTextSignalId;
@@ -16233,7 +16277,7 @@ public:
         , m_pTextBuffer(gtk_text_view_get_buffer(pTextView))
         , m_pVAdjustment(gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(pTextView)))
         , m_pFgCssProvider(nullptr)
-        , m_pFontCssProvider(nullptr)
+        , m_aCustomFont(m_pWidget)
         , m_nMaxTextLength(0)
         , m_nChangedSignalId(g_signal_connect(m_pTextBuffer, "changed", G_CALLBACK(signalChanged), this))
         , m_nInsertTextSignalId(g_signal_connect_after(m_pTextBuffer, "insert-text", G_CALLBACK(signalInserText), this))
@@ -16348,25 +16392,16 @@ public:
                                        GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
     }
 
-    virtual vcl::Font get_font() override
+    virtual void set_font(const vcl::Font& rFont) override
     {
-        if (m_xFont)
-            return *m_xFont;
-        return GtkInstanceWidget::get_font();
+        m_aCustomFont.use_custom_font(&rFont, u"textview");
     }
 
-    virtual void set_font(const vcl::Font& rFont) override
+    virtual vcl::Font get_font() override
     {
-        m_xFont = rFont;
-        GtkStyleContext *pWidgetContext = gtk_widget_get_style_context(GTK_WIDGET(m_pTextView));
-        if (m_pFontCssProvider)
-            gtk_style_context_remove_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pFontCssProvider));
-        m_pFontCssProvider = gtk_css_provider_new();
-        OUString aBuffer = "textview { " + vcl_font_to_css(rFont) +  "}";
-        OString aResult = OUStringToOString(aBuffer, RTL_TEXTENCODING_UTF8);
-        css_provider_load_from_data(m_pFontCssProvider, aResult.getStr(), aResult.getLength());
-        gtk_style_context_add_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pFontCssProvider),
-                                       GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+        if (const vcl::Font* pFont = m_aCustomFont.get_custom_font())
+            return *pFont;
+        return GtkInstanceWidget::get_font();
     }
 
     virtual void disable_notify_events() override
@@ -17198,13 +17233,12 @@ private:
     GtkWidget* m_pEntry;
     GtkEditable* m_pEditable;
 //    GtkCellView* m_pCellView;
-    GtkCssProvider* m_pFontCssProvider;
     GtkEventController* m_pKeyController;
     GtkEventController* m_pEntryKeyController;
     GtkEventController* m_pMenuKeyController;
     GtkEventController* m_pEntryFocusController;
 //    std::unique_ptr<CustomRenderMenuButtonHelper> m_xCustomMenuButtonHelper;
-    std::optional<vcl::Font> m_xFont;
+    WidgetFont m_aCustomFont;
     std::optional<vcl::Font> m_xEntryFont;
     std::unique_ptr<comphelper::string::NaturalStringSorter> m_xSorter;
     vcl::QuickSelectionEngine m_aQuickSelectionEngine;
@@ -18232,7 +18266,7 @@ public:
 //        , m_pToggleButton(GTK_WIDGET(gtk_builder_get_object(pComboBuilder, "button")))
         , m_pEntry(GTK_IS_ENTRY(gtk_combo_box_get_child(pComboBox)) ? gtk_combo_box_get_child(pComboBox) : nullptr)
         , m_pEditable(GTK_EDITABLE(m_pEntry))
-        , m_pFontCssProvider(nullptr)
+        , m_aCustomFont(m_pWidget)
 //        , m_pCellView(nullptr)
         , m_aQuickSelectionEngine(*this)
 //        , m_bHoverSelection(false)
@@ -18599,22 +18633,13 @@ public:
 
     virtual void set_font(const vcl::Font& rFont) override
     {
-        m_xFont = rFont;
-        GtkStyleContext *pWidgetContext = gtk_widget_get_style_context(GTK_WIDGET(m_pComboBox));
-        if (m_pFontCssProvider)
-            gtk_style_context_remove_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pFontCssProvider));
-        m_pFontCssProvider = gtk_css_provider_new();
-        OUString aBuffer = "combobox { " + vcl_font_to_css(rFont) +  "}";
-        OString aResult = OUStringToOString(aBuffer, RTL_TEXTENCODING_UTF8);
-        css_provider_load_from_data(m_pFontCssProvider, aResult.getStr(), aResult.getLength());
-        gtk_style_context_add_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pFontCssProvider),
-                                       GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+        m_aCustomFont.use_custom_font(&rFont, u"combobox");
     }
 
     virtual vcl::Font get_font() override
     {
-        if (m_xFont)
-            return *m_xFont;
+        if (const vcl::Font* pFont = m_aCustomFont.get_custom_font())
+            return *pFont;
         return GtkInstanceWidget::get_font();
     }
 
@@ -18949,9 +18974,8 @@ private:
     GtkWidget* m_pToggleButton;
     GtkWidget* m_pEntry;
     GtkCellView* m_pCellView;
-    GtkCssProvider* m_pFontCssProvider;
+    WidgetFont m_aCustomFont;
     std::unique_ptr<CustomRenderMenuButtonHelper> m_xCustomMenuButtonHelper;
-    std::optional<vcl::Font> m_xFont;
     std::optional<vcl::Font> m_xEntryFont;
     std::unique_ptr<comphelper::string::NaturalStringSorter> m_xSorter;
     vcl::QuickSelectionEngine m_aQuickSelectionEngine;
@@ -19989,7 +20013,7 @@ public:
         , m_pToggleButton(GTK_WIDGET(gtk_builder_get_object(pComboBuilder, "button")))
         , m_pEntry(GTK_WIDGET(gtk_builder_get_object(pComboBuilder, "entry")))
         , m_pCellView(nullptr)
-        , m_pFontCssProvider(nullptr)
+        , m_aCustomFont(m_pWidget)
         , m_aQuickSelectionEngine(*this)
         , m_bHoverSelection(false)
         , m_bMouseInOverlayButton(false)
@@ -20408,23 +20432,14 @@ public:
 
     virtual void set_font(const vcl::Font& rFont) override
     {
-        m_xFont = rFont;
-        GtkStyleContext *pWidgetContext = gtk_widget_get_style_context(GTK_WIDGET(getContainer()));
-        if (m_pFontCssProvider)
-            gtk_style_context_remove_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pFontCssProvider));
-        m_pFontCssProvider = gtk_css_provider_new();
-        OUString aBuffer = "box#combobox { " + vcl_font_to_css(rFont) +  "}";
-        OString aResult = OUStringToOString(aBuffer, RTL_TEXTENCODING_UTF8);
-        css_provider_load_from_data(m_pFontCssProvider, aResult.getStr(), aResult.getLength());
-        gtk_style_context_add_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pFontCssProvider),
-                                       GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+        m_aCustomFont.use_custom_font(&rFont, u"box#combobox");
     }
 
     virtual vcl::Font get_font() override
     {
-        if (m_xFont)
-            return *m_xFont;
-        return GtkInstanceContainer::get_font();
+        if (const vcl::Font* pFont = m_aCustomFont.get_custom_font())
+            return *pFont;
+        return GtkInstanceWidget::get_font();
     }
 
     virtual void set_entry_font(const vcl::Font& rFont) override


More information about the Libreoffice-commits mailing list