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"
32 // -----------------------------------------------------------------------------
34 // -----------------------------------------------------------------------------
36 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
37 // where exactly the specified point is situated:
38 static const int wxTREE_HITTEST_NOWHERE
= 0x0004;
39 // on the bitmap associated with an item.
40 static const int wxTREE_HITTEST_ONITEMICON
= 0x0020;
41 // on the label (string) associated with an item.
42 static const int wxTREE_HITTEST_ONITEMLABEL
= 0x0080;
43 // anywhere on the item
44 static const int wxTREE_HITTEST_ONITEM
= wxTREE_HITTEST_ONITEMICON
|
45 wxTREE_HITTEST_ONITEMLABEL
;
47 // -----------------------------------------------------------------------------
48 // forward declaration
49 // -----------------------------------------------------------------------------
52 class wxGenericTreeItem
;
56 // -----------------------------------------------------------------------------
57 // wxTreeItemId - unique identifier of a tree element
58 // -----------------------------------------------------------------------------
60 class WXDLLEXPORT wxTreeItemId
62 friend class wxTreeCtrl
;
63 friend class wxTreeEvent
;
66 // 0 is invalid value for HTREEITEM
67 wxTreeItemId() { m_pItem
= 0; }
69 // default copy ctor/assignment operator are ok for us
72 // is this a valid tree item?
73 bool IsOk() const { return m_pItem
!= 0; }
75 // deprecated: only for compatibility
76 wxTreeItemId(long itemId
) { m_pItem
= (wxGenericTreeItem
*)itemId
; }
77 operator long() const { return (long)m_pItem
; }
79 //protected: // not for gcc
80 // for wxTreeCtrl usage only
81 wxTreeItemId(wxGenericTreeItem
*pItem
) { m_pItem
= pItem
; }
83 wxGenericTreeItem
*m_pItem
;
86 // ----------------------------------------------------------------------------
87 // wxTreeItemData is some (arbitrary) user class associated with some item.
89 // Because the objects of this class are deleted by the tree, they should
90 // always be allocated on the heap!
91 // ----------------------------------------------------------------------------
92 class WXDLLEXPORT wxTreeItemData
: public wxClientData
94 friend class wxTreeCtrl
;
96 // creation/destruction
97 // --------------------
101 // default copy ctor/assignment operator are ok
103 // accessor: get the item associated with us
104 const wxTreeItemId
& GetId() const { return m_pItem
; }
105 void SetId(const wxTreeItemId
& id
) { m_pItem
= id
; }
108 wxTreeItemId m_pItem
;
111 // -----------------------------------------------------------------------------
112 // wxTreeEvent - the event generated by the tree control
113 // -----------------------------------------------------------------------------
114 class WXDLLEXPORT wxTreeEvent
: public wxNotifyEvent
116 friend class wxTreeCtrl
;
118 wxTreeEvent(wxEventType commandType
= wxEVT_NULL
, int id
= 0);
121 // get the item on which the operation was performed or the newly
122 // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events
123 wxTreeItemId
GetItem() const { return m_item
; }
125 // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously
127 wxTreeItemId
GetOldItem() const { return m_itemOld
; }
129 // the point where the mouse was when the drag operation started (for
130 // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only)
131 wxPoint
GetPoint() const { return m_pointDrag
; }
133 // keyboard code (for wxEVT_COMMAND_TREE_KEY_DOWN only)
134 int GetCode() const { return m_code
; }
136 // set return code for wxEVT_COMMAND_TREE_ITEM_{EXPAND|COLLAPS}ING events
137 // call this to forbid the change in item status
138 void Veto() { m_code
= TRUE
; }
140 // for implementation usage only
141 bool WasVetoed() const { return m_code
; }
144 // @@ we could save some space by using union here
150 DECLARE_DYNAMIC_CLASS(wxTreeEvent
)
153 typedef void (wxEvtHandler::*wxTreeEventFunction
)(wxTreeEvent
&);
155 // ----------------------------------------------------------------------------
156 // macros for handling tree control events
157 // ----------------------------------------------------------------------------
159 // GetItem() returns the item being dragged, GetPoint() the mouse coords
160 #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
161 #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
163 // GetItem() returns the itme whose label is being edited
164 #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
165 #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
167 // provide/update information about GetItem() item
168 #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
169 #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
171 // GetItem() is the item being expanded/collapsed, the "ING" versions can use
172 #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
173 #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
174 #define EVT_TREE_ITEM_COLLAPSED(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
175 #define EVT_TREE_ITEM_COLLAPSING(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
177 // GetOldItem() is the item which had the selection previously, GetItem() is
178 // the item which acquires selection
179 #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
180 #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
182 // GetCode() returns the key code
183 // NB: this is the only message for which GetItem() is invalid (you may get the
184 // item from GetSelection())
185 #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
187 // GetItem() returns the item being deleted, the associated data (if any) will
188 // be deleted just after the return of this event handler (if any)
189 #define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
191 // GetItem() returns the item that was activated (double click, enter, space)
192 #define EVT_TREE_ITEM_ACTIVATED(id, fn) { wxEVT_COMMAND_TREE_ITEM_ACTIVATED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
194 // -----------------------------------------------------------------------------
195 // wxTreeCtrl - the tree control
196 // -----------------------------------------------------------------------------
198 class WXDLLEXPORT wxTreeCtrl
: public wxScrolledWindow
203 wxTreeCtrl() { Init(); }
205 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
206 const wxPoint
& pos
= wxDefaultPosition
,
207 const wxSize
& size
= wxDefaultSize
,
208 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
209 const wxValidator
&validator
= wxDefaultValidator
,
210 const wxString
& name
= wxTreeCtrlNameStr
)
212 Create(parent
, id
, pos
, size
, style
, validator
, name
);
215 virtual ~wxTreeCtrl();
217 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
218 const wxPoint
& pos
= wxDefaultPosition
,
219 const wxSize
& size
= wxDefaultSize
,
220 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
221 const wxValidator
&validator
= wxDefaultValidator
,
222 const wxString
& name
= wxTreeCtrlNameStr
);
227 // get the total number of items in the control
228 size_t GetCount() const;
230 // indent is the number of pixels the children are indented relative to
231 // the parents position. SetIndent() also redraws the control
233 unsigned int GetIndent() const { return m_indent
; }
234 void SetIndent(unsigned int indent
);
236 // image list: these functions allow to associate an image list with
237 // the control and retrieve it. Note that the control does _not_ delete
238 // the associated image list when it's deleted in order to allow image
239 // lists to be shared between different controls.
241 // The normal image list is for the icons which correspond to the
242 // normal tree item state (whether it is selected or not).
243 // Additionally, the application might choose to show a state icon
244 // which corresponds to an app-defined item state (for example,
245 // checked/unchecked) which are taken from the state image list.
246 wxImageList
*GetImageList() const;
247 wxImageList
*GetStateImageList() const;
249 void SetImageList(wxImageList
*imageList
);
250 void SetStateImageList(wxImageList
*imageList
);
252 // Functions to work with tree ctrl items.
257 // retrieve items label
258 wxString
GetItemText(const wxTreeItemId
& item
) const;
259 // get the normal item image
260 int GetItemImage(const wxTreeItemId
& item
) const;
261 // get the selected item image
262 int GetItemSelectedImage(const wxTreeItemId
& item
) const;
263 // get the data associated with the item
264 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
270 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
271 // set the normal item image
272 void SetItemImage(const wxTreeItemId
& item
, int image
);
273 // set the selected item image
274 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
);
275 // associate some data with the item
276 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
278 // force appearance of [+] button near the item. This is useful to
279 // allow the user to expand the items which don't have any children now
280 // - but instead add them only when needed, thus minimizing memory
281 // usage and loading time.
282 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
284 // the item will be shown in bold
285 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
287 // item status inquiries
288 // ---------------------
290 // is the item visible (it might be outside the view or not expanded)?
291 bool IsVisible(const wxTreeItemId
& item
) const;
292 // does the item has any children?
293 bool HasChildren(const wxTreeItemId
& item
) const
294 { return ItemHasChildren(item
); }
295 bool ItemHasChildren(const wxTreeItemId
& item
) const;
296 // is the item expanded (only makes sense if HasChildren())?
297 bool IsExpanded(const wxTreeItemId
& item
) const;
298 // is this item currently selected (the same as has focus)?
299 bool IsSelected(const wxTreeItemId
& item
) const;
300 // is item text in bold font?
301 bool IsBold(const wxTreeItemId
& item
) const;
303 // number of children
304 // ------------------
306 // if 'recursively' is FALSE, only immediate children count, otherwise
307 // the returned number is the number of all items in this branch
308 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
313 // wxTreeItemId.IsOk() will return FALSE if there is no such item
315 // get the root tree item
316 wxTreeItemId
GetRootItem() const { return m_anchor
; }
318 // get the item currently selected (may return NULL if no selection)
319 wxTreeItemId
GetSelection() const { return m_current
; }
321 // get the parent of this item (may return NULL if root)
322 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
324 // for this enumeration function you must pass in a "cookie" parameter
325 // which is opaque for the application but is necessary for the library
326 // to make these functions reentrant (i.e. allow more than one
327 // enumeration on one and the same object simultaneously). Of course,
328 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
331 // get the first child of this item
332 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const;
333 // get the next child
334 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& cookie
) const;
336 // get the next sibling of this item
337 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
338 // get the previous sibling
339 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
341 // get first visible item
342 wxTreeItemId
GetFirstVisibleItem() const;
343 // get the next visible item: item must be visible itself!
344 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
345 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
346 // get the previous visible item: item must be visible itself!
347 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
352 // add the root node to the tree
353 wxTreeItemId
AddRoot(const wxString
& text
,
354 int image
= -1, int selectedImage
= -1,
355 wxTreeItemData
*data
= NULL
);
357 // insert a new item in as the first child of the parent
358 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
359 const wxString
& text
,
360 int image
= -1, int selectedImage
= -1,
361 wxTreeItemData
*data
= NULL
);
363 // insert a new item after a given one
364 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
365 const wxTreeItemId
& idPrevious
,
366 const wxString
& text
,
367 int image
= -1, int selectedImage
= -1,
368 wxTreeItemData
*data
= NULL
);
370 // insert a new item in as the last child of the parent
371 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
372 const wxString
& text
,
373 int image
= -1, int selectedImage
= -1,
374 wxTreeItemData
*data
= NULL
);
376 // delete this item and associated data if any
377 void Delete(const wxTreeItemId
& item
);
378 // delete all children (but don't delete the item itself)
379 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
380 void DeleteChildren(const wxTreeItemId
& item
);
381 // delete all items from the tree
382 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
383 void DeleteAllItems();
386 void Expand(const wxTreeItemId
& item
);
387 // collapse the item without removing its children
388 void Collapse(const wxTreeItemId
& item
);
389 // collapse the item and remove all children
390 void CollapseAndReset(const wxTreeItemId
& item
);
391 // toggles the current state
392 void Toggle(const wxTreeItemId
& item
);
394 // remove the selection from currently selected item (if any)
397 void SelectItem(const wxTreeItemId
& item
);
398 // make sure this item is visible (expanding the parent item and/or
399 // scrolling to this item if necessary)
400 void EnsureVisible(const wxTreeItemId
& item
);
401 // scroll to this item (but don't expand its parent)
402 void ScrollTo(const wxTreeItemId
& item
);
404 // The first function is more portable (because easier to implement
405 // on other platforms), but the second one returns some extra info.
406 wxTreeItemId
HitTest(const wxPoint
& point
)
407 { int dummy
; return HitTest(point
, dummy
); }
408 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
410 // start editing the item label: this (temporarily) replaces the item
411 // with a one line edit control. The item will be selected if it hadn't
412 // been before. textCtrlClass parameter allows you to create an edit
413 // control of arbitrary user-defined class deriving from wxTextCtrl.
414 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
415 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
416 // returns the same pointer as StartEdit() if the item is being edited,
417 // NULL otherwise (it's assumed that no more than one item may be
418 // edited simultaneously)
419 wxTextCtrl
* GetEditControl() const;
420 // end editing and accept or discard the changes to item label
421 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
424 // this function is called to compare 2 items and should return -1, 0
425 // or +1 if the first item is less than, equal to or greater than the
426 // second one. The base class version performs alphabetic comparaison
427 // of item labels (GetText)
428 virtual int OnCompareItems(const wxTreeItemId
& item1
,
429 const wxTreeItemId
& item2
);
430 // sort the children of this item using OnCompareItems
432 // NB: this function is not reentrant and not MT-safe (FIXME)!
433 void SortChildren(const wxTreeItemId
& item
);
436 void OnPaint( wxPaintEvent
&event
);
437 void OnSetFocus( wxFocusEvent
&event
);
438 void OnKillFocus( wxFocusEvent
&event
);
439 void OnChar( wxKeyEvent
&event
);
440 void OnMouse( wxMouseEvent
&event
);
441 void OnIdle( wxIdleEvent
&event
);
444 void SendDeleteEvent(wxGenericTreeItem
*itemBeingDeleted
);
447 wxGenericTreeItem
*m_anchor
;
448 wxGenericTreeItem
*m_current
;
451 int m_xScroll
,m_yScroll
;
452 unsigned int m_indent
;
455 wxBrush
*m_hilightBrush
;
456 wxImageList
*m_imageListNormal
,
459 // the common part of all ctors
463 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
465 const wxString
& text
,
466 int image
, int selectedImage
,
467 wxTreeItemData
*data
);
469 void AdjustMyScrollbars();
470 void PaintLevel( wxGenericTreeItem
*item
, wxDC
& dc
, int level
, int &y
);
471 void PaintItem( wxGenericTreeItem
*item
, wxDC
& dc
);
473 void CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
);
474 void CalculatePositions();
476 void RefreshSubtree( wxGenericTreeItem
*item
);
477 void RefreshLine( wxGenericTreeItem
*item
);
480 DECLARE_EVENT_TABLE()
481 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
484 #endif // _GENERIC_TREECTRL_H_