1 #define _WIN32_WINNT 0x0601 // Windows 7
3 #include "window_private.h"
5 #include <versionhelpers.h>
6 #include <msp/core/application.h>
7 #include <msp/core/systemerror.h>
13 LRESULT CALLBACK wndproc_(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
17 CREATESTRUCT *cs = reinterpret_cast<CREATESTRUCT *>(lparam);
18 SetWindowLongPtr(hwnd, 0, reinterpret_cast<LONG_PTR>(cs->lpCreateParams));
22 Msp::Graphics::Window *wnd = reinterpret_cast<Msp::Graphics::Window *>(GetWindowLongPtr(hwnd, 0));
23 Msp::Graphics::Window::Event ev;
27 ev.extra = GetMessageExtraInfo();
28 if(wnd && wnd->event(ev))
32 return DefWindowProc(hwnd, msg, wparam, lparam);
40 void Window::platform_init()
42 static bool wndclass_created = false;
48 wndcl.cbSize = sizeof(WNDCLASSEX);
50 wndcl.lpfnWndProc = &wndproc_;
52 wndcl.cbWndExtra = sizeof(Window *);
53 wndcl.hInstance = reinterpret_cast<HINSTANCE>(Application::get_data());
54 wndcl.hIcon = nullptr;
55 wndcl.hCursor = LoadCursor(nullptr, IDC_ARROW);
56 wndcl.hbrBackground = nullptr;
57 wndcl.lpszMenuName = nullptr;
58 wndcl.lpszClassName = "mspgui";
59 wndcl.hIconSm = nullptr;
61 if(!RegisterClassEx(&wndcl))
62 throw system_error("RegisterClassEx");
64 wndclass_created = true;
68 SetRect(&rect, 0, 0, options.width, options.height);
70 int style = (options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
71 if(!options.resizable)
72 style &= ~WS_THICKFRAME;
73 int exstyle = (options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
74 AdjustWindowRectEx(&rect, style, false, exstyle);
76 priv->window = CreateWindowEx(exstyle,
80 (options.user_position ? options.x : CW_USEDEFAULT),
81 (options.user_position ? options.y : CW_USEDEFAULT),
82 rect.right-rect.left, rect.bottom-rect.top,
85 reinterpret_cast<HINSTANCE>(Application::get_data()),
88 throw system_error("CreateWindowEx");
91 void Window::platform_cleanup()
94 CloseWindow(priv->window);
97 void Window::set_title(const string &title)
99 SetWindowText(priv->window, title.c_str());
102 void Window::platform_reconfigure(bool fullscreen_changed)
105 SetRect(&rect, 0, 0, options.width, options.height);
107 int style = (options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
108 if(!options.resizable)
109 style &= ~WS_THICKFRAME;
110 int exstyle = (options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
111 AdjustWindowRectEx(&rect, style, false, exstyle);
113 if(fullscreen_changed)
115 bool was_visible = visible;
118 SetWindowLongPtr(priv->window, GWL_EXSTYLE, exstyle);
119 SetWindowLongPtr(priv->window, GWL_STYLE, style);
124 if(options.fullscreen)
125 SetWindowPos(priv->window, nullptr, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOZORDER);
126 else if(options.user_position)
127 SetWindowPos(priv->window, nullptr, options.x, options.y, rect.right-rect.left, rect.bottom-rect.top, SWP_NOZORDER);
129 SetWindowPos(priv->window, nullptr, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOMOVE|SWP_NOZORDER);
132 void Window::show_cursor(bool s)
134 priv->cursor_visible = s;
135 if(priv->cursor_in_client_area && !s)
139 void Window::warp_pointer(int, int)
143 void Window::platform_set_touch_input()
145 if(!IsWindows7OrGreater())
148 throw runtime_error("no touch support");
152 RegisterTouchWindow(priv->window, 3); // TWF_FINETOUCH|TWF_WANTPALM
154 UnregisterTouchWindow(priv->window);
157 void Window::platform_show()
159 ShowWindow(priv->window, SW_SHOWNORMAL);
162 void Window::platform_hide()
164 ShowWindow(priv->window, SW_HIDE);
167 bool Window::event(const Event &evnt)
186 signal_input_event.emit(evnt);
189 options.width = LOWORD(evnt.lparam);
190 options.height = HIWORD(evnt.lparam);
192 signal_resize.emit(options.width, options.height);
195 options.x = static_cast<short>(LOWORD(evnt.lparam));
196 options.y = static_cast<short>(HIWORD(evnt.lparam));
198 signal_move.emit(options.x, options.y);
206 GetUpdateRect(priv->window, &update_rect, false);
207 unsigned width = update_rect.right-update_rect.left;
208 unsigned height = update_rect.bottom-update_rect.top;
209 signal_expose.emit(update_rect.left, update_rect.top, width, height, evnt);
212 if(BeginPaint(priv->window, &paint))
213 EndPaint(priv->window, &paint);
217 signal_got_focus.emit();
220 signal_lost_focus.emit();
223 priv->cursor_in_client_area = (LOWORD(evnt.lparam)==HTCLIENT);
224 if(priv->cursor_in_client_area && !priv->cursor_visible)
236 } // namespace Graphics