]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/windows/window.cpp
2d4d75bead21b6a3f5b6e8208c454379479adcad
[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                 CW_USEDEFAULT, CW_USEDEFAULT,
80                 rect.right-rect.left, rect.bottom-rect.top,
81                 0,
82                 0,
83                 reinterpret_cast<HINSTANCE>(Application::get_data()),
84                 this);
85         if(!priv->window)
86                 throw system_error("CreateWindowEx");
87 }
88
89 void Window::platform_cleanup()
90 {
91         if(priv->window)
92                 CloseWindow(priv->window);
93 }
94
95 void Window::set_title(const string &title)
96 {
97         SetWindowText(priv->window, title.c_str());
98 }
99
100 void Window::platform_reconfigure(bool fullscreen_changed)
101 {
102         RECT rect;
103         SetRect(&rect, 0, 0, options.width, options.height);
104
105         int style = (options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
106         if(!options.resizable)
107                 style &= ~WS_THICKFRAME;
108         int exstyle = (options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
109         AdjustWindowRectEx(&rect, style, false, exstyle);
110
111         if(fullscreen_changed)
112         {
113                 hide();
114                 SetWindowLong(priv->window, GWL_EXSTYLE, exstyle);
115                 SetWindowLong(priv->window, GWL_STYLE, style);
116                 show();
117         }
118
119         if(options.fullscreen)
120                 SetWindowPos(priv->window, 0, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOZORDER);
121         else
122                 SetWindowPos(priv->window, 0, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOMOVE|SWP_NOZORDER);
123 }
124
125 void Window::show_cursor(bool s)
126 {
127         ShowCursor(s);
128 }
129
130 void Window::warp_pointer(int, int)
131 {
132 }
133
134 void Window::platform_set_touch_input()
135 {
136         WORD winver = LOWORD(GetVersion);
137         if(winver<_WIN32_WINNT)
138         {
139                 touch_input = false;
140                 throw runtime_error("no touch support");
141         }
142
143         if(touch_input)
144                 RegisterTouchWindow(priv->window, 3);  // TWF_FINETOUCH|TWF_WANTPALM
145         else
146                 UnregisterTouchWindow(priv->window);
147 }
148
149 void Window::platform_show()
150 {
151         ShowWindow(priv->window, SW_SHOWNORMAL);
152 }
153
154 void Window::platform_hide()
155 {
156         ShowWindow(priv->window, SW_HIDE);
157 }
158
159 bool Window::event(const Event &evnt)
160 {
161         switch(evnt.msg)
162         {
163         case WM_KEYDOWN:
164         case WM_KEYUP:
165         case WM_CHAR:
166         case WM_LBUTTONDOWN:
167         case WM_LBUTTONUP:
168         case WM_MBUTTONDOWN:
169         case WM_MBUTTONUP:
170         case WM_RBUTTONDOWN:
171         case WM_RBUTTONUP:
172         case WM_MOUSEWHEEL:
173         case WM_MOUSEMOVE:
174         case WM_TOUCHMOVE:
175                 signal_input_event.emit(evnt);
176                 break;
177         case WM_SIZE:
178                 options.width = LOWORD(evnt.lparam);
179                 options.height = HIWORD(evnt.lparam);
180                 resizing = false;
181                 signal_resize.emit(options.width, options.height);
182                 break;
183         case WM_CLOSE:
184                 signal_close.emit();
185                 break;
186         default:
187                 return false;
188         }
189
190         return true;
191 }
192
193 } // namespace Graphics
194 } // namespace Msp