2014-07-17 21:47:04 -05:00
|
|
|
/* 17 july 2014 */
|
|
|
|
|
|
|
|
#include "winapi_windows.h"
|
2014-07-17 23:29:15 -05:00
|
|
|
#include "_cgo_export.h"
|
2014-07-17 21:47:04 -05:00
|
|
|
|
2014-08-01 22:36:13 -05:00
|
|
|
/* TODO rename to sizer_windows.c and move all but the first function to control_windows.c */
|
|
|
|
|
2014-07-28 13:43:34 -05:00
|
|
|
BOOL baseUnitsCalculated = FALSE;
|
|
|
|
int baseX;
|
|
|
|
int baseY;
|
|
|
|
|
|
|
|
/* called by newWindow() so we can calculate base units when we have a window */
|
|
|
|
void calculateBaseUnits(HWND hwnd)
|
2014-07-17 21:47:04 -05:00
|
|
|
{
|
|
|
|
HDC dc;
|
2014-07-28 13:43:34 -05:00
|
|
|
HFONT prevFont;
|
|
|
|
TEXTMETRICW tm;
|
2014-07-17 21:47:04 -05:00
|
|
|
|
2014-07-28 13:43:34 -05:00
|
|
|
if (baseUnitsCalculated)
|
|
|
|
return;
|
2014-07-17 21:47:04 -05:00
|
|
|
dc = GetDC(hwnd);
|
|
|
|
if (dc == NULL)
|
|
|
|
xpanic("error getting DC for preferred size calculations", GetLastError());
|
2014-07-28 13:43:34 -05:00
|
|
|
prevFont = (HFONT) SelectObject(dc, controlFont);
|
|
|
|
if (prevFont == NULL)
|
2014-07-17 21:47:04 -05:00
|
|
|
xpanic("error loading control font into device context for preferred size calculation", GetLastError());
|
2014-07-28 13:43:34 -05:00
|
|
|
if (GetTextMetricsW(dc, &tm) == 0)
|
|
|
|
xpanic("error getting text metrics for preferred size calculations", GetLastError());
|
|
|
|
baseX = (int) tm.tmAveCharWidth; /* TODO not optimal; third reference below has better way */
|
|
|
|
baseY = (int) tm.tmHeight;
|
|
|
|
if (SelectObject(dc, prevFont) != controlFont)
|
|
|
|
xpanic("error restoring previous font into device context after preferred size calculations", GetLastError());
|
2014-07-17 21:47:04 -05:00
|
|
|
if (ReleaseDC(hwnd, dc) == 0)
|
|
|
|
xpanic("error releasing DC for preferred size calculations", GetLastError());
|
2014-07-28 13:43:34 -05:00
|
|
|
baseUnitsCalculated = TRUE;
|
2014-07-17 21:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void moveWindow(HWND hwnd, int x, int y, int width, int height)
|
|
|
|
{
|
|
|
|
if (MoveWindow(hwnd, x, y, width, height, TRUE) == 0)
|
|
|
|
xpanic("error setting window/control rect", GetLastError());
|
|
|
|
}
|
2014-08-01 18:15:03 -05:00
|
|
|
|
|
|
|
LONG controlTextLength(HWND hwnd, LPWSTR text)
|
|
|
|
{
|
|
|
|
HDC dc;
|
|
|
|
HFONT prev;
|
|
|
|
SIZE size;
|
|
|
|
|
|
|
|
dc = GetDC(hwnd);
|
|
|
|
if (dc == NULL)
|
|
|
|
xpanic("error getting DC of control for text length", GetLastError());
|
|
|
|
prev = SelectObject(dc, controlFont);
|
|
|
|
if (prev == NULL)
|
|
|
|
xpanic("error setting control font to DC for text length", GetLastError());
|
|
|
|
if (GetTextExtentPoint32W(dc, text, wcslen(text), &size) == 0)
|
|
|
|
xpanic("error actually getting text length", GetLastError());
|
|
|
|
if (SelectObject(dc, prev) != controlFont)
|
|
|
|
xpanic("error restoring previous control font to DC for text length", GetLastError());
|
|
|
|
if (ReleaseDC(hwnd, dc) == 0)
|
|
|
|
xpanic("error releasing DC of control for text length", GetLastError());
|
|
|
|
return size.cx;
|
|
|
|
}
|