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 // 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 // TODO document
_UI_EXTERN void uiWindowsRegisterWM_COMMANDHandler(HWND hwnd, BOOL (*handler)(uiControl *, HWND, WORD, LRESULT *), uiControl *c); _UI_EXTERN void uiWindowsRegisterWM_COMMANDHandler(HWND hwnd, BOOL (*handler)(uiControl *, HWND, WORD, LRESULT *), uiControl *c);
_UI_EXTERN void uiWindowsUnregisterWM_COMMANDHandler(HWND hwnd); _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) #define logHRESULT(s, hr) _logHRESULT(L ## __FILE__, __LINE__, L ## __func__, s, hr)
extern void _implbug(debugargs, const WCHAR *format, ...); extern void _implbug(debugargs, const WCHAR *format, ...);
#define implbug(...) _implbug(L ## __FILE__, __LINE__, L ## __func__, __VA_LIST__) #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; WCHAR *curname;
int i; int i;
if (GetClassNameW(hwnd, classname, maxClassName) == 0) if (GetClassNameW(hwnd, classname, maxClassName) == 0) {
logLastError("error getting name of window class in windowClassOf()"); logLastError(L"error getting name of window class");
// assume no match on error, just to be safe
return -1;
}
va_start(ap, hwnd); va_start(ap, hwnd);
i = 0; i = 0;
for (;;) { for (;;) {
@ -34,30 +37,22 @@ int windowClassOf(HWND hwnd, ...)
return -1; 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 // wrapper around MapWindowRect() that handles the complex error handling
void mapWindowRect(HWND from, HWND to, RECT *r) void mapWindowRect(HWND from, HWND to, RECT *r)
{ {
RECT prevr;
DWORD le; DWORD le;
prevr = *r;
SetLastError(0); SetLastError(0);
if (MapWindowRect(from, to, r) == 0) { if (MapWindowRect(from, to, r) == 0) {
le = GetLastError(); le = GetLastError();
SetLastError(le); // just to be safe SetLastError(le); // just to be safe
if (le != 0) if (le != 0) {
logLastError("error calling MapWindowRect() in mapWindowRect()"); 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) void uiWindowsEnsureDestroyWindow(HWND hwnd)
{ {
if (DestroyWindow(hwnd) == 0) 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) void uiWindowsEnsureSetParent(HWND hwnd, HWND parent)
{ {
if (SetParent(hwnd, parent) == 0) 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) 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.top = 0;
window.right = *width; window.right = *width;
window.bottom = *height; window.bottom = *height;
if (AdjustWindowRectEx(&window, getStyle(hwnd), hasMenubar, getExStyle(hwnd)) == 0) if (AdjustWindowRectEx(&window, getStyle(hwnd), hasMenubar, getExStyle(hwnd)) == 0) {
logLastError("error getting real window coordinates in clientSizeToWindowSize()"); 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) { if (hasMenubar) {
RECT temp; RECT temp;
@ -121,3 +124,13 @@ void clientSizeToWindowSize(HWND hwnd, intmax_t *width, intmax_t *height, BOOL h
*width = window.right - window.left; *width = window.right - window.left;
*height = window.bottom - window.top; *height = window.bottom - window.top;
} }
HWND parentOf(HWND child)
{
return GetAncestor(child, GA_PARENT);
}
HWND parentToplevel(HWND child)
{
return GetAncestor(child, GA_ROOT);
}