/[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 28 by twoaday, Thu Oct 20 12:35:59 2005 UTC revision 48 by werner, Mon Oct 31 21:14:11 2005 UTC
# Line 1  Line 1 
 /* wptListView.cpp - Dynamic list view control  
  *      Copyright (C) 2000-2005 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_get_item2 (listview_ctrl_t ctx, int pos)  
 {  
     LVITEM lvi;  
   
     memset (&lvi, 0, sizeof lvi);  
     lvi.mask = LVIF_PARAM;  
     lvi.iItem = pos;  
     ListView_GetItem (ctx->ctrl, &lvi);  
     return (void*)lvi.lParam;  
 }  
   
 int  
 listview_set_item2 (listview_ctrl_t ctx, int pos, void *magic)  
 {  
     LVITEM lvi;  
   
     memset (&lvi, 0, sizeof (lvi));  
     lvi.mask = LVIF_PARAM;  
     lvi.iItem = pos;  
     lvi.lParam = (LPARAM)magic;  
     if (ListView_SetItem (ctx->ctrl, &lvi) == -1)  
         return 1;  
     return 0;  
 }  
   
   
 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 i, n;  
   
     /* delete all selected entries */  
     n = listview_count_items (ctx, 0);  
     for( i = n;  i > 0; i-- ) {  
         if ( listview_get_item_state( ctx, i ) )  
             listview_del_item( ctx, i );          
     }  
     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 KEY_SORT_USERID:    idx = 0; break;  
     case KEY_SORT_KEYID:     idx = 1; break;  
     case KEY_SORT_IS_SECRET: idx = 2; break;  
     case KEY_SORT_LEN:       idx = 3; break;  
     case KEY_SORT_VALIDITY:  idx = 5; break;  
     case KEY_SORT_OTRUST:    idx = 6; break;  
     case KEY_SORT_CREATED:   idx = 7; break;  
     case KEY_SORT_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);  
 }  
   
   
 void  
 listview_scroll (listview_ctrl_t ctx, int oldpos, int newpos)  
 {      
     const int size=14;  
       
     if (oldpos == -1)  
         oldpos = 0;  
     //log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d", oldpos, newpos, newpos-oldpos);  
     ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size);  
 }  
   
   
 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-2005 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    #ifdef HAVE_CONFIG_H
22    #include <config.h>
23    #endif
24    
25    #ifdef __MINGW32__
26    #include <w32api.h>
27    #define _WIN32_IE IE401
28    #endif
29    
30    #include <stdio.h>
31    #include <stdio.h>
32    #include <windows.h>
33    #include <commctrl.h>
34    
35    #include "wptCommonCtl.h"
36    #include "wptW32API.h"
37    #include "wptVersion.h"
38    #include "wptErrors.h"
39    #include "wptTypes.h"
40    #include "wptGPG.h"
41    #include "wptKeylist.h"
42    #include "resource.h"
43    
44    
45    int
46    listview_new( listview_ctrl_t *ctx )
47    {
48        struct listview_ctrl_s *c;
49    
50        c = new struct listview_ctrl_s;
51        if( !c )
52            BUG( NULL );
53        c->cols = 0;
54        c->items = 0;
55        c->ctrl = NULL;    
56        *ctx = c;
57        return 0;
58    } /* listview_new */
59    
60    
61    void
62    listview_release( listview_ctrl_t ctx )
63    {
64        if( ctx ) {
65            ctx->cols = 0;
66            ctx->ctrl = NULL;
67            ctx->items = 0;
68            delete ctx;
69            ctx = NULL;
70        }
71    } /* listview_release */
72    
73    
74    int
75    listview_add_column (listview_ctrl_t ctx, listview_column_t col)
76    {      
77        int rc = 0;
78        LV_COLUMN lvc;
79    
80        memset( &lvc, 0, sizeof lvc );
81        lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
82        lvc.pszText = col->fieldname;
83        lvc.cx = col->width;
84        lvc.fmt = LVCFMT_LEFT;
85        lvc.iSubItem = col->pos;
86        if( ListView_InsertColumn (ctx->ctrl, col->pos, &lvc) == -1)
87            rc = 1;
88        ctx->cols++;
89        return rc;
90    } /* listview_add_column */
91    
92    
93    int
94    listview_add_item_pos (listview_ctrl_t ctx, int pos)
95    {
96        int rc = 0;
97        LV_ITEM lvi;
98    
99        memset (&lvi, 0, sizeof lvi);
100        lvi.iItem = pos;
101        rc = ListView_InsertItem (ctx->ctrl, &lvi);
102        if (rc == -1)
103            rc = 1;
104        ctx->items++;
105        return rc;
106    }
107    
108    
109    int
110    listview_add_item( listview_ctrl_t ctx, const char *text )
111    {
112        int rc = 0;
113        LV_ITEM lvi;
114    
115        memset( &lvi, 0, sizeof lvi );
116        lvi.mask = LVIF_TEXT;
117        lvi.pszText = (char *)text;
118        rc = ListView_InsertItem( ctx->ctrl, &lvi );
119        if( rc == -1 )
120            rc = 1;
121        ctx->items++;
122        return rc;
123    } /* listview_add_item */
124    
125    
126    int
127    listview_add_item2 (listview_ctrl_t ctx, const char *text, void *magic)
128    {
129        int rc = 0;
130        LV_ITEM lvi;
131    
132        memset( &lvi, 0, sizeof lvi );
133        lvi.mask = LVIF_TEXT | LVIF_PARAM;
134        lvi.pszText = (char *)text;
135        lvi.lParam = (LPARAM)magic;
136        rc = ListView_InsertItem( ctx->ctrl, &lvi );
137        if( rc == -1 )
138            rc = 1;
139        ctx->items++;
140        return rc;
141    } /* listview_add_item2 */
142    
143    
144    void*
145    listview_get_item2 (listview_ctrl_t ctx, int pos)
146    {
147        LVITEM lvi;
148    
149        memset (&lvi, 0, sizeof lvi);
150        lvi.mask = LVIF_PARAM;
151        lvi.iItem = pos;
152        ListView_GetItem (ctx->ctrl, &lvi);
153        return (void*)lvi.lParam;
154    }
155    
156    int
157    listview_set_item2 (listview_ctrl_t ctx, int pos, void *magic)
158    {
159        LVITEM lvi;
160    
161        memset (&lvi, 0, sizeof (lvi));
162        lvi.mask = LVIF_PARAM;
163        lvi.iItem = pos;
164        lvi.lParam = (LPARAM)magic;
165        if (ListView_SetItem (ctx->ctrl, &lvi) == -1)
166            return 1;
167        return 0;
168    }
169    
170    
171    void
172    listview_add_sub_item( listview_ctrl_t ctx, int pos, int col, const char *text )
173    {      
174        ListView_SetItemText( ctx->ctrl, pos, col, (char *)text );
175    } /* listview_add_sub_item */
176    
177    
178    int
179    listview_count_items( listview_ctrl_t ctx, int curr_sel )
180    {      
181        int n;
182    
183        n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) :    
184                      ListView_GetItemCount( ctx->ctrl );
185        return n;
186    } /* listview_count_items */
187    
188    
189    int
190    listview_del_item( listview_ctrl_t ctx, int pos )
191    {
192        int rc = 0;
193    
194        if( ListView_DeleteItem( ctx->ctrl, pos ) == -1 )
195            rc = 1;
196        return rc;
197    } /* listview_del_item */
198    
199    
200    int
201    listview_del_items( listview_ctrl_t ctx )
202    {      
203        int i, n;
204    
205        /* delete all selected entries */
206        n = listview_count_items (ctx, 0);
207        for( i = n;  i > 0; i-- ) {
208            if ( listview_get_item_state( ctx, i ) )
209                listview_del_item( ctx, i );        
210        }
211        return 0;
212    } /* listview_del_items */
213    
214    
215    int
216    listview_del_all( listview_ctrl_t ctx )
217    {      
218        int rc = 0;
219    
220        if( ListView_DeleteAllItems( ctx->ctrl ) == FALSE )
221            rc = 1;
222        return rc;
223    } /* listview_del_all */
224    
225    
226    int
227    listview_get_item_state( listview_ctrl_t ctx, int pos )
228    {      
229        return ListView_GetItemState( ctx->ctrl, pos, LVIS_SELECTED );
230    } /* listview_get_item_state */
231    
232    
233    int
234    listview_sort_items( listview_ctrl_t ctx, int sortby, listview_cmp sort_cb )
235    {      
236        HWND hheader;
237        HDITEM hdi;
238        int idx;
239    
240        ListView_SortItems (ctx->ctrl, sort_cb, sortby);
241        hheader = ListView_GetHeader (ctx->ctrl);
242        if (!hheader)
243            return 0;
244    
245        /* Remove image from all header fields */
246        memset (&hdi, 0, sizeof(hdi));
247    
248        for (int i=0; i < 7; i++) {
249            hdi.mask = HDI_FORMAT;
250            Header_GetItem (hheader, i, &hdi);
251            hdi.fmt &= ~HDF_IMAGE;
252            Header_SetItem (hheader, i, &hdi);
253        }
254    
255        switch (sortby & ~KEYLIST_SORT_DESC) {
256        case KEY_SORT_USERID:    idx = 0; break;
257        case KEY_SORT_KEYID:     idx = 1; break;
258        case KEY_SORT_IS_SECRET: idx = 2; break;
259        case KEY_SORT_LEN:       idx = 3; break;
260        case KEY_SORT_VALIDITY:  idx = 5; break;
261        case KEY_SORT_OTRUST:    idx = 6; break;
262        case KEY_SORT_CREATED:   idx = 7; break;
263        case KEY_SORT_ALGO:      idx = 8; break;
264        default:                 idx = 0; break;
265        }
266    
267        /* Set image to currently sorted field */
268        memset (&hdi, 0, sizeof(hdi));
269        hdi.mask = HDI_IMAGE | HDI_FORMAT;
270        Header_GetItem (hheader, idx, &hdi);
271        hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT;
272        hdi.iImage = imagelist_getindex((sortby & KEYLIST_SORT_DESC) ?
273                                        IMI_SORT_DOWNARROW : IMI_SORT_UPARROW);
274        Header_SetItem (hheader, idx, &hdi);
275        return 0;
276    } /* listview_sort_items */
277    
278    
279    int
280    listview_get_curr_pos( listview_ctrl_t ctx )
281    {      
282        return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED );
283    } /* listview_get_curr_pos */
284    
285    
286    int
287    listview_get_item_text (listview_ctrl_t ctx, int pos, int col, char *text,
288                            int maxbytes)
289    {
290        if (pos == -1) {
291            pos = listview_get_curr_pos (ctx);
292            if (pos == -1)
293                return -1;
294        }
295        ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes);
296        return 0;
297    } /* listview_get_item_text */
298    
299    
300    void
301    listview_set_ext_style( listview_ctrl_t ctx )
302    {      
303        ListView_SetExtendedListViewStyle( ctx->ctrl, LVS_EX_FULLROWSELECT );
304    } /* listview_set_ext_style */
305    
306    
307    int
308    listview_set_column_order( listview_ctrl_t ctx, int *array )
309    {      
310        return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array );
311    } /* listview_set_column_order */
312    
313    
314    void
315    listview_select_all (listview_ctrl_t ctx)
316    {      
317        ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED );
318    } /* listview_select_all */
319    
320    
321    void
322    listview_select_one (listview_ctrl_t ctx, int pos)
323    {
324        ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED, LVIS_FOCUSED|LVIS_SELECTED);
325    }
326    
327    
328    void
329    listview_scroll (listview_ctrl_t ctx, int oldpos, int newpos)
330    {    
331        const int size=14;
332        
333        if (oldpos == -1)
334            oldpos = 0;
335        //log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d", oldpos, newpos, newpos-oldpos);
336        ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size);
337    }
338    
339    
340    int
341    listview_find (listview_ctrl_t ctx, const char * str)
342    {
343        LVFINDINFO inf;
344        int pos;
345    
346        memset (&inf, 0, sizeof (inf));
347        inf.flags = LVFI_STRING|LVFI_PARTIAL;
348        inf.psz = str;
349        pos = ListView_FindItem (ctx->ctrl, -1, &inf);
350        return pos;
351    }
352    
353    /** Some functions to make the handling with TreeView Controls easier **/
354    int
355    treeview_add_item( HWND tree, HTREEITEM parent, const char *text )
356    {      
357        TVINSERTSTRUCT tvis;
358            
359        memset( &tvis, 0, sizeof tvis );
360        tvis.hParent = parent;
361        tvis.hInsertAfter = TVI_LAST;
362        tvis.item.mask = TVIF_TEXT;
363        tvis.item.pszText = (char *)text;
364        TreeView_InsertItem( tree, &tvis );
365    
366        return 0;
367    } /* treeview_add_item */

Legend:
Removed from v.28  
changed lines
  Added in v.48

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26