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

Annotation of /trunk/Src/wptListView.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 273 - (hide annotations)
Fri Dec 8 10:22:17 2006 UTC (18 years, 2 months ago) by twoaday
File size: 9205 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 twoaday 273 memset (c, 0, sizeof *c);
50 twoaday 208 c->ctrl = ctrl? ctrl : NULL;
51 twoaday 133 c->hil = NULL;
52 werner 36 *ctx = c;
53 twoaday 174 }
54 werner 36
55    
56     void
57 twoaday 133 listview_release (listview_ctrl_t ctx)
58 werner 36 {
59 twoaday 133 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 twoaday 193 }
69 werner 36
70    
71     int
72 twoaday 193 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 werner 36 listview_add_column (listview_ctrl_t ctx, listview_column_t col)
86     {
87     int rc = 0;
88     LV_COLUMN lvc;
89    
90 twoaday 273 memset (&lvc, 0, sizeof lvc);
91 werner 36 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 twoaday 273 if (ListView_InsertColumn (ctx->ctrl, col->pos, &lvc) == -1)
97 werner 36 rc = 1;
98     ctx->cols++;
99     return rc;
100 twoaday 200 }
101 werner 36
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 twoaday 133 if (ListView_InsertItem (ctx->ctrl, &lvi) != pos)
112     rc = -1;
113 werner 36 ctx->items++;
114     return rc;
115     }
116    
117    
118     int
119 twoaday 241 listview_add_item (listview_ctrl_t ctx, const char *text)
120 werner 36 {
121     int rc = 0;
122     LV_ITEM lvi;
123    
124 twoaday 241 memset (&lvi, 0, sizeof lvi);
125 werner 36 lvi.mask = LVIF_TEXT;
126     lvi.pszText = (char *)text;
127 twoaday 241 rc = ListView_InsertItem (ctx->ctrl, &lvi);
128 werner 36 if( rc == -1 )
129     rc = 1;
130     ctx->items++;
131     return rc;
132 twoaday 200 }
133 werner 36
134    
135     int
136 twoaday 133 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 werner 36 listview_add_item2 (listview_ctrl_t ctx, const char *text, void *magic)
155     {
156     int rc = 0;
157 twoaday 139 LVITEM lvi;
158 werner 36
159 twoaday 139 memset (&lvi, 0, sizeof (lvi));
160 werner 36 lvi.mask = LVIF_TEXT | LVIF_PARAM;
161     lvi.pszText = (char *)text;
162     lvi.lParam = (LPARAM)magic;
163 twoaday 139 rc = ListView_InsertItem (ctx->ctrl, &lvi);
164     if (rc == -1)
165 werner 36 rc = 1;
166     ctx->items++;
167     return rc;
168 twoaday 200 }
169 werner 36
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 twoaday 241
184 werner 36 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 twoaday 204 listview_add_sub_item (listview_ctrl_t ctx, int pos, int col, const char *text)
201     {
202 twoaday 205 ListView_SetItemText (ctx->ctrl, pos, col, (char*)text);
203 twoaday 200 }
204 werner 36
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 twoaday 200 }
215 werner 36
216    
217     int
218 twoaday 129 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 twoaday 150 /* Delete a single item from @ctx at position @pos. */
226 twoaday 129 int
227 twoaday 150 listview_del_item (listview_ctrl_t ctx, int pos)
228 werner 36 {
229     int rc = 0;
230    
231 twoaday 150 if (ListView_DeleteItem (ctx->ctrl, pos) == -1)
232 werner 36 rc = 1;
233     return rc;
234 twoaday 150 }
235 werner 36
236    
237 twoaday 150 /* Delete all selected items in @ctx. */
238 werner 36 int
239 twoaday 150 listview_del_sel_items (listview_ctrl_t ctx)
240 werner 36 {
241     int i, n;
242    
243     n = listview_count_items (ctx, 0);
244 twoaday 150 for (i = n; i > -1; i--) {
245     if (listview_get_item_state (ctx, i))
246     listview_del_item (ctx, i);
247 werner 36 }
248     return 0;
249 twoaday 150 }
250 werner 36
251    
252     int
253 twoaday 150 listview_del_all_items (listview_ctrl_t ctx)
254 werner 36 {
255     int rc = 0;
256    
257 twoaday 150 if (ListView_DeleteAllItems (ctx->ctrl) == FALSE)
258 werner 36 rc = 1;
259     return rc;
260 twoaday 150 }
261 werner 36
262    
263 twoaday 193 /* 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 twoaday 174 /* Return if the given element is selected or if the ext
282     style is used, if the checkbox is activated. */
283 werner 36 int
284 twoaday 174 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 werner 36
294    
295     int
296 twoaday 236 listview_sort_items (listview_ctrl_t ctx, int sortby, listview_cmp sort_cb)
297 twoaday 273 {
298 werner 36 ListView_SortItems (ctx->ctrl, sort_cb, sortby);
299     return 0;
300 twoaday 200 }
301 werner 36
302    
303     int
304 twoaday 273 listview_get_curr_pos (listview_ctrl_t ctx)
305 werner 36 {
306     return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED );
307 twoaday 200 }
308 werner 36
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 twoaday 200 }
322 werner 36
323    
324 twoaday 174 /* Use extended style with checkboxes for each item. */
325 werner 36 void
326 twoaday 174 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 werner 36 {
337 twoaday 174 ListView_SetExtendedListViewStyle (ctx->ctrl, LVS_EX_FULLROWSELECT);
338     }
339 werner 36
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 twoaday 200 }
346 werner 36
347    
348     void
349     listview_select_all (listview_ctrl_t ctx)
350     {
351     ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED );
352 twoaday 200 }
353 werner 36
354    
355     void
356 twoaday 77 listview_deselect_all (listview_ctrl_t ctx)
357     {
358     ListView_SetItemState (ctx->ctrl, -1, ~LVNI_SELECTED, LVNI_SELECTED);
359     }
360    
361    
362     void
363 werner 36 listview_select_one (listview_ctrl_t ctx, int pos)
364     {
365 twoaday 241 ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED,
366     LVIS_FOCUSED|LVIS_SELECTED);
367 werner 36 }
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 twoaday 241 //log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d",
378     //oldpos, newpos, newpos-oldpos);
379 werner 36 ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size);
380     }
381    
382    
383 twoaday 241 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 werner 36 int
405 twoaday 241 listview_find (listview_ctrl_t ctx, const char *str, int substr)
406 werner 36 {
407     LVFINDINFO inf;
408     int pos;
409    
410 twoaday 207 /* XXX: allow to make a substring search. */
411 werner 36 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 twoaday 241 if (pos == -1 && substr)
416     pos = listview_find_substr (ctx, str);
417 werner 36 return pos;
418     }
419    
420 twoaday 129
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 twoaday 133
429    
430     void
431 twoaday 181 listview_set_image_list (listview_ctrl_t ctx, int cx, int cy,
432     HICON *ico, DWORD nicons)
433 twoaday 133 {
434     HIMAGELIST hil;
435     DWORD i;
436    
437 twoaday 273 if (cx <= 0 || cy <= 0)
438     cx = cy = 16; /* default size. */
439 twoaday 181
440     hil = ImageList_Create (cx, cy, ILC_COLOR8|ILC_MASK, nicons, 1);
441 twoaday 144 ImageList_SetBkColor (hil, CLR_NONE);
442 twoaday 133 for (i=0; i < nicons; i++)
443 twoaday 144 ImageList_AddIcon (hil, ico[i]);
444     ListView_SetImageList (ctx->ctrl, hil, LVSIL_SMALL);
445 twoaday 133 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26