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

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

Window::~Window()
{
}

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 = std::move(title);
    WindowManager::the().notifyTitleChanged(*this);
}


void Window::setRect(const Rect& rect)
{
    if (m_rect == rect)
        return;
    auto oldRect = m_rect;
    m_rect = rect;
    WindowManager::the().notifyRectChanged(*this, oldRect, m_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->className(), 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());
            result.widget->event(*localEvent);
            return m_mainWidget->event(event);
        }
        return Object::event(event);
    }

    if (event.isPaintEvent()) {
        if (isBeingDragged()) {
            // Ignore paint events during window drag.
            return;
        }
        if (m_mainWidget)
            return m_mainWidget->event(event);
        return Object::event(event);
    }

    return Object::event(event);
}