From fecb7caa14e3180f854217c39c505667f07c31f1 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 7 Apr 2015 03:39:47 -0400 Subject: [PATCH] Hooked up resizing. Now to make a control that uses this. --- ui_windows.h | 4 ++++ uipriv_windows.h | 1 + util_windows.c | 29 +++++++++++++++++++++++++++++ window_windows.c | 12 ++++++++++++ 4 files changed, 46 insertions(+) diff --git a/ui_windows.h b/ui_windows.h index 330b7cba..6186a619 100644 --- a/ui_windows.h +++ b/ui_windows.h @@ -33,4 +33,8 @@ struct uiWindowsNewControlParams { }; uiControl *uiWindowsNewControl(uiWindowsNewControlParams *); +// use these in your preferredSize() implementation with baseX and baseY +#define uiDlgUnitToX(dlg, baseX) MulDiv((dlg), baseX, 4) +#define uiDlgUnitToY(dlg, baseY) MulDiv((dlg), baseY, 8) + #endif diff --git a/uipriv_windows.h b/uipriv_windows.h index c7527c0d..d415100a 100644 --- a/uipriv_windows.h +++ b/uipriv_windows.h @@ -64,6 +64,7 @@ extern HWND initialParent; // util_windows.c extern WCHAR *toUTF16(const char *); extern BOOL sharedWndProc(HWND, UINT, WPARAM, LPARAM, LRESULT *); +extern void resize(uiControl *, HWND, RECT); // comctl32_windows.c extern BOOL (*WINAPI fv_SetWindowSubclass)(HWND, SUBCLASSPROC, UINT_PTR, DWORD_PTR); diff --git a/util_windows.c b/util_windows.c index 62929673..39bffc36 100644 --- a/util_windows.c +++ b/util_windows.c @@ -70,3 +70,32 @@ BOOL sharedWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT * */ } return FALSE; } + +// TODO add function names +void resize(uiControl *control, HWND parent, RECT r) +{ + uiSizing d; + HDC dc; + HFONT prevFont; + TEXTMETRICW tm; + SIZE size; + + dc = GetDC(parent); + if (dc == NULL) + logLastError("error getting DC for preferred size calculations"); + prevFont = (HFONT) SelectObject(dc, hMessageFont); + if (prevFont == NULL) + logLastError("error loading control font into device context for preferred size calculation"); + if (GetTextMetricsW(dc, &tm) == 0) + logLastError("error getting text metrics for preferred size calculations"); + if (GetTextExtentPoint32W(dc, L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52, &size) == 0) + logLastError("error getting text extent point for preferred size calculations"); + d.baseX = (int) ((size.cx / 26 + 1) / 2); + d.baseY = (int) tm.tmHeight; + d.internalLeading = tm.tmInternalLeading; + if (SelectObject(dc, prevFont) != hMessageFont) + logLastError("error restoring previous font into device context after preferred size calculations"); + if (ReleaseDC(parent, dc) == 0) + logLastError("error releasing DC for preferred size calculations"); + (*(control->resize))(control, r.left, r.top, r.right - r.left, r.bottom - r.top, &d); +} diff --git a/window_windows.c b/window_windows.c index c0ca6c35..d5f675e2 100644 --- a/window_windows.c +++ b/window_windows.c @@ -3,6 +3,7 @@ struct uiWindow { HWND hwnd; + uiControl *child; BOOL shownOnce; int (*onClosing)(uiWindow *, void *); void *onClosingData; @@ -15,6 +16,8 @@ static LRESULT CALLBACK uiWindowWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPA uiWindow *w; CREATESTRUCTW *cs = (CREATESTRUCTW *) lParam; LRESULT lResult; + WINDOWPOS *wp = (WINDOWPOS *) lParam; + RECT r; w = (uiWindow *) GetWindowLongPtrW(hwnd, GWLP_USERDATA); if (w == NULL) { @@ -26,6 +29,15 @@ static LRESULT CALLBACK uiWindowWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPA if (sharedWndProc(hwnd, uMsg, wParam, lParam, &lResult) != FALSE) return lResult; switch (uMsg) { + case WM_WINDOWPOSCHANGING: + if (w->child == NULL) + break; + if ((wp->flags & SWP_NOSIZE) != 0) + break; + if (GetClientRect(w->hwnd, &r) == 0) + logLastError("error getting window client rect for resize in uiWindowWndProc()"); + resize(w->child, w->hwnd, r); + return 0; case WM_CLOSE: if (!(*(w->onClosing))(w, w->onClosingData)) return 0;