]> git.saurik.com Git - wxWidgets.git/blob - include/wx/univ/listbox.h
cleanup
[wxWidgets.git] / include / wx / univ / listbox.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/univ/listbox.h
3 // Purpose: the universal listbox
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 30.08.00
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_UNIV_LISTBOX_H_
13 #define _WX_UNIV_LISTBOX_H_
14
15 #include "wx/scrolwin.h" // for wxScrollHelper
16 #include "wx/dynarray.h"
17 #include "wx/arrstr.h"
18
19 // ----------------------------------------------------------------------------
20 // the actions supported by this control
21 // ----------------------------------------------------------------------------
22
23 // change the current item
24 #define wxACTION_LISTBOX_SETFOCUS _T("setfocus") // select the item
25 #define wxACTION_LISTBOX_MOVEDOWN _T("down") // select item below
26 #define wxACTION_LISTBOX_MOVEUP _T("up") // select item above
27 #define wxACTION_LISTBOX_PAGEDOWN _T("pagedown") // go page down
28 #define wxACTION_LISTBOX_PAGEUP _T("pageup") // go page up
29 #define wxACTION_LISTBOX_START _T("start") // go to first item
30 #define wxACTION_LISTBOX_END _T("end") // go to last item
31 #define wxACTION_LISTBOX_FIND _T("find") // find item by 1st letter
32
33 // do something with the current item
34 #define wxACTION_LISTBOX_ACTIVATE _T("activate") // activate (choose)
35 #define wxACTION_LISTBOX_TOGGLE _T("toggle") // togglee selected state
36 #define wxACTION_LISTBOX_SELECT _T("select") // sel this, unsel others
37 #define wxACTION_LISTBOX_SELECTADD _T("selectadd") // add to selection
38 #define wxACTION_LISTBOX_UNSELECT _T("unselect") // unselect
39 #define wxACTION_LISTBOX_ANCHOR _T("selanchor") // anchor selection
40
41 // do something with the selection globally (not for single selection ones)
42 #define wxACTION_LISTBOX_SELECTALL _T("selectall") // select all items
43 #define wxACTION_LISTBOX_UNSELECTALL _T("unselectall") // unselect all items
44 #define wxACTION_LISTBOX_SELTOGGLE _T("togglesel") // invert the selection
45 #define wxACTION_LISTBOX_EXTENDSEL _T("extend") // extend to item
46
47 // ----------------------------------------------------------------------------
48 // wxListBox: a list of selectable items
49 // ----------------------------------------------------------------------------
50
51 class WXDLLEXPORT wxListBox : public wxListBoxBase, public wxScrollHelper
52 {
53 public:
54 // ctors and such
55 wxListBox() : wxScrollHelper(this) { Init(); }
56 wxListBox(wxWindow *parent,
57 wxWindowID id,
58 const wxPoint& pos = wxDefaultPosition,
59 const wxSize& size = wxDefaultSize,
60 int n = 0, const wxString choices[] = (const wxString *) NULL,
61 long style = 0,
62 const wxValidator& validator = wxDefaultValidator,
63 const wxString& name = wxListBoxNameStr )
64 : wxScrollHelper(this)
65 {
66 Init();
67
68 Create(parent, id, pos, size, n, choices, style, validator, name);
69 }
70 wxListBox(wxWindow *parent,
71 wxWindowID id,
72 const wxPoint& pos,
73 const wxSize& size,
74 const wxArrayString& choices,
75 long style = 0,
76 const wxValidator& validator = wxDefaultValidator,
77 const wxString& name = wxListBoxNameStr );
78
79 virtual ~wxListBox();
80
81 bool Create(wxWindow *parent,
82 wxWindowID id,
83 const wxPoint& pos = wxDefaultPosition,
84 const wxSize& size = wxDefaultSize,
85 int n = 0, const wxString choices[] = (const wxString *) NULL,
86 long style = 0,
87 const wxValidator& validator = wxDefaultValidator,
88 const wxString& name = wxListBoxNameStr);
89 bool Create(wxWindow *parent,
90 wxWindowID id,
91 const wxPoint& pos,
92 const wxSize& size,
93 const wxArrayString& choices,
94 long style = 0,
95 const wxValidator& validator = wxDefaultValidator,
96 const wxString& name = wxListBoxNameStr);
97
98 // implement the listbox interface defined by wxListBoxBase
99 virtual void DoClear();
100 virtual void DoDeleteOneItem(unsigned int n);
101
102 virtual unsigned int GetCount() const
103 { return (unsigned int)m_strings->GetCount(); }
104 virtual wxString GetString(unsigned int n) const
105 { return m_strings->Item(n); }
106 virtual void SetString(unsigned int n, const wxString& s);
107 virtual int FindString(const wxString& s, bool bCase = false) const
108 { return m_strings->Index(s, bCase); }
109
110 virtual bool IsSelected(int n) const
111 { return m_selections.Index(n) != wxNOT_FOUND; }
112 virtual int GetSelection() const;
113 virtual int GetSelections(wxArrayInt& aSelections) const;
114
115 protected:
116 virtual void DoSetSelection(int n, bool select);
117
118 virtual int DoInsertItems(const wxArrayStringsAdapter& items,
119 unsigned int pos,
120 void **clientData,
121 wxClientDataType type);
122
123 // universal wxComboBox implementation internally uses wxListBox
124 friend class WXDLLEXPORT wxComboBox;
125
126 virtual void DoSetFirstItem(int n);
127
128 virtual void DoSetItemClientData(unsigned int n, void* clientData);
129 virtual void* DoGetItemClientData(unsigned int n) const;
130
131 public:
132 // override some more base class methods
133 virtual bool SetFont(const wxFont& font);
134
135 // the wxUniversal-specific methods
136 // --------------------------------
137
138 // the current item is the same as the selected one for wxLB_SINGLE
139 // listboxes but for the other ones it is just the focused item which may
140 // be selected or not
141 int GetCurrentItem() const { return m_current; }
142 void SetCurrentItem(int n);
143
144 // select the item which is diff items below the current one
145 void ChangeCurrent(int diff);
146
147 // activate (i.e. send a LISTBOX_DOUBLECLICKED message) the specified or
148 // current (if -1) item
149 void Activate(int item = -1);
150
151 // select or unselect the specified or current (if -1) item
152 void DoSelect(int item = -1, bool sel = true);
153
154 // more readable wrapper
155 void DoUnselect(int item) { DoSelect(item, false); }
156
157 // select an item and send a notification about it
158 void SelectAndNotify(int item);
159
160 // ensure that the given item is visible by scrolling it into view
161 virtual void EnsureVisible(int n);
162
163 // find the first item [strictly] after the current one which starts with
164 // the given string and make it the current one, return true if the current
165 // item changed
166 bool FindItem(const wxString& prefix, bool strictlyAfter = false);
167 bool FindNextItem(const wxString& prefix) { return FindItem(prefix, true); }
168
169 // extend the selection to span the range from the anchor (see below) to
170 // the specified or current item
171 void ExtendSelection(int itemTo = -1);
172
173 // make this item the new selection anchor: extending selection with
174 // ExtendSelection() will work with it
175 void AnchorSelection(int itemFrom) { m_selAnchor = itemFrom; }
176
177 // get, calculating it if necessary, the number of items per page, the
178 // height of each line and the max width of an item
179 int GetItemsPerPage() const;
180 wxCoord GetLineHeight() const;
181 wxCoord GetMaxWidth() const;
182
183 // override the wxControl virtual methods
184 virtual bool PerformAction(const wxControlAction& action,
185 long numArg = 0l,
186 const wxString& strArg = wxEmptyString);
187
188 static wxInputHandler *GetStdInputHandler(wxInputHandler *handlerDef);
189 virtual wxInputHandler *DoGetStdInputHandler(wxInputHandler *handlerDef)
190 {
191 return GetStdInputHandler(handlerDef);
192 }
193
194 // idle processing
195 virtual void OnInternalIdle();
196
197 protected:
198 // geometry
199 virtual wxSize DoGetBestClientSize() const;
200 virtual void DoSetSize(int x, int y,
201 int width, int height,
202 int sizeFlags = wxSIZE_AUTO);
203
204 virtual void DoDraw(wxControlRenderer *renderer);
205 virtual wxBorder GetDefaultBorder() const;
206
207 // special hook for wxCheckListBox which allows it to update its internal
208 // data when a new item is inserted into the listbox
209 virtual void OnItemInserted(unsigned int WXUNUSED(pos)) { }
210
211
212 // common part of all ctors
213 void Init();
214
215 // event handlers
216 void OnSize(wxSizeEvent& event);
217
218 // refresh the given item(s) or everything
219 void RefreshItems(int from, int count);
220 void RefreshItem(int n);
221 void RefreshFromItemToEnd(int n);
222 void RefreshAll();
223
224 // send an event of the given type (using m_current by default)
225 bool SendEvent(wxEventType type, int item = -1);
226
227 // calculate the number of items per page using our current size
228 void CalcItemsPerPage();
229
230 // can/should we have a horz scrollbar?
231 bool HasHorzScrollbar() const
232 { return (m_windowStyle & wxLB_HSCROLL) != 0; }
233
234 // redraw the items in the given range only: called from DoDraw()
235 virtual void DoDrawRange(wxControlRenderer *renderer,
236 int itemFirst, int itemLast);
237
238 // update the scrollbars and then ensure that the item is visible
239 void DoEnsureVisible(int n);
240
241 // mark horz scrollbar for updating
242 void RefreshHorzScrollbar();
243
244 // update (show/hide/adjust) the scrollbars
245 void UpdateScrollbars();
246
247 // refresh the items specified by m_updateCount and m_updateFrom
248 void UpdateItems();
249
250 // the array containing all items (it is sorted if the listbox has
251 // wxLB_SORT style)
252 wxArrayString* m_strings;
253
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;
258
259 // and this one the client data (either void or wxClientData)
260 wxArrayPtrVoid m_itemsClientData;
261
262 // the current item
263 int m_current;
264
265 private:
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
269 int m_updateFrom,
270 m_updateCount;
271
272 // the height of one line in the listbox (all lines have the same height)
273 wxCoord m_lineHeight;
274
275 // the maximal width of a listbox item and the item which has it
276 wxCoord m_maxWidth;
277 int m_maxWidthItem;
278
279 // the extents of horz and vert scrollbars
280 int m_scrollRangeX,
281 m_scrollRangeY;
282
283 // the number of items per page
284 size_t m_itemsPerPage;
285
286 // if the number of items has changed we may need to show/hide the
287 // scrollbar
288 bool m_updateScrollbarX, m_updateScrollbarY,
289 m_showScrollbarX, m_showScrollbarY;
290
291 // if the current item has changed, we might need to scroll if it went out
292 // of the window
293 bool m_currentChanged;
294
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
298 int m_selAnchor;
299
300 DECLARE_EVENT_TABLE()
301 DECLARE_DYNAMIC_CLASS(wxListBox)
302 };
303
304 #endif // _WX_UNIV_LISTBOX_H_