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"
25 // the type for "untyped" data
26 typedef long wxDataType
;
29 class WXDLLEXPORT wxImageList
;
30 struct WXDLLEXPORT wxTreeViewItem
;
32 // a callback function used for sorting tree items, it should return -1 if the
33 // first item precedes the second, +1 if the second precedes the first or 0 if
36 typedef int (*wxTreeItemCmpFunc
)(wxTreeItemData
*item1
, wxTreeItemData
*item2
);
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
43 // where exactly the specified point is situated:
44 // above the client area.
45 static const int wxTREE_HITTEST_ABOVE
= 0x0001;
46 // below the client area.
47 static const int wxTREE_HITTEST_BELOW
= 0x0002;
48 // in the client area but below the last item.
49 static const int wxTREE_HITTEST_NOWHERE
= 0x0004;
50 // on the button associated with an item.
51 static const int wxTREE_HITTEST_ONITEMBUTTON
= 0x0010;
52 // on the bitmap associated with an item.
53 static const int wxTREE_HITTEST_ONITEMICON
= 0x0020;
54 // in the indentation associated with an item.
55 static const int wxTREE_HITTEST_ONITEMINDENT
= 0x0040;
56 // on the label (string) associated with an item.
57 static const int wxTREE_HITTEST_ONITEMLABEL
= 0x0080;
58 // in the area to the right of an item.
59 static const int wxTREE_HITTEST_ONITEMRIGHT
= 0x0100;
60 // on the state icon for a tree view item that is in a user-defined state.
61 static const int wxTREE_HITTEST_ONITEMSTATEICON
= 0x0200;
62 // to the right of the client area.
63 static const int wxTREE_HITTEST_TOLEFT
= 0x0400;
64 // to the left of the client area.
65 static const int wxTREE_HITTEST_TORIGHT
= 0x0800;
66 // anywhere on the item
67 static const int wxTREE_HITTEST_ONITEM
= wxTREE_HITTEST_ONITEMICON
|
68 wxTREE_HITTEST_ONITEMLABEL
|
69 wxTREE_HITTEST_ONITEMSTATEICON
;
71 // NB: all the following flags are for compatbility only and will be removed in the
74 // flags for deprecated `Expand(int action)'
78 wxTREE_EXPAND_COLLAPSE
,
79 wxTREE_EXPAND_COLLAPSE_RESET
,
83 // flags for deprecated InsertItem() variant
84 #define wxTREE_INSERT_FIRST 0xFFFF0001
85 #define wxTREE_INSERT_LAST 0xFFFF0002
87 // ----------------------------------------------------------------------------
88 // wxTreeItemId identifies an element of the tree. In this implementation, it's
89 // just a trivial wrapper around Win32 HTREEITEM. It's opaque for the
91 // ----------------------------------------------------------------------------
92 class WXDLLEXPORT wxTreeItemId
96 // 0 is invalid value for HTREEITEM
97 wxTreeItemId() { m_itemId
= 0; }
99 // default copy ctor/assignment operator are ok for us
102 // is this a valid tree item?
103 bool IsOk() const { return m_itemId
!= 0; }
105 // conversion to/from either real (system-dependent) tree item id or
106 // to "long" which used to be the type for tree item ids in previous
107 // versions of wxWindows
109 // for wxTreeCtrl usage only
110 wxTreeItemId(WXHTREEITEM itemId
) { m_itemId
= (long)itemId
; }
111 operator WXHTREEITEM() const { return (WXHTREEITEM
)m_itemId
; }
113 void operator=(WXHTREEITEM item
) { m_itemId
= (long) item
; }
119 // ----------------------------------------------------------------------------
120 // wxTreeItemData is some (arbitrary) user class associated with some item. The
121 // main advantage of having this class (compared to old untyped interface) is
122 // that wxTreeItemData's are destroyed automatically by the tree and, as this
123 // class has virtual dtor, it means that the memory will be automatically
124 // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
125 // the size of this class is critical: in any real application, each tree leaf
126 // will have wxTreeItemData associated with it and number of leaves may be
129 // Because the objects of this class are deleted by the tree, they should
130 // always be allocated on the heap!
131 // ----------------------------------------------------------------------------
132 class WXDLLEXPORT wxTreeItemData
: private wxTreeItemId
135 // default ctor/copy ctor/assignment operator are ok
137 // dtor is virtual and all the items are deleted by the tree control when
138 // it's deleted, so you normally don't have to care about freeing memory
139 // allocated in your wxTreeItemData-derived class
140 virtual ~wxTreeItemData() { }
142 // accessors: set/get the item associated with this node
143 void SetId(const wxTreeItemId
& id
) { m_itemId
= id
; }
144 const wxTreeItemId
& GetId() const { return (wxTreeItemId
&) m_itemId
; }
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
150 class WXDLLEXPORT wxTreeCtrl
: public wxControl
155 wxTreeCtrl() { Init(); }
157 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
158 const wxPoint
& pos
= wxDefaultPosition
,
159 const wxSize
& size
= wxDefaultSize
,
160 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
161 const wxValidator
& validator
= wxDefaultValidator
,
162 const wxString
& name
= "wxTreeCtrl")
164 Create(parent
, id
, pos
, size
, style
, validator
, name
);
167 virtual ~wxTreeCtrl();
169 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
170 const wxPoint
& pos
= wxDefaultPosition
,
171 const wxSize
& size
= wxDefaultSize
,
172 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
173 const wxValidator
& validator
= wxDefaultValidator
,
174 const wxString
& name
= "wxTreeCtrl");
179 // get the total number of items in the control
180 size_t GetCount() const;
182 // indent is the number of pixels the children are indented relative to
183 // the parents position. SetIndent() also redraws the control
185 unsigned int GetIndent() const;
186 void SetIndent(unsigned int indent
);
188 // image list: these functions allow to associate an image list with
189 // the control and retrieve it. Note that the control does _not_ delete
190 // the associated image list when it's deleted in order to allow image
191 // lists to be shared between different controls.
193 // The normal image list is for the icons which correspond to the
194 // normal tree item state (whether it is selected or not).
195 // Additionally, the application might choose to show a state icon
196 // which corresponds to an app-defined item state (for example,
197 // checked/unchecked) which are taken from the state image list.
198 wxImageList
*GetImageList() const;
199 wxImageList
*GetStateImageList() const;
201 void SetImageList(wxImageList
*imageList
);
202 void SetStateImageList(wxImageList
*imageList
);
204 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
205 // member functions of wxTreeItem because they must know the tree the item
206 // belongs to for Windows implementation and storing the pointer to
207 // wxTreeCtrl in each wxTreeItem is just too much waste.
212 // retrieve items label
213 wxString
GetItemText(const wxTreeItemId
& item
) const;
214 // get the normal item image
215 int GetItemImage(const wxTreeItemId
& item
) const;
216 // get the selected item image
217 int GetItemSelectedImage(const wxTreeItemId
& item
) const;
218 // get the data associated with the item
219 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
225 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
226 // set the normal item image
227 void SetItemImage(const wxTreeItemId
& item
, int image
);
228 // set the selected item image
229 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
);
230 // associate some data with the item
231 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
233 // force appearance of [+] button near the item. This is useful to
234 // allow the user to expand the items which don't have any children now
235 // - but instead add them only when needed, thus minimizing memory
236 // usage and loading time.
237 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
239 // the item will be shown in bold
240 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
242 // item status inquiries
243 // ---------------------
245 // is the item visible (it might be outside the view or not expanded)?
246 bool IsVisible(const wxTreeItemId
& item
) const;
247 // does the item has any children?
248 bool ItemHasChildren(const wxTreeItemId
& item
) const;
249 // is the item expanded (only makes sense if HasChildren())?
250 bool IsExpanded(const wxTreeItemId
& item
) const;
251 // is this item currently selected (the same as has focus)?
252 bool IsSelected(const wxTreeItemId
& item
) const;
253 // is item text in bold font?
254 bool IsBold(const wxTreeItemId
& item
) const;
256 // number of children
257 // ------------------
259 // if 'recursively' is FALSE, only immediate children count, otherwise
260 // the returned number is the number of all items in this branch
261 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
266 // wxTreeItemId.IsOk() will return FALSE if there is no such item
268 // get the root tree item
269 wxTreeItemId
GetRootItem() const;
271 // get the item currently selected (may return NULL if no selection)
272 wxTreeItemId
GetSelection() const;
274 // get the parent of this item (may return NULL if root)
275 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
277 // for this enumeration function you must pass in a "cookie" parameter
278 // which is opaque for the application but is necessary for the library
279 // to make these functions reentrant (i.e. allow more than one
280 // enumeration on one and the same object simultaneously). Of course,
281 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
284 // get the first child of this item
285 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& _cookie
) const;
286 // get the next child
287 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& _cookie
) const;
289 // get the next sibling of this item
290 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
291 // get the previous sibling
292 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
294 // get first visible item
295 wxTreeItemId
GetFirstVisibleItem() const;
296 // get the next visible item: item must be visible itself!
297 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
298 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
299 // get the previous visible item: item must be visible itself!
300 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
305 // add the root node to the tree
306 wxTreeItemId
AddRoot(const wxString
& text
,
307 int image
= -1, int selectedImage
= -1,
308 wxTreeItemData
*data
= NULL
);
310 // insert a new item in as the first child of the parent
311 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
312 const wxString
& text
,
313 int image
= -1, int selectedImage
= -1,
314 wxTreeItemData
*data
= NULL
);
316 // insert a new item after a given one
317 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
318 const wxTreeItemId
& idPrevious
,
319 const wxString
& text
,
320 int image
= -1, int selectedImage
= -1,
321 wxTreeItemData
*data
= NULL
);
323 // insert a new item in as the last child of the parent
324 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
325 const wxString
& text
,
326 int image
= -1, int selectedImage
= -1,
327 wxTreeItemData
*data
= NULL
);
329 // delete this item and associated data if any
330 void Delete(const wxTreeItemId
& item
);
331 // delete all items from the tree
332 void DeleteAllItems();
335 void Expand(const wxTreeItemId
& item
);
336 // collapse the item without removing its children
337 void Collapse(const wxTreeItemId
& item
);
338 // collapse the item and remove all children
339 void CollapseAndReset(const wxTreeItemId
& item
);
340 // toggles the current state
341 void Toggle(const wxTreeItemId
& item
);
343 // remove the selection from currently selected item (if any)
346 void SelectItem(const wxTreeItemId
& item
);
347 // make sure this item is visible (expanding the parent item and/or
348 // scrolling to this item if necessary)
349 void EnsureVisible(const wxTreeItemId
& item
);
350 // scroll to this item (but don't expand its parent)
351 void ScrollTo(const wxTreeItemId
& item
);
353 // start editing the item label: this (temporarily) replaces the item
354 // with a one line edit control. The item will be selected if it hadn't
355 // been before. textCtrlClass parameter allows you to create an edit
356 // control of arbitrary user-defined class deriving from wxTextCtrl.
357 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
358 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
359 // returns the same pointer as StartEdit() if the item is being edited,
360 // NULL otherwise (it's assumed that no more than one item may be
361 // edited simultaneously)
362 wxTextCtrl
* GetEditControl() const;
363 // end editing and accept or discard the changes to item label
364 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
366 // sort the children of this item using the specified callback function
367 // (it should return -1, 0 or +1 as usual), if it's not specified
368 // alphabetical comparaison is performed.
370 // NB: this function is not reentrant!
371 void SortChildren(const wxTreeItemId
& item
,
372 wxTreeItemCmpFunc
*cmpFunction
= NULL
);
377 // determine to which item (if any) belongs the given point (the
378 // coordinates specified are relative to the client area of tree ctrl)
379 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
382 // The first function is more portable (because easier to implement
383 // on other platforms), but the second one returns some extra info.
384 wxTreeItemId
HitTest(const wxPoint
& point
)
385 { int dummy
; return HitTest(point
, dummy
); }
386 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
388 // get the bounding rectangle of the item (or of its label only)
389 // @@@ do we really need to expose this functions to the application?
390 void GetBoundingRect(const wxTreeItemId
& item
,
392 bool textOnly
= FALSE
) const;
397 // these methods are deprecated and will be removed in future versions of
398 // wxWindows, they're here for compatibility only, don't use them in new
399 // code (the comments indicate why these methods are now useless and how to
402 // use Expand, Collapse, CollapseAndReset or Toggle
403 void ExpandItem(const wxTreeItemId
& item
, int action
);
405 // use AddRoot, PrependItem or AppendItem
406 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
407 const wxString
& text
,
408 int image
= -1, int selImage
= -1,
409 long insertAfter
= wxTREE_INSERT_LAST
);
411 // use Set/GetImageList and Set/GetStateImageList
412 wxImageList
*GetImageList(int) const
413 { return GetImageList(); }
414 void SetImageList(wxImageList
*imageList
, int)
415 { SetImageList(imageList
); }
419 void Command(wxCommandEvent
& event
) { ProcessCommand(event
); };
420 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
421 virtual bool MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
*result
);
424 // SetImageList helper
425 void SetAnyImageList(wxImageList
*imageList
, int which
);
427 wxTextCtrl
* m_textCtrl
; // used while editing the item label
428 wxImageList
*m_imageListNormal
, // images for tree elements
429 *m_imageListState
; // special images for app defined states
432 // the common part of all ctors
436 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
437 inline void DoSetItem(wxTreeViewItem
*tvItem
);
439 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
441 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
442 wxTreeItemId hInsertAfter
,
443 const wxString
& text
,
444 int image
, int selectedImage
,
445 wxTreeItemData
*data
);
447 void DeleteTextCtrl();
449 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
452 // ----------------------------------------------------------------------------
453 // wxTreeEvent is a special class for all events associated with tree controls
455 // NB: note that not all accessors make sense for all events, see the event
456 // descriptions below
457 // ----------------------------------------------------------------------------
458 class WXDLLEXPORT wxTreeEvent
: public wxNotifyEvent
462 wxTreeEvent(wxEventType commandType
= wxEVT_NULL
, int id
= 0);
465 // get the item on which the operation was performed or the newly
466 // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events
467 wxTreeItemId
GetItem() const { return m_item
; }
469 // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously
471 wxTreeItemId
GetOldItem() const { return m_itemOld
; }
473 // the point where the mouse was when the drag operation started (for
474 // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only)
475 wxPoint
GetPoint() const { return m_pointDrag
; }
477 // keyboard code (for wxEVT_COMMAND_TREE_KEY_DOWN only)
478 int GetCode() const { return m_code
; }
481 // @@ we could save some space by using union here
487 DECLARE_DYNAMIC_CLASS(wxTreeEvent
)
490 typedef void (wxEvtHandler::*wxTreeEventFunction
)(wxTreeEvent
&);
492 // ----------------------------------------------------------------------------
493 // macros for handling tree control events
494 // ----------------------------------------------------------------------------
496 // GetItem() returns the item being dragged, GetPoint() the mouse coords
497 #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
498 #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
500 // GetItem() returns the itme whose label is being edited
501 #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
502 #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
504 // provide/update information about GetItem() item
505 #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
506 #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
508 // GetItem() is the item being expanded/collapsed, the "ING" versions can use
509 #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
510 #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
511 #define EVT_TREE_ITEM_COLLAPSED(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
512 #define EVT_TREE_ITEM_COLLAPSING(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
514 // GetOldItem() is the item which had the selection previously, GetItem() is
515 // the item which acquires selection
516 #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
517 #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
519 // GetCode() returns the key code
520 // NB: this is the only message for which GetItem() is invalid (you may get the
521 // item from GetSelection())
522 #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
524 // GetItem() returns the item being deleted, the associated data (if any) will
525 // be deleted just after the return of this event handler (if any)
526 #define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },