LibGUI: Make GCheckBox inherit from GAbstractButton.

This commit is contained in:
Andreas Kling 2019-05-24 17:11:42 +02:00
parent 21c56477b0
commit 677794f30d
Notes: sideshowbarker 2024-07-19 13:58:21 +09:00
9 changed files with 25 additions and 97 deletions

View file

@ -4,8 +4,6 @@
#include <SharedGraphics/StylePainter.h>
#include <Kernel/KeyCode.h>
//#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());
}