/[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 77 by twoaday, Mon Nov 14 15:01:01 2005 UTC revision 205 by twoaday, Thu Apr 27 12:46:03 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
# Line 33  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  int
42  listview_new( listview_ctrl_t *ctx )  listview_new (listview_ctrl_t *ctx)
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 = NULL;
52        c->hil = NULL;
53        c->ext_chkbox = 0;
54      *ctx = c;      *ctx = c;
55      return 0;      return 0;
56  } /* listview_new */  }
57    
58    
59  void  void
60  listview_release( listview_ctrl_t ctx )  listview_release (listview_ctrl_t ctx)
61  {  {
62      if( ctx ) {      if (!ctx)
63          ctx->cols = 0;          return;
64          ctx->ctrl = NULL;  
65          ctx->items = 0;      ctx->cols = 0;
66          delete ctx;      ctx->ctrl = NULL;
67          ctx = NULL;      ctx->items = 0;
68      }      if (ctx->hil)
69  } /* listview_release */          ImageList_Destroy (ctx->hil);
70        delete ctx;
71    }
72    
73    
74    int
75    listview_set_column_width (listview_ctrl_t ctx, int col, int width)
76    {
77        LVCOLUMN lvc;
78    
79        memset (&lvc, 0, sizeof (lvc));
80        lvc.mask = LVCF_WIDTH;
81        lvc.cx = width;
82        ListView_SetColumn (ctx->ctrl, col, &lvc);
83        return 0;
84    }
85    
86    
87  int  int
# Line 81  listview_add_column (listview_ctrl_t ctx Line 100  listview_add_column (listview_ctrl_t ctx
100          rc = 1;          rc = 1;
101      ctx->cols++;      ctx->cols++;
102      return rc;      return rc;
103  } /* listview_add_column */  }
104    
105    
106  int  int
# Line 92  listview_add_item_pos (listview_ctrl_t c Line 111  listview_add_item_pos (listview_ctrl_t c
111    
112      memset (&lvi, 0, sizeof lvi);      memset (&lvi, 0, sizeof lvi);
113      lvi.iItem = pos;      lvi.iItem = pos;
114      rc = ListView_InsertItem (ctx->ctrl, &lvi);      if (ListView_InsertItem (ctx->ctrl, &lvi) != pos)
115      if (rc == -1)          rc = -1;
         rc = 1;  
116      ctx->items++;      ctx->items++;
117      return rc;      return rc;
118  }  }
# Line 114  listview_add_item( listview_ctrl_t ctx, Line 132  listview_add_item( listview_ctrl_t ctx,
132          rc = 1;          rc = 1;
133      ctx->items++;      ctx->items++;
134      return rc;      return rc;
135  } /* listview_add_item */  }
136    
137    
138  int  int
139  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)
140  {  {
141      int rc = 0;      int rc = 0;
142      LV_ITEM lvi;      LV_ITEM lvi;
143    
144      memset( &lvi, 0, sizeof lvi );      memset( &lvi, 0, sizeof lvi );
145        lvi.mask = LVIF_TEXT | LVIF_IMAGE;
146        lvi.pszText = (char *)text;
147        lvi.iImage = image;
148        rc = ListView_InsertItem (ctx->ctrl, &lvi);
149        if (rc == -1)
150            rc = 1;
151        ctx->items++;
152        return rc;
153    }
154    
155    
156    int
157    listview_add_item2 (listview_ctrl_t ctx, const char *text, void *magic)
158    {
159        int rc = 0;
160        LVITEM lvi;
161    
162        memset (&lvi, 0, sizeof (lvi));
163      lvi.mask = LVIF_TEXT | LVIF_PARAM;      lvi.mask = LVIF_TEXT | LVIF_PARAM;
164      lvi.pszText = (char *)text;      lvi.pszText = (char *)text;
165      lvi.lParam = (LPARAM)magic;      lvi.lParam = (LPARAM)magic;
166      rc = ListView_InsertItem( ctx->ctrl, &lvi );      rc = ListView_InsertItem (ctx->ctrl, &lvi);
167      if( rc == -1 )      if (rc == -1)
168          rc = 1;          rc = 1;
169      ctx->items++;      ctx->items++;
170      return rc;      return rc;
171  } /* listview_add_item2 */  }
172    
173    
174  void*  void*
# Line 163  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 177  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
220  listview_del_item( listview_ctrl_t ctx, int pos )  listview_del_column (listview_ctrl_t ctx, int pos)
221    {
222        ctx->cols--;
223        return ListView_DeleteColumn (ctx->ctrl, pos)? 0 : 1;
224    }
225    
226    
227    /* Delete a single item from @ctx at position @pos. */
228    int
229    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
# Line 267  listview_sort_items( listview_ctrl_t ctx Line 337  listview_sort_items( listview_ctrl_t ctx
337                                      IMI_SORT_DOWNARROW : IMI_SORT_UPARROW);                                      IMI_SORT_DOWNARROW : IMI_SORT_UPARROW);
338      Header_SetItem (hheader, idx, &hdi);      Header_SetItem (hheader, idx, &hdi);
339      return 0;      return 0;
340  } /* listview_sort_items */  }
341    
342    
343  int  int
344  listview_get_curr_pos( listview_ctrl_t ctx )  listview_get_curr_pos( listview_ctrl_t ctx )
345  {        {      
346      return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED );      return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED );
347  } /* listview_get_curr_pos */  }
348    
349    
350  int  int
# Line 288  listview_get_item_text (listview_ctrl_t Line 358  listview_get_item_text (listview_ctrl_t
358      }      }
359      ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes);      ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes);
360      return 0;      return 0;
361  } /* listview_get_item_text */  }
362    
363    
364    /* Use extended style with checkboxes for each item. */
365  void  void
366  listview_set_ext_style( listview_ctrl_t ctx )  listview_set_chkbox_style (listview_ctrl_t ctx)
367    {
368        ListView_SetExtendedListViewStyle (ctx->ctrl, LVS_EX_CHECKBOXES);
369        ctx->ext_chkbox = 1;
370    }
371    
372    
373    /* Use extended style to select the entire row. */
374    void
375    listview_set_ext_style (listview_ctrl_t ctx)
376  {        {      
377      ListView_SetExtendedListViewStyle( ctx->ctrl, LVS_EX_FULLROWSELECT );      ListView_SetExtendedListViewStyle (ctx->ctrl, LVS_EX_FULLROWSELECT);
378  } /* listview_set_ext_style */  }
379    
380    
381  int  int
382  listview_set_column_order( listview_ctrl_t ctx, int *array )  listview_set_column_order( listview_ctrl_t ctx, int *array )
383  {        {      
384      return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array );      return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array );
385  } /* listview_set_column_order */  }
386    
387    
388  void  void
389  listview_select_all (listview_ctrl_t ctx)  listview_select_all (listview_ctrl_t ctx)
390  {        {      
391      ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED );      ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED );
392  } /* listview_select_all */  }
393    
394    
395  void  void
# Line 351  listview_find (listview_ctrl_t ctx, cons Line 431  listview_find (listview_ctrl_t ctx, cons
431      return pos;      return pos;
432  }  }
433    
 /** 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 );  
434    
435      return 0;  void
436  } /* treeview_add_item */  listview_setview (listview_ctrl_t ctx, DWORD view)
437    {
438        DWORD style = GetWindowLong (ctx->ctrl, GWL_STYLE);
439        if ((style & LVS_TYPEMASK) != view)
440            SetWindowLong (ctx->ctrl, GWL_STYLE, (style & ~LVS_TYPEMASK) | view);
441    }
442    
443    
444    void
445    listview_set_image_list (listview_ctrl_t ctx, int cx, int cy,
446                             HICON *ico, DWORD nicons)
447    {
448        HIMAGELIST hil;
449        DWORD i;
450    
451        if (cx == 0 || cy == 0)
452            cx = cy = 16;
453    
454        hil = ImageList_Create (cx, cy, ILC_COLOR8|ILC_MASK, nicons, 1);
455        ImageList_SetBkColor (hil, CLR_NONE);
456        for (i=0; i < nicons; i++)
457            ImageList_AddIcon (hil, ico[i]);    
458        ListView_SetImageList (ctx->ctrl, hil, LVSIL_SMALL);    
459    }

Legend:
Removed from v.77  
changed lines
  Added in v.205

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26