1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl class
4 // Author: Robert Roebling
8 // Copyright: (c) 1997,1998 Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _GENERIC_TREECTRL_H_
13 #define _GENERIC_TREECTRL_H_
16 #pragma interface "treectrl.h"
20 #include "wx/string.h"
21 #include "wx/object.h"
23 #include "wx/scrolwin.h"
24 #include "wx/textctrl.h"
26 // -----------------------------------------------------------------------------
27 // forward declaration
28 // -----------------------------------------------------------------------------
31 class wxGenericTreeItem
;
34 typedef int (*wxTreeItemCmpFunc
)(wxTreeItemData
*item1
, wxTreeItemData
*item2
);
36 // -----------------------------------------------------------------------------
37 // wxTreeItemId - unique identifier of a tree element
38 // -----------------------------------------------------------------------------
40 class WXDLLEXPORT wxTreeItemId
42 friend class wxTreeCtrl
;
43 friend class wxTreeEvent
;
46 // 0 is invalid value for HTREEITEM
47 wxTreeItemId() { m_pItem
= 0; }
49 // default copy ctor/assignment operator are ok for us
52 // is this a valid tree item?
53 bool IsOk() const { return m_pItem
!= 0; }
55 // deprecated: only for compatibility
56 wxTreeItemId(long itemId
) { m_pItem
= (wxGenericTreeItem
*)itemId
; }
57 operator long() const { return (long)m_pItem
; }
59 //protected: // not for gcc
60 // for wxTreeCtrl usage only
61 wxTreeItemId(wxGenericTreeItem
*pItem
) { m_pItem
= pItem
; }
63 wxGenericTreeItem
*m_pItem
;
66 // ----------------------------------------------------------------------------
67 // wxTreeItemData is some (arbitrary) user class associated with some item.
69 // Because the objects of this class are deleted by the tree, they should
70 // always be allocated on the heap!
71 // ----------------------------------------------------------------------------
72 class WXDLLEXPORT wxTreeItemData
: public wxClientData
74 friend class wxTreeCtrl
;
76 // creation/destruction
77 // --------------------
81 // default copy ctor/assignment operator are ok
83 // accessor: get the item associated with us
84 const wxTreeItemId
& GetId() const { return m_pItem
; }
85 void SetId(const wxTreeItemId
& id
) { m_pItem
= id
; }
91 // -----------------------------------------------------------------------------
92 // wxTreeEvent - the event generated by the tree control
93 // -----------------------------------------------------------------------------
94 class WXDLLEXPORT wxTreeEvent
: public wxNotifyEvent
96 friend class wxTreeCtrl
;
98 wxTreeEvent(wxEventType commandType
= wxEVT_NULL
, int id
= 0);
101 // get the item on which the operation was performed or the newly
102 // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events
103 wxTreeItemId
GetItem() const { return m_item
; }
105 // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously
107 wxTreeItemId
GetOldItem() const { return m_itemOld
; }
109 // the point where the mouse was when the drag operation started (for
110 // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only)
111 wxPoint
GetPoint() const { return m_pointDrag
; }
113 // keyboard code (for wxEVT_COMMAND_TREE_KEY_DOWN only)
114 int GetCode() const { return m_code
; }
116 // set return code for wxEVT_COMMAND_TREE_ITEM_{EXPAND|COLLAPS}ING events
117 // call this to forbid the change in item status
118 void Veto() { m_code
= TRUE
; }
120 // for implementation usage only
121 bool WasVetoed() const { return m_code
; }
124 // @@ we could save some space by using union here
130 DECLARE_DYNAMIC_CLASS(wxTreeEvent
)
133 typedef void (wxEvtHandler::*wxTreeEventFunction
)(wxTreeEvent
&);
135 // ----------------------------------------------------------------------------
136 // macros for handling tree control events
137 // ----------------------------------------------------------------------------
139 // GetItem() returns the item being dragged, GetPoint() the mouse coords
140 #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
141 #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
143 // GetItem() returns the itme whose label is being edited
144 #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
145 #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
147 // provide/update information about GetItem() item
148 #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
149 #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
151 // GetItem() is the item being expanded/collapsed, the "ING" versions can use
152 #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
153 #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
154 #define EVT_TREE_ITEM_COLLAPSED(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
155 #define EVT_TREE_ITEM_COLLAPSING(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
157 // GetOldItem() is the item which had the selection previously, GetItem() is
158 // the item which acquires selection
159 #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
160 #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
162 // GetCode() returns the key code
163 // NB: this is the only message for which GetItem() is invalid (you may get the
164 // item from GetSelection())
165 #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
167 // GetItem() returns the item being deleted, the associated data (if any) will
168 // be deleted just after the return of this event handler (if any)
169 #define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
171 // GetItem() returns the item that was activated (double click, enter, space)
172 #define EVT_TREE_ITEM_ACTIVATED(id, fn) { wxEVT_COMMAND_TREE_ITEM_ACTIVATED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
174 // -----------------------------------------------------------------------------
175 // wxTreeCtrl - the tree control
176 // -----------------------------------------------------------------------------
178 class WXDLLEXPORT wxTreeCtrl
: public wxScrolledWindow
183 wxTreeCtrl() { Init(); }
185 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
186 const wxPoint
& pos
= wxDefaultPosition
,
187 const wxSize
& size
= wxDefaultSize
,
188 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
189 const wxString
& name
= "wxTreeCtrl")
191 Create(parent
, id
, pos
, size
, style
, name
);
194 virtual ~wxTreeCtrl();
196 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
197 const wxPoint
& pos
= wxDefaultPosition
,
198 const wxSize
& size
= wxDefaultSize
,
199 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
200 const wxString
& name
= "wxTreeCtrl");
205 // get the total number of items in the control
206 size_t GetCount() const;
208 // indent is the number of pixels the children are indented relative to
209 // the parents position. SetIndent() also redraws the control
211 unsigned int GetIndent() const { return m_indent
; }
212 void SetIndent(unsigned int indent
);
214 // image list: these functions allow to associate an image list with
215 // the control and retrieve it. Note that the control does _not_ delete
216 // the associated image list when it's deleted in order to allow image
217 // lists to be shared between different controls.
219 // The normal image list is for the icons which correspond to the
220 // normal tree item state (whether it is selected or not).
221 // Additionally, the application might choose to show a state icon
222 // which corresponds to an app-defined item state (for example,
223 // checked/unchecked) which are taken from the state image list.
224 wxImageList
*GetImageList() const;
225 wxImageList
*GetStateImageList() const;
227 void SetImageList(wxImageList
*imageList
);
228 void SetStateImageList(wxImageList
*imageList
);
230 // Functions to work with tree ctrl items.
235 // retrieve items label
236 wxString
GetItemText(const wxTreeItemId
& item
) const;
237 // get the normal item image
238 int GetItemImage(const wxTreeItemId
& item
) const;
239 // get the selected item image
240 int GetItemSelectedImage(const wxTreeItemId
& item
) const;
241 // get the data associated with the item
242 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
248 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
249 // set the normal item image
250 void SetItemImage(const wxTreeItemId
& item
, int image
);
251 // set the selected item image
252 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
);
253 // associate some data with the item
254 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
256 // force appearance of [+] button near the item. This is useful to
257 // allow the user to expand the items which don't have any children now
258 // - but instead add them only when needed, thus minimizing memory
259 // usage and loading time.
260 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
262 // the item will be shown in bold
263 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
265 // item status inquiries
266 // ---------------------
268 // is the item visible (it might be outside the view or not expanded)?
269 bool IsVisible(const wxTreeItemId
& item
) const;
270 // does the item has any children?
271 bool HasChildren(const wxTreeItemId
& item
) const
272 { return ItemHasChildren(item
); }
273 bool ItemHasChildren(const wxTreeItemId
& item
) const;
274 // is the item expanded (only makes sense if HasChildren())?
275 bool IsExpanded(const wxTreeItemId
& item
) const;
276 // is this item currently selected (the same as has focus)?
277 bool IsSelected(const wxTreeItemId
& item
) const;
278 // is item text in bold font?
279 bool IsBold(const wxTreeItemId
& item
) const;
281 // number of children
282 // ------------------
284 // if 'recursively' is FALSE, only immediate children count, otherwise
285 // the returned number is the number of all items in this branch
286 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
291 // wxTreeItemId.IsOk() will return FALSE if there is no such item
293 // get the root tree item
294 wxTreeItemId
GetRootItem() const { return m_anchor
; }
296 // get the item currently selected (may return NULL if no selection)
297 wxTreeItemId
GetSelection() const { return m_current
; }
299 // get the parent of this item (may return NULL if root)
300 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
302 // for this enumeration function you must pass in a "cookie" parameter
303 // which is opaque for the application but is necessary for the library
304 // to make these functions reentrant (i.e. allow more than one
305 // enumeration on one and the same object simultaneously). Of course,
306 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
309 // get the first child of this item
310 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const;
311 // get the next child
312 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& cookie
) const;
314 // get the next sibling of this item
315 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
316 // get the previous sibling
317 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
319 // get first visible item
320 wxTreeItemId
GetFirstVisibleItem() const;
321 // get the next visible item: item must be visible itself!
322 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
323 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
324 // get the previous visible item: item must be visible itself!
325 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
330 // add the root node to the tree
331 wxTreeItemId
AddRoot(const wxString
& text
,
332 int image
= -1, int selectedImage
= -1,
333 wxTreeItemData
*data
= NULL
);
335 // insert a new item in as the first child of the parent
336 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
337 const wxString
& text
,
338 int image
= -1, int selectedImage
= -1,
339 wxTreeItemData
*data
= NULL
);
341 // insert a new item after a given one
342 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
343 const wxTreeItemId
& idPrevious
,
344 const wxString
& text
,
345 int image
= -1, int selectedImage
= -1,
346 wxTreeItemData
*data
= NULL
);
348 // insert a new item in as the last child of the parent
349 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
350 const wxString
& text
,
351 int image
= -1, int selectedImage
= -1,
352 wxTreeItemData
*data
= NULL
);
354 // delete this item and associated data if any
355 void Delete(const wxTreeItemId
& item
);
356 // delete all items from the tree
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
);
391 // sort the children of this item using the specified callback function
392 // (it should return -1, 0 or +1 as usual), if it's not specified
393 // alphabetical comparaison is performed.
395 // NB: this function is not reentrant!
396 void SortChildren(const wxTreeItemId
& item
,
397 wxTreeItemCmpFunc
*cmpFunction
= NULL
);
399 void OnPaint( wxPaintEvent
&event
);
400 void OnSetFocus( wxFocusEvent
&event
);
401 void OnKillFocus( wxFocusEvent
&event
);
402 void OnChar( wxKeyEvent
&event
);
403 void OnMouse( wxMouseEvent
&event
);
404 void OnIdle( wxIdleEvent
&event
);
407 wxGenericTreeItem
*m_anchor
;
408 wxGenericTreeItem
*m_current
;
411 int m_xScroll
,m_yScroll
;
412 unsigned int m_indent
;
415 wxBrush
*m_hilightBrush
;
416 wxImageList
*m_imageListNormal
,
419 // the common part of all ctors
423 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
425 const wxString
& text
,
426 int image
, int selectedImage
,
427 wxTreeItemData
*data
);
429 void AdjustMyScrollbars();
430 void PaintLevel( wxGenericTreeItem
*item
, wxDC
& dc
, int level
, int &y
);
431 void PaintItem( wxGenericTreeItem
*item
, wxDC
& dc
);
433 void CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
);
434 void CalculatePositions();
436 void RefreshSubtree( wxGenericTreeItem
*item
);
437 void RefreshLine( wxGenericTreeItem
*item
);
440 DECLARE_EVENT_TABLE()
441 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
444 #endif // _GENERIC_TREECTRL_H_