summaryrefslogtreecommitdiff
path: root/Widgets/Widget.h
blob: c0e9b428f2325a0ae9fcad52f4447f19c234eef6 (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
#pragma once

#include "Event.h"
#include "Object.h"
#include "Rect.h"
#include "Color.h"
#include <AK/String.h>
#include <functional>

class Window;

class Widget : public Object {
public:
    explicit Widget(Widget* parent = nullptr);
    virtual ~Widget();

    virtual void event(Event&);
    virtual void paintEvent(PaintEvent&);
    virtual void showEvent(ShowEvent&);
    virtual void hideEvent(HideEvent&);
    virtual void keyDownEvent(KeyEvent&);
    virtual void keyUpEvent(KeyEvent&);
    virtual void mouseMoveEvent(MouseEvent&);
    virtual void mouseDownEvent(MouseEvent&);
    virtual void mouseUpEvent(MouseEvent&);

    Rect relativeRect() const { return m_relativeRect; }
    Point relativePosition() const { return m_relativeRect.location(); }

    int x() const { return m_relativeRect.x(); }
    int y() const { return m_relativeRect.y(); }
    int width() const { return m_relativeRect.width(); }
    int height() const { return m_relativeRect.height(); }

    Rect rect() const { return { 0, 0, width(), height() }; }

    void update();
    void repaint(const Rect&);

    bool isFocused() const;
    void setFocus(bool);

    struct HitTestResult {
        Widget* widget { nullptr };
        int localX { 0 };
        int localY { 0 };
    };
    HitTestResult hitTest(int x, int y);

    virtual const char* className() const override { return "Widget"; }

    void setWindowRelativeRect(const Rect&);

    Color backgroundColor() const { return m_backgroundColor; }
    Color foregroundColor() const { return m_foregroundColor; }

    void setBackgroundColor(Color color) { m_backgroundColor = color; }
    void setForegroundColor(Color color) { m_foregroundColor = color; }

    Window* window()
    {
        if (auto* pw = parentWidget())
            return pw->window();
        return m_window;
    }

    const Window* window() const
    {
        if (auto* pw = parentWidget())
            return pw->window();
        return m_window;
    }

    void setWindow(Window*);

    Widget* parentWidget() { return static_cast<Widget*>(parent()); }
    const Widget* parentWidget() const { return static_cast<const Widget*>(parent()); }

    void setFillWithBackgroundColor(bool b) { m_fillWithBackgroundColor = b; }
    bool fillWithBackgroundColor() const { return m_fillWithBackgroundColor; }

private:
    Window* m_window { nullptr };

    Rect m_relativeRect;
    Color m_backgroundColor;
    Color m_foregroundColor;

    bool m_hasPendingPaintEvent { false };
    bool m_fillWithBackgroundColor { false };
};