summaryrefslogtreecommitdiff
path: root/new/windows/label.c
blob: 1c4ef011c9c784e8067984942bdfe419eaced15e (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
// 11 april 2015
#include "uipriv_windows.h"

struct label {
	uiLabel l;
};

static BOOL onWM_COMMAND(uiControl *c, WORD code, LRESULT *lResult)
{
	return FALSE;
}

static BOOL onWM_NOTIFY(uiControl *c, NMHDR *nm, LRESULT *lResult)
{
	return FALSE;
}

static void onWM_DESTROY(uiControl *c)
{
	struct label *l = (struct label *) c;

	uiFree(l);
}

// via http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
#define labelHeight 8

static void preferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
{
	*width = uiWindowsWindowTextWidth(uiControlHWND(c));
	*height = uiDlgUnitsToY(labelHeight, d->sys->baseY);
}

static char *getText(uiLabel *l)
{
	return uiWindowsControlText(uiControl(l));
}

static void setText(uiLabel *l, const char *text)
{
	uiWindowsControlSetText(uiControl(l), text);
}

uiControl *uiNewLabel(const char *text)
{
	struct label *l;
	uiWindowsNewControlParams p;
	WCHAR *wtext;

	l = uiNew(struct label);

	p.dwExStyle = 0;
	p.lpClassName = L"static";
	wtext = toUTF16(text);
	p.lpWindowName = wtext;
	// SS_LEFTNOWORDWRAP clips text past the end; SS_NOPREFIX avoids accelerator translation
	// controls are vertically aligned to the top by default (thanks Xeek in irc.freenode.net/#winapi)
	p.dwStyle = SS_LEFTNOWORDWRAP | SS_NOPREFIX;
	p.hInstance = hInstance;
	p.useStandardControlFont = TRUE;
	p.onWM_COMMAND = onWM_COMMAND;
	p.onWM_NOTIFY = onWM_NOTIFY;
	p.onWM_DESTROY = onWM_DESTROY;
	uiWindowsNewControl(uiControl(l), &p);
	uiFree(wtext);

	uiControl(l)->PreferredSize = preferredSize;

	uiLabel(l)->Text = getText;
	uiLabel(l)->SetText = setText;

	return uiLabel(l);
}