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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26