From 63ce320021ede343a3e48d591fd91d676758d7fe Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Thu, 21 Apr 2016 23:05:10 -0400 Subject: [PATCH] Migrated util.c. --- windows/ui_windows_new.h | 9 ++++++ windows/uipriv_windows_new.hpp | 11 +++++++ windows/{util.c => winutil.cpp} | 55 ++++++++++++++++++++------------- 3 files changed, 54 insertions(+), 21 deletions(-) rename windows/{util.c => winutil.cpp} (75%) diff --git a/windows/ui_windows_new.h b/windows/ui_windows_new.h index e372339e..e02e64e0 100644 --- a/windows/ui_windows_new.h +++ b/windows/ui_windows_new.h @@ -1,5 +1,14 @@ // 21 april 2016 +// TODO document +_UI_EXTERN void uiWindowsEnsureDestroyWindow(HWND hwnd); + +// TODO document +_UI_EXTERN void uiWindowsEnsureSetParent(HWND hwnd, HWND parent); + +// TODO document +_UI_EXTERN void uiWindowsEnsureAssignControlIDZOrder(HWND hwnd, LONG_PTR controlID, HWND insertAfter); + // TODO document _UI_EXTERN void uiWindowsRegisterWM_COMMANDHandler(HWND hwnd, BOOL (*handler)(uiControl *, HWND, WORD, LRESULT *), uiControl *c); _UI_EXTERN void uiWindowsUnregisterWM_COMMANDHandler(HWND hwnd); diff --git a/windows/uipriv_windows_new.hpp b/windows/uipriv_windows_new.hpp index 0d8b25a5..f698f4e2 100644 --- a/windows/uipriv_windows_new.hpp +++ b/windows/uipriv_windows_new.hpp @@ -24,3 +24,14 @@ extern HRESULT _logHRESULT(debugargs, const WCHAR *s, HRESULT hr); #define logHRESULT(s, hr) _logHRESULT(L ## __FILE__, __LINE__, L ## __func__, s, hr) extern void _implbug(debugargs, const WCHAR *format, ...); #define implbug(...) _implbug(L ## __FILE__, __LINE__, L ## __func__, __VA_LIST__) + +// winutil.cpp +extern int windowClassOf(HWND hwnd, ...); +extern void mapWindowRect(HWND from, HWND to, RECT *r); +extern DWORD getStyle(HWND hwnd); +extern void setStyle(HWND hwnd, DWORD style); +extern DWORD getExStyle(HWND hwnd); +extern void setExStyle(HWND hwnd, DWORD exstyle); +extern void clientSizeToWindowSize(HWND hwnd, intmax_t *width, intmax_t *height, BOOL hasMenubar); +extern HWND parentOf(HWND child); +extern HWND parentToplevel(HWND child); diff --git a/windows/util.c b/windows/winutil.cpp similarity index 75% rename from windows/util.c rename to windows/winutil.cpp index ef31b7b4..07051a0a 100644 --- a/windows/util.c +++ b/windows/winutil.cpp @@ -15,8 +15,11 @@ int windowClassOf(HWND hwnd, ...) WCHAR *curname; int i; - if (GetClassNameW(hwnd, classname, maxClassName) == 0) - logLastError("error getting name of window class in windowClassOf()"); + if (GetClassNameW(hwnd, classname, maxClassName) == 0) { + logLastError(L"error getting name of window class"); + // assume no match on error, just to be safe + return -1; + } va_start(ap, hwnd); i = 0; for (;;) { @@ -34,30 +37,22 @@ int windowClassOf(HWND hwnd, ...) return -1; } -void complain(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - fprintf(stderr, "[libui] "); - vfprintf(stderr, fmt, ap); - fprintf(stderr, "\n"); - va_end(ap); - DebugBreak(); - abort(); // just in case -} - // wrapper around MapWindowRect() that handles the complex error handling void mapWindowRect(HWND from, HWND to, RECT *r) { + RECT prevr; DWORD le; + prevr = *r; SetLastError(0); if (MapWindowRect(from, to, r) == 0) { le = GetLastError(); SetLastError(le); // just to be safe - if (le != 0) - logLastError("error calling MapWindowRect() in mapWindowRect()"); + if (le != 0) { + logLastError(L"error calling MapWindowRect()"); + // restore original rect on error, just in case + *r = prevr; + } } } @@ -84,13 +79,15 @@ void setExStyle(HWND hwnd, DWORD exstyle) void uiWindowsEnsureDestroyWindow(HWND hwnd) { if (DestroyWindow(hwnd) == 0) - logLastError("error destroying window in uiWindowsEnsureDestroyWindow"); + logLastError(L"error destroying window"); } +// TODO allow passing NULL to indicate no parent +// this would allow for custom containers void uiWindowsEnsureSetParent(HWND hwnd, HWND parent) { if (SetParent(hwnd, parent) == 0) - logLastError("error setting window parent in uiWindowsEnsureSetParent"); + logLastError(L"error setting window parent in uiWindowsEnsureSetParent"); } void uiWindowsEnsureAssignControlIDZOrder(HWND hwnd, LONG_PTR controlID, HWND insertAfter) @@ -108,8 +105,14 @@ void clientSizeToWindowSize(HWND hwnd, intmax_t *width, intmax_t *height, BOOL h window.top = 0; window.right = *width; window.bottom = *height; - if (AdjustWindowRectEx(&window, getStyle(hwnd), hasMenubar, getExStyle(hwnd)) == 0) - logLastError("error getting real window coordinates in clientSizeToWindowSize()"); + if (AdjustWindowRectEx(&window, getStyle(hwnd), hasMenubar, getExStyle(hwnd)) == 0) { + logLastError("error getting adjusted window rect"); + // on error, don't give up; the window will be smaller but whatever + window.left = 0; + window.top = 0; + window.right = *width; + window.bottom = *height; + } if (hasMenubar) { RECT temp; @@ -121,3 +124,13 @@ void clientSizeToWindowSize(HWND hwnd, intmax_t *width, intmax_t *height, BOOL h *width = window.right - window.left; *height = window.bottom - window.top; } + +HWND parentOf(HWND child) +{ + return GetAncestor(child, GA_PARENT); +} + +HWND parentToplevel(HWND child) +{ + return GetAncestor(child, GA_ROOT); +}