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

Contents of /trunk/Src/wptListView.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 181 - (show annotations)
Tue Mar 14 11:01:22 2006 UTC (18 years, 11 months ago) by twoaday
File size: 9545 byte(s)
2006-03-12  Timo Schulz  <ts@g10code.de>
 
        * wptGPG.cpp (gnupg_load_config): Search for 'ask-cert-expire'.
        * wptKeyPropsDlg.cpp (display_key_info): Automatically update
        sym algorithm preferences if needed.
        * wptKeysignDlg.cpp (date_is_today): New.
        (keysign_dlg_proc): Only allow to set cert expire date if
        the option was found.
        * wptGPGPrefsDlg.cpp (gpgprefs_dlg_proc): Allow to set
        'ask-cert-expire'.
         


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26