authorgravatar for jens.goldberg@gmail.comAransentin <jens.goldberg@gmail.com> 2020-11-22 13:00:57+00:00
committergravatar for jens.goldberg@gmail.comAransentin <jens.goldberg@gmail.com> 2020-11-22 13:00:57+00:00
logfcf3f61c287eb07a5a9587c6fc3d4c7aa47def11
tree70bf747f478cd525611b356498db5c3defd776ad
parent2fbe9519acec0d7b9c9dcc41a877fec912337124

user32 cleanup, added wrappers and additional functions


3 files changed, 617 insertions(+), 105 deletions(-)

lib/std/os/windows/bits.zig+13
......@@ -37,6 +37,7 @@ pub const UCHAR = u8;
3737pub const FLOAT = f32;
3838pub const HANDLE = *c_void;
3939pub const HCRYPTPROV = ULONG_PTR;
40pub const ATOM = u16;
4041pub const HBRUSH = *opaque {};
4142pub const HCURSOR = *opaque {};
4243pub const HICON = *opaque {};
......@@ -770,6 +771,13 @@ pub const FILE_FLAG_SESSION_AWARE = 0x00800000;
770771pub const FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000;
771772pub const FILE_FLAG_WRITE_THROUGH = 0x80000000;
772773
774pub const RECT = extern struct {
775 left: LONG,
776 top: LONG,
777 right: LONG,
778 bottom: LONG,
779};
780
773781pub const SMALL_RECT = extern struct {
774782 Left: SHORT,
775783 Top: SHORT,
......@@ -777,6 +785,11 @@ pub const SMALL_RECT = extern struct {
777785 Bottom: SHORT,
778786};
779787
788pub const POINT = extern struct {
789 x: LONG,
790 y: LONG,
791};
792
780793pub const COORD = extern struct {
781794 X: SHORT,
782795 Y: SHORT,
lib/std/os/windows/kernel32.zig+1
......@@ -115,6 +115,7 @@ pub extern "kernel32" fn GetModuleFileNameW(hModule: ?HMODULE, lpFilename: [*]u1
115115pub extern "kernel32" fn GetModuleHandleW(lpModuleName: ?[*:0]const WCHAR) callconv(WINAPI) ?HMODULE;
116116
117117pub extern "kernel32" fn GetLastError() callconv(WINAPI) Win32Error;
118pub extern "kernel32" fn SetLastError(dwErrCode: Win32Error) callconv(WINAPI) void;
118119
119120pub extern "kernel32" fn GetFileInformationByHandle(
120121 hFile: HANDLE,
lib/std/os/windows/user32.zig+603-105
......@@ -4,45 +4,79 @@
44// The MIT license requires this copyright notice to be included in all copies
55// and substantial portions of the software.
66usingnamespace @import("bits.zig");
7const std = @import("std");
8const builtin = @import("builtin");
9const assert = std.debug.assert;
10const windows = @import("../windows.zig");
11const unexpectedError = windows.unexpectedError;
12const GetLastError = windows.kernel32.GetLastError;
13const SetLastError = windows.kernel32.SetLastError;
714
8// PM
9pub const PM_REMOVE = 0x0001;
10pub const PM_NOREMOVE = 0x0000;
11pub const PM_NOYIELD = 0x0002;
15fn selectSymbol(comptime function_static: anytype, function_dynamic: anytype, comptime os: std.Target.Os.WindowsVersion) @TypeOf(function_static) {
16 comptime {
17 const sym_ok = builtin.Target.current.os.isAtLeast(.windows, os);
18 if (sym_ok == true) return function_static;
19 if (sym_ok == null) return function_dynamic;
20 if (sym_ok == false) @compileError("Target OS range does not support function, at least " ++ @tagName(os) ++ " is required");
21 }
22}
23
24// === Messages ===
25
26pub const WNDPROC = fn (hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM) callconv(WINAPI) LRESULT;
27
28pub const MSG = extern struct {
29 hWnd: ?HWND,
30 message: UINT,
31 wParam: WPARAM,
32 lParam: LPARAM,
33 time: DWORD,
34 pt: POINT,
35 lPrivate: DWORD,
36};
1237
13// WM
1438pub const WM_NULL = 0x0000;
1539pub const WM_CREATE = 0x0001;
1640pub const WM_DESTROY = 0x0002;
41pub const WM_NCDESTROY = WM_DESTROY;
1742pub const WM_MOVE = 0x0003;
1843pub const WM_SIZE = 0x0005;
19
2044pub const WM_ACTIVATE = 0x0006;
21pub const WM_PAINT = 0x000F;
22pub const WM_CLOSE = 0x0010;
23pub const WM_QUIT = 0x0012;
2445pub const WM_SETFOCUS = 0x0007;
25
2646pub const WM_KILLFOCUS = 0x0008;
2747pub const WM_ENABLE = 0x000A;
2848pub const WM_SETREDRAW = 0x000B;
29
30pub const WM_SYSCOLORCHANGE = 0x0015;
49pub const WM_SETTEXT = 0x000C;
50pub const WM_GETTEXT = 0x000D;
51pub const WM_GETTEXTLENGTH = 0x000E;
52pub const WM_PAINT = 0x000F;
53pub const WM_CLOSE = 0x0010;
54pub const WM_QUIT = 0x0012;
55pub const WM_ERASEBKGND = 0x0014;
3156pub const WM_SHOWWINDOW = 0x0018;
32
33pub const WM_WINDOWPOSCHANGING = 0x0046;
57pub const WM_CTLCOLOR = 0x0019;
58pub const WM_NEXTDLGCTL = 0x0028;
59pub const WM_DRAWITEM = 0x002B;
60pub const WM_MEASUREITEM = 0x002C;
61pub const WM_DELETEITEM = 0x002D;
62pub const WM_VKEYTOITEM = 0x002E;
63pub const WM_CHARTOITEM = 0x002F;
64pub const WM_SETFONT = 0x0030;
65pub const WM_GETFONT = 0x0031;
66pub const WM_COMPAREITEM = 0x0039;
3467pub const WM_WINDOWPOSCHANGED = 0x0047;
35pub const WM_POWER = 0x0048;
36
37pub const WM_CONTEXTMENU = 0x007B;
38pub const WM_STYLECHANGING = 0x007C;
39pub const WM_STYLECHANGED = 0x007D;
40pub const WM_DISPLAYCHANGE = 0x007E;
41pub const WM_GETICON = 0x007F;
42pub const WM_SETICON = 0x0080;
43
44pub const WM_INPUT_DEVICE_CHANGE = 0x00fe;
45pub const WM_INPUT = 0x00FF;
68pub const WM_NOTIFY = 0x004E;
69pub const WM_NCCALCSIZE = 0x0083;
70pub const WM_NCHITTEST = 0x0084;
71pub const WM_NCPAINT = 0x0085;
72pub const WM_GETDLGCODE = 0x0087;
73pub const WM_NCMOUSEMOVE = 0x00A0;
74pub const WM_NCLBUTTONDOWN = 0x00A1;
75pub const WM_NCLBUTTONUP = 0x00A2;
76pub const WM_NCLBUTTONDBLCLK = 0x00A3;
77pub const WM_NCRBUTTONDOWN = 0x00A4;
78pub const WM_NCRBUTTONUP = 0x00A5;
79pub const WM_NCRBUTTONDBLCLK = 0x00A6;
4680pub const WM_KEYFIRST = 0x0100;
4781pub const WM_KEYDOWN = 0x0100;
4882pub const WM_KEYUP = 0x0101;
......@@ -52,13 +86,21 @@ pub const WM_SYSKEYDOWN = 0x0104;
5286pub const WM_SYSKEYUP = 0x0105;
5387pub const WM_SYSCHAR = 0x0106;
5488pub const WM_SYSDEADCHAR = 0x0107;
55pub const WM_UNICHAR = 0x0109;
56pub const WM_KEYLAST = 0x0109;
57
89pub const WM_KEYLAST = 0x0108;
90pub const WM_INITDIALOG = 0x0110;
5891pub const WM_COMMAND = 0x0111;
5992pub const WM_SYSCOMMAND = 0x0112;
6093pub const WM_TIMER = 0x0113;
61
94pub const WM_HSCROLL = 0x0114;
95pub const WM_VSCROLL = 0x0115;
96pub const WM_ENTERIDLE = 0x0121;
97pub const WM_CTLCOLORMSGBOX = 0x0132;
98pub const WM_CTLCOLOREDIT = 0x0133;
99pub const WM_CTLCOLORLISTBOX = 0x0134;
100pub const WM_CTLCOLORBTN = 0x0135;
101pub const WM_CTLCOLORDLG = 0x0136;
102pub const WM_CTLCOLORSCROLLBAR = 0x0137;
103pub const WM_CTLCOLORSTATIC = 0x0138;
62104pub const WM_MOUSEFIRST = 0x0200;
63105pub const WM_MOUSEMOVE = 0x0200;
64106pub const WM_LBUTTONDOWN = 0x0201;
......@@ -71,105 +113,561 @@ pub const WM_MBUTTONDOWN = 0x0207;
71113pub const WM_MBUTTONUP = 0x0208;
72114pub const WM_MBUTTONDBLCLK = 0x0209;
73115pub const WM_MOUSEWHEEL = 0x020A;
74pub const WM_XBUTTONDOWN = 0x020B;
75pub const WM_XBUTTONUP = 0x020C;
76pub const WM_XBUTTONDBLCLK = 0x020D;
116pub const WM_MOUSELAST = 0x020A;
117pub const WM_HOTKEY = 0x0312;
118pub const WM_CARET_CREATE = 0x03E0;
119pub const WM_CARET_DESTROY = 0x03E1;
120pub const WM_CARET_BLINK = 0x03E2;
121pub const WM_FDINPUT = 0x03F0;
122pub const WM_FDOUTPUT = 0x03F1;
123pub const WM_FDEXCEPT = 0x03F2;
124pub const WM_USER = 0x0400;
77125
78// WA
79pub const WA_INACTIVE = 0;
80pub const WA_ACTIVE = 0x0006;
126pub extern "user32" fn GetMessageA(lpMsg: *MSG, hWnd: ?HWND, wMsgFilterMin: UINT, wMsgFilterMax: UINT) callconv(WINAPI) BOOL;
127pub fn getMessageA(lpMsg: *MSG, hWnd: ?HWND, wMsgFilterMin: u32, wMsgFilterMax: u32) !void {
128 const r = GetMessageA(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
129 if (r == 0) return error.Quit;
130 if (r != -1) return;
131 return switch (GetLastError()) {
132 .INVALID_WINDOW_HANDLE => unreachable,
133 .INVALID_PARAMETER => unreachable,
134 else => |err| return windows.unexpectedError(err),
135 };
136}
81137
82// WS
83pub const WS_OVERLAPPED = 0x00000000;
84pub const WS_CAPTION = 0x00C00000;
85pub const WS_SYSMENU = 0x00080000;
86pub const WS_THICKFRAME = 0x00040000;
87pub const WS_MINIMIZEBOX = 0x00020000;
88pub const WS_MAXIMIZEBOX = 0x00010000;
138pub extern "user32" fn GetMessageW(lpMsg: *MSG, hWnd: ?HWND, wMsgFilterMin: UINT, wMsgFilterMax: UINT) callconv(WINAPI) BOOL;
139pub var pfnGetMessageW: @TypeOf(GetMessageW) = undefined;
140pub fn getMessageW(lpMsg: *MSG, hWnd: ?HWND, wMsgFilterMin: u32, wMsgFilterMax: u32) !void {
141 const function = selectSymbol(GetMessageW, pfnGetMessageW, .win2k);
89142
90// PFD
91pub const PFD_DRAW_TO_WINDOW = 0x00000004;
92pub const PFD_SUPPORT_OPENGL = 0x00000020;
93pub const PFD_DOUBLEBUFFER = 0x00000001;
94pub const PFD_MAIN_PLANE = 0;
95pub const PFD_TYPE_RGBA = 0;
143 const r = function(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
144 if (r == 0) return error.Quit;
145 if (r != -1) return;
146 return switch (GetLastError()) {
147 .INVALID_WINDOW_HANDLE => unreachable,
148 .INVALID_PARAMETER => unreachable,
149 else => |err| return windows.unexpectedError(err),
150 };
151}
96152
97// CS
98pub const CS_HREDRAW = 0x0002;
99pub const CS_VREDRAW = 0x0001;
100pub const CS_OWNDC = 0x0020;
153pub const PM_NOREMOVE = 0x0000;
154pub const PM_REMOVE = 0x0001;
155pub const PM_NOYIELD = 0x0002;
101156
102// SW
103pub const SW_HIDE = 0;
104pub const SW_SHOW = 5;
157pub extern "user32" fn PeekMessageA(lpMsg: *MSG, hWnd: ?HWND, wMsgFilterMin: UINT, wMsgFilterMax: UINT, wRemoveMsg: UINT) callconv(WINAPI) BOOL;
158pub fn peekMessageA(lpMsg: *MSG, hWnd: ?HWND, wMsgFilterMin: u32, wMsgFilterMax: u32, wRemoveMsg: u32) !bool {
159 const r = PeekMessageA(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg);
160 if (r == 0) return false;
161 if (r != -1) return true;
162 return switch (GetLastError()) {
163 .INVALID_WINDOW_HANDLE => unreachable,
164 .INVALID_PARAMETER => unreachable,
165 else => |err| return windows.unexpectedError(err),
166 };
167}
168
169pub extern "user32" fn PeekMessageW(lpMsg: *MSG, hWnd: ?HWND, wMsgFilterMin: UINT, wMsgFilterMax: UINT, wRemoveMsg: UINT) callconv(WINAPI) BOOL;
170pub var pfnPeekMessageW: @TypeOf(PeekMessageW) = undefined;
171pub fn peekMessageW(lpMsg: *MSG, hWnd: ?HWND, wMsgFilterMin: u32, wMsgFilterMax: u32, wRemoveMsg: u32) !bool {
172 const function = selectSymbol(PeekMessageW, pfnPeekMessageW, .win2k);
173
174 const r = function(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg);
175 if (r == 0) return false;
176 if (r != -1) return true;
177 return switch (GetLastError()) {
178 .INVALID_WINDOW_HANDLE => unreachable,
179 .INVALID_PARAMETER => unreachable,
180 else => |err| return windows.unexpectedError(err),
181 };
182}
183
184pub extern "user32" fn TranslateMessage(lpMsg: *const MSG) callconv(WINAPI) BOOL;
185pub fn translateMessage(lpMsg: *const MSG) bool {
186 return if (TranslateMessage(lpMsg) == 0) false else true;
187}
188
189pub extern "user32" fn DispatchMessageA(lpMsg: *const MSG) callconv(WINAPI) LRESULT;
190pub fn dispatchMessageA(lpMsg: *const MSG) LRESULT {
191 return DispatchMessageA(lpMsg);
192}
193
194pub extern "user32" fn DispatchMessageW(lpMsg: *const MSG) callconv(WINAPI) LRESULT;
195pub var pfnDispatchMessageW: @TypeOf(DispatchMessageW) = undefined;
196pub fn dispatchMessageW(lpMsg: *const MSG) LRESULT {
197 const function = selectSymbol(DispatchMessageW, pfnDispatchMessageW, .win2k);
198 return function(lpMsg);
199}
105200
106pub const WNDPROC = fn (HWND, UINT, WPARAM, LPARAM) callconv(WINAPI) LRESULT;
201pub extern "user32" fn PostQuitMessage(nExitCode: i32) callconv(WINAPI) void;
202pub fn postQuitMessage(nExitCode: i32) void {
203 PostQuitMessage(nExitCode);
204}
205
206pub extern "user32" fn DefWindowProcA(hWnd: HWND, Msg: UINT, wParam: WPARAM, lParam: LPARAM) callconv(WINAPI) LRESULT;
207pub fn defWindowProcA(hWnd: HWND, Msg: UINT, wParam: WPARAM, lParam: LPARAM) LRESULT {
208 return DefWindowProcA(hWnd, Msg, wParam, lParam);
209}
210
211pub extern "user32" fn DefWindowProcW(hWnd: HWND, Msg: UINT, wParam: WPARAM, lParam: LPARAM) callconv(WINAPI) LRESULT;
212pub var pfnDefWindowProcW: @TypeOf(DefWindowProcW) = undefined;
213pub fn defWindowProcW(hWnd: HWND, Msg: UINT, wParam: WPARAM, lParam: LPARAM) LRESULT {
214 const function = selectSymbol(DefWindowProcW, pfnDefWindowProcW, .win2k);
215 return function(hWnd, Msg, wParam, lParam);
216}
217
218// === Windows ===
219
220pub const CS_VREDRAW = 0x0001;
221pub const CS_HREDRAW = 0x0002;
222pub const CS_DBLCLKS = 0x0008;
223pub const CS_OWNDC = 0x0020;
224pub const CS_CLASSDC = 0x0040;
225pub const CS_PARENTDC = 0x0080;
226pub const CS_NOCLOSE = 0x0200;
227pub const CS_SAVEBITS = 0x0800;
228pub const CS_BYTEALIGNCLIENT = 0x1000;
229pub const CS_BYTEALIGNWINDOW = 0x2000;
230pub const CS_GLOBALCLASS = 0x4000;
107231
108232pub const WNDCLASSEXA = extern struct {
109233 cbSize: UINT = @sizeOf(WNDCLASSEXA),
110234 style: UINT,
111235 lpfnWndProc: WNDPROC,
112 cbClsExtra: i32,
113 cbWndExtra: i32,
236 cbClsExtra: i32 = 0,
237 cbWndExtra: i32 = 0,
114238 hInstance: HINSTANCE,
115239 hIcon: ?HICON,
116240 hCursor: ?HCURSOR,
117241 hbrBackground: ?HBRUSH,
118 lpszMenuName: ?LPCSTR,
119 lpszClassName: LPCSTR,
242 lpszMenuName: ?[*:0]const u8,
243 lpszClassName: [*:0]const u8,
120244 hIconSm: ?HICON,
121245};
122246
123pub const POINT = extern struct {
124 x: c_long, y: c_long
247pub const WNDCLASSEXW = extern struct {
248 cbSize: UINT = @sizeOf(WNDCLASSEXW),
249 style: UINT,
250 lpfnWndProc: WNDPROC,
251 cbClsExtra: i32 = 0,
252 cbWndExtra: i32 = 0,
253 hInstance: HINSTANCE,
254 hIcon: ?HICON,
255 hCursor: ?HCURSOR,
256 hbrBackground: ?HBRUSH,
257 lpszMenuName: ?[*:0]const u16,
258 lpszClassName: [*:0]const u16,
259 hIconSm: ?HICON,
125260};
126261
127pub const MSG = extern struct {
128 hWnd: ?HWND,
129 message: UINT,
130 wParam: WPARAM,
131 lParam: LPARAM,
132 time: DWORD,
133 pt: POINT,
134 lPrivate: DWORD,
135};
262pub extern "user32" fn RegisterClassExA(*const WNDCLASSEXA) callconv(WINAPI) ATOM;
263pub fn registerClassExA(window_class: *const WNDCLASSEXA) !ATOM {
264 const atom = RegisterClassExA(window_class);
265 if (atom != 0) return atom;
266 return switch (GetLastError()) {
267 .CLASS_ALREADY_EXISTS => error.AlreadyExists,
268 .INVALID_PARAMETER => unreachable,
269 else => |err| return windows.unexpectedError(err),
270 };
271}
136272
137pub extern "user32" fn CreateWindowExA(
138 dwExStyle: DWORD,
139 lpClassName: LPCSTR,
140 lpWindowName: LPCSTR,
141 dwStyle: DWORD,
142 X: i32,
143 Y: i32,
144 nWidth: i32,
145 nHeight: i32,
146 hWindParent: ?HWND,
147 hMenu: ?HMENU,
148 hInstance: HINSTANCE,
149 lpParam: ?LPVOID,
150) callconv(WINAPI) ?HWND;
273pub extern "user32" fn RegisterClassExW(*const WNDCLASSEXW) callconv(WINAPI) ATOM;
274pub var pfnRegisterClassExW: @TypeOf(RegisterClassExW) = undefined;
275pub fn registerClassExW(window_class: *const WNDCLASSEXA) !ATOM {
276 const function = selectSymbol(RegisterClassExW, pfnRegisterClassExW, .win2k);
277 const atom = function(window_class);
278 if (atom != 0) return atom;
279 return switch (GetLastError()) {
280 .CLASS_ALREADY_EXISTS => error.AlreadyExists,
281 .CALL_NOT_IMPLEMENTED => unreachable,
282 .INVALID_PARAMETER => unreachable,
283 else => |err| return windows.unexpectedError(err),
284 };
285}
286
287pub extern "user32" fn UnregisterClassA(lpClassName: LPCSTR, hInstance: HINSTANCE) callconv(.Stdcall) BOOL;
288pub fn unregisterClassA(lpClassName: [*:0]const u8, hInstance: HINSTANCE) !void {
289 if (UnregisterClassA(lpClassName, hInstance) == 0) {
290 return switch (GetLastError()) {
291 .CLASS_DOES_NOT_EXIST => error.ClassDoesNotExist,
292 else => |err| return windows.unexpectedError(err),
293 };
294 }
295}
296
297pub extern "user32" fn UnregisterClassW(lpClassName: LPCWSTR, hInstance: HINSTANCE) callconv(.Stdcall) BOOL;
298pub var pfnUnregisterClassW: @TypeOf(UnregisterClassW) = undefined;
299pub fn unregisterClassW(lpClassName: [*:0]const u16, hInstance: HINSTANCE) !void {
300 const function = selectSymbol(UnregisterClassW, pfnUnregisterClassW, .win2k);
301 if (function(lpClassName, hInstance) == 0) {
302 return switch (GetLastError()) {
303 .CLASS_DOES_NOT_EXIST => error.ClassDoesNotExist,
304 else => |err| return windows.unexpectedError(err),
305 };
306 }
307}
308
309pub const WS_OVERLAPPED = 0x00000000;
310pub const WS_POPUP = 0x80000000;
311pub const WS_CHILD = 0x40000000;
312pub const WS_MINIMIZE = 0x20000000;
313pub const WS_VISIBLE = 0x10000000;
314pub const WS_DISABLED = 0x08000000;
315pub const WS_CLIPSIBLINGS = 0x04000000;
316pub const WS_CLIPCHILDREN = 0x02000000;
317pub const WS_MAXIMIZE = 0x01000000;
318pub const WS_CAPTION = WS_BORDER | WS_DLGFRAME;
319pub const WS_BORDER = 0x00800000;
320pub const WS_DLGFRAME = 0x00400000;
321pub const WS_VSCROLL = 0x00200000;
322pub const WS_HSCROLL = 0x00100000;
323pub const WS_SYSMENU = 0x00080000;
324pub const WS_THICKFRAME = 0x00040000;
325pub const WS_GROUP = 0x00020000;
326pub const WS_TABSTOP = 0x00010000;
327pub const WS_MINIMIZEBOX = 0x00020000;
328pub const WS_MAXIMIZEBOX = 0x00010000;
329pub const WS_TILED = WS_OVERLAPPED;
330pub const WS_ICONIC = WS_MINIMIZE;
331pub const WS_SIZEBOX = WS_THICKFRAME;
332pub const WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW;
333pub const WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
334pub const WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU;
335pub const WS_CHILDWINDOW = WS_CHILD;
336
337pub const WS_EX_DLGMODALFRAME = 0x00000001;
338pub const WS_EX_NOPARENTNOTIFY = 0x00000004;
339pub const WS_EX_TOPMOST = 0x00000008;
340pub const WS_EX_ACCEPTFILES = 0x00000010;
341pub const WS_EX_TRANSPARENT = 0x00000020;
342pub const WS_EX_MDICHILD = 0x00000040;
343pub const WS_EX_TOOLWINDOW = 0x00000080;
344pub const WS_EX_WINDOWEDGE = 0x00000100;
345pub const WS_EX_CLIENTEDGE = 0x00000200;
346pub const WS_EX_CONTEXTHELP = 0x00000400;
347pub const WS_EX_RIGHT = 0x00001000;
348pub const WS_EX_LEFT = 0x00000000;
349pub const WS_EX_RTLREADING = 0x00002000;
350pub const WS_EX_LTRREADING = 0x00000000;
351pub const WS_EX_LEFTSCROLLBAR = 0x00004000;
352pub const WS_EX_RIGHTSCROLLBAR = 0x00000000;
353pub const WS_EX_CONTROLPARENT = 0x00010000;
354pub const WS_EX_STATICEDGE = 0x00020000;
355pub const WS_EX_APPWINDOW = 0x00040000;
356pub const WS_EX_LAYERED = 0x00080000;
357pub const WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
358pub const WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST);
359
360pub const CW_USEDEFAULT = @bitCast(i32, @as(u32, 0x80000000));
361
362pub extern "user32" fn CreateWindowExA(dwExStyle: DWORD, lpClassName: [*:0]const u8, lpWindowName: [*:0]const u8, dwStyle: DWORD, X: i32, Y: i32, nWidth: i32, nHeight: i32, hWindParent: ?HWND, hMenu: ?HMENU, hInstance: HINSTANCE, lpParam: ?LPVOID) callconv(WINAPI) ?HWND;
363pub fn createWindowExA(dwExStyle: u32, lpClassName: [*:0]const u8, lpWindowName: [*:0]const u8, dwStyle: u32, X: i32, Y: i32, nWidth: i32, nHeight: i32, hWindParent: ?HWND, hMenu: ?HMENU, hInstance: HINSTANCE, lpParam: ?*c_void) !HWND {
364 const window = CreateWindowExA(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight, hWindParent, hMenu, hInstance, lpParam);
365 if (window) |win| return win;
366
367 return switch (GetLastError()) {
368 .CLASS_DOES_NOT_EXIST => error.ClassDoesNotExist,
369 .INVALID_PARAMETER => unreachable,
370 else => |err| return windows.unexpectedError(err),
371 };
372}
373
374pub extern "user32" fn CreateWindowExW(dwExStyle: DWORD, lpClassName: [*:0]const u16, lpWindowName: [*:0]const u16, dwStyle: DWORD, X: i32, Y: i32, nWidth: i32, nHeight: i32, hWindParent: ?HWND, hMenu: ?HMENU, hInstance: HINSTANCE, lpParam: ?LPVOID) callconv(WINAPI) ?HWND;
375pub var pfnCreateWindowExW: @TypeOf(RegisterClassExW) = undefined;
376pub fn createWindowExW(dwExStyle: u32, lpClassName: [*:0]const u16, lpWindowName: [*:0]const u16, dwStyle: u32, X: i32, Y: i32, nWidth: i32, nHeight: i32, hWindParent: ?HWND, hMenu: ?HMENU, hInstance: HINSTANCE, lpParam: ?*c_void) !HWND {
377 const function = selectSymbol(CreateWindowExW, pfnCreateWindowExW, .win2k);
378 const window = function(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight, hWindParent, hMenu, hInstance, lpParam);
379 if (window) |win| return win;
380
381 return switch (GetLastError()) {
382 .CLASS_DOES_NOT_EXIST => error.ClassDoesNotExist,
383 .INVALID_PARAMETER => unreachable,
384 else => |err| return windows.unexpectedError(err),
385 };
386}
387
388pub extern "user32" fn DestroyWindow(hWnd: HWND) callconv(WINAPI) BOOL;
389pub fn destroyWindow(hWnd: HWND) !void {
390 if (DestroyWindow(hWnd) == 0) {
391 return switch (GetLastError()) {
392 .INVALID_WINDOW_HANDLE => unreachable,
393 .INVALID_PARAMETER => unreachable,
394 else => |err| return windows.unexpectedError(err),
395 };
396 }
397}
398
399pub const SW_HIDE = 0;
400pub const SW_SHOWNORMAL = 1;
401pub const SW_NORMAL = 1;
402pub const SW_SHOWMINIMIZED = 2;
403pub const SW_SHOWMAXIMIZED = 3;
404pub const SW_MAXIMIZE = 3;
405pub const SW_SHOWNOACTIVATE = 4;
406pub const SW_SHOW = 5;
407pub const SW_MINIMIZE = 6;
408pub const SW_SHOWMINNOACTIVE = 7;
409pub const SW_SHOWNA = 8;
410pub const SW_RESTORE = 9;
411pub const SW_SHOWDEFAULT = 10;
412pub const SW_FORCEMINIMIZE = 11;
413pub const SW_MAX = 11;
414
415pub extern "user32" fn ShowWindow(hWnd: HWND, nCmdShow: i32) callconv(WINAPI) BOOL;
416pub fn showWindow(hWnd: HWND, nCmdShow: i32) bool {
417 return (ShowWindow(hWnd, nCmdShow) == TRUE);
418}
419
420pub extern "user32" fn UpdateWindow(hWnd: HWND) callconv(WINAPI) BOOL;
421pub fn updateWindow(hWnd: HWND) !void {
422 if (ShowWindow(hWnd, nCmdShow) == 0) {
423 return switch (GetLastError()) {
424 .INVALID_WINDOW_HANDLE => unreachable,
425 .INVALID_PARAMETER => unreachable,
426 else => |err| return windows.unexpectedError(err),
427 };
428 }
429}
430
431pub extern "user32" fn AdjustWindowRectEx(lpRect: *RECT, dwStyle: DWORD, bMenu: BOOL, dwExStyle: DWORD) callconv(WINAPI) BOOL;
432pub fn adjustWindowRectEx(lpRect: *RECT, dwStyle: u32, bMenu: bool, dwExStyle: u32) !void {
433 assert(dwStyle & WS_OVERLAPPED == 0);
434
435 if (AdjustWindowRectEx(lpRect, dwStyle, bMenu, dwExStyle) == 0) {
436 return switch (GetLastError()) {
437 .INVALID_PARAMETER => unreachable,
438 else => |err| return windows.unexpectedError(err),
439 };
440 }
441}
442
443pub const GWL_WNDPROC = -4;
444pub const GWL_HINSTANCE = -6;
445pub const GWL_HWNDPARENT = -8;
446pub const GWL_STYLE = -16;
447pub const GWL_EXSTYLE = -20;
448pub const GWL_USERDATA = -21;
449pub const GWL_ID = -12;
450
451pub extern "user32" fn GetWindowLongA(hWnd: HWND, nIndex: i32) callconv(WINAPI) LONG;
452pub fn getWindowLongA(hWnd: HWND, nIndex: i32) !i32 {
453 const value = GetWindowLongA(hWnd, nIndex);
454 if (value != 0) return value;
455
456 return switch (GetLastError()) {
457 .SUCCESS => 0,
458 .INVALID_WINDOW_HANDLE => unreachable,
459 .INVALID_PARAMETER => unreachable,
460 else => |err| return windows.unexpectedError(err),
461 };
462}
463
464pub extern "user32" fn GetWindowLongW(hWnd: HWND, nIndex: i32) callconv(WINAPI) LONG;
465pub var pfnGetWindowLongW: @TypeOf(GetWindowLongW) = undefined;
466pub fn getWindowLongW(hWnd: HWND, nIndex: i32) !i32 {
467 const function = selectSymbol(GetWindowLongW, pfnGetWindowLongW, .win2k);
468
469 const value = function(hWnd, nIndex);
470 if (value != 0) return value;
471
472 return switch (GetLastError()) {
473 .SUCCESS => 0,
474 .INVALID_WINDOW_HANDLE => unreachable,
475 .INVALID_PARAMETER => unreachable,
476 else => |err| return windows.unexpectedError(err),
477 };
478}
479
480pub extern "user32" fn GetWindowLongPtrA(hWnd: HWND, nIndex: i32) callconv(WINAPI) LONG_PTR;
481pub fn getWindowLongPtrA(hWnd: HWND, nIndex: i32) !isize {
482 // "When compiling for 32-bit Windows, GetWindowLongPtr is defined as a call to the GetWindowLong function."
483 // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowlongptrw
484 if (@sizeOf(LONG_PTR) == 4) return getWindowLongA(hWnd, nIndex);
485
486 const value = GetWindowLongPtrA(hWnd, nIndex);
487 if (value != 0) return value;
488
489 return switch (GetLastError()) {
490 .SUCCESS => 0,
491 .INVALID_WINDOW_HANDLE => unreachable,
492 .INVALID_PARAMETER => unreachable,
493 else => |err| return windows.unexpectedError(err),
494 };
495}
496
497pub extern "user32" fn GetWindowLongPtrW(hWnd: HWND, nIndex: i32) callconv(WINAPI) LONG_PTR;
498pub var pfnGetWindowLongPtrW: @TypeOf(GetWindowLongPtrW) = undefined;
499pub fn getWindowLongPtrW(hWnd: HWND, nIndex: i32) !isize {
500 if (@sizeOf(LONG_PTR) == 4) return getWindowLongW(hWnd, nIndex);
501 const function = selectSymbol(GetWindowLongPtrW, pfnGetWindowLongPtrW, .win2k);
502
503 const value = function(hWnd, nIndex);
504 if (value != 0) return value;
505
506 return switch (GetLastError()) {
507 .SUCCESS => 0,
508 .INVALID_WINDOW_HANDLE => unreachable,
509 .INVALID_PARAMETER => unreachable,
510 else => |err| return windows.unexpectedError(err),
511 };
512}
513
514pub extern "user32" fn SetWindowLongA(hWnd: HWND, nIndex: i32, dwNewLong: LONG) callconv(WINAPI) LONG;
515pub fn setWindowLongA(hWnd: HWND, nIndex: i32, dwNewLong: i32) !i32 {
516 // [...] you should clear the last error information by calling SetLastError with 0 before calling SetWindowLong.
517 // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowlonga
518 SetLastError(.SUCCESS);
519
520 const value = SetWindowLongA(hWnd, nIndex, dwNewLong);
521 if (value != 0) return value;
522
523 return switch (GetLastError()) {
524 .SUCCESS => 0,
525 .INVALID_WINDOW_HANDLE => unreachable,
526 .INVALID_PARAMETER => unreachable,
527 else => |err| return windows.unexpectedError(err),
528 };
529}
530
531pub extern "user32" fn SetWindowLongW(hWnd: HWND, nIndex: i32, dwNewLong: LONG) callconv(WINAPI) LONG;
532pub var pfnSetWindowLongW: @TypeOf(SetWindowLongW) = undefined;
533pub fn setWindowLongW(hWnd: HWND, nIndex: i32, dwNewLong: i32) !i32 {
534 const function = selectSymbol(SetWindowLongW, pfnSetWindowLongW, .win2k);
535
536 SetLastError(.SUCCESS);
537 const value = function(hWnd, nIndex, dwNewLong);
538 if (value != 0) return value;
539
540 return switch (GetLastError()) {
541 .SUCCESS => 0,
542 .INVALID_WINDOW_HANDLE => unreachable,
543 .INVALID_PARAMETER => unreachable,
544 else => |err| return windows.unexpectedError(err),
545 };
546}
547
548pub extern "user32" fn SetWindowLongPtrA(hWnd: HWND, nIndex: i32, dwNewLong: LONG_PTR) callconv(WINAPI) LONG_PTR;
549pub fn setWindowLongPtrA(hWnd: HWND, nIndex: i32, dwNewLong: isize) !isize {
550 // "When compiling for 32-bit Windows, GetWindowLongPtr is defined as a call to the GetWindowLong function."
551 // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowlongptrw
552 if (@sizeOf(LONG_PTR) == 4) return setWindowLongA(hWnd, nIndex, dwNewLong);
553
554 SetLastError(.SUCCESS);
555 const value = SetWindowLongPtrA(hWnd, nIndex, dwNewLong);
556 if (value != 0) return value;
557
558 return switch (GetLastError()) {
559 .SUCCESS => 0,
560 .INVALID_WINDOW_HANDLE => unreachable,
561 .INVALID_PARAMETER => unreachable,
562 else => |err| return windows.unexpectedError(err),
563 };
564}
565
566pub extern "user32" fn SetWindowLongPtrW(hWnd: HWND, nIndex: i32, dwNewLong: LONG_PTR) callconv(WINAPI) LONG_PTR;
567pub var pfnSetWindowLongPtrW: @TypeOf(SetWindowLongPtrW) = undefined;
568pub fn setWindowLongPtrW(hWnd: HWND, nIndex: i32, dwNewLong: isize) !isize {
569 if (@sizeOf(LONG_PTR) == 4) return setWindowLongW(hWnd, nIndex, dwNewLong);
570 const function = selectSymbol(SetWindowLongPtrW, pfnSetWindowLongPtrW, .win2k);
571
572 SetLastError(.SUCCESS);
573 const value = function(hWnd, nIndex, dwNewLong);
574 if (value != 0) return value;
575
576 return switch (GetLastError()) {
577 .SUCCESS => 0,
578 .INVALID_WINDOW_HANDLE => unreachable,
579 .INVALID_PARAMETER => unreachable,
580 else => |err| return windows.unexpectedError(err),
581 };
582}
151583
152pub extern "user32" fn RegisterClassExA(*const WNDCLASSEXA) callconv(WINAPI) c_ushort;
153pub extern "user32" fn DefWindowProcA(HWND, Msg: UINT, WPARAM, LPARAM) callconv(WINAPI) LRESULT;
154pub extern "user32" fn ShowWindow(hWnd: ?HWND, nCmdShow: i32) callconv(WINAPI) bool;
155pub extern "user32" fn UpdateWindow(hWnd: ?HWND) callconv(WINAPI) bool;
156584pub extern "user32" fn GetDC(hWnd: ?HWND) callconv(WINAPI) ?HDC;
585pub fn getDC(hWnd: ?HWND) !HDC {
586 const hdc = GetDC(hWnd);
587 if (hdc) |h| return h;
157588
158pub extern "user32" fn PeekMessageA(
159 lpMsg: ?*MSG,
160 hWnd: ?HWND,
161 wMsgFilterMin: UINT,
162 wMsgFilterMax: UINT,
163 wRemoveMsg: UINT,
164) callconv(WINAPI) bool;
589 return switch (GetLastError()) {
590 .INVALID_WINDOW_HANDLE => unreachable,
591 .INVALID_PARAMETER => unreachable,
592 else => |err| return windows.unexpectedError(err),
593 };
594}
165595
166pub extern "user32" fn GetMessageA(
167 lpMsg: ?*MSG,
168 hWnd: ?HWND,
169 wMsgFilterMin: UINT,
170 wMsgFilterMax: UINT,
171) callconv(WINAPI) bool;
596pub extern "user32" fn ReleaseDC(hWnd: ?HWND, hDC: HDC) callconv(WINAPI) i32;
597pub fn releaseDC(hWnd: ?HWND, hDC: HDC) bool {
598 return if (ReleaseDC(hWnd, hDC) == 1) true else false;
599}
172600
173pub extern "user32" fn TranslateMessage(lpMsg: *const MSG) callconv(WINAPI) bool;
174pub extern "user32" fn DispatchMessageA(lpMsg: *const MSG) callconv(WINAPI) LRESULT;
175pub extern "user32" fn PostQuitMessage(nExitCode: i32) callconv(WINAPI) void;
601// === Modal dialogue boxes ===
602
603pub const MB_OK = 0x00000000;
604pub const MB_OKCANCEL = 0x00000001;
605pub const MB_ABORTRETRYIGNORE = 0x00000002;
606pub const MB_YESNOCANCEL = 0x00000003;
607pub const MB_YESNO = 0x00000004;
608pub const MB_RETRYCANCEL = 0x00000005;
609pub const MB_CANCELTRYCONTINUE = 0x00000006;
610pub const MB_ICONHAND = 0x00000010;
611pub const MB_ICONQUESTION = 0x00000020;
612pub const MB_ICONEXCLAMATION = 0x00000030;
613pub const MB_ICONASTERISK = 0x00000040;
614pub const MB_USERICON = 0x00000080;
615pub const MB_ICONWARNING = MB_ICONEXCLAMATION;
616pub const MB_ICONERROR = MB_ICONHAND;
617pub const MB_ICONINFORMATION = MB_ICONASTERISK;
618pub const MB_ICONSTOP = MB_ICONHAND;
619pub const MB_DEFBUTTON1 = 0x00000000;
620pub const MB_DEFBUTTON2 = 0x00000100;
621pub const MB_DEFBUTTON3 = 0x00000200;
622pub const MB_DEFBUTTON4 = 0x00000300;
623pub const MB_APPLMODAL = 0x00000000;
624pub const MB_SYSTEMMODAL = 0x00001000;
625pub const MB_TASKMODAL = 0x00002000;
626pub const MB_HELP = 0x00004000;
627pub const MB_NOFOCUS = 0x00008000;
628pub const MB_SETFOREGROUND = 0x00010000;
629pub const MB_DEFAULT_DESKTOP_ONLY = 0x00020000;
630pub const MB_TOPMOST = 0x00040000;
631pub const MB_RIGHT = 0x00080000;
632pub const MB_RTLREADING = 0x00100000;
633pub const MB_TYPEMASK = 0x0000000F;
634pub const MB_ICONMASK = 0x000000F0;
635pub const MB_DEFMASK = 0x00000F00;
636pub const MB_MODEMASK = 0x00003000;
637pub const MB_MISCMASK = 0x0000C000;
638
639pub const IDOK = 1;
640pub const IDCANCEL = 2;
641pub const IDABORT = 3;
642pub const IDRETRY = 4;
643pub const IDIGNORE = 5;
644pub const IDYES = 6;
645pub const IDNO = 7;
646pub const IDCLOSE = 8;
647pub const IDHELP = 9;
648pub const IDTRYAGAIN = 10;
649pub const IDCONTINUE = 11;
650
651pub extern "user32" fn MessageBoxA(hWnd: ?HWND, lpText: [*:0]const u8, lpCaption: [*:0]const u8, uType: UINT) callconv(WINAPI) i32;
652pub fn messageBoxA(hWnd: ?HWND, lpText: [*:0]const u8, lpCaption: [*:0]const u8, uType: u32) !i32 {
653 const value = MessageBoxA(hWnd, lpText, lpCaption, uType);
654 if (value != 0) return value;
655 return switch (GetLastError()) {
656 .INVALID_WINDOW_HANDLE => unreachable,
657 .INVALID_PARAMETER => unreachable,
658 else => |err| return windows.unexpectedError(err),
659 };
660}
661
662pub extern "user32" fn MessageBoxW(hWnd: ?HWND, lpText: [*:0]const u16, lpCaption: ?[*:0]const u16, uType: UINT) callconv(WINAPI) i32;
663pub var pfnMessageBoxW: @TypeOf(MessageBoxW) = undefined;
664pub fn messageBoxW(hWnd: ?HWND, lpText: [*:0]const u16, lpCaption: [*:0]const u16, uType: u32) !i32 {
665 const function = selectSymbol(pfnMessageBoxW, MessageBoxW, .win2k);
666 const value = function(hWnd, lpText, lpCaption, uType);
667 if (value != 0) return value;
668 return switch (GetLastError()) {
669 .INVALID_WINDOW_HANDLE => unreachable,
670 .INVALID_PARAMETER => unreachable,
671 else => |err| return windows.unexpectedError(err),
672 };
673}