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 // ----------------------------------------------------------------------------
20 #pragma interface "treectrl.h"
23 #include "wx/control.h"
25 #include "wx/textctrl.h"
28 // Cygwin windows.h defines these identifiers
33 // the type for "untyped" data
34 typedef long wxDataType
;
37 class WXDLLEXPORT wxImageList
;
38 struct WXDLLEXPORT wxTreeViewItem
;
40 // a callback function used for sorting tree items, it should return -1 if the
41 // first item precedes the second, +1 if the second precedes the first or 0 if
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
50 // where exactly the specified point is situated:
51 // above the client area.
52 static const int wxTREE_HITTEST_ABOVE
= 0x0001;
53 // below the client area.
54 static const int wxTREE_HITTEST_BELOW
= 0x0002;
55 // in the client area but below the last item.
56 static const int wxTREE_HITTEST_NOWHERE
= 0x0004;
57 // on the button associated with an item.
58 static const int wxTREE_HITTEST_ONITEMBUTTON
= 0x0010;
59 // on the bitmap associated with an item.
60 static const int wxTREE_HITTEST_ONITEMICON
= 0x0020;
61 // in the indentation associated with an item.
62 static const int wxTREE_HITTEST_ONITEMINDENT
= 0x0040;
63 // on the label (string) associated with an item.
64 static const int wxTREE_HITTEST_ONITEMLABEL
= 0x0080;
65 // in the area to the right of an item.
66 static const int wxTREE_HITTEST_ONITEMRIGHT
= 0x0100;
67 // on the state icon for a tree view item that is in a user-defined state.
68 static const int wxTREE_HITTEST_ONITEMSTATEICON
= 0x0200;
69 // to the right of the client area.
70 static const int wxTREE_HITTEST_TOLEFT
= 0x0400;
71 // to the left of the client area.
72 static const int wxTREE_HITTEST_TORIGHT
= 0x0800;
73 // anywhere on the item
74 static const int wxTREE_HITTEST_ONITEM
= wxTREE_HITTEST_ONITEMICON
|
75 wxTREE_HITTEST_ONITEMLABEL
|
76 wxTREE_HITTEST_ONITEMSTATEICON
;
78 // NB: all the following flags are for compatbility only and will be removed in the
81 // flags for deprecated `Expand(int action)'
85 wxTREE_EXPAND_COLLAPSE
,
86 wxTREE_EXPAND_COLLAPSE_RESET
,
90 // flags for deprecated InsertItem() variant
91 #define wxTREE_INSERT_FIRST 0xFFFF0001
92 #define wxTREE_INSERT_LAST 0xFFFF0002
94 // ----------------------------------------------------------------------------
95 // wxTreeItemId identifies an element of the tree. In this implementation, it's
96 // just a trivial wrapper around Win32 HTREEITEM. It's opaque for the
98 // ----------------------------------------------------------------------------
99 class WXDLLEXPORT wxTreeItemId
103 // 0 is invalid value for HTREEITEM
104 wxTreeItemId() { m_itemId
= 0; }
106 // default copy ctor/assignment operator are ok for us
109 // is this a valid tree item?
110 bool IsOk() const { return m_itemId
!= 0; }
112 // conversion to/from either real (system-dependent) tree item id or
113 // to "long" which used to be the type for tree item ids in previous
114 // versions of wxWindows
116 // for wxTreeCtrl usage only
117 wxTreeItemId(WXHTREEITEM itemId
) { m_itemId
= (long)itemId
; }
118 operator WXHTREEITEM() const { return (WXHTREEITEM
)m_itemId
; }
120 void operator=(WXHTREEITEM item
) { m_itemId
= (long) item
; }
126 // ----------------------------------------------------------------------------
127 // wxTreeItemData is some (arbitrary) user class associated with some item. The
128 // main advantage of having this class (compared to old untyped interface) is
129 // that wxTreeItemData's are destroyed automatically by the tree and, as this
130 // class has virtual dtor, it means that the memory will be automatically
131 // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
132 // the size of this class is critical: in any real application, each tree leaf
133 // will have wxTreeItemData associated with it and number of leaves may be
136 // Because the objects of this class are deleted by the tree, they should
137 // always be allocated on the heap!
138 // ----------------------------------------------------------------------------
139 class WXDLLEXPORT wxTreeItemData
: private wxTreeItemId
142 // default ctor/copy ctor/assignment operator are ok
144 // dtor is virtual and all the items are deleted by the tree control when
145 // it's deleted, so you normally don't have to care about freeing memory
146 // allocated in your wxTreeItemData-derived class
147 virtual ~wxTreeItemData() { }
149 // accessors: set/get the item associated with this node
150 void SetId(const wxTreeItemId
& id
) { m_itemId
= id
; }
152 const wxTreeItemId
GetId() const { return m_itemId
; }
154 const wxTreeItemId
& GetId() const { return (wxTreeItemId
&) m_itemId
; }
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
161 class WXDLLEXPORT wxTreeCtrl
: public wxControl
166 wxTreeCtrl() { Init(); }
168 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
169 const wxPoint
& pos
= wxDefaultPosition
,
170 const wxSize
& size
= wxDefaultSize
,
171 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
172 const wxValidator
& validator
= wxDefaultValidator
,
173 const wxString
& name
= "wxTreeCtrl")
175 Create(parent
, id
, pos
, size
, style
, validator
, name
);
178 virtual ~wxTreeCtrl();
180 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
181 const wxPoint
& pos
= wxDefaultPosition
,
182 const wxSize
& size
= wxDefaultSize
,
183 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
184 const wxValidator
& validator
= wxDefaultValidator
,
185 const wxString
& name
= "wxTreeCtrl");
190 // get the total number of items in the control
191 size_t GetCount() const;
193 // indent is the number of pixels the children are indented relative to
194 // the parents position. SetIndent() also redraws the control
196 unsigned int GetIndent() const;
197 void SetIndent(unsigned int indent
);
199 // spacing is the number of pixels between the start and the Text
200 // not implemented under wxMSW
201 unsigned int GetSpacing() const { return 18; } // return wxGTK default
202 void SetSpacing(unsigned int ) {}
204 // image list: these functions allow to associate an image list with
205 // the control and retrieve it. Note that the control does _not_ delete
206 // the associated image list when it's deleted in order to allow image
207 // lists to be shared between different controls.
209 // The normal image list is for the icons which correspond to the
210 // normal tree item state (whether it is selected or not).
211 // Additionally, the application might choose to show a state icon
212 // which corresponds to an app-defined item state (for example,
213 // checked/unchecked) which are taken from the state image list.
214 wxImageList
*GetImageList() const;
215 wxImageList
*GetStateImageList() const;
217 void SetImageList(wxImageList
*imageList
);
218 void SetStateImageList(wxImageList
*imageList
);
220 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
221 // member functions of wxTreeItem because they must know the tree the item
222 // belongs to for Windows implementation and storing the pointer to
223 // wxTreeCtrl in each wxTreeItem is just too much waste.
228 // retrieve items label
229 wxString
GetItemText(const wxTreeItemId
& item
) const;
230 // get the normal item image
231 int GetItemImage(const wxTreeItemId
& item
) const;
232 // get the selected item image
233 int GetItemSelectedImage(const wxTreeItemId
& item
) const;
234 // get the data associated with the item
235 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
241 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
242 // set the normal item image
243 void SetItemImage(const wxTreeItemId
& item
, int image
);
244 // set the selected item image
245 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
);
246 // associate some data with the item
247 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
249 // force appearance of [+] button near the item. This is useful to
250 // allow the user to expand the items which don't have any children now
251 // - but instead add them only when needed, thus minimizing memory
252 // usage and loading time.
253 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
255 // the item will be shown in bold
256 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
258 // the item will be shown with a drop highlight
259 void SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
= TRUE
);
261 // item status inquiries
262 // ---------------------
264 // is the item visible (it might be outside the view or not expanded)?
265 bool IsVisible(const wxTreeItemId
& item
) const;
266 // does the item has any children?
267 bool ItemHasChildren(const wxTreeItemId
& item
) const;
268 // is the item expanded (only makes sense if HasChildren())?
269 bool IsExpanded(const wxTreeItemId
& item
) const;
270 // is this item currently selected (the same as has focus)?
271 bool IsSelected(const wxTreeItemId
& item
) const;
272 // is item text in bold font?
273 bool IsBold(const wxTreeItemId
& item
) const;
275 // number of children
276 // ------------------
278 // if 'recursively' is FALSE, only immediate children count, otherwise
279 // the returned number is the number of all items in this branch
280 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
285 // wxTreeItemId.IsOk() will return FALSE if there is no such item
287 // get the root tree item
288 wxTreeItemId
GetRootItem() const;
290 // get the item currently selected (may return NULL if no selection)
291 wxTreeItemId
GetSelection() const;
293 // get the parent of this item (may return NULL if root)
294 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
296 // for this enumeration function you must pass in a "cookie" parameter
297 // which is opaque for the application but is necessary for the library
298 // to make these functions reentrant (i.e. allow more than one
299 // enumeration on one and the same object simultaneously). Of course,
300 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
303 // get the first child of this item
304 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& _cookie
) const;
305 // get the next child
306 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& _cookie
) const;
307 // get the last child of this item - this method doesn't use cookies
308 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
310 // get the next sibling of this item
311 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
312 // get the previous sibling
313 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
315 // get first visible item
316 wxTreeItemId
GetFirstVisibleItem() const;
317 // get the next visible item: item must be visible itself!
318 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
319 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
320 // get the previous visible item: item must be visible itself!
321 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
326 // add the root node to the tree
327 wxTreeItemId
AddRoot(const wxString
& text
,
328 int image
= -1, int selectedImage
= -1,
329 wxTreeItemData
*data
= NULL
);
331 // insert a new item in as the first child of the parent
332 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
333 const wxString
& text
,
334 int image
= -1, int selectedImage
= -1,
335 wxTreeItemData
*data
= NULL
);
337 // insert a new item after a given one
338 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
339 const wxTreeItemId
& idPrevious
,
340 const wxString
& text
,
341 int image
= -1, int selectedImage
= -1,
342 wxTreeItemData
*data
= NULL
);
344 // insert a new item in as the last child of the parent
345 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
346 const wxString
& text
,
347 int image
= -1, int selectedImage
= -1,
348 wxTreeItemData
*data
= NULL
);
350 // delete this item and associated data if any
351 void Delete(const wxTreeItemId
& item
);
352 // delete all children (but don't delete the item itself)
353 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
354 void DeleteChildren(const wxTreeItemId
& item
);
355 // delete all items from the tree
356 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
357 void DeleteAllItems();
360 void Expand(const wxTreeItemId
& item
);
361 // collapse the item without removing its children
362 void Collapse(const wxTreeItemId
& item
);
363 // collapse the item and remove all children
364 void CollapseAndReset(const wxTreeItemId
& item
);
365 // toggles the current state
366 void Toggle(const wxTreeItemId
& item
);
368 // remove the selection from currently selected item (if any)
371 void SelectItem(const wxTreeItemId
& item
);
372 // make sure this item is visible (expanding the parent item and/or
373 // scrolling to this item if necessary)
374 void EnsureVisible(const wxTreeItemId
& item
);
375 // scroll to this item (but don't expand its parent)
376 void ScrollTo(const wxTreeItemId
& item
);
378 // start editing the item label: this (temporarily) replaces the item
379 // with a one line edit control. The item will be selected if it hadn't
380 // been before. textCtrlClass parameter allows you to create an edit
381 // control of arbitrary user-defined class deriving from wxTextCtrl.
382 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
383 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
384 // returns the same pointer as StartEdit() if the item is being edited,
385 // NULL otherwise (it's assumed that no more than one item may be
386 // edited simultaneously)
387 wxTextCtrl
* GetEditControl() const;
388 // end editing and accept or discard the changes to item label
389 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
392 // this function is called to compare 2 items and should return -1, 0
393 // or +1 if the first item is less than, equal to or greater than the
394 // second one. The base class version performs alphabetic comparaison
395 // of item labels (GetText)
396 virtual int OnCompareItems(const wxTreeItemId
& item1
,
397 const wxTreeItemId
& item2
);
398 // sort the children of this item using OnCompareItems
400 // NB: this function is not reentrant and not MT-safe (FIXME)!
401 void SortChildren(const wxTreeItemId
& item
);
406 // determine to which item (if any) belongs the given point (the
407 // coordinates specified are relative to the client area of tree ctrl)
408 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
411 // The first function is more portable (because easier to implement
412 // on other platforms), but the second one returns some extra info.
413 wxTreeItemId
HitTest(const wxPoint
& point
)
414 { int dummy
; return HitTest(point
, dummy
); }
415 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
417 // get the bounding rectangle of the item (or of its label only)
418 bool GetBoundingRect(const wxTreeItemId
& item
,
420 bool textOnly
= FALSE
) const;
425 // these methods are deprecated and will be removed in future versions of
426 // wxWindows, they're here for compatibility only, don't use them in new
427 // code (the comments indicate why these methods are now useless and how to
430 // use Expand, Collapse, CollapseAndReset or Toggle
431 void ExpandItem(const wxTreeItemId
& item
, int action
);
433 // use AddRoot, PrependItem or AppendItem
434 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
435 const wxString
& text
,
436 int image
= -1, int selImage
= -1,
437 long insertAfter
= wxTREE_INSERT_LAST
);
439 // use Set/GetImageList and Set/GetStateImageList
440 wxImageList
*GetImageList(int) const
441 { return GetImageList(); }
442 void SetImageList(wxImageList
*imageList
, int)
443 { SetImageList(imageList
); }
447 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
448 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
451 // SetImageList helper
452 void SetAnyImageList(wxImageList
*imageList
, int which
);
454 wxTextCtrl
* m_textCtrl
; // used while editing the item label
455 wxImageList
*m_imageListNormal
, // images for tree elements
456 *m_imageListState
; // special images for app defined states
459 // the common part of all ctors
463 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
464 inline void DoSetItem(wxTreeViewItem
*tvItem
);
466 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
468 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
469 wxTreeItemId hInsertAfter
,
470 const wxString
& text
,
471 int image
, int selectedImage
,
472 wxTreeItemData
*data
);
474 void DeleteTextCtrl();
476 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
479 // ----------------------------------------------------------------------------
480 // wxTreeEvent is a special class for all events associated with tree controls
482 // NB: note that not all accessors make sense for all events, see the event
483 // descriptions below
484 // ----------------------------------------------------------------------------
485 class WXDLLEXPORT wxTreeEvent
: public wxNotifyEvent
489 wxTreeEvent(wxEventType commandType
= wxEVT_NULL
, int id
= 0);
492 // get the item on which the operation was performed or the newly
493 // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events
494 wxTreeItemId
GetItem() const { return m_item
; }
496 // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously
498 wxTreeItemId
GetOldItem() const { return m_itemOld
; }
500 // the point where the mouse was when the drag operation started (for
501 // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only)
502 wxPoint
GetPoint() const { return m_pointDrag
; }
504 // keyboard code (for wxEVT_COMMAND_TREE_KEY_DOWN only)
505 int GetCode() const { return m_code
; }
508 // @@ we could save some space by using union here
514 DECLARE_DYNAMIC_CLASS(wxTreeEvent
)
517 typedef void (wxEvtHandler::*wxTreeEventFunction
)(wxTreeEvent
&);
519 // ----------------------------------------------------------------------------
520 // macros for handling tree control events
521 // ----------------------------------------------------------------------------
523 // GetItem() returns the item being dragged, GetPoint() the mouse coords
524 #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
525 #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
527 // GetItem() returns the itme whose label is being edited
528 #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
529 #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
531 // provide/update information about GetItem() item
532 #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
533 #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
535 // GetItem() is the item being expanded/collapsed, the "ING" versions can use
536 #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
537 #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
538 #define EVT_TREE_ITEM_COLLAPSED(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
539 #define EVT_TREE_ITEM_COLLAPSING(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
541 // GetOldItem() is the item which had the selection previously, GetItem() is
542 // the item which acquires selection
543 #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
544 #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
546 // GetCode() returns the key code
547 // NB: this is the only message for which GetItem() is invalid (you may get the
548 // item from GetSelection())
549 #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
551 // GetItem() returns the item being deleted, the associated data (if any) will
552 // be deleted just after the return of this event handler (if any)
553 #define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
555 // GetItem() returns the item that was activated (double click, enter, space)
556 #define EVT_TREE_ITEM_ACTIVATED(id, fn) { wxEVT_COMMAND_TREE_ITEM_ACTIVATED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },