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 WXDLLEXPORT_DATA(extern const char*) wxTreeCtrlNameStr
;
22 #define wxTreeCtrlNameStr "wxTreeCtrl"
26 #include "wx/string.h"
27 #include "wx/object.h"
29 #include "wx/scrolwin.h"
30 #include "wx/textctrl.h"
33 // -----------------------------------------------------------------------------
35 // -----------------------------------------------------------------------------
37 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
38 // where exactly the specified point is situated:
39 static const int wxTREE_HITTEST_NOWHERE
= 0x0004;
40 // on the bitmap associated with an item.
41 static const int wxTREE_HITTEST_ONITEMICON
= 0x0020;
42 // on the label (string) associated with an item.
43 static const int wxTREE_HITTEST_ONITEMLABEL
= 0x0080;
44 // anywhere on the item
45 static const int wxTREE_HITTEST_ONITEM
= wxTREE_HITTEST_ONITEMICON
|
46 wxTREE_HITTEST_ONITEMLABEL
;
48 // -----------------------------------------------------------------------------
49 // forward declaration
50 // -----------------------------------------------------------------------------
53 class wxGenericTreeItem
;
57 // -----------------------------------------------------------------------------
58 // wxTreeItemId - unique identifier of a tree element
59 // -----------------------------------------------------------------------------
61 class WXDLLEXPORT wxTreeItemId
63 friend class wxTreeCtrl
;
64 friend class wxTreeEvent
;
67 // 0 is invalid value for HTREEITEM
68 wxTreeItemId() { m_pItem
= 0; }
70 // default copy ctor/assignment operator are ok for us
73 // is this a valid tree item?
74 bool IsOk() const { return m_pItem
!= 0; }
76 // deprecated: only for compatibility
77 wxTreeItemId(long itemId
) { m_pItem
= (wxGenericTreeItem
*)itemId
; }
78 operator long() const { return (long)m_pItem
; }
80 //protected: // not for gcc
81 // for wxTreeCtrl usage only
82 wxTreeItemId(wxGenericTreeItem
*pItem
) { m_pItem
= pItem
; }
84 wxGenericTreeItem
*m_pItem
;
87 // ----------------------------------------------------------------------------
88 // wxTreeItemData is some (arbitrary) user class associated with some item.
90 // Because the objects of this class are deleted by the tree, they should
91 // always be allocated on the heap!
92 // ----------------------------------------------------------------------------
93 class WXDLLEXPORT wxTreeItemData
: public wxClientData
95 friend class wxTreeCtrl
;
97 // creation/destruction
98 // --------------------
102 // default copy ctor/assignment operator are ok
104 // accessor: get the item associated with us
105 const wxTreeItemId
& GetId() const { return m_pItem
; }
106 void SetId(const wxTreeItemId
& id
) { m_pItem
= id
; }
109 wxTreeItemId m_pItem
;
112 // -----------------------------------------------------------------------------
113 // wxTreeEvent - the event generated by the tree control
114 // -----------------------------------------------------------------------------
115 class WXDLLEXPORT wxTreeEvent
: public wxNotifyEvent
117 friend class wxTreeCtrl
;
119 wxTreeEvent(wxEventType commandType
= wxEVT_NULL
, int id
= 0);
122 // get the item on which the operation was performed or the newly
123 // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events
124 wxTreeItemId
GetItem() const { return m_item
; }
126 // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously
128 wxTreeItemId
GetOldItem() const { return m_itemOld
; }
130 // the point where the mouse was when the drag operation started (for
131 // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only)
132 wxPoint
GetPoint() const { return m_pointDrag
; }
134 // keyboard code (for wxEVT_COMMAND_TREE_KEY_DOWN only)
135 int GetCode() const { return m_code
; }
137 // set return code for wxEVT_COMMAND_TREE_ITEM_{EXPAND|COLLAPS}ING events
138 // call this to forbid the change in item status
139 void Veto() { m_code
= TRUE
; }
141 // for implementation usage only
142 bool WasVetoed() const { return m_code
; }
145 // @@ we could save some space by using union here
151 DECLARE_DYNAMIC_CLASS(wxTreeEvent
)
154 typedef void (wxEvtHandler::*wxTreeEventFunction
)(wxTreeEvent
&);
156 // ----------------------------------------------------------------------------
157 // macros for handling tree control events
158 // ----------------------------------------------------------------------------
160 // GetItem() returns the item being dragged, GetPoint() the mouse coords
161 #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
162 #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
164 // GetItem() returns the itme whose label is being edited
165 #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
166 #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
168 // provide/update information about GetItem() item
169 #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
170 #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
172 // GetItem() is the item being expanded/collapsed, the "ING" versions can use
173 #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
174 #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
175 #define EVT_TREE_ITEM_COLLAPSED(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
176 #define EVT_TREE_ITEM_COLLAPSING(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
178 // GetOldItem() is the item which had the selection previously, GetItem() is
179 // the item which acquires selection
180 #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
181 #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
183 // GetCode() returns the key code
184 // NB: this is the only message for which GetItem() is invalid (you may get the
185 // item from GetSelection())
186 #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
188 // GetItem() returns the item being deleted, the associated data (if any) will
189 // be deleted just after the return of this event handler (if any)
190 #define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
192 // GetItem() returns the item that was activated (double click, enter, space)
193 #define EVT_TREE_ITEM_ACTIVATED(id, fn) { wxEVT_COMMAND_TREE_ITEM_ACTIVATED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
195 // -----------------------------------------------------------------------------
196 // wxTreeCtrl - the tree control
197 // -----------------------------------------------------------------------------
199 class WXDLLEXPORT wxTreeCtrl
: public wxScrolledWindow
204 wxTreeCtrl() { Init(); }
206 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
207 const wxPoint
& pos
= wxDefaultPosition
,
208 const wxSize
& size
= wxDefaultSize
,
209 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
210 const wxValidator
&validator
= wxDefaultValidator
,
211 const wxString
& name
= wxTreeCtrlNameStr
)
213 Create(parent
, id
, pos
, size
, style
, validator
, name
);
216 virtual ~wxTreeCtrl();
218 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
219 const wxPoint
& pos
= wxDefaultPosition
,
220 const wxSize
& size
= wxDefaultSize
,
221 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
222 const wxValidator
&validator
= wxDefaultValidator
,
223 const wxString
& name
= wxTreeCtrlNameStr
);
228 // get the total number of items in the control
229 size_t GetCount() const;
231 // indent is the number of pixels the children are indented relative to
232 // the parents position. SetIndent() also redraws the control
234 unsigned int GetIndent() const { return m_indent
; }
235 void SetIndent(unsigned int indent
);
237 // image list: these functions allow to associate an image list with
238 // the control and retrieve it. Note that the control does _not_ delete
239 // the associated image list when it's deleted in order to allow image
240 // lists to be shared between different controls.
242 // The normal image list is for the icons which correspond to the
243 // normal tree item state (whether it is selected or not).
244 // Additionally, the application might choose to show a state icon
245 // which corresponds to an app-defined item state (for example,
246 // checked/unchecked) which are taken from the state image list.
247 wxImageList
*GetImageList() const;
248 wxImageList
*GetStateImageList() const;
250 void SetImageList(wxImageList
*imageList
);
251 void SetStateImageList(wxImageList
*imageList
);
253 // Functions to work with tree ctrl items.
258 // retrieve items label
259 wxString
GetItemText(const wxTreeItemId
& item
) const;
260 // get the normal item image
261 int GetItemImage(const wxTreeItemId
& item
) const;
262 // get the selected item image
263 int GetItemSelectedImage(const wxTreeItemId
& item
) const;
264 // get the data associated with the item
265 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
271 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
272 // set the normal item image
273 void SetItemImage(const wxTreeItemId
& item
, int image
);
274 // set the selected item image
275 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
);
276 // associate some data with the item
277 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
279 // force appearance of [+] button near the item. This is useful to
280 // allow the user to expand the items which don't have any children now
281 // - but instead add them only when needed, thus minimizing memory
282 // usage and loading time.
283 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
285 // the item will be shown in bold
286 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
288 // item status inquiries
289 // ---------------------
291 // is the item visible (it might be outside the view or not expanded)?
292 bool IsVisible(const wxTreeItemId
& item
) const;
293 // does the item has any children?
294 bool HasChildren(const wxTreeItemId
& item
) const
295 { return ItemHasChildren(item
); }
296 bool ItemHasChildren(const wxTreeItemId
& item
) const;
297 // is the item expanded (only makes sense if HasChildren())?
298 bool IsExpanded(const wxTreeItemId
& item
) const;
299 // is this item currently selected (the same as has focus)?
300 bool IsSelected(const wxTreeItemId
& item
) const;
301 // is item text in bold font?
302 bool IsBold(const wxTreeItemId
& item
) const;
304 // number of children
305 // ------------------
307 // if 'recursively' is FALSE, only immediate children count, otherwise
308 // the returned number is the number of all items in this branch
309 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
314 // wxTreeItemId.IsOk() will return FALSE if there is no such item
316 // get the root tree item
317 wxTreeItemId
GetRootItem() const { return m_anchor
; }
319 // get the item currently selected (may return NULL if no selection)
320 wxTreeItemId
GetSelection() const { return m_current
; }
322 // get the parent of this item (may return NULL if root)
323 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
325 // for this enumeration function you must pass in a "cookie" parameter
326 // which is opaque for the application but is necessary for the library
327 // to make these functions reentrant (i.e. allow more than one
328 // enumeration on one and the same object simultaneously). Of course,
329 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
332 // get the first child of this item
333 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const;
334 // get the next child
335 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& cookie
) const;
337 // get the next sibling of this item
338 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
339 // get the previous sibling
340 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
342 // get first visible item
343 wxTreeItemId
GetFirstVisibleItem() const;
344 // get the next visible item: item must be visible itself!
345 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
346 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
347 // get the previous visible item: item must be visible itself!
348 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
353 // add the root node to the tree
354 wxTreeItemId
AddRoot(const wxString
& text
,
355 int image
= -1, int selectedImage
= -1,
356 wxTreeItemData
*data
= NULL
);
358 // insert a new item in as the first child of the parent
359 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
360 const wxString
& text
,
361 int image
= -1, int selectedImage
= -1,
362 wxTreeItemData
*data
= NULL
);
364 // insert a new item after a given one
365 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
366 const wxTreeItemId
& idPrevious
,
367 const wxString
& text
,
368 int image
= -1, int selectedImage
= -1,
369 wxTreeItemData
*data
= NULL
);
371 // insert a new item in as the last child of the parent
372 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
373 const wxString
& text
,
374 int image
= -1, int selectedImage
= -1,
375 wxTreeItemData
*data
= NULL
);
377 // delete this item and associated data if any
378 void Delete(const wxTreeItemId
& item
);
379 // delete all children (but don't delete the item itself)
380 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
381 void DeleteChildren(const wxTreeItemId
& item
);
382 // delete all items from the tree
383 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
384 void DeleteAllItems();
387 void Expand(const wxTreeItemId
& item
);
388 // collapse the item without removing its children
389 void Collapse(const wxTreeItemId
& item
);
390 // collapse the item and remove all children
391 void CollapseAndReset(const wxTreeItemId
& item
);
392 // toggles the current state
393 void Toggle(const wxTreeItemId
& item
);
395 // remove the selection from currently selected item (if any)
398 void SelectItem(const wxTreeItemId
& item
);
399 // make sure this item is visible (expanding the parent item and/or
400 // scrolling to this item if necessary)
401 void EnsureVisible(const wxTreeItemId
& item
);
402 // scroll to this item (but don't expand its parent)
403 void ScrollTo(const wxTreeItemId
& item
);
405 // The first function is more portable (because easier to implement
406 // on other platforms), but the second one returns some extra info.
407 wxTreeItemId
HitTest(const wxPoint
& point
)
408 { int dummy
; return HitTest(point
, dummy
); }
409 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
411 // start editing the item label: this (temporarily) replaces the item
412 // with a one line edit control. The item will be selected if it hadn't
413 // been before. textCtrlClass parameter allows you to create an edit
414 // control of arbitrary user-defined class deriving from wxTextCtrl.
415 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
416 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
417 // returns the same pointer as StartEdit() if the item is being edited,
418 // NULL otherwise (it's assumed that no more than one item may be
419 // edited simultaneously)
420 wxTextCtrl
* GetEditControl() const;
421 // end editing and accept or discard the changes to item label
422 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
425 // this function is called to compare 2 items and should return -1, 0
426 // or +1 if the first item is less than, equal to or greater than the
427 // second one. The base class version performs alphabetic comparaison
428 // of item labels (GetText)
429 virtual int OnCompareItems(const wxTreeItemId
& item1
,
430 const wxTreeItemId
& item2
);
431 // sort the children of this item using OnCompareItems
433 // NB: this function is not reentrant and not MT-safe (FIXME)!
434 void SortChildren(const wxTreeItemId
& item
);
437 void OnPaint( wxPaintEvent
&event
);
438 void OnSetFocus( wxFocusEvent
&event
);
439 void OnKillFocus( wxFocusEvent
&event
);
440 void OnChar( wxKeyEvent
&event
);
441 void OnMouse( wxMouseEvent
&event
);
442 void OnIdle( wxIdleEvent
&event
);
445 void SendDeleteEvent(wxGenericTreeItem
*itemBeingDeleted
);
448 wxGenericTreeItem
*m_anchor
;
449 wxGenericTreeItem
*m_current
;
452 int m_xScroll
,m_yScroll
;
453 unsigned int m_indent
;
456 wxBrush
*m_hilightBrush
;
457 wxImageList
*m_imageListNormal
,
461 // the common part of all ctors
465 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
467 const wxString
& text
,
468 int image
, int selectedImage
,
469 wxTreeItemData
*data
);
471 void AdjustMyScrollbars();
472 void PaintLevel( wxGenericTreeItem
*item
, wxDC
& dc
, int level
, int &y
);
473 void PaintItem( wxGenericTreeItem
*item
, wxDC
& dc
);
475 void CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
);
476 void CalculatePositions();
478 void RefreshSubtree( wxGenericTreeItem
*item
);
479 void RefreshLine( wxGenericTreeItem
*item
);
482 DECLARE_EVENT_TABLE()
483 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
486 #endif // _GENERIC_TREECTRL_H_