]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/windows/window.cpp
0e3365393e653ebdf829be465d6efb30d57ee169
[libs/gui.git] / source / graphics / windows / window.cpp
1 #define _WIN32_WINNT 0x0601  // Windows 7
2 #include "window.h"
3 #include "window_private.h"
4 #include <windowsx.h>
5 #include <versionhelpers.h>
6 #include <msp/core/application.h>
7 #include <msp/core/systemerror.h>
8
9 using namespace std;
10
11 namespace {
12
13 LRESULT CALLBACK wndproc_(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
14 {
15         if(msg==WM_CREATE)
16         {
17                 CREATESTRUCT *cs = reinterpret_cast<CREATESTRUCT *>(lparam);
18                 SetWindowLongPtr(hwnd, 0, reinterpret_cast<LONG_PTR>(cs->lpCreateParams));
19         }
20         else
21         {
22                 Msp::Graphics::Window *wnd = reinterpret_cast<Msp::Graphics::Window *>(GetWindowLongPtr(hwnd, 0));
23                 Msp::Graphics::Window::Event ev;
24                 ev.msg = msg;
25                 ev.wparam = wparam;
26                 ev.lparam = lparam;
27                 ev.extra = GetMessageExtraInfo();
28                 if(wnd && wnd->event(ev))
29                         return 0;
30         }
31
32         return DefWindowProc(hwnd, msg, wparam, lparam);
33 }
34
35 }
36
37 namespace Msp {
38 namespace Graphics {
39
40 void Window::platform_init()
41 {
42         static bool wndclass_created = false;
43
44         if(!wndclass_created)
45         {
46                 WNDCLASSEX wndcl;
47
48                 wndcl.cbSize = sizeof(WNDCLASSEX);
49                 wndcl.style = 0;
50                 wndcl.lpfnWndProc = &wndproc_;
51                 wndcl.cbClsExtra = 0;
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;
60
61                 if(!RegisterClassEx(&wndcl))
62                         throw system_error("RegisterClassEx");
63
64                 wndclass_created = true;
65         }
66
67         RECT rect;
68         SetRect(&rect, 0, 0, options.width, options.height);
69
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);
75
76         priv->window = CreateWindowEx(exstyle,
77                 "mspgui",
78                 "Window",
79                 style,
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,
83                 0,
84                 0,
85                 reinterpret_cast<HINSTANCE>(Application::get_data()),
86                 this);
87         if(!priv->window)
88                 throw system_error("CreateWindowEx");
89 }
90
91 void Window::platform_cleanup()
92 {
93         if(priv->window)
94                 CloseWindow(priv->window);
95 }
96
97 void Window::set_title(const string &title)
98 {
99         SetWindowText(priv->window, title.c_str());
100 }
101
102 void Window::platform_reconfigure(bool fullscreen_changed)
103 {
104         RECT rect;
105         SetRect(&rect, 0, 0, options.width, options.height);
106
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);
112
113         if(fullscreen_changed)
114         {
115                 bool was_visible = visible;
116                 if(was_visible)
117                         hide();
118                 SetWindowLongPtr(priv->window, GWL_EXSTYLE, exstyle);
119                 SetWindowLongPtr(priv->window, GWL_STYLE, style);
120                 if(was_visible)
121                         show();
122         }
123
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);
128         else
129                 SetWindowPos(priv->window, nullptr, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOMOVE|SWP_NOZORDER);
130 }
131
132 void Window::show_cursor(bool s)
133 {
134         priv->cursor_visible = s;
135         if(priv->cursor_in_client_area && !s)
136                 SetCursor(nullptr);
137 }
138
139 void Window::warp_pointer(int, int)
140 {
141 }
142
143 void Window::platform_set_touch_input()
144 {
145         if(!IsWindows7OrGreater())
146         {
147                 touch_input = false;
148                 throw runtime_error("no touch support");
149         }
150
151         if(touch_input)
152                 RegisterTouchWindow(priv->window, 3);  // TWF_FINETOUCH|TWF_WANTPALM
153         else
154                 UnregisterTouchWindow(priv->window);
155 }
156
157 void Window::platform_show()
158 {
159         ShowWindow(priv->window, SW_SHOWNORMAL);
160 }
161
162 void Window::platform_hide()
163 {
164         ShowWindow(priv->window, SW_HIDE);
165 }
166
167 bool Window::event(const Event &evnt)
168 {
169         switch(evnt.msg)
170         {
171         case WM_KEYDOWN:
172         case WM_KEYUP:
173         case WM_CHAR:
174         case WM_SYSKEYDOWN:
175         case WM_SYSKEYUP:
176         case WM_SYSCHAR:
177         case WM_LBUTTONDOWN:
178         case WM_LBUTTONUP:
179         case WM_MBUTTONDOWN:
180         case WM_MBUTTONUP:
181         case WM_RBUTTONDOWN:
182         case WM_RBUTTONUP:
183         case WM_MOUSEWHEEL:
184         case WM_MOUSEMOVE:
185         case WM_TOUCH:
186                 signal_input_event.emit(evnt);
187                 break;
188         case WM_SIZE:
189                 options.width = LOWORD(evnt.lparam);
190                 options.height = HIWORD(evnt.lparam);
191                 resizing = false;
192                 signal_resize.emit(options.width, options.height);
193                 break;
194         case WM_MOVE:
195                 options.x = static_cast<short>(LOWORD(evnt.lparam));
196                 options.y = static_cast<short>(HIWORD(evnt.lparam));
197                 moving = false;
198                 signal_move.emit(options.x, options.y);
199                 break;
200         case WM_CLOSE:
201                 signal_close.emit();
202                 break;
203         case WM_PAINT:
204                 {
205                         RECT update_rect;
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);
210
211                         PAINTSTRUCT paint;
212                         if(BeginPaint(priv->window, &paint))
213                                 EndPaint(priv->window, &paint);
214                 }
215                 break;
216         case WM_SETFOCUS:
217                 signal_got_focus.emit();
218                 break;
219         case WM_KILLFOCUS:
220                 signal_lost_focus.emit();
221                 break;
222         case WM_SETCURSOR:
223                 priv->cursor_in_client_area = (LOWORD(evnt.lparam)==HTCLIENT);
224                 if(priv->cursor_in_client_area && !priv->cursor_visible)
225                         SetCursor(nullptr);
226                 else
227                         return false;
228                 break;
229         default:
230                 return false;
231         }
232
233         return true;
234 }
235
236 } // namespace Graphics
237 } // namespace Msp