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

Contents of /trunk/Src/wptListView.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 204 - (show annotations)
Wed Apr 26 06:37:23 2006 UTC (18 years, 10 months ago) by twoaday
File size: 9952 byte(s)
2006-04-25  Timo Schulz  <ts@g10code.de>
                                                                                
        * wptFileManagerDlg.cpp (file_manager_dlg_proc): Handle IDCANCEL.
        * wptMDSumDlg.cpp (hash_selected_files): New.
        (mdsum_dlg_proc): Cleanups.
        * wptKeysignDlg.cpp (keysign_dlg_proc): Fix segv.
                                                                                
2006-04-23  Timo Schulz  <ts@g10code.de>
                                                                                
        * wptW32API.cpp (SetDlgItemText_utf8): New.
        (SetWindowText_utf8): New.
        (GetDlgItemText_utf8): New.
        * wptListView.cpp (listview_add_sub_item): Internal
        utf8 support.


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 "wptUTF8.h"
38 #include "resource.h"
39
40
41 int
42 listview_new (listview_ctrl_t *ctx)
43 {
44 struct listview_ctrl_s *c;
45
46 c = new struct listview_ctrl_s;
47 if (!c)
48 BUG (NULL);
49 c->cols = 0;
50 c->items = 0;
51 c->ctrl = NULL;
52 c->hil = NULL;
53 c->ext_chkbox = 0;
54 *ctx = c;
55 return 0;
56 }
57
58
59 void
60 listview_release (listview_ctrl_t ctx)
61 {
62 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 }
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
88 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 }
104
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 if (ListView_InsertItem (ctx->ctrl, &lvi) != pos)
115 rc = -1;
116 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 }
136
137
138 int
139 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 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;
164 lvi.pszText = (char *)text;
165 lvi.lParam = (LPARAM)magic;
166 rc = ListView_InsertItem (ctx->ctrl, &lvi);
167 if (rc == -1)
168 rc = 1;
169 ctx->items++;
170 return rc;
171 }
172
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 listview_add_sub_item (listview_ctrl_t ctx, int pos, int col, const char *text)
203 {
204 char *utf8_txt;
205
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
213 listview_count_items( listview_ctrl_t ctx, int curr_sel )
214 {
215 int n;
216
217 n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) :
218 ListView_GetItemCount( ctx->ctrl );
219 return n;
220 }
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
233 listview_del_item (listview_ctrl_t ctx, int pos)
234 {
235 int rc = 0;
236
237 if (ListView_DeleteItem (ctx->ctrl, pos) == -1)
238 rc = 1;
239 return rc;
240 }
241
242
243 /* Delete all selected items in @ctx. */
244 int
245 listview_del_sel_items (listview_ctrl_t ctx)
246 {
247 int i, n;
248
249 n = listview_count_items (ctx, 0);
250 for (i = n; i > -1; i--) {
251 if (listview_get_item_state (ctx, i))
252 listview_del_item (ctx, i);
253 }
254 return 0;
255 }
256
257
258 int
259 listview_del_all_items (listview_ctrl_t ctx)
260 {
261 int rc = 0;
262
263 if (ListView_DeleteAllItems (ctx->ctrl) == FALSE)
264 rc = 1;
265 return rc;
266 }
267
268
269 /* Return the index of the selected item.
270 Support both selections and checkboxes. */
271 int
272 listview_get_selected_item (listview_ctrl_t lv)
273 {
274 int pos, i;
275
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
302 listview_sort_items( listview_ctrl_t ctx, int sortby, listview_cmp sort_cb )
303 {
304 HWND hheader;
305 HDITEM hdi;
306 int idx;
307
308 ListView_SortItems (ctx->ctrl, sort_cb, sortby);
309 hheader = ListView_GetHeader (ctx->ctrl);
310 if (!hheader)
311 return 0;
312
313 /* Remove image from all header fields */
314 memset (&hdi, 0, sizeof(hdi));
315
316 for (int i=0; i < 7; i++) {
317 hdi.mask = HDI_FORMAT;
318 Header_GetItem (hheader, i, &hdi);
319 hdi.fmt &= ~HDF_IMAGE;
320 Header_SetItem (hheader, i, &hdi);
321 }
322
323 switch (sortby & ~KEYLIST_SORT_DESC) {
324 case KEY_SORT_USERID: idx = 0; break;
325 case KEY_SORT_KEYID: idx = 1; break;
326 case KEY_SORT_IS_SECRET: idx = 2; break;
327 case KEY_SORT_LEN: idx = 3; break;
328 case KEY_SORT_VALIDITY: idx = 5; break;
329 case KEY_SORT_OTRUST: idx = 6; break;
330 case KEY_SORT_CREATED: idx = 7; break;
331 case KEY_SORT_ALGO: idx = 8; break;
332 default: idx = 0; break;
333 }
334
335 /* Set image to currently sorted field */
336 memset (&hdi, 0, sizeof(hdi));
337 hdi.mask = HDI_IMAGE | HDI_FORMAT;
338 Header_GetItem (hheader, idx, &hdi);
339 hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT;
340 hdi.iImage = imagelist_getindex((sortby & KEYLIST_SORT_DESC) ?
341 IMI_SORT_DOWNARROW : IMI_SORT_UPARROW);
342 Header_SetItem (hheader, idx, &hdi);
343 return 0;
344 }
345
346
347 int
348 listview_get_curr_pos( listview_ctrl_t ctx )
349 {
350 return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED );
351 }
352
353
354 int
355 listview_get_item_text (listview_ctrl_t ctx, int pos, int col, char *text,
356 int maxbytes)
357 {
358 if (pos == -1) {
359 pos = listview_get_curr_pos (ctx);
360 if (pos == -1)
361 return -1;
362 }
363 ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes);
364 return 0;
365 }
366
367
368 /* Use extended style with checkboxes for each item. */
369 void
370 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);
382 }
383
384
385 int
386 listview_set_column_order( listview_ctrl_t ctx, int *array )
387 {
388 return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array );
389 }
390
391
392 void
393 listview_select_all (listview_ctrl_t ctx)
394 {
395 ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED );
396 }
397
398
399 void
400 listview_deselect_all (listview_ctrl_t ctx)
401 {
402 ListView_SetItemState (ctx->ctrl, -1, ~LVNI_SELECTED, LVNI_SELECTED);
403 }
404
405
406 void
407 listview_select_one (listview_ctrl_t ctx, int pos)
408 {
409 ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED, LVIS_FOCUSED|LVIS_SELECTED);
410 }
411
412
413 void
414 listview_scroll (listview_ctrl_t ctx, int oldpos, int newpos)
415 {
416 const int size=14;
417
418 if (oldpos == -1)
419 oldpos = 0;
420 //log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d", oldpos, newpos, newpos-oldpos);
421 ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size);
422 }
423
424
425 int
426 listview_find (listview_ctrl_t ctx, const char * str)
427 {
428 LVFINDINFO inf;
429 int pos;
430
431 memset (&inf, 0, sizeof (inf));
432 inf.flags = LVFI_STRING|LVFI_PARTIAL;
433 inf.psz = str;
434 pos = ListView_FindItem (ctx->ctrl, -1, &inf);
435 return pos;
436 }
437
438
439 void
440 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 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26