1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/univ/listbox.h
3 // Purpose: the universal listbox
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_UNIV_LISTBOX_H_
12 #define _WX_UNIV_LISTBOX_H_
14 #include "wx/scrolwin.h" // for wxScrollHelper
15 #include "wx/dynarray.h"
16 #include "wx/arrstr.h"
18 // ----------------------------------------------------------------------------
19 // the actions supported by this control
20 // ----------------------------------------------------------------------------
22 // change the current item
23 #define wxACTION_LISTBOX_SETFOCUS wxT("setfocus") // select the item
24 #define wxACTION_LISTBOX_MOVEDOWN wxT("down") // select item below
25 #define wxACTION_LISTBOX_MOVEUP wxT("up") // select item above
26 #define wxACTION_LISTBOX_PAGEDOWN wxT("pagedown") // go page down
27 #define wxACTION_LISTBOX_PAGEUP wxT("pageup") // go page up
28 #define wxACTION_LISTBOX_START wxT("start") // go to first item
29 #define wxACTION_LISTBOX_END wxT("end") // go to last item
30 #define wxACTION_LISTBOX_FIND wxT("find") // find item by 1st letter
32 // do something with the current item
33 #define wxACTION_LISTBOX_ACTIVATE wxT("activate") // activate (choose)
34 #define wxACTION_LISTBOX_TOGGLE wxT("toggle") // togglee selected state
35 #define wxACTION_LISTBOX_SELECT wxT("select") // sel this, unsel others
36 #define wxACTION_LISTBOX_SELECTADD wxT("selectadd") // add to selection
37 #define wxACTION_LISTBOX_UNSELECT wxT("unselect") // unselect
38 #define wxACTION_LISTBOX_ANCHOR wxT("selanchor") // anchor selection
40 // do something with the selection globally (not for single selection ones)
41 #define wxACTION_LISTBOX_SELECTALL wxT("selectall") // select all items
42 #define wxACTION_LISTBOX_UNSELECTALL wxT("unselectall") // unselect all items
43 #define wxACTION_LISTBOX_SELTOGGLE wxT("togglesel") // invert the selection
44 #define wxACTION_LISTBOX_EXTENDSEL wxT("extend") // extend to item
46 // ----------------------------------------------------------------------------
47 // wxListBox: a list of selectable items
48 // ----------------------------------------------------------------------------
50 class WXDLLIMPEXP_CORE wxListBox
: public wxListBoxBase
, public wxScrollHelper
54 wxListBox() : wxScrollHelper(this) { Init(); }
55 wxListBox(wxWindow
*parent
,
57 const wxPoint
& pos
= wxDefaultPosition
,
58 const wxSize
& size
= wxDefaultSize
,
59 int n
= 0, const wxString choices
[] = (const wxString
*) NULL
,
61 const wxValidator
& validator
= wxDefaultValidator
,
62 const wxString
& name
= wxListBoxNameStr
)
63 : wxScrollHelper(this)
67 Create(parent
, id
, pos
, size
, n
, choices
, style
, validator
, name
);
69 wxListBox(wxWindow
*parent
,
73 const wxArrayString
& choices
,
75 const wxValidator
& validator
= wxDefaultValidator
,
76 const wxString
& name
= wxListBoxNameStr
);
80 bool Create(wxWindow
*parent
,
82 const wxPoint
& pos
= wxDefaultPosition
,
83 const wxSize
& size
= wxDefaultSize
,
84 int n
= 0, const wxString choices
[] = (const wxString
*) NULL
,
86 const wxValidator
& validator
= wxDefaultValidator
,
87 const wxString
& name
= wxListBoxNameStr
);
88 bool Create(wxWindow
*parent
,
92 const wxArrayString
& choices
,
94 const wxValidator
& validator
= wxDefaultValidator
,
95 const wxString
& name
= wxListBoxNameStr
);
97 // implement the listbox interface defined by wxListBoxBase
98 virtual void DoClear();
99 virtual void DoDeleteOneItem(unsigned int n
);
101 virtual unsigned int GetCount() const;
102 virtual wxString
GetString(unsigned int n
) const;
103 virtual void SetString(unsigned int n
, const wxString
& s
);
104 virtual int FindString(const wxString
& s
, bool bCase
= false) const;
106 virtual bool IsSelected(int n
) const
107 { return m_selections
.Index(n
) != wxNOT_FOUND
; }
108 virtual int GetSelection() const;
109 virtual int GetSelections(wxArrayInt
& aSelections
) const;
112 virtual void DoSetSelection(int n
, bool select
);
114 virtual int DoInsertItems(const wxArrayStringsAdapter
& items
,
117 wxClientDataType type
);
119 // universal wxComboBox implementation internally uses wxListBox
120 friend class WXDLLIMPEXP_FWD_CORE wxComboBox
;
122 virtual void DoSetFirstItem(int n
);
124 virtual void DoSetItemClientData(unsigned int n
, void* clientData
);
125 virtual void* DoGetItemClientData(unsigned int n
) const;
128 // override some more base class methods
129 virtual bool SetFont(const wxFont
& font
);
131 // the wxUniversal-specific methods
132 // --------------------------------
134 // the current item is the same as the selected one for wxLB_SINGLE
135 // listboxes but for the other ones it is just the focused item which may
136 // be selected or not
137 int GetCurrentItem() const { return m_current
; }
138 void SetCurrentItem(int n
);
140 // select the item which is diff items below the current one
141 void ChangeCurrent(int diff
);
143 // activate (i.e. send a LISTBOX_DOUBLECLICKED message) the specified or
144 // current (if -1) item
145 void Activate(int item
= -1);
147 // select or unselect the specified or current (if -1) item
148 void DoSelect(int item
= -1, bool sel
= true);
150 // more readable wrapper
151 void DoUnselect(int item
) { DoSelect(item
, false); }
153 // select an item and send a notification about it
154 void SelectAndNotify(int item
);
156 // ensure that the given item is visible by scrolling it into view
157 virtual void EnsureVisible(int n
);
159 // find the first item [strictly] after the current one which starts with
160 // the given string and make it the current one, return true if the current
162 bool FindItem(const wxString
& prefix
, bool strictlyAfter
= false);
163 bool FindNextItem(const wxString
& prefix
) { return FindItem(prefix
, true); }
165 // extend the selection to span the range from the anchor (see below) to
166 // the specified or current item
167 void ExtendSelection(int itemTo
= -1);
169 // make this item the new selection anchor: extending selection with
170 // ExtendSelection() will work with it
171 void AnchorSelection(int itemFrom
) { m_selAnchor
= itemFrom
; }
173 // get, calculating it if necessary, the number of items per page, the
174 // height of each line and the max width of an item
175 int GetItemsPerPage() const;
176 wxCoord
GetLineHeight() const;
177 wxCoord
GetMaxWidth() const;
179 // override the wxControl virtual methods
180 virtual bool PerformAction(const wxControlAction
& action
,
182 const wxString
& strArg
= wxEmptyString
);
184 static wxInputHandler
*GetStdInputHandler(wxInputHandler
*handlerDef
);
185 virtual wxInputHandler
*DoGetStdInputHandler(wxInputHandler
*handlerDef
)
187 return GetStdInputHandler(handlerDef
);
191 virtual void OnInternalIdle();
195 virtual wxSize
DoGetBestClientSize() const;
196 virtual void DoSetSize(int x
, int y
,
197 int width
, int height
,
198 int sizeFlags
= wxSIZE_AUTO
);
200 virtual void DoDraw(wxControlRenderer
*renderer
);
201 virtual wxBorder
GetDefaultBorder() const;
203 // special hook for wxCheckListBox which allows it to update its internal
204 // data when a new item is inserted into the listbox
205 virtual void OnItemInserted(unsigned int WXUNUSED(pos
)) { }
208 // common part of all ctors
212 void OnSize(wxSizeEvent
& event
);
214 // refresh the given item(s) or everything
215 void RefreshItems(int from
, int count
);
216 void RefreshItem(int n
);
217 void RefreshFromItemToEnd(int n
);
220 // send an event of the given type (using m_current by default)
221 bool SendEvent(wxEventType type
, int item
= -1);
223 // calculate the number of items per page using our current size
224 void CalcItemsPerPage();
226 // can/should we have a horz scrollbar?
227 bool HasHorzScrollbar() const
228 { return (m_windowStyle
& wxLB_HSCROLL
) != 0; }
230 // redraw the items in the given range only: called from DoDraw()
231 virtual void DoDrawRange(wxControlRenderer
*renderer
,
232 int itemFirst
, int itemLast
);
234 // update the scrollbars and then ensure that the item is visible
235 void DoEnsureVisible(int n
);
237 // mark horz scrollbar for updating
238 void RefreshHorzScrollbar();
240 // update (show/hide/adjust) the scrollbars
241 void UpdateScrollbars();
243 // refresh the items specified by m_updateCount and m_updateFrom
246 // the array containing all items (it is sorted if the listbox has
250 wxArrayString
*unsorted
;
251 wxSortedArrayString
*sorted
;
254 // this array contains the indices of the selected items (for the single
255 // selection listboxes only the first element of it is used and contains
256 // the current selection)
257 wxArrayInt m_selections
;
259 // and this one the client data (either void or wxClientData)
260 wxArrayPtrVoid m_itemsClientData
;
266 // the range of elements which must be updated: if m_updateCount is 0 no
267 // update is needed, if it is -1 everything must be updated, otherwise
268 // m_updateCount items starting from m_updateFrom have to be redrawn
272 // the height of one line in the listbox (all lines have the same height)
273 wxCoord m_lineHeight
;
275 // the maximal width of a listbox item and the item which has it
279 // the extents of horz and vert scrollbars
283 // the number of items per page
284 size_t m_itemsPerPage
;
286 // if the number of items has changed we may need to show/hide the
288 bool m_updateScrollbarX
, m_updateScrollbarY
,
289 m_showScrollbarX
, m_showScrollbarY
;
291 // if the current item has changed, we might need to scroll if it went out
293 bool m_currentChanged
;
295 // the anchor from which the selection is extended for the listboxes with
296 // wxLB_EXTENDED style - this is set to the last item which was selected
297 // by not extending the selection but by choosing it directly
300 DECLARE_EVENT_TABLE()
301 DECLARE_DYNAMIC_CLASS(wxListBox
)
304 #endif // _WX_UNIV_LISTBOX_H_