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