]>
Commit | Line | Data |
---|---|---|
1e6feb95 VZ |
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$ | |
a3870b2f | 8 | // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
1e6feb95 VZ |
9 | // Licence: wxWindows licence |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_UNIV_LISTBOX_H_ | |
13 | #define _WX_UNIV_LISTBOX_H_ | |
14 | ||
12028905 | 15 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
11d13ed0 | 16 | #pragma interface "univlistbox.h" |
1e6feb95 VZ |
17 | #endif |
18 | ||
19 | #include "wx/scrolwin.h" // for wxScrollHelper | |
ed39ff57 | 20 | #include "wx/dynarray.h" |
18dbdd3c | 21 | #include "wx/arrstr.h" |
1e6feb95 VZ |
22 | |
23 | // ---------------------------------------------------------------------------- | |
24 | // the actions supported by this control | |
25 | // ---------------------------------------------------------------------------- | |
26 | ||
27 | // change the current item | |
28 | #define wxACTION_LISTBOX_SETFOCUS _T("setfocus") // select the item | |
29 | #define wxACTION_LISTBOX_MOVEDOWN _T("down") // select item below | |
30 | #define wxACTION_LISTBOX_MOVEUP _T("up") // select item above | |
31 | #define wxACTION_LISTBOX_PAGEDOWN _T("pagedown") // go page down | |
32 | #define wxACTION_LISTBOX_PAGEUP _T("pageup") // go page up | |
33 | #define wxACTION_LISTBOX_START _T("start") // go to first item | |
34 | #define wxACTION_LISTBOX_END _T("end") // go to last item | |
35 | #define wxACTION_LISTBOX_FIND _T("find") // find item by 1st letter | |
36 | ||
37 | // do something with the current item | |
38 | #define wxACTION_LISTBOX_ACTIVATE _T("activate") // activate (choose) | |
39 | #define wxACTION_LISTBOX_TOGGLE _T("toggle") // togglee selected state | |
40 | #define wxACTION_LISTBOX_SELECT _T("select") // sel this, unsel others | |
41 | #define wxACTION_LISTBOX_SELECTADD _T("selectadd") // add to selection | |
42 | #define wxACTION_LISTBOX_UNSELECT _T("unselect") // unselect | |
43 | #define wxACTION_LISTBOX_ANCHOR _T("selanchor") // anchor selection | |
44 | ||
45 | // do something with the selection globally (not for single selection ones) | |
46 | #define wxACTION_LISTBOX_SELECTALL _T("selectall") // select all items | |
47 | #define wxACTION_LISTBOX_UNSELECTALL _T("unselectall") // unselect all items | |
48 | #define wxACTION_LISTBOX_SELTOGGLE _T("togglesel") // invert the selection | |
49 | #define wxACTION_LISTBOX_EXTENDSEL _T("extend") // extend to item | |
50 | ||
51 | // ---------------------------------------------------------------------------- | |
52 | // wxListBox: a list of selectable items | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
d2a533a0 | 55 | class WXDLLEXPORT wxListBox : public wxListBoxBase, public wxScrollHelper |
1e6feb95 VZ |
56 | { |
57 | public: | |
58 | // ctors and such | |
59 | wxListBox() { Init(); } | |
60 | wxListBox(wxWindow *parent, | |
61 | wxWindowID id, | |
62 | const wxPoint& pos = wxDefaultPosition, | |
63 | const wxSize& size = wxDefaultSize, | |
64 | int n = 0, const wxString choices[] = (const wxString *) NULL, | |
65 | long style = 0, | |
66 | const wxValidator& validator = wxDefaultValidator, | |
67 | const wxString& name = wxListBoxNameStr ) | |
68 | { | |
69 | Init(); | |
70 | ||
71 | Create(parent, id, pos, size, n, choices, style, validator, name); | |
72 | } | |
73 | ||
74 | virtual ~wxListBox(); | |
75 | ||
76 | bool Create(wxWindow *parent, | |
77 | wxWindowID id, | |
78 | const wxPoint& pos = wxDefaultPosition, | |
79 | const wxSize& size = wxDefaultSize, | |
80 | int n = 0, const wxString choices[] = (const wxString *) NULL, | |
81 | long style = 0, | |
82 | const wxValidator& validator = wxDefaultValidator, | |
83 | const wxString& name = wxListBoxNameStr); | |
84 | ||
85 | // implement the listbox interface defined by wxListBoxBase | |
86 | virtual void Clear(); | |
87 | virtual void Delete(int n); | |
88 | ||
18dbdd3c MB |
89 | virtual int GetCount() const { return (int)m_strings->GetCount(); } |
90 | virtual wxString GetString(int n) const { return (*m_strings)[n]; } | |
1e6feb95 VZ |
91 | virtual void SetString(int n, const wxString& s); |
92 | virtual int FindString(const wxString& s) const | |
18dbdd3c | 93 | { return IsSorted() ? m_stringsSorted->Index(s) : m_strings->Index(s); } |
1e6feb95 VZ |
94 | |
95 | virtual bool IsSelected(int n) const | |
96 | { return m_selections.Index(n) != wxNOT_FOUND; } | |
97 | virtual void SetSelection(int n, bool select = TRUE); | |
98 | virtual int GetSelection() const; | |
99 | virtual int GetSelections(wxArrayInt& aSelections) const; | |
100 | ||
101 | protected: | |
102 | virtual int DoAppend(const wxString& item); | |
103 | virtual void DoInsertItems(const wxArrayString& items, int pos); | |
104 | virtual void DoSetItems(const wxArrayString& items, void **clientData); | |
105 | ||
106 | virtual void DoSetFirstItem(int n); | |
107 | ||
108 | virtual void DoSetItemClientData(int n, void* clientData); | |
109 | virtual void* DoGetItemClientData(int n) const; | |
110 | virtual void DoSetItemClientObject(int n, wxClientData* clientData); | |
111 | virtual wxClientData* DoGetItemClientObject(int n) const; | |
112 | ||
113 | public: | |
114 | // override some more base class methods | |
115 | virtual bool SetFont(const wxFont& font); | |
116 | ||
117 | // the wxUniversal-specific methods | |
118 | // -------------------------------- | |
119 | ||
120 | // the current item is the same as the selected one for wxLB_SINGLE | |
121 | // listboxes but for the other ones it is just the focused item which may | |
122 | // be selected or not | |
123 | int GetCurrentItem() const { return m_current; } | |
124 | void SetCurrentItem(int n); | |
125 | ||
126 | // select the item which is diff items below the current one | |
127 | void ChangeCurrent(int diff); | |
128 | ||
129 | // activate (i.e. send a LISTBOX_DOUBLECLICKED message) the specified or | |
130 | // current (if -1) item | |
131 | void Activate(int item = -1); | |
132 | ||
133 | // select or unselect the specified or current (if -1) item | |
2b5f62a0 | 134 | void DoSelect(int item = -1, bool sel = TRUE); |
1e6feb95 VZ |
135 | |
136 | // more readable wrapper | |
2b5f62a0 | 137 | void DoUnselect(int item) { DoSelect(item, FALSE); } |
1e6feb95 VZ |
138 | |
139 | // select an item and send a notification about it | |
140 | void SelectAndNotify(int item); | |
141 | ||
142 | // ensure that the given item is visible by scrolling it into view | |
143 | virtual void EnsureVisible(int n); | |
144 | ||
145 | // find the first item [strictly] after the current one which starts with | |
146 | // the given string and make it the current one, return TRUE if the current | |
147 | // item changed | |
148 | bool FindItem(const wxString& prefix, bool strictlyAfter = FALSE); | |
149 | bool FindNextItem(const wxString& prefix) { return FindItem(prefix, TRUE); } | |
150 | ||
151 | // extend the selection to span the range from the anchor (see below) to | |
152 | // the specified or current item | |
153 | void ExtendSelection(int itemTo = -1); | |
154 | ||
155 | // make this item the new selection anchor: extending selection with | |
156 | // ExtendSelection() will work with it | |
157 | void AnchorSelection(int itemFrom) { m_selAnchor = itemFrom; } | |
158 | ||
159 | // get, calculating it if necessary, the number of items per page, the | |
160 | // height of each line and the max width of an item | |
161 | int GetItemsPerPage() const; | |
162 | wxCoord GetLineHeight() const; | |
163 | wxCoord GetMaxWidth() const; | |
164 | ||
165 | // override the wxControl virtual methods | |
166 | virtual bool PerformAction(const wxControlAction& action, | |
167 | long numArg = 0l, | |
168 | const wxString& strArg = wxEmptyString); | |
169 | ||
e39af974 JS |
170 | // idle processing |
171 | virtual void OnInternalIdle(); | |
81b344a5 | 172 | |
1e6feb95 VZ |
173 | protected: |
174 | // geometry | |
175 | virtual wxSize DoGetBestClientSize() const; | |
176 | virtual void DoSetSize(int x, int y, | |
177 | int width, int height, | |
178 | int sizeFlags = wxSIZE_AUTO); | |
179 | ||
180 | virtual void DoDraw(wxControlRenderer *renderer); | |
181 | virtual wxBorder GetDefaultBorder() const; | |
182 | ||
183 | // common part of all ctors | |
184 | void Init(); | |
185 | ||
186 | // event handlers | |
1e6feb95 VZ |
187 | void OnSize(wxSizeEvent& event); |
188 | ||
189 | // common part of Clear() and DoSetItems(): clears everything | |
190 | virtual void DoClear(); | |
191 | ||
192 | // refresh the given item(s) or everything | |
193 | void RefreshItems(int from, int count); | |
194 | void RefreshItem(int n); | |
195 | void RefreshFromItemToEnd(int n); | |
196 | void RefreshAll(); | |
197 | ||
198 | // send an event of the given type (using m_current by default) | |
199 | bool SendEvent(wxEventType type, int item = -1); | |
200 | ||
201 | // calculate the number of items per page using our current size | |
202 | void CalcItemsPerPage(); | |
203 | ||
204 | // can/should we have a horz scrollbar? | |
205 | bool HasHorzScrollbar() const | |
206 | { return (m_windowStyle & wxLB_HSCROLL) != 0; } | |
207 | ||
208 | // redraw the items in the given range only: called from DoDraw() | |
209 | virtual void DoDrawRange(wxControlRenderer *renderer, | |
210 | int itemFirst, int itemLast); | |
211 | ||
212 | // update the scrollbars and then ensure that the item is visible | |
213 | void DoEnsureVisible(int n); | |
214 | ||
215 | // mark horz scrollbar for updating | |
216 | void RefreshHorzScrollbar(); | |
217 | ||
218 | // update (show/hide/adjust) the scrollbars | |
219 | void UpdateScrollbars(); | |
220 | ||
221 | // refresh the items specified by m_updateCount and m_updateFrom | |
222 | void UpdateItems(); | |
223 | ||
224 | // the array containing all items (it is sorted if the listbox has | |
18dbdd3c MB |
225 | // wxLB_SORT style). Note the evil trick: the pointers share the |
226 | // same location, hence we use m_strings when we don't care if the | |
227 | // array is sorted or not, m_stringsSorted when we do | |
228 | union | |
229 | { | |
230 | wxArrayString* m_strings; | |
231 | wxSortedArrayString* m_stringsSorted; | |
232 | }; | |
1e6feb95 VZ |
233 | |
234 | // this array contains the indices of the selected items (for the single | |
235 | // selection listboxes only the first element of it is used and contains | |
236 | // the current selection) | |
237 | wxArrayInt m_selections; | |
238 | ||
239 | // and this one the client data (either void or wxClientData) | |
240 | wxArrayPtrVoid m_itemsClientData; | |
241 | ||
242 | // the current item | |
243 | int m_current; | |
244 | ||
245 | private: | |
246 | // the range of elements which must be updated: if m_updateCount is 0 no | |
247 | // update is needed, if it is -1 everything must be updated, otherwise | |
248 | // m_updateCount items starting from m_updateFrom have to be redrawn | |
249 | int m_updateFrom, | |
250 | m_updateCount; | |
251 | ||
252 | // the height of one line in the listbox (all lines have the same height) | |
253 | wxCoord m_lineHeight; | |
254 | ||
255 | // the maximal width of a listbox item and the item which has it | |
256 | wxCoord m_maxWidth; | |
257 | int m_maxWidthItem; | |
258 | ||
259 | // the extents of horz and vert scrollbars | |
260 | int m_scrollRangeX, | |
261 | m_scrollRangeY; | |
262 | ||
263 | // the number of items per page | |
264 | size_t m_itemsPerPage; | |
265 | ||
266 | // if the number of items has changed we may need to show/hide the | |
267 | // scrollbar | |
268 | bool m_updateScrollbarX, m_updateScrollbarY, | |
269 | m_showScrollbarX, m_showScrollbarY; | |
270 | ||
271 | // if the current item has changed, we might need to scroll if it went out | |
272 | // of the window | |
273 | bool m_currentChanged; | |
274 | ||
275 | // the anchor from which the selection is extended for the listboxes with | |
276 | // wxLB_EXTENDED style - this is set to the last item which was selected | |
277 | // by not extending the selection but by choosing it directly | |
278 | int m_selAnchor; | |
279 | ||
280 | DECLARE_EVENT_TABLE() | |
281 | DECLARE_DYNAMIC_CLASS(wxListBox) | |
282 | }; | |
283 | ||
284 | // ---------------------------------------------------------------------------- | |
285 | // wxStdListboxInputHandler: handles mouse and kbd in a single or multi | |
286 | // selection listbox | |
287 | // ---------------------------------------------------------------------------- | |
288 | ||
289 | class WXDLLEXPORT wxStdListboxInputHandler : public wxStdInputHandler | |
290 | { | |
291 | public: | |
292 | // if pressing the mouse button in a multiselection listbox should toggle | |
293 | // the item under mouse immediately, then specify TRUE as the second | |
294 | // parameter (this is the standard behaviour, under GTK the item is toggled | |
295 | // only when the mouse is released in the multi selection listbox) | |
296 | wxStdListboxInputHandler(wxInputHandler *inphand, | |
297 | bool toggleOnPressAlways = TRUE); | |
298 | ||
299 | // base class methods | |
23645bfa | 300 | virtual bool HandleKey(wxInputConsumer *consumer, |
1e6feb95 VZ |
301 | const wxKeyEvent& event, |
302 | bool pressed); | |
23645bfa | 303 | virtual bool HandleMouse(wxInputConsumer *consumer, |
1e6feb95 | 304 | const wxMouseEvent& event); |
23645bfa | 305 | virtual bool HandleMouseMove(wxInputConsumer *consumer, |
1e6feb95 VZ |
306 | const wxMouseEvent& event); |
307 | ||
308 | protected: | |
309 | // return the item under mouse, 0 if the mouse is above the listbox or | |
310 | // GetCount() if it is below it | |
311 | int HitTest(const wxListBox *listbox, const wxMouseEvent& event); | |
312 | ||
313 | // parts of HitTest(): first finds the pseudo (because not in range) index | |
314 | // of the item and the second one adjusts it if necessary - that is if the | |
315 | // third one returns FALSE | |
316 | int HitTestUnsafe(const wxListBox *listbox, const wxMouseEvent& event); | |
317 | int FixItemIndex(const wxListBox *listbox, int item); | |
318 | bool IsValidIndex(const wxListBox *listbox, int item); | |
319 | ||
320 | // init m_btnCapture and m_actionMouse | |
321 | wxControlAction SetupCapture(wxListBox *lbox, | |
322 | const wxMouseEvent& event, | |
323 | int item); | |
324 | ||
325 | wxRenderer *m_renderer; | |
326 | ||
327 | // the button which initiated the mouse capture (currently 0 or 1) | |
328 | int m_btnCapture; | |
329 | ||
330 | // the action to perform when the mouse moves while we capture it | |
331 | wxControlAction m_actionMouse; | |
332 | ||
333 | // the ctor parameter toggleOnPressAlways (see comments near it) | |
334 | bool m_toggleOnPressAlways; | |
335 | ||
336 | // do we track the mouse outside the window when it is captured? | |
337 | bool m_trackMouseOutside; | |
338 | }; | |
339 | ||
340 | #endif // _WX_UNIV_LISTBOX_H_ |