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 CLASSINFO macro
27 class WXDLLEXPORT wxImageList
;
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 class WXDLLEXPORT wxTreeCtrlBase
: public wxControl
39 m_imageListState
= NULL
;
40 m_ownsImageListNormal
=
41 m_ownsImageListState
= false;
47 virtual ~wxTreeCtrlBase();
52 // get the total number of items in the control
53 virtual size_t GetCount() const = 0;
55 // indent is the number of pixels the children are indented relative to
56 // the parents position. SetIndent() also redraws the control
58 virtual unsigned int GetIndent() const = 0;
59 virtual void SetIndent(unsigned int indent
) = 0;
61 // spacing is the number of pixels between the start and the Text
62 // (has no effect under wxMSW)
63 unsigned int GetSpacing() const { return m_spacing
; }
64 void SetSpacing(unsigned int spacing
) { m_spacing
= spacing
; }
66 // image list: these functions allow to associate an image list with
67 // the control and retrieve it. Note that the control does _not_ delete
68 // the associated image list when it's deleted in order to allow image
69 // lists to be shared between different controls.
71 // The normal image list is for the icons which correspond to the
72 // normal tree item state (whether it is selected or not).
73 // Additionally, the application might choose to show a state icon
74 // which corresponds to an app-defined item state (for example,
75 // checked/unchecked) which are taken from the state image list.
76 wxImageList
*GetImageList() const { return m_imageListNormal
; }
77 wxImageList
*GetStateImageList() const { return m_imageListState
; }
79 virtual void SetImageList(wxImageList
*imageList
) = 0;
80 virtual void SetStateImageList(wxImageList
*imageList
) = 0;
81 void AssignImageList(wxImageList
*imageList
)
83 SetImageList(imageList
);
84 m_ownsImageListNormal
= true;
86 void AssignStateImageList(wxImageList
*imageList
)
88 SetStateImageList(imageList
);
89 m_ownsImageListState
= true;
93 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
94 // member functions of wxTreeItem because they must know the tree the item
95 // belongs to for Windows implementation and storing the pointer to
96 // wxTreeCtrl in each wxTreeItem is just too much waste.
101 // retrieve items label
102 virtual wxString
GetItemText(const wxTreeItemId
& item
) const = 0;
103 // get one of the images associated with the item (normal by default)
104 virtual int GetItemImage(const wxTreeItemId
& item
,
105 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const = 0;
106 // get the data associated with the item
107 virtual wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const = 0;
109 // get the item's text colour
110 virtual wxColour
GetItemTextColour(const wxTreeItemId
& item
) const = 0;
112 // get the item's background colour
113 virtual wxColour
GetItemBackgroundColour(const wxTreeItemId
& item
) const = 0;
115 // get the item's font
116 virtual wxFont
GetItemFont(const wxTreeItemId
& item
) const = 0;
122 virtual void SetItemText(const wxTreeItemId
& item
, const wxString
& text
) = 0;
123 // get one of the images associated with the item (normal by default)
124 virtual void SetItemImage(const wxTreeItemId
& item
,
126 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) = 0;
127 // associate some data with the item
128 virtual void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
) = 0;
130 // force appearance of [+] button near the item. This is useful to
131 // allow the user to expand the items which don't have any children now
132 // - but instead add them only when needed, thus minimizing memory
133 // usage and loading time.
134 virtual void SetItemHasChildren(const wxTreeItemId
& item
,
135 bool has
= true) = 0;
137 // the item will be shown in bold
138 virtual void SetItemBold(const wxTreeItemId
& item
, bool bold
= true) = 0;
140 // the item will be shown with a drop highlight
141 virtual void SetItemDropHighlight(const wxTreeItemId
& item
,
142 bool highlight
= true) = 0;
144 // set the items text colour
145 virtual void SetItemTextColour(const wxTreeItemId
& item
,
146 const wxColour
& col
) = 0;
148 // set the items background colour
149 virtual void SetItemBackgroundColour(const wxTreeItemId
& item
,
150 const wxColour
& col
) = 0;
152 // set the items font (should be of the same height for all items)
153 virtual void SetItemFont(const wxTreeItemId
& item
,
154 const wxFont
& font
) = 0;
156 // item status inquiries
157 // ---------------------
159 // is the item visible (it might be outside the view or not expanded)?
160 virtual bool IsVisible(const wxTreeItemId
& item
) const = 0;
161 // does the item has any children?
162 virtual bool ItemHasChildren(const wxTreeItemId
& item
) const = 0;
164 bool HasChildren(const wxTreeItemId
& item
) const
165 { return ItemHasChildren(item
); }
166 // is the item expanded (only makes sense if HasChildren())?
167 virtual bool IsExpanded(const wxTreeItemId
& item
) const = 0;
168 // is this item currently selected (the same as has focus)?
169 virtual bool IsSelected(const wxTreeItemId
& item
) const = 0;
170 // is item text in bold font?
171 virtual bool IsBold(const wxTreeItemId
& item
) const = 0;
173 // number of children
174 // ------------------
176 // if 'recursively' is false, only immediate children count, otherwise
177 // the returned number is the number of all items in this branch
178 virtual size_t GetChildrenCount(const wxTreeItemId
& item
,
179 bool recursively
= true) const = 0;
184 // wxTreeItemId.IsOk() will return false if there is no such item
186 // get the root tree item
187 virtual wxTreeItemId
GetRootItem() const = 0;
189 // get the item currently selected (may return NULL if no selection)
190 virtual wxTreeItemId
GetSelection() const = 0;
192 // get the items currently selected, return the number of such item
194 // NB: this operation is expensive and can take a long time for a
195 // control with a lot of items (~ O(number of items)).
196 virtual size_t GetSelections(wxArrayTreeItemIds
& selections
) const = 0;
198 // get the parent of this item (may return NULL if root)
199 virtual wxTreeItemId
GetItemParent(const wxTreeItemId
& item
) const = 0;
201 // for this enumeration function you must pass in a "cookie" parameter
202 // which is opaque for the application but is necessary for the library
203 // to make these functions reentrant (i.e. allow more than one
204 // enumeration on one and the same object simultaneously). Of course,
205 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
208 // get the first child of this item
209 virtual wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
210 wxTreeItemIdValue
& cookie
) const = 0;
211 // get the next child
212 virtual wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
213 wxTreeItemIdValue
& cookie
) const = 0;
214 // get the last child of this item - this method doesn't use cookies
215 virtual wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const = 0;
217 // get the next sibling of this item
218 virtual wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const = 0;
219 // get the previous sibling
220 virtual wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const = 0;
222 // get first visible item
223 virtual wxTreeItemId
GetFirstVisibleItem() const = 0;
224 // get the next visible item: item must be visible itself!
225 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
226 virtual wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const = 0;
227 // get the previous visible item: item must be visible itself!
228 virtual wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const = 0;
233 // add the root node to the tree
234 virtual wxTreeItemId
AddRoot(const wxString
& text
,
235 int image
= -1, int selImage
= -1,
236 wxTreeItemData
*data
= NULL
) = 0;
238 // insert a new item in as the first child of the parent
239 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
240 const wxString
& text
,
241 int image
= -1, int selImage
= -1,
242 wxTreeItemData
*data
= NULL
)
244 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
247 // insert a new item after a given one
248 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
249 const wxTreeItemId
& idPrevious
,
250 const wxString
& text
,
251 int image
= -1, int selImage
= -1,
252 wxTreeItemData
*data
= NULL
)
254 return DoInsertAfter(parent
, idPrevious
, text
, image
, selImage
, data
);
257 // insert a new item before the one with the given index
258 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
260 const wxString
& text
,
261 int image
= -1, int selImage
= -1,
262 wxTreeItemData
*data
= NULL
)
264 return DoInsertItem(parent
, pos
, text
, image
, selImage
, data
);
267 // insert a new item in as the last child of the parent
268 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
269 const wxString
& text
,
270 int image
= -1, int selImage
= -1,
271 wxTreeItemData
*data
= NULL
)
273 return DoInsertItem(parent
, (size_t)-1, text
, image
, selImage
, data
);
276 // delete this item and associated data if any
277 virtual void Delete(const wxTreeItemId
& item
) = 0;
278 // delete all children (but don't delete the item itself)
279 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
280 virtual void DeleteChildren(const wxTreeItemId
& item
) = 0;
281 // delete all items from the tree
282 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
283 virtual void DeleteAllItems() = 0;
286 virtual void Expand(const wxTreeItemId
& item
) = 0;
287 // collapse the item without removing its children
288 virtual void Collapse(const wxTreeItemId
& item
) = 0;
289 // collapse the item and remove all children
290 virtual void CollapseAndReset(const wxTreeItemId
& item
) = 0;
291 // toggles the current state
292 virtual void Toggle(const wxTreeItemId
& item
) = 0;
294 // remove the selection from currently selected item (if any)
295 virtual void Unselect() = 0;
296 // unselect all items (only makes sense for multiple selection control)
297 virtual void UnselectAll() = 0;
299 virtual void SelectItem(const wxTreeItemId
& item
, bool select
= true) = 0;
300 // unselect this item
301 void UnselectItem(const wxTreeItemId
& item
) { SelectItem(item
, false); }
302 // toggle item selection
303 void ToggleItemSelection(const wxTreeItemId
& item
)
305 SelectItem(item
, !IsSelected(item
));
308 // make sure this item is visible (expanding the parent item and/or
309 // scrolling to this item if necessary)
310 virtual void EnsureVisible(const wxTreeItemId
& item
) = 0;
311 // scroll to this item (but don't expand its parent)
312 virtual void ScrollTo(const wxTreeItemId
& item
) = 0;
314 // start editing the item label: this (temporarily) replaces the item
315 // with a one line edit control. The item will be selected if it hadn't
316 // been before. textCtrlClass parameter allows you to create an edit
317 // control of arbitrary user-defined class deriving from wxTextCtrl.
318 virtual wxTextCtrl
*EditLabel(const wxTreeItemId
& item
,
319 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
)) = 0;
320 // returns the same pointer as StartEdit() if the item is being edited,
321 // NULL otherwise (it's assumed that no more than one item may be
322 // edited simultaneously)
323 virtual wxTextCtrl
*GetEditControl() const = 0;
324 // end editing and accept or discard the changes to item label
325 virtual void EndEditLabel(const wxTreeItemId
& item
,
326 bool discardChanges
= false) = 0;
331 // this function is called to compare 2 items and should return -1, 0
332 // or +1 if the first item is less than, equal to or greater than the
333 // second one. The base class version performs alphabetic comparaison
334 // of item labels (GetText)
335 virtual int OnCompareItems(const wxTreeItemId
& item1
,
336 const wxTreeItemId
& item2
)
338 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
341 // sort the children of this item using OnCompareItems
343 // NB: this function is not reentrant and not MT-safe (FIXME)!
344 virtual void SortChildren(const wxTreeItemId
& item
) = 0;
349 // determine to which item (if any) belongs the given point (the
350 // coordinates specified are relative to the client area of tree ctrl)
351 // and, in the second variant, fill the flags parameter with a bitmask
352 // of wxTREE_HITTEST_xxx constants.
353 wxTreeItemId
HitTest(const wxPoint
& point
)
354 { int dummy
; return DoTreeHitTest(point
, dummy
); }
355 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
)
356 { return DoTreeHitTest(point
, flags
); }
358 // get the bounding rectangle of the item (or of its label only)
359 virtual bool GetBoundingRect(const wxTreeItemId
& item
,
361 bool textOnly
= false) const = 0;
367 virtual bool ShouldInheritColours() const { return false; }
370 virtual wxSize
DoGetBestSize() const;
372 // common part of Append/Prepend/InsertItem()
374 // pos is the position at which to insert the item or (size_t)-1 to append
376 virtual wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
378 const wxString
& text
,
379 int image
, int selImage
,
380 wxTreeItemData
*data
) = 0;
382 // and this function implements overloaded InsertItem() taking wxTreeItemId
383 // (it can't be called InsertItem() as we'd have virtual function hiding
384 // problem in derived classes then)
385 virtual wxTreeItemId
DoInsertAfter(const wxTreeItemId
& parent
,
386 const wxTreeItemId
& idPrevious
,
387 const wxString
& text
,
388 int image
= -1, int selImage
= -1,
389 wxTreeItemData
*data
= NULL
) = 0;
391 // real HitTest() implementation: again, can't be called just HitTest()
392 // because it's overloaded and so the non-virtual overload would be hidden
393 // (and can't be called DoHitTest() because this is already in wxWindow)
394 virtual wxTreeItemId
DoTreeHitTest(const wxPoint
& point
, int& flags
) = 0;
397 wxImageList
*m_imageListNormal
, // images for tree elements
398 *m_imageListState
; // special images for app defined states
399 bool m_ownsImageListNormal
,
400 m_ownsImageListState
;
402 // spacing between left border and the text
403 unsigned int m_spacing
;
406 DECLARE_NO_COPY_CLASS(wxTreeCtrlBase
)
409 // ----------------------------------------------------------------------------
410 // include the platform-dependent wxTreeCtrl class
411 // ----------------------------------------------------------------------------
413 #if defined(__WXUNIVERSAL__)
414 #include "wx/generic/treectlg.h"
415 #elif defined(__WXPALMOS__)
416 #include "wx/palmos/treectrl.h"
417 #elif defined(__WXMSW__)
418 #include "wx/msw/treectrl.h"
419 #elif defined(__WXMOTIF__)
420 #include "wx/generic/treectlg.h"
421 #elif defined(__WXGTK__)
422 #include "wx/generic/treectlg.h"
423 #elif defined(__WXMAC__)
424 #include "wx/generic/treectlg.h"
425 #elif defined(__WXCOCOA__)
426 #include "wx/generic/treectlg.h"
427 #elif defined(__WXPM__)
428 #include "wx/generic/treectlg.h"
431 #endif // wxUSE_TREECTRL
433 #endif // _WX_TREECTRL_H_BASE_