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 // -----------------------------------------------------------------------------
28 // -----------------------------------------------------------------------------
30 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
31 // where exactly the specified point is situated:
32 static const int wxTREE_HITTEST_NOWHERE
= 0x0004;
33 // on the bitmap associated with an item.
34 static const int wxTREE_HITTEST_ONITEMICON
= 0x0020;
35 // on the label (string) associated with an item.
36 static const int wxTREE_HITTEST_ONITEMLABEL
= 0x0080;
37 // anywhere on the item
38 static const int wxTREE_HITTEST_ONITEM
= wxTREE_HITTEST_ONITEMICON
|
39 wxTREE_HITTEST_ONITEMLABEL
;
41 // -----------------------------------------------------------------------------
42 // forward declaration
43 // -----------------------------------------------------------------------------
46 class wxGenericTreeItem
;
50 // -----------------------------------------------------------------------------
51 // wxTreeItemId - unique identifier of a tree element
52 // -----------------------------------------------------------------------------
54 class WXDLLEXPORT wxTreeItemId
56 friend class wxTreeCtrl
;
57 friend class wxTreeEvent
;
60 // 0 is invalid value for HTREEITEM
61 wxTreeItemId() { m_pItem
= 0; }
63 // default copy ctor/assignment operator are ok for us
66 // is this a valid tree item?
67 bool IsOk() const { return m_pItem
!= 0; }
69 // deprecated: only for compatibility
70 wxTreeItemId(long itemId
) { m_pItem
= (wxGenericTreeItem
*)itemId
; }
71 operator long() const { return (long)m_pItem
; }
73 //protected: // not for gcc
74 // for wxTreeCtrl usage only
75 wxTreeItemId(wxGenericTreeItem
*pItem
) { m_pItem
= pItem
; }
77 wxGenericTreeItem
*m_pItem
;
80 // ----------------------------------------------------------------------------
81 // wxTreeItemData is some (arbitrary) user class associated with some item.
83 // Because the objects of this class are deleted by the tree, they should
84 // always be allocated on the heap!
85 // ----------------------------------------------------------------------------
86 class WXDLLEXPORT wxTreeItemData
: public wxClientData
88 friend class wxTreeCtrl
;
90 // creation/destruction
91 // --------------------
95 // default copy ctor/assignment operator are ok
97 // accessor: get the item associated with us
98 const wxTreeItemId
& GetId() const { return m_pItem
; }
99 void SetId(const wxTreeItemId
& id
) { m_pItem
= id
; }
102 wxTreeItemId m_pItem
;
105 // -----------------------------------------------------------------------------
106 // wxTreeEvent - the event generated by the tree control
107 // -----------------------------------------------------------------------------
108 class WXDLLEXPORT wxTreeEvent
: public wxNotifyEvent
110 friend class wxTreeCtrl
;
112 wxTreeEvent(wxEventType commandType
= wxEVT_NULL
, int id
= 0);
115 // get the item on which the operation was performed or the newly
116 // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events
117 wxTreeItemId
GetItem() const { return m_item
; }
119 // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously
121 wxTreeItemId
GetOldItem() const { return m_itemOld
; }
123 // the point where the mouse was when the drag operation started (for
124 // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only)
125 wxPoint
GetPoint() const { return m_pointDrag
; }
127 // keyboard code (for wxEVT_COMMAND_TREE_KEY_DOWN only)
128 int GetCode() const { return m_code
; }
130 // set return code for wxEVT_COMMAND_TREE_ITEM_{EXPAND|COLLAPS}ING events
131 // call this to forbid the change in item status
132 void Veto() { m_code
= TRUE
; }
134 // for implementation usage only
135 bool WasVetoed() const { return m_code
; }
138 // @@ we could save some space by using union here
144 DECLARE_DYNAMIC_CLASS(wxTreeEvent
)
147 typedef void (wxEvtHandler::*wxTreeEventFunction
)(wxTreeEvent
&);
149 // ----------------------------------------------------------------------------
150 // macros for handling tree control events
151 // ----------------------------------------------------------------------------
153 // GetItem() returns the item being dragged, GetPoint() the mouse coords
154 #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
155 #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
157 // GetItem() returns the itme whose label is being edited
158 #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
159 #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
161 // provide/update information about GetItem() item
162 #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
163 #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
165 // GetItem() is the item being expanded/collapsed, the "ING" versions can use
166 #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
167 #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
168 #define EVT_TREE_ITEM_COLLAPSED(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
169 #define EVT_TREE_ITEM_COLLAPSING(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
171 // GetOldItem() is the item which had the selection previously, GetItem() is
172 // the item which acquires selection
173 #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
174 #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
176 // GetCode() returns the key code
177 // NB: this is the only message for which GetItem() is invalid (you may get the
178 // item from GetSelection())
179 #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
181 // GetItem() returns the item being deleted, the associated data (if any) will
182 // be deleted just after the return of this event handler (if any)
183 #define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
185 // GetItem() returns the item that was activated (double click, enter, space)
186 #define EVT_TREE_ITEM_ACTIVATED(id, fn) { wxEVT_COMMAND_TREE_ITEM_ACTIVATED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
188 // -----------------------------------------------------------------------------
189 // wxTreeCtrl - the tree control
190 // -----------------------------------------------------------------------------
192 class WXDLLEXPORT wxTreeCtrl
: public wxScrolledWindow
197 wxTreeCtrl() { Init(); }
199 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
200 const wxPoint
& pos
= wxDefaultPosition
,
201 const wxSize
& size
= wxDefaultSize
,
202 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
203 const wxValidator
&validator
= wxDefaultValidator
,
204 const wxString
& name
= "wxTreeCtrl")
206 Create(parent
, id
, pos
, size
, style
, validator
, name
);
209 virtual ~wxTreeCtrl();
211 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
212 const wxPoint
& pos
= wxDefaultPosition
,
213 const wxSize
& size
= wxDefaultSize
,
214 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
215 const wxValidator
&validator
= wxDefaultValidator
,
216 const wxString
& name
= "wxTreeCtrl");
221 // get the total number of items in the control
222 size_t GetCount() const;
224 // indent is the number of pixels the children are indented relative to
225 // the parents position. SetIndent() also redraws the control
227 unsigned int GetIndent() const { return m_indent
; }
228 void SetIndent(unsigned int indent
);
230 // image list: these functions allow to associate an image list with
231 // the control and retrieve it. Note that the control does _not_ delete
232 // the associated image list when it's deleted in order to allow image
233 // lists to be shared between different controls.
235 // The normal image list is for the icons which correspond to the
236 // normal tree item state (whether it is selected or not).
237 // Additionally, the application might choose to show a state icon
238 // which corresponds to an app-defined item state (for example,
239 // checked/unchecked) which are taken from the state image list.
240 wxImageList
*GetImageList() const;
241 wxImageList
*GetStateImageList() const;
243 void SetImageList(wxImageList
*imageList
);
244 void SetStateImageList(wxImageList
*imageList
);
246 // Functions to work with tree ctrl items.
251 // retrieve items label
252 wxString
GetItemText(const wxTreeItemId
& item
) const;
253 // get the normal item image
254 int GetItemImage(const wxTreeItemId
& item
) const;
255 // get the selected item image
256 int GetItemSelectedImage(const wxTreeItemId
& item
) const;
257 // get the data associated with the item
258 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
264 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
265 // set the normal item image
266 void SetItemImage(const wxTreeItemId
& item
, int image
);
267 // set the selected item image
268 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
);
269 // associate some data with the item
270 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
272 // force appearance of [+] button near the item. This is useful to
273 // allow the user to expand the items which don't have any children now
274 // - but instead add them only when needed, thus minimizing memory
275 // usage and loading time.
276 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
278 // the item will be shown in bold
279 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
281 // item status inquiries
282 // ---------------------
284 // is the item visible (it might be outside the view or not expanded)?
285 bool IsVisible(const wxTreeItemId
& item
) const;
286 // does the item has any children?
287 bool HasChildren(const wxTreeItemId
& item
) const
288 { return ItemHasChildren(item
); }
289 bool ItemHasChildren(const wxTreeItemId
& item
) const;
290 // is the item expanded (only makes sense if HasChildren())?
291 bool IsExpanded(const wxTreeItemId
& item
) const;
292 // is this item currently selected (the same as has focus)?
293 bool IsSelected(const wxTreeItemId
& item
) const;
294 // is item text in bold font?
295 bool IsBold(const wxTreeItemId
& item
) const;
297 // number of children
298 // ------------------
300 // if 'recursively' is FALSE, only immediate children count, otherwise
301 // the returned number is the number of all items in this branch
302 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
307 // wxTreeItemId.IsOk() will return FALSE if there is no such item
309 // get the root tree item
310 wxTreeItemId
GetRootItem() const { return m_anchor
; }
312 // get the item currently selected (may return NULL if no selection)
313 wxTreeItemId
GetSelection() const { return m_current
; }
315 // get the parent of this item (may return NULL if root)
316 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
318 // for this enumeration function you must pass in a "cookie" parameter
319 // which is opaque for the application but is necessary for the library
320 // to make these functions reentrant (i.e. allow more than one
321 // enumeration on one and the same object simultaneously). Of course,
322 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
325 // get the first child of this item
326 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const;
327 // get the next child
328 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& cookie
) const;
330 // get the next sibling of this item
331 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
332 // get the previous sibling
333 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
335 // get first visible item
336 wxTreeItemId
GetFirstVisibleItem() const;
337 // get the next visible item: item must be visible itself!
338 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
339 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
340 // get the previous visible item: item must be visible itself!
341 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
346 // add the root node to the tree
347 wxTreeItemId
AddRoot(const wxString
& text
,
348 int image
= -1, int selectedImage
= -1,
349 wxTreeItemData
*data
= NULL
);
351 // insert a new item in as the first child of the parent
352 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
353 const wxString
& text
,
354 int image
= -1, int selectedImage
= -1,
355 wxTreeItemData
*data
= NULL
);
357 // insert a new item after a given one
358 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
359 const wxTreeItemId
& idPrevious
,
360 const wxString
& text
,
361 int image
= -1, int selectedImage
= -1,
362 wxTreeItemData
*data
= NULL
);
364 // insert a new item in as the last child of the parent
365 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
366 const wxString
& text
,
367 int image
= -1, int selectedImage
= -1,
368 wxTreeItemData
*data
= NULL
);
370 // delete this item and associated data if any
371 void Delete(const wxTreeItemId
& item
);
372 // delete all children (but don't delete the item itself)
373 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
374 void DeleteChildren(const wxTreeItemId
& item
);
375 // delete all items from the tree
376 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
377 void DeleteAllItems();
380 void Expand(const wxTreeItemId
& item
);
381 // collapse the item without removing its children
382 void Collapse(const wxTreeItemId
& item
);
383 // collapse the item and remove all children
384 void CollapseAndReset(const wxTreeItemId
& item
);
385 // toggles the current state
386 void Toggle(const wxTreeItemId
& item
);
388 // remove the selection from currently selected item (if any)
391 void SelectItem(const wxTreeItemId
& item
);
392 // make sure this item is visible (expanding the parent item and/or
393 // scrolling to this item if necessary)
394 void EnsureVisible(const wxTreeItemId
& item
);
395 // scroll to this item (but don't expand its parent)
396 void ScrollTo(const wxTreeItemId
& item
);
398 // The first function is more portable (because easier to implement
399 // on other platforms), but the second one returns some extra info.
400 wxTreeItemId
HitTest(const wxPoint
& point
)
401 { int dummy
; return HitTest(point
, dummy
); }
402 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
404 // start editing the item label: this (temporarily) replaces the item
405 // with a one line edit control. The item will be selected if it hadn't
406 // been before. textCtrlClass parameter allows you to create an edit
407 // control of arbitrary user-defined class deriving from wxTextCtrl.
408 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
409 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
410 // returns the same pointer as StartEdit() if the item is being edited,
411 // NULL otherwise (it's assumed that no more than one item may be
412 // edited simultaneously)
413 wxTextCtrl
* GetEditControl() const;
414 // end editing and accept or discard the changes to item label
415 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
418 // this function is called to compare 2 items and should return -1, 0
419 // or +1 if the first item is less than, equal to or greater than the
420 // second one. The base class version performs alphabetic comparaison
421 // of item labels (GetText)
422 virtual int OnCompareItems(const wxTreeItemId
& item1
,
423 const wxTreeItemId
& item2
);
424 // sort the children of this item using OnCompareItems
426 // NB: this function is not reentrant and not MT-safe (FIXME)!
427 void SortChildren(const wxTreeItemId
& item
);
430 void OnPaint( wxPaintEvent
&event
);
431 void OnSetFocus( wxFocusEvent
&event
);
432 void OnKillFocus( wxFocusEvent
&event
);
433 void OnChar( wxKeyEvent
&event
);
434 void OnMouse( wxMouseEvent
&event
);
435 void OnIdle( wxIdleEvent
&event
);
438 void SendDeleteEvent(wxGenericTreeItem
*itemBeingDeleted
);
441 wxGenericTreeItem
*m_anchor
;
442 wxGenericTreeItem
*m_current
;
445 int m_xScroll
,m_yScroll
;
446 unsigned int m_indent
;
449 wxBrush
*m_hilightBrush
;
450 wxImageList
*m_imageListNormal
,
453 // the common part of all ctors
457 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
459 const wxString
& text
,
460 int image
, int selectedImage
,
461 wxTreeItemData
*data
);
463 void AdjustMyScrollbars();
464 void PaintLevel( wxGenericTreeItem
*item
, wxDC
& dc
, int level
, int &y
);
465 void PaintItem( wxGenericTreeItem
*item
, wxDC
& dc
);
467 void CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
);
468 void CalculatePositions();
470 void RefreshSubtree( wxGenericTreeItem
*item
);
471 void RefreshLine( wxGenericTreeItem
*item
);
474 DECLARE_EVENT_TABLE()
475 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
478 #endif // _GENERIC_TREECTRL_H_