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"
26 #undef GetFirstChild()
27 #undef GetNextSibling()
30 // the type for "untyped" data
31 typedef long wxDataType
;
34 class WXDLLEXPORT wxImageList
;
35 struct WXDLLEXPORT wxTreeViewItem
;
37 // a callback function used for sorting tree items, it should return -1 if the
38 // first item precedes the second, +1 if the second precedes the first or 0 if
41 typedef int (*wxTreeItemCmpFunc
)(wxTreeItemData
*item1
, wxTreeItemData
*item2
);
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
48 // where exactly the specified point is situated:
49 // above the client area.
50 static const int wxTREE_HITTEST_ABOVE
= 0x0001;
51 // below the client area.
52 static const int wxTREE_HITTEST_BELOW
= 0x0002;
53 // in the client area but below the last item.
54 static const int wxTREE_HITTEST_NOWHERE
= 0x0004;
55 // on the button associated with an item.
56 static const int wxTREE_HITTEST_ONITEMBUTTON
= 0x0010;
57 // on the bitmap associated with an item.
58 static const int wxTREE_HITTEST_ONITEMICON
= 0x0020;
59 // in the indentation associated with an item.
60 static const int wxTREE_HITTEST_ONITEMINDENT
= 0x0040;
61 // on the label (string) associated with an item.
62 static const int wxTREE_HITTEST_ONITEMLABEL
= 0x0080;
63 // in the area to the right of an item.
64 static const int wxTREE_HITTEST_ONITEMRIGHT
= 0x0100;
65 // on the state icon for a tree view item that is in a user-defined state.
66 static const int wxTREE_HITTEST_ONITEMSTATEICON
= 0x0200;
67 // to the right of the client area.
68 static const int wxTREE_HITTEST_TOLEFT
= 0x0400;
69 // to the left of the client area.
70 static const int wxTREE_HITTEST_TORIGHT
= 0x0800;
71 // anywhere on the item
72 static const int wxTREE_HITTEST_ONITEM
= wxTREE_HITTEST_ONITEMICON
|
73 wxTREE_HITTEST_ONITEMLABEL
|
74 wxTREE_HITTEST_ONITEMSTATEICON
;
76 // NB: all the following flags are for compatbility only and will be removed in the
79 // flags for deprecated `Expand(int action)'
83 wxTREE_EXPAND_COLLAPSE
,
84 wxTREE_EXPAND_COLLAPSE_RESET
,
88 // flags for deprecated InsertItem() variant
89 #define wxTREE_INSERT_FIRST 0xFFFF0001
90 #define wxTREE_INSERT_LAST 0xFFFF0002
92 // ----------------------------------------------------------------------------
93 // wxTreeItemId identifies an element of the tree. In this implementation, it's
94 // just a trivial wrapper around Win32 HTREEITEM. It's opaque for the
96 // ----------------------------------------------------------------------------
97 class WXDLLEXPORT wxTreeItemId
101 // 0 is invalid value for HTREEITEM
102 wxTreeItemId() { m_itemId
= 0; }
104 // default copy ctor/assignment operator are ok for us
107 // is this a valid tree item?
108 bool IsOk() const { return m_itemId
!= 0; }
110 // conversion to/from either real (system-dependent) tree item id or
111 // to "long" which used to be the type for tree item ids in previous
112 // versions of wxWindows
114 // for wxTreeCtrl usage only
115 wxTreeItemId(WXHTREEITEM itemId
) { m_itemId
= (long)itemId
; }
116 operator WXHTREEITEM() const { return (WXHTREEITEM
)m_itemId
; }
118 void operator=(WXHTREEITEM item
) { m_itemId
= (long) item
; }
124 // ----------------------------------------------------------------------------
125 // wxTreeItemData is some (arbitrary) user class associated with some item. The
126 // main advantage of having this class (compared to old untyped interface) is
127 // that wxTreeItemData's are destroyed automatically by the tree and, as this
128 // class has virtual dtor, it means that the memory will be automatically
129 // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
130 // the size of this class is critical: in any real application, each tree leaf
131 // will have wxTreeItemData associated with it and number of leaves may be
134 // Because the objects of this class are deleted by the tree, they should
135 // always be allocated on the heap!
136 // ----------------------------------------------------------------------------
137 class WXDLLEXPORT wxTreeItemData
: private wxTreeItemId
140 // default ctor/copy ctor/assignment operator are ok
142 // dtor is virtual and all the items are deleted by the tree control when
143 // it's deleted, so you normally don't have to care about freeing memory
144 // allocated in your wxTreeItemData-derived class
145 virtual ~wxTreeItemData() { }
147 // accessors: set/get the item associated with this node
148 void SetId(const wxTreeItemId
& id
) { m_itemId
= id
; }
149 const wxTreeItemId
& GetId() const { return (wxTreeItemId
&) m_itemId
; }
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
155 class WXDLLEXPORT wxTreeCtrl
: public wxControl
160 wxTreeCtrl() { Init(); }
162 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
163 const wxPoint
& pos
= wxDefaultPosition
,
164 const wxSize
& size
= wxDefaultSize
,
165 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
166 const wxValidator
& validator
= wxDefaultValidator
,
167 const wxString
& name
= "wxTreeCtrl")
169 Create(parent
, id
, pos
, size
, style
, validator
, name
);
172 virtual ~wxTreeCtrl();
174 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
175 const wxPoint
& pos
= wxDefaultPosition
,
176 const wxSize
& size
= wxDefaultSize
,
177 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
178 const wxValidator
& validator
= wxDefaultValidator
,
179 const wxString
& name
= "wxTreeCtrl");
184 // get the total number of items in the control
185 size_t GetCount() const;
187 // indent is the number of pixels the children are indented relative to
188 // the parents position. SetIndent() also redraws the control
190 unsigned int GetIndent() const;
191 void SetIndent(unsigned int indent
);
193 // image list: these functions allow to associate an image list with
194 // the control and retrieve it. Note that the control does _not_ delete
195 // the associated image list when it's deleted in order to allow image
196 // lists to be shared between different controls.
198 // The normal image list is for the icons which correspond to the
199 // normal tree item state (whether it is selected or not).
200 // Additionally, the application might choose to show a state icon
201 // which corresponds to an app-defined item state (for example,
202 // checked/unchecked) which are taken from the state image list.
203 wxImageList
*GetImageList() const;
204 wxImageList
*GetStateImageList() const;
206 void SetImageList(wxImageList
*imageList
);
207 void SetStateImageList(wxImageList
*imageList
);
209 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
210 // member functions of wxTreeItem because they must know the tree the item
211 // belongs to for Windows implementation and storing the pointer to
212 // wxTreeCtrl in each wxTreeItem is just too much waste.
217 // retrieve items label
218 wxString
GetItemText(const wxTreeItemId
& item
) const;
219 // get the normal item image
220 int GetItemImage(const wxTreeItemId
& item
) const;
221 // get the selected item image
222 int GetItemSelectedImage(const wxTreeItemId
& item
) const;
223 // get the data associated with the item
224 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
230 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
231 // set the normal item image
232 void SetItemImage(const wxTreeItemId
& item
, int image
);
233 // set the selected item image
234 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
);
235 // associate some data with the item
236 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
238 // force appearance of [+] button near the item. This is useful to
239 // allow the user to expand the items which don't have any children now
240 // - but instead add them only when needed, thus minimizing memory
241 // usage and loading time.
242 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
244 // the item will be shown in bold
245 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
247 // item status inquiries
248 // ---------------------
250 // is the item visible (it might be outside the view or not expanded)?
251 bool IsVisible(const wxTreeItemId
& item
) const;
252 // does the item has any children?
253 bool ItemHasChildren(const wxTreeItemId
& item
) const;
254 // is the item expanded (only makes sense if HasChildren())?
255 bool IsExpanded(const wxTreeItemId
& item
) const;
256 // is this item currently selected (the same as has focus)?
257 bool IsSelected(const wxTreeItemId
& item
) const;
258 // is item text in bold font?
259 bool IsBold(const wxTreeItemId
& item
) const;
261 // number of children
262 // ------------------
264 // if 'recursively' is FALSE, only immediate children count, otherwise
265 // the returned number is the number of all items in this branch
266 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
271 // wxTreeItemId.IsOk() will return FALSE if there is no such item
273 // get the root tree item
274 wxTreeItemId
GetRootItem() const;
276 // get the item currently selected (may return NULL if no selection)
277 wxTreeItemId
GetSelection() const;
279 // get the parent of this item (may return NULL if root)
280 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
282 // for this enumeration function you must pass in a "cookie" parameter
283 // which is opaque for the application but is necessary for the library
284 // to make these functions reentrant (i.e. allow more than one
285 // enumeration on one and the same object simultaneously). Of course,
286 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
289 // get the first child of this item
290 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& _cookie
) const;
291 // get the next child
292 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& _cookie
) const;
294 // get the next sibling of this item
295 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
296 // get the previous sibling
297 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
299 // get first visible item
300 wxTreeItemId
GetFirstVisibleItem() const;
301 // get the next visible item: item must be visible itself!
302 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
303 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
304 // get the previous visible item: item must be visible itself!
305 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
310 // add the root node to the tree
311 wxTreeItemId
AddRoot(const wxString
& text
,
312 int image
= -1, int selectedImage
= -1,
313 wxTreeItemData
*data
= NULL
);
315 // insert a new item in as the first child of the parent
316 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
317 const wxString
& text
,
318 int image
= -1, int selectedImage
= -1,
319 wxTreeItemData
*data
= NULL
);
321 // insert a new item after a given one
322 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
323 const wxTreeItemId
& idPrevious
,
324 const wxString
& text
,
325 int image
= -1, int selectedImage
= -1,
326 wxTreeItemData
*data
= NULL
);
328 // insert a new item in as the last child of the parent
329 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
330 const wxString
& text
,
331 int image
= -1, int selectedImage
= -1,
332 wxTreeItemData
*data
= NULL
);
334 // delete this item and associated data if any
335 void Delete(const wxTreeItemId
& item
);
336 // delete all items from the tree
337 void DeleteAllItems();
340 void Expand(const wxTreeItemId
& item
);
341 // collapse the item without removing its children
342 void Collapse(const wxTreeItemId
& item
);
343 // collapse the item and remove all children
344 void CollapseAndReset(const wxTreeItemId
& item
);
345 // toggles the current state
346 void Toggle(const wxTreeItemId
& item
);
348 // remove the selection from currently selected item (if any)
351 void SelectItem(const wxTreeItemId
& item
);
352 // make sure this item is visible (expanding the parent item and/or
353 // scrolling to this item if necessary)
354 void EnsureVisible(const wxTreeItemId
& item
);
355 // scroll to this item (but don't expand its parent)
356 void ScrollTo(const wxTreeItemId
& item
);
358 // start editing the item label: this (temporarily) replaces the item
359 // with a one line edit control. The item will be selected if it hadn't
360 // been before. textCtrlClass parameter allows you to create an edit
361 // control of arbitrary user-defined class deriving from wxTextCtrl.
362 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
363 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
364 // returns the same pointer as StartEdit() if the item is being edited,
365 // NULL otherwise (it's assumed that no more than one item may be
366 // edited simultaneously)
367 wxTextCtrl
* GetEditControl() const;
368 // end editing and accept or discard the changes to item label
369 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
371 // sort the children of this item using the specified callback function
372 // (it should return -1, 0 or +1 as usual), if it's not specified
373 // alphabetical comparaison is performed.
375 // NB: this function is not reentrant!
376 void SortChildren(const wxTreeItemId
& item
,
377 wxTreeItemCmpFunc
*cmpFunction
= NULL
);
382 // determine to which item (if any) belongs the given point (the
383 // coordinates specified are relative to the client area of tree ctrl)
384 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
387 // The first function is more portable (because easier to implement
388 // on other platforms), but the second one returns some extra info.
389 wxTreeItemId
HitTest(const wxPoint
& point
)
390 { int dummy
; return HitTest(point
, dummy
); }
391 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
393 // get the bounding rectangle of the item (or of its label only)
394 // @@@ do we really need to expose this functions to the application?
395 void GetBoundingRect(const wxTreeItemId
& item
,
397 bool textOnly
= FALSE
) const;
402 // these methods are deprecated and will be removed in future versions of
403 // wxWindows, they're here for compatibility only, don't use them in new
404 // code (the comments indicate why these methods are now useless and how to
407 // use Expand, Collapse, CollapseAndReset or Toggle
408 void ExpandItem(const wxTreeItemId
& item
, int action
);
410 // use AddRoot, PrependItem or AppendItem
411 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
412 const wxString
& text
,
413 int image
= -1, int selImage
= -1,
414 long insertAfter
= wxTREE_INSERT_LAST
);
416 // use Set/GetImageList and Set/GetStateImageList
417 wxImageList
*GetImageList(int) const
418 { return GetImageList(); }
419 void SetImageList(wxImageList
*imageList
, int)
420 { SetImageList(imageList
); }
424 void Command(wxCommandEvent
& event
) { ProcessCommand(event
); };
425 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
426 virtual bool MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
*result
);
429 // SetImageList helper
430 void SetAnyImageList(wxImageList
*imageList
, int which
);
432 wxTextCtrl
* m_textCtrl
; // used while editing the item label
433 wxImageList
*m_imageListNormal
, // images for tree elements
434 *m_imageListState
; // special images for app defined states
437 // the common part of all ctors
441 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
442 inline void DoSetItem(wxTreeViewItem
*tvItem
);
444 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
446 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
447 wxTreeItemId hInsertAfter
,
448 const wxString
& text
,
449 int image
, int selectedImage
,
450 wxTreeItemData
*data
);
452 void DeleteTextCtrl();
454 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
457 // ----------------------------------------------------------------------------
458 // wxTreeEvent is a special class for all events associated with tree controls
460 // NB: note that not all accessors make sense for all events, see the event
461 // descriptions below
462 // ----------------------------------------------------------------------------
463 class WXDLLEXPORT wxTreeEvent
: public wxNotifyEvent
467 wxTreeEvent(wxEventType commandType
= wxEVT_NULL
, int id
= 0);
470 // get the item on which the operation was performed or the newly
471 // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events
472 wxTreeItemId
GetItem() const { return m_item
; }
474 // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously
476 wxTreeItemId
GetOldItem() const { return m_itemOld
; }
478 // the point where the mouse was when the drag operation started (for
479 // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only)
480 wxPoint
GetPoint() const { return m_pointDrag
; }
482 // keyboard code (for wxEVT_COMMAND_TREE_KEY_DOWN only)
483 int GetCode() const { return m_code
; }
486 // @@ we could save some space by using union here
492 DECLARE_DYNAMIC_CLASS(wxTreeEvent
)
495 typedef void (wxEvtHandler::*wxTreeEventFunction
)(wxTreeEvent
&);
497 // ----------------------------------------------------------------------------
498 // macros for handling tree control events
499 // ----------------------------------------------------------------------------
501 // GetItem() returns the item being dragged, GetPoint() the mouse coords
502 #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
503 #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
505 // GetItem() returns the itme whose label is being edited
506 #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
507 #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
509 // provide/update information about GetItem() item
510 #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
511 #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
513 // GetItem() is the item being expanded/collapsed, the "ING" versions can use
514 #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
515 #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
516 #define EVT_TREE_ITEM_COLLAPSED(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
517 #define EVT_TREE_ITEM_COLLAPSING(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
519 // GetOldItem() is the item which had the selection previously, GetItem() is
520 // the item which acquires selection
521 #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
522 #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
524 // GetCode() returns the key code
525 // NB: this is the only message for which GetItem() is invalid (you may get the
526 // item from GetSelection())
527 #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
529 // GetItem() returns the item being deleted, the associated data (if any) will
530 // be deleted just after the return of this event handler (if any)
531 #define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },