forked from BellCrow/GettingInShapes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Window.cpp
85 lines (71 loc) · 2.12 KB
/
Window.cpp
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
#include "Window.h"
#include "WindowClass.h"
void Window::FireEvent(const WindowMessage& message)
{
for (auto subscriber : _subscribers)
{
subscriber->HandleWindowEvent(message);
}
}
Window::Window(int width, int heigth, const char * name)
{
RECT wr = { 0 };
wr.left = 100;
wr.top = 100;
wr.bottom = wr.top + heigth;
wr.right = wr.left + width;
if (AdjustWindowRect(&wr, WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU, false) == FALSE)
{
throw std::exception("Error in adjust window rect");
}
m_windowHandle = CreateWindow(
WindowClass::GetWindowClassName(), name,
WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
CW_USEDEFAULT, CW_USEDEFAULT, wr.right - wr.left, wr.bottom - wr.top,
nullptr, nullptr, WindowClass::GetInstance(), this);
if (m_windowHandle == 0)
{
throw std::exception("Error in create window");
}
ShowWindow(m_windowHandle, SW_SHOWDEFAULT);
}
Window::~Window() noexcept
{
DestroyWindow(m_windowHandle);
}
LRESULT Window::InitialMessageHandler(HWND handle, UINT msg, WPARAM w, LPARAM l) noexcept
{
if (msg == WM_NCCREATE)
{
const CREATESTRUCT* const pCreate = reinterpret_cast<CREATESTRUCT*>(l);
auto pWnd = static_cast<Window*>(pCreate->lpCreateParams);
SetWindowLongPtr(handle, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pWnd));
SetWindowLongPtr(handle, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(&HandleMessageAdapter));
return pWnd->HandleMsg(handle, msg, w, l);
}
return DefWindowProc(handle, msg, w, l);
}
LRESULT Window::HandleMessageAdapter(HWND handle, UINT msg, WPARAM w, LPARAM l) noexcept
{
Window* windowClassPtr = reinterpret_cast<Window*>(GetWindowLongPtr(handle, GWLP_USERDATA));
return windowClassPtr->HandleMsg(handle, msg, w, l);
}
LRESULT Window::HandleMsg(HWND handle, UINT msg, WPARAM w, LPARAM l) noexcept
{
FireEvent(WindowMessage(handle, msg, w, l));
if (msg == WM_CLOSE)
{
PostQuitMessage(0);
return 0;
}
return DefWindowProc(handle, msg, w, l);
}
void Window::SetTitle(const char * title) const
{
SetWindowText(m_windowHandle, title);
}
void Window::Subscribe(std::shared_ptr<IWindowMessageReceiver> subscriber)
{
_subscribers.push_back(subscriber);
}
//STATIC WINDOW CLASS