summaryrefslogtreecommitdiff
path: root/newctrl/container_windows.c
blob: 50ebc1e51c066b5b9cec9cb50b540ee34a7a8c41 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// 17 july 2014

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

/*
This could all just be part of Window, but doing so just makes things complex.
In this case, I chose to waste a window handle rather than keep things super complex.
If this is seriously an issue in the future, I can roll it back.
*/

#define containerclass L"gouicontainer"

static LRESULT CALLBACK containerWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	LRESULT lResult;

	if (sharedWndProc(hwnd, uMsg, wParam, lParam, &lResult))
		return lResult;
	switch (uMsg) {
	default:
		return DefWindowProcW(hwnd, uMsg, wParam, lParam);
	}
	xmissedmsg("container", "containerWndProc()", uMsg);
	return 0;		// unreached
}

DWORD makeContainerWindowClass(char **errmsg)
{
	WNDCLASSW wc;

	ZeroMemory(&wc, sizeof (WNDCLASSW));
	wc.lpfnWndProc = containerWndProc;
	wc.hInstance = hInstance;
	wc.hIcon = hDefaultIcon;
	wc.hCursor = hArrowCursor;
	wc.hbrBackground = NULL;//(HBRUSH) (COLOR_BTNFACE + 1);
	wc.lpszClassName = containerclass;
	if (RegisterClassW(&wc) == 0) {
		*errmsg = "error registering container window class";
		return GetLastError();
	}
	return 0;
}

HWND newContainer(void)
{
	HWND hwnd;

	hwnd = CreateWindowExW(
		WS_EX_CONTROLPARENT | WS_EX_TRANSPARENT,
		containerclass, L"",
		WS_CHILD | WS_VISIBLE,
		CW_USEDEFAULT, CW_USEDEFAULT,
		100, 100,
		msgwin, NULL, hInstance, NULL);
	if (hwnd == NULL)
		xpanic("container creation failed", GetLastError());
	return hwnd;
}

void calculateBaseUnits(HWND hwnd, int *baseX, int *baseY, LONG *internalLeading)
{
	HDC dc;
	HFONT prevFont;
	TEXTMETRICW tm;
	SIZE size;

	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());
	if (GetTextExtentPoint32W(dc, L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52, &size) == 0)
		xpanic("error getting text extent point for preferred size calculations", GetLastError());
	*baseX = (int) ((size.cx / 26 + 1) / 2);
	*baseY = (int) tm.tmHeight;
	*internalLeading = tm.tmInternalLeading;
	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());
}