Mercurial > projects > dynamin
annotate dynamin/gui/windows_window.d @ 72:8dac206ea523
Add shift/control/altDown to KeyEventArgs.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Tue, 11 Aug 2009 01:45:42 -0500 |
parents | c138461bf845 |
children | d5928da5a1f0 |
rev | line source |
---|---|
0 | 1 // Written in the D programming language |
2 // www.digitalmars.com/d/ | |
3 | |
4 /* | |
5 * The contents of this file are subject to the Mozilla Public License Version | |
6 * 1.1 (the "License"); you may not use this file except in compliance with | |
7 * the License. You may obtain a copy of the License at | |
8 * http://www.mozilla.org/MPL/ | |
9 * | |
10 * Software distributed under the License is distributed on an "AS IS" basis, | |
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 * for the specific language governing rights and limitations under the | |
13 * License. | |
14 * | |
15 * The Original Code is the Dynamin library. | |
16 * | |
17 * The Initial Developer of the Original Code is Jordan Miner. | |
18 * Portions created by the Initial Developer are Copyright (C) 2006-2009 | |
19 * the Initial Developer. All Rights Reserved. | |
20 * | |
21 * Contributor(s): | |
22 * Jordan Miner <jminer7@gmail.com> | |
23 * | |
24 */ | |
25 | |
26 module dynamin.gui.windows_window; | |
27 | |
28 public import dynamin.c.windows; | |
29 public import dynamin.c.cairo; | |
30 public import dynamin.c.cairo_win32; | |
31 public import dynamin.all_core; | |
32 public import dynamin.all_gui; | |
33 public import dynamin.gui.window; | |
34 public import dynamin.gui.key; | |
35 public import dynamin.all_painting; | |
36 public import tango.io.Stdout; | |
12
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
37 public import tango.core.sync.Semaphore; |
0 | 38 |
39 /// | |
40 enum WindowsVersion { | |
41 /// | |
17
ef81af74a306
Add Windows 7 to version checking
Jordan Miner <jminer7@gmail.com>
parents:
12
diff
changeset
|
42 Windows95, /// |
ef81af74a306
Add Windows 7 to version checking
Jordan Miner <jminer7@gmail.com>
parents:
12
diff
changeset
|
43 Windows98, /// |
ef81af74a306
Add Windows 7 to version checking
Jordan Miner <jminer7@gmail.com>
parents:
12
diff
changeset
|
44 WindowsMe, /// |
ef81af74a306
Add Windows 7 to version checking
Jordan Miner <jminer7@gmail.com>
parents:
12
diff
changeset
|
45 Windows2000, /// |
ef81af74a306
Add Windows 7 to version checking
Jordan Miner <jminer7@gmail.com>
parents:
12
diff
changeset
|
46 WindowsXP, /// |
ef81af74a306
Add Windows 7 to version checking
Jordan Miner <jminer7@gmail.com>
parents:
12
diff
changeset
|
47 WindowsVista,/// |
ef81af74a306
Add Windows 7 to version checking
Jordan Miner <jminer7@gmail.com>
parents:
12
diff
changeset
|
48 Windows7 |
0 | 49 } |
50 /** | |
51 * Returns true if the version of Windows that is runninng now is the | |
52 * specified version or newer. | |
53 */ | |
54 bool checkWindowsVersion(WindowsVersion ver) { | |
55 // Windows Server "Longhorn" is 6.0 | |
56 // Windows Vista is 6.0 | |
57 // Windows Server 2003 is 5.2 | |
58 // Windows XP is 5.1 | |
59 // Windows Me is 4.90 | |
60 // Windows 98 is 4.10 | |
61 // Windows 95 is 4.0 | |
62 // Windows NT is 4.0 | |
63 OSVERSIONINFO info; | |
64 info.dwOSVersionInfoSize = OSVERSIONINFO.sizeof; | |
65 GetVersionEx(&info); | |
66 DWORD major, minor; | |
67 switch(ver) { | |
68 case WindowsVersion.Windows95: major = 4; minor = 0; break; | |
69 case WindowsVersion.Windows98: major = 4; minor = 10; break; | |
70 case WindowsVersion.WindowsMe: major = 4; minor = 90; break; | |
71 case WindowsVersion.Windows2000: major = 5; minor = 0; break; | |
72 case WindowsVersion.WindowsXP: major = 5; minor = 1; break; | |
73 case WindowsVersion.WindowsVista: major = 6; minor = 0; break; | |
17
ef81af74a306
Add Windows 7 to version checking
Jordan Miner <jminer7@gmail.com>
parents:
12
diff
changeset
|
74 case WindowsVersion.Windows7: major = 6; minor = 1; break; |
0 | 75 } |
76 return info.dwMajorVersion > major || | |
77 (info.dwMajorVersion == major && info.dwMinorVersion >= minor); | |
78 } | |
79 /* unittest { | |
80 Stdout.format("Windows95 or newer: {}", checkWindowsVersion(WindowsVersion.Windows95)).newline; | |
81 Stdout.format("Windows98 or newer: {}", checkWindowsVersion(WindowsVersion.Windows98)).newline; | |
82 Stdout.format("WindowsMe or newer: {}", checkWindowsVersion(WindowsVersion.WindowsMe)).newline; | |
83 Stdout.format("Windows2000 or newer: {}", checkWindowsVersion(WindowsVersion.Windows2000)).newline; | |
84 Stdout.format("WindowsXP or newer: {}", checkWindowsVersion(WindowsVersion.WindowsXP)).newline; | |
85 } */ | |
86 | |
87 // TODO: the way I have stored references using SetProp() will not work | |
88 // if/when D gets a copying collector. I will need to store a key with | |
89 // SetProp() and use that key to store the reference in either an | |
90 // array or a hashtable. | |
91 Window[HWND] windows; | |
92 void setControl(HWND hwnd, Window win) { | |
93 if(win is null) | |
94 windows.remove(hwnd); | |
95 else | |
96 windows[hwnd] = win; | |
97 } | |
98 /** | |
99 * Returns: the Dynamin NativeControl that wraps the specified handle | |
100 */ | |
101 // TODO: change return type to NativeControl | |
102 Window getControl(HWND hwnd) { | |
103 assert(IsWindow(hwnd), "Invalid HWND"); | |
104 auto tmp = hwnd in windows; | |
105 return tmp is null ? null : *tmp; | |
106 } | |
107 | |
108 template ApplicationBackend() { | |
109 void backend_run(Window w) { | |
110 bool isWindowVisible() { | |
111 if(w is null) return true; | |
112 return w.visible; | |
113 } | |
114 MSG msg; | |
115 BOOL ret; | |
116 while(isWindowVisible() && (ret = GetMessage(&msg, null, 0, 0)) != 0) { | |
117 if(ret == -1) | |
118 Stdout("GetMessage() failed!").newline; | |
119 TranslateMessage(&msg); | |
120 DispatchMessage(&msg); | |
121 } | |
122 } | |
12
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
123 void backend_invoke(void delegate() dg) { |
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
124 PostMessage(msgWnd, WM_USER + 7, |
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
125 cast(word)dg.ptr, cast(word)dg.funcptr); |
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
126 } |
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
127 void backend_invokeNow(void delegate() dg) { |
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
128 SendMessage(msgWnd, WM_USER + 7, |
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
129 cast(word)dg.ptr, cast(word)dg.funcptr); |
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
130 } |
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
131 |
0 | 132 } |
133 /* | |
134 * The reason backends use the backend_ prefix and: | |
135 * mixin Backend(); | |
136 * instead of using just the method name and | |
137 * mixin Backend() backend; | |
138 * is that D mistakenly calls the method in the class, rather than | |
139 * the one mixed-in...causing infinite recursion/stack overflow | |
140 */ | |
141 //{{{ WindowBackend | |
142 template WindowBackend() { | |
143 HWND _handle; | |
144 bool backend_handleCreated() { return _handle !is null; } | |
145 //WS_CAPTION == WS_BORDER | WS_DLGFRAME; | |
146 void backend_recreateHandle() { | |
147 LONG style, exStyle; | |
148 backend_getWindowStyles(style, exStyle); | |
149 style &= ~WS_VISIBLE; // don't create visible | |
150 // TODO: set the owner with CreateWindowEx | |
151 HWND newHandle = CreateWindowEx(exStyle, "DynaminWindow", _text.toWcharPtr(), | |
152 style, cast(int)x, cast(int)y, cast(int)width, cast(int)height, | |
153 null, null, GetModuleHandle(null), null); | |
154 if(!newHandle) | |
155 Stdout("CreateWindowEx() failed").newline; | |
156 setControl(newHandle, this); | |
157 | |
158 // Windows does not completely obey the styles given in CreateWindowEx() | |
159 SetWindowLong(newHandle, GWL_STYLE, style); | |
160 SetWindowLong(newHandle, GWL_EXSTYLE, exStyle); | |
161 SetWindowPos(newHandle, null, 0, 0, 0, 0, | |
162 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); | |
163 | |
164 if(handleCreated) { | |
165 // TODO: move native children to new window? | |
166 | |
167 // set z-order to right above old window | |
168 // SetWindowPos() puts the window above the specified | |
169 // window in the z-order | |
170 SetWindowPos(newHandle, _handle, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); | |
171 if(IsWindowVisible(_handle)) | |
172 ShowWindow(newHandle, SW_SHOWNA); | |
173 DestroyWindow(_handle); | |
174 } | |
175 _handle = newHandle; | |
176 backend_nativeToBorderSize(); | |
177 } | |
178 extern(C) static void freeDC(void* hdc) { | |
179 ReleaseDC(null, hdc); | |
180 } | |
181 Graphics backend_quickCreateGraphics() { | |
182 HDC hdc = GetDC(handle); | |
183 cairo_surface_t* surface = cairo_win32_surface_create(hdc); | |
184 cairo_surface_set_user_data(surface, cast(cairo_user_data_key_t*)1, | |
185 hdc, &freeDC); | |
186 cairo_t* cr = cairo_create(surface); | |
187 cairo_surface_destroy(surface); | |
188 cairo_translate(cr, -borderSize.left, -borderSize.top); | |
189 auto g = new Graphics(cr); | |
190 cairo_destroy(cr); | |
191 return g; | |
192 } | |
193 void backend_visible(bool b) { | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
194 if(b) |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
195 // visible has been set to true by now...use state() to show window |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
196 backend_state = _state; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
197 else |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
198 //if not created, create the handle by calling handle() |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
199 ShowWindow(handle, SW_HIDE); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
200 } |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
201 void backend_state(WindowState s) { |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
202 if(!visible) |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
203 return; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
204 //if not created, create the handle by calling handle() |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
205 if(s == WindowState.Normal) |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
206 ShowWindow(handle, SW_RESTORE); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
207 else if(s == WindowState.Minimized) |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
208 ShowWindow(handle, SW_MINIMIZE); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
209 else if(s == WindowState.Maximized) |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
210 ShowWindow(handle, SW_MAXIMIZE); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
211 } |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
212 void backend_activate() { |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
213 SetForegroundWindow(_handle); |
0 | 214 } |
215 void backend_borderStyle(WindowBorderStyle border) { | |
216 backend_updateWindowStyles(); | |
217 } | |
23
d55b5b998412
Implement built-in mouse cursors with X.
Jordan Miner <jminer7@gmail.com>
parents:
17
diff
changeset
|
218 void backend_setCurrentCursor(Cursor cur) { |
d55b5b998412
Implement built-in mouse cursors with X.
Jordan Miner <jminer7@gmail.com>
parents:
17
diff
changeset
|
219 SetCursor(cur.handle); |
d55b5b998412
Implement built-in mouse cursors with X.
Jordan Miner <jminer7@gmail.com>
parents:
17
diff
changeset
|
220 } |
0 | 221 void backend_repaint(Rect rect) { |
222 RECT wrect; | |
223 wrect.left = cast(int)(rect.x-_borderSize.left); | |
224 wrect.top = cast(int)(rect.y-_borderSize.top); | |
225 wrect.right = wrect.left+cast(int)rect.width; | |
226 wrect.bottom = wrect.top+cast(int)rect.height; | |
227 InvalidateRect(handle, &wrect, false); | |
228 } | |
229 void backend_resizable(bool b) { | |
230 backend_updateWindowStyles(); | |
231 } | |
232 void backend_contentMinSizeChanged() { | |
233 } | |
234 void backend_contentMaxSizeChanged() { | |
235 backend_updateWindowStyles(); | |
236 } | |
237 void backend_location(Point pt) { | |
238 SetWindowPos(handle, null, | |
239 cast(int)pt.x, cast(int)pt.y, 0, 0, | |
240 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER); | |
241 } | |
242 void backend_size(Size size) { | |
243 SetWindowPos(handle, null, | |
244 0, 0, cast(int)size.width, cast(int)size.height, | |
245 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER); | |
246 } | |
247 void backend_text(string str) { | |
248 SetWindowText(handle, str.toWcharPtr()); | |
249 } | |
250 //{{{ backend specific | |
251 | |
252 void backend_nativeToLocationSize() { | |
253 RECT winRect; | |
254 GetWindowRect(handle, &winRect); | |
255 _location.x = winRect.left; | |
256 _location.y = winRect.top; | |
257 _size.width = winRect.right-winRect.left; | |
258 _size.height = winRect.bottom-winRect.top; | |
259 } | |
260 package void backend_nativeToBorderSize() { | |
261 RECT clientRect, winRect; | |
262 POINT clientLoc; | |
263 GetClientRect(handle, &clientRect); | |
264 GetWindowRect(handle, &winRect); | |
265 ClientToScreen(handle, &clientLoc); | |
266 _borderSize.left = clientLoc.x-winRect.left; | |
267 _borderSize.top = clientLoc.y-winRect.top; | |
268 _borderSize.right = winRect.right-clientLoc.x-clientRect.right; | |
269 _borderSize.bottom = winRect.bottom-clientLoc.y-clientRect.bottom; | |
270 backend_nativeToLocationSize(); | |
271 } | |
272 void backend_getWindowStyles(out LONG style, out LONG exStyle) { | |
273 if(handleCreated) { | |
274 style = GetWindowLong(handle, GWL_STYLE); | |
275 exStyle = GetWindowLong(handle, GWL_EXSTYLE); | |
276 } | |
277 void SetIf(LONG s, bool b) { | |
278 // if condition satisfied, add style, otherwise clear style | |
279 b ? (style |= s) : (style &= ~s); | |
280 } | |
281 SetIf(WS_DLGFRAME, borderStyle != WindowBorderStyle.None); | |
282 SetIf(WS_BORDER, borderStyle != WindowBorderStyle.None); | |
283 SetIf(WS_THICKFRAME, | |
284 resizable && borderStyle != WindowBorderStyle.None); | |
285 SetIf(WS_MINIMIZEBOX, borderStyle == WindowBorderStyle.Normal); | |
286 SetIf(WS_MAXIMIZEBOX, | |
287 borderStyle == WindowBorderStyle.Normal && resizable && | |
288 content.maxWidth == 0 && content.maxHeight == 0); | |
289 SetIf(WS_SYSMENU, borderStyle != WindowBorderStyle.None); | |
290 if(borderStyle == WindowBorderStyle.Tool) | |
291 exStyle |= WS_EX_TOOLWINDOW; | |
292 else | |
293 exStyle &= ~WS_EX_TOOLWINDOW; | |
294 } | |
295 void backend_updateWindowStyles() { | |
296 if(!handleCreated) | |
297 return; | |
298 LONG style, exStyle; | |
299 backend_getWindowStyles(style, exStyle); | |
300 SetWindowLong(handle, GWL_STYLE, style); | |
301 SetWindowLong(handle, GWL_EXSTYLE, exStyle); | |
302 SetWindowPos(handle, null, 0, 0, 0, 0, | |
303 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); | |
304 } | |
305 //--FixedToorWindow-- | |
306 //Style: 0x16c80000 | |
307 //ExStyle: 0x10180 | |
308 //--SizableToorWindow-- | |
309 //Style: 0x16cc0000 | |
310 //ExStyle: 0x10180 | |
311 //--FixedDialog-- | |
312 //Style: 0x16c80000 | |
313 //ExStyle: 0x10101 | |
314 //--None-- | |
315 //Style: 0x16010000 | |
316 //ExStyle: 0x10000 | |
317 //--FixedSingle-- | |
318 //Style: 0x16c80000 | |
319 //ExStyle: 0x10100 | |
320 //--Sizable-- | |
321 //Style: 0x16cc0000 | |
322 //ExStyle: 0x10100 | |
323 //}}} | |
324 } | |
325 //}}} | |
326 | |
327 //{{{ Ux class | |
328 class Ux { | |
329 static: | |
330 private: | |
331 HMODULE uxLib = null; | |
332 // TODO: these are the wrong calling convention!! | |
333 extern(Windows) { | |
334 BOOL function() _IsAppThemed; | |
335 BOOL function() _IsThemeActive; | |
336 HTHEME function(HWND hwnd, LPCWSTR pszClassList) _OpenThemeData; | |
337 HRESULT function(HTHEME hTheme) _CloseThemeData; | |
338 HRESULT function(HTHEME hTheme, HDC hdc, int iPartId, int iStateId, | |
339 RECT* pRect, RECT* pClipRect) _DrawThemeBackground; | |
340 } | |
341 static this() { | |
342 uxLib = LoadLibrary("uxtheme"); | |
343 if(uxLib) { | |
344 _IsAppThemed = cast(typeof(_IsAppThemed)) | |
345 GetProcAddress(uxLib, "IsAppThemed"); | |
346 _IsThemeActive = cast(typeof(_IsThemeActive)) | |
347 GetProcAddress(uxLib, "IsThemeActive"); | |
348 _OpenThemeData = cast(typeof(_OpenThemeData)) | |
349 GetProcAddress(uxLib, "OpenThemeData"); | |
350 _CloseThemeData = cast(typeof(_CloseThemeData)) | |
351 GetProcAddress(uxLib, "CloseThemeData"); | |
352 _DrawThemeBackground = cast(typeof(_DrawThemeBackground)) | |
353 GetProcAddress(uxLib, "DrawThemeBackground"); | |
354 } | |
355 } | |
356 HTHEME[string] cache; | |
357 // opens an HTHEME for the specified controlName and caches it | |
358 // next time, just returns the HTHEME from the cache | |
359 HTHEME getHTHEME(string controlName) { | |
360 HTHEME* hthemePtr = controlName in cache; | |
361 HTHEME htheme = controlName in cache; | |
362 if(hthemePtr) { | |
363 htheme = *hthemePtr; | |
364 } else { | |
365 htheme = _OpenThemeData(null, controlName.toWcharPtr()); | |
366 if(!htheme) { | |
367 if(_IsThemeActive()) | |
368 throw new Exception("invalid uxtheme controlName"); | |
369 else | |
370 throw new Exception("no theme active"); | |
371 } | |
372 cache[controlName] = htheme; | |
373 } | |
374 return htheme; | |
375 } | |
376 // This is called when the WM_THEMECHANGED message is sent | |
377 package void themeChanged() { | |
378 foreach(htheme; cache.values) | |
379 _CloseThemeData(htheme); | |
380 cache = null; | |
381 updateThemeActive = true; | |
382 } | |
383 bool themeActive; | |
384 bool updateThemeActive = true; | |
385 public: | |
386 // cache this value, as this function was showing up in profiles | |
387 bool isThemeActive() { | |
388 if(updateThemeActive) { | |
389 themeActive = uxLib && _IsThemeActive() && _IsAppThemed(); | |
390 updateThemeActive = false; | |
391 } | |
392 return themeActive; | |
393 } | |
394 // draw directly onto the HDC with the uxTheme API if the following | |
395 // three conditions are met: | |
396 // - the Graphics must be drawing to an HDC (duh) | |
397 // - there cannot be a scale or rotation...translation can be handled | |
398 // - the clip must be a pixel-aligned rectangle | |
399 bool drawBackground(Graphics g, Rect rect, string controlName, int part, int state) { | |
400 if(!uxLib) | |
401 throw new Exception("UxPaintBackground(): uxtheme library not found!"); | |
402 HTHEME htheme = getHTHEME(controlName); | |
403 static if(true) { | |
404 | |
405 HDC hdc = cairo_win32_surface_get_dc(cairo_get_target(g.handle)); | |
406 //HDC hdc = null; | |
407 bool isMatrixTranslationOnly() { | |
408 cairo_matrix_t matrix; | |
409 cairo_get_matrix(g.handle, &matrix); | |
410 return matrix.xx == 1 && matrix.xy == 0 && | |
411 matrix.xy == 0 && matrix.yy == 1; | |
412 } | |
413 bool isClipIntegerRect(cairo_rectangle_list_t* list) { | |
414 return list.status != CAIRO_STATUS_CLIP_NOT_REPRESENTABLE; | |
415 } | |
416 RECT locRect; | |
417 auto list = cairo_copy_clip_rectangle_list(g.handle); | |
418 scope(exit) cairo_rectangle_list_destroy(list); | |
419 if(hdc && isMatrixTranslationOnly() && isClipIntegerRect(list)) { | |
420 double x = rect.x, y = rect.y; | |
421 double right = rect.right, bottom = rect.bottom; | |
422 cairo_user_to_device(g.handle, &x, &y); | |
423 cairo_user_to_device(g.handle, &right, &bottom); | |
424 locRect.left = cast(int)x; | |
425 locRect.top = cast(int)y; | |
426 locRect.right = cast(int)right; | |
427 locRect.bottom = cast(int)bottom; | |
428 cairo_surface_flush(cairo_get_target(g.handle)); | |
429 | |
430 RECT clip; | |
431 for(int i = 0; i < list.num_rectangles; ++i) { | |
432 cairo_user_to_device(g.handle, &(list.rectangles[i].x), &(list.rectangles[i].y)); | |
433 clip.left = cast(int)list.rectangles[i].x; | |
434 clip.top = cast(int)list.rectangles[i].y; | |
435 clip.right = clip.left + cast(int)list.rectangles[i].width; | |
436 clip.bottom = clip.top + cast(int)list.rectangles[i].height; | |
437 _DrawThemeBackground(htheme, hdc, part, state, &locRect, &clip); | |
438 } | |
439 | |
440 cairo_surface_mark_dirty(cairo_get_target(g.handle)); | |
441 } else { | |
442 assert(0, "So far, visual styles are supported only with no rotation, no scaling, a rectangular clip, and HDC surfaces."); | |
443 static if(0) { | |
444 BITMAPINFO bmi; | |
445 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); | |
446 bmi.bmiHeader.biWidth = width; | |
447 bmi.bmiHeader.biHeight = -height; // top-down DIB | |
448 bmi.bmiHeader.biPlanes = 1; | |
449 bmi.bmiHeader.biBitCount = 32; | |
450 bmi.bmiHeader.biCompression = BI_RGB; | |
451 //bmi.bmiHeader.biSizeImage = width * height * 4; | |
452 hbmpBlack = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, | |
453 &bmpBits, NULL, 0); | |
454 hbmpWhite = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, | |
455 &bmpBits, NULL, 0); | |
456 //draw on a black background and on a white background | |
457 //calculate alpha and colors and draw that image with cairo | |
458 // DO TESTS WITH ROTATION AND SCALING | |
459 _DrawThemeBackground(htheme, hdc, part, state, null/*change*/, null); | |
460 DeleteObject(hbmpBlack); | |
461 DeleteObject(hbmpWhite); | |
462 } | |
463 } | |
464 | |
465 } | |
466 return false; | |
467 } | |
468 } | |
469 //}}} | |
470 | |
471 //{{{ module constructor and destructor | |
472 HWND msgWnd; | |
473 static this() { | |
474 assert(cairo_version() >= CAIRO_VERSION_ENCODE(1, 4, 0), | |
475 "cairo version 1.4.0 or newer is required"); | |
476 | |
477 /* create window classes */ | |
478 WNDCLASSEX wc; | |
479 wc.cbSize = wc.sizeof; | |
480 wc.style = 0; | |
481 wc.hInstance = GetModuleHandle(null); | |
482 | |
483 wc.lpfnWndProc = &dynaminMsgWindowProc; | |
484 wc.lpszClassName = "DynaminMsgWindow"; | |
485 if(!RegisterClassExW(&wc)) | |
486 Stdout("RegisterClassEx() failed registering class 'DynaminMsgWindow'").newline; | |
487 | |
488 wc.lpfnWndProc = &dynaminWindowProc; | |
489 //wc.hbrBackground = cast(HBRUSH)16; | |
490 | |
491 wc.lpszClassName = "DynaminWindow"; | |
492 if(!RegisterClassEx(&wc)) | |
493 Stdout("RegisterClassEx() failed registering class 'DynaminWindow'").newline; | |
494 | |
495 wc.style = CS_SAVEBITS; | |
496 wc.lpszClassName = "DynaminPopup"; | |
497 if(!RegisterClassExW(&wc)) | |
498 Stdout("RegisterClassEx() failed registering class 'DynaminPopup'").newline; | |
499 | |
500 | |
501 msgWnd = CreateWindowEx(0, "DynaminMsgWindow", "", | |
502 0, | |
503 0, 0, 0, 0, | |
504 null, null, GetModuleHandle(null), null); | |
505 if(!msgWnd) | |
506 Stdout("CreateWindowEx() failed").newline; | |
507 | |
508 /* initialize COM */ | |
509 auto ret = CoInitializeEx(null, COINIT_APARTMENTTHREADED); | |
510 if(ret != S_OK && ret != S_FALSE) | |
511 Stdout("Failed to initialize COM").newline; | |
512 | |
513 } | |
514 static ~this() { | |
515 CoUninitialize(); | |
516 } | |
517 //}}} | |
518 | |
519 //{{{ dynaminWindowProc() | |
520 extern(Windows) | |
521 LRESULT dynaminWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { | |
522 //used in WM_MOVING | |
523 static int dragX, dragY; //the cursor location in window coordinates when the drag was started | |
524 static bool trackingMouseLeave = false; | |
525 auto c = getControl(hwnd); | |
526 //{{{ helper functions | |
527 void createMouseEvent(MouseButton button, void delegate(MouseEventArgs args) func) { | |
528 scope args = new MouseEventArgs( | |
529 cast(short)LOWORD(lParam)+c.borderSize.left, | |
530 cast(short)HIWORD(lParam)+c.borderSize.top, button); | |
531 func(args); | |
532 } | |
533 void snapSide(inout int sideToSnap, float side1, float side2) { | |
534 if(sideToSnap >= side1-c.snapDistance && sideToSnap <= side1+c.snapDistance) | |
535 sideToSnap = cast(int)side1; | |
536 if(sideToSnap >= side2-c.snapDistance && sideToSnap <= side2+c.snapDistance) | |
537 sideToSnap = cast(int)side2; | |
538 } | |
539 auto emptyFunc = (Rect snapRect) {}; | |
540 // used to snap vertical sides, left and right | |
541 void snapVSide(inout int side, RECT* rect, void delegate(Rect snapRect) func = emptyFunc) { | |
542 if(c.snapRects is null) | |
543 return; | |
544 foreach(snapRect; c.snapRects) { | |
545 if(rect.bottom >= snapRect.y && rect.top <= snapRect.bottom) { | |
546 snapSide(side, snapRect.x, snapRect.right); | |
547 func(snapRect); | |
548 } | |
549 } | |
550 } | |
551 // used to snap horizontal sides, top and bottom | |
552 void snapHSide(inout int side, RECT* rect, void delegate(Rect snapRect) func = emptyFunc) { | |
553 if(c.snapRects is null) | |
554 return; | |
555 foreach(snapRect; c.snapRects) { | |
556 if(rect.right >= snapRect.x && rect.left <= snapRect.right) { | |
557 snapSide(side, snapRect.y, snapRect.bottom); | |
558 func(snapRect); | |
559 } | |
560 } | |
561 } | |
562 MouseButton getFromXBUTTONX() { | |
563 switch(HIWORD(wParam)) { | |
564 case XBUTTON1: return MouseButton.XButton1; | |
565 case XBUTTON2: return MouseButton.XButton2; | |
566 default: return MouseButton.None; | |
567 } | |
568 } | |
72
8dac206ea523
Add shift/control/altDown to KeyEventArgs.
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
569 bool isKeyDown(int vk) { |
8dac206ea523
Add shift/control/altDown to KeyEventArgs.
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
570 return cast(bool)HIWORD(GetKeyState(vk)); |
8dac206ea523
Add shift/control/altDown to KeyEventArgs.
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
571 } |
0 | 572 //}}} |
573 switch(uMsg) { | |
574 case WM_ENTERSIZEMOVE: //when the user starts moving or resizing the window | |
575 //{{{ | |
576 POINT cursor; | |
577 GetCursorPos(&cursor); | |
578 RECT rect; | |
579 GetWindowRect(hwnd, &rect); | |
580 dragX = cursor.x-rect.left; | |
581 dragY = cursor.y-rect.top; | |
582 return 0; | |
583 //}}} | |
584 case WM_MOVING: | |
585 //{{{ | |
586 if(c.snapRects is null || c.snapRects.length == 0) | |
587 break; | |
588 RECT* rect = cast(RECT*)lParam; | |
589 int rectWidth = rect.right-rect.left; | |
590 int rectHeight = rect.bottom-rect.top; | |
591 POINT cursor; | |
592 GetCursorPos(&cursor); | |
593 rect.left = cursor.x-dragX; | |
594 rect.top = cursor.y-dragY; | |
595 void updateRightAndBottom() { | |
596 rect.right = rect.left+rectWidth; | |
597 rect.bottom = rect.top+rectHeight; | |
598 } | |
599 updateRightAndBottom(); | |
600 snapVSide(rect.left, rect, (Rect snapRect) { | |
601 snapSide(rect.left, snapRect.x-rectWidth, snapRect.right-rectWidth); | |
602 }); | |
603 updateRightAndBottom(); | |
604 snapHSide(rect.top, rect, (Rect snapRect) { | |
605 snapSide(rect.top, snapRect.y-rectHeight, snapRect.bottom-rectHeight); | |
606 }); | |
607 updateRightAndBottom(); | |
608 snapVSide(rect.left, rect, (Rect snapRect) { | |
609 snapSide(rect.left, snapRect.x-rectWidth, snapRect.right-rectWidth); | |
610 }); | |
611 updateRightAndBottom(); | |
612 return true; | |
613 //}}} | |
614 case WM_SIZING: | |
615 //{{{ | |
616 Size minSize = c.content.minSize+c.borderSize; | |
617 Size maxSize = c.content.maxSize+c.borderSize; | |
618 RECT* rect = cast(RECT*)lParam; | |
619 switch(wParam) { | |
620 case WMSZ_TOPLEFT: | |
621 snapHSide(rect.top, rect); | |
622 goto case WMSZ_LEFT; | |
623 case WMSZ_BOTTOMLEFT: | |
624 snapHSide(rect.bottom, rect); | |
625 case WMSZ_LEFT: | |
626 snapVSide(rect.left, rect); | |
627 // adjust left according to min and max | |
628 if(c.content.maxWidth != 0) | |
629 rect.left = max(rect.left, rect.right-cast(int)maxSize.width); | |
630 if(c.content.minWidth != 0) | |
631 rect.left = min(rect.left, rect.right-cast(int)minSize.width); | |
632 break; | |
633 case WMSZ_TOPRIGHT: | |
634 snapHSide(rect.top, rect); | |
635 goto case WMSZ_RIGHT; | |
636 case WMSZ_BOTTOMRIGHT: | |
637 snapHSide(rect.bottom, rect); | |
638 case WMSZ_RIGHT: | |
639 snapVSide(rect.right, rect); | |
640 // adjust right according to min and max | |
641 if(c.content.maxWidth != 0) | |
642 rect.right = min(rect.right, rect.left+cast(int)maxSize.width); | |
643 if(c.content.minWidth != 0) | |
644 rect.right = max(rect.right, rect.left+cast(int)minSize.width); | |
645 break; | |
646 default: break; | |
647 } | |
648 switch(wParam) { | |
649 case WMSZ_TOPLEFT: //already snapped left above | |
650 case WMSZ_TOPRIGHT: //already snapped right above | |
651 case WMSZ_TOP: | |
652 snapHSide(rect.top, rect); | |
653 // adjust top according to min and max | |
654 if(c.content.maxHeight != 0) | |
655 rect.top = max(rect.top, rect.bottom-cast(int)maxSize.height); | |
656 if(c.content.minHeight != 0) | |
657 rect.top = min(rect.top, rect.bottom-cast(int)minSize.height); | |
658 break; | |
659 case WMSZ_BOTTOMLEFT: //already snapped left above | |
660 case WMSZ_BOTTOMRIGHT: //already snapped right above | |
661 case WMSZ_BOTTOM: | |
662 snapHSide(rect.bottom, rect); | |
663 // adjust bottom according to min and max | |
664 if(c.content.maxHeight != 0) | |
665 rect.bottom = min(rect.bottom, rect.top+cast(int)maxSize.height); | |
666 if(c.content.minHeight != 0) | |
667 rect.bottom = max(rect.bottom, rect.top+cast(int)minSize.height); | |
668 break; | |
669 default: break; | |
670 } | |
671 return true; | |
672 //}}} | |
673 case WM_MOVE: | |
674 RECT rect; | |
675 GetWindowRect(hwnd, &rect); | |
676 c._location = Point(rect.left, rect.top); | |
677 scope args = new EventArgs(); | |
678 c.moved(args); | |
679 return 0; | |
680 case WM_SIZE: | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
681 if(wParam == SIZE_RESTORED) |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
682 c._state = WindowState.Normal; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
683 else if(wParam == SIZE_MINIMIZED) { |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
684 c._state = WindowState.Minimized; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
685 break; // don't update size if minimized (would be wierd size) |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
686 } else if(wParam == SIZE_MAXIMIZED) |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
687 c._state = WindowState.Maximized; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
688 |
0 | 689 RECT rect; |
690 GetWindowRect(hwnd, &rect); | |
691 c._size = Size(rect.right-rect.left, rect.bottom-rect.top); | |
692 c.backend_nativeToBorderSize(); | |
693 scope args = new EventArgs(); | |
694 c.resized(args); | |
695 return 0; | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
696 case WM_ACTIVATE: |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
697 scope e = new EventArgs; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
698 if(LOWORD(wParam) == WA_ACTIVE || LOWORD(wParam) == WA_CLICKACTIVE) { |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
699 c._active = true; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
700 c.activated(e); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
701 } else if(LOWORD(wParam) == WA_INACTIVE) { |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
702 c._active = false; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
703 c.deactivated(e); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
704 } |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
31
diff
changeset
|
705 return 0; |
0 | 706 case WM_MOUSEMOVE: |
707 if(!trackingMouseLeave) { | |
708 TRACKMOUSEEVENT tme; | |
709 tme.cbSize = TRACKMOUSEEVENT.sizeof; | |
710 tme.dwFlags = TME_LEAVE; | |
711 tme.hWndTrack = hwnd; | |
712 tme.dwHoverTime = 0; | |
713 TrackMouseEvent(&tme); | |
714 trackingMouseLeave = true; | |
715 } | |
716 auto pt = Point(cast(short)LOWORD(lParam)+c.borderSize.left, cast(short)HIWORD(lParam)+c.borderSize.top); | |
717 Control captor = getCaptorControl(); | |
718 if(captor) | |
719 pt = c.contentToContent(pt, captor); | |
720 else | |
721 captor = c; | |
28
00efca2c1545
Rename variable to be consistent.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
722 scope args = new MouseEventArgs( |
0 | 723 pt.x, pt.y, MouseButton.None); |
724 if(wParam & | |
725 (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON | | |
726 MK_XBUTTON1 | MK_XBUTTON2)) { | |
28
00efca2c1545
Rename variable to be consistent.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
727 captor.mouseDragged(args); |
0 | 728 } else { |
28
00efca2c1545
Rename variable to be consistent.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
729 captor.mouseMoved(args); |
0 | 730 } |
731 return 0; | |
732 case WM_MOUSELEAVE: | |
733 trackingMouseLeave = false; | |
734 setHotControl(null); | |
735 return 0; | |
736 case WM_LBUTTONDOWN: | |
737 SetCapture(hwnd); | |
738 createMouseEvent(MouseButton.Left, (MouseEventArgs args) { | |
739 c.mouseDown(args); | |
740 }); | |
741 return 0; | |
742 case WM_MBUTTONDOWN: | |
743 SetCapture(hwnd); | |
744 createMouseEvent(MouseButton.Middle, (MouseEventArgs args) { | |
745 c.mouseDown(args); | |
746 }); | |
747 return 0; | |
748 case WM_RBUTTONDOWN: | |
749 SetCapture(hwnd); | |
750 createMouseEvent(MouseButton.Right, (MouseEventArgs args) { | |
751 c.mouseDown(args); | |
752 }); | |
753 return 0; | |
754 case WM_XBUTTONDOWN: | |
755 SetCapture(hwnd); | |
756 auto button = getFromXBUTTONX(); | |
757 if(!button) break; | |
758 createMouseEvent(button, (MouseEventArgs args) { | |
759 c.mouseDown(args); | |
760 }); | |
761 return true; | |
762 case WM_LBUTTONUP: | |
763 ReleaseCapture(); | |
764 createMouseEvent(MouseButton.Left, (MouseEventArgs args) { | |
765 c.mouseUp(args); | |
766 }); | |
767 return 0; | |
768 case WM_MBUTTONUP: | |
769 ReleaseCapture(); | |
770 createMouseEvent(MouseButton.Middle, (MouseEventArgs args) { | |
771 c.mouseUp(args); | |
772 }); | |
773 return 0; | |
774 case WM_RBUTTONUP: | |
775 ReleaseCapture(); | |
776 createMouseEvent(MouseButton.Right, (MouseEventArgs args) { | |
777 c.mouseUp(args); | |
778 }); | |
779 return 0; | |
780 case WM_XBUTTONUP: | |
781 ReleaseCapture(); | |
782 auto button = getFromXBUTTONX(); | |
783 if(!button) break; | |
784 createMouseEvent(button, (MouseEventArgs args) { | |
785 c.mouseUp(args); | |
786 }); | |
787 return true; | |
788 case WM_MOUSEWHEEL: | |
789 int scrollLines; | |
790 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &scrollLines, 0); | |
31
2a194d52fdb5
Implement MouseTurnedEventArgs.scrollScreen.
Jordan Miner <jminer7@gmail.com>
parents:
30
diff
changeset
|
791 bool sScreen = (scrollLines == 0xFFFFFFFF); |
2a194d52fdb5
Implement MouseTurnedEventArgs.scrollScreen.
Jordan Miner <jminer7@gmail.com>
parents:
30
diff
changeset
|
792 if(sScreen) |
0 | 793 scrollLines = 3; |
794 int delta = -cast(short)HIWORD(wParam); | |
795 auto screenPt = Point(LOWORD(lParam), HIWORD(lParam)); | |
796 auto des = c.getDescendantAtPoint(c.screenToContent(screenPt)); | |
31
2a194d52fdb5
Implement MouseTurnedEventArgs.scrollScreen.
Jordan Miner <jminer7@gmail.com>
parents:
30
diff
changeset
|
797 scope args = new MouseTurnedEventArgs(delta*scrollLines/120.0, sScreen); |
29
e6843df719a8
Use scope when creating EventArgs to avoid unnecessary heap usage.
Jordan Miner <jminer7@gmail.com>
parents:
28
diff
changeset
|
798 des.mouseTurned(args); |
0 | 799 return 0; |
800 case WM_SYSKEYDOWN: | |
801 //Stdout.format("WM_SYSKEYDOWN: {:x}", cast(int)wParam).newline; | |
802 if(wParam == 0x79) return 0; | |
803 break; | |
804 case WM_KEYDOWN: | |
805 //Stdout.format("WM_KEYDOWN: {:x}", cast(int)wParam).newline; | |
806 Control focused = c.focusedControl ? c.focusedControl : c; | |
72
8dac206ea523
Add shift/control/altDown to KeyEventArgs.
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
807 scope args = new KeyEventArgs(VKToKey(wParam), |
8dac206ea523
Add shift/control/altDown to KeyEventArgs.
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
808 cast(bool)(lParam & (1 << 30)), isKeyDown(VK_SHIFT), |
8dac206ea523
Add shift/control/altDown to KeyEventArgs.
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
809 isKeyDown(VK_CONTROL), isKeyDown(VK_MENU) ); |
29
e6843df719a8
Use scope when creating EventArgs to avoid unnecessary heap usage.
Jordan Miner <jminer7@gmail.com>
parents:
28
diff
changeset
|
810 focused.keyDown(args); |
0 | 811 return 0; |
812 case WM_SYSKEYUP: | |
813 //Stdout.format("WM_SYSKEYUP: {:x}", cast(int)wParam).newline; | |
814 if(wParam == 0x79) return 0; | |
815 break; | |
816 case WM_KEYUP: | |
817 //Stdout.format("WM_KEYUP: {:x}", cast(int)wParam).newline; | |
818 Control focused = c.focusedControl ? c.focusedControl : c; | |
72
8dac206ea523
Add shift/control/altDown to KeyEventArgs.
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
819 scope args = new KeyEventArgs( VKToKey(wParam), false, |
8dac206ea523
Add shift/control/altDown to KeyEventArgs.
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
820 isKeyDown(VK_SHIFT), isKeyDown(VK_CONTROL), isKeyDown(VK_MENU) ); |
29
e6843df719a8
Use scope when creating EventArgs to avoid unnecessary heap usage.
Jordan Miner <jminer7@gmail.com>
parents:
28
diff
changeset
|
821 focused.keyUp(args); |
0 | 822 return 0; |
823 case WM_CHAR: | |
824 // DO NOT use the repeat count from the lParam to send multiple events | |
825 // I hate when programs do that | |
826 | |
827 //stop backspace and escape and shift+enter | |
828 if(wParam == 0x08 || wParam == 0x1B || wParam == 0x0A) | |
829 break; | |
830 // don't process characters typed while control is down | |
831 if(HIBYTE(GetKeyState(VK_CONTROL))) | |
832 break; | |
833 if(wParam == 0x0D) // change \r to \n | |
834 wParam = 0x0A; | |
835 bool repeat; | |
836 repeat = cast(bool)(lParam & (1 << 30)); | |
837 Control focused = c.focusedControl ? c.focusedControl : c; | |
29
e6843df719a8
Use scope when creating EventArgs to avoid unnecessary heap usage.
Jordan Miner <jminer7@gmail.com>
parents:
28
diff
changeset
|
838 scope args = new KeyTypedEventArgs(cast(dchar)wParam, repeat); |
e6843df719a8
Use scope when creating EventArgs to avoid unnecessary heap usage.
Jordan Miner <jminer7@gmail.com>
parents:
28
diff
changeset
|
839 focused.keyTyped(args); |
0 | 840 return 0; |
841 case WM_PRINT: | |
842 paintToHDC(cast(HDC)wParam, getControl(hwnd), null); | |
843 return 0; | |
844 case WM_PAINT: | |
845 PAINTSTRUCT ps; | |
846 BeginPaint(hwnd, &ps); | |
847 RECT* clip = &ps.rcPaint; | |
848 | |
849 HDC hdcBuffer = CreateCompatibleDC(ps.hdc); | |
850 HBITMAP hbmpBuffer = CreateCompatibleBitmap(ps.hdc, | |
851 clip.right-clip.left, clip.bottom-clip.top); | |
852 HBITMAP hbmpDefault = SelectObject(hdcBuffer, hbmpBuffer); | |
853 | |
854 paintToHDC(hdcBuffer, getControl(hwnd), clip); | |
855 | |
856 BitBlt(ps.hdc, clip.left, clip.top, clip.right-clip.left, clip.bottom-clip.top, | |
857 hdcBuffer, 0, 0, SRCCOPY); | |
858 SelectObject(hdcBuffer, hbmpDefault); | |
859 DeleteDC(hdcBuffer); | |
860 DeleteObject(hbmpBuffer); | |
861 | |
862 EndPaint(hwnd, &ps); | |
863 return 0; | |
864 case WM_CLOSE: | |
865 c.visible = false; | |
866 //DestroyWindow(hwnd); | |
867 //PostQuitMessage(0); | |
868 return 0; | |
869 case WM_DESTROY: | |
870 return 0; | |
871 default: | |
872 break; | |
873 } | |
874 return DefWindowProc(hwnd, uMsg, wParam, lParam); | |
875 } | |
876 //}}} | |
877 | |
878 //{{{ paintToHDC() | |
879 void paintToHDC(HDC hdc, Window w, RECT* clip) { | |
880 cairo_surface_t* surface = cairo_win32_surface_create(hdc); | |
881 cairo_t* cr = cairo_create(surface); | |
882 cairo_surface_destroy(surface); | |
883 | |
884 if(clip) { | |
885 cairo_translate(cr, | |
886 -clip.left-w.borderSize.left, -clip.top-w.borderSize.top); | |
887 | |
888 //cairo_rectangle(cr, clip.left, clip.top, clip.right-clip.left, clip.bottom-clip.top); | |
889 } | |
890 //if(w.Opaque) { | |
891 //cairo_set_source_rgb(cr, w.content.backColor.R/255.0, w.content.backColor.G/255.0, w.content.backColor.B/255.0); | |
892 //cairo_paint(cr); | |
893 //} | |
894 | |
895 //cairo_set_source_rgb(cr, .3, .3, .3); | |
896 //cairo_paint(cr); | |
897 | |
898 //cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR); | |
899 //cairo_paint(cr); | |
900 //cairo_set_operator(cr, CAIRO_OPERATOR_OVER); | |
901 | |
902 cairo_set_source_rgb(cr, 0, 0, 0); | |
903 cairo_set_line_width(cr, 1.0); | |
904 | |
905 //HBITMAP hbmp = LoadImage(null, cast(wchar*)cast(ushort)OIC_WARNING, | |
906 // IMAGE_ICON, 0, 0, LR_SHARED); | |
907 //if(hbmp == null) | |
908 // Stdout.format("LoadImage failed. GetLastError()={}", GetLastError()).newline; | |
909 | |
910 //auto imgSurface = cairo_image_surface_create_for_data(); | |
911 //cairo_set_source_surface(cr, imgSurface, 50, 50); | |
912 //cairo_paint(cr); | |
913 //cairo_surface_destroy(imgSurface); | |
914 | |
915 auto g = new Graphics(cr); | |
916 scope args = new PaintingEventArgs(g); | |
917 w.painting(args); | |
918 delete g; | |
919 cairo_destroy(cr); | |
920 } | |
921 //}}} | |
922 | |
923 //{{{ VKToKey() | |
924 Key VKToKey(int code) { | |
925 switch(code) { | |
926 case VK_F1: return Key.F1; | |
927 case VK_F2: return Key.F2; | |
928 case VK_F3: return Key.F3; | |
929 case VK_F4: return Key.F4; | |
930 case VK_F5: return Key.F5; | |
931 case VK_F6: return Key.F6; | |
932 case VK_F7: return Key.F7; | |
933 case VK_F8: return Key.F8; | |
934 case VK_F9: return Key.F9; | |
935 case VK_F10: return Key.F10; | |
936 case VK_F11: return Key.F11; | |
937 case VK_F12: return Key.F12; | |
938 | |
939 case VK_ESCAPE: return Key.Escape; | |
940 case VK_TAB: return Key.Tab; | |
941 case VK_BACK: return Key.Backspace; | |
942 case VK_RETURN: return Key.Enter; | |
943 case VK_SPACE: return Key.Space; | |
944 | |
945 case VK_LEFT: return Key.Left; | |
946 case VK_RIGHT: return Key.Right; | |
947 case VK_UP: return Key.Up; | |
948 case VK_DOWN: return Key.Down; | |
949 | |
950 case VK_INSERT: return Key.Insert; | |
951 case VK_DELETE: return Key.Delete; | |
952 case VK_HOME: return Key.Home; | |
953 case VK_END: return Key.End; | |
954 case VK_PRIOR: return Key.PageUp; | |
955 case VK_NEXT: return Key.PageDown; | |
956 | |
957 case VK_SNAPSHOT: return Key.PrintScreen; | |
958 case VK_PAUSE: return Key.Pause; | |
959 | |
960 case VK_CAPITAL: return Key.CapsLock; | |
961 case VK_NUMLOCK: return Key.NumLock; | |
962 case VK_SCROLL: return Key.ScrollLock; | |
963 | |
964 case VK_NUMPAD0: return Key.NumPad0; | |
965 case VK_NUMPAD1: return Key.NumPad1; | |
966 case VK_NUMPAD2: return Key.NumPad2; | |
967 case VK_NUMPAD3: return Key.NumPad3; | |
968 case VK_NUMPAD4: return Key.NumPad4; | |
969 case VK_NUMPAD5: return Key.NumPad5; | |
970 case VK_NUMPAD6: return Key.NumPad6; | |
971 case VK_NUMPAD7: return Key.NumPad7; | |
972 case VK_NUMPAD8: return Key.NumPad8; | |
973 case VK_NUMPAD9: return Key.NumPad9; | |
974 case VK_DIVIDE: return Key.NumPadDivide; | |
975 case VK_MULTIPLY: return Key.NumPadMultiply; | |
976 case VK_SUBTRACT: return Key.NumPadSubtract; | |
977 case VK_ADD: return Key.NumPadAdd; | |
978 case VK_DECIMAL: return Key.NumPadDecimal; | |
979 | |
980 case VK_OEM_3: return Key.Backquote; | |
981 case VK_OEM_MINUS: return Key.Minus; | |
982 case VK_OEM_PLUS: return Key.Equals; | |
983 case VK_OEM_4: return Key.OpenBracket; | |
984 case VK_OEM_6: return Key.CloseBracket; | |
985 case VK_OEM_5: return Key.Backslash; | |
986 case VK_OEM_1: return Key.Semicolon; | |
987 case VK_OEM_7: return Key.Quote; | |
988 case VK_OEM_COMMA: return Key.Comma; | |
989 case VK_OEM_PERIOD: return Key.Period; | |
990 case VK_OEM_2: return Key.Slash; | |
991 | |
992 //case VK_APPS: return Key.Menu; | |
993 | |
994 case VK_SHIFT: return Key.Shift; | |
995 case VK_CONTROL: return Key.Control; | |
996 case VK_MENU: return Key.Alt; | |
997 | |
998 //case VK_: return Key.; | |
999 default: | |
1000 if(code >= 0x30 && code <= 0x39) // Key.D0 - Key.D9 | |
1001 return cast(Key)code; | |
1002 if(code >= 0x41 && code <= 0x5A) // Key.A - Key.Z | |
1003 return cast(Key)code; | |
1004 return cast(Key)0; | |
1005 } | |
1006 } | |
1007 //}}} | |
1008 | |
1009 // Use the msgWnd for the following: | |
1010 // Timers | |
1011 // Clipboard.DataChanged event | |
1012 // Clipboard? | |
1013 // Taskbar created event using RegisterWindowMessage("TaskbarCreated") | |
1014 // different settings changed events? | |
1015 // screen saver enabled? | |
1016 extern(Windows) | |
1017 LRESULT dynaminMsgWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { | |
1018 switch(uMsg) { | |
1019 case WM_THEMECHANGED: | |
1020 Ux.themeChanged(); | |
1021 return 0; | |
1022 case WM_POWERBROADCAST: | |
1023 if(wParam == PBT_APMRESUMESUSPEND || wParam == PBT_APMRESUMECRITICAL) | |
1024 Environment.backend_increaseTimerRes(); | |
1025 return 0; | |
12
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
1026 case WM_USER + 7: |
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
1027 void delegate() dg; |
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
1028 dg.ptr = cast(void*)wParam; |
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
1029 dg.funcptr = cast(void function())lParam; |
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
1030 dg(); |
7a7e5f9bd1ae
Implement invoke() and invokeNow() on Windows.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
1031 return 0; |
0 | 1032 case WM_TIMER: |
1033 case WM_CHANGECBCHAIN: | |
1034 case WM_DRAWCLIPBOARD: | |
1035 case WM_TIMECHANGE: //?? | |
1036 default: | |
1037 break; | |
1038 } | |
1039 return DefWindowProc(hwnd, uMsg, wParam, lParam); | |
1040 } | |
1041 |