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 |
*ctx = c; |
53 |
return 0; |
54 |
} /* listview_new */ |
55 |
|
56 |
|
57 |
void |
58 |
listview_release (listview_ctrl_t ctx) |
59 |
{ |
60 |
if (!ctx) |
61 |
return; |
62 |
|
63 |
ctx->cols = 0; |
64 |
ctx->ctrl = NULL; |
65 |
ctx->items = 0; |
66 |
if (ctx->hil) |
67 |
ImageList_Destroy (ctx->hil); |
68 |
delete ctx; |
69 |
} /* listview_release */ |
70 |
|
71 |
|
72 |
int |
73 |
listview_add_column (listview_ctrl_t ctx, listview_column_t col) |
74 |
{ |
75 |
int rc = 0; |
76 |
LV_COLUMN lvc; |
77 |
|
78 |
memset( &lvc, 0, sizeof lvc ); |
79 |
lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM; |
80 |
lvc.pszText = col->fieldname; |
81 |
lvc.cx = col->width; |
82 |
lvc.fmt = LVCFMT_LEFT; |
83 |
lvc.iSubItem = col->pos; |
84 |
if( ListView_InsertColumn (ctx->ctrl, col->pos, &lvc) == -1) |
85 |
rc = 1; |
86 |
ctx->cols++; |
87 |
return rc; |
88 |
} /* listview_add_column */ |
89 |
|
90 |
|
91 |
int |
92 |
listview_add_item_pos (listview_ctrl_t ctx, int pos) |
93 |
{ |
94 |
int rc = 0; |
95 |
LV_ITEM lvi; |
96 |
|
97 |
memset (&lvi, 0, sizeof lvi); |
98 |
lvi.iItem = pos; |
99 |
if (ListView_InsertItem (ctx->ctrl, &lvi) != pos) |
100 |
rc = -1; |
101 |
ctx->items++; |
102 |
return rc; |
103 |
} |
104 |
|
105 |
|
106 |
int |
107 |
listview_add_item( listview_ctrl_t ctx, const char *text ) |
108 |
{ |
109 |
int rc = 0; |
110 |
LV_ITEM lvi; |
111 |
|
112 |
memset( &lvi, 0, sizeof lvi ); |
113 |
lvi.mask = LVIF_TEXT; |
114 |
lvi.pszText = (char *)text; |
115 |
rc = ListView_InsertItem( ctx->ctrl, &lvi ); |
116 |
if( rc == -1 ) |
117 |
rc = 1; |
118 |
ctx->items++; |
119 |
return rc; |
120 |
} /* listview_add_item */ |
121 |
|
122 |
|
123 |
int |
124 |
listview_add_item_image (listview_ctrl_t ctx, const char *text, int image) |
125 |
{ |
126 |
int rc = 0; |
127 |
LV_ITEM lvi; |
128 |
|
129 |
memset( &lvi, 0, sizeof lvi ); |
130 |
lvi.mask = LVIF_TEXT | LVIF_IMAGE; |
131 |
lvi.pszText = (char *)text; |
132 |
lvi.iImage = image; |
133 |
rc = ListView_InsertItem (ctx->ctrl, &lvi); |
134 |
if (rc == -1) |
135 |
rc = 1; |
136 |
ctx->items++; |
137 |
return rc; |
138 |
} |
139 |
|
140 |
|
141 |
int |
142 |
listview_add_item2 (listview_ctrl_t ctx, const char *text, void *magic) |
143 |
{ |
144 |
int rc = 0; |
145 |
LVITEM lvi; |
146 |
|
147 |
memset (&lvi, 0, sizeof (lvi)); |
148 |
lvi.mask = LVIF_TEXT | LVIF_PARAM; |
149 |
lvi.pszText = (char *)text; |
150 |
lvi.lParam = (LPARAM)magic; |
151 |
rc = ListView_InsertItem (ctx->ctrl, &lvi); |
152 |
if (rc == -1) |
153 |
rc = 1; |
154 |
ctx->items++; |
155 |
return rc; |
156 |
} /* listview_add_item2 */ |
157 |
|
158 |
|
159 |
void* |
160 |
listview_get_item2 (listview_ctrl_t ctx, int pos) |
161 |
{ |
162 |
LVITEM lvi; |
163 |
|
164 |
memset (&lvi, 0, sizeof lvi); |
165 |
lvi.mask = LVIF_PARAM; |
166 |
lvi.iItem = pos; |
167 |
ListView_GetItem (ctx->ctrl, &lvi); |
168 |
return (void*)lvi.lParam; |
169 |
} |
170 |
|
171 |
int |
172 |
listview_set_item2 (listview_ctrl_t ctx, int pos, void *magic) |
173 |
{ |
174 |
LVITEM lvi; |
175 |
|
176 |
memset (&lvi, 0, sizeof (lvi)); |
177 |
lvi.mask = LVIF_PARAM; |
178 |
lvi.iItem = pos; |
179 |
lvi.lParam = (LPARAM)magic; |
180 |
if (ListView_SetItem (ctx->ctrl, &lvi) == -1) |
181 |
return 1; |
182 |
return 0; |
183 |
} |
184 |
|
185 |
|
186 |
void |
187 |
listview_add_sub_item( listview_ctrl_t ctx, int pos, int col, const char *text ) |
188 |
{ |
189 |
ListView_SetItemText( ctx->ctrl, pos, col, (char *)text ); |
190 |
} /* listview_add_sub_item */ |
191 |
|
192 |
|
193 |
int |
194 |
listview_count_items( listview_ctrl_t ctx, int curr_sel ) |
195 |
{ |
196 |
int n; |
197 |
|
198 |
n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) : |
199 |
ListView_GetItemCount( ctx->ctrl ); |
200 |
return n; |
201 |
} /* listview_count_items */ |
202 |
|
203 |
|
204 |
int |
205 |
listview_del_column (listview_ctrl_t ctx, int pos) |
206 |
{ |
207 |
ctx->cols--; |
208 |
return ListView_DeleteColumn (ctx->ctrl, pos)? 0 : 1; |
209 |
} |
210 |
|
211 |
|
212 |
/* Delete a single item from @ctx at position @pos. */ |
213 |
int |
214 |
listview_del_item (listview_ctrl_t ctx, int pos) |
215 |
{ |
216 |
int rc = 0; |
217 |
|
218 |
if (ListView_DeleteItem (ctx->ctrl, pos) == -1) |
219 |
rc = 1; |
220 |
return rc; |
221 |
} |
222 |
|
223 |
|
224 |
/* Delete all selected items in @ctx. */ |
225 |
int |
226 |
listview_del_sel_items (listview_ctrl_t ctx) |
227 |
{ |
228 |
int i, n; |
229 |
|
230 |
n = listview_count_items (ctx, 0); |
231 |
for (i = n; i > -1; i--) { |
232 |
if (listview_get_item_state (ctx, i)) |
233 |
listview_del_item (ctx, i); |
234 |
} |
235 |
return 0; |
236 |
} |
237 |
|
238 |
|
239 |
int |
240 |
listview_del_all_items (listview_ctrl_t ctx) |
241 |
{ |
242 |
int rc = 0; |
243 |
|
244 |
if (ListView_DeleteAllItems (ctx->ctrl) == FALSE) |
245 |
rc = 1; |
246 |
return rc; |
247 |
} |
248 |
|
249 |
|
250 |
int |
251 |
listview_get_item_state( listview_ctrl_t ctx, int pos ) |
252 |
{ |
253 |
return ListView_GetItemState( ctx->ctrl, pos, LVIS_SELECTED ); |
254 |
} /* listview_get_item_state */ |
255 |
|
256 |
|
257 |
int |
258 |
listview_sort_items( listview_ctrl_t ctx, int sortby, listview_cmp sort_cb ) |
259 |
{ |
260 |
HWND hheader; |
261 |
HDITEM hdi; |
262 |
int idx; |
263 |
|
264 |
ListView_SortItems (ctx->ctrl, sort_cb, sortby); |
265 |
hheader = ListView_GetHeader (ctx->ctrl); |
266 |
if (!hheader) |
267 |
return 0; |
268 |
|
269 |
/* Remove image from all header fields */ |
270 |
memset (&hdi, 0, sizeof(hdi)); |
271 |
|
272 |
for (int i=0; i < 7; i++) { |
273 |
hdi.mask = HDI_FORMAT; |
274 |
Header_GetItem (hheader, i, &hdi); |
275 |
hdi.fmt &= ~HDF_IMAGE; |
276 |
Header_SetItem (hheader, i, &hdi); |
277 |
} |
278 |
|
279 |
switch (sortby & ~KEYLIST_SORT_DESC) { |
280 |
case KEY_SORT_USERID: idx = 0; break; |
281 |
case KEY_SORT_KEYID: idx = 1; break; |
282 |
case KEY_SORT_IS_SECRET: idx = 2; break; |
283 |
case KEY_SORT_LEN: idx = 3; break; |
284 |
case KEY_SORT_VALIDITY: idx = 5; break; |
285 |
case KEY_SORT_OTRUST: idx = 6; break; |
286 |
case KEY_SORT_CREATED: idx = 7; break; |
287 |
case KEY_SORT_ALGO: idx = 8; break; |
288 |
default: idx = 0; break; |
289 |
} |
290 |
|
291 |
/* Set image to currently sorted field */ |
292 |
memset (&hdi, 0, sizeof(hdi)); |
293 |
hdi.mask = HDI_IMAGE | HDI_FORMAT; |
294 |
Header_GetItem (hheader, idx, &hdi); |
295 |
hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT; |
296 |
hdi.iImage = imagelist_getindex((sortby & KEYLIST_SORT_DESC) ? |
297 |
IMI_SORT_DOWNARROW : IMI_SORT_UPARROW); |
298 |
Header_SetItem (hheader, idx, &hdi); |
299 |
return 0; |
300 |
} /* listview_sort_items */ |
301 |
|
302 |
|
303 |
int |
304 |
listview_get_curr_pos( listview_ctrl_t ctx ) |
305 |
{ |
306 |
return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED ); |
307 |
} /* listview_get_curr_pos */ |
308 |
|
309 |
|
310 |
int |
311 |
listview_get_item_text (listview_ctrl_t ctx, int pos, int col, char *text, |
312 |
int maxbytes) |
313 |
{ |
314 |
if (pos == -1) { |
315 |
pos = listview_get_curr_pos (ctx); |
316 |
if (pos == -1) |
317 |
return -1; |
318 |
} |
319 |
ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes); |
320 |
return 0; |
321 |
} /* listview_get_item_text */ |
322 |
|
323 |
|
324 |
void |
325 |
listview_set_ext_style( listview_ctrl_t ctx ) |
326 |
{ |
327 |
ListView_SetExtendedListViewStyle( ctx->ctrl, LVS_EX_FULLROWSELECT ); |
328 |
} /* listview_set_ext_style */ |
329 |
|
330 |
|
331 |
int |
332 |
listview_set_column_order( listview_ctrl_t ctx, int *array ) |
333 |
{ |
334 |
return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array ); |
335 |
} /* listview_set_column_order */ |
336 |
|
337 |
|
338 |
void |
339 |
listview_select_all (listview_ctrl_t ctx) |
340 |
{ |
341 |
ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED ); |
342 |
} /* listview_select_all */ |
343 |
|
344 |
|
345 |
void |
346 |
listview_deselect_all (listview_ctrl_t ctx) |
347 |
{ |
348 |
ListView_SetItemState (ctx->ctrl, -1, ~LVNI_SELECTED, LVNI_SELECTED); |
349 |
} |
350 |
|
351 |
|
352 |
void |
353 |
listview_select_one (listview_ctrl_t ctx, int pos) |
354 |
{ |
355 |
ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED, LVIS_FOCUSED|LVIS_SELECTED); |
356 |
} |
357 |
|
358 |
|
359 |
void |
360 |
listview_scroll (listview_ctrl_t ctx, int oldpos, int newpos) |
361 |
{ |
362 |
const int size=14; |
363 |
|
364 |
if (oldpos == -1) |
365 |
oldpos = 0; |
366 |
//log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d", oldpos, newpos, newpos-oldpos); |
367 |
ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size); |
368 |
} |
369 |
|
370 |
|
371 |
int |
372 |
listview_find (listview_ctrl_t ctx, const char * str) |
373 |
{ |
374 |
LVFINDINFO inf; |
375 |
int pos; |
376 |
|
377 |
memset (&inf, 0, sizeof (inf)); |
378 |
inf.flags = LVFI_STRING|LVFI_PARTIAL; |
379 |
inf.psz = str; |
380 |
pos = ListView_FindItem (ctx->ctrl, -1, &inf); |
381 |
return pos; |
382 |
} |
383 |
|
384 |
|
385 |
void |
386 |
listview_setview (listview_ctrl_t ctx, DWORD view) |
387 |
{ |
388 |
DWORD style = GetWindowLong (ctx->ctrl, GWL_STYLE); |
389 |
if ((style & LVS_TYPEMASK) != view) |
390 |
SetWindowLong (ctx->ctrl, GWL_STYLE, (style & ~LVS_TYPEMASK) | view); |
391 |
} |
392 |
|
393 |
|
394 |
void |
395 |
listview_set_image_list (listview_ctrl_t ctx, HICON *ico, DWORD nicons) |
396 |
{ |
397 |
HIMAGELIST hil; |
398 |
DWORD i; |
399 |
|
400 |
hil = ImageList_Create (16, 16, ILC_COLOR8|ILC_MASK, nicons, 1); |
401 |
ImageList_SetBkColor (hil, CLR_NONE); |
402 |
for (i=0; i < nicons; i++) |
403 |
ImageList_AddIcon (hil, ico[i]); |
404 |
ListView_SetImageList (ctx->ctrl, hil, LVSIL_SMALL); |
405 |
} |
406 |
|