Migrated util.c.

This commit is contained in:
Pietro Gagliardi 2016-04-21 23:05:10 -04:00
parent f3cf43da21
commit 63ce320021
3 changed files with 54 additions and 21 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);
}