1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/palmos/treectrl.h
3 // Purpose: wxTreeCtrl class
4 // Author: William Osborne - minimal working wxPalmOS port
8 // Copyright: (c) William Osborne
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_TREECTRL_H_
13 #define _WX_TREECTRL_H_
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
20 #pragma interface "treectrl.h"
25 #include "wx/textctrl.h"
26 #include "wx/dynarray.h"
27 #include "wx/treebase.h"
28 #include "wx/hashmap.h"
31 class WXDLLEXPORT wxImageList
;
32 class WXDLLEXPORT wxDragImage
;
33 struct WXDLLEXPORT wxTreeViewItem
;
35 // NB: all the following flags are for compatbility only and will be removed in the
38 // flags for deprecated InsertItem() variant (their values are the same as of
39 // TVI_FIRST and TVI_LAST)
40 #define wxTREE_INSERT_FIRST 0xFFFF0001
41 #define wxTREE_INSERT_LAST 0xFFFF0002
43 // hash storing attributes for our items
44 WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr
*, wxMapTreeAttr
);
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 class WXDLLEXPORT wxTreeCtrl
: public wxControl
55 wxTreeCtrl() { Init(); }
57 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
58 const wxPoint
& pos
= wxDefaultPosition
,
59 const wxSize
& size
= wxDefaultSize
,
60 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
61 const wxValidator
& validator
= wxDefaultValidator
,
62 const wxString
& name
= wxTreeCtrlNameStr
)
64 Create(parent
, id
, pos
, size
, style
, validator
, name
);
67 virtual ~wxTreeCtrl();
69 bool Create(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
70 const wxPoint
& pos
= wxDefaultPosition
,
71 const wxSize
& size
= wxDefaultSize
,
72 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
73 const wxValidator
& validator
= wxDefaultValidator
,
74 const wxString
& name
= wxTreeCtrlNameStr
);
79 // get the total number of items in the control
80 size_t GetCount() const;
82 // indent is the number of pixels the children are indented relative to
83 // the parents position. SetIndent() also redraws the control
85 unsigned int GetIndent() const;
86 void SetIndent(unsigned int indent
);
88 // spacing is the number of pixels between the start and the Text
89 unsigned int GetSpacing() const { return 18; } // return wxGTK default
90 void SetSpacing(unsigned int WXUNUSED(spacing
)) { }
92 // image list: these functions allow to associate an image list with
93 // the control and retrieve it. Note that the control does _not_ delete
94 // the associated image list when it's deleted in order to allow image
95 // lists to be shared between different controls.
97 // The normal image list is for the icons which correspond to the
98 // normal tree item state (whether it is selected or not).
99 // Additionally, the application might choose to show a state icon
100 // which corresponds to an app-defined item state (for example,
101 // checked/unchecked) which are taken from the state image list.
102 wxImageList
*GetImageList() const;
103 wxImageList
*GetStateImageList() const;
105 void SetImageList(wxImageList
*imageList
);
106 void SetStateImageList(wxImageList
*imageList
);
107 void AssignImageList(wxImageList
*imageList
);
108 void AssignStateImageList(wxImageList
*imageList
);
110 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
111 // member functions of wxTreeItem because they must know the tree the item
112 // belongs to for Windows implementation and storing the pointer to
113 // wxTreeCtrl in each wxTreeItem is just too much waste.
118 // retrieve items label
119 wxString
GetItemText(const wxTreeItemId
& item
) const;
120 // get one of the images associated with the item (normal by default)
121 int GetItemImage(const wxTreeItemId
& item
,
122 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
123 // get the data associated with the item
124 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
126 // get the item's text colour
127 wxColour
GetItemTextColour(const wxTreeItemId
& item
) const;
129 // get the item's background colour
130 wxColour
GetItemBackgroundColour(const wxTreeItemId
& item
) const;
132 // get the item's font
133 wxFont
GetItemFont(const wxTreeItemId
& item
) const;
139 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
140 // get one of the images associated with the item (normal by default)
141 void SetItemImage(const wxTreeItemId
& item
, int image
,
142 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
143 // associate some data with the item
144 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
146 // force appearance of [+] button near the item. This is useful to
147 // allow the user to expand the items which don't have any children now
148 // - but instead add them only when needed, thus minimizing memory
149 // usage and loading time.
150 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= true);
152 // the item will be shown in bold
153 void SetItemBold(const wxTreeItemId
& item
, bool bold
= true);
155 // the item will be shown with a drop highlight
156 void SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
= true);
158 // set the items text colour
159 void SetItemTextColour(const wxTreeItemId
& item
, const wxColour
& col
);
161 // set the items background colour
162 void SetItemBackgroundColour(const wxTreeItemId
& item
, const wxColour
& col
);
164 // set the items font (should be of the same height for all items)
165 void SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
);
167 // item status inquiries
168 // ---------------------
170 // is the item visible (it might be outside the view or not expanded)?
171 bool IsVisible(const wxTreeItemId
& item
) const;
172 // does the item has any children?
173 bool ItemHasChildren(const wxTreeItemId
& item
) const;
174 // is the item expanded (only makes sense if HasChildren())?
175 bool IsExpanded(const wxTreeItemId
& item
) const;
176 // is this item currently selected (the same as has focus)?
177 bool IsSelected(const wxTreeItemId
& item
) const;
178 // is item text in bold font?
179 bool IsBold(const wxTreeItemId
& item
) const;
181 // number of children
182 // ------------------
184 // if 'recursively' is false, only immediate children count, otherwise
185 // the returned number is the number of all items in this branch
186 size_t GetChildrenCount(const wxTreeItemId
& item
,
187 bool recursively
= true) const;
192 // wxTreeItemId.IsOk() will return false if there is no such item
194 // get the root tree item
195 wxTreeItemId
GetRootItem() const;
197 // get the item currently selected (may return NULL if no selection)
198 wxTreeItemId
GetSelection() const;
200 // get the items currently selected, return the number of such item
202 // NB: this operation is expensive and can take a long time for a
203 // control with a lot of items (~ O(number of items)).
204 size_t GetSelections(wxArrayTreeItemIds
& selections
) const;
206 // get the parent of this item (may return NULL if root)
207 wxTreeItemId
GetItemParent(const wxTreeItemId
& item
) const;
209 // for this enumeration function you must pass in a "cookie" parameter
210 // which is opaque for the application but is necessary for the library
211 // to make these functions reentrant (i.e. allow more than one
212 // enumeration on one and the same object simultaneously). Of course,
213 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
216 // get the first child of this item
217 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
218 wxTreeItemIdValue
& cookie
) const;
219 // get the next child
220 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
221 wxTreeItemIdValue
& cookie
) const;
222 // get the last child of this item - this method doesn't use cookies
223 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
225 // get the next sibling of this item
226 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
227 // get the previous sibling
228 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
230 // get first visible item
231 wxTreeItemId
GetFirstVisibleItem() const;
232 // get the next visible item: item must be visible itself!
233 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
234 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
235 // get the previous visible item: item must be visible itself!
236 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
241 // add the root node to the tree
242 wxTreeItemId
AddRoot(const wxString
& text
,
243 int image
= -1, int selectedImage
= -1,
244 wxTreeItemData
*data
= NULL
);
246 // insert a new item in as the first child of the parent
247 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
248 const wxString
& text
,
249 int image
= -1, int selectedImage
= -1,
250 wxTreeItemData
*data
= NULL
);
252 // insert a new item after a given one
253 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
254 const wxTreeItemId
& idPrevious
,
255 const wxString
& text
,
256 int image
= -1, int selectedImage
= -1,
257 wxTreeItemData
*data
= NULL
);
259 // insert a new item before the one with the given index
260 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
262 const wxString
& text
,
263 int image
= -1, int selectedImage
= -1,
264 wxTreeItemData
*data
= NULL
);
266 // insert a new item in as the last child of the parent
267 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
268 const wxString
& text
,
269 int image
= -1, int selectedImage
= -1,
270 wxTreeItemData
*data
= NULL
);
272 // delete this item and associated data if any
273 void Delete(const wxTreeItemId
& item
);
274 // delete all children (but don't delete the item itself)
275 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
276 void DeleteChildren(const wxTreeItemId
& item
);
277 // delete all items from the tree
278 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
279 void DeleteAllItems();
282 void Expand(const wxTreeItemId
& item
);
283 // collapse the item without removing its children
284 void Collapse(const wxTreeItemId
& item
);
285 // collapse the item and remove all children
286 void CollapseAndReset(const wxTreeItemId
& item
);
287 // toggles the current state
288 void Toggle(const wxTreeItemId
& item
);
290 // remove the selection from currently selected item (if any)
292 // unselect all items (only makes sense for multiple selection control)
295 void SelectItem(const wxTreeItemId
& item
, bool select
= true);
296 // unselect this item
297 void UnselectItem(const wxTreeItemId
& item
);
298 // toggle item selection
299 void ToggleItemSelection(const wxTreeItemId
& item
);
301 // make sure this item is visible (expanding the parent item and/or
302 // scrolling to this item if necessary)
303 void EnsureVisible(const wxTreeItemId
& item
);
304 // scroll to this item (but don't expand its parent)
305 void ScrollTo(const wxTreeItemId
& item
);
307 // start editing the item label: this (temporarily) replaces the item
308 // with a one line edit control. The item will be selected if it hadn't
309 // been before. textCtrlClass parameter allows you to create an edit
310 // control of arbitrary user-defined class deriving from wxTextCtrl.
311 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
312 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
313 // returns the same pointer as StartEdit() if the item is being edited,
314 // NULL otherwise (it's assumed that no more than one item may be
315 // edited simultaneously)
316 wxTextCtrl
* GetEditControl() const;
317 // end editing and accept or discard the changes to item label
318 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= false);
321 // this function is called to compare 2 items and should return -1, 0
322 // or +1 if the first item is less than, equal to or greater than the
323 // second one. The base class version performs alphabetic comparaison
324 // of item labels (GetText)
325 virtual int OnCompareItems(const wxTreeItemId
& item1
,
326 const wxTreeItemId
& item2
);
327 // sort the children of this item using OnCompareItems
329 // NB: this function is not reentrant and not MT-safe (FIXME)!
330 void SortChildren(const wxTreeItemId
& item
);
335 // determine to which item (if any) belongs the given point (the
336 // coordinates specified are relative to the client area of tree ctrl)
337 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
340 // The first function is more portable (because easier to implement
341 // on other platforms), but the second one returns some extra info.
342 wxTreeItemId
HitTest(const wxPoint
& point
)
343 { int dummy
; return HitTest(point
, dummy
); }
344 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
346 // get the bounding rectangle of the item (or of its label only)
347 bool GetBoundingRect(const wxTreeItemId
& item
,
349 bool textOnly
= false) const;
354 virtual bool ShouldInheritColours() const { return false; }
356 virtual wxVisualAttributes
GetDefaultAttributes() const
358 return GetClassDefaultAttributes(GetWindowVariant());
361 static wxVisualAttributes
362 GetClassDefaultAttributes(wxWindowVariant variant
= wxWINDOW_VARIANT_NORMAL
);
364 // override some base class virtuals
365 virtual bool SetBackgroundColour(const wxColour
&colour
);
366 virtual bool SetForegroundColour(const wxColour
&colour
);
368 // get/set the check state for the item (only for wxTR_MULTIPLE)
369 bool IsItemChecked(const wxTreeItemId
& item
) const;
370 void SetItemCheck(const wxTreeItemId
& item
, bool check
= true);
372 // set/get the item state.image (state == -1 means cycle to the next one)
373 void SetState(const wxTreeItemId
& node
, int state
);
374 int GetState(const wxTreeItemId
& node
);
377 // SetImageList helper
378 void SetAnyImageList(wxImageList
*imageList
, int which
);
380 // refresh a single item
381 void RefreshItem(const wxTreeItemId
& item
);
383 wxTextCtrl
*m_textCtrl
; // used while editing the item label
384 wxImageList
*m_imageListNormal
, // images for tree elements
385 *m_imageListState
; // special images for app defined states
386 bool m_ownsImageListNormal
, m_ownsImageListState
;
389 // the common part of all ctors
393 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
394 inline void DoSetItem(wxTreeViewItem
*tvItem
);
396 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
398 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
399 wxTreeItemId hInsertAfter
,
400 const wxString
& text
,
401 int image
, int selectedImage
,
402 wxTreeItemData
*data
);
404 int DoGetItemImageFromData(const wxTreeItemId
& item
,
405 wxTreeItemIcon which
) const;
406 void DoSetItemImageFromData(const wxTreeItemId
& item
,
408 wxTreeItemIcon which
) const;
409 void DoSetItemImages(const wxTreeItemId
& item
, int image
, int imageSel
);
411 void DeleteTextCtrl();
413 // support for additional item images which we implement using
414 // wxTreeItemIndirectData technique
415 void SetIndirectItemData(const wxTreeItemId
& item
,
416 class wxTreeItemIndirectData
*data
);
417 bool HasIndirectData(const wxTreeItemId
& item
) const;
418 bool IsDataIndirect(wxTreeItemData
*data
) const
419 { return data
&& data
->GetId().m_pItem
== 0; }
421 // the hash storing the items attributes (indexed by item ids)
422 wxMapTreeAttr m_attrs
;
424 // true if the hash above is not empty
428 wxDragImage
*m_dragImage
;
430 // Virtual root item, if wxTR_HIDE_ROOT is set.
431 void* m_pVirtualRoot
;
433 // the starting item for selection with Shift
434 wxTreeItemId m_htSelStart
;
436 friend class wxTreeItemIndirectData
;
437 friend class wxTreeSortHelper
;
439 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
440 DECLARE_NO_COPY_CLASS(wxTreeCtrl
)
443 #endif // wxUSE_TREECTRL
445 #endif // _WX_TREECTRL_H_