Skip to content

Commit

Permalink
win32: fix #22
Browse files Browse the repository at this point in the history
  • Loading branch information
zenith391 committed Sep 18, 2022
1 parent ad1e3c9 commit af433af
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
zig-out/
zig-cache/
.zigmod/
./deps.zig
deps.zig
34 changes: 21 additions & 13 deletions src/backends/win32/backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ var hInst: HINSTANCE = undefined;
/// anti-aliasing. So we take the real default caption font from
/// NONFCLIENTEMETRICS and apply it manually to every widget.
var captionFont: win32.HFONT = undefined;
/// Default arrow cursor used to avoid components keeping the last cursor icon
/// that's been set (which is usually the resize cursor or loading cursor)
var defaultCursor: win32.HCURSOR = undefined;

var hasInit: bool = false;

pub fn init() !void {
Expand Down Expand Up @@ -61,6 +65,10 @@ pub fn init() !void {
ncMetrics.cbSize = @sizeOf(win32.NONCLIENTMETRICSA);
_ = win32.SystemParametersInfoA(win32.SPI_GETNONCLIENTMETRICS, @sizeOf(win32.NONCLIENTMETRICSA), &ncMetrics, 0);
captionFont = win32.CreateFontIndirectA(&ncMetrics.lfCaptionFont).?;

// Load the default arrow cursor so that components can use it
// This avoids components keeping the last cursor (resize cursor or loading cursor)
defaultCursor = win32.LoadCursorA(null, win32.IDC_ARROW);
}
}

Expand Down Expand Up @@ -126,7 +134,7 @@ pub const Window = struct {
.cbWndExtra = 0,
.hInstance = hInst,
.hIcon = null, // TODO: LoadIcon
.hCursor = null, // TODO: LoadCursor
.hCursor = defaultCursor,
.hbrBackground = win32.GetSysColorBrush(win32.COLOR_3DFACE),
.lpszMenuName = null,
.lpszClassName = className,
Expand Down Expand Up @@ -516,22 +524,22 @@ pub const Canvas = struct {
.cbWndExtra = 0,
.hInstance = hInst,
.hIcon = null, // TODO: LoadIcon
.hCursor = null, // TODO: LoadCursor
.hCursor = defaultCursor,
.hbrBackground = null,
.lpszMenuName = null,
.lpszClassName = "zgtCanvasClass",
.lpszClassName = "capyCanvasClass",
.hIconSm = null,
};

if ((try win32.registerClassExA(&wc)) == 0) {
showNativeMessageDialog(.Error, "Could not register window class {s}", .{"zgtCanvasClass"});
showNativeMessageDialog(.Error, "Could not register window class {s}", .{"capyCanvasClass"});
return Win32Error.InitializationError;
}
classRegistered = true;
}

const hwnd = try win32.createWindowExA(win32.WS_EX_LEFT, // dwExtStyle
"zgtCanvasClass", // lpClassName
"capyCanvasClass", // lpClassName
"", // lpWindowName
win32.WS_TABSTOP | win32.WS_CHILD, // dwStyle
10, // X
Expand Down Expand Up @@ -722,22 +730,22 @@ pub const TabContainer = struct {
.lpfnWndProc = TabContainer.process,
.hInstance = hInst,
.hIcon = null, // TODO: LoadIcon
.hCursor = null, // TODO: LoadCursor
.hCursor = defaultCursor,
.hbrBackground = null,
.lpszMenuName = null,
.lpszClassName = "zgtTabClass",
.lpszClassName = "capyTabClass",
.hIconSm = null,
};

if ((try win32.registerClassExA(&wc)) == 0) {
showNativeMessageDialog(.Error, "Could not register window class zgtTabClass", .{});
showNativeMessageDialog(.Error, "Could not register window class capyTabClass", .{});
return Win32Error.InitializationError;
}
classRegistered = true;
}

const wrapperHwnd = try win32.createWindowExA(win32.WS_EX_LEFT, // dwExtStyle
"zgtTabClass", // lpClassName
"capyTabClass", // lpClassName
"", // lpWindowName
win32.WS_TABSTOP | win32.WS_CHILD | win32.WS_CLIPCHILDREN, // dwStyle
10, // X
Expand Down Expand Up @@ -818,22 +826,22 @@ pub const Container = struct {
.cbWndExtra = 0,
.hInstance = hInst,
.hIcon = null, // TODO: LoadIcon
.hCursor = null, // TODO: LoadCursor
.hCursor = defaultCursor,
.hbrBackground = null,
.lpszMenuName = null,
.lpszClassName = "zgtContainerClass",
.lpszClassName = "capyContainerClass",
.hIconSm = null,
};

if ((try win32.registerClassExA(&wc)) == 0) {
showNativeMessageDialog(.Error, "Could not register window class {s}", .{"zgtContainerClass"});
showNativeMessageDialog(.Error, "Could not register window class {s}", .{"capyContainerClass"});
return Win32Error.InitializationError;
}
classRegistered = true;
}

const hwnd = try win32.createWindowExA(win32.WS_EX_LEFT, // dwExtStyle
"zgtContainerClass", // lpClassName
"capyContainerClass", // lpClassName
"", // lpWindowName
win32.WS_TABSTOP | win32.WS_CHILD | win32.WS_CLIPCHILDREN, // dwStyle
10, // X
Expand Down
5 changes: 5 additions & 0 deletions src/backends/win32/win32.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub const HDC = std.os.windows.HDC;
pub const HBRUSH = std.os.windows.HBRUSH;
pub const HMENU = std.os.windows.HMENU;
pub const HFONT = *opaque {};
pub const HCURSOR = std.os.windows.HCURSOR;
pub const COLORREF = std.os.windows.DWORD;
pub const BOOL = std.os.windows.BOOL;
pub const BYTE = std.os.windows.BYTE;
Expand Down Expand Up @@ -47,6 +48,9 @@ pub const EN_CHANGE = 0x0300;

pub const SPI_GETNONCLIENTMETRICS = 0x0029;

/// Standard arrow cursor.
pub const IDC_ARROW = @intToPtr([*:0]const u8, 32512);

pub const WNDENUMPROC = fn (hwnd: HWND, lParam: LPARAM) callconv(WINAPI) c_int;

pub extern "user32" fn SendMessageA(hWnd: HWND, Msg: UINT, wParam: WPARAM, lParam: LPARAM) callconv(WINAPI) LRESULT;
Expand Down Expand Up @@ -85,6 +89,7 @@ pub extern "user32" fn InvalidateRect(hWnd: HWND, lpRect: *const RECT, bErase: B
pub extern "user32" fn GetWindowExtEx(hdc: HDC, lpsize: *SIZE) callconv(WINAPI) BOOL;
pub extern "user32" fn EnableWindow(hWnd: HWND, enable: BOOL) callconv(WINAPI) BOOL;
pub extern "user32" fn SystemParametersInfoA(uiAction: UINT, uiParam: UINT, pvParam: ?*anyopaque, fWinIni: UINT) callconv(WINAPI) BOOL;
pub extern "user32" fn LoadCursorA(hInst: ?HINSTANCE, lpCursorName: std.os.windows.LPCSTR) callconv(WINAPI) HCURSOR;

// stock objects constants
pub const WHITE_BRUSH = 0;
Expand Down

0 comments on commit af433af

Please sign in to comment.