summaryrefslogtreecommitdiff
path: root/LibGUI/GAbstractButton.cpp
blob: 03f95e0e1dc7f0d83256f222c054a01f0039b548 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <LibGUI/GAbstractButton.h>
#include <LibGUI/GPainter.h>

GAbstractButton::GAbstractButton(GWidget* parent)
    : GWidget(parent)
{
}

GAbstractButton::GAbstractButton(const String& text, GWidget* parent)
    : GWidget(parent)
    , m_text(text)
{
}

GAbstractButton::~GAbstractButton()
{
}

void GAbstractButton::set_text(const String& text)
{
    if (m_text == text)
        return;
    m_text = text;
    update();
}

void GAbstractButton::set_checked(bool checked)
{
    if (m_checked == checked)
        return;
    m_checked = checked;
    update();
    if (on_checked)
        on_checked(checked);
}

void GAbstractButton::set_checkable(bool checkable)
{
    if (m_checkable == checkable)
        return;
    m_checkable = checkable;
    update();
}

void GAbstractButton::mousemove_event(GMouseEvent& event)
{
    bool is_over = rect().contains(event.position());
    m_hovered = is_over;
    if (event.buttons() & GMouseButton::Left) {
        if (is_enabled()) {
            bool being_pressed = is_over;
            if (being_pressed != m_being_pressed) {
                m_being_pressed = being_pressed;
                update();
            }
        }
    }
    GWidget::mousemove_event(event);
}

void GAbstractButton::mousedown_event(GMouseEvent& event)
{
#ifdef GABSTRACTBUTTON_DEBUG
    dbgprintf("GAbstractButton::mouse_down_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
#endif
    if (event.button() == GMouseButton::Left) {
        if (is_enabled()) {
            m_being_pressed = true;
            update();
        }
    }
    GWidget::mousedown_event(event);
}

void GAbstractButton::mouseup_event(GMouseEvent& event)
{
#ifdef GABSTRACTBUTTON_DEBUG
    dbgprintf("GAbstractButton::mouse_up_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
#endif
    if (event.button() == GMouseButton::Left) {
        if (is_enabled()) {
            bool was_being_pressed = m_being_pressed;
            m_being_pressed = false;
            update();
            if (was_being_pressed)
                click();
        }
    }
    GWidget::mouseup_event(event);
}

void GAbstractButton::enter_event(CEvent&)
{
    m_hovered = true;
    update();
}

void GAbstractButton::leave_event(CEvent&)
{
    m_hovered = false;
    update();
}

void GAbstractButton::keydown_event(GKeyEvent& event)
{
    if (event.key() == KeyCode::Key_Return)
        click();
    GWidget::keydown_event(event);
}

void GAbstractButton::paint_text(GPainter& painter, const Rect& rect, const Font& font, TextAlignment text_alignment)
{
    auto clipped_rect = rect.intersected(this->rect());

    if (!is_enabled()) {
        painter.draw_text(clipped_rect.translated(1, 1), text(), font, text_alignment, Color::White, TextElision::Right);
        painter.draw_text(clipped_rect, text(), font, text_alignment, Color::from_rgb(0x808080), TextElision::Right);
        return;
    }

    if (text().is_empty())
        return;
    painter.draw_text(clipped_rect, text(), font, text_alignment, foreground_color(), TextElision::Right);
    if (is_focused())
        painter.draw_rect(clipped_rect.inflated(6, 4), Color(140, 140, 140));
}

void GAbstractButton::change_event(GEvent& event)
{
    if (event.type() == GEvent::Type::EnabledChange) {
        if (!is_enabled()) {
            bool was_being_pressed = m_being_pressed;
            m_being_pressed = false;
            if (was_being_pressed)
                update();
        }
    }
    GWidget::change_event(event);
}