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 |
int |
213 |
listview_del_item( listview_ctrl_t ctx, int pos ) |
214 |
{ |
215 |
int rc = 0; |
216 |
|
217 |
if( ListView_DeleteItem( ctx->ctrl, pos ) == -1 ) |
218 |
rc = 1; |
219 |
return rc; |
220 |
} /* listview_del_item */ |
221 |
|
222 |
|
223 |
int |
224 |
listview_del_items( listview_ctrl_t ctx ) |
225 |
{ |
226 |
int i, n; |
227 |
|
228 |
/* delete all selected entries */ |
229 |
n = listview_count_items (ctx, 0); |
230 |
for( i = n; i > 0; i-- ) { |
231 |
if ( listview_get_item_state( ctx, i ) ) |
232 |
listview_del_item( ctx, i ); |
233 |
} |
234 |
return 0; |
235 |
} /* listview_del_items */ |
236 |
|
237 |
|
238 |
int |
239 |
listview_del_all( listview_ctrl_t ctx ) |
240 |
{ |
241 |
int rc = 0; |
242 |
|
243 |
if( ListView_DeleteAllItems( ctx->ctrl ) == FALSE ) |
244 |
rc = 1; |
245 |
return rc; |
246 |
} /* listview_del_all */ |
247 |
|
248 |
|
249 |
int |
250 |
listview_get_item_state( listview_ctrl_t ctx, int pos ) |
251 |
{ |
252 |
return ListView_GetItemState( ctx->ctrl, pos, LVIS_SELECTED ); |
253 |
} /* listview_get_item_state */ |
254 |
|
255 |
|
256 |
int |
257 |
listview_sort_items( listview_ctrl_t ctx, int sortby, listview_cmp sort_cb ) |
258 |
{ |
259 |
HWND hheader; |
260 |
HDITEM hdi; |
261 |
int idx; |
262 |
|
263 |
ListView_SortItems (ctx->ctrl, sort_cb, sortby); |
264 |
hheader = ListView_GetHeader (ctx->ctrl); |
265 |
if (!hheader) |
266 |
return 0; |
267 |
|
268 |
/* Remove image from all header fields */ |
269 |
memset (&hdi, 0, sizeof(hdi)); |
270 |
|
271 |
for (int i=0; i < 7; i++) { |
272 |
hdi.mask = HDI_FORMAT; |
273 |
Header_GetItem (hheader, i, &hdi); |
274 |
hdi.fmt &= ~HDF_IMAGE; |
275 |
Header_SetItem (hheader, i, &hdi); |
276 |
} |
277 |
|
278 |
switch (sortby & ~KEYLIST_SORT_DESC) { |
279 |
case KEY_SORT_USERID: idx = 0; break; |
280 |
case KEY_SORT_KEYID: idx = 1; break; |
281 |
case KEY_SORT_IS_SECRET: idx = 2; break; |
282 |
case KEY_SORT_LEN: idx = 3; break; |
283 |
case KEY_SORT_VALIDITY: idx = 5; break; |
284 |
case KEY_SORT_OTRUST: idx = 6; break; |
285 |
case KEY_SORT_CREATED: idx = 7; break; |
286 |
case KEY_SORT_ALGO: idx = 8; break; |
287 |
default: idx = 0; break; |
288 |
} |
289 |
|
290 |
/* Set image to currently sorted field */ |
291 |
memset (&hdi, 0, sizeof(hdi)); |
292 |
hdi.mask = HDI_IMAGE | HDI_FORMAT; |
293 |
Header_GetItem (hheader, idx, &hdi); |
294 |
hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT; |
295 |
hdi.iImage = imagelist_getindex((sortby & KEYLIST_SORT_DESC) ? |
296 |
IMI_SORT_DOWNARROW : IMI_SORT_UPARROW); |
297 |
Header_SetItem (hheader, idx, &hdi); |
298 |
return 0; |
299 |
} /* listview_sort_items */ |
300 |
|
301 |
|
302 |
int |
303 |
listview_get_curr_pos( listview_ctrl_t ctx ) |
304 |
{ |
305 |
return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED ); |
306 |
} /* listview_get_curr_pos */ |
307 |
|
308 |
|
309 |
int |
310 |
listview_get_item_text (listview_ctrl_t ctx, int pos, int col, char *text, |
311 |
int maxbytes) |
312 |
{ |
313 |
if (pos == -1) { |
314 |
pos = listview_get_curr_pos (ctx); |
315 |
if (pos == -1) |
316 |
return -1; |
317 |
} |
318 |
ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes); |
319 |
return 0; |
320 |
} /* listview_get_item_text */ |
321 |
|
322 |
|
323 |
void |
324 |
listview_set_ext_style( listview_ctrl_t ctx ) |
325 |
{ |
326 |
ListView_SetExtendedListViewStyle( ctx->ctrl, LVS_EX_FULLROWSELECT ); |
327 |
} /* listview_set_ext_style */ |
328 |
|
329 |
|
330 |
int |
331 |
listview_set_column_order( listview_ctrl_t ctx, int *array ) |
332 |
{ |
333 |
return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array ); |
334 |
} /* listview_set_column_order */ |
335 |
|
336 |
|
337 |
void |
338 |
listview_select_all (listview_ctrl_t ctx) |
339 |
{ |
340 |
ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED ); |
341 |
} /* listview_select_all */ |
342 |
|
343 |
|
344 |
void |
345 |
listview_deselect_all (listview_ctrl_t ctx) |
346 |
{ |
347 |
ListView_SetItemState (ctx->ctrl, -1, ~LVNI_SELECTED, LVNI_SELECTED); |
348 |
} |
349 |
|
350 |
|
351 |
void |
352 |
listview_select_one (listview_ctrl_t ctx, int pos) |
353 |
{ |
354 |
ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED, LVIS_FOCUSED|LVIS_SELECTED); |
355 |
} |
356 |
|
357 |
|
358 |
void |
359 |
listview_scroll (listview_ctrl_t ctx, int oldpos, int newpos) |
360 |
{ |
361 |
const int size=14; |
362 |
|
363 |
if (oldpos == -1) |
364 |
oldpos = 0; |
365 |
//log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d", oldpos, newpos, newpos-oldpos); |
366 |
ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size); |
367 |
} |
368 |
|
369 |
|
370 |
int |
371 |
listview_find (listview_ctrl_t ctx, const char * str) |
372 |
{ |
373 |
LVFINDINFO inf; |
374 |
int pos; |
375 |
|
376 |
memset (&inf, 0, sizeof (inf)); |
377 |
inf.flags = LVFI_STRING|LVFI_PARTIAL; |
378 |
inf.psz = str; |
379 |
pos = ListView_FindItem (ctx->ctrl, -1, &inf); |
380 |
return pos; |
381 |
} |
382 |
|
383 |
|
384 |
void |
385 |
listview_setview (listview_ctrl_t ctx, DWORD view) |
386 |
{ |
387 |
DWORD style = GetWindowLong (ctx->ctrl, GWL_STYLE); |
388 |
if ((style & LVS_TYPEMASK) != view) |
389 |
SetWindowLong (ctx->ctrl, GWL_STYLE, (style & ~LVS_TYPEMASK) | view); |
390 |
} |
391 |
|
392 |
|
393 |
void |
394 |
listview_set_image_list (listview_ctrl_t ctx, HICON *ico, DWORD nicons) |
395 |
{ |
396 |
HIMAGELIST hil; |
397 |
DWORD i; |
398 |
|
399 |
hil = ImageList_Create (16, 16, ILC_COLOR16, nicons, 1); |
400 |
for (i=0; i < nicons; i++) |
401 |
ImageList_AddIcon (hil, ico[i]); |
402 |
ListView_SetImageList (ctx->ctrl, hil, LVSIL_SMALL); |
403 |
} |
404 |
|