1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl class
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10/10/98
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_TREECTRL_H_
13 #define _WX_TREECTRL_H_
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #pragma interface "treectrl.h"
22 #include "wx/control.h"
24 #include "wx/textctrl.h"
31 # ifdef GetNextSibling
32 # undef GetNextSibling
36 // the type for "untyped" data
37 typedef long wxDataType
;
40 class WXDLLEXPORT wxImageList
;
41 struct WXDLLEXPORT wxTreeViewItem
;
43 // a callback function used for sorting tree items, it should return -1 if the
44 // first item precedes the second, +1 if the second precedes the first or 0 if
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
53 // where exactly the specified point is situated:
54 // above the client area.
55 static const int wxTREE_HITTEST_ABOVE
= 0x0001;
56 // below the client area.
57 static const int wxTREE_HITTEST_BELOW
= 0x0002;
58 // in the client area but below the last item.
59 static const int wxTREE_HITTEST_NOWHERE
= 0x0004;
60 // on the button associated with an item.
61 static const int wxTREE_HITTEST_ONITEMBUTTON
= 0x0010;
62 // on the bitmap associated with an item.
63 static const int wxTREE_HITTEST_ONITEMICON
= 0x0020;
64 // in the indentation associated with an item.
65 static const int wxTREE_HITTEST_ONITEMINDENT
= 0x0040;
66 // on the label (string) associated with an item.
67 static const int wxTREE_HITTEST_ONITEMLABEL
= 0x0080;
68 // in the area to the right of an item.
69 static const int wxTREE_HITTEST_ONITEMRIGHT
= 0x0100;
70 // on the state icon for a tree view item that is in a user-defined state.
71 static const int wxTREE_HITTEST_ONITEMSTATEICON
= 0x0200;
72 // to the right of the client area.
73 static const int wxTREE_HITTEST_TOLEFT
= 0x0400;
74 // to the left of the client area.
75 static const int wxTREE_HITTEST_TORIGHT
= 0x0800;
76 // anywhere on the item
77 static const int wxTREE_HITTEST_ONITEM
= wxTREE_HITTEST_ONITEMICON
|
78 wxTREE_HITTEST_ONITEMLABEL
|
79 wxTREE_HITTEST_ONITEMSTATEICON
;
81 // NB: all the following flags are for compatbility only and will be removed in the
84 // flags for deprecated `Expand(int action)'
88 wxTREE_EXPAND_COLLAPSE
,
89 wxTREE_EXPAND_COLLAPSE_RESET
,
93 // flags for deprecated InsertItem() variant
94 #define wxTREE_INSERT_FIRST 0xFFFF0001
95 #define wxTREE_INSERT_LAST 0xFFFF0002
97 // ----------------------------------------------------------------------------
98 // wxTreeItemId identifies an element of the tree. In this implementation, it's
99 // just a trivial wrapper around Win32 HTREEITEM. It's opaque for the
101 // ----------------------------------------------------------------------------
102 class WXDLLEXPORT wxTreeItemId
106 // 0 is invalid value for HTREEITEM
107 wxTreeItemId() { m_itemId
= 0; }
109 // default copy ctor/assignment operator are ok for us
112 // is this a valid tree item?
113 bool IsOk() const { return m_itemId
!= 0; }
115 // conversion to/from either real (system-dependent) tree item id or
116 // to "long" which used to be the type for tree item ids in previous
117 // versions of wxWindows
119 // for wxTreeCtrl usage only
120 wxTreeItemId(WXHTREEITEM itemId
) { m_itemId
= (long)itemId
; }
121 operator WXHTREEITEM() const { return (WXHTREEITEM
)m_itemId
; }
123 void operator=(WXHTREEITEM item
) { m_itemId
= (long) item
; }
129 // ----------------------------------------------------------------------------
130 // wxTreeItemData is some (arbitrary) user class associated with some item. The
131 // main advantage of having this class (compared to old untyped interface) is
132 // that wxTreeItemData's are destroyed automatically by the tree and, as this
133 // class has virtual dtor, it means that the memory will be automatically
134 // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
135 // the size of this class is critical: in any real application, each tree leaf
136 // will have wxTreeItemData associated with it and number of leaves may be
139 // Because the objects of this class are deleted by the tree, they should
140 // always be allocated on the heap!
141 // ----------------------------------------------------------------------------
142 class WXDLLEXPORT wxTreeItemData
: private wxTreeItemId
145 // default ctor/copy ctor/assignment operator are ok
147 // dtor is virtual and all the items are deleted by the tree control when
148 // it's deleted, so you normally don't have to care about freeing memory
149 // allocated in your wxTreeItemData-derived class
150 virtual ~wxTreeItemData() { }
152 // accessors: set/get the item associated with this node
153 void SetId(const wxTreeItemId
& id
) { m_itemId
= id
; }
155 const wxTreeItemId
GetId() const { return m_itemId
; }
157 const wxTreeItemId
& GetId() const { return (wxTreeItemId
&) m_itemId
; }
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
164 class WXDLLEXPORT wxTreeCtrl
: public wxControl
169 wxTreeCtrl() { Init(); }
171 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
172 const wxPoint
& pos
= wxDefaultPosition
,
173 const wxSize
& size
= wxDefaultSize
,
174 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
175 const wxValidator
& validator
= wxDefaultValidator
,
176 const wxString
& name
= "wxTreeCtrl")
178 Create(parent
, id
, pos
, size
, style
, validator
, name
);
181 virtual ~wxTreeCtrl();
183 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
184 const wxPoint
& pos
= wxDefaultPosition
,
185 const wxSize
& size
= wxDefaultSize
,
186 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
187 const wxValidator
& validator
= wxDefaultValidator
,
188 const wxString
& name
= "wxTreeCtrl");
193 // get the total number of items in the control
194 size_t GetCount() const;
196 // indent is the number of pixels the children are indented relative to
197 // the parents position. SetIndent() also redraws the control
199 unsigned int GetIndent() const;
200 void SetIndent(unsigned int indent
);
202 // image list: these functions allow to associate an image list with
203 // the control and retrieve it. Note that the control does _not_ delete
204 // the associated image list when it's deleted in order to allow image
205 // lists to be shared between different controls.
207 // The normal image list is for the icons which correspond to the
208 // normal tree item state (whether it is selected or not).
209 // Additionally, the application might choose to show a state icon
210 // which corresponds to an app-defined item state (for example,
211 // checked/unchecked) which are taken from the state image list.
212 wxImageList
*GetImageList() const;
213 wxImageList
*GetStateImageList() const;
215 void SetImageList(wxImageList
*imageList
);
216 void SetStateImageList(wxImageList
*imageList
);
218 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
219 // member functions of wxTreeItem because they must know the tree the item
220 // belongs to for Windows implementation and storing the pointer to
221 // wxTreeCtrl in each wxTreeItem is just too much waste.
226 // retrieve items label
227 wxString
GetItemText(const wxTreeItemId
& item
) const;
228 // get the normal item image
229 int GetItemImage(const wxTreeItemId
& item
) const;
230 // get the selected item image
231 int GetItemSelectedImage(const wxTreeItemId
& item
) const;
232 // get the data associated with the item
233 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
239 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
240 // set the normal item image
241 void SetItemImage(const wxTreeItemId
& item
, int image
);
242 // set the selected item image
243 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
);
244 // associate some data with the item
245 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
247 // force appearance of [+] button near the item. This is useful to
248 // allow the user to expand the items which don't have any children now
249 // - but instead add them only when needed, thus minimizing memory
250 // usage and loading time.
251 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
253 // the item will be shown in bold
254 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
256 // item status inquiries
257 // ---------------------
259 // is the item visible (it might be outside the view or not expanded)?
260 bool IsVisible(const wxTreeItemId
& item
) const;
261 // does the item has any children?
262 bool ItemHasChildren(const wxTreeItemId
& item
) const;
263 // is the item expanded (only makes sense if HasChildren())?
264 bool IsExpanded(const wxTreeItemId
& item
) const;
265 // is this item currently selected (the same as has focus)?
266 bool IsSelected(const wxTreeItemId
& item
) const;
267 // is item text in bold font?
268 bool IsBold(const wxTreeItemId
& item
) const;
270 // number of children
271 // ------------------
273 // if 'recursively' is FALSE, only immediate children count, otherwise
274 // the returned number is the number of all items in this branch
275 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
280 // wxTreeItemId.IsOk() will return FALSE if there is no such item
282 // get the root tree item
283 wxTreeItemId
GetRootItem() const;
285 // get the item currently selected (may return NULL if no selection)
286 wxTreeItemId
GetSelection() const;
288 // get the parent of this item (may return NULL if root)
289 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
291 // for this enumeration function you must pass in a "cookie" parameter
292 // which is opaque for the application but is necessary for the library
293 // to make these functions reentrant (i.e. allow more than one
294 // enumeration on one and the same object simultaneously). Of course,
295 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
298 // get the first child of this item
299 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& _cookie
) const;
300 // get the next child
301 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& _cookie
) const;
302 // get the last child of this item - this method doesn't use cookies
303 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
305 // get the next sibling of this item
306 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
307 // get the previous sibling
308 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
310 // get first visible item
311 wxTreeItemId
GetFirstVisibleItem() const;
312 // get the next visible item: item must be visible itself!
313 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
314 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
315 // get the previous visible item: item must be visible itself!
316 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
321 // add the root node to the tree
322 wxTreeItemId
AddRoot(const wxString
& text
,
323 int image
= -1, int selectedImage
= -1,
324 wxTreeItemData
*data
= NULL
);
326 // insert a new item in as the first child of the parent
327 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
328 const wxString
& text
,
329 int image
= -1, int selectedImage
= -1,
330 wxTreeItemData
*data
= NULL
);
332 // insert a new item after a given one
333 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
334 const wxTreeItemId
& idPrevious
,
335 const wxString
& text
,
336 int image
= -1, int selectedImage
= -1,
337 wxTreeItemData
*data
= NULL
);
339 // insert a new item in as the last child of the parent
340 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
341 const wxString
& text
,
342 int image
= -1, int selectedImage
= -1,
343 wxTreeItemData
*data
= NULL
);
345 // delete this item and associated data if any
346 void Delete(const wxTreeItemId
& item
);
347 // delete all children (but don't delete the item itself)
348 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
349 void DeleteChildren(const wxTreeItemId
& item
);
350 // delete all items from the tree
351 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
352 void DeleteAllItems();
355 void Expand(const wxTreeItemId
& item
);
356 // collapse the item without removing its children
357 void Collapse(const wxTreeItemId
& item
);
358 // collapse the item and remove all children
359 void CollapseAndReset(const wxTreeItemId
& item
);
360 // toggles the current state
361 void Toggle(const wxTreeItemId
& item
);
363 // remove the selection from currently selected item (if any)
366 void SelectItem(const wxTreeItemId
& item
);
367 // make sure this item is visible (expanding the parent item and/or
368 // scrolling to this item if necessary)
369 void EnsureVisible(const wxTreeItemId
& item
);
370 // scroll to this item (but don't expand its parent)
371 void ScrollTo(const wxTreeItemId
& item
);
373 // start editing the item label: this (temporarily) replaces the item
374 // with a one line edit control. The item will be selected if it hadn't
375 // been before. textCtrlClass parameter allows you to create an edit
376 // control of arbitrary user-defined class deriving from wxTextCtrl.
377 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
378 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
379 // returns the same pointer as StartEdit() if the item is being edited,
380 // NULL otherwise (it's assumed that no more than one item may be
381 // edited simultaneously)
382 wxTextCtrl
* GetEditControl() const;
383 // end editing and accept or discard the changes to item label
384 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
387 // this function is called to compare 2 items and should return -1, 0
388 // or +1 if the first item is less than, equal to or greater than the
389 // second one. The base class version performs alphabetic comparaison
390 // of item labels (GetText)
391 virtual int OnCompareItems(const wxTreeItemId
& item1
,
392 const wxTreeItemId
& item2
);
393 // sort the children of this item using OnCompareItems
395 // NB: this function is not reentrant and not MT-safe (FIXME)!
396 void SortChildren(const wxTreeItemId
& item
);
401 // determine to which item (if any) belongs the given point (the
402 // coordinates specified are relative to the client area of tree ctrl)
403 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
406 // The first function is more portable (because easier to implement
407 // on other platforms), but the second one returns some extra info.
408 wxTreeItemId
HitTest(const wxPoint
& point
)
409 { int dummy
; return HitTest(point
, dummy
); }
410 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
412 // get the bounding rectangle of the item (or of its label only)
413 bool GetBoundingRect(const wxTreeItemId
& item
,
415 bool textOnly
= FALSE
) const;
420 // these methods are deprecated and will be removed in future versions of
421 // wxWindows, they're here for compatibility only, don't use them in new
422 // code (the comments indicate why these methods are now useless and how to
425 // use Expand, Collapse, CollapseAndReset or Toggle
426 void ExpandItem(const wxTreeItemId
& item
, int action
);
428 // use AddRoot, PrependItem or AppendItem
429 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
430 const wxString
& text
,
431 int image
= -1, int selImage
= -1,
432 long insertAfter
= wxTREE_INSERT_LAST
);
434 // use Set/GetImageList and Set/GetStateImageList
435 wxImageList
*GetImageList(int) const
436 { return GetImageList(); }
437 void SetImageList(wxImageList
*imageList
, int)
438 { SetImageList(imageList
); }
442 void Command(wxCommandEvent
& event
) { ProcessCommand(event
); };
443 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
444 virtual bool MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
*result
);
447 // SetImageList helper
448 void SetAnyImageList(wxImageList
*imageList
, int which
);
450 wxTextCtrl
* m_textCtrl
; // used while editing the item label
451 wxImageList
*m_imageListNormal
, // images for tree elements
452 *m_imageListState
; // special images for app defined states
455 // the common part of all ctors
459 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
460 inline void DoSetItem(wxTreeViewItem
*tvItem
);
462 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
464 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
465 wxTreeItemId hInsertAfter
,
466 const wxString
& text
,
467 int image
, int selectedImage
,
468 wxTreeItemData
*data
);
470 void DeleteTextCtrl();
472 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
475 // ----------------------------------------------------------------------------
476 // wxTreeEvent is a special class for all events associated with tree controls
478 // NB: note that not all accessors make sense for all events, see the event
479 // descriptions below
480 // ----------------------------------------------------------------------------
481 class WXDLLEXPORT wxTreeEvent
: public wxNotifyEvent
485 wxTreeEvent(wxEventType commandType
= wxEVT_NULL
, int id
= 0);
488 // get the item on which the operation was performed or the newly
489 // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events
490 wxTreeItemId
GetItem() const { return m_item
; }
492 // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously
494 wxTreeItemId
GetOldItem() const { return m_itemOld
; }
496 // the point where the mouse was when the drag operation started (for
497 // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only)
498 wxPoint
GetPoint() const { return m_pointDrag
; }
500 // keyboard code (for wxEVT_COMMAND_TREE_KEY_DOWN only)
501 int GetCode() const { return m_code
; }
504 // @@ we could save some space by using union here
510 DECLARE_DYNAMIC_CLASS(wxTreeEvent
)
513 typedef void (wxEvtHandler::*wxTreeEventFunction
)(wxTreeEvent
&);
515 // ----------------------------------------------------------------------------
516 // macros for handling tree control events
517 // ----------------------------------------------------------------------------
519 // GetItem() returns the item being dragged, GetPoint() the mouse coords
520 #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
521 #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
523 // GetItem() returns the itme whose label is being edited
524 #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
525 #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
527 // provide/update information about GetItem() item
528 #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
529 #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
531 // GetItem() is the item being expanded/collapsed, the "ING" versions can use
532 #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
533 #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
534 #define EVT_TREE_ITEM_COLLAPSED(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
535 #define EVT_TREE_ITEM_COLLAPSING(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
537 // GetOldItem() is the item which had the selection previously, GetItem() is
538 // the item which acquires selection
539 #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
540 #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
542 // GetCode() returns the key code
543 // NB: this is the only message for which GetItem() is invalid (you may get the
544 // item from GetSelection())
545 #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
547 // GetItem() returns the item being deleted, the associated data (if any) will
548 // be deleted just after the return of this event handler (if any)
549 #define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
551 // GetItem() returns the item that was activated (double click, enter, space)
552 #define EVT_TREE_ITEM_ACTIVATED(id, fn) { wxEVT_COMMAND_TREE_ITEM_ACTIVATED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },