summaryrefslogtreecommitdiff
path: root/Widgets/CheckBox.cpp
blob: 8e36694245b3bcdbc04cdee5c2fc012fb143c530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "CheckBox.h"
#include "Painter.h"
#include "CharacterBitmap.h"

CheckBox::CheckBox(Widget* parent)
    : Widget(parent)
{
}

CheckBox::~CheckBox()
{
}

void CheckBox::setCaption(String&& caption)
{
    if (caption == m_caption)
        return;
    m_caption = move(caption);
    update();
}

void CheckBox::setIsChecked(bool b)
{
    if (m_isChecked == b)
        return;
    m_isChecked = b;
    update();
}

static const char* uncheckedBitmap = {
    "###########"
    "#         #"
    "#         #"
    "#         #"
    "#         #"
    "#         #"
    "#         #"
    "#         #"
    "#         #"
    "#         #"
    "###########"
};

#if 0
static const char* checkedBitmap = {
    "############"
    "#          #"
    "#       ## #"
    "#       ## #"
    "#      ##  #"
    "#      ##  #"
    "#     ##   #"
    "# ##  ##   #"
    "#  ## ##   #"
    "#   ###    #"
    "#          #"
    "############"
};
#endif

static const char* checkedBitmap = {
    "###########"
    "##       ##"
    "# #     # #"
    "#  #   #  #"
    "#   # #   #"
    "#    #    #"
    "#   # #   #"
    "#  #   #  #"
    "# #     # #"
    "##       ##"
    "###########"
};

void CheckBox::paintEvent(PaintEvent&)
{
    Painter painter(*this);
    auto bitmap = CharacterBitmap::createFromASCII(isChecked() ? checkedBitmap : uncheckedBitmap, 11, 11);

    auto textRect = rect();
    textRect.set_left(bitmap->width() + 4);
    textRect.set_top(height() / 2 - font().glyphHeight() / 2);

    Point bitmapPosition;
    bitmapPosition.setX(2);
    bitmapPosition.setY(height() / 2 - bitmap->height() / 2 - 1);

    painter.fillRect(rect(), backgroundColor());
    painter.drawBitmap(bitmapPosition, *bitmap, foregroundColor());

    if (!caption().is_empty()) {
        painter.drawText(textRect, caption(), Painter::TextAlignment::TopLeft, foregroundColor());
    }
}

void CheckBox::mouseDownEvent(MouseEvent& event)
{
    printf("CheckBox::mouseDownEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());

    setIsChecked(!isChecked());
}