/[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 36 by werner, Thu Oct 27 15:25:13 2005 UTC revision 273 by twoaday, Fri Dec 8 10:22:17 2006 UTC
# Line 1  Line 1 
1  /* wptListView.cpp - Dynamic list view control  /* wptListView.cpp - Dynamic list view control
2   *      Copyright (C) 2000-2005 Timo Schulz   *      Copyright (C) 2000-2006 Timo Schulz
3   *      Copyright (C) 2004 Andreas Jobs   *      Copyright (C) 2004 Andreas Jobs
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
# Line 18  Line 18 
18   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
19   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20   */   */
21    
22  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
23  #include <config.h>  #include <config.h>
24  #endif  #endif
25    
26  #include <stdio.h>  #include <stdio.h>
 #include <stdio.h>  
27  #include <windows.h>  #include <windows.h>
28  #include <commctrl.h>  #include <commctrl.h>
29    
# Line 34  Line 34 
34  #include "wptTypes.h"  #include "wptTypes.h"
35  #include "wptGPG.h"  #include "wptGPG.h"
36  #include "wptKeylist.h"  #include "wptKeylist.h"
37  #include "../resource.h"  #include "wptUTF8.h"
38    #include "resource.h"
39    
40    
41  int  void
42  listview_new( listview_ctrl_t *ctx )  listview_new (listview_ctrl_t *ctx, HWND ctrl)
43  {  {
44      struct listview_ctrl_s *c;      struct listview_ctrl_s *c;
45    
46      c = new struct listview_ctrl_s;      c = new struct listview_ctrl_s;
47      if( !c )      if (!c)
48          BUG( NULL );          BUG (NULL);
49      c->cols = 0;      memset (c, 0, sizeof *c);
50      c->items = 0;      c->ctrl = ctrl? ctrl : NULL;
51      c->ctrl = NULL;          c->hil = NULL;
52      *ctx = c;      *ctx = c;
53      return 0;  }
 } /* listview_new */  
54    
55    
56  void  void
57  listview_release( listview_ctrl_t ctx )  listview_release (listview_ctrl_t ctx)
58  {  {
59      if( ctx ) {      if (!ctx)
60          ctx->cols = 0;          return;
61          ctx->ctrl = NULL;  
62          ctx->items = 0;      ctx->cols = 0;
63          delete ctx;      ctx->ctrl = NULL;
64          ctx = NULL;      ctx->items = 0;
65      }      if (ctx->hil)
66  } /* listview_release */          ImageList_Destroy (ctx->hil);
67        delete ctx;
68    }
69    
70    
71    int
72    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  int
# Line 72  listview_add_column (listview_ctrl_t ctx Line 87  listview_add_column (listview_ctrl_t ctx
87      int rc = 0;      int rc = 0;
88      LV_COLUMN lvc;      LV_COLUMN lvc;
89    
90      memset( &lvc, 0, sizeof lvc );      memset (&lvc, 0, sizeof lvc);
91      lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;      lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
92      lvc.pszText = col->fieldname;      lvc.pszText = col->fieldname;
93      lvc.cx = col->width;      lvc.cx = col->width;
94      lvc.fmt = LVCFMT_LEFT;      lvc.fmt = LVCFMT_LEFT;
95      lvc.iSubItem = col->pos;      lvc.iSubItem = col->pos;
96      if( ListView_InsertColumn (ctx->ctrl, col->pos, &lvc) == -1)      if (ListView_InsertColumn (ctx->ctrl, col->pos, &lvc) == -1)
97          rc = 1;          rc = 1;
98      ctx->cols++;      ctx->cols++;
99      return rc;      return rc;
100  } /* listview_add_column */  }
101    
102    
103  int  int
# Line 93  listview_add_item_pos (listview_ctrl_t c Line 108  listview_add_item_pos (listview_ctrl_t c
108    
109      memset (&lvi, 0, sizeof lvi);      memset (&lvi, 0, sizeof lvi);
110      lvi.iItem = pos;      lvi.iItem = pos;
111      rc = ListView_InsertItem (ctx->ctrl, &lvi);      if (ListView_InsertItem (ctx->ctrl, &lvi) != pos)
112      if (rc == -1)          rc = -1;
         rc = 1;  
113      ctx->items++;      ctx->items++;
114      return rc;      return rc;
115  }  }
116    
117    
118  int  int
119  listview_add_item( listview_ctrl_t ctx, const char *text )  listview_add_item (listview_ctrl_t ctx, const char *text)
120  {  {
121      int rc = 0;      int rc = 0;
122      LV_ITEM lvi;      LV_ITEM lvi;
123    
124      memset( &lvi, 0, sizeof lvi );      memset (&lvi, 0, sizeof lvi);
125      lvi.mask = LVIF_TEXT;      lvi.mask = LVIF_TEXT;
126      lvi.pszText = (char *)text;      lvi.pszText = (char *)text;
127      rc = ListView_InsertItem( ctx->ctrl, &lvi );      rc = ListView_InsertItem (ctx->ctrl, &lvi);
128      if( rc == -1 )      if( rc == -1 )
129          rc = 1;          rc = 1;
130      ctx->items++;      ctx->items++;
131      return rc;      return rc;
132  } /* listview_add_item */  }
133    
134    
135  int  int
136  listview_add_item2 (listview_ctrl_t ctx, const char *text, void *magic)  listview_add_item_image (listview_ctrl_t ctx, const char *text, int image)
137  {  {
138      int rc = 0;      int rc = 0;
139      LV_ITEM lvi;      LV_ITEM lvi;
140    
141      memset( &lvi, 0, sizeof lvi );      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    listview_add_item2 (listview_ctrl_t ctx, const char *text, void *magic)
155    {
156        int rc = 0;
157        LVITEM lvi;
158    
159        memset (&lvi, 0, sizeof (lvi));
160      lvi.mask = LVIF_TEXT | LVIF_PARAM;      lvi.mask = LVIF_TEXT | LVIF_PARAM;
161      lvi.pszText = (char *)text;      lvi.pszText = (char *)text;
162      lvi.lParam = (LPARAM)magic;      lvi.lParam = (LPARAM)magic;
163      rc = ListView_InsertItem( ctx->ctrl, &lvi );      rc = ListView_InsertItem (ctx->ctrl, &lvi);
164      if( rc == -1 )      if (rc == -1)
165          rc = 1;          rc = 1;
166      ctx->items++;      ctx->items++;
167      return rc;      return rc;
168  } /* listview_add_item2 */  }
169    
170    
171  void*  void*
# Line 148  listview_get_item2 (listview_ctrl_t ctx, Line 180  listview_get_item2 (listview_ctrl_t ctx,
180      return (void*)lvi.lParam;      return (void*)lvi.lParam;
181  }  }
182    
183    
184  int  int
185  listview_set_item2 (listview_ctrl_t ctx, int pos, void *magic)  listview_set_item2 (listview_ctrl_t ctx, int pos, void *magic)
186  {  {
# Line 164  listview_set_item2 (listview_ctrl_t ctx, Line 197  listview_set_item2 (listview_ctrl_t ctx,
197    
198    
199  void  void
200  listview_add_sub_item( listview_ctrl_t ctx, int pos, int col, const char *text )  listview_add_sub_item (listview_ctrl_t ctx, int pos, int col, const char *text)
201  {        {
202      ListView_SetItemText( ctx->ctrl, pos, col, (char *)text );      ListView_SetItemText (ctx->ctrl, pos, col, (char*)text);
203  } /* listview_add_sub_item */  }
204    
205    
206  int  int
# Line 178  listview_count_items( listview_ctrl_t ct Line 211  listview_count_items( listview_ctrl_t ct
211      n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) :          n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) :    
212                    ListView_GetItemCount( ctx->ctrl );                    ListView_GetItemCount( ctx->ctrl );
213      return n;      return n;
214  } /* listview_count_items */  }
215    
216    
217    int
218    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    /* Delete a single item from @ctx at position @pos. */
226  int  int
227  listview_del_item( listview_ctrl_t ctx, int pos )  listview_del_item (listview_ctrl_t ctx, int pos)
228  {  {
229      int rc = 0;      int rc = 0;
230    
231      if( ListView_DeleteItem( ctx->ctrl, pos ) == -1 )      if (ListView_DeleteItem (ctx->ctrl, pos) == -1)
232          rc = 1;          rc = 1;
233      return rc;      return rc;
234  } /* listview_del_item */  }
235    
236    
237    /* Delete all selected items in @ctx. */
238  int  int
239  listview_del_items( listview_ctrl_t ctx )  listview_del_sel_items (listview_ctrl_t ctx)
240  {        {      
241      int i, n;      int i, n;
242    
     /* delete all selected entries */  
243      n = listview_count_items (ctx, 0);      n = listview_count_items (ctx, 0);
244      for( i = n;  i > 0; i-- ) {      for (i = n;  i > -1; i--) {
245          if ( listview_get_item_state( ctx, i ) )          if (listview_get_item_state (ctx, i))
246              listview_del_item( ctx, i );                      listview_del_item (ctx, i);
247      }      }
248      return 0;      return 0;
249  } /* listview_del_items */  }
250    
251    
252  int  int
253  listview_del_all( listview_ctrl_t ctx )  listview_del_all_items (listview_ctrl_t ctx)
254  {        {      
255      int rc = 0;      int rc = 0;
256    
257      if( ListView_DeleteAllItems( ctx->ctrl ) == FALSE )      if (ListView_DeleteAllItems (ctx->ctrl) == FALSE)
258          rc = 1;          rc = 1;
259      return rc;      return rc;
260  } /* listview_del_all */  }
261    
262    
263    /* Return the index of the selected item.
264       Support both selections and checkboxes. */
265  int  int
266  listview_get_item_state( listview_ctrl_t ctx, int pos )  listview_get_selected_item (listview_ctrl_t lv)
267  {        {
268      return ListView_GetItemState( ctx->ctrl, pos, LVIS_SELECTED );      int pos, i;
269  } /* listview_get_item_state */      
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    /* Return if the given element is selected or if the ext
282       style is used, if the checkbox is activated. */
283  int  int
284  listview_sort_items( listview_ctrl_t ctx, int sortby, listview_cmp sort_cb )  listview_get_item_state (listview_ctrl_t ctx, int pos)
285  {        {
286      HWND hheader;      int ret;
287      HDITEM hdi;      if (ctx->ext_chkbox)
288      int idx;          ret = ListView_GetCheckState (ctx->ctrl, pos);
289        else
290            ret = ListView_GetItemState (ctx->ctrl, pos, LVIS_SELECTED);
291        return ret;
292    }
293    
     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);  
     }  
294    
295      switch (sortby & ~KEYLIST_SORT_DESC) {  int
296      case KEY_SORT_USERID:    idx = 0; break;  listview_sort_items (listview_ctrl_t ctx, int sortby, listview_cmp sort_cb)
297      case KEY_SORT_KEYID:     idx = 1; break;  {
298      case KEY_SORT_IS_SECRET: idx = 2; break;      ListView_SortItems (ctx->ctrl, sort_cb, sortby);
     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);  
299      return 0;      return 0;
300  } /* listview_sort_items */  }
301    
302    
303  int  int
304  listview_get_curr_pos( listview_ctrl_t ctx )  listview_get_curr_pos (listview_ctrl_t ctx)
305  {        {      
306      return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED );      return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED );
307  } /* listview_get_curr_pos */  }
308    
309    
310  int  int
# Line 289  listview_get_item_text (listview_ctrl_t Line 318  listview_get_item_text (listview_ctrl_t
318      }      }
319      ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes);      ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes);
320      return 0;      return 0;
321  } /* listview_get_item_text */  }
322    
323    
324    /* Use extended style with checkboxes for each item. */
325  void  void
326  listview_set_ext_style( listview_ctrl_t ctx )  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  {        {      
337      ListView_SetExtendedListViewStyle( ctx->ctrl, LVS_EX_FULLROWSELECT );      ListView_SetExtendedListViewStyle (ctx->ctrl, LVS_EX_FULLROWSELECT);
338  } /* listview_set_ext_style */  }
339    
340    
341  int  int
342  listview_set_column_order( listview_ctrl_t ctx, int *array )  listview_set_column_order( listview_ctrl_t ctx, int *array )
343  {        {      
344      return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array );      return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array );
345  } /* listview_set_column_order */  }
346    
347    
348  void  void
349  listview_select_all (listview_ctrl_t ctx)  listview_select_all (listview_ctrl_t ctx)
350  {        {      
351      ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED );      ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED );
352  } /* listview_select_all */  }
353    
354    
355    void
356    listview_deselect_all (listview_ctrl_t ctx)
357    {
358        ListView_SetItemState (ctx->ctrl, -1, ~LVNI_SELECTED, LVNI_SELECTED);
359    }
360    
361    
362  void  void
363  listview_select_one (listview_ctrl_t ctx, int pos)  listview_select_one (listview_ctrl_t ctx, int pos)
364  {  {
365      ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED, LVIS_FOCUSED|LVIS_SELECTED);      ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED,
366                                                LVIS_FOCUSED|LVIS_SELECTED);
367  }  }
368    
369    
# Line 327  listview_scroll (listview_ctrl_t ctx, in Line 374  listview_scroll (listview_ctrl_t ctx, in
374            
375      if (oldpos == -1)      if (oldpos == -1)
376          oldpos = 0;          oldpos = 0;
377      //log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d", oldpos, newpos, newpos-oldpos);      //log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d",
378                    //oldpos, newpos, newpos-oldpos);
379      ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size);      ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size);
380  }  }
381    
382    
383    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  int  int
405  listview_find (listview_ctrl_t ctx, const char * str)  listview_find (listview_ctrl_t ctx, const char *str, int substr)
406  {  {
407      LVFINDINFO inf;      LVFINDINFO inf;
408      int pos;      int pos;
409    
410        /* XXX: allow to make a substring search. */
411      memset (&inf, 0, sizeof (inf));      memset (&inf, 0, sizeof (inf));
412      inf.flags = LVFI_STRING|LVFI_PARTIAL;      inf.flags = LVFI_STRING|LVFI_PARTIAL;
413      inf.psz = str;      inf.psz = str;
414      pos = ListView_FindItem (ctx->ctrl, -1, &inf);      pos = ListView_FindItem (ctx->ctrl, -1, &inf);
415        if (pos == -1 && substr)
416            pos = listview_find_substr (ctx, str);
417      return pos;      return pos;
418  }  }
419    
 /** 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 );  
420    
421      return 0;  void
422  } /* treeview_add_item */  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    
429    
430    void
431    listview_set_image_list (listview_ctrl_t ctx, int cx, int cy,
432                             HICON *ico, DWORD nicons)
433    {
434        HIMAGELIST hil;
435        DWORD i;
436    
437        if (cx <= 0 || cy <= 0)
438            cx = cy = 16; /* default size. */
439    
440        hil = ImageList_Create (cx, cy, ILC_COLOR8|ILC_MASK, nicons, 1);
441        ImageList_SetBkColor (hil, CLR_NONE);
442        for (i=0; i < nicons; i++)
443            ImageList_AddIcon (hil, ico[i]);    
444        ListView_SetImageList (ctx->ctrl, hil, LVSIL_SMALL);    
445    }

Legend:
Removed from v.36  
changed lines
  Added in v.273

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26