1 |
werner |
36 |
/* wptListView.cpp - Dynamic list view control |
2 |
twoaday |
304 |
* Copyright (C) 2000-2007 Timo Schulz |
3 |
werner |
36 |
* Copyright (C) 2004 Andreas Jobs |
4 |
|
|
* |
5 |
|
|
* This file is part of WinPT. |
6 |
|
|
* |
7 |
|
|
* WinPT is free software; you can redistribute it and/or |
8 |
|
|
* modify it under the terms of the GNU General Public License |
9 |
|
|
* as published by the Free Software Foundation; either version 2 |
10 |
|
|
* of the License, or (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* WinPT is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
|
|
* General Public License for more details. |
16 |
|
|
*/ |
17 |
|
|
#ifdef HAVE_CONFIG_H |
18 |
|
|
#include <config.h> |
19 |
|
|
#endif |
20 |
|
|
|
21 |
|
|
#include <stdio.h> |
22 |
|
|
#include <windows.h> |
23 |
|
|
#include <commctrl.h> |
24 |
|
|
|
25 |
|
|
#include "wptCommonCtl.h" |
26 |
|
|
#include "wptW32API.h" |
27 |
|
|
#include "wptVersion.h" |
28 |
|
|
#include "wptErrors.h" |
29 |
|
|
#include "wptTypes.h" |
30 |
|
|
#include "wptGPG.h" |
31 |
|
|
#include "wptKeylist.h" |
32 |
twoaday |
204 |
#include "wptUTF8.h" |
33 |
werner |
47 |
#include "resource.h" |
34 |
werner |
36 |
|
35 |
|
|
|
36 |
twoaday |
208 |
void |
37 |
|
|
listview_new (listview_ctrl_t *ctx, HWND ctrl) |
38 |
werner |
36 |
{ |
39 |
|
|
struct listview_ctrl_s *c; |
40 |
|
|
|
41 |
|
|
c = new struct listview_ctrl_s; |
42 |
twoaday |
133 |
if (!c) |
43 |
|
|
BUG (NULL); |
44 |
twoaday |
273 |
memset (c, 0, sizeof *c); |
45 |
twoaday |
208 |
c->ctrl = ctrl? ctrl : NULL; |
46 |
twoaday |
133 |
c->hil = NULL; |
47 |
werner |
36 |
*ctx = c; |
48 |
twoaday |
174 |
} |
49 |
werner |
36 |
|
50 |
|
|
|
51 |
|
|
void |
52 |
twoaday |
133 |
listview_release (listview_ctrl_t ctx) |
53 |
werner |
36 |
{ |
54 |
twoaday |
133 |
if (!ctx) |
55 |
|
|
return; |
56 |
|
|
|
57 |
|
|
ctx->cols = 0; |
58 |
|
|
ctx->ctrl = NULL; |
59 |
|
|
ctx->items = 0; |
60 |
|
|
if (ctx->hil) |
61 |
|
|
ImageList_Destroy (ctx->hil); |
62 |
|
|
delete ctx; |
63 |
twoaday |
193 |
} |
64 |
werner |
36 |
|
65 |
|
|
|
66 |
|
|
int |
67 |
twoaday |
193 |
listview_set_column_width (listview_ctrl_t ctx, int col, int width) |
68 |
|
|
{ |
69 |
|
|
LVCOLUMN lvc; |
70 |
|
|
|
71 |
|
|
memset (&lvc, 0, sizeof (lvc)); |
72 |
|
|
lvc.mask = LVCF_WIDTH; |
73 |
|
|
lvc.cx = width; |
74 |
|
|
ListView_SetColumn (ctx->ctrl, col, &lvc); |
75 |
|
|
return 0; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
|
79 |
|
|
int |
80 |
werner |
36 |
listview_add_column (listview_ctrl_t ctx, listview_column_t col) |
81 |
|
|
{ |
82 |
|
|
int rc = 0; |
83 |
|
|
LV_COLUMN lvc; |
84 |
|
|
|
85 |
twoaday |
273 |
memset (&lvc, 0, sizeof lvc); |
86 |
werner |
36 |
lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM; |
87 |
|
|
lvc.pszText = col->fieldname; |
88 |
|
|
lvc.cx = col->width; |
89 |
|
|
lvc.fmt = LVCFMT_LEFT; |
90 |
|
|
lvc.iSubItem = col->pos; |
91 |
twoaday |
273 |
if (ListView_InsertColumn (ctx->ctrl, col->pos, &lvc) == -1) |
92 |
werner |
36 |
rc = 1; |
93 |
|
|
ctx->cols++; |
94 |
|
|
return rc; |
95 |
twoaday |
200 |
} |
96 |
werner |
36 |
|
97 |
|
|
|
98 |
|
|
int |
99 |
|
|
listview_add_item_pos (listview_ctrl_t ctx, int pos) |
100 |
|
|
{ |
101 |
|
|
int rc = 0; |
102 |
|
|
LV_ITEM lvi; |
103 |
|
|
|
104 |
|
|
memset (&lvi, 0, sizeof lvi); |
105 |
|
|
lvi.iItem = pos; |
106 |
twoaday |
133 |
if (ListView_InsertItem (ctx->ctrl, &lvi) != pos) |
107 |
|
|
rc = -1; |
108 |
werner |
36 |
ctx->items++; |
109 |
|
|
return rc; |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
|
113 |
|
|
int |
114 |
twoaday |
241 |
listview_add_item (listview_ctrl_t ctx, const char *text) |
115 |
werner |
36 |
{ |
116 |
|
|
int rc = 0; |
117 |
|
|
LV_ITEM lvi; |
118 |
|
|
|
119 |
twoaday |
241 |
memset (&lvi, 0, sizeof lvi); |
120 |
werner |
36 |
lvi.mask = LVIF_TEXT; |
121 |
|
|
lvi.pszText = (char *)text; |
122 |
twoaday |
241 |
rc = ListView_InsertItem (ctx->ctrl, &lvi); |
123 |
werner |
36 |
if( rc == -1 ) |
124 |
|
|
rc = 1; |
125 |
|
|
ctx->items++; |
126 |
|
|
return rc; |
127 |
twoaday |
200 |
} |
128 |
werner |
36 |
|
129 |
|
|
|
130 |
|
|
int |
131 |
twoaday |
133 |
listview_add_item_image (listview_ctrl_t ctx, const char *text, int image) |
132 |
|
|
{ |
133 |
|
|
int rc = 0; |
134 |
|
|
LV_ITEM lvi; |
135 |
|
|
|
136 |
|
|
memset( &lvi, 0, sizeof lvi ); |
137 |
|
|
lvi.mask = LVIF_TEXT | LVIF_IMAGE; |
138 |
|
|
lvi.pszText = (char *)text; |
139 |
|
|
lvi.iImage = image; |
140 |
|
|
rc = ListView_InsertItem (ctx->ctrl, &lvi); |
141 |
|
|
if (rc == -1) |
142 |
|
|
rc = 1; |
143 |
|
|
ctx->items++; |
144 |
|
|
return rc; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
|
148 |
|
|
int |
149 |
werner |
36 |
listview_add_item2 (listview_ctrl_t ctx, const char *text, void *magic) |
150 |
|
|
{ |
151 |
|
|
int rc = 0; |
152 |
twoaday |
139 |
LVITEM lvi; |
153 |
werner |
36 |
|
154 |
twoaday |
139 |
memset (&lvi, 0, sizeof (lvi)); |
155 |
werner |
36 |
lvi.mask = LVIF_TEXT | LVIF_PARAM; |
156 |
|
|
lvi.pszText = (char *)text; |
157 |
|
|
lvi.lParam = (LPARAM)magic; |
158 |
twoaday |
139 |
rc = ListView_InsertItem (ctx->ctrl, &lvi); |
159 |
|
|
if (rc == -1) |
160 |
werner |
36 |
rc = 1; |
161 |
|
|
ctx->items++; |
162 |
|
|
return rc; |
163 |
twoaday |
200 |
} |
164 |
werner |
36 |
|
165 |
|
|
|
166 |
|
|
void* |
167 |
|
|
listview_get_item2 (listview_ctrl_t ctx, int pos) |
168 |
|
|
{ |
169 |
|
|
LVITEM lvi; |
170 |
|
|
|
171 |
|
|
memset (&lvi, 0, sizeof lvi); |
172 |
|
|
lvi.mask = LVIF_PARAM; |
173 |
|
|
lvi.iItem = pos; |
174 |
|
|
ListView_GetItem (ctx->ctrl, &lvi); |
175 |
|
|
return (void*)lvi.lParam; |
176 |
|
|
} |
177 |
|
|
|
178 |
twoaday |
241 |
|
179 |
werner |
36 |
int |
180 |
|
|
listview_set_item2 (listview_ctrl_t ctx, int pos, void *magic) |
181 |
|
|
{ |
182 |
|
|
LVITEM lvi; |
183 |
|
|
|
184 |
|
|
memset (&lvi, 0, sizeof (lvi)); |
185 |
|
|
lvi.mask = LVIF_PARAM; |
186 |
|
|
lvi.iItem = pos; |
187 |
|
|
lvi.lParam = (LPARAM)magic; |
188 |
|
|
if (ListView_SetItem (ctx->ctrl, &lvi) == -1) |
189 |
|
|
return 1; |
190 |
|
|
return 0; |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
|
194 |
|
|
void |
195 |
twoaday |
204 |
listview_add_sub_item (listview_ctrl_t ctx, int pos, int col, const char *text) |
196 |
|
|
{ |
197 |
twoaday |
205 |
ListView_SetItemText (ctx->ctrl, pos, col, (char*)text); |
198 |
twoaday |
200 |
} |
199 |
werner |
36 |
|
200 |
|
|
|
201 |
|
|
int |
202 |
|
|
listview_count_items( listview_ctrl_t ctx, int curr_sel ) |
203 |
|
|
{ |
204 |
|
|
int n; |
205 |
|
|
|
206 |
|
|
n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) : |
207 |
|
|
ListView_GetItemCount( ctx->ctrl ); |
208 |
|
|
return n; |
209 |
twoaday |
200 |
} |
210 |
werner |
36 |
|
211 |
|
|
|
212 |
|
|
int |
213 |
twoaday |
129 |
listview_del_column (listview_ctrl_t ctx, int pos) |
214 |
|
|
{ |
215 |
|
|
ctx->cols--; |
216 |
|
|
return ListView_DeleteColumn (ctx->ctrl, pos)? 0 : 1; |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
|
220 |
twoaday |
150 |
/* Delete a single item from @ctx at position @pos. */ |
221 |
twoaday |
129 |
int |
222 |
twoaday |
150 |
listview_del_item (listview_ctrl_t ctx, int pos) |
223 |
werner |
36 |
{ |
224 |
|
|
int rc = 0; |
225 |
|
|
|
226 |
twoaday |
150 |
if (ListView_DeleteItem (ctx->ctrl, pos) == -1) |
227 |
werner |
36 |
rc = 1; |
228 |
|
|
return rc; |
229 |
twoaday |
150 |
} |
230 |
werner |
36 |
|
231 |
|
|
|
232 |
twoaday |
150 |
/* Delete all selected items in @ctx. */ |
233 |
werner |
36 |
int |
234 |
twoaday |
150 |
listview_del_sel_items (listview_ctrl_t ctx) |
235 |
werner |
36 |
{ |
236 |
|
|
int i, n; |
237 |
|
|
|
238 |
|
|
n = listview_count_items (ctx, 0); |
239 |
twoaday |
150 |
for (i = n; i > -1; i--) { |
240 |
|
|
if (listview_get_item_state (ctx, i)) |
241 |
|
|
listview_del_item (ctx, i); |
242 |
werner |
36 |
} |
243 |
|
|
return 0; |
244 |
twoaday |
150 |
} |
245 |
werner |
36 |
|
246 |
|
|
|
247 |
|
|
int |
248 |
twoaday |
150 |
listview_del_all_items (listview_ctrl_t ctx) |
249 |
werner |
36 |
{ |
250 |
|
|
int rc = 0; |
251 |
|
|
|
252 |
twoaday |
150 |
if (ListView_DeleteAllItems (ctx->ctrl) == FALSE) |
253 |
werner |
36 |
rc = 1; |
254 |
|
|
return rc; |
255 |
twoaday |
150 |
} |
256 |
werner |
36 |
|
257 |
|
|
|
258 |
twoaday |
193 |
/* Return the index of the selected item. |
259 |
|
|
Support both selections and checkboxes. */ |
260 |
|
|
int |
261 |
|
|
listview_get_selected_item (listview_ctrl_t lv) |
262 |
|
|
{ |
263 |
|
|
int pos, i; |
264 |
|
|
|
265 |
|
|
pos = listview_get_curr_pos (lv); |
266 |
|
|
if (pos != -1) |
267 |
|
|
return pos; |
268 |
|
|
for (i=0; i < listview_count_items (lv, 0); i++) { |
269 |
|
|
if (listview_get_item_state (lv, i)) |
270 |
|
|
return i; |
271 |
|
|
} |
272 |
|
|
return -1; |
273 |
|
|
} |
274 |
|
|
|
275 |
|
|
|
276 |
twoaday |
174 |
/* Return if the given element is selected or if the ext |
277 |
|
|
style is used, if the checkbox is activated. */ |
278 |
werner |
36 |
int |
279 |
twoaday |
174 |
listview_get_item_state (listview_ctrl_t ctx, int pos) |
280 |
|
|
{ |
281 |
|
|
int ret; |
282 |
|
|
if (ctx->ext_chkbox) |
283 |
|
|
ret = ListView_GetCheckState (ctx->ctrl, pos); |
284 |
|
|
else |
285 |
|
|
ret = ListView_GetItemState (ctx->ctrl, pos, LVIS_SELECTED); |
286 |
|
|
return ret; |
287 |
|
|
} |
288 |
werner |
36 |
|
289 |
|
|
|
290 |
|
|
int |
291 |
twoaday |
236 |
listview_sort_items (listview_ctrl_t ctx, int sortby, listview_cmp sort_cb) |
292 |
twoaday |
273 |
{ |
293 |
werner |
36 |
ListView_SortItems (ctx->ctrl, sort_cb, sortby); |
294 |
|
|
return 0; |
295 |
twoaday |
200 |
} |
296 |
werner |
36 |
|
297 |
|
|
|
298 |
|
|
int |
299 |
twoaday |
273 |
listview_get_curr_pos (listview_ctrl_t ctx) |
300 |
werner |
36 |
{ |
301 |
|
|
return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED ); |
302 |
twoaday |
200 |
} |
303 |
werner |
36 |
|
304 |
|
|
|
305 |
|
|
int |
306 |
|
|
listview_get_item_text (listview_ctrl_t ctx, int pos, int col, char *text, |
307 |
|
|
int maxbytes) |
308 |
|
|
{ |
309 |
|
|
if (pos == -1) { |
310 |
|
|
pos = listview_get_curr_pos (ctx); |
311 |
|
|
if (pos == -1) |
312 |
|
|
return -1; |
313 |
|
|
} |
314 |
|
|
ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes); |
315 |
|
|
return 0; |
316 |
twoaday |
200 |
} |
317 |
werner |
36 |
|
318 |
|
|
|
319 |
twoaday |
174 |
/* Use extended style with checkboxes for each item. */ |
320 |
werner |
36 |
void |
321 |
twoaday |
174 |
listview_set_chkbox_style (listview_ctrl_t ctx) |
322 |
|
|
{ |
323 |
|
|
ListView_SetExtendedListViewStyle (ctx->ctrl, LVS_EX_CHECKBOXES); |
324 |
|
|
ctx->ext_chkbox = 1; |
325 |
|
|
} |
326 |
|
|
|
327 |
|
|
|
328 |
|
|
/* Use extended style to select the entire row. */ |
329 |
|
|
void |
330 |
|
|
listview_set_ext_style (listview_ctrl_t ctx) |
331 |
werner |
36 |
{ |
332 |
twoaday |
174 |
ListView_SetExtendedListViewStyle (ctx->ctrl, LVS_EX_FULLROWSELECT); |
333 |
|
|
} |
334 |
werner |
36 |
|
335 |
|
|
|
336 |
|
|
int |
337 |
|
|
listview_set_column_order( listview_ctrl_t ctx, int *array ) |
338 |
|
|
{ |
339 |
|
|
return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array ); |
340 |
twoaday |
200 |
} |
341 |
werner |
36 |
|
342 |
|
|
|
343 |
|
|
void |
344 |
|
|
listview_select_all (listview_ctrl_t ctx) |
345 |
|
|
{ |
346 |
|
|
ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED ); |
347 |
twoaday |
200 |
} |
348 |
werner |
36 |
|
349 |
|
|
|
350 |
|
|
void |
351 |
twoaday |
77 |
listview_deselect_all (listview_ctrl_t ctx) |
352 |
|
|
{ |
353 |
|
|
ListView_SetItemState (ctx->ctrl, -1, ~LVNI_SELECTED, LVNI_SELECTED); |
354 |
|
|
} |
355 |
|
|
|
356 |
|
|
|
357 |
|
|
void |
358 |
werner |
36 |
listview_select_one (listview_ctrl_t ctx, int pos) |
359 |
|
|
{ |
360 |
twoaday |
241 |
ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED, |
361 |
|
|
LVIS_FOCUSED|LVIS_SELECTED); |
362 |
werner |
36 |
} |
363 |
|
|
|
364 |
|
|
|
365 |
|
|
void |
366 |
|
|
listview_scroll (listview_ctrl_t ctx, int oldpos, int newpos) |
367 |
|
|
{ |
368 |
|
|
const int size=14; |
369 |
|
|
|
370 |
|
|
if (oldpos == -1) |
371 |
|
|
oldpos = 0; |
372 |
twoaday |
241 |
//log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d", |
373 |
|
|
//oldpos, newpos, newpos-oldpos); |
374 |
werner |
36 |
ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size); |
375 |
|
|
} |
376 |
|
|
|
377 |
|
|
|
378 |
twoaday |
241 |
static int |
379 |
|
|
listview_find_substr (listview_ctrl_t ctx, const char *str) |
380 |
|
|
{ |
381 |
|
|
char buf[256]; |
382 |
|
|
int i, n, fnd = 0; |
383 |
|
|
|
384 |
|
|
/* We assume the first column contains text. */ |
385 |
|
|
n = listview_count_items (ctx, 0); |
386 |
|
|
for (i = 0; i < n; i++) { |
387 |
twoaday |
304 |
listview_get_item_text (ctx, i, 0, buf, DIM (buf)-1); |
388 |
twoaday |
241 |
if (stristr (buf, str)) { |
389 |
|
|
fnd = 1; |
390 |
|
|
break; |
391 |
|
|
} |
392 |
|
|
} |
393 |
|
|
if (!fnd) |
394 |
|
|
i = -1; /* not found */ |
395 |
|
|
return i; |
396 |
|
|
} |
397 |
|
|
|
398 |
|
|
|
399 |
werner |
36 |
int |
400 |
twoaday |
241 |
listview_find (listview_ctrl_t ctx, const char *str, int substr) |
401 |
werner |
36 |
{ |
402 |
|
|
LVFINDINFO inf; |
403 |
|
|
int pos; |
404 |
|
|
|
405 |
twoaday |
207 |
/* XXX: allow to make a substring search. */ |
406 |
werner |
36 |
memset (&inf, 0, sizeof (inf)); |
407 |
|
|
inf.flags = LVFI_STRING|LVFI_PARTIAL; |
408 |
|
|
inf.psz = str; |
409 |
|
|
pos = ListView_FindItem (ctx->ctrl, -1, &inf); |
410 |
twoaday |
241 |
if (pos == -1 && substr) |
411 |
|
|
pos = listview_find_substr (ctx, str); |
412 |
werner |
36 |
return pos; |
413 |
|
|
} |
414 |
|
|
|
415 |
twoaday |
129 |
|
416 |
|
|
void |
417 |
|
|
listview_setview (listview_ctrl_t ctx, DWORD view) |
418 |
|
|
{ |
419 |
|
|
DWORD style = GetWindowLong (ctx->ctrl, GWL_STYLE); |
420 |
|
|
if ((style & LVS_TYPEMASK) != view) |
421 |
|
|
SetWindowLong (ctx->ctrl, GWL_STYLE, (style & ~LVS_TYPEMASK) | view); |
422 |
|
|
} |
423 |
twoaday |
133 |
|
424 |
|
|
|
425 |
|
|
void |
426 |
twoaday |
181 |
listview_set_image_list (listview_ctrl_t ctx, int cx, int cy, |
427 |
|
|
HICON *ico, DWORD nicons) |
428 |
twoaday |
133 |
{ |
429 |
|
|
HIMAGELIST hil; |
430 |
|
|
DWORD i; |
431 |
|
|
|
432 |
twoaday |
273 |
if (cx <= 0 || cy <= 0) |
433 |
|
|
cx = cy = 16; /* default size. */ |
434 |
twoaday |
181 |
|
435 |
|
|
hil = ImageList_Create (cx, cy, ILC_COLOR8|ILC_MASK, nicons, 1); |
436 |
twoaday |
144 |
ImageList_SetBkColor (hil, CLR_NONE); |
437 |
twoaday |
133 |
for (i=0; i < nicons; i++) |
438 |
twoaday |
144 |
ImageList_AddIcon (hil, ico[i]); |
439 |
|
|
ListView_SetImageList (ctx->ctrl, hil, LVSIL_SMALL); |
440 |
twoaday |
133 |
} |