diff options
author | Caolán McNamara <caolanm@redhat.com> | 2020-10-16 15:34:03 +0100 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2020-10-16 21:20:46 +0200 |
commit | f9725762112ba543d57c6cfa26942b0a5b28b50e (patch) | |
tree | 257e7e0fff57d9a2f0c8a9bb784be954fec64cee | |
parent | 056c1284d6a68525002c54bef10834cc135385db (diff) |
support set_font for weld::Button
Change-Id: Ie56ba126bf7677e1605c0080064aa463f9277033
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/104441
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
-rw-r--r-- | include/vcl/weld.hxx | 3 | ||||
-rw-r--r-- | vcl/inc/salvtables.hxx | 2 | ||||
-rw-r--r-- | vcl/source/app/salvtables.cxx | 6 | ||||
-rw-r--r-- | vcl/unx/gtk3/gtk3gtkinst.cxx | 207 |
4 files changed, 139 insertions, 79 deletions
diff --git a/include/vcl/weld.hxx b/include/vcl/weld.hxx index 433322aae085..e463084da93b 100644 --- a/include/vcl/weld.hxx +++ b/include/vcl/weld.hxx @@ -1280,6 +1280,9 @@ public: virtual void set_label_line_wrap(bool wrap) = 0; void clicked() { signal_clicked(); } + // font size is in points, not pixels, e.g. see Window::[G]etPointFont + virtual void set_font(const vcl::Font& rFont) = 0; + void connect_clicked(const Link<Button&, void>& rLink) { m_aClickHdl = rLink; } }; diff --git a/vcl/inc/salvtables.hxx b/vcl/inc/salvtables.hxx index b7dd10de3a69..7f925973bd92 100644 --- a/vcl/inc/salvtables.hxx +++ b/vcl/inc/salvtables.hxx @@ -994,6 +994,8 @@ public: virtual OUString get_label() const override; + virtual void set_font(const vcl::Font& rFont) override; + virtual ~SalInstanceButton() override; }; diff --git a/vcl/source/app/salvtables.cxx b/vcl/source/app/salvtables.cxx index a1e533e2a2bd..19cd239d2848 100644 --- a/vcl/source/app/salvtables.cxx +++ b/vcl/source/app/salvtables.cxx @@ -2477,6 +2477,12 @@ void SalInstanceButton::set_label_line_wrap(bool wrap) m_xButton->queue_resize(); } +void SalInstanceButton::set_font(const vcl::Font& rFont) +{ + m_xButton->SetPointFont(*m_xButton, rFont); + m_xButton->Invalidate(); +} + OUString SalInstanceButton::get_label() const { return m_xButton->GetText(); } SalInstanceButton::~SalInstanceButton() { m_xButton->SetClickHdl(Link<::Button*, void>()); } diff --git a/vcl/unx/gtk3/gtk3gtkinst.cxx b/vcl/unx/gtk3/gtk3gtkinst.cxx index 9dcf0645c9d4..0a4b3a4911cf 100644 --- a/vcl/unx/gtk3/gtk3gtkinst.cxx +++ b/vcl/unx/gtk3/gtk3gtkinst.cxx @@ -6668,6 +6668,88 @@ public: } }; +PangoAttrList* create_attr_list(const vcl::Font& rFont) +{ + PangoAttrList* pAttrList = pango_attr_list_new(); + pango_attr_list_insert(pAttrList, pango_attr_family_new(OUStringToOString(rFont.GetFamilyName(), RTL_TEXTENCODING_UTF8).getStr())); + pango_attr_list_insert(pAttrList, pango_attr_size_new(rFont.GetFontSize().Height() * PANGO_SCALE)); + switch (rFont.GetItalic()) + { + case ITALIC_NONE: + pango_attr_list_insert(pAttrList, pango_attr_style_new(PANGO_STYLE_NORMAL)); + break; + case ITALIC_NORMAL: + pango_attr_list_insert(pAttrList, pango_attr_style_new(PANGO_STYLE_ITALIC)); + break; + case ITALIC_OBLIQUE: + pango_attr_list_insert(pAttrList, pango_attr_style_new(PANGO_STYLE_OBLIQUE)); + break; + default: + break; + } + switch (rFont.GetWeight()) + { + case WEIGHT_ULTRALIGHT: + pango_attr_list_insert(pAttrList, pango_attr_weight_new(PANGO_WEIGHT_ULTRALIGHT)); + break; + case WEIGHT_LIGHT: + pango_attr_list_insert(pAttrList, pango_attr_weight_new(PANGO_WEIGHT_LIGHT)); + break; + case WEIGHT_NORMAL: + pango_attr_list_insert(pAttrList, pango_attr_weight_new(PANGO_WEIGHT_NORMAL)); + break; + case WEIGHT_BOLD: + pango_attr_list_insert(pAttrList, pango_attr_weight_new(PANGO_WEIGHT_BOLD)); + break; + case WEIGHT_ULTRABOLD: + pango_attr_list_insert(pAttrList, pango_attr_weight_new(PANGO_WEIGHT_ULTRABOLD)); + break; + default: + break; + } + switch (rFont.GetWidthType()) + { + case WIDTH_ULTRA_CONDENSED: + pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_ULTRA_CONDENSED)); + break; + case WIDTH_EXTRA_CONDENSED: + pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_EXTRA_CONDENSED)); + break; + case WIDTH_CONDENSED: + pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_CONDENSED)); + break; + case WIDTH_SEMI_CONDENSED: + pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_SEMI_CONDENSED)); + break; + case WIDTH_NORMAL: + pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_NORMAL)); + break; + case WIDTH_SEMI_EXPANDED: + pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_SEMI_EXPANDED)); + break; + case WIDTH_EXPANDED: + pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_EXPANDED)); + break; + case WIDTH_EXTRA_EXPANDED: + pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_EXTRA_EXPANDED)); + break; + case WIDTH_ULTRA_EXPANDED: + pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_ULTRA_EXPANDED)); + break; + default: + break; + } + return pAttrList; +} + +void set_font(GtkLabel* pLabel, const vcl::Font& rFont) +{ + // TODO?, clear old props like set_text_foreground_color does + PangoAttrList* pAttrList = create_attr_list(rFont); + gtk_label_set_attributes(pLabel, pAttrList); + pango_attr_list_unref(pAttrList); +} + class GtkInstanceButton : public GtkInstanceContainer, public virtual weld::Button { private: @@ -6688,6 +6770,44 @@ private: m_pMouseEventBox = m_pWidget; } + static GtkWidget* find_label_widget(GtkContainer* pContainer) + { + GList* pChildren = gtk_container_get_children(pContainer); + + GtkWidget* pChild = nullptr; + for (GList* pCandidate = pChildren; pCandidate; pCandidate = pCandidate->next) + { + if (GTK_IS_LABEL(pCandidate->data)) + { + pChild = GTK_WIDGET(pCandidate->data); + break; + } + else if (GTK_IS_CONTAINER(pCandidate->data)) + { + pChild = find_label_widget(GTK_CONTAINER(pCandidate->data)); + if (pChild) + break; + } + } + g_list_free(pChildren); + + return pChild; + } + + GtkWidget* get_label_widget() + { + GtkWidget* pChild = gtk_bin_get_child(GTK_BIN(m_pButton)); + if (GTK_IS_ALIGNMENT(pChild)) + pChild = gtk_bin_get_child(GTK_BIN(pChild)); + + if (GTK_IS_CONTAINER(pChild)) + pChild = find_label_widget(GTK_CONTAINER(pChild)); + else if (!GTK_IS_LABEL(pChild)) + pChild = nullptr; + + return pChild; + } + public: GtkInstanceButton(GtkButton* pButton, GtkInstanceBuilder* pBuilder, bool bTakeOwnership) : GtkInstanceContainer(GTK_CONTAINER(pButton), pBuilder, bTakeOwnership) @@ -6743,10 +6863,16 @@ public: virtual void set_label_line_wrap(bool wrap) override { - GtkWidget* pChild = gtk_bin_get_child(GTK_BIN(m_pButton)); + GtkWidget* pChild = get_label_widget(); gtk_label_set_line_wrap(GTK_LABEL(pChild), wrap); } + virtual void set_font(const vcl::Font& rFont) override + { + GtkWidget* pChild = get_label_widget(); + ::set_font(GTK_LABEL(pChild), rFont); + } + // allow us to block buttons with click handlers making dialogs return a response bool has_click_handler() const { @@ -8568,80 +8694,6 @@ public: g_signal_handler_disconnect(m_pCalendar, m_nDaySelectedSignalId); } }; - - PangoAttrList* create_attr_list(const vcl::Font& rFont) - { - PangoAttrList* pAttrList = pango_attr_list_new(); - pango_attr_list_insert(pAttrList, pango_attr_family_new(OUStringToOString(rFont.GetFamilyName(), RTL_TEXTENCODING_UTF8).getStr())); - pango_attr_list_insert(pAttrList, pango_attr_size_new(rFont.GetFontSize().Height() * PANGO_SCALE)); - switch (rFont.GetItalic()) - { - case ITALIC_NONE: - pango_attr_list_insert(pAttrList, pango_attr_style_new(PANGO_STYLE_NORMAL)); - break; - case ITALIC_NORMAL: - pango_attr_list_insert(pAttrList, pango_attr_style_new(PANGO_STYLE_ITALIC)); - break; - case ITALIC_OBLIQUE: - pango_attr_list_insert(pAttrList, pango_attr_style_new(PANGO_STYLE_OBLIQUE)); - break; - default: - break; - } - switch (rFont.GetWeight()) - { - case WEIGHT_ULTRALIGHT: - pango_attr_list_insert(pAttrList, pango_attr_weight_new(PANGO_WEIGHT_ULTRALIGHT)); - break; - case WEIGHT_LIGHT: - pango_attr_list_insert(pAttrList, pango_attr_weight_new(PANGO_WEIGHT_LIGHT)); - break; - case WEIGHT_NORMAL: - pango_attr_list_insert(pAttrList, pango_attr_weight_new(PANGO_WEIGHT_NORMAL)); - break; - case WEIGHT_BOLD: - pango_attr_list_insert(pAttrList, pango_attr_weight_new(PANGO_WEIGHT_BOLD)); - break; - case WEIGHT_ULTRABOLD: - pango_attr_list_insert(pAttrList, pango_attr_weight_new(PANGO_WEIGHT_ULTRABOLD)); - break; - default: - break; - } - switch (rFont.GetWidthType()) - { - case WIDTH_ULTRA_CONDENSED: - pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_ULTRA_CONDENSED)); - break; - case WIDTH_EXTRA_CONDENSED: - pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_EXTRA_CONDENSED)); - break; - case WIDTH_CONDENSED: - pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_CONDENSED)); - break; - case WIDTH_SEMI_CONDENSED: - pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_SEMI_CONDENSED)); - break; - case WIDTH_NORMAL: - pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_NORMAL)); - break; - case WIDTH_SEMI_EXPANDED: - pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_SEMI_EXPANDED)); - break; - case WIDTH_EXPANDED: - pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_EXPANDED)); - break; - case WIDTH_EXTRA_EXPANDED: - pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_EXTRA_EXPANDED)); - break; - case WIDTH_ULTRA_EXPANDED: - pango_attr_list_insert(pAttrList, pango_attr_stretch_new(PANGO_STRETCH_ULTRA_EXPANDED)); - break; - default: - break; - } - return pAttrList; - } } namespace @@ -12696,10 +12748,7 @@ public: virtual void set_font(const vcl::Font& rFont) override { - // TODO, clear old props like set_text_foreground_color does - PangoAttrList* pAttrList = create_attr_list(rFont); - gtk_label_set_attributes(m_pLabel, pAttrList); - pango_attr_list_unref(pAttrList); + ::set_font(m_pLabel, rFont); } virtual void set_font_color(const Color& rColor) override |