aboutsummaryrefslogtreecommitdiff
path: root/src/libui_sdl/libui/windows/checkbox.cpp
blob: be425c0041b4d95ee6c2e267cd6772a4f34ba25a (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// 7 april 2015
#include "uipriv_windows.hpp"

struct uiCheckbox {
	uiWindowsControl c;
	HWND hwnd;
	void (*onToggled)(uiCheckbox *, void *);
	void *onToggledData;
};

static BOOL onWM_COMMAND(uiControl *cc, HWND hwnd, WORD code, LRESULT *lResult)
{
	uiCheckbox *c = uiCheckbox(cc);
	WPARAM check;

	if (code != BN_CLICKED)
		return FALSE;

	// we didn't use BS_AUTOCHECKBOX (http://blogs.msdn.com/b/oldnewthing/archive/2014/05/22/10527522.aspx) so we have to manage the check state ourselves
	check = BST_CHECKED;
	if (SendMessage(c->hwnd, BM_GETCHECK, 0, 0) == BST_CHECKED)
		check = BST_UNCHECKED;
	SendMessage(c->hwnd, BM_SETCHECK, check, 0);

	(*(c->onToggled))(c, c->onToggledData);
	*lResult = 0;
	return TRUE;
}

static void uiCheckboxDestroy(uiControl *cc)
{
	uiCheckbox *c = uiCheckbox(cc);

	uiWindowsUnregisterWM_COMMANDHandler(c->hwnd);
	uiWindowsEnsureDestroyWindow(c->hwnd);
	uiFreeControl(uiControl(c));
}

uiWindowsControlAllDefaultsExceptDestroy(uiCheckbox)

// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
#define checkboxHeight 10
// from http://msdn.microsoft.com/en-us/library/windows/desktop/bb226818%28v=vs.85%29.aspx
#define checkboxXFromLeftOfBoxToLeftOfLabel 12

static void uiCheckboxMinimumSize(uiWindowsControl *cc, int *width, int *height)
{
	uiCheckbox *c = uiCheckbox(cc);
	uiWindowsSizing sizing;
	int x, y;

	x = checkboxXFromLeftOfBoxToLeftOfLabel;
	y = checkboxHeight;
	uiWindowsGetSizing(c->hwnd, &sizing);
	uiWindowsSizingDlgUnitsToPixels(&sizing, &x, &y);
	*width = x + uiWindowsWindowTextWidth(c->hwnd);
	*height = y;
}

static void defaultOnToggled(uiCheckbox *c, void *data)
{
	// do nothing
}

char *uiCheckboxText(uiCheckbox *c)
{
	return uiWindowsWindowText(c->hwnd);
}

void uiCheckboxSetText(uiCheckbox *c, const char *text)
{
	uiWindowsSetWindowText(c->hwnd, text);
	// changing the text might necessitate a change in the checkbox's size
	uiWindowsControlMinimumSizeChanged(uiWindowsControl(c));
}

void uiCheckboxOnToggled(uiCheckbox *c, void (*f)(uiCheckbox *, void *), void *data)
{
	c->onToggled = f;
	c->onToggledData = data;
}

int uiCheckboxChecked(uiCheckbox *c)
{
	return SendMessage(c->hwnd, BM_GETCHECK, 0, 0) == BST_CHECKED;
}

void uiCheckboxSetChecked(uiCheckbox *c, int checked)
{
	WPARAM check;

	check = BST_CHECKED;
	if (!checked)
		check = BST_UNCHECKED;
	SendMessage(c->hwnd, BM_SETCHECK, check, 0);
}

uiCheckbox *uiNewCheckbox(const char *text)
{
	uiCheckbox *c;
	WCHAR *wtext;

	uiWindowsNewControl(uiCheckbox, c);

	wtext = toUTF16(text);
	c->hwnd = uiWindowsEnsureCreateControlHWND(0,
		L"button", wtext,
		BS_CHECKBOX | WS_TABSTOP,
		hInstance, NULL,
		TRUE);
	uiFree(wtext);

	uiWindowsRegisterWM_COMMANDHandler(c->hwnd, onWM_COMMAND, uiControl(c));
	uiCheckboxOnToggled(c, defaultOnToggled, NULL);

	return c;
}