/[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 12 by twoaday, Thu Apr 14 12:56:25 2005 UTC revision 144 by twoaday, Thu Jan 12 16:28:06 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_pos (listview_ctrl_t ctx, int pos)  
 {  
     int rc = 0;  
     LV_ITEM lvi;  
   
     memset (&lvi, 0, sizeof lvi);  
     lvi.iItem = pos;  
     rc = ListView_InsertItem (ctx->ctrl, &lvi);  
     if (rc == -1)  
         rc = 1;  
     ctx->items++;  
     return rc;  
 }  
   
   
 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    int
213    listview_del_item( listview_ctrl_t ctx, int pos )
214    {
215        int rc = 0;
216    
217        if( ListView_DeleteItem( ctx->ctrl, pos ) == -1 )
218            rc = 1;
219        return rc;
220    } /* listview_del_item */
221    
222    
223    int
224    listview_del_items( listview_ctrl_t ctx )
225    {      
226        int i, n;
227    
228        /* delete all selected entries */
229        n = listview_count_items (ctx, 0);
230        for( i = n;  i > 0; i-- ) {
231            if ( listview_get_item_state( ctx, i ) )
232                listview_del_item( ctx, i );        
233        }
234        return 0;
235    } /* listview_del_items */
236    
237    
238    int
239    listview_del_all( listview_ctrl_t ctx )
240    {      
241        int rc = 0;
242    
243        if( ListView_DeleteAllItems( ctx->ctrl ) == FALSE )
244            rc = 1;
245        return rc;
246    } /* listview_del_all */
247    
248    
249    int
250    listview_get_item_state( listview_ctrl_t ctx, int pos )
251    {      
252        return ListView_GetItemState( ctx->ctrl, pos, LVIS_SELECTED );
253    } /* listview_get_item_state */
254    
255    
256    int
257    listview_sort_items( listview_ctrl_t ctx, int sortby, listview_cmp sort_cb )
258    {      
259        HWND hheader;
260        HDITEM hdi;
261        int idx;
262    
263        ListView_SortItems (ctx->ctrl, sort_cb, sortby);
264        hheader = ListView_GetHeader (ctx->ctrl);
265        if (!hheader)
266            return 0;
267    
268        /* Remove image from all header fields */
269        memset (&hdi, 0, sizeof(hdi));
270    
271        for (int i=0; i < 7; i++) {
272            hdi.mask = HDI_FORMAT;
273            Header_GetItem (hheader, i, &hdi);
274            hdi.fmt &= ~HDF_IMAGE;
275            Header_SetItem (hheader, i, &hdi);
276        }
277    
278        switch (sortby & ~KEYLIST_SORT_DESC) {
279        case KEY_SORT_USERID:    idx = 0; break;
280        case KEY_SORT_KEYID:     idx = 1; break;
281        case KEY_SORT_IS_SECRET: idx = 2; break;
282        case KEY_SORT_LEN:       idx = 3; break;
283        case KEY_SORT_VALIDITY:  idx = 5; break;
284        case KEY_SORT_OTRUST:    idx = 6; break;
285        case KEY_SORT_CREATED:   idx = 7; break;
286        case KEY_SORT_ALGO:      idx = 8; break;
287        default:                 idx = 0; break;
288        }
289    
290        /* Set image to currently sorted field */
291        memset (&hdi, 0, sizeof(hdi));
292        hdi.mask = HDI_IMAGE | HDI_FORMAT;
293        Header_GetItem (hheader, idx, &hdi);
294        hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT;
295        hdi.iImage = imagelist_getindex((sortby & KEYLIST_SORT_DESC) ?
296                                        IMI_SORT_DOWNARROW : IMI_SORT_UPARROW);
297        Header_SetItem (hheader, idx, &hdi);
298        return 0;
299    } /* listview_sort_items */
300    
301    
302    int
303    listview_get_curr_pos( listview_ctrl_t ctx )
304    {      
305        return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED );
306    } /* listview_get_curr_pos */
307    
308    
309    int
310    listview_get_item_text (listview_ctrl_t ctx, int pos, int col, char *text,
311                            int maxbytes)
312    {
313        if (pos == -1) {
314            pos = listview_get_curr_pos (ctx);
315            if (pos == -1)
316                return -1;
317        }
318        ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes);
319        return 0;
320    } /* listview_get_item_text */
321    
322    
323    void
324    listview_set_ext_style( listview_ctrl_t ctx )
325    {      
326        ListView_SetExtendedListViewStyle( ctx->ctrl, LVS_EX_FULLROWSELECT );
327    } /* listview_set_ext_style */
328    
329    
330    int
331    listview_set_column_order( listview_ctrl_t ctx, int *array )
332    {      
333        return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array );
334    } /* listview_set_column_order */
335    
336    
337    void
338    listview_select_all (listview_ctrl_t ctx)
339    {      
340        ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED );
341    } /* listview_select_all */
342    
343    
344    void
345    listview_deselect_all (listview_ctrl_t ctx)
346    {
347        ListView_SetItemState (ctx->ctrl, -1, ~LVNI_SELECTED, LVNI_SELECTED);
348    }
349    
350    
351    void
352    listview_select_one (listview_ctrl_t ctx, int pos)
353    {
354        ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED, LVIS_FOCUSED|LVIS_SELECTED);
355    }
356    
357    
358    void
359    listview_scroll (listview_ctrl_t ctx, int oldpos, int newpos)
360    {    
361        const int size=14;
362        
363        if (oldpos == -1)
364            oldpos = 0;
365        //log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d", oldpos, newpos, newpos-oldpos);
366        ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size);
367    }
368    
369    
370    int
371    listview_find (listview_ctrl_t ctx, const char * str)
372    {
373        LVFINDINFO inf;
374        int pos;
375    
376        memset (&inf, 0, sizeof (inf));
377        inf.flags = LVFI_STRING|LVFI_PARTIAL;
378        inf.psz = str;
379        pos = ListView_FindItem (ctx->ctrl, -1, &inf);
380        return pos;
381    }
382    
383    
384    void
385    listview_setview (listview_ctrl_t ctx, DWORD view)
386    {
387        DWORD style = GetWindowLong (ctx->ctrl, GWL_STYLE);
388        if ((style & LVS_TYPEMASK) != view)
389            SetWindowLong (ctx->ctrl, GWL_STYLE, (style & ~LVS_TYPEMASK) | view);
390    }
391    
392    
393    void
394    listview_set_image_list (listview_ctrl_t ctx, HICON *ico, DWORD nicons)
395    {
396        HIMAGELIST hil;
397        DWORD i;
398    
399        hil = ImageList_Create (16, 16, ILC_COLOR8|ILC_MASK, nicons, 1);
400        ImageList_SetBkColor (hil, CLR_NONE);
401        for (i=0; i < nicons; i++)
402            ImageList_AddIcon (hil, ico[i]);    
403        ListView_SetImageList (ctx->ctrl, hil, LVSIL_SMALL);    
404    }
405        

Legend:
Removed from v.12  
changed lines
  Added in v.144

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26