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

Annotation of /trunk/Src/wptListView.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 236 - (hide annotations)
Wed Jun 28 06:59:30 2006 UTC (18 years, 8 months ago) by twoaday
File size: 10011 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26