wxListCtrlA list control presents lists in a number of formats: list view, report view, icon view and small icon view. In any case, elements are numbered from zero. For all these modes, the items are stored in the control and must be added to it using InsertItem method. A special case of report view quite different from the other modes of the list control is a virtual control in which the items data (including text, images and attributes) is managed by the main program and is requested by the control itself only when needed which allows to have controls with millions of items without consuming much memory. To use virtual list control you must use SetItemCount first and overload at least OnGetItemText (and optionally OnGetItemImage or OnGetItemColumnImage and OnGetItemAttr) to return the information about the items when the control requests it. Virtual list control can be used as a normal one except that no operations which can take time proportional to the number of items in the control happen -- this is required to allow having a practically infinite number of items. For example, in a multiple selection virtual list control, the selections won't be sent when many items are selected at once because this could mean iterating over all the items. Using many of wxListCtrl features is shown in the corresponding sample. To intercept events from a list control, use the event table macros described in wxListEvent. Mac Note: Starting with 2.8, wxListCtrl uses a native implementation for report mode, and uses a generic implementation for other modes. You can use the generic implementation for report mode as well by setting the mac.listctrl.always_use_generic wxSystemOption to 1. Derived from
wxControl Include files <wx/listctrl.h> Window styles
See also window styles overview. Event handling To process input from a list control, use these event handler macros to direct input to member functions that take a wxListEvent argument.
wxListCtrl overview, wxListView, wxListBox, wxTreeCtrl, wxImageList, wxListEvent, wxListItem Members
wxListCtrl::wxListCtrl
wxListCtrl::wxListCtrlwxListCtrl() Default constructor. wxListCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxLC_ICON, const wxValidator& validator = wxDefaultValidator, const wxString& name = wxListCtrlNameStr) Constructor, creating and showing a list control. Parameters parent
id
pos
size
style
validator
name
See also wxListCtrl::Create, wxValidator
wxListCtrl::~wxListCtrlvoid ~wxListCtrl() Destructor, destroying the list control.
wxListCtrl::Arrangebool Arrange(int flag = wxLIST_ALIGN_DEFAULT) Arranges the items in icon or small icon view. This only has effect on Win32. flag is one of:
wxListCtrl::AssignImageListvoid AssignImageList(wxImageList* imageList, int which) Sets the image list associated with the control and takes ownership of it (i.e. the control will, unlike when using SetImageList, delete the list when destroyed). which is one of wxIMAGE_LIST_NORMAL, wxIMAGE_LIST_SMALL, wxIMAGE_LIST_STATE (the last is unimplemented). See also
wxListCtrl::ClearAllvoid ClearAll() Deletes all items and all columns.
wxListCtrl::Createbool Create(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxLC_ICON, const wxValidator& validator = wxDefaultValidator, const wxString& name = wxListCtrlNameStr) Creates the list control. See wxListCtrl::wxListCtrl for further details.
wxListCtrl::DeleteAllItemsbool DeleteAllItems() Deletes all items in the list control. NB: This function does not send the wxEVT_COMMAND_LIST_DELETE_ITEM event because deleting many items from the control would be too slow then (unlike DeleteItem).
wxListCtrl::DeleteColumnbool DeleteColumn(int col) Deletes a column.
wxListCtrl::DeleteItembool DeleteItem(long item) Deletes the specified item. This function sends the wxEVT_COMMAND_LIST_DELETE_ITEM event for the item being deleted. See also: DeleteAllItems
wxListCtrl::EditLabelvoid EditLabel(long item) Starts editing the label of the given item. This function generates a EVT_LIST_BEGIN_LABEL_EDIT event which can be vetoed so that no text control will appear for in-place editing. If the user changed the label (i.e. s/he does not press ESC or leave the text control without changes, a EVT_LIST_END_LABEL_EDIT event will be sent which can be vetoed as well.
wxListCtrl::EnsureVisiblebool EnsureVisible(long item) Ensures this item is visible.
wxListCtrl::FindItemlong FindItem(long start, const wxString& str, const bool partial = false) Find an item whose label matches this string, starting from start or the beginning if start is -1. The string comparison is case insensitive. If partial is true then this method will look for items which begin with str. long FindItem(long start, long data) Find an item whose data matches this data, starting from start or the beginning if 'start' is -1. long FindItem(long start, const wxPoint& pt, int direction) Find an item nearest this position in the specified direction, starting from start or the beginning if start is -1. wxPython note: In place of a single overloaded method name, wxPython implements the following methods:
wxPerl note: In wxPerl there are three methods instead of a single overloaded method:
wxListCtrl::GetColumnbool GetColumn(int col, wxListItem& item) const Gets information about this column. See wxListCtrl::SetItem for more information. wxPerl note: In wxPerl this method takes only the col parameter and returns a Wx::ListItem ( or undef ).
wxListCtrl::GetColumnCountint GetColumnCount() const Returns the number of columns.
wxListCtrl::GetColumnWidthint GetColumnWidth(int col) const Gets the column width (report view only).
wxListCtrl::GetCountPerPageint GetCountPerPage() const Gets the number of items that can fit vertically in the visible area of the list control (list or report view) or the total number of items in the list control (icon or small icon view).
wxListCtrl::GetEditControlwxTextCtrl * GetEditControl() const Returns the edit control being currently used to edit a label. Returns NULL if no label is being edited. NB: It is currently only implemented for wxMSW and the generic version, not for the native Mac OS X version.
wxListCtrl::GetImageListwxImageList* GetImageList(int which) const Returns the specified image list. which may be one of:
wxListCtrl::GetItembool GetItem(wxListItem& info) const Gets information about the item. See wxListCtrl::SetItem for more information. You must call info.SetId() to the ID of item you're interested in before calling this method. wxPython note: The wxPython version of this method takes an integer parameter for the item ID, an optional integer for the column number, and returns the wxListItem object. wxPerl note: In wxPerl this method takes as parameter the ID of the item and ( optionally ) the column, and returns a Wx::ListItem object.
wxListCtrl::GetItemBackgroundColourwxColour GetItemBackgroundColour(long item) const Returns the colour for this item. If the item has no specific colour, returns an invalid colour (and not the default background control of the control itself). See also
wxListCtrl::GetItemCountint GetItemCount() const Returns the number of items in the list control.
wxListCtrl::GetItemDatalong GetItemData(long item) const Gets the application-defined data associated with this item.
wxListCtrl::GetItemFontwxFont GetItemFont(long item) const Returns the item's font.
wxListCtrl::GetItemPositionbool GetItemPosition(long item, wxPoint& pos) const Returns the position of the item, in icon or small icon view. wxPython note: The wxPython version of this method accepts only the item ID and returns the wxPoint. wxPerl note: In wxPerl this method takes only the item parameter and returns a Wx::Point ( or undef ).
wxListCtrl::GetItemRectbool GetItemRect(long item, wxRect& rect, int code = wxLIST_RECT_BOUNDS) const Returns the rectangle representing the item's size and position, in physical coordinates. code is one of wxLIST_RECT_BOUNDS, wxLIST_RECT_ICON, wxLIST_RECT_LABEL. wxPython note: The wxPython version of this method accepts only the item ID and code and returns the wxRect. wxPerl note: In wxPerl this method takes only the item parameter and returns a Wx::Rect ( or undef ).
wxListCtrl::GetSubItemRectbool GetSubItemRect(long item, long subItem, wxRect& rect, int code = wxLIST_RECT_BOUNDS) const Returns the rectangle representing the size and position, in physical coordinates, of the given subitem, i.e. the part of the row item in the column subItem. This method is only meaningfull when the wxListCtrl is in the report mode. If subItem parameter is equal to the special value wxLIST_GETSUBITEMRECT_WHOLEITEM the return value is the same as for GetItemRect. code can be one of wxLIST_RECT_BOUNDS, wxLIST_RECT_ICON or wxLIST_RECT_LABEL. This function is new since wxWidgets version 2.7.0
wxListCtrl::GetItemSpacingwxSize GetItemSpacing() const Retrieves the spacing between icons in pixels: horizontal spacing is returned as x component of the wxSize object and the vertical spacing as its y component.
wxListCtrl::GetItemStateint GetItemState(long item, long stateMask) const Gets the item state. For a list of state flags, see wxListCtrl::SetItem. The stateMask indicates which state flags are of interest.
wxListCtrl::GetItemTextwxString GetItemText(long item) const Gets the item text for this item.
wxListCtrl::GetItemTextColourwxColour GetItemTextColour(long item) const Returns the colour for this item. If the item has no specific colour, returns an invalid colour (and not the default foreground control of the control itself as this wouldn't allow distinguishing between items having the same colour as the current control foreground and items with default colour which, hence, have always the same colour as the control).
wxListCtrl::GetNextItemlong GetNextItem(long item, int geometry = wxLIST_NEXT_ALL, int state = wxLIST_STATE_DONTCARE) const Searches for an item with the given geometry or state, starting from item but excluding the item itself. If item is -1, the first item that matches the specified flags will be returned. Returns the first item with given state following item or -1 if no such item found. This function may be used to find all selected items in the control like this:
long item = -1; for ( ;; ) { item = listctrl->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); if ( item == -1 ) break; // this item is selected - do whatever is needed with it wxLogMessage("Item %ld is selected.", item); }geometry can be one of:
NB: this parameter is only supported by wxMSW currently and ignored on other platforms. state can be a bitlist of the following:
wxListCtrl::GetSelectedItemCountint GetSelectedItemCount() const Returns the number of selected items in the list control.
wxListCtrl::GetTextColourwxColour GetTextColour() const Gets the text colour of the list control.
wxListCtrl::GetTopItemlong GetTopItem() const Gets the index of the topmost visible item when in list or report view.
wxListCtrl::GetViewRectwxRect GetViewRect() const Returns the rectangle taken by all items in the control. In other words, if the controls client size were equal to the size of this rectangle, no scrollbars would be needed and no free space would be left. Note that this function only works in the icon and small icon views, not in list or report views (this is a limitation of the native Win32 control).
wxListCtrl::HitTestlong HitTest(const wxPoint& point, int& flags, long *ptrSubItem) const Determines which item (if any) is at the specified point, giving details in flags. Returns index of the item or wxNOT_FOUND if no item is at the specified point. flags will be a combination of the following flags:
If ptrSubItem is not NULL and the wxListCtrl is in the report mode the subitem (or column) number will also be provided. This feature is only available in version 2.7.0 or higher and is currently only implemented under wxMSW and requires at least comctl32.dll of verion 4.70 on the host system or the value stored in ptrSubItem will be always -1. To compile this feature into wxWidgets library you need to have access to commctrl.h of version 4.70 that is provided by Microsoft. wxPython note: A tuple of values is returned in the wxPython version of this method. The first value is the item id and the second is the flags value mentioned above. wxPerl note: In wxPerl this method only takes the point parameter and returns a 2-element list ( item, flags ).
wxListCtrl::InsertColumnlong InsertColumn(long col, wxListItem& info) long InsertColumn(long col, const wxString& heading, int format = wxLIST_FORMAT_LEFT, int width = -1) For report view mode (only), inserts a column. For more details, see wxListCtrl::SetItem. wxPython note: In place of a single overloaded method name, wxPython implements the following methods:
wxListCtrl::InsertItemlong InsertItem(wxListItem& info) Inserts an item, returning the index of the new item if successful, -1 otherwise. long InsertItem(long index, const wxString& label) Inserts a string item. long InsertItem(long index, int imageIndex) Inserts an image item. long InsertItem(long index, const wxString& label, int imageIndex) Insert an image/string item. Parameters info
index
label
imageIndex
wxPython note: In place of a single overloaded method name, wxPython implements the following methods:
wxPerl note: In wxPerl there are four methods instead of a single overloaded method:
wxListCtrl::OnGetItemAttrvirtual wxListItemAttr * OnGetItemAttr(long item) const This function may be overloaded in the derived class for a control with wxLC_VIRTUAL style. It should return the attribute for the for the specified item or NULL to use the default appearance parameters. wxListCtrl will not delete the pointer or keep a reference of it. You can return the same wxListItemAttr pointer for every OnGetItemAttr call. The base class version always returns NULL. See also
OnGetItemImage,
wxListCtrl::OnGetItemImagevirtual int OnGetItemImage(long item) const This function must be overloaded in the derived class for a control with wxLC_VIRTUAL style having an image list (if the control doesn't have an image list, it is not necessary to overload it). It should return the index of the items image in the controls image list or -1 for no image. In a control with wxLC_REPORT style, OnGetItemImage only gets called for the first column of each line. The base class version always returns -1. See also
OnGetItemText,
wxListCtrl::OnGetItemColumnImagevirtual int OnGetItemColumnImage(long item, long column) const Overload this function in the derived class for a control with wxLC_VIRTUAL and wxLC_REPORT styles in order to specify the image index for the given line and column. The base class version always calls OnGetItemImage for the first column, else it returns -1. See also
OnGetItemText,
wxListCtrl::OnGetItemTextvirtual wxString OnGetItemText(long item, long column) const This function must be overloaded in the derived class for a control with wxLC_VIRTUAL style. It should return the string containing the text of the given column for the specified item. See also
SetItemCount,
wxListCtrl::RefreshItemvoid RefreshItem(long item) Redraws the given item. This is only useful for the virtual list controls as without calling this function the displayed value of the item doesn't change even when the underlying data does change. See also
wxListCtrl::RefreshItemsvoid RefreshItems(long itemFrom, long itemTo) Redraws the items between itemFrom and itemTo. The starting item must be less than or equal to the ending one. Just as RefreshItem this is only useful for virtual list controls.
wxListCtrl::ScrollListbool ScrollList(int dx, int dy) Scrolls the list control. If in icon, small icon or report view mode, dx specifies the number of pixels to scroll. If in list view mode, dx specifies the number of columns to scroll. dy always specifies the number of pixels to scroll vertically. NB: This method is currently only implemented in the Windows version.
wxListCtrl::SetBackgroundColourvoid SetBackgroundColour(const wxColour& col) Sets the background colour (GetBackgroundColour already implicit in wxWindow class).
wxListCtrl::SetColumnbool SetColumn(int col, wxListItem& item) Sets information about this column. See wxListCtrl::SetItem for more information.
wxListCtrl::SetColumnWidthbool SetColumnWidth(int col, int width) Sets the column width. width can be a width in pixels or wxLIST_AUTOSIZE (-1) or wxLIST_AUTOSIZE_USEHEADER (-2). wxLIST_AUTOSIZE will resize the column to the length of its longest item. wxLIST_AUTOSIZE_USEHEADER will resize the column to the length of the header (Win32) or 80 pixels (other platforms). In small or normal icon view, col must be -1, and the column width is set for all columns.
wxListCtrl::SetImageListvoid SetImageList(wxImageList* imageList, int which) Sets the image list associated with the control. which is one of wxIMAGE_LIST_NORMAL, wxIMAGE_LIST_SMALL, wxIMAGE_LIST_STATE (the last is unimplemented). This method does not take ownership of the image list, you have to delete it yourself. See also
wxListCtrl::SetItembool SetItem(wxListItem& info) long SetItem(long index, int col, const wxString& label, int imageId = -1) Sets information about the item. wxListItem is a class with the following members:
The m_mask member contains a bitlist specifying which of the other fields are valid. The flags are:
The m_stateMask and m_state members take flags from the following:
The wxListItem object can also contain item-specific colour and font information: for this you need to call one of SetTextColour(), SetBackgroundColour() or SetFont() functions on it passing it the colour/font to use. If the colour/font is not specified, the default list control colour/font is used. long SetItem(long index, int col, const wxString& label, int imageId = -1) Sets a string field at a particular column. wxPython note: In place of a single overloaded method name, wxPython implements the following methods:
wxListCtrl::SetItemBackgroundColourvoid SetItemBackgroundColour(long item, const wxColour& col) Sets the background colour for this item. This function only works in report view. The colour can be retrieved using GetItemBackgroundColour.
wxListCtrl::SetItemCountvoid SetItemCount(long count) This method can only be used with virtual list controls. It is used to indicate to the control the number of items it contains. After calling it, the main program should be ready to handle calls to various item callbacks (such as OnGetItemText) for all items in the range from 0 to count.
wxListCtrl::SetItemDatabool SetItemData(long item, long data) Associates application-defined data with this item. Notice that this function cannot be used to associate pointers with the control items, use SetItemPtrData instead.
wxListCtrl::SetItemFontvoid SetItemFont(long item, const wxFont& font) Sets the item's font.
wxListCtrl::SetItemImagebool SetItemImage(long item, int image) Sets the image associated with the item. The image is an index into the image list associated with the list control. In report view, this only sets the image for the first column. bool SetItemImage(long item, int image, int selImage) Sets the unselected and selected images associated with the item. The images are indices into the image list associated with the list control. This form is deprecated: selImage is not used.
wxListCtrl::SetItemColumnImagebool SetItemImage(long item, long columnint image) Sets the image associated with the item. In report view, you can specify the column. The image is an index into the image list associated with the list control.
wxListCtrl::SetItemPositionbool SetItemPosition(long item, const wxPoint& pos) Sets the position of the item, in icon or small icon view. Windows only.
wxListCtrl::SetItemPtrDatabool SetItemPtrData(long item, wxUIntPtr data) Associates application-defined data with this item. The data parameter may be either an integer or a pointer cast to the wxUIntPtr type which is guaranteed to be large enough to be able to contain all integer types and pointers. This function is new since wxWidgets version 2.8.4
wxListCtrl::SetItemStatebool SetItemState(long item, long state, long stateMask) Sets the item state. For a list of state flags, see wxListCtrl::SetItem. The stateMask indicates which state flags are valid.
wxListCtrl::SetItemTextvoid SetItemText(long item, const wxString& text) Sets the item text for this item.
wxListCtrl::SetItemTextColourvoid SetItemTextColour(long item, const wxColour& col) Sets the colour for this item. This function only works in report view. The colour can be retrieved using GetItemTextColour.
wxListCtrl::SetSingleStylevoid SetSingleStyle(long style, const bool add = true) Adds or removes a single window style.
wxListCtrl::SetTextColourvoid SetTextColour(const wxColour& col) Sets the text colour of the list control.
wxListCtrl::SetWindowStyleFlagvoid SetWindowStyleFlag(long style) Sets the whole window style, deleting all items.
wxListCtrl::SortItemsbool SortItems(wxListCtrlCompare fnSortCallBack, long data) Call this function to sort the items in the list control. Sorting is done using the specified fnSortCallBack function. This function must have the following prototype:
int wxCALLBACK wxListCompareFunction(long item1, long item2, long sortData)It is called each time when the two items must be compared and should return 0 if the items are equal, negative value if the first item is less than the second one and positive value if the first one is greater than the second one (the same convention as used by qsort(3)). Parameters item1
item2
data
Notice that the control may only be sorted on client data associated with the items, so you must use SetItemData if you want to be able to sort the items in the control. Please see the listctrl sample for an example of using this function. wxPython note: wxPython uses the sortData parameter to pass the Python function to call, so it is not available for programmer use. Call SortItems with a reference to a callable object that expects two parameters. wxPerl note: In wxPerl the comparison function must take just two parameters; however, you may use a closure to achieve an effect similar to the SortItems third parameter.
|