1 |
/* wptListView.cpp - Dynamic list view control |
/* wptListView.cpp - Dynamic list view control |
2 |
* Copyright (C) 2000-2005 Timo Schulz |
* Copyright (C) 2000-2006 Timo Schulz |
3 |
* Copyright (C) 2004 Andreas Jobs |
* Copyright (C) 2004 Andreas Jobs |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
18 |
* along with WinPT; if not, write to the Free Software Foundation, |
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
*/ |
21 |
|
|
22 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
23 |
#include <config.h> |
#include <config.h> |
24 |
#endif |
#endif |
34 |
#include "wptTypes.h" |
#include "wptTypes.h" |
35 |
#include "wptGPG.h" |
#include "wptGPG.h" |
36 |
#include "wptKeylist.h" |
#include "wptKeylist.h" |
37 |
|
#include "wptUTF8.h" |
38 |
#include "resource.h" |
#include "resource.h" |
39 |
|
|
40 |
|
|
41 |
int |
int |
42 |
listview_new( listview_ctrl_t *ctx ) |
listview_new (listview_ctrl_t *ctx) |
43 |
{ |
{ |
44 |
struct listview_ctrl_s *c; |
struct listview_ctrl_s *c; |
45 |
|
|
46 |
c = new struct listview_ctrl_s; |
c = new struct listview_ctrl_s; |
47 |
if( !c ) |
if (!c) |
48 |
BUG( NULL ); |
BUG (NULL); |
49 |
c->cols = 0; |
c->cols = 0; |
50 |
c->items = 0; |
c->items = 0; |
51 |
c->ctrl = NULL; |
c->ctrl = NULL; |
52 |
|
c->hil = NULL; |
53 |
|
c->ext_chkbox = 0; |
54 |
*ctx = c; |
*ctx = c; |
55 |
return 0; |
return 0; |
56 |
} /* listview_new */ |
} |
57 |
|
|
58 |
|
|
59 |
void |
void |
60 |
listview_release( listview_ctrl_t ctx ) |
listview_release (listview_ctrl_t ctx) |
61 |
{ |
{ |
62 |
if( ctx ) { |
if (!ctx) |
63 |
ctx->cols = 0; |
return; |
64 |
ctx->ctrl = NULL; |
|
65 |
ctx->items = 0; |
ctx->cols = 0; |
66 |
delete ctx; |
ctx->ctrl = NULL; |
67 |
ctx = NULL; |
ctx->items = 0; |
68 |
} |
if (ctx->hil) |
69 |
} /* listview_release */ |
ImageList_Destroy (ctx->hil); |
70 |
|
delete ctx; |
71 |
|
} |
72 |
|
|
73 |
|
|
74 |
|
int |
75 |
|
listview_set_column_width (listview_ctrl_t ctx, int col, int width) |
76 |
|
{ |
77 |
|
LVCOLUMN lvc; |
78 |
|
|
79 |
|
memset (&lvc, 0, sizeof (lvc)); |
80 |
|
lvc.mask = LVCF_WIDTH; |
81 |
|
lvc.cx = width; |
82 |
|
ListView_SetColumn (ctx->ctrl, col, &lvc); |
83 |
|
return 0; |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
int |
int |
100 |
rc = 1; |
rc = 1; |
101 |
ctx->cols++; |
ctx->cols++; |
102 |
return rc; |
return rc; |
103 |
} /* listview_add_column */ |
} |
104 |
|
|
105 |
|
|
106 |
int |
int |
111 |
|
|
112 |
memset (&lvi, 0, sizeof lvi); |
memset (&lvi, 0, sizeof lvi); |
113 |
lvi.iItem = pos; |
lvi.iItem = pos; |
114 |
rc = ListView_InsertItem (ctx->ctrl, &lvi); |
if (ListView_InsertItem (ctx->ctrl, &lvi) != pos) |
115 |
if (rc == -1) |
rc = -1; |
|
rc = 1; |
|
116 |
ctx->items++; |
ctx->items++; |
117 |
return rc; |
return rc; |
118 |
} |
} |
132 |
rc = 1; |
rc = 1; |
133 |
ctx->items++; |
ctx->items++; |
134 |
return rc; |
return rc; |
135 |
} /* listview_add_item */ |
} |
136 |
|
|
137 |
|
|
138 |
int |
int |
139 |
listview_add_item2 (listview_ctrl_t ctx, const char *text, void *magic) |
listview_add_item_image (listview_ctrl_t ctx, const char *text, int image) |
140 |
{ |
{ |
141 |
int rc = 0; |
int rc = 0; |
142 |
LV_ITEM lvi; |
LV_ITEM lvi; |
143 |
|
|
144 |
memset( &lvi, 0, sizeof lvi ); |
memset( &lvi, 0, sizeof lvi ); |
145 |
|
lvi.mask = LVIF_TEXT | LVIF_IMAGE; |
146 |
|
lvi.pszText = (char *)text; |
147 |
|
lvi.iImage = image; |
148 |
|
rc = ListView_InsertItem (ctx->ctrl, &lvi); |
149 |
|
if (rc == -1) |
150 |
|
rc = 1; |
151 |
|
ctx->items++; |
152 |
|
return rc; |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
|
int |
157 |
|
listview_add_item2 (listview_ctrl_t ctx, const char *text, void *magic) |
158 |
|
{ |
159 |
|
int rc = 0; |
160 |
|
LVITEM lvi; |
161 |
|
|
162 |
|
memset (&lvi, 0, sizeof (lvi)); |
163 |
lvi.mask = LVIF_TEXT | LVIF_PARAM; |
lvi.mask = LVIF_TEXT | LVIF_PARAM; |
164 |
lvi.pszText = (char *)text; |
lvi.pszText = (char *)text; |
165 |
lvi.lParam = (LPARAM)magic; |
lvi.lParam = (LPARAM)magic; |
166 |
rc = ListView_InsertItem( ctx->ctrl, &lvi ); |
rc = ListView_InsertItem (ctx->ctrl, &lvi); |
167 |
if( rc == -1 ) |
if (rc == -1) |
168 |
rc = 1; |
rc = 1; |
169 |
ctx->items++; |
ctx->items++; |
170 |
return rc; |
return rc; |
171 |
} /* listview_add_item2 */ |
} |
172 |
|
|
173 |
|
|
174 |
void* |
void* |
199 |
|
|
200 |
|
|
201 |
void |
void |
202 |
listview_add_sub_item( listview_ctrl_t ctx, int pos, int col, const char *text ) |
listview_add_sub_item (listview_ctrl_t ctx, int pos, int col, const char *text) |
203 |
{ |
{ |
204 |
ListView_SetItemText( ctx->ctrl, pos, col, (char *)text ); |
char *utf8_txt; |
205 |
} /* listview_add_sub_item */ |
|
206 |
|
utf8_txt = utf8_to_native (text); |
207 |
|
ListView_SetItemText (ctx->ctrl, pos, col, utf8_txt); |
208 |
|
safe_free (utf8_txt); |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
int |
int |
217 |
n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) : |
n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) : |
218 |
ListView_GetItemCount( ctx->ctrl ); |
ListView_GetItemCount( ctx->ctrl ); |
219 |
return n; |
return n; |
220 |
} /* listview_count_items */ |
} |
221 |
|
|
222 |
|
|
223 |
|
int |
224 |
|
listview_del_column (listview_ctrl_t ctx, int pos) |
225 |
|
{ |
226 |
|
ctx->cols--; |
227 |
|
return ListView_DeleteColumn (ctx->ctrl, pos)? 0 : 1; |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
|
/* Delete a single item from @ctx at position @pos. */ |
232 |
int |
int |
233 |
listview_del_item( listview_ctrl_t ctx, int pos ) |
listview_del_item (listview_ctrl_t ctx, int pos) |
234 |
{ |
{ |
235 |
int rc = 0; |
int rc = 0; |
236 |
|
|
237 |
if( ListView_DeleteItem( ctx->ctrl, pos ) == -1 ) |
if (ListView_DeleteItem (ctx->ctrl, pos) == -1) |
238 |
rc = 1; |
rc = 1; |
239 |
return rc; |
return rc; |
240 |
} /* listview_del_item */ |
} |
241 |
|
|
242 |
|
|
243 |
|
/* Delete all selected items in @ctx. */ |
244 |
int |
int |
245 |
listview_del_items( listview_ctrl_t ctx ) |
listview_del_sel_items (listview_ctrl_t ctx) |
246 |
{ |
{ |
247 |
int i, n; |
int i, n; |
248 |
|
|
|
/* delete all selected entries */ |
|
249 |
n = listview_count_items (ctx, 0); |
n = listview_count_items (ctx, 0); |
250 |
for( i = n; i > 0; i-- ) { |
for (i = n; i > -1; i--) { |
251 |
if ( listview_get_item_state( ctx, i ) ) |
if (listview_get_item_state (ctx, i)) |
252 |
listview_del_item( ctx, i ); |
listview_del_item (ctx, i); |
253 |
} |
} |
254 |
return 0; |
return 0; |
255 |
} /* listview_del_items */ |
} |
256 |
|
|
257 |
|
|
258 |
int |
int |
259 |
listview_del_all( listview_ctrl_t ctx ) |
listview_del_all_items (listview_ctrl_t ctx) |
260 |
{ |
{ |
261 |
int rc = 0; |
int rc = 0; |
262 |
|
|
263 |
if( ListView_DeleteAllItems( ctx->ctrl ) == FALSE ) |
if (ListView_DeleteAllItems (ctx->ctrl) == FALSE) |
264 |
rc = 1; |
rc = 1; |
265 |
return rc; |
return rc; |
266 |
} /* listview_del_all */ |
} |
267 |
|
|
268 |
|
|
269 |
|
/* Return the index of the selected item. |
270 |
|
Support both selections and checkboxes. */ |
271 |
int |
int |
272 |
listview_get_item_state( listview_ctrl_t ctx, int pos ) |
listview_get_selected_item (listview_ctrl_t lv) |
273 |
{ |
{ |
274 |
return ListView_GetItemState( ctx->ctrl, pos, LVIS_SELECTED ); |
int pos, i; |
275 |
} /* listview_get_item_state */ |
|
276 |
|
pos = listview_get_curr_pos (lv); |
277 |
|
if (pos != -1) |
278 |
|
return pos; |
279 |
|
for (i=0; i < listview_count_items (lv, 0); i++) { |
280 |
|
if (listview_get_item_state (lv, i)) |
281 |
|
return i; |
282 |
|
} |
283 |
|
return -1; |
284 |
|
} |
285 |
|
|
286 |
|
|
287 |
|
/* Return if the given element is selected or if the ext |
288 |
|
style is used, if the checkbox is activated. */ |
289 |
|
int |
290 |
|
listview_get_item_state (listview_ctrl_t ctx, int pos) |
291 |
|
{ |
292 |
|
int ret; |
293 |
|
if (ctx->ext_chkbox) |
294 |
|
ret = ListView_GetCheckState (ctx->ctrl, pos); |
295 |
|
else |
296 |
|
ret = ListView_GetItemState (ctx->ctrl, pos, LVIS_SELECTED); |
297 |
|
return ret; |
298 |
|
} |
299 |
|
|
300 |
|
|
301 |
int |
int |
341 |
IMI_SORT_DOWNARROW : IMI_SORT_UPARROW); |
IMI_SORT_DOWNARROW : IMI_SORT_UPARROW); |
342 |
Header_SetItem (hheader, idx, &hdi); |
Header_SetItem (hheader, idx, &hdi); |
343 |
return 0; |
return 0; |
344 |
} /* listview_sort_items */ |
} |
345 |
|
|
346 |
|
|
347 |
int |
int |
348 |
listview_get_curr_pos( listview_ctrl_t ctx ) |
listview_get_curr_pos( listview_ctrl_t ctx ) |
349 |
{ |
{ |
350 |
return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED ); |
return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED ); |
351 |
} /* listview_get_curr_pos */ |
} |
352 |
|
|
353 |
|
|
354 |
int |
int |
362 |
} |
} |
363 |
ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes); |
ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes); |
364 |
return 0; |
return 0; |
365 |
} /* listview_get_item_text */ |
} |
366 |
|
|
367 |
|
|
368 |
|
/* Use extended style with checkboxes for each item. */ |
369 |
void |
void |
370 |
listview_set_ext_style( listview_ctrl_t ctx ) |
listview_set_chkbox_style (listview_ctrl_t ctx) |
371 |
|
{ |
372 |
|
ListView_SetExtendedListViewStyle (ctx->ctrl, LVS_EX_CHECKBOXES); |
373 |
|
ctx->ext_chkbox = 1; |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
|
/* Use extended style to select the entire row. */ |
378 |
|
void |
379 |
|
listview_set_ext_style (listview_ctrl_t ctx) |
380 |
{ |
{ |
381 |
ListView_SetExtendedListViewStyle( ctx->ctrl, LVS_EX_FULLROWSELECT ); |
ListView_SetExtendedListViewStyle (ctx->ctrl, LVS_EX_FULLROWSELECT); |
382 |
} /* listview_set_ext_style */ |
} |
383 |
|
|
384 |
|
|
385 |
int |
int |
386 |
listview_set_column_order( listview_ctrl_t ctx, int *array ) |
listview_set_column_order( listview_ctrl_t ctx, int *array ) |
387 |
{ |
{ |
388 |
return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array ); |
return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array ); |
389 |
} /* listview_set_column_order */ |
} |
390 |
|
|
391 |
|
|
392 |
void |
void |
393 |
listview_select_all (listview_ctrl_t ctx) |
listview_select_all (listview_ctrl_t ctx) |
394 |
{ |
{ |
395 |
ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED ); |
ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED ); |
396 |
} /* listview_select_all */ |
} |
397 |
|
|
398 |
|
|
399 |
void |
void |
435 |
return pos; |
return pos; |
436 |
} |
} |
437 |
|
|
|
/** Some functions to make the handling with TreeView Controls easier **/ |
|
|
int |
|
|
treeview_add_item( HWND tree, HTREEITEM parent, const char *text ) |
|
|
{ |
|
|
TVINSERTSTRUCT tvis; |
|
|
|
|
|
memset( &tvis, 0, sizeof tvis ); |
|
|
tvis.hParent = parent; |
|
|
tvis.hInsertAfter = TVI_LAST; |
|
|
tvis.item.mask = TVIF_TEXT; |
|
|
tvis.item.pszText = (char *)text; |
|
|
TreeView_InsertItem( tree, &tvis ); |
|
438 |
|
|
439 |
return 0; |
void |
440 |
} /* treeview_add_item */ |
listview_setview (listview_ctrl_t ctx, DWORD view) |
441 |
|
{ |
442 |
|
DWORD style = GetWindowLong (ctx->ctrl, GWL_STYLE); |
443 |
|
if ((style & LVS_TYPEMASK) != view) |
444 |
|
SetWindowLong (ctx->ctrl, GWL_STYLE, (style & ~LVS_TYPEMASK) | view); |
445 |
|
} |
446 |
|
|
447 |
|
|
448 |
|
void |
449 |
|
listview_set_image_list (listview_ctrl_t ctx, int cx, int cy, |
450 |
|
HICON *ico, DWORD nicons) |
451 |
|
{ |
452 |
|
HIMAGELIST hil; |
453 |
|
DWORD i; |
454 |
|
|
455 |
|
if (cx == 0 || cy == 0) |
456 |
|
cx = cy = 16; |
457 |
|
|
458 |
|
hil = ImageList_Create (cx, cy, ILC_COLOR8|ILC_MASK, nicons, 1); |
459 |
|
ImageList_SetBkColor (hil, CLR_NONE); |
460 |
|
for (i=0; i < nicons; i++) |
461 |
|
ImageList_AddIcon (hil, ico[i]); |
462 |
|
ListView_SetImageList (ctx->ctrl, hil, LVSIL_SMALL); |
463 |
|
} |