ladybird/LibGUI/GCheckBox.cpp

103 lines
2.1 KiB
C++
Raw Normal View History

#include "GCheckBox.h"
2019-01-19 23:49:56 +01:00
#include <SharedGraphics/Painter.h>
#include <SharedGraphics/CharacterBitmap.h>
2018-10-12 14:15:14 +02:00
GCheckBox::GCheckBox(GWidget* parent)
: GWidget(parent)
2018-10-12 14:15:14 +02:00
{
}
GCheckBox::~GCheckBox()
2018-10-12 14:15:14 +02:00
{
}
void GCheckBox::setCaption(String&& caption)
2018-10-12 14:15:14 +02:00
{
if (caption == m_caption)
return;
m_caption = move(caption);
2018-10-12 14:15:14 +02:00
update();
}
void GCheckBox::setIsChecked(bool b)
2018-10-12 14:15:14 +02:00
{
if (m_isChecked == b)
return;
m_isChecked = b;
update();
}
static const char* uncheckedBitmap = {
2018-10-12 19:47:20 +02:00
"###########"
"# #"
"# #"
"# #"
"# #"
"# #"
"# #"
"# #"
"# #"
"# #"
"###########"
2018-10-12 14:15:14 +02:00
};
2018-10-12 19:47:20 +02:00
#if 0
2018-10-12 14:15:14 +02:00
static const char* checkedBitmap = {
"############"
"# #"
2018-10-12 19:47:20 +02:00
"# ## #"
"# ## #"
"# ## #"
"# ## #"
"# ## #"
"# ## ## #"
"# ## ## #"
"# ### #"
2018-10-12 14:15:14 +02:00
"# #"
"############"
};
2018-10-12 19:47:20 +02:00
#endif
static const char* checkedBitmap = {
"###########"
"## ##"
"# # # #"
"# # # #"
"# # # #"
"# # #"
"# # # #"
"# # # #"
"# # # #"
"## ##"
"###########"
};
2018-10-12 14:15:14 +02:00
void GCheckBox::paintEvent(GPaintEvent&)
2018-10-12 14:15:14 +02:00
{
Painter painter(*this);
auto bitmap = CharacterBitmap::create_from_ascii(isChecked() ? checkedBitmap : uncheckedBitmap, 11, 11);
2018-10-12 14:15:14 +02:00
auto textRect = rect();
textRect.set_left(bitmap->width() + 4);
textRect.set_top(height() / 2 - font().glyph_height() / 2);
2018-10-12 17:00:51 +02:00
Point bitmapPosition;
bitmapPosition.set_x(2);
bitmapPosition.set_y(height() / 2 - bitmap->height() / 2 - 1);
2018-10-12 14:15:14 +02:00
painter.fill_rect(rect(), backgroundColor());
painter.draw_bitmap(bitmapPosition, *bitmap, foregroundColor());
2018-10-12 14:15:14 +02:00
2018-12-21 02:10:45 +01:00
if (!caption().is_empty()) {
painter.draw_text(textRect, caption(), Painter::TextAlignment::TopLeft, foregroundColor());
2018-10-12 14:15:14 +02:00
}
}
void GCheckBox::mouseDownEvent(GMouseEvent& event)
2018-10-12 14:15:14 +02:00
{
dbgprintf("GCheckBox::mouseDownEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
2018-10-12 14:15:14 +02:00
setIsChecked(!isChecked());
}