1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl base header
4 // Author: Karsten Ballueder
7 // Copyright: (c) Karsten Ballueder
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_TREECTRL_H_BASE_
13 #define _WX_TREECTRL_H_BASE_
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
23 #include "wx/control.h"
24 #include "wx/treebase.h"
25 #include "wx/textctrl.h" // wxTextCtrl::ms_classinfo used through wxCLASSINFO macro
27 class WXDLLIMPEXP_FWD_CORE wxImageList
;
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 class WXDLLIMPEXP_CORE wxTreeCtrlBase
: public wxControl
37 virtual ~wxTreeCtrlBase();
42 // get the total number of items in the control
43 virtual unsigned int GetCount() const = 0;
45 // indent is the number of pixels the children are indented relative to
46 // the parents position. SetIndent() also redraws the control
48 virtual unsigned int GetIndent() const = 0;
49 virtual void SetIndent(unsigned int indent
) = 0;
51 // spacing is the number of pixels between the start and the Text
52 // (has no effect under wxMSW)
53 unsigned int GetSpacing() const { return m_spacing
; }
54 void SetSpacing(unsigned int spacing
) { m_spacing
= spacing
; }
56 // image list: these functions allow to associate an image list with
57 // the control and retrieve it. Note that the control does _not_ delete
58 // the associated image list when it's deleted in order to allow image
59 // lists to be shared between different controls.
61 // The normal image list is for the icons which correspond to the
62 // normal tree item state (whether it is selected or not).
63 // Additionally, the application might choose to show a state icon
64 // which corresponds to an app-defined item state (for example,
65 // checked/unchecked) which are taken from the state image list.
66 wxImageList
*GetImageList() const { return m_imageListNormal
; }
67 wxImageList
*GetStateImageList() const { return m_imageListState
; }
69 virtual void SetImageList(wxImageList
*imageList
) = 0;
70 virtual void SetStateImageList(wxImageList
*imageList
) = 0;
71 void AssignImageList(wxImageList
*imageList
)
73 SetImageList(imageList
);
74 m_ownsImageListNormal
= true;
76 void AssignStateImageList(wxImageList
*imageList
)
78 SetStateImageList(imageList
);
79 m_ownsImageListState
= true;
83 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
84 // member functions of wxTreeItem because they must know the tree the item
85 // belongs to for Windows implementation and storing the pointer to
86 // wxTreeCtrl in each wxTreeItem is just too much waste.
91 // retrieve items label
92 virtual wxString
GetItemText(const wxTreeItemId
& item
) const = 0;
93 // get one of the images associated with the item (normal by default)
94 virtual int GetItemImage(const wxTreeItemId
& item
,
95 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const = 0;
96 // get the data associated with the item
97 virtual wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const = 0;
99 // get the item's text colour
100 virtual wxColour
GetItemTextColour(const wxTreeItemId
& item
) const = 0;
102 // get the item's background colour
103 virtual wxColour
GetItemBackgroundColour(const wxTreeItemId
& item
) const = 0;
105 // get the item's font
106 virtual wxFont
GetItemFont(const wxTreeItemId
& item
) const = 0;
108 // get the items state
109 int GetItemState(const wxTreeItemId
& item
) const
111 return DoGetItemState(item
);
118 virtual void SetItemText(const wxTreeItemId
& item
, const wxString
& text
) = 0;
119 // set one of the images associated with the item (normal by default)
120 virtual void SetItemImage(const wxTreeItemId
& item
,
122 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) = 0;
123 // associate some data with the item
124 virtual void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
) = 0;
126 // force appearance of [+] button near the item. This is useful to
127 // allow the user to expand the items which don't have any children now
128 // - but instead add them only when needed, thus minimizing memory
129 // usage and loading time.
130 virtual void SetItemHasChildren(const wxTreeItemId
& item
,
131 bool has
= true) = 0;
133 // the item will be shown in bold
134 virtual void SetItemBold(const wxTreeItemId
& item
, bool bold
= true) = 0;
136 // the item will be shown with a drop highlight
137 virtual void SetItemDropHighlight(const wxTreeItemId
& item
,
138 bool highlight
= true) = 0;
140 // set the items text colour
141 virtual void SetItemTextColour(const wxTreeItemId
& item
,
142 const wxColour
& col
) = 0;
144 // set the items background colour
145 virtual void SetItemBackgroundColour(const wxTreeItemId
& item
,
146 const wxColour
& col
) = 0;
148 // set the items font (should be of the same height for all items)
149 virtual void SetItemFont(const wxTreeItemId
& item
,
150 const wxFont
& font
) = 0;
152 // set the items state (special state values: wxTREE_ITEMSTATE_NONE/NEXT/PREV)
153 void SetItemState(const wxTreeItemId
& item
, int state
);
155 // item status inquiries
156 // ---------------------
158 // is the item visible (it might be outside the view or not expanded)?
159 virtual bool IsVisible(const wxTreeItemId
& item
) const = 0;
160 // does the item has any children?
161 virtual bool ItemHasChildren(const wxTreeItemId
& item
) const = 0;
163 bool HasChildren(const wxTreeItemId
& item
) const
164 { return ItemHasChildren(item
); }
165 // is the item expanded (only makes sense if HasChildren())?
166 virtual bool IsExpanded(const wxTreeItemId
& item
) const = 0;
167 // is this item currently selected (the same as has focus)?
168 virtual bool IsSelected(const wxTreeItemId
& item
) const = 0;
169 // is item text in bold font?
170 virtual bool IsBold(const wxTreeItemId
& item
) const = 0;
171 // is the control empty?
172 bool IsEmpty() const;
175 // number of children
176 // ------------------
178 // if 'recursively' is false, only immediate children count, otherwise
179 // the returned number is the number of all items in this branch
180 virtual size_t GetChildrenCount(const wxTreeItemId
& item
,
181 bool recursively
= true) const = 0;
186 // wxTreeItemId.IsOk() will return false if there is no such item
188 // get the root tree item
189 virtual wxTreeItemId
GetRootItem() const = 0;
191 // get the item currently selected (may return NULL if no selection)
192 virtual wxTreeItemId
GetSelection() const = 0;
194 // get the items currently selected, return the number of such item
196 // NB: this operation is expensive and can take a long time for a
197 // control with a lot of items (~ O(number of items)).
198 virtual size_t GetSelections(wxArrayTreeItemIds
& selections
) const = 0;
200 // get the last item to be clicked when the control has wxTR_MULTIPLE
201 // equivalent to GetSelection() if not wxTR_MULTIPLE
202 virtual wxTreeItemId
GetFocusedItem() const = 0;
205 // Clears the currently focused item
206 virtual void ClearFocusedItem() = 0;
207 // Sets the currently focused item. Item should be valid
208 virtual void SetFocusedItem(const wxTreeItemId
& item
) = 0;
211 // get the parent of this item (may return NULL if root)
212 virtual wxTreeItemId
GetItemParent(const wxTreeItemId
& item
) const = 0;
214 // for this enumeration function you must pass in a "cookie" parameter
215 // which is opaque for the application but is necessary for the library
216 // to make these functions reentrant (i.e. allow more than one
217 // enumeration on one and the same object simultaneously). Of course,
218 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
221 // get the first child of this item
222 virtual wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
223 wxTreeItemIdValue
& cookie
) const = 0;
224 // get the next child
225 virtual wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
226 wxTreeItemIdValue
& cookie
) const = 0;
227 // get the last child of this item - this method doesn't use cookies
228 virtual wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const = 0;
230 // get the next sibling of this item
231 virtual wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const = 0;
232 // get the previous sibling
233 virtual wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const = 0;
235 // get first visible item
236 virtual wxTreeItemId
GetFirstVisibleItem() const = 0;
237 // get the next visible item: item must be visible itself!
238 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
239 virtual wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const = 0;
240 // get the previous visible item: item must be visible itself!
241 virtual wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const = 0;
246 // add the root node to the tree
247 virtual wxTreeItemId
AddRoot(const wxString
& text
,
248 int image
= -1, int selImage
= -1,
249 wxTreeItemData
*data
= NULL
) = 0;
251 // insert a new item in as the first child of the parent
252 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
253 const wxString
& text
,
254 int image
= -1, int selImage
= -1,
255 wxTreeItemData
*data
= NULL
)
257 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
260 // insert a new item after a given one
261 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
262 const wxTreeItemId
& idPrevious
,
263 const wxString
& text
,
264 int image
= -1, int selImage
= -1,
265 wxTreeItemData
*data
= NULL
)
267 return DoInsertAfter(parent
, idPrevious
, text
, image
, selImage
, data
);
270 // insert a new item before the one with the given index
271 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
273 const wxString
& text
,
274 int image
= -1, int selImage
= -1,
275 wxTreeItemData
*data
= NULL
)
277 return DoInsertItem(parent
, pos
, text
, image
, selImage
, data
);
280 // insert a new item in as the last child of the parent
281 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
282 const wxString
& text
,
283 int image
= -1, int selImage
= -1,
284 wxTreeItemData
*data
= NULL
)
286 return DoInsertItem(parent
, (size_t)-1, text
, image
, selImage
, data
);
289 // delete this item and associated data if any
290 virtual void Delete(const wxTreeItemId
& item
) = 0;
291 // delete all children (but don't delete the item itself)
292 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
293 virtual void DeleteChildren(const wxTreeItemId
& item
) = 0;
294 // delete all items from the tree
295 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
296 virtual void DeleteAllItems() = 0;
299 virtual void Expand(const wxTreeItemId
& item
) = 0;
300 // expand the item and all its children recursively
301 void ExpandAllChildren(const wxTreeItemId
& item
);
304 // collapse the item without removing its children
305 virtual void Collapse(const wxTreeItemId
& item
) = 0;
306 // collapse the item and all its children
307 void CollapseAllChildren(const wxTreeItemId
& item
);
308 // collapse all items
310 // collapse the item and remove all children
311 virtual void CollapseAndReset(const wxTreeItemId
& item
) = 0;
312 // toggles the current state
313 virtual void Toggle(const wxTreeItemId
& item
) = 0;
315 // remove the selection from currently selected item (if any)
316 virtual void Unselect() = 0;
317 // unselect all items (only makes sense for multiple selection control)
318 virtual void UnselectAll() = 0;
320 virtual void SelectItem(const wxTreeItemId
& item
, bool select
= true) = 0;
321 // selects all (direct) children for given parent (only for
322 // multiselection controls)
323 virtual void SelectChildren(const wxTreeItemId
& parent
) = 0;
324 // unselect this item
325 void UnselectItem(const wxTreeItemId
& item
) { SelectItem(item
, false); }
326 // toggle item selection
327 void ToggleItemSelection(const wxTreeItemId
& item
)
329 SelectItem(item
, !IsSelected(item
));
332 // make sure this item is visible (expanding the parent item and/or
333 // scrolling to this item if necessary)
334 virtual void EnsureVisible(const wxTreeItemId
& item
) = 0;
335 // scroll to this item (but don't expand its parent)
336 virtual void ScrollTo(const wxTreeItemId
& item
) = 0;
338 // start editing the item label: this (temporarily) replaces the item
339 // with a one line edit control. The item will be selected if it hadn't
340 // been before. textCtrlClass parameter allows you to create an edit
341 // control of arbitrary user-defined class deriving from wxTextCtrl.
342 virtual wxTextCtrl
*EditLabel(const wxTreeItemId
& item
,
343 wxClassInfo
* textCtrlClass
= wxCLASSINFO(wxTextCtrl
)) = 0;
344 // returns the same pointer as StartEdit() if the item is being edited,
345 // NULL otherwise (it's assumed that no more than one item may be
346 // edited simultaneously)
347 virtual wxTextCtrl
*GetEditControl() const = 0;
348 // end editing and accept or discard the changes to item label
349 virtual void EndEditLabel(const wxTreeItemId
& item
,
350 bool discardChanges
= false) = 0;
355 // this function is called to compare 2 items and should return -1, 0
356 // or +1 if the first item is less than, equal to or greater than the
357 // second one. The base class version performs alphabetic comparaison
358 // of item labels (GetText)
359 virtual int OnCompareItems(const wxTreeItemId
& item1
,
360 const wxTreeItemId
& item2
)
362 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
365 // sort the children of this item using OnCompareItems
367 // NB: this function is not reentrant and not MT-safe (FIXME)!
368 virtual void SortChildren(const wxTreeItemId
& item
) = 0;
373 // determine to which item (if any) belongs the given point (the
374 // coordinates specified are relative to the client area of tree ctrl)
375 // and, in the second variant, fill the flags parameter with a bitmask
376 // of wxTREE_HITTEST_xxx constants.
377 wxTreeItemId
HitTest(const wxPoint
& point
) const
378 { int dummy
; return DoTreeHitTest(point
, dummy
); }
379 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
) const
380 { return DoTreeHitTest(point
, flags
); }
382 // get the bounding rectangle of the item (or of its label only)
383 virtual bool GetBoundingRect(const wxTreeItemId
& item
,
385 bool textOnly
= false) const = 0;
391 virtual bool ShouldInheritColours() const { return false; }
393 // hint whether to calculate best size quickly or accurately
394 void SetQuickBestSize(bool q
) { m_quickBestSize
= q
; }
395 bool GetQuickBestSize() const { return m_quickBestSize
; }
398 virtual wxSize
DoGetBestSize() const;
400 // common part of Get/SetItemState()
401 virtual int DoGetItemState(const wxTreeItemId
& item
) const = 0;
402 virtual void DoSetItemState(const wxTreeItemId
& item
, int state
) = 0;
404 // common part of Append/Prepend/InsertItem()
406 // pos is the position at which to insert the item or (size_t)-1 to append
408 virtual wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
410 const wxString
& text
,
411 int image
, int selImage
,
412 wxTreeItemData
*data
) = 0;
414 // and this function implements overloaded InsertItem() taking wxTreeItemId
415 // (it can't be called InsertItem() as we'd have virtual function hiding
416 // problem in derived classes then)
417 virtual wxTreeItemId
DoInsertAfter(const wxTreeItemId
& parent
,
418 const wxTreeItemId
& idPrevious
,
419 const wxString
& text
,
420 int image
= -1, int selImage
= -1,
421 wxTreeItemData
*data
= NULL
) = 0;
423 // real HitTest() implementation: again, can't be called just HitTest()
424 // because it's overloaded and so the non-virtual overload would be hidden
425 // (and can't be called DoHitTest() because this is already in wxWindow)
426 virtual wxTreeItemId
DoTreeHitTest(const wxPoint
& point
,
427 int& flags
) const = 0;
430 wxImageList
*m_imageListNormal
, // images for tree elements
431 *m_imageListState
; // special images for app defined states
432 bool m_ownsImageListNormal
,
433 m_ownsImageListState
;
435 // spacing between left border and the text
436 unsigned int m_spacing
;
438 // whether full or quick calculation is done in DoGetBestSize
439 bool m_quickBestSize
;
443 // Intercept Escape and Return keys to ensure that our in-place edit
444 // control always gets them before they're used for dialog navigation or
446 void OnCharHook(wxKeyEvent
& event
);
449 wxDECLARE_NO_COPY_CLASS(wxTreeCtrlBase
);
452 // ----------------------------------------------------------------------------
453 // include the platform-dependent wxTreeCtrl class
454 // ----------------------------------------------------------------------------
456 #if defined(__WXUNIVERSAL__)
457 #include "wx/generic/treectlg.h"
458 #elif defined(__WXMSW__)
459 #include "wx/msw/treectrl.h"
460 #elif defined(__WXMOTIF__)
461 #include "wx/generic/treectlg.h"
462 #elif defined(__WXGTK__)
463 #include "wx/generic/treectlg.h"
464 #elif defined(__WXMAC__)
465 #include "wx/generic/treectlg.h"
466 #elif defined(__WXCOCOA__)
467 #include "wx/generic/treectlg.h"
468 #elif defined(__WXPM__)
469 #include "wx/generic/treectlg.h"
472 #endif // wxUSE_TREECTRL
474 #endif // _WX_TREECTRL_H_BASE_