summaryrefslogtreecommitdiff
path: root/wintable/checkboxstuff
blob: 5533608c9ddd2bf344e22076b317865b9d8890a9 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
	BOOL lastmouse;
	intptr_t lastmouseRow;
	intptr_t lastmouseColumn;
	BOOL mouseDown;			// TRUE if over a checkbox; the next two decide which ones
	intptr_t mouseDownRow;
	intptr_t mouseDownColumn;

static RECT checkboxRect(struct table *t, intptr_t row, intptr_t column, LONG rowHeight)
{
	RECT r;
	HDITEMW item;
	intptr_t i;

	// TODO count dividers
	for (i = 0; i < column; i++) {
		ZeroMemory(&item, sizeof (HDITEMW));
		item.mask = HDI_WIDTH;
		if (SendMessageW(t->header, HDM_GETITEM, (WPARAM) i, (LPARAM) (&item)) == FALSE)
			abort();
		r.left += item.cxy;
	}
	// TODO double-check to see if this takes any parameters
	r.left += SendMessageW(t->header, HDM_GETBITMAPMARGIN, 0, 0);
	r.right = r.left + t->checkboxWidth;
	// TODO vertical center
	r.top = row * rowHeight;
	r.bottom = r.top + t->checkboxHeight;
	return r;
}

// TODO clean up variables
static BOOL lParamInCheckbox(struct table *t, LPARAM lParam, intptr_t *row, intptr_t *column)
{
	int x, y;
	LONG h;
	intptr_t col;
	RECT r;
	POINT pt;

	x = GET_X_LPARAM(lParam);
	y = GET_Y_LPARAM(lParam);
	h = rowHeight(t);
	y += t->firstVisible * h;
	y -= t->headerHeight;
	pt.y = y;		// save actual y coordinate now
	y /= h;		// turn it into a row count
	if (y >= t->count)
		return FALSE;
	col = hitTestColumn(t, x);
	if (col == -1)
		return FALSE;
	if (t->columnTypes[col] != tableColumnCheckbox)
		return FALSE;
	r = checkboxRect(t, y, col, h);
	pt.x = x;
	if (PtInRect(&r, pt) == 0)
		return FALSE;
	if (row != NULL)
		*row = y;
	if (column != NULL)
		*column = col;
	return TRUE;
}

static void retrack(struct table *t)
{
	TRACKMOUSEEVENT tm;

	ZeroMemory(&tm, sizeof (TRACKMOUSEEVENT));
	tm.cbSize = sizeof (TRACKMOUSEEVENT);
	tm.dwFlags = TME_LEAVE;		// TODO also TME_NONCLIENT?
	tm.hwndTrack = t->hwnd;
	if (_TrackMouseEvent(&tm) == 0)
		abort();
}

static void track(struct table *t, LPARAM lParam)
{
	intptr_t row, column;
	BOOL prev;
	intptr_t prevrow, prevcolumn;

	prev = t->lastmouse;
	prevrow = t->lastmouseRow;
	prevcolumn = t->lastmouseColumn;
	t->lastmouse = lParamInCheckbox(t, lParam, &(t->lastmouseRow), &(t->lastmouseColumn));
	if (prev)
		if (prevrow != row || prevcolumn != column)
			redrawRow(t, prevrow);
	redrawRow(t, t->lastmouseRow);
}

in selectItem
	// TODO only if inside a checkbox
	t->mouseDown = TRUE;
	t->mouseDownRow = t->selected;
	t->mouseDownColumn = t->focusedColumn;

in drawItem
			// TODO replace all this
			rsel.left = headeritem.left + xoff;
			rsel.top = y;
			rsel.right = rsel.left + t->checkboxWidth;
			rsel.bottom = rsel.top + t->checkboxHeight;
			{ COLORREF c;

			c = RGB(255, 0, 0);
			if (t->mouseDown) {
				if (i == t->mouseDownRow && j == t->mouseDownColumn)
					c = RGB(0, 0, 255);
			} else if (t->lastmouse) {
				if (i == t->lastmouseRow && j == t->lastmouseColumn)
					c = RGB(0, 255, 0);
			}
			if (SetDCBrushColor(dc, c) == CLR_INVALID)
				abort();
			}
			if (FillRect(dc, &rsel, GetStockObject(DC_BRUSH)) == 0)
				abort();
			break;

	case WM_LBUTTONDOWN:
		selectItem(t, wParam, lParam);
		return 0;
	case WM_LBUTTONUP:
		// TODO toggle checkbox
		if (t->mouseDown) {
			t->mouseDown = FALSE;
			redrawRow(t, t->mouseDownRow);
		}
		return 0;
	// TODO other mouse buttons?
	case WM_MOUSEMOVE:
		track(t, lParam);
		return 0;
	case WM_MOUSELEAVE:
		t->lastmouse = FALSE;
		retrack(t);
		// TODO redraw row mouse is currently over
		// TODO split into its own function
		if (t->mouseDown) {
			t->mouseDown = FALSE;
			redrawRow(t, t->mouseDownRow);
		}
		return 0;