summaryrefslogtreecommitdiff
path: root/LibGUI/GWidget.h
blob: cb40a6dfa04f5425db8593c8b07e3827dce33757 (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
#pragma once

#include "GEvent.h"
#include "GObject.h"
#include <SharedGraphics/Rect.h>
#include <SharedGraphics/Color.h>
#include <SharedGraphics/Font.h>
#include <AK/AKString.h>

class GraphicsBitmap;
class GWindow;

class GWidget : public GObject {
public:
    explicit GWidget(GWidget* parent = nullptr);
    virtual ~GWidget() override;

    virtual void event(GEvent&) override;
    virtual void paint_event(GPaintEvent&);
    virtual void resize_event(GResizeEvent&);
    virtual void show_event(GShowEvent&);
    virtual void hide_event(GHideEvent&);
    virtual void keydown_event(GKeyEvent&);
    virtual void keyup_event(GKeyEvent&);
    virtual void mousemove_event(GMouseEvent&);
    virtual void mousedown_event(GMouseEvent&);
    virtual void mouseup_event(GMouseEvent&);
    virtual void focusin_event(GEvent&);
    virtual void focusout_event(GEvent&);

    Rect relative_rect() const { return m_relative_rect; }
    Point relative_position() const { return m_relative_rect.location(); }

    Rect window_relative_rect() const;

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

    Rect rect() const { return { 0, 0, width(), height() }; }
    Size size() const { return m_relative_rect.size(); }

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

    virtual bool accepts_focus() const { return false; }

    bool is_focused() const;
    void set_focus(bool);

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

    virtual const char* class_name() const override { return "GWidget"; }

    void set_relative_rect(const Rect&);
    void set_relative_rect(int x, int y, int width, int height) { set_relative_rect({ x, y, width, height }); }

    void move_to(const Point& point) { set_relative_rect({ point, relative_rect().size() }); }
    void move_to(int x, int y) { move_to({ x, y }); }
    void resize(const Size& size) { set_relative_rect({ relative_rect().location(), size }); }
    void resize(int width, int height) { resize({ width, height }); }

    Color background_color() const { return m_background_color; }
    Color foreground_color() const { return m_foreground_color; }

    void set_background_color(Color color) { m_background_color = color; }
    void set_foreground_color(Color color) { m_foreground_color = color; }

    GWindow* window()
    {
        if (auto* pw = parent_widget())
            return pw->window();
        return m_window;
    }

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

    void set_window(GWindow*);

    GWidget* parent_widget() { return static_cast<GWidget*>(parent()); }
    const GWidget* parent_widget() const { return static_cast<const GWidget*>(parent()); }

    void set_fill_with_background_color(bool b) { m_fill_with_background_color = b; }
    bool fill_with_background_color() const { return m_fill_with_background_color; }

    const Font& font() const { return *m_font; }
    void set_font(RetainPtr<Font>&&);

    void set_global_cursor_tracking(bool);
    bool global_cursor_tracking() const;

private:
    void handle_paint_event(GPaintEvent&);

    GWindow* m_window { nullptr };

    Rect m_relative_rect;
    Color m_background_color { 0xffffff };
    Color m_foreground_color { 0x000000 };
    RetainPtr<Font> m_font;

    bool m_has_pending_paint_event { false };
    bool m_fill_with_background_color { true };
};