/[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 48 by werner, Mon Oct 31 21:14:11 2005 UTC revision 236 by twoaday, Wed Jun 28 06:59:30 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    
 #ifdef __MINGW32__  
 #include <w32api.h>  
 #define _WIN32_IE IE401  
 #endif  
   
 #include <stdio.h>  
26  #include <stdio.h>  #include <stdio.h>
27  #include <windows.h>  #include <windows.h>
28  #include <commctrl.h>  #include <commctrl.h>
# Line 39  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 "wptUTF8.h"
38  #include "resource.h"  #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;      c->cols = 0;
50      c->items = 0;      c->items = 0;
51      c->ctrl = NULL;          c->ctrl = ctrl? ctrl : NULL;
52        c->hil = NULL;
53        c->ext_chkbox = 0;
54      *ctx = c;      *ctx = c;
55      return 0;  }
 } /* listview_new */  
56    
57    
58  void  void
59  listview_release( listview_ctrl_t ctx )  listview_release (listview_ctrl_t ctx)
60  {  {
61      if( ctx ) {      if (!ctx)
62          ctx->cols = 0;          return;
63          ctx->ctrl = NULL;  
64          ctx->items = 0;      ctx->cols = 0;
65          delete ctx;      ctx->ctrl = NULL;
66          ctx = NULL;      ctx->items = 0;
67      }      if (ctx->hil)
68  } /* listview_release */          ImageList_Destroy (ctx->hil);
69        delete ctx;
70    }
71    
72    
73    int
74    listview_set_column_width (listview_ctrl_t ctx, int col, int width)
75    {
76        LVCOLUMN lvc;
77    
78        memset (&lvc, 0, sizeof (lvc));
79        lvc.mask = LVCF_WIDTH;
80        lvc.cx = width;
81        ListView_SetColumn (ctx->ctrl, col, &lvc);
82        return 0;
83    }
84    
85    
86  int  int
# Line 87  listview_add_column (listview_ctrl_t ctx Line 99  listview_add_column (listview_ctrl_t ctx
99          rc = 1;          rc = 1;
100      ctx->cols++;      ctx->cols++;
101      return rc;      return rc;
102  } /* listview_add_column */  }
103    
104    
105  int  int
# Line 98  listview_add_item_pos (listview_ctrl_t c Line 110  listview_add_item_pos (listview_ctrl_t c
110    
111      memset (&lvi, 0, sizeof lvi);      memset (&lvi, 0, sizeof lvi);
112      lvi.iItem = pos;      lvi.iItem = pos;
113      rc = ListView_InsertItem (ctx->ctrl, &lvi);      if (ListView_InsertItem (ctx->ctrl, &lvi) != pos)
114      if (rc == -1)          rc = -1;
         rc = 1;  
115      ctx->items++;      ctx->items++;
116      return rc;      return rc;
117  }  }
# Line 120  listview_add_item( listview_ctrl_t ctx, Line 131  listview_add_item( listview_ctrl_t ctx,
131          rc = 1;          rc = 1;
132      ctx->items++;      ctx->items++;
133      return rc;      return rc;
134  } /* listview_add_item */  }
135    
136    
137  int  int
138  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)
139  {  {
140      int rc = 0;      int rc = 0;
141      LV_ITEM lvi;      LV_ITEM lvi;
142    
143      memset( &lvi, 0, sizeof lvi );      memset( &lvi, 0, sizeof lvi );
144        lvi.mask = LVIF_TEXT | LVIF_IMAGE;
145        lvi.pszText = (char *)text;
146        lvi.iImage = image;
147        rc = ListView_InsertItem (ctx->ctrl, &lvi);
148        if (rc == -1)
149            rc = 1;
150        ctx->items++;
151        return rc;
152    }
153    
154    
155    int
156    listview_add_item2 (listview_ctrl_t ctx, const char *text, void *magic)
157    {
158        int rc = 0;
159        LVITEM lvi;
160    
161        memset (&lvi, 0, sizeof (lvi));
162      lvi.mask = LVIF_TEXT | LVIF_PARAM;      lvi.mask = LVIF_TEXT | LVIF_PARAM;
163      lvi.pszText = (char *)text;      lvi.pszText = (char *)text;
164      lvi.lParam = (LPARAM)magic;      lvi.lParam = (LPARAM)magic;
165      rc = ListView_InsertItem( ctx->ctrl, &lvi );      rc = ListView_InsertItem (ctx->ctrl, &lvi);
166      if( rc == -1 )      if (rc == -1)
167          rc = 1;          rc = 1;
168      ctx->items++;      ctx->items++;
169      return rc;      return rc;
170  } /* listview_add_item2 */  }
171    
172    
173  void*  void*
# Line 169  listview_set_item2 (listview_ctrl_t ctx, Line 198  listview_set_item2 (listview_ctrl_t ctx,
198    
199    
200  void  void
201  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)
202  {        {
203      ListView_SetItemText( ctx->ctrl, pos, col, (char *)text );      ListView_SetItemText (ctx->ctrl, pos, col, (char*)text);
204  } /* listview_add_sub_item */  }
205    
206    
207  int  int
# Line 183  listview_count_items( listview_ctrl_t ct Line 212  listview_count_items( listview_ctrl_t ct
212      n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) :          n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) :    
213                    ListView_GetItemCount( ctx->ctrl );                    ListView_GetItemCount( ctx->ctrl );
214      return n;      return n;
215  } /* listview_count_items */  }
216    
217    
218    int
219    listview_del_column (listview_ctrl_t ctx, int pos)
220    {
221        ctx->cols--;
222        return ListView_DeleteColumn (ctx->ctrl, pos)? 0 : 1;
223    }
224    
225    
226    /* Delete a single item from @ctx at position @pos. */
227  int  int
228  listview_del_item( listview_ctrl_t ctx, int pos )  listview_del_item (listview_ctrl_t ctx, int pos)
229  {  {
230      int rc = 0;      int rc = 0;
231    
232      if( ListView_DeleteItem( ctx->ctrl, pos ) == -1 )      if (ListView_DeleteItem (ctx->ctrl, pos) == -1)
233          rc = 1;          rc = 1;
234      return rc;      return rc;
235  } /* listview_del_item */  }
236    
237    
238    /* Delete all selected items in @ctx. */
239  int  int
240  listview_del_items( listview_ctrl_t ctx )  listview_del_sel_items (listview_ctrl_t ctx)
241  {        {      
242      int i, n;      int i, n;
243    
     /* delete all selected entries */  
244      n = listview_count_items (ctx, 0);      n = listview_count_items (ctx, 0);
245      for( i = n;  i > 0; i-- ) {      for (i = n;  i > -1; i--) {
246          if ( listview_get_item_state( ctx, i ) )          if (listview_get_item_state (ctx, i))
247              listview_del_item( ctx, i );                      listview_del_item (ctx, i);
248      }      }
249      return 0;      return 0;
250  } /* listview_del_items */  }
251    
252    
253  int  int
254  listview_del_all( listview_ctrl_t ctx )  listview_del_all_items (listview_ctrl_t ctx)
255  {        {      
256      int rc = 0;      int rc = 0;
257    
258      if( ListView_DeleteAllItems( ctx->ctrl ) == FALSE )      if (ListView_DeleteAllItems (ctx->ctrl) == FALSE)
259          rc = 1;          rc = 1;
260      return rc;      return rc;
261  } /* listview_del_all */  }
262    
263    
264    /* Return the index of the selected item.
265       Support both selections and checkboxes. */
266  int  int
267  listview_get_item_state( listview_ctrl_t ctx, int pos )  listview_get_selected_item (listview_ctrl_t lv)
268  {        {
269      return ListView_GetItemState( ctx->ctrl, pos, LVIS_SELECTED );      int pos, i;
270  } /* listview_get_item_state */      
271        pos = listview_get_curr_pos (lv);
272        if (pos != -1)
273            return pos;
274        for (i=0; i < listview_count_items (lv, 0); i++) {
275            if (listview_get_item_state (lv, i))
276                return i;
277        }
278        return -1;
279    }
280    
281    
282    /* Return if the given element is selected or if the ext
283       style is used, if the checkbox is activated. */
284    int
285    listview_get_item_state (listview_ctrl_t ctx, int pos)
286    {
287        int ret;
288        if (ctx->ext_chkbox)
289            ret = ListView_GetCheckState (ctx->ctrl, pos);
290        else
291            ret = ListView_GetItemState (ctx->ctrl, pos, LVIS_SELECTED);
292        return ret;
293    }
294    
295    
296  int  int
297  listview_sort_items( listview_ctrl_t ctx, int sortby, listview_cmp sort_cb )  listview_sort_items (listview_ctrl_t ctx, int sortby, listview_cmp sort_cb)
298  {        {      
299      HWND hheader;      HWND hheader;
300      HDITEM hdi;      HDITEM hdi;
# Line 267  listview_sort_items( listview_ctrl_t ctx Line 330  listview_sort_items( listview_ctrl_t ctx
330      /* Set image to currently sorted field */      /* Set image to currently sorted field */
331      memset (&hdi, 0, sizeof(hdi));      memset (&hdi, 0, sizeof(hdi));
332      hdi.mask = HDI_IMAGE | HDI_FORMAT;      hdi.mask = HDI_IMAGE | HDI_FORMAT;
333      Header_GetItem (hheader, idx, &hdi);      Header_GetItem (hheader, idx, &hdi);  
334      hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT;      hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT;
335      hdi.iImage = imagelist_getindex((sortby & KEYLIST_SORT_DESC) ?      if (!ctx->ext_chkbox)
336                                      IMI_SORT_DOWNARROW : IMI_SORT_UPARROW);          hdi.iImage = imagelist_getindex((sortby & KEYLIST_SORT_DESC) ?
337                                            IMI_SORT_DOWNARROW : IMI_SORT_UPARROW);
338        else
339            hdi.iImage = (sortby & KEYLIST_SORT_DESC)? 2 : 3;
340      Header_SetItem (hheader, idx, &hdi);      Header_SetItem (hheader, idx, &hdi);
341      return 0;      return 0;
342  } /* listview_sort_items */  }
343    
344    
345  int  int
346  listview_get_curr_pos( listview_ctrl_t ctx )  listview_get_curr_pos( listview_ctrl_t ctx )
347  {        {      
348      return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED );      return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED );
349  } /* listview_get_curr_pos */  }
350    
351    
352  int  int
# Line 294  listview_get_item_text (listview_ctrl_t Line 360  listview_get_item_text (listview_ctrl_t
360      }      }
361      ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes);      ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes);
362      return 0;      return 0;
363  } /* listview_get_item_text */  }
364    
365    
366    /* Use extended style with checkboxes for each item. */
367    void
368    listview_set_chkbox_style (listview_ctrl_t ctx)
369    {
370        ListView_SetExtendedListViewStyle (ctx->ctrl, LVS_EX_CHECKBOXES);
371        ctx->ext_chkbox = 1;
372    }
373    
374    
375    /* Use extended style to select the entire row. */
376  void  void
377  listview_set_ext_style( listview_ctrl_t ctx )  listview_set_ext_style (listview_ctrl_t ctx)
378  {        {      
379      ListView_SetExtendedListViewStyle( ctx->ctrl, LVS_EX_FULLROWSELECT );      ListView_SetExtendedListViewStyle (ctx->ctrl, LVS_EX_FULLROWSELECT);
380  } /* listview_set_ext_style */  }
381    
382    
383  int  int
384  listview_set_column_order( listview_ctrl_t ctx, int *array )  listview_set_column_order( listview_ctrl_t ctx, int *array )
385  {        {      
386      return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array );      return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array );
387  } /* listview_set_column_order */  }
388    
389    
390  void  void
391  listview_select_all (listview_ctrl_t ctx)  listview_select_all (listview_ctrl_t ctx)
392  {        {      
393      ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED );      ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED );
394  } /* listview_select_all */  }
395    
396    
397    void
398    listview_deselect_all (listview_ctrl_t ctx)
399    {
400        ListView_SetItemState (ctx->ctrl, -1, ~LVNI_SELECTED, LVNI_SELECTED);
401    }
402    
403    
404  void  void
# Line 338  listview_scroll (listview_ctrl_t ctx, in Line 421  listview_scroll (listview_ctrl_t ctx, in
421    
422    
423  int  int
424  listview_find (listview_ctrl_t ctx, const char * str)  listview_find (listview_ctrl_t ctx, const char *str)
425  {  {
426      LVFINDINFO inf;      LVFINDINFO inf;
427      int pos;      int pos;
428    
429        /* XXX: allow to make a substring search. */
430      memset (&inf, 0, sizeof (inf));      memset (&inf, 0, sizeof (inf));
431      inf.flags = LVFI_STRING|LVFI_PARTIAL;      inf.flags = LVFI_STRING|LVFI_PARTIAL;
432      inf.psz = str;      inf.psz = str;
# Line 350  listview_find (listview_ctrl_t ctx, cons Line 434  listview_find (listview_ctrl_t ctx, cons
434      return pos;      return pos;
435  }  }
436    
 /** 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 );  
437    
438      return 0;  void
439  } /* treeview_add_item */  listview_setview (listview_ctrl_t ctx, DWORD view)
440    {
441        DWORD style = GetWindowLong (ctx->ctrl, GWL_STYLE);
442        if ((style & LVS_TYPEMASK) != view)
443            SetWindowLong (ctx->ctrl, GWL_STYLE, (style & ~LVS_TYPEMASK) | view);
444    }
445    
446    
447    void
448    listview_set_image_list (listview_ctrl_t ctx, int cx, int cy,
449                             HICON *ico, DWORD nicons)
450    {
451        HIMAGELIST hil;
452        DWORD i;
453    
454        if (cx == 0 || cy == 0)
455            cx = cy = 16;
456    
457        hil = ImageList_Create (cx, cy, ILC_COLOR8|ILC_MASK, nicons, 1);
458        ImageList_SetBkColor (hil, CLR_NONE);
459        for (i=0; i < nicons; i++)
460            ImageList_AddIcon (hil, ico[i]);    
461        ListView_SetImageList (ctx->ctrl, hil, LVSIL_SMALL);    
462    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26