From 643ed46b00eee72ab3a0e0856e9bc494f7eb227b Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 3 May 2015 11:55:34 -0400 Subject: [PATCH] Cleaned up windows/container.c resize(). More TODOs. --- TODO.md | 1 + windows/container.c | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/TODO.md b/TODO.md index b2512cd5..af668b00 100644 --- a/TODO.md +++ b/TODO.md @@ -41,6 +41,7 @@ - determine whether or not margins count in preferredSize() when there is no main control - menu item state change while the menu is visible (not in response to user action) - figure out where we should return HRESULTs +- Windows: don't abort if a cleanup function fails? ultimately: - add some sort of runtime type checking diff --git a/windows/container.c b/windows/container.c index ae09a8ff..0a119a3d 100644 --- a/windows/container.c +++ b/windows/container.c @@ -16,7 +16,8 @@ struct container { #define winXPadding 4 #define winYPadding 4 -static void resize(uiContainer *cc, RECT *r) +// abort the resize if something fails and we don't have what we need to do a resize +static HRESULT resize(uiContainer *cc, RECT *r) { struct container *c = (struct container *) (uiControl(cc)->Internal); uiSizing d; @@ -26,27 +27,28 @@ static void resize(uiContainer *cc, RECT *r) TEXTMETRICW tm; SIZE size; - // TODO clean this up a bit - size.cx = 0; - size.cy = 0; - ZeroMemory(&tm, sizeof (TEXTMETRICW)); dc = GetDC(c->hwnd); if (dc == NULL) - logLastError("error getting DC in resize()"); + return logLastError("error getting DC in resize()"); prevfont = (HFONT) SelectObject(dc, hMessageFont); if (prevfont == NULL) - logLastError("error loading control font into device context in resize()"); + return logLastError("error loading control font into device context in resize()"); + + ZeroMemory(&tm, sizeof (TEXTMETRICW)); if (GetTextMetricsW(dc, &tm) == 0) - logLastError("error getting text metrics in resize()"); + return logLastError("error getting text metrics in resize()"); if (GetTextExtentPoint32W(dc, L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52, &size) == 0) - logLastError("error getting text extent point in resize()"); + return logLastError("error getting text extent point in resize()"); + sys.baseX = (int) ((size.cx / 26 + 1) / 2); sys.baseY = (int) tm.tmHeight; sys.internalLeading = tm.tmInternalLeading; + if (SelectObject(dc, prevfont) != hMessageFont) - logLastError("error restoring previous font into device context in resize()"); + return logLastError("error restoring previous font into device context in resize()"); if (ReleaseDC(c->hwnd, dc) == 0) - logLastError("error releasing DC in resize()"); + return logLastError("error releasing DC in resize()"); + d.xPadding = uiDlgUnitsToX(winXPadding, sys.baseX); d.yPadding = uiDlgUnitsToY(winYPadding, sys.baseY); d.sys = &sys;