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"
30 # ifdef GetNextSibling
31 # undef GetNextSibling
35 // the type for "untyped" data
36 typedef long wxDataType
;
39 class WXDLLEXPORT wxImageList
;
40 struct WXDLLEXPORT wxTreeViewItem
;
42 // a callback function used for sorting tree items, it should return -1 if the
43 // first item precedes the second, +1 if the second precedes the first or 0 if
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
52 // where exactly the specified point is situated:
53 // above the client area.
54 static const int wxTREE_HITTEST_ABOVE
= 0x0001;
55 // below the client area.
56 static const int wxTREE_HITTEST_BELOW
= 0x0002;
57 // in the client area but below the last item.
58 static const int wxTREE_HITTEST_NOWHERE
= 0x0004;
59 // on the button associated with an item.
60 static const int wxTREE_HITTEST_ONITEMBUTTON
= 0x0010;
61 // on the bitmap associated with an item.
62 static const int wxTREE_HITTEST_ONITEMICON
= 0x0020;
63 // in the indentation associated with an item.
64 static const int wxTREE_HITTEST_ONITEMINDENT
= 0x0040;
65 // on the label (string) associated with an item.
66 static const int wxTREE_HITTEST_ONITEMLABEL
= 0x0080;
67 // in the area to the right of an item.
68 static const int wxTREE_HITTEST_ONITEMRIGHT
= 0x0100;
69 // on the state icon for a tree view item that is in a user-defined state.
70 static const int wxTREE_HITTEST_ONITEMSTATEICON
= 0x0200;
71 // to the right of the client area.
72 static const int wxTREE_HITTEST_TOLEFT
= 0x0400;
73 // to the left of the client area.
74 static const int wxTREE_HITTEST_TORIGHT
= 0x0800;
75 // anywhere on the item
76 static const int wxTREE_HITTEST_ONITEM
= wxTREE_HITTEST_ONITEMICON
|
77 wxTREE_HITTEST_ONITEMLABEL
|
78 wxTREE_HITTEST_ONITEMSTATEICON
;
80 // NB: all the following flags are for compatbility only and will be removed in the
83 // flags for deprecated `Expand(int action)'
87 wxTREE_EXPAND_COLLAPSE
,
88 wxTREE_EXPAND_COLLAPSE_RESET
,
92 // flags for deprecated InsertItem() variant
93 #define wxTREE_INSERT_FIRST 0xFFFF0001
94 #define wxTREE_INSERT_LAST 0xFFFF0002
96 // ----------------------------------------------------------------------------
97 // wxTreeItemId identifies an element of the tree. In this implementation, it's
98 // just a trivial wrapper around Win32 HTREEITEM. It's opaque for the
100 // ----------------------------------------------------------------------------
101 class WXDLLEXPORT wxTreeItemId
105 // 0 is invalid value for HTREEITEM
106 wxTreeItemId() { m_itemId
= 0; }
108 // default copy ctor/assignment operator are ok for us
111 // is this a valid tree item?
112 bool IsOk() const { return m_itemId
!= 0; }
114 // conversion to/from either real (system-dependent) tree item id or
115 // to "long" which used to be the type for tree item ids in previous
116 // versions of wxWindows
118 // for wxTreeCtrl usage only
119 wxTreeItemId(WXHTREEITEM itemId
) { m_itemId
= (long)itemId
; }
120 operator WXHTREEITEM() const { return (WXHTREEITEM
)m_itemId
; }
122 void operator=(WXHTREEITEM item
) { m_itemId
= (long) item
; }
128 // ----------------------------------------------------------------------------
129 // wxTreeItemData is some (arbitrary) user class associated with some item. The
130 // main advantage of having this class (compared to old untyped interface) is
131 // that wxTreeItemData's are destroyed automatically by the tree and, as this
132 // class has virtual dtor, it means that the memory will be automatically
133 // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
134 // the size of this class is critical: in any real application, each tree leaf
135 // will have wxTreeItemData associated with it and number of leaves may be
138 // Because the objects of this class are deleted by the tree, they should
139 // always be allocated on the heap!
140 // ----------------------------------------------------------------------------
141 class WXDLLEXPORT wxTreeItemData
: private wxTreeItemId
144 // default ctor/copy ctor/assignment operator are ok
146 // dtor is virtual and all the items are deleted by the tree control when
147 // it's deleted, so you normally don't have to care about freeing memory
148 // allocated in your wxTreeItemData-derived class
149 virtual ~wxTreeItemData() { }
151 // accessors: set/get the item associated with this node
152 void SetId(const wxTreeItemId
& id
) { m_itemId
= id
; }
154 const wxTreeItemId
GetId() const { return m_itemId
; }
156 const wxTreeItemId
& GetId() const { return (wxTreeItemId
&) m_itemId
; }
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
163 class WXDLLEXPORT wxTreeCtrl
: public wxControl
168 wxTreeCtrl() { Init(); }
170 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
171 const wxPoint
& pos
= wxDefaultPosition
,
172 const wxSize
& size
= wxDefaultSize
,
173 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
174 const wxValidator
& validator
= wxDefaultValidator
,
175 const wxString
& name
= "wxTreeCtrl")
177 Create(parent
, id
, pos
, size
, style
, validator
, name
);
180 virtual ~wxTreeCtrl();
182 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
183 const wxPoint
& pos
= wxDefaultPosition
,
184 const wxSize
& size
= wxDefaultSize
,
185 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
186 const wxValidator
& validator
= wxDefaultValidator
,
187 const wxString
& name
= "wxTreeCtrl");
192 // get the total number of items in the control
193 size_t GetCount() const;
195 // indent is the number of pixels the children are indented relative to
196 // the parents position. SetIndent() also redraws the control
198 unsigned int GetIndent() const;
199 void SetIndent(unsigned int indent
);
201 // image list: these functions allow to associate an image list with
202 // the control and retrieve it. Note that the control does _not_ delete
203 // the associated image list when it's deleted in order to allow image
204 // lists to be shared between different controls.
206 // The normal image list is for the icons which correspond to the
207 // normal tree item state (whether it is selected or not).
208 // Additionally, the application might choose to show a state icon
209 // which corresponds to an app-defined item state (for example,
210 // checked/unchecked) which are taken from the state image list.
211 wxImageList
*GetImageList() const;
212 wxImageList
*GetStateImageList() const;
214 void SetImageList(wxImageList
*imageList
);
215 void SetStateImageList(wxImageList
*imageList
);
217 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
218 // member functions of wxTreeItem because they must know the tree the item
219 // belongs to for Windows implementation and storing the pointer to
220 // wxTreeCtrl in each wxTreeItem is just too much waste.
225 // retrieve items label
226 wxString
GetItemText(const wxTreeItemId
& item
) const;
227 // get the normal item image
228 int GetItemImage(const wxTreeItemId
& item
) const;
229 // get the selected item image
230 int GetItemSelectedImage(const wxTreeItemId
& item
) const;
231 // get the data associated with the item
232 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
238 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
239 // set the normal item image
240 void SetItemImage(const wxTreeItemId
& item
, int image
);
241 // set the selected item image
242 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
);
243 // associate some data with the item
244 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
246 // force appearance of [+] button near the item. This is useful to
247 // allow the user to expand the items which don't have any children now
248 // - but instead add them only when needed, thus minimizing memory
249 // usage and loading time.
250 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
252 // the item will be shown in bold
253 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
255 // item status inquiries
256 // ---------------------
258 // is the item visible (it might be outside the view or not expanded)?
259 bool IsVisible(const wxTreeItemId
& item
) const;
260 // does the item has any children?
261 bool ItemHasChildren(const wxTreeItemId
& item
) const;
262 // is the item expanded (only makes sense if HasChildren())?
263 bool IsExpanded(const wxTreeItemId
& item
) const;
264 // is this item currently selected (the same as has focus)?
265 bool IsSelected(const wxTreeItemId
& item
) const;
266 // is item text in bold font?
267 bool IsBold(const wxTreeItemId
& item
) const;
269 // number of children
270 // ------------------
272 // if 'recursively' is FALSE, only immediate children count, otherwise
273 // the returned number is the number of all items in this branch
274 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
279 // wxTreeItemId.IsOk() will return FALSE if there is no such item
281 // get the root tree item
282 wxTreeItemId
GetRootItem() const;
284 // get the item currently selected (may return NULL if no selection)
285 wxTreeItemId
GetSelection() const;
287 // get the parent of this item (may return NULL if root)
288 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
290 // for this enumeration function you must pass in a "cookie" parameter
291 // which is opaque for the application but is necessary for the library
292 // to make these functions reentrant (i.e. allow more than one
293 // enumeration on one and the same object simultaneously). Of course,
294 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
297 // get the first child of this item
298 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& _cookie
) const;
299 // get the next child
300 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& _cookie
) const;
302 // get the next sibling of this item
303 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
304 // get the previous sibling
305 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
307 // get first visible item
308 wxTreeItemId
GetFirstVisibleItem() const;
309 // get the next visible item: item must be visible itself!
310 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
311 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
312 // get the previous visible item: item must be visible itself!
313 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
318 // add the root node to the tree
319 wxTreeItemId
AddRoot(const wxString
& text
,
320 int image
= -1, int selectedImage
= -1,
321 wxTreeItemData
*data
= NULL
);
323 // insert a new item in as the first child of the parent
324 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
325 const wxString
& text
,
326 int image
= -1, int selectedImage
= -1,
327 wxTreeItemData
*data
= NULL
);
329 // insert a new item after a given one
330 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
331 const wxTreeItemId
& idPrevious
,
332 const wxString
& text
,
333 int image
= -1, int selectedImage
= -1,
334 wxTreeItemData
*data
= NULL
);
336 // insert a new item in as the last child of the parent
337 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
338 const wxString
& text
,
339 int image
= -1, int selectedImage
= -1,
340 wxTreeItemData
*data
= NULL
);
342 // delete this item and associated data if any
343 void Delete(const wxTreeItemId
& item
);
344 // delete all children (but don't delete the item itself)
345 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
346 void DeleteChildren(const wxTreeItemId
& item
);
347 // delete all items from the tree
348 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
349 void DeleteAllItems();
352 void Expand(const wxTreeItemId
& item
);
353 // collapse the item without removing its children
354 void Collapse(const wxTreeItemId
& item
);
355 // collapse the item and remove all children
356 void CollapseAndReset(const wxTreeItemId
& item
);
357 // toggles the current state
358 void Toggle(const wxTreeItemId
& item
);
360 // remove the selection from currently selected item (if any)
363 void SelectItem(const wxTreeItemId
& item
);
364 // make sure this item is visible (expanding the parent item and/or
365 // scrolling to this item if necessary)
366 void EnsureVisible(const wxTreeItemId
& item
);
367 // scroll to this item (but don't expand its parent)
368 void ScrollTo(const wxTreeItemId
& item
);
370 // start editing the item label: this (temporarily) replaces the item
371 // with a one line edit control. The item will be selected if it hadn't
372 // been before. textCtrlClass parameter allows you to create an edit
373 // control of arbitrary user-defined class deriving from wxTextCtrl.
374 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
375 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
376 // returns the same pointer as StartEdit() if the item is being edited,
377 // NULL otherwise (it's assumed that no more than one item may be
378 // edited simultaneously)
379 wxTextCtrl
* GetEditControl() const;
380 // end editing and accept or discard the changes to item label
381 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
384 // this function is called to compare 2 items and should return -1, 0
385 // or +1 if the first item is less than, equal to or greater than the
386 // second one. The base class version performs alphabetic comparaison
387 // of item labels (GetText)
388 virtual int OnCompareItems(const wxTreeItemId
& item1
,
389 const wxTreeItemId
& item2
);
390 // sort the children of this item using OnCompareItems
392 // NB: this function is not reentrant and not MT-safe (FIXME)!
393 void SortChildren(const wxTreeItemId
& item
);
398 // determine to which item (if any) belongs the given point (the
399 // coordinates specified are relative to the client area of tree ctrl)
400 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
403 // The first function is more portable (because easier to implement
404 // on other platforms), but the second one returns some extra info.
405 wxTreeItemId
HitTest(const wxPoint
& point
)
406 { int dummy
; return HitTest(point
, dummy
); }
407 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
409 // get the bounding rectangle of the item (or of its label only)
410 // @@@ do we really need to expose this functions to the application?
411 void GetBoundingRect(const wxTreeItemId
& item
,
413 bool textOnly
= FALSE
) const;
418 // these methods are deprecated and will be removed in future versions of
419 // wxWindows, they're here for compatibility only, don't use them in new
420 // code (the comments indicate why these methods are now useless and how to
423 // use Expand, Collapse, CollapseAndReset or Toggle
424 void ExpandItem(const wxTreeItemId
& item
, int action
);
426 // use AddRoot, PrependItem or AppendItem
427 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
428 const wxString
& text
,
429 int image
= -1, int selImage
= -1,
430 long insertAfter
= wxTREE_INSERT_LAST
);
432 // use Set/GetImageList and Set/GetStateImageList
433 wxImageList
*GetImageList(int) const
434 { return GetImageList(); }
435 void SetImageList(wxImageList
*imageList
, int)
436 { SetImageList(imageList
); }
440 void Command(wxCommandEvent
& event
) { ProcessCommand(event
); };
441 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
442 virtual bool MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
*result
);
445 // SetImageList helper
446 void SetAnyImageList(wxImageList
*imageList
, int which
);
448 wxTextCtrl
* m_textCtrl
; // used while editing the item label
449 wxImageList
*m_imageListNormal
, // images for tree elements
450 *m_imageListState
; // special images for app defined states
453 // the common part of all ctors
457 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
458 inline void DoSetItem(wxTreeViewItem
*tvItem
);
460 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
462 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
463 wxTreeItemId hInsertAfter
,
464 const wxString
& text
,
465 int image
, int selectedImage
,
466 wxTreeItemData
*data
);
468 void DeleteTextCtrl();
470 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
473 // ----------------------------------------------------------------------------
474 // wxTreeEvent is a special class for all events associated with tree controls
476 // NB: note that not all accessors make sense for all events, see the event
477 // descriptions below
478 // ----------------------------------------------------------------------------
479 class WXDLLEXPORT wxTreeEvent
: public wxNotifyEvent
483 wxTreeEvent(wxEventType commandType
= wxEVT_NULL
, int id
= 0);
486 // get the item on which the operation was performed or the newly
487 // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events
488 wxTreeItemId
GetItem() const { return m_item
; }
490 // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously
492 wxTreeItemId
GetOldItem() const { return m_itemOld
; }
494 // the point where the mouse was when the drag operation started (for
495 // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only)
496 wxPoint
GetPoint() const { return m_pointDrag
; }
498 // keyboard code (for wxEVT_COMMAND_TREE_KEY_DOWN only)
499 int GetCode() const { return m_code
; }
502 // @@ we could save some space by using union here
508 DECLARE_DYNAMIC_CLASS(wxTreeEvent
)
511 typedef void (wxEvtHandler::*wxTreeEventFunction
)(wxTreeEvent
&);
513 // ----------------------------------------------------------------------------
514 // macros for handling tree control events
515 // ----------------------------------------------------------------------------
517 // GetItem() returns the item being dragged, GetPoint() the mouse coords
518 #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
519 #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
521 // GetItem() returns the itme whose label is being edited
522 #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
523 #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
525 // provide/update information about GetItem() item
526 #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
527 #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
529 // GetItem() is the item being expanded/collapsed, the "ING" versions can use
530 #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
531 #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
532 #define EVT_TREE_ITEM_COLLAPSED(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
533 #define EVT_TREE_ITEM_COLLAPSING(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
535 // GetOldItem() is the item which had the selection previously, GetItem() is
536 // the item which acquires selection
537 #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
538 #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
540 // GetCode() returns the key code
541 // NB: this is the only message for which GetItem() is invalid (you may get the
542 // item from GetSelection())
543 #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
545 // GetItem() returns the item being deleted, the associated data (if any) will
546 // be deleted just after the return of this event handler (if any)
547 #define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
549 // GetItem() returns the item that was activated (double click, enter, space)
550 #define EVT_TREE_ITEM_ACTIVATED(id, fn) { wxEVT_COMMAND_TREE_ITEM_ACTIVATED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },