summaryrefslogtreecommitdiff
path: root/Widgets/Window.cpp
blob: 63c0408329bc11efea1df17096d257429faca918 (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
#include "Window.h"
#include "WindowManager.h"
#include "Event.h"
#include "EventLoop.h"
#include "Widget.h"

Window::Window(Object* parent)
    : Object(parent)
{
    WindowManager::the().addWindow(*this);
}

Window::~Window()
{
    delete m_mainWidget;
    m_mainWidget = nullptr;
    if (parent())
        parent()->removeChild(*this);
    WindowManager::the().removeWindow(*this);
}

void Window::setMainWidget(Widget* widget)
{
    if (m_mainWidget == widget)
        return;

    m_mainWidget = widget;
    widget->setWindow(this);
}

void Window::setTitle(String&& title)
{
    if (m_title == title)
        return;

    m_title = move(title);
    WindowManager::the().notifyTitleChanged(*this);
}

void Window::setRect(const Rect& rect)
{
    if (m_rect == rect)
        return;
    auto oldRect = m_rect;
    m_rect = rect;
    m_backing = GraphicsBitmap::create(m_rect.size());
    WindowManager::the().notifyRectChanged(*this, oldRect, m_rect);
}

void Window::repaint(const Rect& rect)
{
    event(*make<PaintEvent>(rect));
}

void Window::update(const Rect& rect)
{
    EventLoop::main().postEvent(this, make<PaintEvent>(rect));
}

void Window::event(Event& event)
{
    if (event.isMouseEvent()) {
        auto& me = static_cast<MouseEvent&>(event);
        //printf("Window{%p}: %s %d,%d\n", this, me.name(), me.x(), me.y());
        if (m_mainWidget) {
            auto result = m_mainWidget->hitTest(me.x(), me.y());
            //printf("hit test for %d,%d found: %s{%p} %d,%d\n", me.x(), me.y(), result.widget->class_name(), result.widget, result.localX, result.localY);
            // FIXME: Re-use the existing event instead of crafting a new one?
            auto localEvent = make<MouseEvent>(event.type(), result.localX, result.localY, me.button());
            return result.widget->event(*localEvent);
        }
        return Object::event(event);
    }

    if (event.isPaintEvent()) {
        auto& pe = static_cast<PaintEvent&>(event);
        printf("Window[\"%s\"]: paintEvent %d,%d %dx%d\n", title().characters(),
                pe.rect().x(),
                pe.rect().y(),
                pe.rect().width(),
                pe.rect().height());

        if (isBeingDragged()) {
            // Ignore paint events during window drag.
            return;
        }
        if (m_mainWidget) {
            if (pe.rect().is_empty())
                m_mainWidget->event(*make<PaintEvent>(m_mainWidget->rect()));
            else
                m_mainWidget->event(event);
            WindowManager::the().did_paint(*this);
            return;
        }
        return Object::event(event);
    }

    if (event.isKeyEvent()) {
        if (m_focusedWidget)
            return m_focusedWidget->event(event);
        return Object::event(event);
    }

    return Object::event(event);
}

void Window::did_paint()
{
    WindowManager::the().did_paint(*this);
}

bool Window::isActive() const
{
    return WindowManager::the().activeWindow() == this;
}

bool Window::isVisible() const
{
    return WindowManager::the().isVisible(const_cast<Window&>(*this));
}

void Window::setFocusedWidget(Widget* widget)
{
    if (m_focusedWidget.ptr() == widget)
        return;
    auto* previously_focused_widget = m_focusedWidget.ptr();
    if (!widget)
        m_focusedWidget = nullptr;
    else {
        m_focusedWidget = widget->makeWeakPtr();
        m_focusedWidget->update();
        EventLoop::main().postEvent(m_focusedWidget.ptr(), make<Event>(Event::FocusIn));
    }
    if (previously_focused_widget) {
        previously_focused_widget->update();
        EventLoop::main().postEvent(previously_focused_widget, make<Event>(Event::FocusOut));
    }
}

void Window::close()
{
    WindowManager::the().removeWindow(*this);
    deleteLater();
}