/[winpt]/trunk/Src/wptListView.cpp
ViewVC logotype

Annotation of /trunk/Src/wptListView.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 205 - (hide annotations)
Thu Apr 27 12:46:03 2006 UTC (18 years, 10 months ago) by twoaday
File size: 9870 byte(s)
2006-04-27  Timo Schulz  <ts@g10code.de>
                                                                                
        * wptKeyManager.cpp (km_get_key_ptr): New.
        * wptListview.cpp (listview_get_item_text): Drop utf8 support.
        * wptKeyCache.cpp (keycache_decode_uids): New.
        (free_native_uids): New.
        * wptKeyEdit.cpp (uid_inf_colon_handler): Do utf8 decodig here.
                                                                                
2006-04-26  Timo Schulz  <ts@g10code.de>
                                                                                
        * wptKeylist.cpp (get_keyid_from_fpr): New.
        * wptDecryptClipDlg.cpp (clip_decrypt_dlg): Use it here.
        * wptVerifyList.cpp (verlist_add_sig): Likewise.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26