1 |
/* wptListView.cpp - Dynamic list view control |
/* wptListView.cpp - Dynamic list view control |
2 |
* Copyright (C) 2000-2006 Timo Schulz |
* Copyright (C) 2000-2007, 2009 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. |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
* General Public License for more details. |
* General Public License for more details. |
|
* |
|
|
* You should have received a copy of the GNU General Public License |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
*/ |
*/ |
|
|
|
17 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
18 |
#include <config.h> |
#include <config.h> |
19 |
#endif |
#endif |
33 |
#include "resource.h" |
#include "resource.h" |
34 |
|
|
35 |
|
|
36 |
int |
void |
37 |
listview_new (listview_ctrl_t *ctx) |
listview_new (listview_ctrl_t *ctx, HWND ctrl) |
38 |
{ |
{ |
39 |
struct listview_ctrl_s *c; |
struct listview_ctrl_s *c; |
40 |
|
|
41 |
c = new struct listview_ctrl_s; |
c = new struct listview_ctrl_s; |
42 |
if (!c) |
if (!c) |
43 |
BUG (NULL); |
BUG (NULL); |
44 |
c->cols = 0; |
memset (c, 0, sizeof *c); |
45 |
c->items = 0; |
c->ctrl = ctrl? ctrl : NULL; |
|
c->ctrl = NULL; |
|
46 |
c->hil = NULL; |
c->hil = NULL; |
|
c->ext_chkbox = 0; |
|
47 |
*ctx = c; |
*ctx = c; |
|
return 0; |
|
48 |
} |
} |
49 |
|
|
50 |
|
|
82 |
int rc = 0; |
int rc = 0; |
83 |
LV_COLUMN lvc; |
LV_COLUMN lvc; |
84 |
|
|
85 |
memset( &lvc, 0, sizeof lvc ); |
memset (&lvc, 0, sizeof lvc); |
86 |
lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM; |
lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM; |
87 |
lvc.pszText = col->fieldname; |
lvc.pszText = col->fieldname; |
88 |
lvc.cx = col->width; |
lvc.cx = col->width; |
89 |
lvc.fmt = LVCFMT_LEFT; |
lvc.fmt = LVCFMT_LEFT; |
90 |
lvc.iSubItem = col->pos; |
lvc.iSubItem = col->pos; |
91 |
if( ListView_InsertColumn (ctx->ctrl, col->pos, &lvc) == -1) |
if (ListView_InsertColumn (ctx->ctrl, col->pos, &lvc) == -1) |
92 |
rc = 1; |
rc = 1; |
93 |
ctx->cols++; |
ctx->cols++; |
94 |
return rc; |
return rc; |
111 |
|
|
112 |
|
|
113 |
int |
int |
114 |
listview_add_item( listview_ctrl_t ctx, const char *text ) |
listview_add_item (listview_ctrl_t ctx, const char *text) |
115 |
{ |
{ |
116 |
int rc = 0; |
int rc = 0; |
117 |
LV_ITEM lvi; |
LV_ITEM lvi; |
118 |
|
|
119 |
memset( &lvi, 0, sizeof lvi ); |
memset (&lvi, 0, sizeof lvi); |
120 |
lvi.mask = LVIF_TEXT; |
lvi.mask = LVIF_TEXT; |
121 |
lvi.pszText = (char *)text; |
lvi.pszText = (char *)text; |
122 |
rc = ListView_InsertItem( ctx->ctrl, &lvi ); |
rc = ListView_InsertItem (ctx->ctrl, &lvi); |
123 |
if( rc == -1 ) |
if( rc == -1 ) |
124 |
rc = 1; |
rc = 1; |
125 |
ctx->items++; |
ctx->items++; |
175 |
return (void*)lvi.lParam; |
return (void*)lvi.lParam; |
176 |
} |
} |
177 |
|
|
178 |
|
|
179 |
int |
int |
180 |
listview_set_item2 (listview_ctrl_t ctx, int pos, void *magic) |
listview_set_item2 (listview_ctrl_t ctx, int pos, void *magic) |
181 |
{ |
{ |
194 |
void |
void |
195 |
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) |
196 |
{ |
{ |
197 |
char *utf8_txt; |
ListView_SetItemText (ctx->ctrl, pos, col, (char*)text); |
|
|
|
|
utf8_txt = utf8_to_native (text); |
|
|
ListView_SetItemText (ctx->ctrl, pos, col, utf8_txt); |
|
|
safe_free (utf8_txt); |
|
198 |
} |
} |
199 |
|
|
200 |
|
|
201 |
int |
int |
202 |
listview_count_items( listview_ctrl_t ctx, int curr_sel ) |
listview_count_items (listview_ctrl_t ctx, int curr_sel) |
203 |
{ |
{ |
204 |
int n; |
int n; |
205 |
|
|
288 |
|
|
289 |
|
|
290 |
int |
int |
291 |
listview_sort_items( listview_ctrl_t ctx, int sortby, listview_cmp sort_cb ) |
listview_sort_items (listview_ctrl_t ctx, int sortby, listview_cmp sort_cb) |
292 |
{ |
{ |
|
HWND hheader; |
|
|
HDITEM hdi; |
|
|
int idx; |
|
|
|
|
293 |
ListView_SortItems (ctx->ctrl, sort_cb, sortby); |
ListView_SortItems (ctx->ctrl, sort_cb, sortby); |
|
hheader = ListView_GetHeader (ctx->ctrl); |
|
|
if (!hheader) |
|
|
return 0; |
|
|
|
|
|
/* Remove image from all header fields */ |
|
|
memset (&hdi, 0, sizeof(hdi)); |
|
|
|
|
|
for (int i=0; i < 7; i++) { |
|
|
hdi.mask = HDI_FORMAT; |
|
|
Header_GetItem (hheader, i, &hdi); |
|
|
hdi.fmt &= ~HDF_IMAGE; |
|
|
Header_SetItem (hheader, i, &hdi); |
|
|
} |
|
|
|
|
|
switch (sortby & ~KEYLIST_SORT_DESC) { |
|
|
case KEY_SORT_USERID: idx = 0; break; |
|
|
case KEY_SORT_KEYID: idx = 1; break; |
|
|
case KEY_SORT_IS_SECRET: idx = 2; break; |
|
|
case KEY_SORT_LEN: idx = 3; break; |
|
|
case KEY_SORT_VALIDITY: idx = 5; break; |
|
|
case KEY_SORT_OTRUST: idx = 6; break; |
|
|
case KEY_SORT_CREATED: idx = 7; break; |
|
|
case KEY_SORT_ALGO: idx = 8; break; |
|
|
default: idx = 0; break; |
|
|
} |
|
|
|
|
|
/* Set image to currently sorted field */ |
|
|
memset (&hdi, 0, sizeof(hdi)); |
|
|
hdi.mask = HDI_IMAGE | HDI_FORMAT; |
|
|
Header_GetItem (hheader, idx, &hdi); |
|
|
hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT; |
|
|
hdi.iImage = imagelist_getindex((sortby & KEYLIST_SORT_DESC) ? |
|
|
IMI_SORT_DOWNARROW : IMI_SORT_UPARROW); |
|
|
Header_SetItem (hheader, idx, &hdi); |
|
294 |
return 0; |
return 0; |
295 |
} |
} |
296 |
|
|
297 |
|
|
298 |
int |
int |
299 |
listview_get_curr_pos( listview_ctrl_t ctx ) |
listview_get_curr_pos (listview_ctrl_t ctx) |
300 |
{ |
{ |
301 |
return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED ); |
return ListView_GetNextItem (ctx->ctrl, -1, LVNI_SELECTED); |
302 |
} |
} |
303 |
|
|
304 |
|
|
320 |
void |
void |
321 |
listview_set_chkbox_style (listview_ctrl_t ctx) |
listview_set_chkbox_style (listview_ctrl_t ctx) |
322 |
{ |
{ |
323 |
ListView_SetExtendedListViewStyle (ctx->ctrl, LVS_EX_CHECKBOXES); |
DWORD flag = LVS_EX_CHECKBOXES; |
324 |
|
DWORD style; |
325 |
|
style = ListView_GetExtendedListViewStyle (ctx->ctrl)|flag; |
326 |
|
ListView_SetExtendedListViewStyle (ctx->ctrl, style); |
327 |
ctx->ext_chkbox = 1; |
ctx->ext_chkbox = 1; |
328 |
} |
} |
329 |
|
|
331 |
/* Use extended style to select the entire row. */ |
/* Use extended style to select the entire row. */ |
332 |
void |
void |
333 |
listview_set_ext_style (listview_ctrl_t ctx) |
listview_set_ext_style (listview_ctrl_t ctx) |
334 |
{ |
{ |
335 |
ListView_SetExtendedListViewStyle (ctx->ctrl, LVS_EX_FULLROWSELECT); |
DWORD flag = LVS_EX_FULLROWSELECT; |
336 |
|
DWORD style; |
337 |
|
style = ListView_GetExtendedListViewStyle (ctx->ctrl)|flag; |
338 |
|
ListView_SetExtendedListViewStyle (ctx->ctrl, style); |
339 |
} |
} |
340 |
|
|
341 |
|
|
342 |
|
void |
343 |
|
listview_set_grid_style (listview_ctrl_t ctx) |
344 |
|
{ |
345 |
|
DWORD flag = LVS_EX_GRIDLINES; |
346 |
|
DWORD style; |
347 |
|
style = ListView_GetExtendedListViewStyle (ctx->ctrl)|flag; |
348 |
|
ListView_SetExtendedListViewStyle (ctx->ctrl, style); |
349 |
|
} |
350 |
|
|
351 |
int |
int |
352 |
listview_set_column_order( listview_ctrl_t ctx, int *array ) |
listview_set_column_order (listview_ctrl_t ctx, int *array) |
353 |
{ |
{ |
354 |
return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array ); |
return ListView_SetColumnOrderArray (ctx->ctrl, ctx->cols, array); |
355 |
} |
} |
356 |
|
|
357 |
|
|
358 |
void |
void |
359 |
listview_select_all (listview_ctrl_t ctx) |
listview_select_all (listview_ctrl_t ctx) |
360 |
{ |
{ |
361 |
ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED ); |
ListView_SetItemState (ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED); |
362 |
} |
} |
363 |
|
|
364 |
|
|
372 |
void |
void |
373 |
listview_select_one (listview_ctrl_t ctx, int pos) |
listview_select_one (listview_ctrl_t ctx, int pos) |
374 |
{ |
{ |
375 |
ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED, LVIS_FOCUSED|LVIS_SELECTED); |
ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED, |
376 |
|
LVIS_FOCUSED|LVIS_SELECTED); |
377 |
} |
} |
378 |
|
|
379 |
|
|
384 |
|
|
385 |
if (oldpos == -1) |
if (oldpos == -1) |
386 |
oldpos = 0; |
oldpos = 0; |
|
//log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d", oldpos, newpos, newpos-oldpos); |
|
387 |
ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size); |
ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size); |
388 |
} |
} |
389 |
|
|
390 |
|
|
391 |
|
static int |
392 |
|
listview_find_substr (listview_ctrl_t ctx, const char *str) |
393 |
|
{ |
394 |
|
char buf[256]; |
395 |
|
int i, n, fnd = 0; |
396 |
|
|
397 |
|
/* We assume the first column contains text. */ |
398 |
|
n = listview_count_items (ctx, 0); |
399 |
|
for (i = 0; i < n; i++) { |
400 |
|
listview_get_item_text (ctx, i, 0, buf, DIM (buf)-1); |
401 |
|
if (stristr (buf, str)) { |
402 |
|
fnd = 1; |
403 |
|
break; |
404 |
|
} |
405 |
|
} |
406 |
|
if (!fnd) |
407 |
|
i = -1; /* not found */ |
408 |
|
return i; |
409 |
|
} |
410 |
|
|
411 |
|
|
412 |
int |
int |
413 |
listview_find (listview_ctrl_t ctx, const char * str) |
listview_find (listview_ctrl_t ctx, const char *str, int substr) |
414 |
{ |
{ |
415 |
LVFINDINFO inf; |
LVFINDINFO inf; |
416 |
int pos; |
int pos; |
417 |
|
|
418 |
|
/* XXX: allow to make a substring search. */ |
419 |
memset (&inf, 0, sizeof (inf)); |
memset (&inf, 0, sizeof (inf)); |
420 |
inf.flags = LVFI_STRING|LVFI_PARTIAL; |
inf.flags = LVFI_STRING|LVFI_PARTIAL; |
421 |
inf.psz = str; |
inf.psz = str; |
422 |
pos = ListView_FindItem (ctx->ctrl, -1, &inf); |
pos = ListView_FindItem (ctx->ctrl, -1, &inf); |
423 |
|
if (pos == -1 && substr) |
424 |
|
pos = listview_find_substr (ctx, str); |
425 |
return pos; |
return pos; |
426 |
} |
} |
427 |
|
|
440 |
HICON *ico, DWORD nicons) |
HICON *ico, DWORD nicons) |
441 |
{ |
{ |
442 |
HIMAGELIST hil; |
HIMAGELIST hil; |
|
DWORD i; |
|
443 |
|
|
444 |
if (cx == 0 || cy == 0) |
if (cx <= 0 || cy <= 0) |
445 |
cx = cy = 16; |
cx = cy = 16; /* default size. */ |
446 |
|
|
447 |
hil = ImageList_Create (cx, cy, ILC_COLOR8|ILC_MASK, nicons, 1); |
hil = ImageList_Create (cx, cy, ILC_COLOR8|ILC_MASK, nicons, 1); |
448 |
ImageList_SetBkColor (hil, CLR_NONE); |
ImageList_SetBkColor (hil, CLR_NONE); |
449 |
for (i=0; i < nicons; i++) |
for (DWORD i=0; i < nicons; i++) |
450 |
ImageList_AddIcon (hil, ico[i]); |
ImageList_AddIcon (hil, ico[i]); |
451 |
ListView_SetImageList (ctx->ctrl, hil, LVSIL_SMALL); |
(void)ListView_SetImageList (ctx->ctrl, hil, LVSIL_SMALL); |
452 |
} |
} |