Skip to content

Commit

Permalink
LibGUI: Make GCheckBox inherit from GAbstractButton.
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed May 24, 2019
1 parent 21c5647 commit 677794f
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 97 deletions.
6 changes: 3 additions & 3 deletions Applications/FontEditor/FontEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ FontEditorWidget::FontEditorWidget(const String& path, RetainPtr<Font>&& 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);
Expand Down Expand Up @@ -102,8 +102,8 @@ FontEditorWidget::FontEditorWidget(const String& path, RetainPtr<Font>&& 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();
Expand Down
2 changes: 1 addition & 1 deletion Demos/HelloWorld/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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&) {
Expand Down
2 changes: 1 addition & 1 deletion DevTools/VisualBuilder/VBWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion DevTools/VisualBuilder/VBWidgetRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions LibGUI/GAbstractButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion LibGUI/GAbstractButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class GAbstractButton : public GWidget {
public:
virtual ~GAbstractButton() override;

Function<void(bool)> on_checked;

void set_text(const String&);
const String& text() const { return m_text; }

Expand All @@ -19,8 +21,8 @@ class GAbstractButton : public GWidget {
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);
Expand Down
81 changes: 10 additions & 71 deletions LibGUI/GCheckBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <SharedGraphics/StylePainter.h>
#include <Kernel/KeyCode.h>

//#define GCHECKBOX_DEBUG

static const char* s_checked_bitmap_data = {
" "
" # "
Expand All @@ -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();
Expand All @@ -35,32 +33,14 @@ 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);
painter.add_clip_rect(event.rect());

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());

Expand All @@ -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);
Expand All @@ -90,50 +70,9 @@ void GCheckBox::paint_event(GPaintEvent& event)
}
}

void GCheckBox::mousemove_event(GMouseEvent& event)
{
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)
void GCheckBox::click()
{
#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());
}
21 changes: 3 additions & 18 deletions LibGUI/GCheckBox.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
#pragma once

#include "GWidget.h"
#include <AK/AKString.h>
#include <AK/Function.h>
#include <LibGUI/GAbstractButton.h>

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<void(GCheckBox&, bool)> 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 };
};

2 changes: 1 addition & 1 deletion Userland/guitest2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 677794f

Please sign in to comment.