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