]> git.saurik.com Git - wxWidgets.git/blame - include/wx/univ/listbox.h
Improved handling of column widths, removed need for hidden column for sorting, and...
[wxWidgets.git] / include / wx / univ / listbox.h
CommitLineData
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>
65571936 9// Licence: wxWindows licence
1e6feb95
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_UNIV_LISTBOX_H_
13#define _WX_UNIV_LISTBOX_H_
14
1e6feb95 15#include "wx/scrolwin.h" // for wxScrollHelper
ed39ff57 16#include "wx/dynarray.h"
18dbdd3c 17#include "wx/arrstr.h"
1e6feb95
VZ
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
d2a533a0 51class WXDLLEXPORT wxListBox : public wxListBoxBase, public wxScrollHelper
1e6feb95
VZ
52{
53public:
54 // ctors and such
afe13769 55 wxListBox() : wxScrollHelper(this) { Init(); }
1e6feb95
VZ
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,
6463b9f5 63 const wxString& name = wxListBoxNameStr )
afe13769 64 : wxScrollHelper(this)
6463b9f5
JS
65 {
66 Init();
67
68 Create(parent, id, pos, size, n, choices, style, validator, name);
69 }
584ad2a3
MB
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 );
1e6feb95
VZ
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);
584ad2a3
MB
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);
1e6feb95
VZ
97
98 // implement the listbox interface defined by wxListBoxBase
99 virtual void Clear();
aa61d352 100 virtual void Delete(unsigned int n);
1e6feb95 101
aa61d352
VZ
102 virtual unsigned int GetCount() const
103 { return (unsigned int)m_strings->GetCount(); }
104 virtual wxString GetString(unsigned int n) const
e1d6e01c 105 { return m_strings->Item(n); }
aa61d352 106 virtual void SetString(unsigned int n, const wxString& s);
853dcc57
WS
107 virtual int FindString(const wxString& s, bool bCase = false) const
108 { return m_strings->Index(s, bCase); }
1e6feb95
VZ
109
110 virtual bool IsSelected(int n) const
111 { return m_selections.Index(n) != wxNOT_FOUND; }
1e6feb95
VZ
112 virtual int GetSelection() const;
113 virtual int GetSelections(wxArrayInt& aSelections) const;
114
115protected:
6f02a879 116 virtual void DoSetSelection(int n, bool select);
e1d6e01c 117 virtual int DoAppendOnly(const wxString& item);
1e6feb95 118 virtual int DoAppend(const wxString& item);
aa61d352 119 virtual void DoInsertItems(const wxArrayString& items, unsigned int pos);
1e6feb95
VZ
120 virtual void DoSetItems(const wxArrayString& items, void **clientData);
121
122 virtual void DoSetFirstItem(int n);
123
aa61d352
VZ
124 virtual void DoSetItemClientData(unsigned int n, void* clientData);
125 virtual void* DoGetItemClientData(unsigned int n) const;
126 virtual void DoSetItemClientObject(unsigned int n, wxClientData* clientData);
127 virtual wxClientData* DoGetItemClientObject(unsigned int n) const;
1e6feb95
VZ
128
129public:
130 // override some more base class methods
131 virtual bool SetFont(const wxFont& font);
132
133 // the wxUniversal-specific methods
134 // --------------------------------
135
136 // the current item is the same as the selected one for wxLB_SINGLE
137 // listboxes but for the other ones it is just the focused item which may
138 // be selected or not
139 int GetCurrentItem() const { return m_current; }
140 void SetCurrentItem(int n);
141
142 // select the item which is diff items below the current one
143 void ChangeCurrent(int diff);
144
145 // activate (i.e. send a LISTBOX_DOUBLECLICKED message) the specified or
146 // current (if -1) item
147 void Activate(int item = -1);
148
149 // select or unselect the specified or current (if -1) item
a290fa5a 150 void DoSelect(int item = -1, bool sel = true);
1e6feb95
VZ
151
152 // more readable wrapper
a290fa5a 153 void DoUnselect(int item) { DoSelect(item, false); }
1e6feb95
VZ
154
155 // select an item and send a notification about it
156 void SelectAndNotify(int item);
157
158 // ensure that the given item is visible by scrolling it into view
159 virtual void EnsureVisible(int n);
160
161 // find the first item [strictly] after the current one which starts with
a290fa5a 162 // the given string and make it the current one, return true if the current
1e6feb95 163 // item changed
a290fa5a
WS
164 bool FindItem(const wxString& prefix, bool strictlyAfter = false);
165 bool FindNextItem(const wxString& prefix) { return FindItem(prefix, true); }
1e6feb95
VZ
166
167 // extend the selection to span the range from the anchor (see below) to
168 // the specified or current item
169 void ExtendSelection(int itemTo = -1);
170
171 // make this item the new selection anchor: extending selection with
172 // ExtendSelection() will work with it
173 void AnchorSelection(int itemFrom) { m_selAnchor = itemFrom; }
174
175 // get, calculating it if necessary, the number of items per page, the
176 // height of each line and the max width of an item
177 int GetItemsPerPage() const;
178 wxCoord GetLineHeight() const;
179 wxCoord GetMaxWidth() const;
180
181 // override the wxControl virtual methods
182 virtual bool PerformAction(const wxControlAction& action,
183 long numArg = 0l,
184 const wxString& strArg = wxEmptyString);
185
9467bdb7
VZ
186 static wxInputHandler *GetStdInputHandler(wxInputHandler *handlerDef);
187 virtual wxInputHandler *DoGetStdInputHandler(wxInputHandler *handlerDef)
188 {
189 return GetStdInputHandler(handlerDef);
190 }
191
e39af974
JS
192 // idle processing
193 virtual void OnInternalIdle();
81b344a5 194
1e6feb95
VZ
195protected:
196 // geometry
197 virtual wxSize DoGetBestClientSize() const;
198 virtual void DoSetSize(int x, int y,
199 int width, int height,
200 int sizeFlags = wxSIZE_AUTO);
201
202 virtual void DoDraw(wxControlRenderer *renderer);
203 virtual wxBorder GetDefaultBorder() const;
204
205 // common part of all ctors
206 void Init();
207
208 // event handlers
1e6feb95
VZ
209 void OnSize(wxSizeEvent& event);
210
211 // common part of Clear() and DoSetItems(): clears everything
212 virtual void DoClear();
213
214 // refresh the given item(s) or everything
215 void RefreshItems(int from, int count);
216 void RefreshItem(int n);
217 void RefreshFromItemToEnd(int n);
218 void RefreshAll();
219
220 // send an event of the given type (using m_current by default)
221 bool SendEvent(wxEventType type, int item = -1);
222
223 // calculate the number of items per page using our current size
224 void CalcItemsPerPage();
225
226 // can/should we have a horz scrollbar?
227 bool HasHorzScrollbar() const
228 { return (m_windowStyle & wxLB_HSCROLL) != 0; }
229
230 // redraw the items in the given range only: called from DoDraw()
231 virtual void DoDrawRange(wxControlRenderer *renderer,
232 int itemFirst, int itemLast);
233
234 // update the scrollbars and then ensure that the item is visible
235 void DoEnsureVisible(int n);
236
237 // mark horz scrollbar for updating
238 void RefreshHorzScrollbar();
239
240 // update (show/hide/adjust) the scrollbars
241 void UpdateScrollbars();
242
243 // refresh the items specified by m_updateCount and m_updateFrom
244 void UpdateItems();
245
246 // the array containing all items (it is sorted if the listbox has
e1d6e01c
WS
247 // wxLB_SORT style)
248 wxArrayString* m_strings;
1e6feb95
VZ
249
250 // this array contains the indices of the selected items (for the single
251 // selection listboxes only the first element of it is used and contains
252 // the current selection)
253 wxArrayInt m_selections;
254
255 // and this one the client data (either void or wxClientData)
256 wxArrayPtrVoid m_itemsClientData;
257
258 // the current item
259 int m_current;
260
261private:
262 // the range of elements which must be updated: if m_updateCount is 0 no
263 // update is needed, if it is -1 everything must be updated, otherwise
264 // m_updateCount items starting from m_updateFrom have to be redrawn
265 int m_updateFrom,
266 m_updateCount;
267
268 // the height of one line in the listbox (all lines have the same height)
269 wxCoord m_lineHeight;
270
271 // the maximal width of a listbox item and the item which has it
272 wxCoord m_maxWidth;
273 int m_maxWidthItem;
274
275 // the extents of horz and vert scrollbars
276 int m_scrollRangeX,
277 m_scrollRangeY;
278
279 // the number of items per page
280 size_t m_itemsPerPage;
281
282 // if the number of items has changed we may need to show/hide the
283 // scrollbar
284 bool m_updateScrollbarX, m_updateScrollbarY,
285 m_showScrollbarX, m_showScrollbarY;
286
287 // if the current item has changed, we might need to scroll if it went out
288 // of the window
289 bool m_currentChanged;
290
291 // the anchor from which the selection is extended for the listboxes with
292 // wxLB_EXTENDED style - this is set to the last item which was selected
293 // by not extending the selection but by choosing it directly
294 int m_selAnchor;
295
296 DECLARE_EVENT_TABLE()
297 DECLARE_DYNAMIC_CLASS(wxListBox)
298};
299
1e6feb95 300#endif // _WX_UNIV_LISTBOX_H_