summaryrefslogtreecommitdiff
path: root/redo/sizing_windows.c
blob: 60d57f3f0973abb8857527c1b7b947a73ea56d09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* 17 july 2014 */

#include "winapi_windows.h"
#include "_cgo_export.h"

/* TODO rename to sizer_windows.c and move all but the first function to control_windows.c */

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)
{
	HDC dc;
	HFONT prevFont;
	TEXTMETRICW tm;

	if (baseUnitsCalculated)
		return;
	dc = GetDC(hwnd);
	if (dc == NULL)
		xpanic("error getting DC for preferred size calculations", GetLastError());
	prevFont = (HFONT) SelectObject(dc, controlFont);
	if (prevFont == NULL)
		xpanic("error loading control font into device context for preferred size calculation", GetLastError());
	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());
	if (ReleaseDC(hwnd, dc) == 0)
		xpanic("error releasing DC for preferred size calculations", GetLastError());
	baseUnitsCalculated = TRUE;
}

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());
}

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;
}