Windows resize work.

This commit is contained in:
Pietro Gagliardi 2015-05-17 21:15:39 -04:00
parent f5f38d1fa5
commit 7c5459ff01
5 changed files with 46 additions and 178 deletions

View File

@ -78,6 +78,7 @@ interface Window from Control {
func SetChild(c *Control);
func Margined(void) int;
func SetMargined(margined int);
func ResizeChild(void);
};
func NewWindow(title *const char, width int, height int, hasMenubar int) *Window;

View File

@ -70,20 +70,6 @@ static void onDestroy(void *data)
// do nothing
}
static void containerComputeChildSize(uiControl *c, intmax_t *x, intmax_t *y, intmax_t *width, intmax_t *height, uiSizing *d)
{
HWND hwnd;
RECT r;
hwnd = (HWND) uiControlHandle(c);
if (GetClientRect(hwnd, &r) == 0)
logLastError("error getting container client rect in containerComputeChildSize()");
*x = r.left;
*y = r.top;
*width = r.right - r.left;
*height = r.bottom - r.top;
}
void uiMakeContainer(uiControl *c)
{
uiWindowsMakeControlParams p;
@ -100,6 +86,4 @@ void uiMakeContainer(uiControl *c)
p.onDestroy = onDestroy;
p.onDestroyData = NULL;
uiWindowsMakeControl(c, &p);
uiControl(c)->ComputeChildSize = containerComputeChildSize;
}

View File

@ -20,6 +20,14 @@ void queueResize(uiControl *c)
uintmax_t i;
uiControl *d;
// resizing a control requires us to reocmpute the sizes of everything in the top-level window
for (;;) {
if (uiIsWindow(c))
break;
if (uiControlParent(c) == NULL) // not in a window; don't bother resizing
return;
c = uiControlParent(c);
}
// make sure we're only queued once
for (i = 0 ; i < resizes->len; i++) {
d = ptrArrayIndex(resizes, uiControl *, i);
@ -33,23 +41,14 @@ void queueResize(uiControl *c)
void doResizes(void)
{
uiControl *c, *parent;
intmax_t x, y, width, height;
uiSizing d;
uiSizingSys sys;
uiWindow *w;
HWND hwnd;
while (resizes->len != 0) {
c = ptrArrayIndex(resizes, uiControl *, 0);
w = ptrArrayIndex(resizes, uiWindow *, 0);
ptrArrayDelete(resizes, 0);
parent = uiControlParent(c);
if (parent == NULL) // not in a parent; can't resize
continue; // this is for uiBox, etc.
d.Sys = &sys;
uiControlGetSizing(parent, &d);
uiControlComputeChildSize(parent, &x, &y, &width, &height, &d);
uiControlResize(c, x, y, width, height, &d);
hwnd = (HWND) uiControlHandle(c);
uiWindowResizeChild(w);
hwnd = (HWND) uiControlHandle(uiControl(w));
// we used SWP_NOREDRAW; we need to queue a redraw ourselves
// TODO use RedrawWindow() to bypass WS_CLIPCHILDREN complications
if (InvalidateRect(hwnd, NULL, TRUE) == 0)

View File

@ -1,121 +0,0 @@
// 14 may 2015
#include "uipriv_windows.h"
static struct ptrArray *resizes;
void initResizes(void)
{
resizes = newPtrArray();
}
void uninitResizes(void)
{
while (resizes->len != 0)
ptrArrayDelete(resizes, 0);
ptrArrayDestroy(resizes);
}
void queueResize(uiControl *c)
{
uintmax_t i;
uiControl *d;
// resizing a control requires us to reocmpute the sizes of everything in the top-level window
for (;;) {
if (uiIsWindow(c))
break;
if (uiControlParent(c) == NULL) // not in a window; don't bother resizing
return;
c = uiControlParent(c);
}
// make sure we're only queued once
for (i = 0 ; i < resizes->len; i++) {
d = ptrArrayIndex(resizes, uiControl *, i);
if (c == d)
return;
}
ptrArrayAppend(resizes, c);
}
// TODO dequeueResize
void doResizes(void)
{
uiControl *c, *parent;
intmax_t x, y, width, height;
uiSizing d;
uiSizingSys sys;
HWND hwnd;
while (resizes->len != 0) {
c = ptrArrayIndex(resizes, uiControl *, 0);
ptrArrayDelete(resizes, 0);
parent = uiControlParent(c);
if (parent == NULL) // not in a parent; can't resize
continue; // this is for uiBox, etc.
d.Sys = &sys;
uiControlGetSizing(parent, &d);
uiControlComputeChildSize(parent, &x, &y, &width, &height, &d);
uiControlResize(c, x, y, width, height, &d);
hwnd = (HWND) uiControlHandle(c);
// we used SWP_NOREDRAW; we need to queue a redraw ourselves
// TODO use RedrawWindow() to bypass WS_CLIPCHILDREN complications
if (InvalidateRect(hwnd, NULL, TRUE) == 0)
logLastError("error redrawing controls after a resize in doResizes()");
}
}
#define swpflags (SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOREDRAW)
void moveWindow(HWND hwnd, intmax_t x, intmax_t y, intmax_t width, intmax_t height)
{
if (SetWindowPos(hwnd, NULL, x, y, width, height, swpflags | SWP_NOZORDER) == 0)
logLastError("error moving window in moveWindow()");
}
void moveAndReorderWindow(HWND hwnd, HWND insertAfter, intmax_t x, intmax_t y, intmax_t width, intmax_t height)
{
if (SetWindowPos(hwnd, insertAfter, x, y, width, height, swpflags) == 0)
logLastError("error moving and reordering window in moveAndReorderWindow()");
}
// from https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing and https://msdn.microsoft.com/en-us/library/windows/desktop/bb226818%28v=vs.85%29.aspx
// this X value is really only for buttons but I don't see a better one :/
#define winXPadding 4
#define winYPadding 4
void uiWindowsGetSizing(uiControl *c, uiSizing *d)
{
HWND hwnd;
HDC dc;
HFONT prevfont;
TEXTMETRICW tm;
SIZE size;
hwnd = (HWND) uiControlHandle(c);
dc = GetDC(hwnd);
if (dc == NULL)
logLastError("error getting DC in uiWindowsGetSizing()");
prevfont = (HFONT) SelectObject(dc, hMessageFont);
if (prevfont == NULL)
logLastError("error loading control font into device context in uiWindowsGetSizing()");
ZeroMemory(&tm, sizeof (TEXTMETRICW));
if (GetTextMetricsW(dc, &tm) == 0)
logLastError("error getting text metrics in uiWindowsGetSizing()");
if (GetTextExtentPoint32W(dc, L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52, &size) == 0)
logLastError("error getting text extent point in uiWindowsGetSizing()");
d->Sys->BaseX = (int) ((size.cx / 26 + 1) / 2);
d->Sys->BaseY = (int) tm.tmHeight;
d->Sys->InternalLeading = tm.tmInternalLeading;
if (SelectObject(dc, prevfont) != hMessageFont)
logLastError("error restoring previous font into device context in uiWindowsGetSizing()");
if (ReleaseDC(hwnd, dc) == 0)
logLastError("error releasing DC in uiWindowsGetSizing()");
d->XPadding = uiWindowsDlgUnitsToX(winXPadding, d->Sys->BaseX);
d->YPadding = uiWindowsDlgUnitsToY(winYPadding, d->Sys->BaseY);
}

View File

@ -41,8 +41,10 @@ static LRESULT CALLBACK windowWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARA
case WM_WINDOWPOSCHANGED:
if ((wp->flags & SWP_NOSIZE) != 0)
break;
// wine sends this early so we have to guard
// TODO does real windows?
if (w->child != NULL)
uiControlQueueResize(w->child);
uiControlQueueResize(uiControl(w));
return 0;
case WM_CLOSE:
if ((*(w->onClosing))(uiWindow(w), w->onClosingData))
@ -128,7 +130,7 @@ static void windowResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, i
static void windowQueueResize(uiControl *c)
{
complain("attempt to queue a resize of the uiWindow at %p", c);
queueResize(c);
}
static void windowGetSizing(uiControl *c, uiSizing *d)
@ -136,25 +138,6 @@ static void windowGetSizing(uiControl *c, uiSizing *d)
uiWindowsGetSizing(c, d);
}
// from https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
#define windowMargin 7
static void windowComputeChildSize(uiControl *c, intmax_t *x, intmax_t *y, intmax_t *width, intmax_t *height, uiSizing *d)
{
struct window *w = (struct window *) c;
RECT r;
if (GetClientRect(w->hwnd, &r) == 0)
logLastError("error getting uiWindow client rect in windowComputeChildSize()");
*x = r.left;
*y = r.top;
*width = r.right - r.left;
*height = r.bottom - r.top;
if (w->margined) {
// TODO
}
}
static int windowContainerVisible(uiControl *c)
{
complain("attempt to get container visibility state of uiWindow %p", c);
@ -171,8 +154,7 @@ static void windowShow(uiControl *c)
}
w->shownOnce = TRUE;
// make sure the child is the correct size
if (w->child != NULL)
uiControlQueueResize(w->child);
uiControlQueueResize(uiControl(w));
ShowWindow(w->hwnd, nCmdShow);
if (UpdateWindow(w->hwnd) == 0)
logLastError("error calling UpdateWindow() after showing uiWindow for the first time in windowShow()");
@ -272,8 +254,10 @@ static void windowSetChild(uiWindow *ww, uiControl *child)
if (w->child != NULL)
uiControlSetParent(w->child, NULL);
w->child = child;
uiControlSetParent(w->child, uiControl(w));
uiControlQueueResize(w->child);
if (w->child != NULL) {
uiControlSetParent(w->child, uiControl(w));
uiControlQueueResize(w->child);
}
}
static int windowMargined(uiWindow *ww)
@ -288,8 +272,29 @@ static void windowSetMargined(uiWindow *ww, int margined)
struct window *w = (struct window *) ww;
w->margined = margined;
if (w->child != NULL)
uiControlQueueResize(w->child);
uiControlQueueResize(uiControl(w));
}
// from https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
#define windowMargin 7
static void windowResizeChild(uiControl *c)
{
struct window *w = (struct window *) c;
RECT r;
uiSizing d;
uiSizingSys sys;
if (w->child == NULL)
return;
if (GetClientRect(w->hwnd, &r) == 0)
logLastError("error getting uiWindow client rect in windowComputeChildSize()");
if (w->margined) {
// TODO
}
d.Sys = &sys;
uiControlGetSizing(uiControl(w), &d);
uiControlResize(w->child, r.left, r.top, r.right - r.left, r.bottom - r.top, &d);
}
// see http://blogs.msdn.com/b/oldnewthing/archive/2003/09/11/54885.aspx and http://blogs.msdn.com/b/oldnewthing/archive/2003/09/13/54917.aspx
@ -364,7 +369,6 @@ uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
uiControl(w)->Resize = windowResize;
uiControl(w)->QueueResize = windowQueueResize;
uiControl(w)->GetSizing = windowGetSizing;
uiControl(w)->ComputeChildSize = windowComputeChildSize;
uiControl(w)->ContainerVisible = windowContainerVisible;
uiControl(w)->Show = windowShow;
uiControl(w)->Hide = windowHide;
@ -383,6 +387,7 @@ uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
uiWindow(w)->SetChild = windowSetChild;
uiWindow(w)->Margined = windowMargined;
uiWindow(w)->SetMargined = windowSetMargined;
uiWindow(w)->ResizeChild = windowResizeChild;
return uiWindow(w);
}