From c62be7bb2b0c9fb16c1b313217ce0c03c0b8e242 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 24 May 2019 22:54:37 +0200 Subject: [PATCH] LibGUI: Share code for text painting in GAbstractButton. This gives all the GAbstractButton a consistent disabled appearance. --- LibGUI/GAbstractButton.cpp | 20 ++++++++++++++++++++ LibGUI/GAbstractButton.h | 5 +++++ LibGUI/GButton.cpp | 16 ++-------------- LibGUI/GCheckBox.cpp | 9 +-------- LibGUI/GRadioButton.cpp | 11 +++-------- 5 files changed, 31 insertions(+), 30 deletions(-) diff --git a/LibGUI/GAbstractButton.cpp b/LibGUI/GAbstractButton.cpp index 044dc26950..0e18a8b12c 100644 --- a/LibGUI/GAbstractButton.cpp +++ b/LibGUI/GAbstractButton.cpp @@ -1,4 +1,5 @@ #include +#include GAbstractButton::GAbstractButton(GWidget* parent) : GWidget(parent) @@ -106,3 +107,22 @@ void GAbstractButton::keydown_event(GKeyEvent& event) click(); GWidget::keydown_event(event); } + +void GAbstractButton::paint_text(GPainter& painter, const Rect& rect, const Font& font, TextAlignment text_alignment) +{ + if (!is_enabled()) { + painter.draw_text(rect.translated(1, 1), text(), font, text_alignment, Color::White, TextElision::Right); + painter.draw_text(rect, text(), font, text_alignment, Color::from_rgb(0x808080), TextElision::Right); + return; + } + + if (text().is_empty()) + return; + painter.draw_text(rect, text(), font, text_alignment, foreground_color(), TextElision::Right); + if (is_focused()) { + Rect focus_rect = { 0, 0, font.width(text()), font.glyph_height() }; + focus_rect.inflate(6, 4); + focus_rect.center_within(rect); + painter.draw_rect(focus_rect, Color(140, 140, 140)); + } +} diff --git a/LibGUI/GAbstractButton.h b/LibGUI/GAbstractButton.h index 914693cd38..75627e5e0e 100644 --- a/LibGUI/GAbstractButton.h +++ b/LibGUI/GAbstractButton.h @@ -1,6 +1,9 @@ #pragma once #include +#include + +class GPainter; class GAbstractButton : public GWidget { public: @@ -35,6 +38,8 @@ protected: virtual void enter_event(CEvent&) override; virtual void leave_event(CEvent&) override; + void paint_text(GPainter&, const Rect&, const Font&, TextAlignment); + private: String m_text; bool m_checked { false }; diff --git a/LibGUI/GButton.cpp b/LibGUI/GButton.cpp index 39ed387de5..a00ed10dc0 100644 --- a/LibGUI/GButton.cpp +++ b/LibGUI/GButton.cpp @@ -48,20 +48,8 @@ void GButton::paint_event(GPaintEvent& event) content_rect.move_by(m_icon->width() + 4, 0); content_rect.set_width(content_rect.width() - m_icon->width() - 4); } - if (is_enabled()) { - if (!text().is_empty()) { - painter.draw_text(content_rect, text(), font, text_alignment(), foreground_color(), TextElision::Right); - if (is_focused()) { - Rect focus_rect = { 0, 0, font.width(text()), font.glyph_height() }; - focus_rect.inflate(6, 4); - focus_rect.center_within(content_rect); - painter.draw_rect(focus_rect, Color(140, 140, 140)); - } - } - } else { - painter.draw_text(content_rect.translated(1, 1), text(), font, text_alignment(), Color::White, TextElision::Right); - painter.draw_text(content_rect, text(), font, text_alignment(), Color::from_rgb(0x808080), TextElision::Right); - } + + paint_text(painter, content_rect, font, text_alignment()); } void GButton::click() diff --git a/LibGUI/GCheckBox.cpp b/LibGUI/GCheckBox.cpp index 7b7606c177..b40c22624e 100644 --- a/LibGUI/GCheckBox.cpp +++ b/LibGUI/GCheckBox.cpp @@ -66,14 +66,7 @@ void GCheckBox::paint_event(GPaintEvent& event) painter.draw_bitmap(box_rect.shrunken(4, 4).location(), *s_checked_bitmap, foreground_color()); } - if (!text().is_empty()) { - painter.draw_text(text_rect, text(), TextAlignment::TopLeft, foreground_color()); - if (is_focused()) { - Rect focus_rect = text_rect; - focus_rect.inflate(6, 4); - painter.draw_rect(focus_rect, Color(140, 140, 140)); - } - } + paint_text(painter, text_rect, font(), TextAlignment::TopLeft); } void GCheckBox::click() diff --git a/LibGUI/GRadioButton.cpp b/LibGUI/GRadioButton.cpp index e53684e200..d432823de7 100644 --- a/LibGUI/GRadioButton.cpp +++ b/LibGUI/GRadioButton.cpp @@ -45,14 +45,9 @@ void GRadioButton::paint_event(GPaintEvent& event) auto& bitmap = circle_bitmap(is_checked(), is_being_pressed()); painter.blit(circle_rect.location(), bitmap, bitmap.rect()); - if (!text().is_empty()) { - Rect text_rect { circle_rect.right() + 4, 0, font().width(text()), font().glyph_height() }; - text_rect.center_vertically_within(rect()); - painter.draw_text(text_rect, text(), TextAlignment::CenterLeft, foreground_color()); - - if (is_focused()) - painter.draw_rect(text_rect.inflated(6, 4), Color(140, 140, 140)); - } + Rect text_rect { circle_rect.right() + 4, 0, font().width(text()), font().glyph_height() }; + text_rect.center_vertically_within(rect()); + paint_text(painter, text_rect, font(), TextAlignment::TopLeft); } template