Version: 3.3.1
wxVListBox Class Referenceabstract

#include <wx/vlbox.h>

+ Inheritance diagram for wxVListBox:

Detailed Description

wxVListBox is a wxListBox-like control with the following two main differences from a regular wxListBox: it can have an arbitrarily huge number of items because it doesn't store them itself but uses the OnDrawItem() callback to draw them (so it is a virtual listbox) and its items can have variable height as determined by OnMeasureItem() (so it is also a listbox with the lines of variable height).

Also, as a consequence of its virtual nature, it doesn't have any methods to append or insert items in it as it isn't necessary to do it: you just have to call SetItemCount() to tell the control how many items it should display. Of course, this also means that you will never use this class directly because it has pure virtual functions, but will need to derive your own class from it (for example, wxHtmlListBox).

However it emits the same events as wxListBox and the same event macros may be used with it. Since wxVListBox does not store its items itself, the events will only contain the index, not any contents such as the string of an item.

Library:  wxCore
Category:  Controls
See also
wxSimpleHtmlListBox, wxHtmlListBox

Public Member Functions

 wxVListBox ()
 Default constructor, you must call Create() later. More...
 
 wxVListBox (wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0, const wxString &name=wxVListBoxNameStr)
 Normal constructor which calls Create() internally. More...
 
virtual ~wxVListBox ()
 Destructor. More...
 
void Clear ()
 Deletes all items from the control. More...
 
bool Create (wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0, const wxString &name=wxVListBoxNameStr)
 Creates the control. More...
 
bool DeselectAll ()
 Deselects all the items in the listbox. More...
 
int GetFirstSelected (unsigned long &cookie) const
 Returns the index of the first selected item in the listbox or wxNOT_FOUND if no items are currently selected. More...
 
size_t GetItemCount () const
 Get the number of items in the control. More...
 
wxPoint GetMargins () const
 Returns the margins used by the control. More...
 
wxRect GetItemRect (size_t item) const
 Returns the rectangle occupied by this item in physical coordinates. More...
 
int GetNextSelected (unsigned long &cookie) const
 Returns the index of the next selected item or wxNOT_FOUND if there are no more. More...
 
size_t GetSelectedCount () const
 Returns the number of the items currently selected. More...
 
int GetSelection () const
 Get the currently selected item or wxNOT_FOUND if there is no selection. More...
 
const wxColourGetSelectionBackground () const
 Returns the background colour used for the selected cells. More...
 
bool HasMultipleSelection () const
 Returns true if the listbox was created with wxLB_MULTIPLE style and so supports multiple selection or false if it is a single selection listbox. More...
 
bool IsCurrent (size_t item) const
 Returns true if this item is the current one, false otherwise. More...
 
int GetCurrent () const
 Get the current item or wxNOT_FOUND if there is no current item. More...
 
void SetCurrent (int current)
 Set the specified item as the current item, if it is wxNOT_FOUND the current item is unset. More...
 
bool IsSelected (size_t item) const
 Returns true if this item is selected, false otherwise. More...
 
bool Select (size_t item, bool select=true)
 Selects or deselects the specified item which must be valid (i.e. not equal to wxNOT_FOUND). More...
 
bool SelectAll ()
 Selects all the items in the listbox. More...
 
bool SelectRange (size_t from, size_t to)
 Selects all items in the specified range which may be given in any order. More...
 
virtual void SetItemCount (size_t count)
 Set the number of items to be shown in the control. More...
 
void SetSelection (int selection)
 Set the selection to the specified item, if it is -1 the selection is unset. More...
 
void SetSelectionBackground (const wxColour &col)
 Sets the colour to be used for the selected cells background. More...
 
void Toggle (size_t item)
 Toggles the state of the specified item, i.e. selects it if it was unselected and deselects it if it was selected. More...
 
void SetMargins (const wxPoint &pt)
 Set the margins: horizontal margin is the distance between the window border and the item contents while vertical margin is half of the distance between items. More...
 
void SetMargins (wxCoord x, wxCoord y)
 Set the margins: horizontal margin is the distance between the window border and the item contents while vertical margin is half of the distance between items. More...
 
- Public Member Functions inherited from wxVScrolled< T >
 wxVScrolled ()
 Default constructor, you must call Create() later. More...
 
 wxVScrolled (wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0, const wxString &name=wxPanelNameStr)
 This is the normal constructor, no need to call Create() after using this constructor. More...
 
bool Create (wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0, const wxString &name=wxPanelNameStr)
 Same as the non-default constructor, but returns a status code: true if ok, false if the window couldn't be created. More...
 
- Public Member Functions inherited from wxVarVScrollHelper
 wxVarVScrollHelper (wxWindow *winToScroll)
 Constructor taking the target window to be scrolled by this helper class. More...
 
size_t GetRowCount () const
 Returns the number of rows the target window contains. More...
 
size_t GetVisibleRowsBegin () const
 Returns the index of the first visible row based on the scroll position. More...
 
size_t GetVisibleRowsEnd () const
 Returns the index of the last visible row based on the scroll position. More...
 
bool IsRowVisible (size_t row) const
 Returns true if the given row is currently visible (even if only partially visible) or false otherwise. More...
 
virtual void RefreshRow (size_t row)
 Triggers a refresh for just the given row's area of the window if it's visible. More...
 
virtual void RefreshRows (size_t from, size_t to)
 Triggers a refresh for the area between the specified range of rows given (inclusively). More...
 
virtual bool ScrollRowPages (int pages)
 Scroll by the specified number of pages which may be positive (to scroll down) or negative (to scroll up). More...
 
virtual bool ScrollRows (int rows)
 Scroll by the specified number of rows which may be positive (to scroll down) or negative (to scroll up). More...
 
bool ScrollToRow (size_t row)
 Scroll to the specified row. More...
 
void SetRowCount (size_t rowCount)
 Set the number of rows the window contains. More...
 
- Public Member Functions inherited from wxVarScrollHelperBase
 wxVarScrollHelperBase (wxWindow *winToScroll)
 Constructor taking the target window to be scrolled by this helper class. More...
 
virtual ~wxVarScrollHelperBase ()
 Virtual destructor for detaching scroll event handlers attached with this helper class. More...
 
int CalcScrolledPosition (int coord) const
 Translates the logical coordinate given to the current device coordinate. More...
 
int CalcUnscrolledPosition (int coord) const
 Translates the device coordinate given to the corresponding logical coordinate. More...
 
void EnablePhysicalScrolling (bool scrolling=true)
 With physical scrolling on (when this is true), the device origin is changed properly when a wxPaintDC is prepared, children are actually moved and laid out properly, and the contents of the window (pixels) are actually moved. More...
 
virtual int GetNonOrientationTargetSize () const =0
 This function needs to be overridden in the in the derived class to return the window size with respect to the opposing orientation. More...
 
virtual wxOrientation GetOrientation () const =0
 This function need to be overridden to return the orientation that this helper is working with, either wxHORIZONTAL or wxVERTICAL. More...
 
virtual int GetOrientationTargetSize () const =0
 This function needs to be overridden in the in the derived class to return the window size with respect to the orientation this helper is working with. More...
 
virtual wxWindowGetTargetWindow () const
 This function will return the target window this helper class is currently scrolling. More...
 
size_t GetVisibleBegin () const
 Returns the index of the first visible unit based on the scroll position. More...
 
size_t GetVisibleEnd () const
 Returns the index of the last visible unit based on the scroll position. More...
 
bool IsVisible (size_t unit) const
 Returns true if the given scroll unit is currently visible (even if only partially visible) or false otherwise. More...
 
virtual void RefreshAll ()
 Recalculate all parameters and repaint all units. More...
 
virtual void SetTargetWindow (wxWindow *target)
 Normally the window will scroll itself, but in some rare occasions you might want it to scroll (part of) another window (e.g. More...
 
virtual void UpdateScrollbar ()
 Update the thumb size shown by the scrollbar. More...
 
int VirtualHitTest (wxCoord coord) const
 Returns the virtual scroll unit under the device unit given accounting for scroll position or wxNOT_FOUND if none (i.e. More...
 

Protected Member Functions

virtual void OnDrawItem (wxDC &dc, const wxRect &rect, size_t n) const =0
 The derived class must implement this function to actually draw the item with the given index on the provided DC. More...
 
virtual void OnDrawBackground (wxDC &dc, const wxRect &rect, size_t n) const
 This method is used to draw the item's background and, maybe, a border around it. More...
 
virtual void OnDrawSeparator (wxDC &dc, wxRect &rect, size_t n) const
 This method may be used to draw separators between the lines. More...
 
virtual wxCoord OnMeasureItem (size_t n) const =0
 The derived class must implement this method to return the height of the specified item (in pixels). More...
 
- Protected Member Functions inherited from wxVarVScrollHelper
virtual void OnGetRowsHeightHint (size_t rowMin, size_t rowMax) const
 This function doesn't have to be overridden but it may be useful to do so if calculating the rows' sizes is a relatively expensive operation as it gives your code a chance to calculate several of them at once and cache the result if necessary. More...
 
virtual wxCoord EstimateTotalHeight () const
 This class forwards calls from EstimateTotalSize() to this function so derived classes can override either just the height or the width estimation, or just estimate both differently if desired in any wxHVScrolledWindow derived class. More...
 
virtual wxCoord OnGetRowHeight (size_t row) const =0
 This function must be overridden in the derived class, and should return the height of the given row in pixels. More...
 
- Protected Member Functions inherited from wxVarScrollHelperBase
virtual void OnGetUnitsSizeHint (size_t unitMin, size_t unitMax) const
 This function doesn't have to be overridden but it may be useful to do so if calculating the units' sizes is a relatively expensive operation as it gives your code a chance to calculate several of them at once and cache the result if necessary. More...
 
virtual wxCoord EstimateTotalSize () const
 When the number of scroll units change, we try to estimate the total size of all units when the full window size is needed (i.e. More...
 
virtual wxCoord OnGetUnitSize (size_t unit) const =0
 This function must be overridden in the derived class, and should return the size of the given unit in pixels. More...
 

Constructor & Destructor Documentation

◆ wxVListBox() [1/2]

wxVListBox::wxVListBox ( )

Default constructor, you must call Create() later.

◆ wxVListBox() [2/2]

wxVListBox::wxVListBox ( wxWindow parent,
wxWindowID  id = wxID_ANY,
const wxPoint pos = wxDefaultPosition,
const wxSize size = wxDefaultSize,
long  style = 0,
const wxString name = wxVListBoxNameStr 
)

Normal constructor which calls Create() internally.

◆ ~wxVListBox()

virtual wxVListBox::~wxVListBox ( )
virtual

Destructor.

Member Function Documentation

◆ Clear()

void wxVListBox::Clear ( )

Deletes all items from the control.

◆ Create()

bool wxVListBox::Create ( wxWindow parent,
wxWindowID  id = wxID_ANY,
const wxPoint pos = wxDefaultPosition,
const wxSize size = wxDefaultSize,
long  style = 0,
const wxString name = wxVListBoxNameStr 
)

Creates the control.

To finish creating it you also should call SetItemCount() to let it know about the number of items it contains.

The only special style which may be used with wxVListBox is wxLB_MULTIPLE which indicates that the listbox should support multiple selection.

Returns
true on success or false if the control couldn't be created.

◆ DeselectAll()

bool wxVListBox::DeselectAll ( )

Deselects all the items in the listbox.

This method is only valid for multi selection listboxes.

Returns
true if any items were changed, i.e. if there had been any selected items before, or false if all the items were already deselected.
See also
SelectAll(), Select()

◆ GetCurrent()

int wxVListBox::GetCurrent ( ) const

Get the current item or wxNOT_FOUND if there is no current item.

Since
3.3.0

◆ GetFirstSelected()

int wxVListBox::GetFirstSelected ( unsigned long &  cookie) const

Returns the index of the first selected item in the listbox or wxNOT_FOUND if no items are currently selected.

cookie is an opaque parameter which should be passed to the subsequent calls to GetNextSelected(). It is needed in order to allow parallel iterations over the selected items.

Here is a typical example of using these functions:

unsigned long cookie;
int item = hlbox->GetFirstSelected(cookie);
while ( item != wxNOT_FOUND )
{
// ... process item ...
item = hlbox->GetNextSelected(cookie);
}
#define wxNOT_FOUND
Definition: defs.h:481

This method is only valid for multi selection listboxes.

◆ GetItemCount()

size_t wxVListBox::GetItemCount ( ) const

Get the number of items in the control.

See also
SetItemCount()

◆ GetItemRect()

wxRect wxVListBox::GetItemRect ( size_t  item) const

Returns the rectangle occupied by this item in physical coordinates.

If the item is not currently visible, returns an empty rectangle.

Since
2.9.0

◆ GetMargins()

wxPoint wxVListBox::GetMargins ( ) const

Returns the margins used by the control.

The x field of the returned point is the horizontal margin and the y field is the vertical one.

See also
SetMargins()

◆ GetNextSelected()

int wxVListBox::GetNextSelected ( unsigned long &  cookie) const

Returns the index of the next selected item or wxNOT_FOUND if there are no more.

This method is only valid for multi selection listboxes.

See also
GetFirstSelected()

◆ GetSelectedCount()

size_t wxVListBox::GetSelectedCount ( ) const

Returns the number of the items currently selected.

It is valid for both single and multi selection controls. In the former case it may only return 0 or 1 however.

See also
IsSelected(), GetFirstSelected(), GetNextSelected()

◆ GetSelection()

int wxVListBox::GetSelection ( ) const

Get the currently selected item or wxNOT_FOUND if there is no selection.

◆ GetSelectionBackground()

const wxColour& wxVListBox::GetSelectionBackground ( ) const

Returns the background colour used for the selected cells.

By default the standard system colour is used.

See also
wxSystemSettings::GetColour(), SetSelectionBackground()

◆ HasMultipleSelection()

bool wxVListBox::HasMultipleSelection ( ) const

Returns true if the listbox was created with wxLB_MULTIPLE style and so supports multiple selection or false if it is a single selection listbox.

◆ IsCurrent()

bool wxVListBox::IsCurrent ( size_t  item) const

Returns true if this item is the current one, false otherwise.

The current item is always the same as selected one for the single selection listbox and in this case this method is equivalent to IsSelected() but they are different for multi selection listboxes where many items may be selected but only one (at most) is current.

◆ IsSelected()

bool wxVListBox::IsSelected ( size_t  item) const

Returns true if this item is selected, false otherwise.

◆ OnDrawBackground()

virtual void wxVListBox::OnDrawBackground ( wxDC dc,
const wxRect rect,
size_t  n 
) const
protectedvirtual

This method is used to draw the item's background and, maybe, a border around it.

The base class version implements a reasonable default behaviour which consists in drawing the selected item with the standard background colour and drawing a border around the item if it is either selected or current.

Todo:
Change this function signature to non-const.

◆ OnDrawItem()

virtual void wxVListBox::OnDrawItem ( wxDC dc,
const wxRect rect,
size_t  n 
) const
protectedpure virtual

The derived class must implement this function to actually draw the item with the given index on the provided DC.

Parameters
dcThe device context to use for drawing.
rectThe bounding rectangle for the item being drawn (DC clipping region is set to this rectangle before calling this function).
nThe index of the item to be drawn.
Todo:
Change this function signature to non-const.

◆ OnDrawSeparator()

virtual void wxVListBox::OnDrawSeparator ( wxDC dc,
wxRect rect,
size_t  n 
) const
protectedvirtual

This method may be used to draw separators between the lines.

The rectangle passed to it may be modified, typically to deflate it a bit before passing to OnDrawItem().

The base class version of this method doesn't do anything.

Parameters
dcThe device context to use for drawing.
rectThe bounding rectangle for the item.
nThe index of the item.
Todo:
Change this function signature to non-const.

◆ OnMeasureItem()

virtual wxCoord wxVListBox::OnMeasureItem ( size_t  n) const
protectedpure virtual

The derived class must implement this method to return the height of the specified item (in pixels).

◆ Select()

bool wxVListBox::Select ( size_t  item,
bool  select = true 
)

Selects or deselects the specified item which must be valid (i.e. not equal to wxNOT_FOUND).

Returns
true if the items selection status has changed or false otherwise.

This function is only valid for the multiple selection listboxes, use SetSelection() for the single selection ones.

◆ SelectAll()

bool wxVListBox::SelectAll ( )

Selects all the items in the listbox.

Returns
true if any items were changed, i.e. if there had been any unselected items before, or false if all the items were already selected.

This method is only valid for multi selection listboxes.

See also
DeselectAll(), Select()

◆ SelectRange()

bool wxVListBox::SelectRange ( size_t  from,
size_t  to 
)

Selects all items in the specified range which may be given in any order.

Returns
true if the items selection status has changed or false otherwise.

This method is only valid for multi selection listboxes.

See also
SelectAll(), Select()

◆ SetCurrent()

void wxVListBox::SetCurrent ( int  current)

Set the specified item as the current item, if it is wxNOT_FOUND the current item is unset.

The current item will be automatically scrolled into view if it isn't currently visible.

Since
3.3.0

◆ SetItemCount()

virtual void wxVListBox::SetItemCount ( size_t  count)
virtual

Set the number of items to be shown in the control.

This is just a synonym for wxVScrolledWindow::SetRowCount().

◆ SetMargins() [1/2]

void wxVListBox::SetMargins ( const wxPoint pt)

Set the margins: horizontal margin is the distance between the window border and the item contents while vertical margin is half of the distance between items.

By default both margins are 0.

◆ SetMargins() [2/2]

void wxVListBox::SetMargins ( wxCoord  x,
wxCoord  y 
)

Set the margins: horizontal margin is the distance between the window border and the item contents while vertical margin is half of the distance between items.

By default both margins are 0.

◆ SetSelection()

void wxVListBox::SetSelection ( int  selection)

Set the selection to the specified item, if it is -1 the selection is unset.

The selected item will be automatically scrolled into view if it isn't currently visible.

This method may be used both with single and multiple selection listboxes.

◆ SetSelectionBackground()

void wxVListBox::SetSelectionBackground ( const wxColour col)

Sets the colour to be used for the selected cells background.

The background of the standard cells may be changed by simply calling wxWindow::SetBackgroundColour().

Note
Using a non-default background colour may result in control having an appearance different from the similar native controls and should be avoided in general.
See also
GetSelectionBackground()

◆ Toggle()

void wxVListBox::Toggle ( size_t  item)

Toggles the state of the specified item, i.e. selects it if it was unselected and deselects it if it was selected.

This method is only valid for multi selection listboxes.

See also
Select()