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