2015-04-06 16:41:33 -05:00
|
|
|
// 6 april 2015
|
2015-04-06 23:26:27 -05:00
|
|
|
#include "uipriv_windows.h"
|
2015-04-06 16:41:33 -05:00
|
|
|
|
2015-04-07 18:32:16 -05:00
|
|
|
intmax_t uiWindowsWindowTextWidth(HWND hwnd)
|
|
|
|
{
|
2015-04-11 08:39:04 -05:00
|
|
|
LRESULT len;
|
2015-04-07 18:32:16 -05:00
|
|
|
WCHAR *text;
|
|
|
|
HDC dc;
|
|
|
|
HFONT prevfont;
|
2015-04-07 18:36:46 -05:00
|
|
|
SIZE size;
|
2015-04-07 18:32:16 -05:00
|
|
|
|
2015-04-08 19:14:10 -05:00
|
|
|
size.cx = 0;
|
|
|
|
size.cy = 0;
|
2015-04-11 08:39:04 -05:00
|
|
|
|
|
|
|
// first we need the window text
|
|
|
|
len = SendMessageW(hwnd, WM_GETTEXTLENGTH, 0, 0);
|
2015-04-07 18:32:16 -05:00
|
|
|
if (len == 0) // no text; nothing to do
|
|
|
|
return 0;
|
2015-04-07 22:40:18 -05:00
|
|
|
text = (WCHAR *) uiAlloc((len + 1) * sizeof (WCHAR), "WCHAR[]");
|
2015-04-11 08:39:04 -05:00
|
|
|
// note the comparison: the size includes the null terminator, but the return does not
|
|
|
|
if (GetWindowText(hwnd, text, len + 1) != len)
|
2015-04-07 18:32:16 -05:00
|
|
|
logLastError("error getting window text in uiWindowsWindowTextWidth()");
|
2015-04-11 08:39:04 -05:00
|
|
|
|
|
|
|
// now we can do the calculations
|
2015-04-07 18:36:46 -05:00
|
|
|
dc = GetDC(hwnd);
|
2015-04-07 18:32:16 -05:00
|
|
|
if (dc == NULL)
|
|
|
|
logLastError("error getting DC in uiWindowsWindowTextWidth()");
|
2015-04-07 18:36:46 -05:00
|
|
|
prevfont = (HFONT) SelectObject(dc, hMessageFont);
|
|
|
|
if (prevfont == NULL)
|
2015-04-07 18:32:16 -05:00
|
|
|
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()");
|
2015-04-07 18:36:46 -05:00
|
|
|
if (SelectObject(dc, prevfont) != hMessageFont)
|
2015-04-07 18:32:16 -05:00
|
|
|
logLastError("error restoring previous font into device context in uiWindowsWindowTextWidth()");
|
2015-04-07 18:36:46 -05:00
|
|
|
if (ReleaseDC(hwnd, dc) == 0)
|
2015-04-07 18:32:16 -05:00
|
|
|
logLastError("error releasing DC in uiWindowsWindowTextWidth()");
|
|
|
|
uiFree(text);
|
2015-04-11 08:39:04 -05:00
|
|
|
|
2015-04-07 18:32:16 -05:00
|
|
|
return size.cx;
|
|
|
|
}
|