]> git.saurik.com Git - wxWidgets.git/blob - include/wx/vlbox.h
Committing in .
[wxWidgets.git] / include / wx / vlbox.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/vlbox.h
3 // Purpose: wxVListBox is a virtual listbox with lines of variable height
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 31.05.03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_VLBOX_H_
13 #define _WX_VLBOX_H_
14
15 #include "wx/vscroll.h" // base class
16
17 class WXDLLEXPORT wxSelectionStore;
18
19 #define wxVListBoxNameStr _T("wxVListBox")
20
21 // ----------------------------------------------------------------------------
22 // wxVListBox
23 // ----------------------------------------------------------------------------
24
25 /*
26 This class has two main differences from a regular listbox: it can have an
27 arbitrarily huge number of items because it doesn't store them itself but
28 uses OnDrawItem() callback to draw them and its items can have variable
29 height as determined by OnMeasureItem().
30
31 It emits the same events as wxListBox and the same event macros may be used
32 with it.
33 */
34 class WXDLLEXPORT wxVListBox : public wxVScrolledWindow
35 {
36 public:
37 // constructors and such
38 // ---------------------
39
40 // default constructor, you must call Create() later
41 wxVListBox() { Init(); }
42
43 // normal constructor which calls Create() internally
44 wxVListBox(wxWindow *parent,
45 wxWindowID id = wxID_ANY,
46 const wxPoint& pos = wxDefaultPosition,
47 const wxSize& size = wxDefaultSize,
48 long style = 0,
49 const wxString& name = wxVListBoxNameStr)
50 {
51 Init();
52
53 (void)Create(parent, id, pos, size, style, name);
54 }
55
56 // really creates the control and sets the initial number of items in it
57 // (which may be changed later with SetItemCount())
58 //
59 // the only special style which may be specified here is wxLB_MULTIPLE
60 //
61 // returns true on success or false if the control couldn't be created
62 bool Create(wxWindow *parent,
63 wxWindowID id = wxID_ANY,
64 const wxPoint& pos = wxDefaultPosition,
65 const wxSize& size = wxDefaultSize,
66 long style = 0,
67 const wxString& name = wxVListBoxNameStr);
68
69 // dtor does some internal cleanup (deletes m_selStore if any)
70 virtual ~wxVListBox();
71
72
73 // accessors
74 // ---------
75
76 // get the number of items in the control
77 size_t GetItemCount() const { return GetLineCount(); }
78
79 // does this control use multiple selection?
80 bool HasMultipleSelection() const { return m_selStore != NULL; }
81
82 // get the currently selected item or wxNOT_FOUND if there is no selection
83 //
84 // this method is only valid for the single selection listboxes
85 int GetSelection() const
86 {
87 wxASSERT_MSG( !HasMultipleSelection(),
88 _T("GetSelection() can't be used with wxLB_MULTIPLE") );
89
90 return m_current;
91 }
92
93 // is this item the current one?
94 bool IsCurrent(size_t item) const { return item == (size_t)m_current; }
95
96 // is this item selected?
97 bool IsSelected(size_t item) const;
98
99 // get the number of the selected items (maybe 0)
100 //
101 // this method is valid for both single and multi selection listboxes
102 size_t GetSelectedCount() const;
103
104 // get the first selected item, returns wxNOT_FOUND if none
105 //
106 // cookie is an opaque parameter which should be passed to
107 // GetNextSelected() later
108 //
109 // this method is only valid for the multi selection listboxes
110 int GetFirstSelected(unsigned long& cookie) const;
111
112 // get next selection item, return wxNOT_FOUND if no more
113 //
114 // cookie must be the same parameter that was passed to GetFirstSelected()
115 // before
116 //
117 // this method is only valid for the multi selection listboxes
118 int GetNextSelected(unsigned long& cookie) const;
119
120 // get the margins around each item
121 wxPoint GetMargins() const { return m_ptMargins; }
122
123
124 // operations
125 // ----------
126
127 // set the number of items to be shown in the control
128 //
129 // this is just a synonym for wxVScrolledWindow::SetLineCount()
130 void SetItemCount(size_t count);
131
132 // delete all items from the control
133 void Clear() { SetItemCount(0); }
134
135 // set the selection to the specified item, if it is -1 the selection is
136 // unset
137 //
138 // this function is only valid for the single selection listboxes
139 void SetSelection(int selection);
140
141 // selects or deselects the specified item which must be valid (i.e. not
142 // equal to -1)
143 //
144 // return true if the items selection status has changed or false
145 // otherwise
146 //
147 // this function is only valid for the multiple selection listboxes
148 bool Select(size_t item, bool select = true);
149
150 // selects the items in the specified range whose end points may be given
151 // in any order
152 //
153 // return true if any items selection status has changed, false otherwise
154 //
155 // this function is only valid for the single selection listboxes
156 bool SelectRange(size_t from, size_t to);
157
158 // toggle the selection of the specified item (must be valid)
159 //
160 // this function is only valid for the multiple selection listboxes
161 void Toggle(size_t item) { Select(item, !IsSelected(item)); }
162
163 // select all items in the listbox
164 //
165 // the return code indicates if any items were affected by this operation
166 // (true) or if nothing has changed (false)
167 bool SelectAll() { return DoSelectAll(true); }
168
169 // unselect all items in the listbox
170 //
171 // the return code has the same meaning as for SelectAll()
172 bool DeselectAll() { return DoSelectAll(false); }
173
174 // set the margins: horizontal margin is the distance between the window
175 // border and the item contents while vertical margin is half of the
176 // distance between items
177 //
178 // by default both margins are 0
179 void SetMargins(const wxPoint& pt);
180 void SetMargins(wxCoord x, wxCoord y) { SetMargins(wxPoint(x, y)); }
181
182
183 protected:
184 // the derived class must implement this function to actually draw the item
185 // with the given index on the provided DC
186 virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const = 0;
187
188 // the derived class must implement this method to return the height of the
189 // specified item
190 virtual wxCoord OnMeasureItem(size_t n) const = 0;
191
192 // this method may be used to draw separators between the lines; note that
193 // the rectangle may be modified, typically to deflate it a bit before
194 // passing to OnDrawItem()
195 //
196 // the base class version doesn't do anything
197 virtual void OnDrawSeparator(wxDC& dc, wxRect& rect, size_t n) const;
198
199
200 // we implement OnGetLineHeight() in terms of OnMeasureItem() because this
201 // allows us to add borders to the items easily
202 //
203 // this function is not upposed to be overridden by the derived classes
204 virtual wxCoord OnGetLineHeight(size_t line) const;
205
206
207 // event handlers
208 void OnPaint(wxPaintEvent& event);
209 void OnKeyDown(wxKeyEvent& event);
210 void OnLeftDown(wxMouseEvent& event);
211 void OnLeftDClick(wxMouseEvent& event);
212
213
214 // common part of all ctors
215 void Init();
216
217 // send the wxEVT_COMMAND_LISTBOX_SELECTED event
218 void SendSelectedEvent();
219
220 // common implementation of SelectAll() and DeselectAll()
221 bool DoSelectAll(bool select);
222
223 // change the current item (in single selection listbox it also implicitly
224 // changes the selection); current may be -1 in which case there will be
225 // no current item any more
226 //
227 // return true if the current item changed, false otherwise
228 bool DoSetCurrent(int current);
229
230 // common part of keyboard and mouse handling processing code
231 void DoHandleItemClick(int item, bool shiftDown, bool ctrlDown);
232
233 private:
234 // the current item or -1
235 //
236 // if m_selStore == NULL this is also the selected item, otherwise the
237 // selections are managed by m_selStore
238 int m_current;
239
240 // the object managing our selected items if not NULL
241 wxSelectionStore *m_selStore;
242
243 // margins
244 wxPoint m_ptMargins;
245
246 DECLARE_EVENT_TABLE()
247 };
248
249 #endif // _WX_VLBOX_H_
250