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