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 // ----------------------------------------------------------------------------
21 #include "wx/textctrl.h"
22 #include "wx/dynarray.h"
23 #include "wx/treebase.h"
24 #include "wx/hashmap.h"
27 class WXDLLIMPEXP_CORE wxImageList
;
28 class WXDLLIMPEXP_CORE wxDragImage
;
29 struct WXDLLIMPEXP_FWD_CORE wxTreeViewItem
;
31 // hash storing attributes for our items
32 WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr
*, wxMapTreeAttr
);
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 class WXDLLIMPEXP_CORE wxTreeCtrl
: public wxControl
43 wxTreeCtrl() { Init(); }
45 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
46 const wxPoint
& pos
= wxDefaultPosition
,
47 const wxSize
& size
= wxDefaultSize
,
48 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
49 const wxValidator
& validator
= wxDefaultValidator
,
50 const wxString
& name
= wxTreeCtrlNameStr
)
52 Create(parent
, id
, pos
, size
, style
, validator
, name
);
55 virtual ~wxTreeCtrl();
57 bool Create(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
);
67 // get the total number of items in the control
68 virtual unsigned int GetCount() const;
70 // indent is the number of pixels the children are indented relative to
71 // the parents position. SetIndent() also redraws the control
73 unsigned int GetIndent() const;
74 void SetIndent(unsigned int indent
);
76 // spacing is the number of pixels between the start and the Text
77 unsigned int GetSpacing() const { return 18; } // return wxGTK default
78 void SetSpacing(unsigned int WXUNUSED(spacing
)) { }
80 // image list: these functions allow to associate an image list with
81 // the control and retrieve it. Note that the control does _not_ delete
82 // the associated image list when it's deleted in order to allow image
83 // lists to be shared between different controls.
85 // The normal image list is for the icons which correspond to the
86 // normal tree item state (whether it is selected or not).
87 // Additionally, the application might choose to show a state icon
88 // which corresponds to an app-defined item state (for example,
89 // checked/unchecked) which are taken from the state image list.
90 wxImageList
*GetImageList() const;
91 wxImageList
*GetStateImageList() const;
93 void SetImageList(wxImageList
*imageList
);
94 void SetStateImageList(wxImageList
*imageList
);
95 void AssignImageList(wxImageList
*imageList
);
96 void AssignStateImageList(wxImageList
*imageList
);
98 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
99 // member functions of wxTreeItem because they must know the tree the item
100 // belongs to for Windows implementation and storing the pointer to
101 // wxTreeCtrl in each wxTreeItem is just too much waste.
106 // retrieve items label
107 wxString
GetItemText(const wxTreeItemId
& item
) const;
108 // get one of the images associated with the item (normal by default)
109 int GetItemImage(const wxTreeItemId
& item
,
110 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
111 // get the data associated with the item
112 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
114 // get the item's text colour
115 wxColour
GetItemTextColour(const wxTreeItemId
& item
) const;
117 // get the item's background colour
118 wxColour
GetItemBackgroundColour(const wxTreeItemId
& item
) const;
120 // get the item's font
121 wxFont
GetItemFont(const wxTreeItemId
& item
) const;
127 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
128 // get one of the images associated with the item (normal by default)
129 void SetItemImage(const wxTreeItemId
& item
, int image
,
130 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
131 // associate some data with the item
132 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
134 // force appearance of [+] button near the item. This is useful to
135 // allow the user to expand the items which don't have any children now
136 // - but instead add them only when needed, thus minimizing memory
137 // usage and loading time.
138 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= true);
140 // the item will be shown in bold
141 void SetItemBold(const wxTreeItemId
& item
, bool bold
= true);
143 // the item will be shown with a drop highlight
144 void SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
= true);
146 // set the items text colour
147 void SetItemTextColour(const wxTreeItemId
& item
, const wxColour
& col
);
149 // set the items background colour
150 void SetItemBackgroundColour(const wxTreeItemId
& item
, const wxColour
& col
);
152 // set the items font (should be of the same height for all items)
153 void SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
);
155 // item status inquiries
156 // ---------------------
158 // is the item visible (it might be outside the view or not expanded)?
159 bool IsVisible(const wxTreeItemId
& item
) const;
160 // does the item has any children?
161 bool ItemHasChildren(const wxTreeItemId
& item
) const;
162 // is the item expanded (only makes sense if HasChildren())?
163 bool IsExpanded(const wxTreeItemId
& item
) const;
164 // is this item currently selected (the same as has focus)?
165 bool IsSelected(const wxTreeItemId
& item
) const;
166 // is item text in bold font?
167 bool IsBold(const wxTreeItemId
& item
) const;
169 // number of children
170 // ------------------
172 // if 'recursively' is false, only immediate children count, otherwise
173 // the returned number is the number of all items in this branch
174 size_t GetChildrenCount(const wxTreeItemId
& item
,
175 bool recursively
= true) const;
180 // wxTreeItemId.IsOk() will return false if there is no such item
182 // get the root tree item
183 wxTreeItemId
GetRootItem() const;
185 // get the item currently selected (may return NULL if no selection)
186 wxTreeItemId
GetSelection() const;
188 // get the items currently selected, return the number of such item
190 // NB: this operation is expensive and can take a long time for a
191 // control with a lot of items (~ O(number of items)).
192 size_t GetSelections(wxArrayTreeItemIds
& selections
) const;
194 // get the parent of this item (may return NULL if root)
195 wxTreeItemId
GetItemParent(const wxTreeItemId
& item
) const;
197 // for this enumeration function you must pass in a "cookie" parameter
198 // which is opaque for the application but is necessary for the library
199 // to make these functions reentrant (i.e. allow more than one
200 // enumeration on one and the same object simultaneously). Of course,
201 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
204 // get the first child of this item
205 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
206 wxTreeItemIdValue
& cookie
) const;
207 // get the next child
208 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
209 wxTreeItemIdValue
& cookie
) const;
210 // get the last child of this item - this method doesn't use cookies
211 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
213 // get the next sibling of this item
214 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
215 // get the previous sibling
216 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
218 // get first visible item
219 wxTreeItemId
GetFirstVisibleItem() const;
220 // get the next visible item: item must be visible itself!
221 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
222 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
223 // get the previous visible item: item must be visible itself!
224 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
229 // add the root node to the tree
230 wxTreeItemId
AddRoot(const wxString
& text
,
231 int image
= -1, int selectedImage
= -1,
232 wxTreeItemData
*data
= NULL
);
234 // insert a new item in as the first child of the parent
235 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
236 const wxString
& text
,
237 int image
= -1, int selectedImage
= -1,
238 wxTreeItemData
*data
= NULL
);
240 // insert a new item after a given one
241 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
242 const wxTreeItemId
& idPrevious
,
243 const wxString
& text
,
244 int image
= -1, int selectedImage
= -1,
245 wxTreeItemData
*data
= NULL
);
247 // insert a new item before the one with the given index
248 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
250 const wxString
& text
,
251 int image
= -1, int selectedImage
= -1,
252 wxTreeItemData
*data
= NULL
);
254 // insert a new item in as the last child of the parent
255 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
256 const wxString
& text
,
257 int image
= -1, int selectedImage
= -1,
258 wxTreeItemData
*data
= NULL
);
260 // delete this item and associated data if any
261 void Delete(const wxTreeItemId
& item
);
262 // delete all children (but don't delete the item itself)
263 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
264 void DeleteChildren(const wxTreeItemId
& item
);
265 // delete all items from the tree
266 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
267 void DeleteAllItems();
270 void Expand(const wxTreeItemId
& item
);
271 // collapse the item without removing its children
272 void Collapse(const wxTreeItemId
& item
);
273 // collapse the item and remove all children
274 void CollapseAndReset(const wxTreeItemId
& item
);
275 // toggles the current state
276 void Toggle(const wxTreeItemId
& item
);
278 // remove the selection from currently selected item (if any)
280 // unselect all items (only makes sense for multiple selection control)
283 void SelectItem(const wxTreeItemId
& item
, bool select
= true);
284 // unselect this item
285 void UnselectItem(const wxTreeItemId
& item
);
286 // toggle item selection
287 void ToggleItemSelection(const wxTreeItemId
& item
);
289 // make sure this item is visible (expanding the parent item and/or
290 // scrolling to this item if necessary)
291 void EnsureVisible(const wxTreeItemId
& item
);
292 // scroll to this item (but don't expand its parent)
293 void ScrollTo(const wxTreeItemId
& item
);
295 // start editing the item label: this (temporarily) replaces the item
296 // with a one line edit control. The item will be selected if it hadn't
297 // been before. textCtrlClass parameter allows you to create an edit
298 // control of arbitrary user-defined class deriving from wxTextCtrl.
299 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
300 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
301 // returns the same pointer as StartEdit() if the item is being edited,
302 // NULL otherwise (it's assumed that no more than one item may be
303 // edited simultaneously)
304 wxTextCtrl
* GetEditControl() const;
305 // end editing and accept or discard the changes to item label
306 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= false);
309 // this function is called to compare 2 items and should return -1, 0
310 // or +1 if the first item is less than, equal to or greater than the
311 // second one. The base class version performs alphabetic comparaison
312 // of item labels (GetText)
313 virtual int OnCompareItems(const wxTreeItemId
& item1
,
314 const wxTreeItemId
& item2
);
315 // sort the children of this item using OnCompareItems
317 // NB: this function is not reentrant and not MT-safe (FIXME)!
318 void SortChildren(const wxTreeItemId
& item
);
323 // determine to which item (if any) belongs the given point (the
324 // coordinates specified are relative to the client area of tree ctrl)
325 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
328 // The first function is more portable (because easier to implement
329 // on other platforms), but the second one returns some extra info.
330 wxTreeItemId
HitTest(const wxPoint
& point
)
331 { int dummy
; return HitTest(point
, dummy
); }
332 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
334 // get the bounding rectangle of the item (or of its label only)
335 bool GetBoundingRect(const wxTreeItemId
& item
,
337 bool textOnly
= false) const;
342 virtual bool ShouldInheritColours() const { return false; }
344 virtual wxVisualAttributes
GetDefaultAttributes() const
346 return GetClassDefaultAttributes(GetWindowVariant());
349 static wxVisualAttributes
350 GetClassDefaultAttributes(wxWindowVariant variant
= wxWINDOW_VARIANT_NORMAL
);
352 // override some base class virtuals
353 virtual bool SetBackgroundColour(const wxColour
&colour
);
354 virtual bool SetForegroundColour(const wxColour
&colour
);
356 // get/set the check state for the item (only for wxTR_MULTIPLE)
357 bool IsItemChecked(const wxTreeItemId
& item
) const;
358 void SetItemCheck(const wxTreeItemId
& item
, bool check
= true);
360 // set/get the item state.image (state == -1 means cycle to the next one)
361 void SetState(const wxTreeItemId
& node
, int state
);
362 int GetState(const wxTreeItemId
& node
);
365 // SetImageList helper
366 void SetAnyImageList(wxImageList
*imageList
, int which
);
368 // refresh a single item
369 void RefreshItem(const wxTreeItemId
& item
);
371 wxTextCtrl
*m_textCtrl
; // used while editing the item label
372 wxImageList
*m_imageListNormal
, // images for tree elements
373 *m_imageListState
; // special images for app defined states
374 bool m_ownsImageListNormal
, m_ownsImageListState
;
377 // the common part of all ctors
381 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
382 inline void DoSetItem(wxTreeViewItem
*tvItem
);
384 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
386 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
387 wxTreeItemId hInsertAfter
,
388 const wxString
& text
,
389 int image
, int selectedImage
,
390 wxTreeItemData
*data
);
392 int DoGetItemImageFromData(const wxTreeItemId
& item
,
393 wxTreeItemIcon which
) const;
394 void DoSetItemImageFromData(const wxTreeItemId
& item
,
396 wxTreeItemIcon which
) const;
397 void DoSetItemImages(const wxTreeItemId
& item
, int image
, int imageSel
);
399 void DeleteTextCtrl();
401 // support for additional item images which we implement using
402 // wxTreeItemIndirectData technique
403 void SetIndirectItemData(const wxTreeItemId
& item
,
404 class wxTreeItemIndirectData
*data
);
405 bool HasIndirectData(const wxTreeItemId
& item
) const;
406 bool IsDataIndirect(wxTreeItemData
*data
) const
407 { return data
&& data
->GetId().m_pItem
== 0; }
409 // the hash storing the items attributes (indexed by item ids)
410 wxMapTreeAttr m_attrs
;
412 // true if the hash above is not empty
416 wxDragImage
*m_dragImage
;
418 // Virtual root item, if wxTR_HIDE_ROOT is set.
419 void* m_pVirtualRoot
;
421 // the starting item for selection with Shift
422 wxTreeItemId m_htSelStart
;
424 friend class wxTreeItemIndirectData
;
425 friend class wxTreeSortHelper
;
427 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
428 wxDECLARE_NO_COPY_CLASS(wxTreeCtrl
);
431 #endif // wxUSE_TREECTRL
433 #endif // _WX_TREECTRL_H_