Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 66 additions & 62 deletions examples/basewin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,77 @@ const windowlongptr = @import("win32").windowlongptr;
// NOTE: should Zig allow symbol conflicts so long as they are not referenced?
const mnr = @import("win32").ui.menus_and_resources;

pub fn BaseWindow(comptime DERIVED_TYPE: type) type { return struct {

fn WindowProc(hwnd: HWND , uMsg: u32, wParam: win32.WPARAM, lParam: win32.LPARAM) callconv(WINAPI) win32.LRESULT
{
var pThis : ?*DERIVED_TYPE = null;
if (uMsg == win32.WM_NCCREATE)
{
const pCreate = @intToPtr(*win32.CREATESTRUCT, @bitCast(usize, lParam));
pThis = @ptrCast(*DERIVED_TYPE, @alignCast(@alignOf(DERIVED_TYPE), pCreate.lpCreateParams));
_ = windowlongptr.SetWindowLongPtr(hwnd, win32.GWL_USERDATA, @bitCast(isize, @ptrToInt(pThis)));
pThis.?.base.m_hwnd = hwnd;
}
else
{
pThis = @intToPtr(?*DERIVED_TYPE, @bitCast(usize, windowlongptr.GetWindowLongPtr(hwnd, win32.GWL_USERDATA)));
}
if (pThis) |this|
{
return this.HandleMessage(uMsg, wParam, lParam);
pub fn BaseWindow(comptime DERIVED_TYPE: type) type {
return struct {
fn WindowProc(hwnd: HWND, uMsg: u32, wParam: win32.WPARAM, lParam: win32.LPARAM) callconv(WINAPI) win32.LRESULT {
var pThis: ?*DERIVED_TYPE = null;
if (uMsg == win32.WM_NCCREATE) {
const pCreate = @as(*win32.CREATESTRUCT, @ptrFromInt(@as(usize, @bitCast(lParam))));
pThis = @as(*DERIVED_TYPE, @ptrCast(@alignCast(pCreate.lpCreateParams)));
_ = windowlongptr.SetWindowLongPtr(hwnd, win32.GWL_USERDATA, @as(isize, @bitCast(@intFromPtr(pThis))));
pThis.?.base.m_hwnd = hwnd;
} else {
pThis = @as(?*DERIVED_TYPE, @ptrFromInt(@as(usize, @bitCast(windowlongptr.GetWindowLongPtr(hwnd, win32.GWL_USERDATA)))));
}
if (pThis) |this| {
return this.HandleMessage(uMsg, wParam, lParam);
} else {
return win32.DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
else
{
return win32.DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}

pub fn Create(self: *@This(),
lpWindowName: [*:0]const u16,
dwStyle: win32.WINDOW_STYLE,
options: struct {
dwExStyle: win32.WINDOW_EX_STYLE = @intToEnum(win32.WINDOW_EX_STYLE, 0),
x: i32 = win32.CW_USEDEFAULT,
y: i32 = win32.CW_USEDEFAULT,
nWidth: i32 = win32.CW_USEDEFAULT,
nHeight: i32 = win32.CW_USEDEFAULT,
hWndParent: ?HWND = null,
hMenu: ?win32.HMENU = null,
},
) win32.BOOL {
const wc = win32.WNDCLASS {
.style = @intToEnum(win32.WNDCLASS_STYLES, 0),
.lpfnWndProc = WindowProc,
.cbClsExtra = 0,
.cbWndExtra = 0,
.hInstance = win32.GetModuleHandle(null),
.hIcon = null,
.hCursor = null,
.hbrBackground = null,
// TODO: autogen bindings don't allow for null, should win32metadata allow Option for fields? Or should all strings allow NULL?
.lpszMenuName = L("Placeholder"),
.lpszClassName = DERIVED_TYPE.ClassName(),
};
pub fn Create(
self: *@This(),
lpWindowName: [*:0]const u16,
dwStyle: win32.WINDOW_STYLE,
options: struct {
dwExStyle: win32.WINDOW_EX_STYLE = @enumFromInt(0),
x: i32 = win32.CW_USEDEFAULT,
y: i32 = win32.CW_USEDEFAULT,
nWidth: i32 = win32.CW_USEDEFAULT,
nHeight: i32 = win32.CW_USEDEFAULT,
hWndParent: ?HWND = null,
hMenu: ?win32.HMENU = null,
},
) win32.BOOL {
const wc = win32.WNDCLASS{
.style = @as(win32.WNDCLASS_STYLES, @enumFromInt(0)),
.lpfnWndProc = WindowProc,
.cbClsExtra = 0,
.cbWndExtra = 0,
.hInstance = win32.GetModuleHandle(null),
.hIcon = null,
.hCursor = null,
.hbrBackground = null,
// TODO: autogen bindings don't allow for null, should win32metadata allow Option for fields? Or should all strings allow NULL?
.lpszMenuName = L("Placeholder"),
.lpszClassName = DERIVED_TYPE.ClassName(),
};

_ = win32.RegisterClass(&wc);
_ = win32.RegisterClass(&wc);

self.m_hwnd = win32.CreateWindowEx(
options.dwExStyle, DERIVED_TYPE.ClassName(), lpWindowName,
dwStyle, options.x, options.y,
options.nWidth, options.nHeight, options.hWndParent, options.hMenu,
win32.GetModuleHandle(null),
@ptrCast(*anyopaque, self)
self.m_hwnd = win32.CreateWindowEx(
options.dwExStyle,
DERIVED_TYPE.ClassName(),
lpWindowName,
dwStyle,
options.x,
options.y,
options.nWidth,
options.nHeight,
options.hWndParent,
options.hMenu,
win32.GetModuleHandle(null),
@ptrCast(self),
);

return if (self.m_hwnd != null) win32.TRUE else win32.FALSE;
}
return if (self.m_hwnd != null) win32.TRUE else win32.FALSE;
}

pub fn Window(self: @This()) ?HWND { return self.m_hwnd; }
pub fn Window(self: @This()) ?HWND {
return self.m_hwnd;
}

m_hwnd: ?HWND = null,
};}
m_hwnd: ?HWND = null,
};
}
2 changes: 1 addition & 1 deletion examples/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn makeExe(
) !void {
const exe = b.addExecutable(.{
.name = root,
.root_source_file = .{ .path = try std.mem.concat(b.allocator, u8, &[_][]const u8 {root, ".zig"}) },
.root_source_file = .{ .path = try std.mem.concat(b.allocator, u8, &[_][]const u8{ root, ".zig" }) },
.target = target,
.optimize = optimize,
});
Expand Down
Loading