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