/[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 139 by twoaday, Wed Jan 11 12:19:41 2006 UTC revision 241 by twoaday, Thu Jun 29 11:15:55 2006 UTC
# 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 "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    
# Line 47  listview_new (listview_ctrl_t *ctx) Line 48  listview_new (listview_ctrl_t *ctx)
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;      c->hil = NULL;
53        c->ext_chkbox = 0;
54      *ctx = c;      *ctx = c;
55      return 0;  }
 } /* listview_new */  
56    
57    
58  void  void
# Line 66  listview_release (listview_ctrl_t ctx) Line 67  listview_release (listview_ctrl_t ctx)
67      if (ctx->hil)      if (ctx->hil)
68          ImageList_Destroy (ctx->hil);          ImageList_Destroy (ctx->hil);
69      delete ctx;      delete ctx;
70  } /* listview_release */  }
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 85  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 104  listview_add_item_pos (listview_ctrl_t c Line 118  listview_add_item_pos (listview_ctrl_t c
118    
119    
120  int  int
121  listview_add_item( listview_ctrl_t ctx, const char *text )  listview_add_item (listview_ctrl_t ctx, const char *text)
122  {  {
123      int rc = 0;      int rc = 0;
124      LV_ITEM lvi;      LV_ITEM lvi;
125    
126      memset( &lvi, 0, sizeof lvi );      memset (&lvi, 0, sizeof lvi);
127      lvi.mask = LVIF_TEXT;      lvi.mask = LVIF_TEXT;
128      lvi.pszText = (char *)text;      lvi.pszText = (char *)text;
129      rc = ListView_InsertItem( ctx->ctrl, &lvi );      rc = ListView_InsertItem (ctx->ctrl, &lvi);
130      if( rc == -1 )      if( rc == -1 )
131          rc = 1;          rc = 1;
132      ctx->items++;      ctx->items++;
133      return rc;      return rc;
134  } /* listview_add_item */  }
135    
136    
137  int  int
# Line 153  listview_add_item2 (listview_ctrl_t ctx, Line 167  listview_add_item2 (listview_ctrl_t ctx,
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 168  listview_get_item2 (listview_ctrl_t ctx, Line 182  listview_get_item2 (listview_ctrl_t ctx,
182      return (void*)lvi.lParam;      return (void*)lvi.lParam;
183  }  }
184    
185    
186  int  int
187  listview_set_item2 (listview_ctrl_t ctx, int pos, void *magic)  listview_set_item2 (listview_ctrl_t ctx, int pos, void *magic)
188  {  {
# Line 184  listview_set_item2 (listview_ctrl_t ctx, Line 199  listview_set_item2 (listview_ctrl_t ctx,
199    
200    
201  void  void
202  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)
203  {        {
204      ListView_SetItemText( ctx->ctrl, pos, col, (char *)text );      ListView_SetItemText (ctx->ctrl, pos, col, (char*)text);
205  } /* listview_add_sub_item */  }
206    
207    
208  int  int
# Line 198  listview_count_items( listview_ctrl_t ct Line 213  listview_count_items( listview_ctrl_t ct
213      n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) :          n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) :    
214                    ListView_GetItemCount( ctx->ctrl );                    ListView_GetItemCount( ctx->ctrl );
215      return n;      return n;
216  } /* listview_count_items */  }
217    
218    
219  int  int
# Line 209  listview_del_column (listview_ctrl_t ctx Line 224  listview_del_column (listview_ctrl_t ctx
224  }  }
225    
226    
227    /* Delete a single item from @ctx at position @pos. */
228  int  int
229  listview_del_item( listview_ctrl_t ctx, int pos )  listview_del_item (listview_ctrl_t ctx, int pos)
230  {  {
231      int rc = 0;      int rc = 0;
232    
233      if( ListView_DeleteItem( ctx->ctrl, pos ) == -1 )      if (ListView_DeleteItem (ctx->ctrl, pos) == -1)
234          rc = 1;          rc = 1;
235      return rc;      return rc;
236  } /* listview_del_item */  }
237    
238    
239    /* Delete all selected items in @ctx. */
240  int  int
241  listview_del_items( listview_ctrl_t ctx )  listview_del_sel_items (listview_ctrl_t ctx)
242  {        {      
243      int i, n;      int i, n;
244    
     /* delete all selected entries */  
245      n = listview_count_items (ctx, 0);      n = listview_count_items (ctx, 0);
246      for( i = n;  i > 0; i-- ) {      for (i = n;  i > -1; i--) {
247          if ( listview_get_item_state( ctx, i ) )          if (listview_get_item_state (ctx, i))
248              listview_del_item( ctx, i );                      listview_del_item (ctx, i);
249      }      }
250      return 0;      return 0;
251  } /* listview_del_items */  }
252    
253    
254  int  int
255  listview_del_all( listview_ctrl_t ctx )  listview_del_all_items (listview_ctrl_t ctx)
256  {        {      
257      int rc = 0;      int rc = 0;
258    
259      if( ListView_DeleteAllItems( ctx->ctrl ) == FALSE )      if (ListView_DeleteAllItems (ctx->ctrl) == FALSE)
260          rc = 1;          rc = 1;
261      return rc;      return rc;
262  } /* listview_del_all */  }
263    
264    
265    /* Return the index of the selected item.
266       Support both selections and checkboxes. */
267  int  int
268  listview_get_item_state( listview_ctrl_t ctx, int pos )  listview_get_selected_item (listview_ctrl_t lv)
269  {        {
270      return ListView_GetItemState( ctx->ctrl, pos, LVIS_SELECTED );      int pos, i;
271  } /* listview_get_item_state */      
272        pos = listview_get_curr_pos (lv);
273        if (pos != -1)
274            return pos;
275        for (i=0; i < listview_count_items (lv, 0); i++) {
276            if (listview_get_item_state (lv, i))
277                return i;
278        }
279        return -1;
280    }
281    
282    
283    /* Return if the given element is selected or if the ext
284       style is used, if the checkbox is activated. */
285    int
286    listview_get_item_state (listview_ctrl_t ctx, int pos)
287    {
288        int ret;
289        if (ctx->ext_chkbox)
290            ret = ListView_GetCheckState (ctx->ctrl, pos);
291        else
292            ret = ListView_GetItemState (ctx->ctrl, pos, LVIS_SELECTED);
293        return ret;
294    }
295    
296    
297  int  int
298  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)
299  {        {      
300      HWND hheader;      HWND hheader;
301      HDITEM hdi;      HDITEM hdi;
302      int idx;      int idx, sort_desc = 0;
303    
304      ListView_SortItems (ctx->ctrl, sort_cb, sortby);      ListView_SortItems (ctx->ctrl, sort_cb, sortby);
305      hheader = ListView_GetHeader (ctx->ctrl);      hheader = ListView_GetHeader (ctx->ctrl);
# Line 287  listview_sort_items( listview_ctrl_t ctx Line 328  listview_sort_items( listview_ctrl_t ctx
328      default:                 idx = 0; break;      default:                 idx = 0; break;
329      }      }
330    
331        sort_desc = sortby & KEYLIST_SORT_DESC;
332      /* Set image to currently sorted field */      /* Set image to currently sorted field */
333      memset (&hdi, 0, sizeof(hdi));      memset (&hdi, 0, sizeof(hdi));
334      hdi.mask = HDI_IMAGE | HDI_FORMAT;      hdi.mask = HDI_IMAGE | HDI_FORMAT;
335      Header_GetItem (hheader, idx, &hdi);      Header_GetItem (hheader, idx, &hdi);  
336      hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT;      hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT;
337      hdi.iImage = imagelist_getindex((sortby & KEYLIST_SORT_DESC) ?      if (!ctx->ext_chkbox)
338                                      IMI_SORT_DOWNARROW : IMI_SORT_UPARROW);          hdi.iImage = imagelist_getindex (sort_desc? IMI_SORT_DOWNARROW :
339                                                        IMI_SORT_UPARROW);
340        else
341            hdi.iImage = sort_desc? KEY_IMG_SORT_DOWN : KEY_IMG_SORT_UP;
342      Header_SetItem (hheader, idx, &hdi);      Header_SetItem (hheader, idx, &hdi);
343      return 0;      return 0;
344  } /* listview_sort_items */  }
345    
346    
347  int  int
348  listview_get_curr_pos( listview_ctrl_t ctx )  listview_get_curr_pos( listview_ctrl_t ctx )
349  {        {      
350      return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED );      return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED );
351  } /* listview_get_curr_pos */  }
352    
353    
354  int  int
# Line 317  listview_get_item_text (listview_ctrl_t Line 362  listview_get_item_text (listview_ctrl_t
362      }      }
363      ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes);      ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes);
364      return 0;      return 0;
365  } /* listview_get_item_text */  }
366    
367    
368    /* Use extended style with checkboxes for each item. */
369    void
370    listview_set_chkbox_style (listview_ctrl_t ctx)
371    {
372        ListView_SetExtendedListViewStyle (ctx->ctrl, LVS_EX_CHECKBOXES);
373        ctx->ext_chkbox = 1;
374    }
375    
376    
377    /* Use extended style to select the entire row. */
378  void  void
379  listview_set_ext_style( listview_ctrl_t ctx )  listview_set_ext_style (listview_ctrl_t ctx)
380  {        {      
381      ListView_SetExtendedListViewStyle( ctx->ctrl, LVS_EX_FULLROWSELECT );      ListView_SetExtendedListViewStyle (ctx->ctrl, LVS_EX_FULLROWSELECT);
382  } /* listview_set_ext_style */  }
383    
384    
385  int  int
386  listview_set_column_order( listview_ctrl_t ctx, int *array )  listview_set_column_order( listview_ctrl_t ctx, int *array )
387  {        {      
388      return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array );      return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array );
389  } /* listview_set_column_order */  }
390    
391    
392  void  void
393  listview_select_all (listview_ctrl_t ctx)  listview_select_all (listview_ctrl_t ctx)
394  {        {      
395      ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED );      ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED );
396  } /* listview_select_all */  }
397    
398    
399  void  void
# Line 351  listview_deselect_all (listview_ctrl_t c Line 406  listview_deselect_all (listview_ctrl_t c
406  void  void
407  listview_select_one (listview_ctrl_t ctx, int pos)  listview_select_one (listview_ctrl_t ctx, int pos)
408  {  {
409      ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED, LVIS_FOCUSED|LVIS_SELECTED);      ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED,
410                                                LVIS_FOCUSED|LVIS_SELECTED);
411  }  }
412    
413    
# Line 362  listview_scroll (listview_ctrl_t ctx, in Line 418  listview_scroll (listview_ctrl_t ctx, in
418            
419      if (oldpos == -1)      if (oldpos == -1)
420          oldpos = 0;          oldpos = 0;
421      //log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d", oldpos, newpos, newpos-oldpos);      //log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d",
422                    //oldpos, newpos, newpos-oldpos);
423      ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size);      ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size);
424  }  }
425    
426    
427    static int
428    listview_find_substr (listview_ctrl_t ctx, const char *str)
429    {
430        char buf[256];
431        int i, n, fnd = 0;
432    
433        /* We assume the first column contains text. */
434        n = listview_count_items (ctx, 0);
435        for (i = 0; i < n; i++) {
436            listview_get_item_text (ctx, i, 0, buf, sizeof (buf)-1);
437            if (stristr (buf, str)) {
438                fnd = 1;
439                break;
440            }
441        }
442        if (!fnd)
443            i = -1; /* not found */
444        return i;
445    }
446    
447    
448  int  int
449  listview_find (listview_ctrl_t ctx, const char * str)  listview_find (listview_ctrl_t ctx, const char *str, int substr)
450  {  {
451      LVFINDINFO inf;      LVFINDINFO inf;
452      int pos;      int pos;
453    
454        /* XXX: allow to make a substring search. */
455      memset (&inf, 0, sizeof (inf));      memset (&inf, 0, sizeof (inf));
456      inf.flags = LVFI_STRING|LVFI_PARTIAL;      inf.flags = LVFI_STRING|LVFI_PARTIAL;
457      inf.psz = str;      inf.psz = str;
458      pos = ListView_FindItem (ctx->ctrl, -1, &inf);      pos = ListView_FindItem (ctx->ctrl, -1, &inf);
459        if (pos == -1 && substr)
460            pos = listview_find_substr (ctx, str);
461      return pos;      return pos;
462  }  }
463    
# Line 391  listview_setview (listview_ctrl_t ctx, D Line 472  listview_setview (listview_ctrl_t ctx, D
472    
473    
474  void  void
475  listview_set_image_list (listview_ctrl_t ctx, HICON *ico, DWORD nicons)  listview_set_image_list (listview_ctrl_t ctx, int cx, int cy,
476                             HICON *ico, DWORD nicons)
477  {  {
478      HIMAGELIST hil;      HIMAGELIST hil;
479      DWORD i;      DWORD i;
480    
481      hil = ImageList_Create (16, 16, ILC_COLOR16, nicons, 1);      if (cx == 0 || cy == 0)
482            cx = cy = 16;
483    
484        hil = ImageList_Create (cx, cy, ILC_COLOR8|ILC_MASK, nicons, 1);
485        ImageList_SetBkColor (hil, CLR_NONE);
486      for (i=0; i < nicons; i++)      for (i=0; i < nicons; i++)
487          ImageList_AddIcon (hil, ico[i]);          ImageList_AddIcon (hil, ico[i]);    
488      ListView_SetImageList (ctx->ctrl, hil, LVSIL_SMALL);      ListView_SetImageList (ctx->ctrl, hil, LVSIL_SMALL);    
489  }  }
       

Legend:
Removed from v.139  
changed lines
  Added in v.241

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26