]>
Commit | Line | Data |
---|---|---|
99d80019 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/treectrl.h | |
3 | // Purpose: wxTreeCtrl base header | |
4 | // Author: Karsten Ballueder | |
5 | // Modified by: | |
6 | // Created: | |
7 | // Copyright: (c) Karsten Ballueder | |
8 | // RCS-ID: $Id$ | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
34138703 JS |
12 | #ifndef _WX_TREECTRL_H_BASE_ |
13 | #define _WX_TREECTRL_H_BASE_ | |
c801d85f | 14 | |
b220b1a2 WS |
15 | // ---------------------------------------------------------------------------- |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | #include "wx/defs.h" | |
20 | ||
21 | #if wxUSE_TREECTRL | |
22 | ||
1fd0e166 | 23 | #include "wx/control.h" |
484523cf | 24 | #include "wx/treebase.h" |
1fd0e166 | 25 | #include "wx/textctrl.h" // wxTextCtrl::ms_classinfo used through CLASSINFO macro |
1044a386 | 26 | |
8cee4a30 VZ |
27 | class WXDLLEXPORT wxImageList; |
28 | ||
29 | // ---------------------------------------------------------------------------- | |
30 | // wxTreeCtrlBase | |
31 | // ---------------------------------------------------------------------------- | |
32 | ||
33 | class WXDLLEXPORT wxTreeCtrlBase : public wxControl | |
34 | { | |
35 | public: | |
36 | wxTreeCtrlBase() | |
37 | { | |
38 | m_imageListNormal = | |
39 | m_imageListState = NULL; | |
40 | m_ownsImageListNormal = | |
41 | m_ownsImageListState = false; | |
42 | ||
43 | // arbitrary default | |
44 | m_spacing = 18; | |
7c384067 JS |
45 | |
46 | // quick DoGetBestSize calculation | |
47 | m_quickBestSize = true; | |
8cee4a30 VZ |
48 | } |
49 | ||
50 | virtual ~wxTreeCtrlBase(); | |
51 | ||
52 | // accessors | |
53 | // --------- | |
54 | ||
55 | // get the total number of items in the control | |
027d45e8 | 56 | virtual unsigned int GetCount() const = 0; |
8cee4a30 VZ |
57 | |
58 | // indent is the number of pixels the children are indented relative to | |
59 | // the parents position. SetIndent() also redraws the control | |
60 | // immediately. | |
61 | virtual unsigned int GetIndent() const = 0; | |
62 | virtual void SetIndent(unsigned int indent) = 0; | |
63 | ||
64 | // spacing is the number of pixels between the start and the Text | |
65 | // (has no effect under wxMSW) | |
66 | unsigned int GetSpacing() const { return m_spacing; } | |
67 | void SetSpacing(unsigned int spacing) { m_spacing = spacing; } | |
68 | ||
69 | // image list: these functions allow to associate an image list with | |
70 | // the control and retrieve it. Note that the control does _not_ delete | |
71 | // the associated image list when it's deleted in order to allow image | |
72 | // lists to be shared between different controls. | |
73 | // | |
74 | // The normal image list is for the icons which correspond to the | |
75 | // normal tree item state (whether it is selected or not). | |
76 | // Additionally, the application might choose to show a state icon | |
77 | // which corresponds to an app-defined item state (for example, | |
78 | // checked/unchecked) which are taken from the state image list. | |
79 | wxImageList *GetImageList() const { return m_imageListNormal; } | |
80 | wxImageList *GetStateImageList() const { return m_imageListState; } | |
81 | ||
82 | virtual void SetImageList(wxImageList *imageList) = 0; | |
83 | virtual void SetStateImageList(wxImageList *imageList) = 0; | |
84 | void AssignImageList(wxImageList *imageList) | |
85 | { | |
86 | SetImageList(imageList); | |
87 | m_ownsImageListNormal = true; | |
88 | } | |
89 | void AssignStateImageList(wxImageList *imageList) | |
90 | { | |
91 | SetStateImageList(imageList); | |
92 | m_ownsImageListState = true; | |
93 | } | |
94 | ||
95 | ||
96 | // Functions to work with tree ctrl items. Unfortunately, they can _not_ be | |
97 | // member functions of wxTreeItem because they must know the tree the item | |
98 | // belongs to for Windows implementation and storing the pointer to | |
99 | // wxTreeCtrl in each wxTreeItem is just too much waste. | |
100 | ||
101 | // accessors | |
102 | // --------- | |
103 | ||
104 | // retrieve items label | |
105 | virtual wxString GetItemText(const wxTreeItemId& item) const = 0; | |
106 | // get one of the images associated with the item (normal by default) | |
107 | virtual int GetItemImage(const wxTreeItemId& item, | |
108 | wxTreeItemIcon which = wxTreeItemIcon_Normal) const = 0; | |
109 | // get the data associated with the item | |
110 | virtual wxTreeItemData *GetItemData(const wxTreeItemId& item) const = 0; | |
111 | ||
112 | // get the item's text colour | |
113 | virtual wxColour GetItemTextColour(const wxTreeItemId& item) const = 0; | |
114 | ||
115 | // get the item's background colour | |
116 | virtual wxColour GetItemBackgroundColour(const wxTreeItemId& item) const = 0; | |
117 | ||
118 | // get the item's font | |
119 | virtual wxFont GetItemFont(const wxTreeItemId& item) const = 0; | |
120 | ||
121 | // modifiers | |
122 | // --------- | |
123 | ||
124 | // set items label | |
125 | virtual void SetItemText(const wxTreeItemId& item, const wxString& text) = 0; | |
126 | // get one of the images associated with the item (normal by default) | |
127 | virtual void SetItemImage(const wxTreeItemId& item, | |
128 | int image, | |
129 | wxTreeItemIcon which = wxTreeItemIcon_Normal) = 0; | |
130 | // associate some data with the item | |
131 | virtual void SetItemData(const wxTreeItemId& item, wxTreeItemData *data) = 0; | |
132 | ||
133 | // force appearance of [+] button near the item. This is useful to | |
134 | // allow the user to expand the items which don't have any children now | |
135 | // - but instead add them only when needed, thus minimizing memory | |
136 | // usage and loading time. | |
137 | virtual void SetItemHasChildren(const wxTreeItemId& item, | |
138 | bool has = true) = 0; | |
139 | ||
140 | // the item will be shown in bold | |
141 | virtual void SetItemBold(const wxTreeItemId& item, bool bold = true) = 0; | |
142 | ||
143 | // the item will be shown with a drop highlight | |
144 | virtual void SetItemDropHighlight(const wxTreeItemId& item, | |
145 | bool highlight = true) = 0; | |
146 | ||
147 | // set the items text colour | |
148 | virtual void SetItemTextColour(const wxTreeItemId& item, | |
149 | const wxColour& col) = 0; | |
150 | ||
151 | // set the items background colour | |
152 | virtual void SetItemBackgroundColour(const wxTreeItemId& item, | |
153 | const wxColour& col) = 0; | |
154 | ||
155 | // set the items font (should be of the same height for all items) | |
156 | virtual void SetItemFont(const wxTreeItemId& item, | |
157 | const wxFont& font) = 0; | |
158 | ||
159 | // item status inquiries | |
160 | // --------------------- | |
161 | ||
162 | // is the item visible (it might be outside the view or not expanded)? | |
163 | virtual bool IsVisible(const wxTreeItemId& item) const = 0; | |
164 | // does the item has any children? | |
165 | virtual bool ItemHasChildren(const wxTreeItemId& item) const = 0; | |
166 | // same as above | |
167 | bool HasChildren(const wxTreeItemId& item) const | |
168 | { return ItemHasChildren(item); } | |
169 | // is the item expanded (only makes sense if HasChildren())? | |
170 | virtual bool IsExpanded(const wxTreeItemId& item) const = 0; | |
171 | // is this item currently selected (the same as has focus)? | |
172 | virtual bool IsSelected(const wxTreeItemId& item) const = 0; | |
173 | // is item text in bold font? | |
174 | virtual bool IsBold(const wxTreeItemId& item) const = 0; | |
175 | ||
176 | // number of children | |
177 | // ------------------ | |
178 | ||
179 | // if 'recursively' is false, only immediate children count, otherwise | |
180 | // the returned number is the number of all items in this branch | |
181 | virtual size_t GetChildrenCount(const wxTreeItemId& item, | |
182 | bool recursively = true) const = 0; | |
183 | ||
184 | // navigation | |
185 | // ---------- | |
186 | ||
187 | // wxTreeItemId.IsOk() will return false if there is no such item | |
188 | ||
189 | // get the root tree item | |
190 | virtual wxTreeItemId GetRootItem() const = 0; | |
191 | ||
192 | // get the item currently selected (may return NULL if no selection) | |
193 | virtual wxTreeItemId GetSelection() const = 0; | |
194 | ||
195 | // get the items currently selected, return the number of such item | |
196 | // | |
197 | // NB: this operation is expensive and can take a long time for a | |
198 | // control with a lot of items (~ O(number of items)). | |
199 | virtual size_t GetSelections(wxArrayTreeItemIds& selections) const = 0; | |
200 | ||
201 | // get the parent of this item (may return NULL if root) | |
202 | virtual wxTreeItemId GetItemParent(const wxTreeItemId& item) const = 0; | |
203 | ||
204 | // for this enumeration function you must pass in a "cookie" parameter | |
205 | // which is opaque for the application but is necessary for the library | |
206 | // to make these functions reentrant (i.e. allow more than one | |
207 | // enumeration on one and the same object simultaneously). Of course, | |
208 | // the "cookie" passed to GetFirstChild() and GetNextChild() should be | |
209 | // the same! | |
210 | ||
211 | // get the first child of this item | |
212 | virtual wxTreeItemId GetFirstChild(const wxTreeItemId& item, | |
213 | wxTreeItemIdValue& cookie) const = 0; | |
214 | // get the next child | |
215 | virtual wxTreeItemId GetNextChild(const wxTreeItemId& item, | |
216 | wxTreeItemIdValue& cookie) const = 0; | |
217 | // get the last child of this item - this method doesn't use cookies | |
218 | virtual wxTreeItemId GetLastChild(const wxTreeItemId& item) const = 0; | |
219 | ||
220 | // get the next sibling of this item | |
221 | virtual wxTreeItemId GetNextSibling(const wxTreeItemId& item) const = 0; | |
222 | // get the previous sibling | |
223 | virtual wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const = 0; | |
224 | ||
225 | // get first visible item | |
226 | virtual wxTreeItemId GetFirstVisibleItem() const = 0; | |
227 | // get the next visible item: item must be visible itself! | |
228 | // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem() | |
229 | virtual wxTreeItemId GetNextVisible(const wxTreeItemId& item) const = 0; | |
230 | // get the previous visible item: item must be visible itself! | |
231 | virtual wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const = 0; | |
232 | ||
233 | // operations | |
234 | // ---------- | |
235 | ||
236 | // add the root node to the tree | |
237 | virtual wxTreeItemId AddRoot(const wxString& text, | |
238 | int image = -1, int selImage = -1, | |
239 | wxTreeItemData *data = NULL) = 0; | |
240 | ||
241 | // insert a new item in as the first child of the parent | |
242 | wxTreeItemId PrependItem(const wxTreeItemId& parent, | |
243 | const wxString& text, | |
244 | int image = -1, int selImage = -1, | |
245 | wxTreeItemData *data = NULL) | |
246 | { | |
247 | return DoInsertItem(parent, 0u, text, image, selImage, data); | |
248 | } | |
249 | ||
250 | // insert a new item after a given one | |
251 | wxTreeItemId InsertItem(const wxTreeItemId& parent, | |
252 | const wxTreeItemId& idPrevious, | |
253 | const wxString& text, | |
254 | int image = -1, int selImage = -1, | |
255 | wxTreeItemData *data = NULL) | |
256 | { | |
257 | return DoInsertAfter(parent, idPrevious, text, image, selImage, data); | |
258 | } | |
259 | ||
260 | // insert a new item before the one with the given index | |
261 | wxTreeItemId InsertItem(const wxTreeItemId& parent, | |
262 | size_t pos, | |
263 | const wxString& text, | |
264 | int image = -1, int selImage = -1, | |
265 | wxTreeItemData *data = NULL) | |
266 | { | |
267 | return DoInsertItem(parent, pos, text, image, selImage, data); | |
268 | } | |
269 | ||
270 | // insert a new item in as the last child of the parent | |
271 | wxTreeItemId AppendItem(const wxTreeItemId& parent, | |
272 | const wxString& text, | |
273 | int image = -1, int selImage = -1, | |
274 | wxTreeItemData *data = NULL) | |
275 | { | |
276 | return DoInsertItem(parent, (size_t)-1, text, image, selImage, data); | |
277 | } | |
278 | ||
279 | // delete this item and associated data if any | |
280 | virtual void Delete(const wxTreeItemId& item) = 0; | |
281 | // delete all children (but don't delete the item itself) | |
282 | // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events | |
283 | virtual void DeleteChildren(const wxTreeItemId& item) = 0; | |
284 | // delete all items from the tree | |
285 | // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events | |
286 | virtual void DeleteAllItems() = 0; | |
287 | ||
288 | // expand this item | |
289 | virtual void Expand(const wxTreeItemId& item) = 0; | |
290 | // collapse the item without removing its children | |
291 | virtual void Collapse(const wxTreeItemId& item) = 0; | |
292 | // collapse the item and remove all children | |
293 | virtual void CollapseAndReset(const wxTreeItemId& item) = 0; | |
294 | // toggles the current state | |
295 | virtual void Toggle(const wxTreeItemId& item) = 0; | |
296 | ||
297 | // remove the selection from currently selected item (if any) | |
298 | virtual void Unselect() = 0; | |
299 | // unselect all items (only makes sense for multiple selection control) | |
300 | virtual void UnselectAll() = 0; | |
301 | // select this item | |
302 | virtual void SelectItem(const wxTreeItemId& item, bool select = true) = 0; | |
303 | // unselect this item | |
304 | void UnselectItem(const wxTreeItemId& item) { SelectItem(item, false); } | |
305 | // toggle item selection | |
306 | void ToggleItemSelection(const wxTreeItemId& item) | |
307 | { | |
308 | SelectItem(item, !IsSelected(item)); | |
309 | } | |
310 | ||
311 | // make sure this item is visible (expanding the parent item and/or | |
312 | // scrolling to this item if necessary) | |
313 | virtual void EnsureVisible(const wxTreeItemId& item) = 0; | |
314 | // scroll to this item (but don't expand its parent) | |
315 | virtual void ScrollTo(const wxTreeItemId& item) = 0; | |
316 | ||
317 | // start editing the item label: this (temporarily) replaces the item | |
318 | // with a one line edit control. The item will be selected if it hadn't | |
319 | // been before. textCtrlClass parameter allows you to create an edit | |
320 | // control of arbitrary user-defined class deriving from wxTextCtrl. | |
321 | virtual wxTextCtrl *EditLabel(const wxTreeItemId& item, | |
322 | wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl)) = 0; | |
323 | // returns the same pointer as StartEdit() if the item is being edited, | |
324 | // NULL otherwise (it's assumed that no more than one item may be | |
325 | // edited simultaneously) | |
326 | virtual wxTextCtrl *GetEditControl() const = 0; | |
327 | // end editing and accept or discard the changes to item label | |
328 | virtual void EndEditLabel(const wxTreeItemId& item, | |
329 | bool discardChanges = false) = 0; | |
330 | ||
331 | // sorting | |
332 | // ------- | |
333 | ||
334 | // this function is called to compare 2 items and should return -1, 0 | |
335 | // or +1 if the first item is less than, equal to or greater than the | |
336 | // second one. The base class version performs alphabetic comparaison | |
337 | // of item labels (GetText) | |
338 | virtual int OnCompareItems(const wxTreeItemId& item1, | |
339 | const wxTreeItemId& item2) | |
340 | { | |
341 | return wxStrcmp(GetItemText(item1), GetItemText(item2)); | |
342 | } | |
343 | ||
344 | // sort the children of this item using OnCompareItems | |
345 | // | |
346 | // NB: this function is not reentrant and not MT-safe (FIXME)! | |
347 | virtual void SortChildren(const wxTreeItemId& item) = 0; | |
348 | ||
349 | // items geometry | |
350 | // -------------- | |
351 | ||
352 | // determine to which item (if any) belongs the given point (the | |
353 | // coordinates specified are relative to the client area of tree ctrl) | |
354 | // and, in the second variant, fill the flags parameter with a bitmask | |
355 | // of wxTREE_HITTEST_xxx constants. | |
356 | wxTreeItemId HitTest(const wxPoint& point) | |
22574b4a | 357 | { int dummy; return DoTreeHitTest(point, dummy); } |
8cee4a30 | 358 | wxTreeItemId HitTest(const wxPoint& point, int& flags) |
22574b4a | 359 | { return DoTreeHitTest(point, flags); } |
8cee4a30 VZ |
360 | |
361 | // get the bounding rectangle of the item (or of its label only) | |
362 | virtual bool GetBoundingRect(const wxTreeItemId& item, | |
363 | wxRect& rect, | |
364 | bool textOnly = false) const = 0; | |
365 | ||
366 | ||
367 | // implementation | |
368 | // -------------- | |
369 | ||
370 | virtual bool ShouldInheritColours() const { return false; } | |
371 | ||
7c384067 JS |
372 | // hint whether to calculate best size quickly or accurately |
373 | void SetQuickBestSize(bool q) { m_quickBestSize = q; } | |
374 | bool GetQuickBestSize() const { return m_quickBestSize; } | |
375 | ||
8cee4a30 VZ |
376 | protected: |
377 | virtual wxSize DoGetBestSize() const; | |
378 | ||
379 | // common part of Append/Prepend/InsertItem() | |
380 | // | |
381 | // pos is the position at which to insert the item or (size_t)-1 to append | |
382 | // it to the end | |
383 | virtual wxTreeItemId DoInsertItem(const wxTreeItemId& parent, | |
384 | size_t pos, | |
385 | const wxString& text, | |
386 | int image, int selImage, | |
387 | wxTreeItemData *data) = 0; | |
388 | ||
389 | // and this function implements overloaded InsertItem() taking wxTreeItemId | |
390 | // (it can't be called InsertItem() as we'd have virtual function hiding | |
391 | // problem in derived classes then) | |
392 | virtual wxTreeItemId DoInsertAfter(const wxTreeItemId& parent, | |
393 | const wxTreeItemId& idPrevious, | |
394 | const wxString& text, | |
395 | int image = -1, int selImage = -1, | |
396 | wxTreeItemData *data = NULL) = 0; | |
397 | ||
398 | // real HitTest() implementation: again, can't be called just HitTest() | |
399 | // because it's overloaded and so the non-virtual overload would be hidden | |
22574b4a VZ |
400 | // (and can't be called DoHitTest() because this is already in wxWindow) |
401 | virtual wxTreeItemId DoTreeHitTest(const wxPoint& point, int& flags) = 0; | |
8cee4a30 VZ |
402 | |
403 | ||
404 | wxImageList *m_imageListNormal, // images for tree elements | |
405 | *m_imageListState; // special images for app defined states | |
406 | bool m_ownsImageListNormal, | |
407 | m_ownsImageListState; | |
408 | ||
409 | // spacing between left border and the text | |
b220b1a2 | 410 | unsigned int m_spacing; |
8cee4a30 | 411 | |
7c384067 JS |
412 | // whether full or quick calculation is done in DoGetBestSize |
413 | bool m_quickBestSize; | |
414 | ||
8cee4a30 VZ |
415 | |
416 | DECLARE_NO_COPY_CLASS(wxTreeCtrlBase) | |
417 | }; | |
418 | ||
c193b707 VZ |
419 | // ---------------------------------------------------------------------------- |
420 | // include the platform-dependent wxTreeCtrl class | |
421 | // ---------------------------------------------------------------------------- | |
422 | ||
3e6e2754 JS |
423 | #if defined(__WXUNIVERSAL__) |
424 | #include "wx/generic/treectlg.h" | |
4055ed82 | 425 | #elif defined(__WXPALMOS__) |
b220b1a2 | 426 | #include "wx/palmos/treectrl.h" |
3e6e2754 | 427 | #elif defined(__WXMSW__) |
b220b1a2 | 428 | #include "wx/msw/treectrl.h" |
2049ba38 | 429 | #elif defined(__WXMOTIF__) |
484523cf | 430 | #include "wx/generic/treectlg.h" |
2049ba38 | 431 | #elif defined(__WXGTK__) |
484523cf | 432 | #include "wx/generic/treectlg.h" |
34138703 | 433 | #elif defined(__WXMAC__) |
484523cf | 434 | #include "wx/generic/treectlg.h" |
8d0eaedd DE |
435 | #elif defined(__WXCOCOA__) |
436 | #include "wx/generic/treectlg.h" | |
1777b9bb | 437 | #elif defined(__WXPM__) |
dd133a20 | 438 | #include "wx/generic/treectlg.h" |
c801d85f KB |
439 | #endif |
440 | ||
b220b1a2 | 441 | #endif // wxUSE_TREECTRL |
aad0fe4b | 442 | |
b220b1a2 | 443 | #endif // _WX_TREECTRL_H_BASE_ |