ladybird/Widgets/CheckBox.cpp

104 lines
2 KiB
C++
Raw Normal View History

2018-10-12 14:15:14 +02:00
#include "CheckBox.h"
#include "Painter.h"
#include "CBitmap.h"
#include <cstdio>
CheckBox::CheckBox(Widget* parent)
: Widget(parent)
{
}
CheckBox::~CheckBox()
{
}
void CheckBox::setCaption(String&& caption)
{
if (caption == m_caption)
return;
m_caption = std::move(caption);
update();
}
void CheckBox::setIsChecked(bool b)
{
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 CheckBox::paintEvent(PaintEvent&)
2018-10-12 14:15:14 +02:00
{
Painter painter(*this);
2018-10-12 19:47:20 +02:00
auto bitmap = CBitmap::createFromASCII(isChecked() ? checkedBitmap : uncheckedBitmap, 11, 11);
2018-10-12 14:15:14 +02:00
auto textRect = rect();
textRect.setLeft(bitmap->width() + 4);
textRect.setTop(height() / 2 - font().glyphHeight() / 2);
2018-10-12 17:00:51 +02:00
Point bitmapPosition;
bitmapPosition.setX(2);
2018-10-12 19:47:20 +02:00
bitmapPosition.setY(height() / 2 - bitmap->height() / 2 - 1);
2018-10-12 14:15:14 +02:00
painter.fillRect(rect(), backgroundColor());
2018-10-12 23:02:23 +02:00
painter.drawBitmap(bitmapPosition, *bitmap, foregroundColor());
2018-10-12 14:15:14 +02:00
if (!caption().isEmpty()) {
2018-10-12 23:02:23 +02:00
painter.drawText(textRect, caption(), Painter::TextAlignment::TopLeft, foregroundColor());
2018-10-12 14:15:14 +02:00
}
}
void CheckBox::mouseDownEvent(MouseEvent& event)
2018-10-12 14:15:14 +02:00
{
printf("CheckBox::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());
}