summaryrefslogtreecommitdiff
path: root/Widgets/Widget.cpp
blob: a51cb0ba7c3577311bbea293c6c5649d1a3dc8d2 (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
#include "Widget.h"
#include "Event.h"
#include "EventLoop.h"
#include <AK/Assertions.h>

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

Widget::~Widget()
{
}

void Widget::setRect(const Rect& rect)
{
    // FIXME: Make some kind of event loop driven ResizeEvent?
    m_rect = rect;
    update();
}

void Widget::event(Event& event)
{
    switch (event.type()) {
    case Event::Paint:
        return onPaint(static_cast<PaintEvent&>(event));
    case Event::Show:
        return onShow(static_cast<ShowEvent&>(event));
    case Event::Hide:
        return onHide(static_cast<HideEvent&>(event));
    case Event::KeyDown:
        return onKeyDown(static_cast<KeyEvent&>(event));
    case Event::KeyUp:
        return onKeyUp(static_cast<KeyEvent&>(event));
    case Event::MouseMove:
        return onMouseMove(static_cast<MouseEvent&>(event));
    case Event::MouseDown:
        return onMouseDown(static_cast<MouseEvent&>(event));
    case Event::MouseUp:
        return onMouseUp(static_cast<MouseEvent&>(event));
    default:
        return Object::event(event);
    }
}

void Widget::onPaint(PaintEvent& event)
{
    printf("Widget::onPaint :)\n");
    for (auto* ch : children()) {
        auto* child = (Widget*)ch;
        child->onPaint(event);
    }
}

void Widget::onShow(ShowEvent&)
{
    update();
}

void Widget::onHide(HideEvent&)
{
}

void Widget::onKeyDown(KeyEvent&)
{
}

void Widget::onKeyUp(KeyEvent&)
{
}

void Widget::onMouseDown(MouseEvent&)
{
}

void Widget::onMouseUp(MouseEvent&)
{
}

void Widget::onMouseMove(MouseEvent&)
{
}

void Widget::update()
{
    EventLoop::main().postEvent(this, make<PaintEvent>());
}

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