summaryrefslogtreecommitdiff
path: root/WindowServer/WSWindow.cpp
blob: a0cae6e7b318c0e86e94c661c075e07d1c133c70 (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
#include "WSWindow.h"
#include "WSWindowManager.h"
#include "WSEvent.h"
#include "WSEventLoop.h"
#include "Process.h"

WSWindow::WSWindow(Process& process, int window_id)
    : m_process(process)
    , m_window_id(window_id)
    , m_pid(process.pid())
{
    WSWindowManager::the().add_window(*this);
}

WSWindow::~WSWindow()
{
    WSWindowManager::the().remove_window(*this);
}

void WSWindow::set_title(String&& title)
{
    {
        WSWindowLocker locker(*this);
        if (m_title == title)
            return;
        m_title = move(title);
    }

    WSWindowManager::the().notify_title_changed(*this);
}

void WSWindow::set_rect(const Rect& rect)
{
    Rect old_rect;
    {
        WSWindowLocker locker(*this);
        if (m_rect == rect)
            return;
        old_rect = m_rect;
        m_rect = rect;
        m_backing = GraphicsBitmap::create(m_process, m_rect.size());
    }
    WSWindowManager::the().notify_rect_changed(*this, old_rect, rect);
}

// FIXME: Just use the same types.
static GUI_MouseButton to_api(MouseButton button)
{
    switch (button) {
    case MouseButton::None: return GUI_MouseButton::NoButton;
    case MouseButton::Left: return GUI_MouseButton::Left;
    case MouseButton::Right: return GUI_MouseButton::Right;
    case MouseButton::Middle: return GUI_MouseButton::Middle;
    }
}

void WSWindow::event(WSEvent& event)
{
    GUI_Event gui_event;
    gui_event.window_id = window_id();

    switch (event.type()) {
    case WSEvent::Paint:
        gui_event.type = GUI_Event::Type::Paint;
        gui_event.paint.rect = static_cast<WSPaintEvent&>(event).rect();
        break;
    case WSEvent::MouseMove:
        gui_event.type = GUI_Event::Type::MouseMove;
        gui_event.mouse.position = static_cast<WSMouseEvent&>(event).position();
        gui_event.mouse.button = GUI_MouseButton::NoButton;
        gui_event.mouse.buttons = static_cast<WSMouseEvent&>(event).buttons();
        break;
    case WSEvent::MouseDown:
        gui_event.type = GUI_Event::Type::MouseDown;
        gui_event.mouse.position = static_cast<WSMouseEvent&>(event).position();
        gui_event.mouse.button = to_api(static_cast<WSMouseEvent&>(event).button());
        gui_event.mouse.buttons = static_cast<WSMouseEvent&>(event).buttons();
        break;
    case WSEvent::MouseUp:
        gui_event.type = GUI_Event::Type::MouseUp;
        gui_event.mouse.position = static_cast<WSMouseEvent&>(event).position();
        gui_event.mouse.button = to_api(static_cast<WSMouseEvent&>(event).button());
        gui_event.mouse.buttons = static_cast<WSMouseEvent&>(event).buttons();
        break;
    case WSEvent::KeyDown:
        gui_event.type = GUI_Event::Type::KeyDown;
        gui_event.key.character = static_cast<WSKeyEvent&>(event).character();
        gui_event.key.key = static_cast<WSKeyEvent&>(event).key();
        gui_event.key.alt = static_cast<WSKeyEvent&>(event).alt();
        gui_event.key.ctrl = static_cast<WSKeyEvent&>(event).ctrl();
        gui_event.key.shift = static_cast<WSKeyEvent&>(event).shift();
        break;
    case WSEvent::WM_Invalidate:
        WSWindowManager::the().invalidate(*this, static_cast<WSWindowInvalidationEvent&>(event).rect());
        return;
    case WSEvent::WM_SetWindowRect:
        set_rect(static_cast<WSSetWindowRect&>(event).rect());
        return;
    case WSEvent::WM_SetWindowTitle:
        set_title(static_cast<WSSetWindowTitle&>(event).title());
        return;
    case WSEvent::WindowActivated:
        gui_event.type = GUI_Event::Type::WindowActivated;
        break;
    case WSEvent::WindowDeactivated:
        gui_event.type = GUI_Event::Type::WindowDeactivated;
        break;
    }

    if (gui_event.type == GUI_Event::Type::Invalid)
        return;

    {
        LOCKER(m_process.gui_events_lock());
        m_process.gui_events().append(move(gui_event));
    }
}