From 3563dd74e7d761e56d04d1ce756532ff5a4a5017 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Wed, 27 Apr 2016 00:54:22 -0400 Subject: [PATCH] Finished migrating window.cpp. A few utility functions came along for the ride. Maybe child.cpp won't be needed...? --- windows/area.cpp | 2 +- windows/areadraw.cpp | 2 +- windows/areaevents.cpp | 2 +- windows/areascroll.cpp | 4 +- windows/container.cpp | 2 +- windows/draw.cpp | 2 +- windows/fontdialog.cpp | 2 +- windows/parent.cpp | 4 +- windows/uipriv_windows.hpp | 2 + windows/window.cpp | 102 ++++++++++++++++++++----------------- windows/winutil.cpp | 25 +++++++++ 11 files changed, 91 insertions(+), 58 deletions(-) diff --git a/windows/area.cpp b/windows/area.cpp index 7801c770..38719519 100644 --- a/windows/area.cpp +++ b/windows/area.cpp @@ -36,7 +36,7 @@ static LRESULT CALLBACK areaWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM if (uMsg == WM_WINDOWPOSCHANGED) { if ((wp->flags & SWP_NOSIZE) != 0) return DefWindowProcW(hwnd, uMsg, wParam, lParam); - if (GetClientRect(a->hwnd, &client) == 0) + if (getClientRect(a->hwnd, &client) == 0) logLastError(L"error getting client rect of uiArea for WM_WINDOWPOSCHANGED handling"); areaDrawOnResize(a, &client); areaScrollOnResize(a, &client); diff --git a/windows/areadraw.cpp b/windows/areadraw.cpp index 4319fb6c..29a6fde6 100644 --- a/windows/areadraw.cpp +++ b/windows/areadraw.cpp @@ -94,7 +94,7 @@ static void onWM_PRINTCLIENT(uiArea *a) { RECT client; - if (GetClientRect(a->hwnd, &client) == 0) + if (getClientRect(a->hwnd, &client) == 0) logLastError(L"error getting client rect"); //TODO doPaint(a, (HDC) wParam, &client); } diff --git a/windows/areaevents.cpp b/windows/areaevents.cpp index 82be7c29..ea88e3cc 100644 --- a/windows/areaevents.cpp +++ b/windows/areaevents.cpp @@ -85,7 +85,7 @@ static void areaMouseEvent(uiArea *a, uintmax_t down, uintmax_t up, WPARAM wPar if (a->capturing) { clientpt.x = GET_X_LPARAM(lParam); clientpt.y = GET_Y_LPARAM(lParam); - if (GetClientRect(a->hwnd, &client) == 0) + if (getClientRect(a->hwnd, &client) == 0) logLastError(L"error getting uiAreaclient rect for mouse crossing on capture on drag"); inClient = PtInRect(&client, clientpt); if (inClient && !a->inside) { diff --git a/windows/areascroll.cpp b/windows/areascroll.cpp index 7f4cec9e..da6f5aa5 100644 --- a/windows/areascroll.cpp +++ b/windows/areascroll.cpp @@ -128,7 +128,7 @@ static void hscrollParams(uiArea *a, struct scrollParams *p) ZeroMemory(p, sizeof (struct scrollParams)); p->pos = &(a->hscrollpos); // TODO get rid of these and replace with points - if (GetClientRect(a->hwnd, &r) == 0) + if (getClientRect(a->hwnd, &r) == 0) logLastError(L"error getting area client rect"); p->pagesize = r.right - r.left; p->length = a->scrollWidth; @@ -174,7 +174,7 @@ static void vscrollParams(uiArea *a, struct scrollParams *p) ZeroMemory(p, sizeof (struct scrollParams)); p->pos = &(a->vscrollpos); - if (GetClientRect(a->hwnd, &r) == 0) + if (getClientRect(a->hwnd, &r) == 0) logLastError(L"error getting area client rect"); p->pagesize = r.bottom - r.top; p->length = a->scrollHeight; diff --git a/windows/container.cpp b/windows/container.cpp index b6d2402d..752bdd0d 100644 --- a/windows/container.cpp +++ b/windows/container.cpp @@ -50,7 +50,7 @@ static LRESULT CALLBACK containerWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LP return 0; // tab controls use this to draw the background of the tab area case WM_PRINTCLIENT: - if (GetClientRect(hwnd, &r) == 0) { + if (getClientRect(hwnd, &r) == 0) { logLastError(L"error getting client rect"); // likewise break; diff --git a/windows/draw.cpp b/windows/draw.cpp index 47ec6c09..0f5f2531 100644 --- a/windows/draw.cpp +++ b/windows/draw.cpp @@ -51,7 +51,7 @@ ID2D1HwndRenderTarget *makeHWNDRenderTarget(HWND hwnd) if (ReleaseDC(hwnd, dc) == 0) logLastError(L"error releasing DC for finding DPI"); - if (GetClientRect(hwnd, &r) == 0) + if (getClientRect(hwnd, &r) == 0) logLastError(L"error getting current size of window"); ZeroMemory(&hprops, sizeof (D2D1_HWND_RENDER_TARGET_PROPERTIES)); diff --git a/windows/fontdialog.cpp b/windows/fontdialog.cpp index 75aa8974..7a140b9f 100644 --- a/windows/fontdialog.cpp +++ b/windows/fontdialog.cpp @@ -495,7 +495,7 @@ static struct fontDialog *beginFontDialog(HWND hwnd, LPARAM lParam) samplePlacement = GetDlgItem(f->hwnd, rcFontSamplePlacement); if (samplePlacement == NULL) logLastError(L"error getting sample placement static control handle"); - if (GetWindowRect(samplePlacement, &(f->sampleRect)) == 0) + if (getWindowRect(samplePlacement, &(f->sampleRect)) == 0) logLastError(L"error getting sample placement"); mapWindowRect(NULL, f->hwnd, &(f->sampleRect)); uiWindowsEnsureDestroyWindow(samplePlacement); diff --git a/windows/parent.cpp b/windows/parent.cpp index 3b727665..40db8edb 100644 --- a/windows/parent.cpp +++ b/windows/parent.cpp @@ -33,7 +33,7 @@ static HRESULT parentDraw(HDC dc, HWND parent, struct parentDraw *pd) { RECT r; - if (GetClientRect(parent, &r) == 0) + if (getClientRect(parent, &r) == 0) return logLastError(L"error getting parent's client rect"); pd->cdc = CreateCompatibleDC(dc); if (pd->cdc == NULL) @@ -86,7 +86,7 @@ static HBRUSH getControlBackgroundBrush(HWND hwnd, HDC dc) // now figure out where the control is relative to the parent so we can align the brush properly // if anything fails, give up and return the brush as-is - if (GetWindowRect(hwnd, &hwndScreenRect) == 0) { + if (getWindowRect(hwnd, &hwndScreenRect) == 0) { logLastError(L"error getting control window rect"); return brush; } diff --git a/windows/uipriv_windows.hpp b/windows/uipriv_windows.hpp index ea5d9639..21f8ac28 100644 --- a/windows/uipriv_windows.hpp +++ b/windows/uipriv_windows.hpp @@ -66,6 +66,8 @@ extern void clientSizeToWindowSize(HWND hwnd, intmax_t *width, intmax_t *height, extern HWND parentOf(HWND child); extern HWND parentToplevel(HWND child); extern void setWindowInsertAfter(HWND hwnd, HWND insertAfter); +extern void getClientRect(HWND, RECT *); +extern void getWindowRect(HWND, RECT *); // text.cpp extern WCHAR *windowTextAndLen(HWND hwnd, LRESULT *len); diff --git a/windows/window.cpp b/windows/window.cpp index 1df64df0..839dfb53 100644 --- a/windows/window.cpp +++ b/windows/window.cpp @@ -16,6 +16,23 @@ struct uiWindow { BOOL hasMenubar; }; +// from https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing +#define windowMargin 7 + +static void windowMargins(uiWindow *w, int *mx, int *my) +{ + uiWindowsSizing sizing; + + *mx = 0; + *my = 0; + if (!w->margined) + return; + uiWindowsGetSizing(w->hwnd, &sizing); + *mx = windowMargin; + *my = windowMargin; + uiWindowsSizingDlgUnitsToPixels(&sizing, mx, my); +} + static void windowRelayout(uiWindow *w) { uiWindow *w = uiWindow(c); @@ -28,20 +45,14 @@ static void windowRelayout(uiWindow *w) return; x = 0; y = 0; - if (GetClientRect(w->hwnd, &r) == 0) - /* TODO */; + getClientRect(w->hwnd, &r); width = r.right - r.left; height = r.bottom - r.top; - if (w->margined) { - uiWindowsGetSizing(w->hwnd, &sizing); - mx = windowMargin; - my = windowMargin; - uiWindowsSizingDlgUnitsToPixels(&sizing, &mx, &my); - x += mx; - y += my; - width -= 2 * mx; - height -= 2 * my; - } + windowMargins(w, &mx, &my); + x += mx; + y += my; + width -= 2 * mx; + height -= 2 * my; // TODO } @@ -202,9 +213,6 @@ uiWindowsControlDefaultSyncEnableState(uiWindow) // TODO uiWindowsControlDefaultSetParentHWND(uiWindow) -// from https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing -#define windowMargin 7 - static void uiWindowMinimumSize(uiWindowsControl *c, intmax_t *width, intmax_t *height) { uiWindow *w = uiWindow(c); @@ -215,15 +223,9 @@ static void uiWindowMinimumSize(uiWindowsControl *c, intmax_t *width, intmax_t * *height = 0; if (w->child != NULL) uiWindowsControlMinimumSize(uiWindowsControl(w->child), width, height); - if (w->margined) { - uiWindowsGetSizing(w->hwnd, &sizing); - mx = windowMargin; - my = windowMargin; - uiWindowsSizingDlgUnitsToPixels(&sizing, &mx, &my); - *width += 2 * mx; - *height += 2 * my; - } - uiWindowsFreeSizing(d); + windowMargins(w, &mx, &my); + *width += 2 * mx; + *height += 2 * my; } static void uiWindowChildMinimumSizeChanged(uiWindowsControl *c) @@ -232,24 +234,28 @@ static void uiWindowChildMinimumSizeChanged(uiWindowsControl *c) intmax_t width, height; RECT r; BOOL needsGrowing; + int mx, my; uiWindowsControlMinimumSize(uiWindowsControl(w->child), &width, &height); - if (GetClientRect(w->hwnd, &r) == 0) - /* TODO */; + windowMargns(w, &mx, &my); + width += 2 * mx; + height += 2 * my; + getClientRect(w->hwnd, &r); // TODO discount margins needsGrowing = FALSE; if ((r.right - r.left) < width) needsGrowing = TRUE; if ((r.bottom - r.top) < height) needsGrowing = TRUE; - if (needsGrowing) - /* TODO */; + if (!needsGrowing) + return; + // TODO figure out what to do with this function + // maybe split it into two so WM_GETMINMAXINFO can use it? + ensureMinimumWindowSize(w); } uiWindowsDefaultAssignControlIDZorder(uiWindow) -///////// TODO CONTINUE HERE - char *uiWindowTitle(uiWindow *w) { return uiWindowsWindowText(w->hwnd); @@ -269,12 +275,21 @@ void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *, void *), void *data) void uiWindowSetChild(uiWindow *w, uiControl *child) { - if (w->child != NULL) - childRemove(w->child); - w->child = newChild(child, uiControl(w), w->hwnd); + LONG_PTR id; + HWND dummy; + if (w->child != NULL) { - childSetSoleControlID(w->child); - childQueueRelayout(w->child); + uiControlSetParent(w->child, NULL); + uiWindowsControlSetParentHWND(uiWindowsControl(w->child), NULL); + } + w->child = child; + if (w->child != NULL) { + uiControlSetParent(w->child, uiControl(w)); + uiWindowsControlSetParentHWND(uiWindowsControl(w->child), w->hwnd); + id = 100; + dummy = NULL; + uiWindowsControlAssignControlIDZOrder(uiWindowsControl(w->child), &id, &dummy); + windowRelayout(w); } } @@ -286,7 +301,7 @@ int uiWindowMargined(uiWindow *w) void uiWindowSetMargined(uiWindow *w, int margined) { w->margined = margined; - uiWindowsControlQueueRelayout(uiWindowsControl(w)); + windowRelayout(w); } // 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 @@ -319,7 +334,7 @@ uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar) WCHAR *wtitle; BOOL hasMenubarBOOL; - w = (uiWindow *) uiNewControl(uiWindow); + uiWindowsNewControl(uiWindow, w); hasMenubarBOOL = FALSE; if (hasMenubar) @@ -354,26 +369,17 @@ uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar) uiWindowOnClosing(w, defaultOnClosing, NULL); - uiWindowsFinishNewControl(w, uiWindow); - uiControl(w)->CommitShow = windowCommitShow; - uiControl(w)->ContainerUpdateState = windowContainerUpdateState; - uiWindowsControl(w)->Relayout = windowRelayout; - uiWindowsControl(w)->ArrangeChildrenControlIDsZOrder = windowArrangeChildrenControlIDsZOrder; - return w; } // this cannot queue a resize because it's called by the resize handler void ensureMinimumWindowSize(uiWindow *w) { - uiWindowsControl *c; intmax_t width, height; RECT r; - c = uiWindowsControl(w); - (*(c->MinimumSize))(c, NULL, &width, &height); - if (GetClientRect(w->hwnd, &r) == 0) - logLastError(L"error getting client rect"); + uiWindowsControlMinimumSize(uiWindowsControl(w), &width, &height); + getClientRect(w->hwnd, &r); if (width < (r.right - r.left)) // preserve width if larger width = r.right - r.left; if (height < (r.bottom - r.top)) // preserve height if larger diff --git a/windows/winutil.cpp b/windows/winutil.cpp index b4fa692e..0cbdbc3e 100644 --- a/windows/winutil.cpp +++ b/windows/winutil.cpp @@ -120,3 +120,28 @@ void setWindowInsertAfter(HWND hwnd, HWND insertAfter) if (SetWindowPos(hwnd, insertAfter, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOSIZE) == 0) logLastError(L"error reordering window"); } + +// do these function even error out in any case other than invalid parameters?! I thought all windows had rects +void getClientRect(HWND hwnd, RECT *r) +{ + if (GetClientRect(hwnd, r) == 0) { + logLastError(L"error getting window client rect"); + // zero out the rect on error just to be safe + r->left = 0; + r->top = 0; + r->right = 0; + r->bottom = 0; + } +} + +void getWindowRect(HWND hwnd, RECT *r) +{ + if (GetWindowRect(hwnd, r) == 0) { + logLastError(L"error getting window rect"); + // zero out the rect on error just to be safe + r->left = 0; + r->top = 0; + r->right = 0; + r->bottom = 0; + } +}