mirror of
https://github.com/RGBCube/serenity
synced 2025-05-17 21:45:06 +00:00
LibGUI: Make GCheckBox inherit from GAbstractButton.
This commit is contained in:
parent
21c56477b0
commit
677794f30d
9 changed files with 25 additions and 97 deletions
|
@ -39,7 +39,7 @@ FontEditorWidget::FontEditorWidget(const String& path, RetainPtr<Font>&& edited_
|
||||||
|
|
||||||
auto* fixed_width_checkbox = new GCheckBox(font_group_box);
|
auto* fixed_width_checkbox = new GCheckBox(font_group_box);
|
||||||
fixed_width_checkbox->set_relative_rect(10, 45, 190, 20);
|
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());
|
fixed_width_checkbox->set_checked(m_edited_font->is_fixed_width());
|
||||||
|
|
||||||
m_path_textbox = new GTextBox(this);
|
m_path_textbox = new GTextBox(this);
|
||||||
|
@ -102,8 +102,8 @@ FontEditorWidget::FontEditorWidget(const String& path, RetainPtr<Font>&& edited_
|
||||||
info_label->set_text(String::format("0x%b (%c)", glyph, glyph));
|
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) {
|
fixed_width_checkbox->on_checked = [this, width_spinbox, update_demo] (bool checked) {
|
||||||
m_edited_font->set_fixed_width(is_checked);
|
m_edited_font->set_fixed_width(checked);
|
||||||
width_spinbox->set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph()));
|
width_spinbox->set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph()));
|
||||||
m_glyph_editor_widget->update();
|
m_glyph_editor_widget->update();
|
||||||
update_demo();
|
update_demo();
|
||||||
|
|
|
@ -24,7 +24,7 @@ int main(int argc, char** argv)
|
||||||
label->set_text("Hello World!");
|
label->set_text("Hello World!");
|
||||||
|
|
||||||
auto* button = new GButton(main_widget);
|
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_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
||||||
button->set_preferred_size({ 0, 20 });
|
button->set_preferred_size({ 0, 20 });
|
||||||
button->on_click = [&] (GButton&) {
|
button->on_click = [&] (GButton&) {
|
||||||
|
|
|
@ -161,7 +161,7 @@ void VBWidget::setup_properties()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_type == VBWidgetType::GCheckBox) {
|
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);
|
VB_ADD_PROPERTY(GCheckBox, "checked", is_checked, set_checked, bool);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,7 +73,7 @@ static GWidget* build_gwidget(VBWidgetType type, GWidget* parent)
|
||||||
}
|
}
|
||||||
case VBWidgetType::GCheckBox: {
|
case VBWidgetType::GCheckBox: {
|
||||||
auto* box = new GCheckBox(parent);
|
auto* box = new GCheckBox(parent);
|
||||||
box->set_caption("checkbox_1");
|
box->set_text("checkbox_1");
|
||||||
return box;
|
return box;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -29,6 +29,8 @@ void GAbstractButton::set_checked(bool checked)
|
||||||
return;
|
return;
|
||||||
m_checked = checked;
|
m_checked = checked;
|
||||||
update();
|
update();
|
||||||
|
if (on_checked)
|
||||||
|
on_checked(checked);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GAbstractButton::set_checkable(bool checkable)
|
void GAbstractButton::set_checkable(bool checkable)
|
||||||
|
|
|
@ -6,6 +6,8 @@ class GAbstractButton : public GWidget {
|
||||||
public:
|
public:
|
||||||
virtual ~GAbstractButton() override;
|
virtual ~GAbstractButton() override;
|
||||||
|
|
||||||
|
Function<void(bool)> on_checked;
|
||||||
|
|
||||||
void set_text(const String&);
|
void set_text(const String&);
|
||||||
const String& text() const { return m_text; }
|
const String& text() const { return m_text; }
|
||||||
|
|
||||||
|
@ -19,8 +21,8 @@ public:
|
||||||
bool is_being_pressed() const { return m_being_pressed; }
|
bool is_being_pressed() const { return m_being_pressed; }
|
||||||
|
|
||||||
virtual void click() = 0;
|
virtual void click() = 0;
|
||||||
|
|
||||||
virtual const char* class_name() const override { return "GAbstractButton"; }
|
virtual const char* class_name() const override { return "GAbstractButton"; }
|
||||||
|
virtual bool accepts_focus() const override { return true; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit GAbstractButton(GWidget* parent);
|
explicit GAbstractButton(GWidget* parent);
|
||||||
|
|
|
@ -4,8 +4,6 @@
|
||||||
#include <SharedGraphics/StylePainter.h>
|
#include <SharedGraphics/StylePainter.h>
|
||||||
#include <Kernel/KeyCode.h>
|
#include <Kernel/KeyCode.h>
|
||||||
|
|
||||||
//#define GCHECKBOX_DEBUG
|
|
||||||
|
|
||||||
static const char* s_checked_bitmap_data = {
|
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;
|
static const int s_box_height = 13;
|
||||||
|
|
||||||
GCheckBox::GCheckBox(GWidget* parent)
|
GCheckBox::GCheckBox(GWidget* parent)
|
||||||
: GWidget(parent)
|
: GAbstractButton(parent)
|
||||||
{
|
{
|
||||||
if (!s_checked_bitmap)
|
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();
|
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)
|
void GCheckBox::paint_event(GPaintEvent& event)
|
||||||
{
|
{
|
||||||
GPainter painter(*this);
|
GPainter painter(*this);
|
||||||
|
@ -60,7 +40,7 @@ void GCheckBox::paint_event(GPaintEvent& event)
|
||||||
|
|
||||||
auto text_rect = rect();
|
auto text_rect = rect();
|
||||||
text_rect.set_left(s_box_width + 4);
|
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_top(height() / 2 - font().glyph_height() / 2);
|
||||||
text_rect.set_height(font().glyph_height());
|
text_rect.set_height(font().glyph_height());
|
||||||
|
|
||||||
|
@ -74,14 +54,14 @@ void GCheckBox::paint_event(GPaintEvent& event)
|
||||||
painter.fill_rect(box_rect, Color::White);
|
painter.fill_rect(box_rect, Color::White);
|
||||||
StylePainter::paint_frame(painter, box_rect, FrameShape::Container, FrameShadow::Sunken, 2);
|
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);
|
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());
|
painter.draw_bitmap(box_rect.shrunken(4, 4).location(), *s_checked_bitmap, foreground_color());
|
||||||
|
|
||||||
if (!caption().is_empty()) {
|
if (!text().is_empty()) {
|
||||||
painter.draw_text(text_rect, caption(), TextAlignment::TopLeft, foreground_color());
|
painter.draw_text(text_rect, text(), TextAlignment::TopLeft, foreground_color());
|
||||||
if (is_focused()) {
|
if (is_focused()) {
|
||||||
Rect focus_rect = text_rect;
|
Rect focus_rect = text_rect;
|
||||||
focus_rect.inflate(6, 4);
|
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) {
|
if (!is_enabled())
|
||||||
bool being_modified = rect().contains(event.position());
|
return;
|
||||||
if (being_modified != m_being_modified) {
|
set_checked(!is_checked());
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,34 +1,19 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "GWidget.h"
|
|
||||||
#include <AK/AKString.h>
|
#include <AK/AKString.h>
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
|
#include <LibGUI/GAbstractButton.h>
|
||||||
|
|
||||||
class GCheckBox final : public GWidget {
|
class GCheckBox : public GAbstractButton {
|
||||||
public:
|
public:
|
||||||
explicit GCheckBox(GWidget* parent);
|
explicit GCheckBox(GWidget* parent);
|
||||||
virtual ~GCheckBox() override;
|
virtual ~GCheckBox() override;
|
||||||
|
|
||||||
String caption() const { return m_caption; }
|
virtual void click() override;
|
||||||
void set_caption(const String&);
|
|
||||||
|
|
||||||
bool is_checked() const { return m_checked; }
|
|
||||||
void set_checked(bool);
|
|
||||||
|
|
||||||
Function<void(GCheckBox&, bool)> on_change;
|
|
||||||
|
|
||||||
virtual const char* class_name() const override { return "GCheckBox"; }
|
virtual const char* class_name() const override { return "GCheckBox"; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void paint_event(GPaintEvent&) override;
|
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 };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -113,7 +113,7 @@ GWindow* make_launcher_window()
|
||||||
|
|
||||||
auto* checkbox = new GCheckBox(widget);
|
auto* checkbox = new GCheckBox(widget);
|
||||||
checkbox->set_relative_rect({ 5, 170, 90, 20 });
|
checkbox->set_relative_rect({ 5, 170, 90, 20 });
|
||||||
checkbox->set_caption("CheckBox");
|
checkbox->set_text("CheckBox");
|
||||||
|
|
||||||
window->set_focused_widget(textbox);
|
window->set_focused_widget(textbox);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue