From 677794f30d6d365a714aea3edbeb4a7668a96afc Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 24 May 2019 17:11:42 +0200 Subject: [PATCH] LibGUI: Make GCheckBox inherit from GAbstractButton. --- Applications/FontEditor/FontEditor.cpp | 6 +- Demos/HelloWorld/main.cpp | 2 +- DevTools/VisualBuilder/VBWidget.cpp | 2 +- DevTools/VisualBuilder/VBWidgetRegistry.cpp | 2 +- LibGUI/GAbstractButton.cpp | 2 + LibGUI/GAbstractButton.h | 4 +- LibGUI/GCheckBox.cpp | 81 +++------------------ LibGUI/GCheckBox.h | 21 +----- Userland/guitest2.cpp | 2 +- 9 files changed, 25 insertions(+), 97 deletions(-) diff --git a/Applications/FontEditor/FontEditor.cpp b/Applications/FontEditor/FontEditor.cpp index 0d6be9e57e..1ef0b49639 100644 --- a/Applications/FontEditor/FontEditor.cpp +++ b/Applications/FontEditor/FontEditor.cpp @@ -39,7 +39,7 @@ FontEditorWidget::FontEditorWidget(const String& path, RetainPtr&& edited_ auto* fixed_width_checkbox = new GCheckBox(font_group_box); fixed_width_checkbox->set_relative_rect(10, 45, 190, 20); - fixed_width_checkbox->set_caption("Fixed width"); + fixed_width_checkbox->set_text("Fixed width"); fixed_width_checkbox->set_checked(m_edited_font->is_fixed_width()); m_path_textbox = new GTextBox(this); @@ -102,8 +102,8 @@ FontEditorWidget::FontEditorWidget(const String& path, RetainPtr&& edited_ info_label->set_text(String::format("0x%b (%c)", glyph, glyph)); }; - fixed_width_checkbox->on_change = [this, width_spinbox, update_demo] (GCheckBox&, bool is_checked) { - m_edited_font->set_fixed_width(is_checked); + fixed_width_checkbox->on_checked = [this, width_spinbox, update_demo] (bool checked) { + m_edited_font->set_fixed_width(checked); width_spinbox->set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph())); m_glyph_editor_widget->update(); update_demo(); diff --git a/Demos/HelloWorld/main.cpp b/Demos/HelloWorld/main.cpp index 4f5d422b4b..a66b3490e5 100644 --- a/Demos/HelloWorld/main.cpp +++ b/Demos/HelloWorld/main.cpp @@ -24,7 +24,7 @@ int main(int argc, char** argv) label->set_text("Hello World!"); auto* button = new GButton(main_widget); - button->set_caption("Good-bye"); + button->set_text("Good-bye"); button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); button->set_preferred_size({ 0, 20 }); button->on_click = [&] (GButton&) { diff --git a/DevTools/VisualBuilder/VBWidget.cpp b/DevTools/VisualBuilder/VBWidget.cpp index a90b7fb1ea..084cddbaf4 100644 --- a/DevTools/VisualBuilder/VBWidget.cpp +++ b/DevTools/VisualBuilder/VBWidget.cpp @@ -161,7 +161,7 @@ void VBWidget::setup_properties() } if (m_type == VBWidgetType::GCheckBox) { - VB_ADD_PROPERTY(GCheckBox, "caption", caption, set_caption, string); + VB_ADD_PROPERTY(GCheckBox, "caption", text, set_text, string); VB_ADD_PROPERTY(GCheckBox, "checked", is_checked, set_checked, bool); } } diff --git a/DevTools/VisualBuilder/VBWidgetRegistry.cpp b/DevTools/VisualBuilder/VBWidgetRegistry.cpp index b6e3cfa561..1f38e5de7c 100644 --- a/DevTools/VisualBuilder/VBWidgetRegistry.cpp +++ b/DevTools/VisualBuilder/VBWidgetRegistry.cpp @@ -73,7 +73,7 @@ static GWidget* build_gwidget(VBWidgetType type, GWidget* parent) } case VBWidgetType::GCheckBox: { auto* box = new GCheckBox(parent); - box->set_caption("checkbox_1"); + box->set_text("checkbox_1"); return box; } default: diff --git a/LibGUI/GAbstractButton.cpp b/LibGUI/GAbstractButton.cpp index 9d69aa3fd2..044dc26950 100644 --- a/LibGUI/GAbstractButton.cpp +++ b/LibGUI/GAbstractButton.cpp @@ -29,6 +29,8 @@ void GAbstractButton::set_checked(bool checked) return; m_checked = checked; update(); + if (on_checked) + on_checked(checked); } void GAbstractButton::set_checkable(bool checkable) diff --git a/LibGUI/GAbstractButton.h b/LibGUI/GAbstractButton.h index e2122214ad..914693cd38 100644 --- a/LibGUI/GAbstractButton.h +++ b/LibGUI/GAbstractButton.h @@ -6,6 +6,8 @@ class GAbstractButton : public GWidget { public: virtual ~GAbstractButton() override; + Function on_checked; + void set_text(const String&); const String& text() const { return m_text; } @@ -19,8 +21,8 @@ public: bool is_being_pressed() const { return m_being_pressed; } virtual void click() = 0; - virtual const char* class_name() const override { return "GAbstractButton"; } + virtual bool accepts_focus() const override { return true; } protected: explicit GAbstractButton(GWidget* parent); diff --git a/LibGUI/GCheckBox.cpp b/LibGUI/GCheckBox.cpp index 913c5ecfa0..c2411ebb79 100644 --- a/LibGUI/GCheckBox.cpp +++ b/LibGUI/GCheckBox.cpp @@ -4,8 +4,6 @@ #include #include -//#define GCHECKBOX_DEBUG - static const char* s_checked_bitmap_data = { " " " # " @@ -25,7 +23,7 @@ static const int s_box_width = 13; static const int s_box_height = 13; GCheckBox::GCheckBox(GWidget* parent) - : GWidget(parent) + : GAbstractButton(parent) { if (!s_checked_bitmap) s_checked_bitmap = &CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref(); @@ -35,24 +33,6 @@ GCheckBox::~GCheckBox() { } -void GCheckBox::set_caption(const String& caption) -{ - if (caption == m_caption) - return; - m_caption = caption; - update(); -} - -void GCheckBox::set_checked(bool b) -{ - if (m_checked == b) - return; - m_checked = b; - if (on_change) - on_change(*this, b); - update(); -} - void GCheckBox::paint_event(GPaintEvent& event) { GPainter painter(*this); @@ -60,7 +40,7 @@ void GCheckBox::paint_event(GPaintEvent& event) auto text_rect = rect(); text_rect.set_left(s_box_width + 4); - text_rect.set_width(font().width(m_caption)); + text_rect.set_width(font().width(text())); text_rect.set_top(height() / 2 - font().glyph_height() / 2); text_rect.set_height(font().glyph_height()); @@ -74,14 +54,14 @@ void GCheckBox::paint_event(GPaintEvent& event) painter.fill_rect(box_rect, Color::White); StylePainter::paint_frame(painter, box_rect, FrameShape::Container, FrameShadow::Sunken, 2); - if (m_being_modified) + if (is_being_pressed()) painter.draw_rect(box_rect.shrunken(4, 4), Color::MidGray); - if (m_checked) + if (is_checked()) painter.draw_bitmap(box_rect.shrunken(4, 4).location(), *s_checked_bitmap, foreground_color()); - if (!caption().is_empty()) { - painter.draw_text(text_rect, caption(), TextAlignment::TopLeft, 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); @@ -90,50 +70,9 @@ void GCheckBox::paint_event(GPaintEvent& event) } } -void GCheckBox::mousemove_event(GMouseEvent& event) +void GCheckBox::click() { - if (event.buttons() == GMouseButton::Left) { - bool being_modified = rect().contains(event.position()); - if (being_modified != m_being_modified) { - m_being_modified = being_modified; - update(); - } - } - GWidget::mousemove_event(event); -} - -void GCheckBox::mousedown_event(GMouseEvent& event) -{ -#ifdef GCHECKBOX_DEBUG - dbgprintf("GCheckBox::mouse_down_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button()); -#endif - if (event.button() == GMouseButton::Left) { - m_being_modified = true; - update(); - } - GWidget::mousedown_event(event); -} - -void GCheckBox::mouseup_event(GMouseEvent& event) -{ -#ifdef GCHECKBOX_DEBUG - dbgprintf("GCheckBox::mouseup_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button()); -#endif - if (event.button() == GMouseButton::Left) { - bool was_being_pressed = m_being_modified; - m_being_modified = false; - if (was_being_pressed) - set_checked(!is_checked()); - update(); - } - GWidget::mouseup_event(event); -} - -void GCheckBox::keydown_event(GKeyEvent& event) -{ - if (event.key() == KeyCode::Key_Space) { - set_checked(!is_checked()); - update(); - } - GWidget::keydown_event(event); + if (!is_enabled()) + return; + set_checked(!is_checked()); } diff --git a/LibGUI/GCheckBox.h b/LibGUI/GCheckBox.h index 760eb3f306..7757a82c89 100644 --- a/LibGUI/GCheckBox.h +++ b/LibGUI/GCheckBox.h @@ -1,34 +1,19 @@ #pragma once -#include "GWidget.h" #include #include +#include -class GCheckBox final : public GWidget { +class GCheckBox : public GAbstractButton { public: explicit GCheckBox(GWidget* parent); virtual ~GCheckBox() override; - String caption() const { return m_caption; } - void set_caption(const String&); - - bool is_checked() const { return m_checked; } - void set_checked(bool); - - Function on_change; + virtual void click() override; virtual const char* class_name() const override { return "GCheckBox"; } private: virtual void paint_event(GPaintEvent&) override; - virtual void mousedown_event(GMouseEvent&) override; - virtual void mouseup_event(GMouseEvent&) override; - virtual void mousemove_event(GMouseEvent&) override; - virtual void keydown_event(GKeyEvent&) override; - virtual bool accepts_focus() const override { return true; } - - String m_caption; - bool m_checked { false }; - bool m_being_modified { false }; }; diff --git a/Userland/guitest2.cpp b/Userland/guitest2.cpp index 39f9082559..79c229c8f8 100644 --- a/Userland/guitest2.cpp +++ b/Userland/guitest2.cpp @@ -113,7 +113,7 @@ GWindow* make_launcher_window() auto* checkbox = new GCheckBox(widget); checkbox->set_relative_rect({ 5, 170, 90, 20 }); - checkbox->set_caption("CheckBox"); + checkbox->set_text("CheckBox"); window->set_focused_widget(textbox);