2019-06-07 09:46:02 +00:00
|
|
|
#include <Kernel/KeyCode.h>
|
2019-04-10 01:44:23 +00:00
|
|
|
#include <LibGUI/GCheckBox.h>
|
2019-03-28 16:19:56 +00:00
|
|
|
#include <LibGUI/GPainter.h>
|
2019-01-19 22:49:56 +00:00
|
|
|
#include <SharedGraphics/CharacterBitmap.h>
|
2019-04-10 01:44:23 +00:00
|
|
|
#include <SharedGraphics/StylePainter.h>
|
2019-01-27 19:22:06 +00:00
|
|
|
|
|
|
|
static const char* s_checked_bitmap_data = {
|
|
|
|
" "
|
2019-05-10 20:50:42 +00:00
|
|
|
" # "
|
2019-01-27 19:22:06 +00:00
|
|
|
" ## "
|
2019-05-10 20:50:42 +00:00
|
|
|
" ### "
|
|
|
|
" ## ### "
|
|
|
|
" ##### "
|
|
|
|
" ### "
|
|
|
|
" # "
|
2019-01-27 19:22:06 +00:00
|
|
|
" "
|
|
|
|
};
|
|
|
|
|
|
|
|
static CharacterBitmap* s_checked_bitmap;
|
|
|
|
static const int s_checked_bitmap_width = 9;
|
|
|
|
static const int s_checked_bitmap_height = 9;
|
2019-04-10 01:44:23 +00:00
|
|
|
static const int s_box_width = 13;
|
|
|
|
static const int s_box_height = 13;
|
2018-10-12 12:15:14 +00:00
|
|
|
|
2019-01-20 03:49:48 +00:00
|
|
|
GCheckBox::GCheckBox(GWidget* parent)
|
2019-05-24 15:11:42 +00:00
|
|
|
: GAbstractButton(parent)
|
2018-10-12 12:15:14 +00:00
|
|
|
{
|
2019-05-24 20:47:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 12:58:02 +00:00
|
|
|
GCheckBox::GCheckBox(const StringView& text, GWidget* parent)
|
2019-05-24 20:47:01 +00:00
|
|
|
: GAbstractButton(text, parent)
|
|
|
|
{
|
2018-10-12 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 03:49:48 +00:00
|
|
|
GCheckBox::~GCheckBox()
|
2018-10-12 12:15:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-28 18:34:55 +00:00
|
|
|
void GCheckBox::paint_event(GPaintEvent& event)
|
2018-10-12 12:15:14 +00:00
|
|
|
{
|
2019-03-28 16:19:56 +00:00
|
|
|
GPainter painter(*this);
|
2019-03-29 14:01:54 +00:00
|
|
|
painter.add_clip_rect(event.rect());
|
2018-10-12 12:15:14 +00:00
|
|
|
|
2019-01-27 19:22:06 +00:00
|
|
|
auto text_rect = rect();
|
|
|
|
text_rect.set_left(s_box_width + 4);
|
2019-05-24 15:11:42 +00:00
|
|
|
text_rect.set_width(font().width(text()));
|
2019-01-27 19:22:06 +00:00
|
|
|
text_rect.set_top(height() / 2 - font().glyph_height() / 2);
|
2019-05-15 00:39:58 +00:00
|
|
|
text_rect.set_height(font().glyph_height());
|
2018-10-12 15:00:51 +00:00
|
|
|
|
2019-01-27 19:22:06 +00:00
|
|
|
if (fill_with_background_color())
|
|
|
|
painter.fill_rect(rect(), background_color());
|
2018-10-12 12:15:14 +00:00
|
|
|
|
2019-01-27 19:22:06 +00:00
|
|
|
Rect box_rect {
|
2019-04-10 01:44:23 +00:00
|
|
|
0, height() / 2 - s_box_height / 2 - 1,
|
2019-01-27 19:22:06 +00:00
|
|
|
s_box_width, s_box_height
|
|
|
|
};
|
2019-02-04 10:51:42 +00:00
|
|
|
painter.fill_rect(box_rect, Color::White);
|
2019-04-10 01:44:23 +00:00
|
|
|
StylePainter::paint_frame(painter, box_rect, FrameShape::Container, FrameShadow::Sunken, 2);
|
2018-10-12 12:15:14 +00:00
|
|
|
|
2019-05-24 15:11:42 +00:00
|
|
|
if (is_being_pressed())
|
2019-04-10 01:44:23 +00:00
|
|
|
painter.draw_rect(box_rect.shrunken(4, 4), Color::MidGray);
|
2019-01-27 19:22:06 +00:00
|
|
|
|
2019-05-24 20:47:01 +00:00
|
|
|
if (is_checked()) {
|
|
|
|
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();
|
2019-04-10 01:44:23 +00:00
|
|
|
painter.draw_bitmap(box_rect.shrunken(4, 4).location(), *s_checked_bitmap, foreground_color());
|
2019-05-24 20:47:01 +00:00
|
|
|
}
|
2019-01-27 19:22:06 +00:00
|
|
|
|
2019-05-24 20:54:37 +00:00
|
|
|
paint_text(painter, text_rect, font(), TextAlignment::TopLeft);
|
2018-10-12 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 15:11:42 +00:00
|
|
|
void GCheckBox::click()
|
2019-01-27 19:22:06 +00:00
|
|
|
{
|
2019-05-24 15:11:42 +00:00
|
|
|
if (!is_enabled())
|
|
|
|
return;
|
|
|
|
set_checked(!is_checked());
|
2019-01-27 19:22:06 +00:00
|
|
|
}
|