From aadb35ff4675719948f071d8061cabc3b716e713 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 9 May 2022 19:20:35 +0100 Subject: [PATCH] LibGUI: Add ability to position checkboxes to the right of their text This uses the new `checkbox_position` property, which can be "Left" or "Right". --- Base/usr/share/man/man5/GML-Widget-CheckBox.md | 7 ++++--- Userland/Libraries/LibGUI/CheckBox.cpp | 10 +++++++++- Userland/Libraries/LibGUI/CheckBox.h | 8 ++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Base/usr/share/man/man5/GML-Widget-CheckBox.md b/Base/usr/share/man/man5/GML-Widget-CheckBox.md index b8937cacc8..cc62c7c1be 100644 --- a/Base/usr/share/man/man5/GML-Widget-CheckBox.md +++ b/Base/usr/share/man/man5/GML-Widget-CheckBox.md @@ -27,9 +27,10 @@ Defines a GUI checkbox widget. ## Registered Properties -| Property | Type | Possible values | Description | -|----------|------|-----------------|--------------------------| -| autosize | bool | true or false | Determines if auto-sized | +| Property | Type | Possible values | Description | +|-------------------|--------|-------------------|--------------------------------------------------------------------| +| autosize | bool | true or false | Determines if auto-sized | +| checkbox_position | String | "Left" or "Right" | Place the checkbox itself on either the left or right of its label | ## See also - [GML Button](help://man/5/GML-Widget-Button) diff --git a/Userland/Libraries/LibGUI/CheckBox.cpp b/Userland/Libraries/LibGUI/CheckBox.cpp index a8967388f9..a35b746b2d 100644 --- a/Userland/Libraries/LibGUI/CheckBox.cpp +++ b/Userland/Libraries/LibGUI/CheckBox.cpp @@ -25,6 +25,11 @@ CheckBox::CheckBox(String text) { REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize); + REGISTER_ENUM_PROPERTY( + "checkbox_position", checkbox_position, set_checkbox_position, CheckBoxPosition, + { CheckBoxPosition::Left, "Left" }, + { CheckBoxPosition::Right, "Right" }); + set_min_width(32); set_fixed_height(22); } @@ -35,7 +40,8 @@ void CheckBox::paint_event(PaintEvent& event) painter.add_clip_rect(event.rect()); auto text_rect = rect(); - text_rect.set_left(s_box_width + s_horizontal_padding); + if (m_checkbox_position == CheckBoxPosition::Left) + text_rect.set_left(s_box_width + s_horizontal_padding); text_rect.set_width(font().width(text())); text_rect.set_top(height() / 2 - font().glyph_height() / 2); text_rect.set_height(font().glyph_height()); @@ -50,6 +56,8 @@ void CheckBox::paint_event(PaintEvent& event) 0, height() / 2 - s_box_height / 2 - 1, s_box_width, s_box_height }; + if (m_checkbox_position == CheckBoxPosition::Right) + box_rect.set_right_without_resize(rect().right()); Gfx::StylePainter::paint_check_box(painter, box_rect, palette(), is_enabled(), is_checked(), is_being_pressed()); diff --git a/Userland/Libraries/LibGUI/CheckBox.h b/Userland/Libraries/LibGUI/CheckBox.h index bbc882f6a8..f5899f0ff3 100644 --- a/Userland/Libraries/LibGUI/CheckBox.h +++ b/Userland/Libraries/LibGUI/CheckBox.h @@ -22,6 +22,13 @@ public: bool is_autosize() const { return m_autosize; } void set_autosize(bool); + enum class CheckBoxPosition { + Left, + Right, + }; + CheckBoxPosition checkbox_position() const { return m_checkbox_position; } + void set_checkbox_position(CheckBoxPosition value) { m_checkbox_position = value; } + private: explicit CheckBox(String = {}); @@ -34,6 +41,7 @@ private: virtual void paint_event(PaintEvent&) override; bool m_autosize { false }; + CheckBoxPosition m_checkbox_position { CheckBoxPosition::Left }; }; }