summaryrefslogtreecommitdiff
path: root/redo/containerctrls_windows.c
blob: 5a998b5cb4720f5f0a6ee7bc86a5d185aba8aba7 (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
/* 25 july 2014 */

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

/* provided for cgo's benefit */
LPWSTR xWC_TABCONTROL = WC_TABCONTROL;

static LRESULT CALLBACK tabSubProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR id, DWORD_PTR data)
{
	NMHDR *nmhdr = (NMHDR *) lParam;
	LRESULT r;

	switch (uMsg) {
	case msgNOTIFY:
		switch (nmhdr->code) {
		case TCN_SELCHANGING:
			r = SendMessageW(hwnd, TCM_GETCURSEL, 0, 0);
			if (r == (LRESULT) -1)	/* no tab currently selected */
				return FALSE;
			tabChanging((void *) data, r);
			return FALSE;			/* allow change */
		case TCN_SELCHANGE:
			tabChanged((void *) data, SendMessageW(hwnd, TCM_GETCURSEL, 0, 0));
			return 0;
		}
		return (*fv_DefSubclassProc)(hwnd, uMsg, wParam, lParam);
	case WM_NCDESTROY:
		if ((*fv_RemoveWindowSubclass)(hwnd, tabSubProc, id) == FALSE)
			xpanic("error removing Tab subclass (which was for its own event handler)", GetLastError());
		return (*fv_DefSubclassProc)(hwnd, uMsg, wParam, lParam);
	default:
		return (*fv_DefSubclassProc)(hwnd, uMsg, wParam, lParam);
	}
	xmissedmsg("Tab", "tabSubProc()", uMsg);
	return 0;		/* unreached */
}

void setTabSubclass(HWND hwnd, void *data)
{
	if ((*fv_SetWindowSubclass)(hwnd, tabSubProc, 0, (DWORD_PTR) data) == FALSE)
		xpanic("error subclassing Tab to give it its own event handler", GetLastError());
}

void tabAppend(HWND hwnd, LPWSTR name)
{
	TCITEM item;
	LRESULT n;

	ZeroMemory(&item, sizeof (TCITEM));
	item.mask = TCIF_TEXT;
	item.pszText = name;
	/* MSDN's example code uses the first invalid index directly for this */
	n = SendMessageW(hwnd, TCM_GETITEMCOUNT, 0, 0);
	if (SendMessageW(hwnd, TCM_INSERTITEM, (WPARAM) n, (LPARAM) (&item)) == (LRESULT) -1)
		xpanic("error adding tab to Tab", GetLastError());
}

void tabGetContentRect(HWND hwnd, RECT *r)
{
	/* not &r; already a pointer (thanks MindChild in irc.efnet.net/#winprog for spotting my failure) */
	SendMessageW(hwnd, TCM_ADJUSTRECT, FALSE, (LPARAM) r);
}

/* theoretically we don't need to iterate over every tab for this, but let's do it just to be safe */
LONG tabGetTabHeight(HWND hwnd)
{
	RECT r;
	LRESULT i, n;
	LONG tallest;

	n = SendMessageW(hwnd, TCM_GETITEMCOUNT, 0, 0);
	/* if there are no tabs, then the control just draws a box over the full window rect, reserving no space for tabs (TODO check on windows xp and 7); this is handled here */
	tallest = 0;
	for (i = 0; i < n; i++) {
		if (SendMessageW(hwnd, TCM_GETITEMRECT, (WPARAM) i, (LPARAM) (&r)) == FALSE)
			xpanic("error getting tab height for Tab.preferredSize()", GetLastError());
		if (tallest < (r.bottom - r.top))
			tallest = r.bottom - r.top;
	}
	return tallest;
}