diff options
| author | Pietro Gagliardi <[email protected]> | 2015-04-07 19:32:16 -0400 |
|---|---|---|
| committer | Pietro Gagliardi <[email protected]> | 2015-04-07 19:32:16 -0400 |
| commit | 42204af0865cd6c712b1f9b1e20694c1fc971503 (patch) | |
| tree | 6e398429026632df2067a281c14dbe57970fec3a /new/util_windows.c | |
| parent | 38eecd3fc3538c597a032c940519ffdb78cffd82 (diff) | |
Implemented uiButton.preferredSize() on Windows. This includes adding a function to get the width of text in a control.
Diffstat (limited to 'new/util_windows.c')
| -rw-r--r-- | new/util_windows.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/new/util_windows.c b/new/util_windows.c index 0bd76a0..c0d3d6c 100644 --- a/new/util_windows.c +++ b/new/util_windows.c @@ -16,3 +16,34 @@ WCHAR *toUTF16(const char *str) logLastError("error converting from UTF-8 to UTF-16 in toUTF16()"); return wstr; } + +// TODO this and resize(): initialize size and other values to avoid garbage on failure +intmax_t uiWindowsWindowTextWidth(HWND hwnd) +{ + int len; + WCHAR *text; + HDC dc; + HFONT prevfont; + + // TODO check for error + len = GetWindowTextLengthW(hwnd); + if (len == 0) // no text; nothing to do + return 0; + text = (WCHAR *) uiAlloc((len + 1) * sizeof (WCHAR)); + if (GetWindowText(hwnd, text, len + 1) == 0) // should only happen on error given explicit test for len == 0 above + logLastError("error getting window text in uiWindowsWindowTextWidth()"); + dc = GetDC(parent); + if (dc == NULL) + logLastError("error getting DC in uiWindowsWindowTextWidth()"); + prevFont = (HFONT) SelectObject(dc, hMessageFont); + if (prevFont == NULL) + logLastError("error loading control font into device context in uiWindowsWindowTextWidth()"); + if (GetTextExtentPoint32W(dc, text, len, &size) == 0) + logLastError("error getting text extent point in uiWindowsWindowTextWidth()"); + if (SelectObject(dc, prevFont) != hMessageFont) + logLastError("error restoring previous font into device context in uiWindowsWindowTextWidth()"); + if (ReleaseDC(parent, dc) == 0) + logLastError("error releasing DC in uiWindowsWindowTextWidth()"); + uiFree(text); + return size.cx; +} |
