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

Diff of /trunk/Src/wptListView.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2 by twoaday, Mon Jan 31 11:02:21 2005 UTC revision 150 by twoaday, Wed Jan 18 11:52:45 2006 UTC
# Line 1  Line 1 
 /* wptListView.cpp - Dynamic list view control  
  *      Copyright (C) 2000-2004 Timo Schulz  
  *      Copyright (C) 2004 Andreas Jobs  
  *  
  * This file is part of WinPT.  
  *  
  * WinPT is free software; you can redistribute it and/or  
  * modify it under the terms of the GNU General Public License  
  * as published by the Free Software Foundation; either version 2  
  * of the License, or (at your option) any later version.  
  *    
  * WinPT is distributed in the hope that it will be useful,  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  
  * General Public License for more details.  
  *  
  * You should have received a copy of the GNU General Public License  
  * along with WinPT; if not, write to the Free Software Foundation,  
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  
  */  
   
 #include <stdio.h>  
 #include <windows.h>  
 #include <commctrl.h>  
   
 #include "wptCommonCtl.h"  
 #include "wptW32API.h"  
 #include "wptVersion.h"  
 #include "wptErrors.h"  
 #include "wptTypes.h"  
 #include "wptGPG.h"  
 #include "wptKeylist.h"  
 #include "../resource.h"  
   
   
 int  
 listview_new( listview_ctrl_t *ctx )  
 {  
     struct listview_ctrl_s *c;  
   
     c = new struct listview_ctrl_s;  
     if( !c )  
         BUG( NULL );  
     c->cols = 0;  
     c->items = 0;  
     c->ctrl = NULL;      
     *ctx = c;  
     return 0;  
 } /* listview_new */  
   
   
 void  
 listview_release( listview_ctrl_t ctx )  
 {  
     if( ctx ) {  
         ctx->cols = 0;  
         ctx->ctrl = NULL;  
         ctx->items = 0;  
         delete ctx;  
         ctx = NULL;  
     }  
 } /* listview_release */  
   
   
 int  
 listview_add_column( listview_ctrl_t ctx, listview_column_t col )  
 {        
     int rc = 0;  
     LV_COLUMN lvc;  
   
     memset( &lvc, 0, sizeof lvc );  
     lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;  
     lvc.pszText = col->fieldname;  
     lvc.cx = col->width;  
     lvc.fmt = LVCFMT_LEFT;  
     lvc.iSubItem = col->pos;  
     if( ListView_InsertColumn( ctx->ctrl, col->pos, &lvc ) == -1 )  
         rc = 1;  
     ctx->cols++;  
     return rc;  
 } /* listview_add_column */  
   
   
 int  
 listview_add_item( listview_ctrl_t ctx, const char *text )  
 {  
     int rc = 0;  
     LV_ITEM lvi;  
   
     memset( &lvi, 0, sizeof lvi );  
     lvi.mask = LVIF_TEXT;  
     lvi.pszText = (char *)text;  
     rc = ListView_InsertItem( ctx->ctrl, &lvi );  
     if( rc == -1 )  
         rc = 1;  
     ctx->items++;  
     return rc;  
 } /* listview_add_item */  
   
   
 int  
 listview_add_item2( listview_ctrl_t ctx, const char * text, void * magic )  
 {  
     int rc = 0;  
     LV_ITEM lvi;  
   
     memset( &lvi, 0, sizeof lvi );  
     lvi.mask = LVIF_TEXT | LVIF_PARAM;  
     lvi.pszText = (char *)text;  
     lvi.lParam = (LPARAM)magic;  
     rc = ListView_InsertItem( ctx->ctrl, &lvi );  
     if( rc == -1 )  
         rc = 1;  
     ctx->items++;  
     return rc;  
 } /* listview_add_item2 */  
   
   
 void  
 listview_add_sub_item( listview_ctrl_t ctx, int pos, int col, const char *text )  
 {        
     ListView_SetItemText( ctx->ctrl, pos, col, (char *)text );  
 } /* listview_add_sub_item */  
   
   
 int  
 listview_count_items( listview_ctrl_t ctx, int curr_sel )  
 {        
     int n;  
   
     n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) :      
                   ListView_GetItemCount( ctx->ctrl );  
     return n;  
 } /* listview_count_items */  
   
   
 int  
 listview_del_item( listview_ctrl_t ctx, int pos )  
 {  
     int rc = 0;  
   
     if( ListView_DeleteItem( ctx->ctrl, pos ) == -1 )  
         rc = 1;  
     return rc;  
 } /* listview_del_item */  
   
   
 int  
 listview_del_items( listview_ctrl_t ctx )  
 {        
     int n;  
   
     /* delete all selected entries */  
     for( n = 0;  n < listview_count_items( ctx, 0 ); n++ ) {  
         if ( listview_get_item_state( ctx, n ) )  
             listview_del_item( ctx, n );          
     }  
     return 0;  
 } /* listview_del_items */  
   
   
 int  
 listview_del_all( listview_ctrl_t ctx )  
 {        
     int rc = 0;  
   
     if( ListView_DeleteAllItems( ctx->ctrl ) == FALSE )  
         rc = 1;  
     return rc;  
 } /* listview_del_all */  
   
   
 int  
 listview_get_item_state( listview_ctrl_t ctx, int pos )  
 {        
     return ListView_GetItemState( ctx->ctrl, pos, LVIS_SELECTED );  
 } /* listview_get_item_state */  
   
   
 int  
 listview_sort_items( listview_ctrl_t ctx, int sortby, listview_cmp sort_cb )  
 {        
     HWND hheader;  
     HDITEM hdi;  
     int idx;  
   
     ListView_SortItems (ctx->ctrl, sort_cb, sortby);  
     hheader = ListView_GetHeader (ctx->ctrl);  
     if (!hheader)  
         return 0;  
   
     /* Remove image from all header fields */  
     memset (&hdi, 0, sizeof(hdi));  
   
     for (int i=0; i < 7; i++) {  
         hdi.mask = HDI_FORMAT;  
         Header_GetItem (hheader, i, &hdi);  
         hdi.fmt &= ~HDF_IMAGE;  
         Header_SetItem (hheader, i, &hdi);  
     }  
   
     switch (sortby & ~KEYLIST_SORT_DESC) {  
     case GPGME_ATTR_USERID:    idx = 0; break;  
     case GPGME_ATTR_KEYID:     idx = 1; break;  
     case GPGME_ATTR_IS_SECRET: idx = 2; break;  
     case GPGME_ATTR_LEN:       idx = 3; break;  
     case GPGME_ATTR_VALIDITY:  idx = 5; break;  
     case GPGME_ATTR_OTRUST:    idx = 6; break;  
     case GPGME_ATTR_CREATED:   idx = 7; break;  
     case GPGME_ATTR_ALGO:      idx = 8; break;  
     default:                   idx = 0; break;  
     }  
   
     /* Set image to currently sorted field */  
     memset (&hdi, 0, sizeof(hdi));  
     hdi.mask = HDI_IMAGE | HDI_FORMAT;  
     Header_GetItem (hheader, idx, &hdi);  
     hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT;  
     hdi.iImage = imagelist_getindex((sortby & KEYLIST_SORT_DESC) ?  
                                     IMI_SORT_DOWNARROW : IMI_SORT_UPARROW);  
     Header_SetItem (hheader, idx, &hdi);  
     return 0;  
 } /* listview_sort_items */  
   
   
 int  
 listview_get_curr_pos( listview_ctrl_t ctx )  
 {        
     return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED );  
 } /* listview_get_curr_pos */  
   
   
 int  
 listview_get_item_text (listview_ctrl_t ctx, int pos, int col, char *text,  
                         int maxbytes)  
 {  
     if (pos == -1) {  
         pos = listview_get_curr_pos (ctx);  
         if (pos == -1)  
             return -1;  
     }  
     ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes);  
     return 0;  
 } /* listview_get_item_text */  
   
   
 void  
 listview_set_ext_style( listview_ctrl_t ctx )  
 {        
     ListView_SetExtendedListViewStyle( ctx->ctrl, LVS_EX_FULLROWSELECT );  
 } /* listview_set_ext_style */  
   
   
 int  
 listview_set_column_order( listview_ctrl_t ctx, int *array )  
 {        
     return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array );  
 } /* listview_set_column_order */  
   
   
 void  
 listview_select_all (listview_ctrl_t ctx)  
 {        
     ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED );  
 } /* listview_select_all */  
   
   
 void  
 listview_select_one (listview_ctrl_t ctx, int pos)  
 {  
     ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED, LVIS_FOCUSED|LVIS_SELECTED);  
 }  
   
   
 int  
 listview_find (listview_ctrl_t ctx, const char * str)  
 {  
     LVFINDINFO inf;  
     int pos;  
   
     memset (&inf, 0, sizeof inf);  
     inf.flags = LVFI_STRING|LVFI_PARTIAL;  
     inf.psz = str;  
     pos = ListView_FindItem (ctx->ctrl, -1, &inf);  
     return pos;  
 }  
   
 /** Some functions to make the handling with TreeView Controls easier **/  
 int  
 treeview_add_item( HWND tree, HTREEITEM parent, const char *text )  
 {        
     TVINSERTSTRUCT tvis;  
           
     memset( &tvis, 0, sizeof tvis );  
     tvis.hParent = parent;  
     tvis.hInsertAfter = TVI_LAST;  
     tvis.item.mask = TVIF_TEXT;  
     tvis.item.pszText = (char *)text;  
     TreeView_InsertItem( tree, &tvis );  
   
     return 0;  
 } /* treeview_add_item */  
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        *ctx = c;
53        return 0;
54    } /* listview_new */
55    
56    
57    void
58    listview_release (listview_ctrl_t ctx)
59    {
60        if (!ctx)
61            return;
62    
63        ctx->cols = 0;
64        ctx->ctrl = NULL;
65        ctx->items = 0;
66        if (ctx->hil)
67            ImageList_Destroy (ctx->hil);
68        delete ctx;
69    } /* listview_release */
70    
71    
72    int
73    listview_add_column (listview_ctrl_t ctx, listview_column_t col)
74    {      
75        int rc = 0;
76        LV_COLUMN lvc;
77    
78        memset( &lvc, 0, sizeof lvc );
79        lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
80        lvc.pszText = col->fieldname;
81        lvc.cx = col->width;
82        lvc.fmt = LVCFMT_LEFT;
83        lvc.iSubItem = col->pos;
84        if( ListView_InsertColumn (ctx->ctrl, col->pos, &lvc) == -1)
85            rc = 1;
86        ctx->cols++;
87        return rc;
88    } /* listview_add_column */
89    
90    
91    int
92    listview_add_item_pos (listview_ctrl_t ctx, int pos)
93    {
94        int rc = 0;
95        LV_ITEM lvi;
96    
97        memset (&lvi, 0, sizeof lvi);
98        lvi.iItem = pos;
99        if (ListView_InsertItem (ctx->ctrl, &lvi) != pos)
100            rc = -1;
101        ctx->items++;
102        return rc;
103    }
104    
105    
106    int
107    listview_add_item( listview_ctrl_t ctx, const char *text )
108    {
109        int rc = 0;
110        LV_ITEM lvi;
111    
112        memset( &lvi, 0, sizeof lvi );
113        lvi.mask = LVIF_TEXT;
114        lvi.pszText = (char *)text;
115        rc = ListView_InsertItem( ctx->ctrl, &lvi );
116        if( rc == -1 )
117            rc = 1;
118        ctx->items++;
119        return rc;
120    } /* listview_add_item */
121    
122    
123    int
124    listview_add_item_image (listview_ctrl_t ctx, const char *text, int image)
125    {
126        int rc = 0;
127        LV_ITEM lvi;
128    
129        memset( &lvi, 0, sizeof lvi );
130        lvi.mask = LVIF_TEXT | LVIF_IMAGE;
131        lvi.pszText = (char *)text;
132        lvi.iImage = image;
133        rc = ListView_InsertItem (ctx->ctrl, &lvi);
134        if (rc == -1)
135            rc = 1;
136        ctx->items++;
137        return rc;
138    }
139    
140    
141    int
142    listview_add_item2 (listview_ctrl_t ctx, const char *text, void *magic)
143    {
144        int rc = 0;
145        LVITEM lvi;
146    
147        memset (&lvi, 0, sizeof (lvi));
148        lvi.mask = LVIF_TEXT | LVIF_PARAM;
149        lvi.pszText = (char *)text;
150        lvi.lParam = (LPARAM)magic;
151        rc = ListView_InsertItem (ctx->ctrl, &lvi);
152        if (rc == -1)
153            rc = 1;
154        ctx->items++;
155        return rc;
156    } /* listview_add_item2 */
157    
158    
159    void*
160    listview_get_item2 (listview_ctrl_t ctx, int pos)
161    {
162        LVITEM lvi;
163    
164        memset (&lvi, 0, sizeof lvi);
165        lvi.mask = LVIF_PARAM;
166        lvi.iItem = pos;
167        ListView_GetItem (ctx->ctrl, &lvi);
168        return (void*)lvi.lParam;
169    }
170    
171    int
172    listview_set_item2 (listview_ctrl_t ctx, int pos, void *magic)
173    {
174        LVITEM lvi;
175    
176        memset (&lvi, 0, sizeof (lvi));
177        lvi.mask = LVIF_PARAM;
178        lvi.iItem = pos;
179        lvi.lParam = (LPARAM)magic;
180        if (ListView_SetItem (ctx->ctrl, &lvi) == -1)
181            return 1;
182        return 0;
183    }
184    
185    
186    void
187    listview_add_sub_item( listview_ctrl_t ctx, int pos, int col, const char *text )
188    {      
189        ListView_SetItemText( ctx->ctrl, pos, col, (char *)text );
190    } /* listview_add_sub_item */
191    
192    
193    int
194    listview_count_items( listview_ctrl_t ctx, int curr_sel )
195    {      
196        int n;
197    
198        n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) :    
199                      ListView_GetItemCount( ctx->ctrl );
200        return n;
201    } /* listview_count_items */
202    
203    
204    int
205    listview_del_column (listview_ctrl_t ctx, int pos)
206    {
207        ctx->cols--;
208        return ListView_DeleteColumn (ctx->ctrl, pos)? 0 : 1;
209    }
210    
211    
212    /* Delete a single item from @ctx at position @pos. */
213    int
214    listview_del_item (listview_ctrl_t ctx, int pos)
215    {
216        int rc = 0;
217    
218        if (ListView_DeleteItem (ctx->ctrl, pos) == -1)
219            rc = 1;
220        return rc;
221    }
222    
223    
224    /* Delete all selected items in @ctx. */
225    int
226    listview_del_sel_items (listview_ctrl_t ctx)
227    {      
228        int i, n;
229    
230        n = listview_count_items (ctx, 0);
231        for (i = n;  i > -1; i--) {
232            if (listview_get_item_state (ctx, i))
233                listview_del_item (ctx, i);
234        }
235        return 0;
236    }
237    
238    
239    int
240    listview_del_all_items (listview_ctrl_t ctx)
241    {      
242        int rc = 0;
243    
244        if (ListView_DeleteAllItems (ctx->ctrl) == FALSE)
245            rc = 1;
246        return rc;
247    }
248    
249    
250    int
251    listview_get_item_state( listview_ctrl_t ctx, int pos )
252    {      
253        return ListView_GetItemState( ctx->ctrl, pos, LVIS_SELECTED );
254    } /* listview_get_item_state */
255    
256    
257    int
258    listview_sort_items( listview_ctrl_t ctx, int sortby, listview_cmp sort_cb )
259    {      
260        HWND hheader;
261        HDITEM hdi;
262        int idx;
263    
264        ListView_SortItems (ctx->ctrl, sort_cb, sortby);
265        hheader = ListView_GetHeader (ctx->ctrl);
266        if (!hheader)
267            return 0;
268    
269        /* Remove image from all header fields */
270        memset (&hdi, 0, sizeof(hdi));
271    
272        for (int i=0; i < 7; i++) {
273            hdi.mask = HDI_FORMAT;
274            Header_GetItem (hheader, i, &hdi);
275            hdi.fmt &= ~HDF_IMAGE;
276            Header_SetItem (hheader, i, &hdi);
277        }
278    
279        switch (sortby & ~KEYLIST_SORT_DESC) {
280        case KEY_SORT_USERID:    idx = 0; break;
281        case KEY_SORT_KEYID:     idx = 1; break;
282        case KEY_SORT_IS_SECRET: idx = 2; break;
283        case KEY_SORT_LEN:       idx = 3; break;
284        case KEY_SORT_VALIDITY:  idx = 5; break;
285        case KEY_SORT_OTRUST:    idx = 6; break;
286        case KEY_SORT_CREATED:   idx = 7; break;
287        case KEY_SORT_ALGO:      idx = 8; break;
288        default:                 idx = 0; break;
289        }
290    
291        /* Set image to currently sorted field */
292        memset (&hdi, 0, sizeof(hdi));
293        hdi.mask = HDI_IMAGE | HDI_FORMAT;
294        Header_GetItem (hheader, idx, &hdi);
295        hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT;
296        hdi.iImage = imagelist_getindex((sortby & KEYLIST_SORT_DESC) ?
297                                        IMI_SORT_DOWNARROW : IMI_SORT_UPARROW);
298        Header_SetItem (hheader, idx, &hdi);
299        return 0;
300    } /* listview_sort_items */
301    
302    
303    int
304    listview_get_curr_pos( listview_ctrl_t ctx )
305    {      
306        return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED );
307    } /* listview_get_curr_pos */
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    } /* listview_get_item_text */
322    
323    
324    void
325    listview_set_ext_style( listview_ctrl_t ctx )
326    {      
327        ListView_SetExtendedListViewStyle( ctx->ctrl, LVS_EX_FULLROWSELECT );
328    } /* listview_set_ext_style */
329    
330    
331    int
332    listview_set_column_order( listview_ctrl_t ctx, int *array )
333    {      
334        return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array );
335    } /* listview_set_column_order */
336    
337    
338    void
339    listview_select_all (listview_ctrl_t ctx)
340    {      
341        ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED );
342    } /* listview_select_all */
343    
344    
345    void
346    listview_deselect_all (listview_ctrl_t ctx)
347    {
348        ListView_SetItemState (ctx->ctrl, -1, ~LVNI_SELECTED, LVNI_SELECTED);
349    }
350    
351    
352    void
353    listview_select_one (listview_ctrl_t ctx, int pos)
354    {
355        ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED, LVIS_FOCUSED|LVIS_SELECTED);
356    }
357    
358    
359    void
360    listview_scroll (listview_ctrl_t ctx, int oldpos, int newpos)
361    {    
362        const int size=14;
363        
364        if (oldpos == -1)
365            oldpos = 0;
366        //log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d", oldpos, newpos, newpos-oldpos);
367        ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size);
368    }
369    
370    
371    int
372    listview_find (listview_ctrl_t ctx, const char * str)
373    {
374        LVFINDINFO inf;
375        int pos;
376    
377        memset (&inf, 0, sizeof (inf));
378        inf.flags = LVFI_STRING|LVFI_PARTIAL;
379        inf.psz = str;
380        pos = ListView_FindItem (ctx->ctrl, -1, &inf);
381        return pos;
382    }
383    
384    
385    void
386    listview_setview (listview_ctrl_t ctx, DWORD view)
387    {
388        DWORD style = GetWindowLong (ctx->ctrl, GWL_STYLE);
389        if ((style & LVS_TYPEMASK) != view)
390            SetWindowLong (ctx->ctrl, GWL_STYLE, (style & ~LVS_TYPEMASK) | view);
391    }
392    
393    
394    void
395    listview_set_image_list (listview_ctrl_t ctx, HICON *ico, DWORD nicons)
396    {
397        HIMAGELIST hil;
398        DWORD i;
399    
400        hil = ImageList_Create (16, 16, ILC_COLOR8|ILC_MASK, nicons, 1);
401        ImageList_SetBkColor (hil, CLR_NONE);
402        for (i=0; i < nicons; i++)
403            ImageList_AddIcon (hil, ico[i]);    
404        ListView_SetImageList (ctx->ctrl, hil, LVSIL_SMALL);    
405    }
406        

Legend:
Removed from v.2  
changed lines
  Added in v.150

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26