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

Contents of /trunk/Src/wptListView.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 273 - (show annotations)
Fri Dec 8 10:22:17 2006 UTC (18 years, 2 months ago) by twoaday
File size: 9205 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26