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
|
// 29 november 2014
static LONG rowHeight(struct table *t)
{
HFONT thisfont, prevfont;
TEXTMETRICW tm;
HDC dc;
LONG ret;
dc = GetDC(t->hwnd);
if (dc == NULL)
abort();
thisfont = t->font; // in case WM_SETFONT happens before we return
prevfont = (HFONT) SelectObject(dc, thisfont);
if (prevfont == NULL)
abort();
if (GetTextMetricsW(dc, &tm) == 0)
abort();
if (SelectObject(dc, prevfont) != (HGDIOBJ) (thisfont))
abort();
if (ReleaseDC(t->hwnd, dc) == 0)
abort();
ret = tm.tmHeight;
if (ret < t->imagelistHeight)
ret = t->imagelistHeight;
if (ret < t->checkboxHeight)
ret = t->checkboxHeight;
return ret;
}
static void redrawAll(struct table *t)
{
if (InvalidateRect(t->hwnd, NULL, TRUE) == 0)
abort();
if (UpdateWindow(t->hwnd) == 0)
abort();
}
static RECT realClientRect(struct table *t)
{
RECT r;
if (GetClientRect(t->hwnd, &r) == 0)
abort();
r.top += t->headerHeight;
return r;
}
static void repositionHeader(struct table *t)
{
RECT r;
HDLAYOUT headerlayout;
WINDOWPOS headerpos;
if (GetClientRect(t->hwnd, &r) == 0) // use the whole client rect
abort();
// grow the rectangle to the left to fake scrolling
r.left -= t->hpos;
headerlayout.prc = &r;
headerlayout.pwpos = &headerpos;
if (SendMessageW(t->header, HDM_LAYOUT, 0, (LPARAM) (&headerlayout)) == FALSE)
abort();
if (SetWindowPos(t->header, headerpos.hwndInsertAfter, headerpos.x, headerpos.y, headerpos.cx, headerpos.cy, headerpos.flags | SWP_SHOWWINDOW) == 0)
abort();
t->headerHeight = headerpos.cy;
}
// this counts partially visible rows
// for all fully visible rows use t->pagesize
// cliprect and rowHeight must be specified here to avoid recomputing things multiple times
static intptr_t lastVisible(struct table *t, RECT cliprect, LONG rowHeight)
{
intptr_t last;
last = ((cliprect.bottom + rowHeight - 1) / rowHeight) + t->firstVisible;
if (last >= t->count)
last = t->count;
return last;
}
static void redrawRow(struct table *t, intptr_t row)
{
RECT r;
intptr_t height;
r = realClientRect(t);
height = rowHeight(t);
if (row < t->firstVisible || row > lastVisible(t, r, height)) // not visible; don't bother
return;
r.top = (row - t->firstVisible) * height + t->headerHeight;
r.bottom = r.top + height;
// keep the width and height the same; it spans the client area anyway
if (InvalidateRect(t->hwnd, &r, TRUE) == 0)
abort();
if (UpdateWindow(t->hwnd) == 0)
abort();
}
static intptr_t hitTestColumn(struct table *t, int x)
{
HDITEMW item;
intptr_t i;
// TODO count dividers
for (i = 0; i < t->nColumns; i++) {
ZeroMemory(&item, sizeof (HDITEMW));
item.mask = HDI_WIDTH;
if (SendMessageW(t->header, HDM_GETITEM, (WPARAM) i, (LPARAM) (&item)) == FALSE)
abort();
if (x < item.cxy)
return i;
x -= item.cxy; // not yet
}
// no column
return -1;
}
static void lParamToRowColumn(struct table *t, LPARAM lParam, intptr_t *row, intptr_t *column)
{
int x, y;
LONG h;
x = GET_X_LPARAM(lParam);
y = GET_Y_LPARAM(lParam);
h = rowHeight(t);
y += t->firstVisible * h;
y -= t->headerHeight;
y /= h;
if (row != NULL) {
*row = y;
if (*row >= t->count)
*row = -1;
}
if (column != NULL)
*column = hitTestColumn(t, x);
}
|