]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/windows/window.cpp
cc364723d292b109dcce847122f6096881befe1d
[libs/gui.git] / source / graphics / windows / window.cpp
1 #define _WIN32_WINNT 0x0601
2 #include <windowsx.h>
3 #include <msp/core/application.h>
4 #include <msp/core/systemerror.h>
5 #include "window.h"
6 #include "window_private.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                 SetWindowLong(hwnd, 0, reinterpret_cast<LONG>(cs->lpCreateParams));
18         }
19         else
20         {
21                 Msp::Graphics::Window *wnd = reinterpret_cast<Msp::Graphics::Window *>(GetWindowLong(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
90 void Window::platform_cleanup()
91 {
92         if(priv->window)
93                 CloseWindow(priv->window);
94 }
95
96 void Window::set_title(const string &title)
97 {
98         SetWindowText(priv->window, title.c_str());
99 }
100
101 void Window::platform_reconfigure(bool fullscreen_changed)
102 {
103         RECT rect;
104         SetRect(&rect, 0, 0, options.width, options.height);
105
106         int style = (options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
107         if(!options.resizable)
108                 style &= ~WS_THICKFRAME;
109         int exstyle = (options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
110         AdjustWindowRectEx(&rect, style, false, exstyle);
111
112         if(fullscreen_changed)
113         {
114                 hide();
115                 SetWindowLong(priv->window, GWL_EXSTYLE, exstyle);
116                 SetWindowLong(priv->window, GWL_STYLE, style);
117                 show();
118         }
119
120         if(options.fullscreen)
121                 SetWindowPos(priv->window, 0, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOZORDER);
122         else if(options.user_position)
123                 SetWindowPos(priv->window, 0, options.x, options.y, rect.right-rect.left, rect.bottom-rect.top, SWP_NOZORDER);
124         else
125                 SetWindowPos(priv->window, 0, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOMOVE|SWP_NOZORDER);
126 }
127
128 void Window::show_cursor(bool s)
129 {
130         ShowCursor(s);
131 }
132
133 void Window::warp_pointer(int, int)
134 {
135 }
136
137 void Window::platform_set_touch_input()
138 {
139         WORD winver = LOWORD(GetVersion);
140         if(winver<_WIN32_WINNT)
141         {
142                 touch_input = false;
143                 throw runtime_error("no touch support");
144         }
145
146         if(touch_input)
147                 RegisterTouchWindow(priv->window, 3);  // TWF_FINETOUCH|TWF_WANTPALM
148         else
149                 UnregisterTouchWindow(priv->window);
150 }
151
152 void Window::platform_show()
153 {
154         ShowWindow(priv->window, SW_SHOWNORMAL);
155 }
156
157 void Window::platform_hide()
158 {
159         ShowWindow(priv->window, SW_HIDE);
160 }
161
162 bool Window::event(const Event &evnt)
163 {
164         switch(evnt.msg)
165         {
166         case WM_KEYDOWN:
167         case WM_KEYUP:
168         case WM_CHAR:
169         case WM_LBUTTONDOWN:
170         case WM_LBUTTONUP:
171         case WM_MBUTTONDOWN:
172         case WM_MBUTTONUP:
173         case WM_RBUTTONDOWN:
174         case WM_RBUTTONUP:
175         case WM_MOUSEWHEEL:
176         case WM_MOUSEMOVE:
177         case WM_TOUCH:
178                 signal_input_event.emit(evnt);
179                 break;
180         case WM_SIZE:
181                 options.width = LOWORD(evnt.lparam);
182                 options.height = HIWORD(evnt.lparam);
183                 resizing = false;
184                 signal_resize.emit(options.width, options.height);
185                 break;
186         case WM_MOVE:
187                 options.x = static_cast<short>(LOWORD(evnt.lparam));
188                 options.y = static_cast<short>(HIWORD(evnt.lparam));
189                 moving = false;
190                 signal_move.emit(options.x, options.y);
191                 break;
192         case WM_CLOSE:
193                 signal_close.emit();
194                 break;
195         case WM_PAINT:
196                 {
197                         RECT update_rect;
198                         GetUpdateRect(priv->window, &update_rect, false);
199                         unsigned width = update_rect.right-update_rect.left;
200                         unsigned height = update_rect.bottom-update_rect.top;
201                         signal_expose.emit(update_rect.left, update_rect.top, width, height, evnt);
202
203                         PAINTSTRUCT paint;
204                         if(BeginPaint(priv->window, &paint))
205                                 EndPaint(priv->window, &paint);
206                 }
207                 break;
208         case WM_SETFOCUS:
209                 signal_got_focus.emit();
210                 break;
211         case WM_KILLFOCUS:
212                 signal_lost_focus.emit();
213                 break;
214         default:
215                 return false;
216         }
217
218         return true;
219 }
220
221 } // namespace Graphics
222 } // namespace Msp