ladybird/LibGUI/GCheckBox.cpp

138 lines
3.5 KiB
C++
Raw Normal View History

#include "GCheckBox.h"
#include <LibGUI/GPainter.h>
2019-01-20 01:49:56 +03:00
#include <SharedGraphics/CharacterBitmap.h>
#include <Kernel/KeyCode.h>
//#define GCHECKBOX_DEBUG
static const char* s_checked_bitmap_data = {
" "
" ## "
" ## "
" ## "
" ## "
" ## ## "
" #### "
" ## "
" "
};
static CharacterBitmap* s_checked_bitmap;
static const int s_checked_bitmap_width = 9;
static const int s_checked_bitmap_height = 9;
static const int s_box_width = 11;
static const int s_box_height = 11;
2018-10-12 15:15:14 +03:00
GCheckBox::GCheckBox(GWidget* parent)
: GWidget(parent)
2018-10-12 15:15:14 +03:00
{
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();
2018-10-12 15:15:14 +03:00
}
GCheckBox::~GCheckBox()
2018-10-12 15:15:14 +03:00
{
}
2019-01-21 02:46:08 +03:00
void GCheckBox::set_caption(String&& caption)
2018-10-12 15:15:14 +03:00
{
if (caption == m_caption)
return;
m_caption = move(caption);
2018-10-12 15:15:14 +03:00
update();
}
2019-01-21 02:46:08 +03:00
void GCheckBox::set_checked(bool b)
2018-10-12 15:15:14 +03:00
{
2019-01-21 02:46:08 +03:00
if (m_checked == b)
2018-10-12 15:15:14 +03:00
return;
2019-01-21 02:46:08 +03:00
m_checked = b;
if (on_change)
on_change(*this, b);
2018-10-12 15:15:14 +03:00
update();
}
void GCheckBox::paint_event(GPaintEvent& event)
2018-10-12 15:15:14 +03:00
{
GPainter painter(*this);
painter.add_clip_rect(event.rect());
2018-10-12 15:15:14 +03:00
auto text_rect = rect();
text_rect.set_left(s_box_width + 4);
text_rect.set_top(height() / 2 - font().glyph_height() / 2);
2018-10-12 18:00:51 +03:00
if (fill_with_background_color())
painter.fill_rect(rect(), background_color());
2018-10-12 15:15:14 +03:00
Rect box_rect {
2, height() / 2 - s_box_height / 2 - 1,
s_box_width, s_box_height
};
painter.fill_rect(box_rect, Color::White);
painter.draw_rect(box_rect, Color::Black);
2018-10-12 15:15:14 +03:00
if (m_being_modified)
painter.draw_rect(box_rect.shrunken(2, 2), Color::MidGray);
if (m_checked)
painter.draw_bitmap(box_rect.shrunken(2, 2).location(), *s_checked_bitmap, foreground_color());
if (!caption().is_empty())
2019-02-11 12:08:37 +03:00
painter.draw_text(text_rect, caption(), TextAlignment::TopLeft, foreground_color());
if (is_focused()) {
// NOTE: Painter::draw_focus_rect() will shrink(2,2) the passed rect.
auto focus_rect = box_rect;
focus_rect.inflate(4, 4);
painter.draw_focus_rect(focus_rect);
2018-10-12 15:15:14 +03:00
}
}
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);
}
2019-01-21 02:46:08 +03:00
void GCheckBox::mousedown_event(GMouseEvent& event)
2018-10-12 15:15:14 +03:00
{
#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);
}
2018-10-12 15:15:14 +03:00
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);
2018-10-12 15:15:14 +03:00
}
void GCheckBox::keydown_event(GKeyEvent& event)
{
if (event.key() == KeyCode::Key_Space) {
set_checked(!is_checked());
update();
}
GWidget::keydown_event(event);
}