1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxVListBox is a virtual listbox with lines of variable height
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
15 #include "wx/vscroll.h" // base class
17 class WXDLLEXPORT wxSelectionStore
;
19 #define wxVListBoxNameStr _T("wxVListBox")
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
26 This class has two main differences from a regular listbox: it can have an
27 arbitrarily huge number of items because it doesn't store them itself but
28 uses OnDrawItem() callback to draw them and its items can have variable
29 height as determined by OnMeasureItem().
31 It emits the same events as wxListBox and the same event macros may be used
34 class WXDLLEXPORT wxVListBox
: public wxVScrolledWindow
37 // constructors and such
38 // ---------------------
40 // default constructor, you must call Create() later
41 wxVListBox() { Init(); }
43 // normal constructor which calls Create() internally
44 wxVListBox(wxWindow
*parent
,
45 wxWindowID id
= wxID_ANY
,
46 const wxPoint
& pos
= wxDefaultPosition
,
47 const wxSize
& size
= wxDefaultSize
,
49 const wxString
& name
= wxVListBoxNameStr
)
53 (void)Create(parent
, id
, pos
, size
, style
, name
);
56 // really creates the control and sets the initial number of items in it
57 // (which may be changed later with SetItemCount())
59 // the only special style which may be specified here is wxLB_MULTIPLE
61 // returns true on success or false if the control couldn't be created
62 bool Create(wxWindow
*parent
,
63 wxWindowID id
= wxID_ANY
,
64 const wxPoint
& pos
= wxDefaultPosition
,
65 const wxSize
& size
= wxDefaultSize
,
67 const wxString
& name
= wxVListBoxNameStr
);
69 // dtor does some internal cleanup (deletes m_selStore if any)
70 virtual ~wxVListBox();
76 // get the number of items in the control
77 size_t GetItemCount() const { return GetLineCount(); }
79 // does this control use multiple selection?
80 bool HasMultipleSelection() const { return m_selStore
!= NULL
; }
82 // get the currently selected item or wxNOT_FOUND if there is no selection
84 // this method is only valid for the single selection listboxes
85 int GetSelection() const
87 wxASSERT_MSG( !HasMultipleSelection(),
88 _T("GetSelection() can't be used with wxLB_MULTIPLE") );
93 // is this item the current one?
94 bool IsCurrent(size_t item
) const { return item
== (size_t)m_current
; }
96 // is this item selected?
97 bool IsSelected(size_t item
) const;
99 // get the number of the selected items (maybe 0)
101 // this method is valid for both single and multi selection listboxes
102 size_t GetSelectedCount() const;
104 // get the first selected item, returns wxNOT_FOUND if none
106 // cookie is an opaque parameter which should be passed to
107 // GetNextSelected() later
109 // this method is only valid for the multi selection listboxes
110 int GetFirstSelected(unsigned long& cookie
) const;
112 // get next selection item, return wxNOT_FOUND if no more
114 // cookie must be the same parameter that was passed to GetFirstSelected()
117 // this method is only valid for the multi selection listboxes
118 int GetNextSelected(unsigned long& cookie
) const;
120 // get the margins around each item
121 wxPoint
GetMargins() const { return m_ptMargins
; }
123 // get the background colour of selected cells
124 const wxColour
& GetSelectionBackground() const { return m_colBgSel
; }
130 // set the number of items to be shown in the control
132 // this is just a synonym for wxVScrolledWindow::SetLineCount()
133 virtual void SetItemCount(size_t count
);
135 // delete all items from the control
136 void Clear() { SetItemCount(0); }
138 // set the selection to the specified item, if it is wxNOT_FOUND the
139 // selection is unset
141 // this function is only valid for the single selection listboxes
142 void SetSelection(int selection
);
144 // selects or deselects the specified item which must be valid (i.e. not
145 // equal to wxNOT_FOUND)
147 // return true if the items selection status has changed or false
150 // this function is only valid for the multiple selection listboxes
151 bool Select(size_t item
, bool select
= true);
153 // selects the items in the specified range whose end points may be given
156 // return true if any items selection status has changed, false otherwise
158 // this function is only valid for the single selection listboxes
159 bool SelectRange(size_t from
, size_t to
);
161 // toggle the selection of the specified item (must be valid)
163 // this function is only valid for the multiple selection listboxes
164 void Toggle(size_t item
) { Select(item
, !IsSelected(item
)); }
166 // select all items in the listbox
168 // the return code indicates if any items were affected by this operation
169 // (true) or if nothing has changed (false)
170 bool SelectAll() { return DoSelectAll(true); }
172 // unselect all items in the listbox
174 // the return code has the same meaning as for SelectAll()
175 bool DeselectAll() { return DoSelectAll(false); }
177 // set the margins: horizontal margin is the distance between the window
178 // border and the item contents while vertical margin is half of the
179 // distance between items
181 // by default both margins are 0
182 void SetMargins(const wxPoint
& pt
);
183 void SetMargins(wxCoord x
, wxCoord y
) { SetMargins(wxPoint(x
, y
)); }
185 // change the background colour of the selected cells
186 void SetSelectionBackground(const wxColour
& col
);
190 // the derived class must implement this function to actually draw the item
191 // with the given index on the provided DC
192 virtual void OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const = 0;
194 // the derived class must implement this method to return the height of the
196 virtual wxCoord
OnMeasureItem(size_t n
) const = 0;
198 // this method may be used to draw separators between the lines; note that
199 // the rectangle may be modified, typically to deflate it a bit before
200 // passing to OnDrawItem()
202 // the base class version doesn't do anything
203 virtual void OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t n
) const;
206 // we implement OnGetLineHeight() in terms of OnMeasureItem() because this
207 // allows us to add borders to the items easily
209 // this function is not upposed to be overridden by the derived classes
210 virtual wxCoord
OnGetLineHeight(size_t line
) const;
214 void OnPaint(wxPaintEvent
& event
);
215 void OnKeyDown(wxKeyEvent
& event
);
216 void OnLeftDown(wxMouseEvent
& event
);
217 void OnLeftDClick(wxMouseEvent
& event
);
220 // common part of all ctors
223 // send the wxEVT_COMMAND_LISTBOX_SELECTED event
224 void SendSelectedEvent();
226 // common implementation of SelectAll() and DeselectAll()
227 bool DoSelectAll(bool select
);
229 // change the current item (in single selection listbox it also implicitly
230 // changes the selection); current may be wxNOT_FOUND in which case there
231 // will be no current item any more
233 // return true if the current item changed, false otherwise
234 bool DoSetCurrent(int current
);
236 // flags for DoHandleItemClick
239 ItemClick_Shift
= 1, // item shift-clicked
240 ItemClick_Ctrl
= 2, // ctrl
241 ItemClick_Kbd
= 4 // item selected from keyboard
244 // common part of keyboard and mouse handling processing code
245 void DoHandleItemClick(int item
, int flags
);
248 // the current item or wxNOT_FOUND
250 // if m_selStore == NULL this is also the selected item, otherwise the
251 // selections are managed by m_selStore
254 // the anchor of the selection for the multiselection listboxes:
255 // shift-clicking an item extends the selection from m_anchor to the item
256 // clicked, for example
258 // always wxNOT_FOUND for single selection listboxes
261 // the object managing our selected items if not NULL
262 wxSelectionStore
*m_selStore
;
267 // the selection bg colour
271 DECLARE_EVENT_TABLE()
274 #endif // _WX_VLBOX_H_