summaryrefslogtreecommitdiff
path: root/Widgets/Widget.cpp
blob: 18aa00929da435791a3cbba591d63cad89c4ce39 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "Widget.h"
#include "Event.h"
#include "EventLoop.h"
#include "GraphicsBitmap.h"
#include "WindowManager.h"
#include "Window.h"
#include "Painter.h"
#include <AK/Assertions.h>

Widget::Widget(Widget* parent)
    : Object(parent)
{
    setFont(nullptr);
    m_backgroundColor = Color::White;
    m_foregroundColor = Color::Black;
}

Widget::~Widget()
{
}

void Widget::setWindowRelativeRect(const Rect& rect, bool should_update)
{
    // FIXME: Make some kind of event loop driven ResizeEvent?
    m_relativeRect = rect;
    if (should_update)
        update();
}

void Widget::repaint(const Rect& rect)
{
    // FIXME: Implement.
}

void Widget::event(Event& event)
{
    switch (event.type()) {
    case Event::Paint:
        m_hasPendingPaintEvent = false;
        if (auto* win = window()) {
            if (win->is_being_dragged())
                return;
            if (!win->is_visible())
                return;
        }
        return paintEvent(static_cast<PaintEvent&>(event));
    case Event::Show:
        return showEvent(static_cast<ShowEvent&>(event));
    case Event::Hide:
        return hideEvent(static_cast<HideEvent&>(event));
    case Event::KeyDown:
        return keyDownEvent(static_cast<KeyEvent&>(event));
    case Event::KeyUp:
        return keyUpEvent(static_cast<KeyEvent&>(event));
    case Event::MouseMove:
        return mouseMoveEvent(static_cast<MouseEvent&>(event));
    case Event::MouseDown:
        // FIXME: Focus self if needed.
        return mouseDownEvent(static_cast<MouseEvent&>(event));
    case Event::MouseUp:
        return mouseUpEvent(static_cast<MouseEvent&>(event));
    default:
        return Object::event(event);
    }
}

void Widget::paintEvent(PaintEvent& event)
{
    //printf("Widget::paintEvent :)\n");
    if (fillWithBackgroundColor()) {
        Painter painter(*this);
        painter.fill_rect(rect(), backgroundColor());
    }
    for (auto* ch : children()) {
        auto* child = (Widget*)ch;
        child->event(event);
    }
}

void Widget::showEvent(ShowEvent&)
{
}

void Widget::hideEvent(HideEvent&)
{
}

void Widget::keyDownEvent(KeyEvent&)
{
}

void Widget::keyUpEvent(KeyEvent&)
{
}

void Widget::mouseDownEvent(MouseEvent&)
{
}

void Widget::mouseUpEvent(MouseEvent&)
{
}

void Widget::mouseMoveEvent(MouseEvent&)
{
}

void Widget::update()
{
    auto* w = window();
    if (!w)
        return;
    if (m_hasPendingPaintEvent)
        return;
    m_hasPendingPaintEvent = true;
    EventLoop::main().postEvent(w, make<PaintEvent>(relativeRect()));
}

Widget::HitTestResult Widget::hitTest(int x, int y)
{
    // FIXME: Care about z-order.
    for (auto* ch : children()) {
        auto* child = (Widget*)ch;
        if (child->relativeRect().contains(x, y)) {
            return child->hitTest(x - child->relativeRect().x(), y - child->relativeRect().y());
        }
    }
    return { this, x, y };
}

void Widget::setWindow(Window* window)
{
    if (m_window == window)
        return;
    m_window = window;
}

bool Widget::isFocused() const
{
    // FIXME: Implement.
    return false;
}

void Widget::setFocus(bool focus)
{
    if (focus == isFocused())
        return;
    // FIXME: Implement.
}

void Widget::setFont(RetainPtr<Font>&& font)
{
    if (!font)
        m_font = Font::default_font();
    else
        m_font = move(font);
}

GraphicsBitmap* Widget::backing()
{
    if (auto* w = window())
        return w->backing();
    return nullptr;
}