1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxListCtrl class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "listctrl.h"
19 #include "wx/control.h"
21 #include "wx/imaglist.h"
24 The wxListCtrl can show lists of items in four different modes:
25 wxLC_LIST: multicolumn list view, with optional small icons (icons could be
26 optional for some platforms). Columns are computed automatically,
27 i.e. you don't set columns as in wxLC_REPORT. In other words,
28 the list wraps, unlike a wxListBox.
29 wxLC_REPORT: single or multicolumn report view (with optional header)
30 wxLC_ICON: large icon view, with optional labels
31 wxLC_SMALL_ICON: small icon view, with optional labels
33 You can change the style dynamically, either with SetSingleStyle or
36 Further window styles:
38 wxLC_ALIGN_TOP icons align to the top (default)
39 wxLC_ALIGN_LEFT icons align to the left
40 wxLC_AUTOARRANGE icons arrange themselves
41 wxLC_USER_TEXT the app provides label text on demand, except for column headers
42 wxLC_EDIT_LABELS labels are editable: app will be notified.
43 wxLC_NO_HEADER no header in report mode
44 wxLC_NO_SORT_HEADER can't click on header
45 wxLC_SINGLE_SEL single selection
46 wxLC_SORT_ASCENDING sort ascending (must still supply a comparison callback in SortItems)
47 wxLC_SORT_DESCENDING sort descending (ditto)
49 Items are referred to by their index (position in the list starting from zero).
51 Label text is supplied via insertion/setting functions and is stored by the
52 control, unless the wxLC_USER_TEXT style has been specified, in which case
53 the app will be notified when text is required (see sample).
55 Images are dealt with by (optionally) associating 3 image lists with the control.
56 Zero-based indexes into these image lists indicate which image is to be used for
57 which item. Each image in an image list can contain a mask, and can be made out
58 of either a bitmap, two bitmaps or an icon. See ImagList.h for more details.
60 Notifications are passed via the wxWindows 2.0 event system, or using virtual
61 functions in wxWindows 1.66.
63 See the sample wxListCtrl app for API usage.
66 - addition of further convenience functions
67 to avoid use of wxListItem in some functions
68 - state/overlay images: probably not needed.
69 - in Win95, you can be called back to supply other information
70 besides text, such as state information. This saves no memory
71 and is probably superfluous to requirements.
72 - discover why SetWindowLong doesn't properly change the
73 style, requiring RecreateWindow instead.
74 - testing of whole API, extending current sample.
79 // Mask flags to tell app/GUI what fields of wxListItem are valid
80 #define wxLIST_MASK_STATE 0x0001
81 #define wxLIST_MASK_TEXT 0x0002
82 #define wxLIST_MASK_IMAGE 0x0004
83 #define wxLIST_MASK_DATA 0x0008
84 #define wxLIST_SET_ITEM 0x0010
85 #define wxLIST_MASK_WIDTH 0x0020
86 #define wxLIST_MASK_FORMAT 0x0040
88 // State flags for indicating the state of an item
89 #define wxLIST_STATE_DONTCARE 0x0000
90 #define wxLIST_STATE_DROPHILITED 0x0001
91 #define wxLIST_STATE_FOCUSED 0x0002
92 #define wxLIST_STATE_SELECTED 0x0004
93 #define wxLIST_STATE_CUT 0x0008
95 // Hit test flags, used in HitTest
96 #define wxLIST_HITTEST_ABOVE 0x0001 // Above the client area.
97 #define wxLIST_HITTEST_BELOW 0x0002 // Below the client area.
98 #define wxLIST_HITTEST_NOWHERE 0x0004 // In the client area but below the last item.
99 #define wxLIST_HITTEST_ONITEMICON 0x0020 // On the bitmap associated with an item.
100 #define wxLIST_HITTEST_ONITEMLABEL 0x0080 // On the label (string) associated with an item.
101 #define wxLIST_HITTEST_ONITEMRIGHT 0x0100 // In the area to the right of an item.
102 #define wxLIST_HITTEST_ONITEMSTATEICON 0x0200 // On the state icon for a tree view item that is in a user-defined state.
103 #define wxLIST_HITTEST_TOLEFT 0x0400 // To the left of the client area.
104 #define wxLIST_HITTEST_TORIGHT 0x0800 // To the right of the client area.
106 #define wxLIST_HITTEST_ONITEM (wxLIST_HITTEST_ONITEMICON | wxLIST_HITTEST_ONITEMLABEL wxLIST_HITTEST_ONITEMSTATEICON)
108 // Flags for GetNextItem
110 wxLIST_NEXT_ABOVE
, // Searches for an item above the specified item
111 wxLIST_NEXT_ALL
, // Searches for subsequent item by index
112 wxLIST_NEXT_BELOW
, // Searches for an item below the specified item
113 wxLIST_NEXT_LEFT
, // Searches for an item to the left of the specified item
114 wxLIST_NEXT_RIGHT
, // Searches for an item to the right of the specified item
117 // Alignment flags for Arrange
119 wxLIST_ALIGN_DEFAULT
,
122 wxLIST_ALIGN_SNAP_TO_GRID
129 wxLIST_FORMAT_CENTRE
,
130 wxLIST_FORMAT_CENTER
= wxLIST_FORMAT_CENTRE
133 // Autosize values for SetColumnWidth
135 wxLIST_AUTOSIZE
= -1,
136 wxLIST_AUTOSIZE_USEHEADER
= -2
139 // Flag values for GetItemRect
146 // Flag values for FindItem
154 // wxListItem: data representing an item, or report field.
155 // It also doubles up to represent entire column information
156 // when inserting or setting a column.
157 class WXDLLEXPORT wxListItem
: public wxObject
159 DECLARE_DYNAMIC_CLASS(wxListItem
)
161 long m_mask
; // Indicates what fields are valid
162 long m_itemId
; // The zero-based item position
163 int m_col
; // Zero-based column, if in report mode
164 long m_state
; // The state of the item
165 long m_stateMask
; // Which flags of m_state are valid (uses same flags)
166 wxString m_text
; // The label/header text
167 int m_image
; // The zero-based index into an image list
168 long m_data
; // App-defined data
171 int m_format
; // left, right, centre
172 int m_width
; // width of column
177 // type of compare function for wxListCtrl sort operation
178 typedef int (*wxListCtrlCompare
)(const long item1
, const long item2
, long sortData
);
180 class WXDLLEXPORT wxListCtrl
: public wxControl
182 DECLARE_DYNAMIC_CLASS(wxListCtrl
)
190 inline wxListCtrl(wxWindow
*parent
, const wxWindowID id
= -1, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
191 const long style
= wxLC_ICON
, const wxValidator
& validator
= wxDefaultValidator
,
192 const wxString
& name
= "listCtrl")
194 Create(parent
, id
, pos
, size
, style
, validator
, name
);
198 bool Create(wxWindow
*parent
, const wxWindowID id
= -1, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
199 const long style
= wxLC_ICON
, const wxValidator
& validator
= wxDefaultValidator
, const wxString
& name
= "wxListCtrl");
203 ////////////////////////////////////////////////////////////////////////////
205 // Sets the background colour (GetBackgroundColour already implicit in
207 void SetBackgroundColour(const wxColour
& col
);
209 // Gets information about this column
210 bool GetColumn(const int col
, wxListItem
& item
) const;
212 // Sets information about this column
213 bool SetColumn(const int col
, wxListItem
& item
) ;
215 // Gets the column width
216 int GetColumnWidth(const int col
) const;
218 // Sets the column width
219 bool SetColumnWidth(const int col
, const int width
) ;
221 // Gets the number of items that can fit vertically in the
222 // visible area of the list control (list or report view)
223 // or the total number of items in the list control (icon
224 // or small icon view)
225 int GetCountPerPage(void) const;
227 // Gets the edit control for editing labels.
228 wxTextCtrl
& GetEditControl(void) const;
230 // Gets information about the item
231 bool GetItem(wxListItem
& info
) const ;
233 // Sets information about the item
234 bool SetItem(wxListItem
& info
) ;
236 // Sets a string field at a particular column
237 long SetItem(const long index
, const int col
, const wxString
& label
, const int imageId
= -1);
239 // Gets the item state
240 int GetItemState(const long item
, const long stateMask
) const ;
242 // Sets the item state
243 bool SetItemState(const long item
, const long state
, const long stateMask
) ;
245 // Sets the item image
246 bool SetItemImage(const long item
, const int image
, const int selImage
) ;
248 // Gets the item text
249 wxString
GetItemText(const long item
) const ;
251 // Sets the item text
252 void SetItemText(const long item
, const wxString
& str
) ;
254 // Gets the item data
255 long GetItemData(const long item
) const ;
257 // Sets the item data
258 bool SetItemData(const long item
, long data
) ;
260 // Gets the item rectangle
261 bool GetItemRect(const long item
, wxRectangle
& rect
, const int code
= wxLIST_RECT_BOUNDS
) const ;
263 // Gets the item position
264 bool GetItemPosition(const long item
, wxPoint
& pos
) const ;
266 // Sets the item position
267 bool SetItemPosition(const long item
, const wxPoint
& pos
) ;
269 // Gets the number of items in the list control
270 int GetItemCount(void) const;
272 // Gets the number of columns in the list control
273 int GetColumnCount(void) const;
275 // Retrieves the spacing between icons in pixels.
276 // If small is TRUE, gets the spacing for the small icon
277 // view, otherwise the large icon view.
278 int GetItemSpacing(bool isSmall
) const;
280 // Gets the number of selected items in the list control
281 int GetSelectedItemCount(void) const;
283 // Gets the text colour of the listview
284 wxColour
GetTextColour(void) const;
286 // Sets the text colour of the listview
287 void SetTextColour(const wxColour
& col
);
289 // Gets the index of the topmost visible item when in
290 // list or report view
291 long GetTopItem(void) const ;
293 // Add or remove a single window style
294 void SetSingleStyle(const long style
, const bool add
= TRUE
) ;
296 // Set the whole window style
297 void SetWindowStyleFlag(const long style
) ;
299 // Searches for an item, starting from 'item'.
300 // item can be -1 to find the first item that matches the
302 // Returns the item or -1 if unsuccessful.
303 long GetNextItem(const long item
, int geometry
= wxLIST_NEXT_ALL
, int state
= wxLIST_STATE_DONTCARE
) const ;
305 // Implementation: converts wxWindows style to MSW style.
306 // Can be a single style flag or a bit list.
307 // oldStyle is 'normalised' so that it doesn't contain
308 // conflicting styles.
309 long ConvertToMSWStyle(long& oldStyle
, const long style
) const;
311 // Gets one of the three image lists
312 wxImageList
*GetImageList(const int which
) const ;
314 // Sets the image list
315 // N.B. There's a quirk in the Win95 list view implementation.
316 // If in wxLC_LIST mode, it'll *still* display images by the labels if
317 // there's a small-icon image list set for the control - even though you
318 // haven't specified wxLIST_MASK_IMAGE when inserting.
319 // So you have to set a NULL small-icon image list to be sure that
320 // the wxLC_LIST mode works without icons. Of course, you may want icons...
321 void SetImageList(wxImageList
*imageList
, const int which
) ;
324 ////////////////////////////////////////////////////////////////////////////
326 // Arranges the items
327 bool Arrange(const int flag
= wxLIST_ALIGN_DEFAULT
);
330 bool DeleteItem(const long item
);
333 bool DeleteAllItems(void) ;
336 bool DeleteColumn(const int col
);
338 // Deletes all columns
339 bool DeleteAllColumns(void);
341 // Clears items, and columns if there are any.
345 wxTextCtrl
& Edit(const long item
) ;
347 // Ensures this item is visible
348 bool EnsureVisible(const long item
) ;
350 // Find an item whose label matches this string, starting from the item after 'start'
351 // or the beginning if 'start' is -1.
352 long FindItem(const long start
, const wxString
& str
, const bool partial
= FALSE
);
354 // Find an item whose data matches this data, starting from the item after 'start'
355 // or the beginning if 'start' is -1.
356 long FindItem(const long start
, const long data
);
358 // Find an item nearest this position in the specified direction, starting from
359 // the item after 'start' or the beginning if 'start' is -1.
360 long FindItem(const long start
, const wxPoint
& pt
, const int direction
);
362 // Determines which item (if any) is at the specified point,
363 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
364 long HitTest(const wxPoint
& point
, int& flags
);
366 // Inserts an item, returning the index of the new item if successful,
368 // TOD: Should also have some further convenience functions
369 // which don't require setting a wxListItem object
370 long InsertItem(wxListItem
& info
);
372 // Insert a string item
373 long InsertItem(const long index
, const wxString
& label
);
375 // Insert an image item
376 long InsertItem(const long index
, const int imageIndex
);
378 // Insert an image/string item
379 long InsertItem(const long index
, const wxString
& label
, const int imageIndex
);
381 // For list view mode (only), inserts a column.
382 long InsertColumn(const long col
, wxListItem
& info
);
384 long InsertColumn(const long col
, const wxString
& heading
, const int format
= wxLIST_FORMAT_LEFT
,
385 const int width
= -1);
387 // Scrolls the list control. If in icon, small icon or report view mode,
388 // x specifies the number of pixels to scroll. If in list view mode, x
389 // specifies the number of columns to scroll.
390 // If in icon, small icon or list view mode, y specifies the number of pixels
391 // to scroll. If in report view mode, y specifies the number of lines to scroll.
392 bool ScrollList(const int dx
, const int dy
);
396 // fn is a function which takes 3 long arguments: item1, item2, data.
397 // item1 is the long data associated with a first item (NOT the index).
398 // item2 is the long data associated with a second item (NOT the index).
399 // data is the same value as passed to SortItems.
400 // The return value is a negative number if the first item should precede the second
401 // item, a positive number of the second item should precede the first,
402 // or zero if the two items are equivalent.
404 // data is arbitrary data to be passed to the sort function.
405 bool SortItems(wxListCtrlCompare fn
, long data
);
407 /* Why should we need this function? Leave for now.
408 * WE NEED IT because item data may have changed,
409 * but the display needs refreshing (in string callback mode)
410 // Updates an item. If the list control has the wxLI_AUTO_ARRANGE style,
411 // the items will be rearranged.
412 bool Update(const long item);
415 void Command(wxCommandEvent
& event
) { ProcessCommand(event
); };
418 bool MSWCommand(const WXUINT param
, const WXWORD id
);
419 bool MSWNotify(const WXWPARAM wParam
, const WXLPARAM lParam
);
421 // Recreate window - seems to be necessary when changing a style.
422 void RecreateWindow(void);
424 // Add to pool: necessary because Windows needs to have a string
425 // still exist across 3 callbacks.
426 char *AddPool(const wxString
& str
);
429 wxTextCtrl m_textCtrl
; // The control used for editing a label
430 wxImageList
* m_imageListNormal
; // The image list for normal icons
431 wxImageList
* m_imageListSmall
; // The image list for small icons
432 wxImageList
* m_imageListState
; // The image list state icons (not implemented yet)
434 long m_baseStyle
; // Basic Windows style flags, for recreation purposes
435 wxStringList m_stringPool
; // Pool of 3 strings to satisfy Windows callback
437 int m_colCount
; // Windows doesn't have GetColumnCount so must
438 // keep track of inserted/deleted columns
442 class WXDLLEXPORT wxListEvent
: public wxCommandEvent
444 DECLARE_DYNAMIC_CLASS(wxListEvent
)
447 wxListEvent(WXTYPE commandType
= 0, int id
= 0);
459 typedef void (wxEvtHandler::*wxListEventFunction
)(wxListEvent
&);
461 #define EVT_LIST_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_LIST_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxListEventFunction) & fn, NULL },
462 #define EVT_LIST_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_LIST_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxListEventFunction) & fn, NULL },
463 #define EVT_LIST_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxListEventFunction) & fn, NULL },
464 #define EVT_LIST_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_LIST_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxListEventFunction) & fn, NULL },
465 #define EVT_LIST_DELETE_ITEM(id, fn) { wxEVT_COMMAND_LIST_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxListEventFunction) & fn, NULL },
466 #define EVT_LIST_DELETE_ALL_ITEMS(id, fn) { wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxListEventFunction) & fn, NULL },
467 #define EVT_LIST_GET_INFO(id, fn) { wxEVT_COMMAND_LIST_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxListEventFunction) & fn, NULL },
468 #define EVT_LIST_SET_INFO(id, fn) { wxEVT_COMMAND_LIST_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxListEventFunction) & fn, NULL },
469 #define EVT_LIST_ITEM_SELECTED(id, fn) { wxEVT_COMMAND_LIST_ITEM_SELECTED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxListEventFunction) & fn, NULL },
470 #define EVT_LIST_ITEM_DESELECTED(id, fn) { wxEVT_COMMAND_LIST_ITEM_DESELECTED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxListEventFunction) & fn, NULL },
471 #define EVT_LIST_KEY_DOWN(id, fn) { wxEVT_COMMAND_LIST_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxListEventFunction) & fn, NULL },
472 #define EVT_LIST_INSERT_ITEM(id, fn) { wxEVT_COMMAND_LIST_INSERT_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxListEventFunction) & fn, NULL },
473 #define EVT_LIST_COL_CLICK(id, fn) { wxEVT_COMMAND_LIST_COL_CLICK, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxListEventFunction) & fn, NULL },