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 #include "wx/dynarray.h"
34 //those defines should only be done in generic/treectrl.h,
35 //because wxMSW doesn't allow mutiple selection
38 #define wxTR_SINGLE 0x0000
39 #define wxTR_MULTIPLE 0x0020
40 #define wxTR_EXTENDED 0x0040
41 #define wxTR_HAS_VARIABLE_ROW_HIGHT 0x0080
44 // -----------------------------------------------------------------------------
46 // -----------------------------------------------------------------------------
48 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
49 // where exactly the specified point is situated:
51 static const int wxTREE_HITTEST_ABOVE
= 0x0001;
52 static const int wxTREE_HITTEST_BELOW
= 0x0002;
53 static const int wxTREE_HITTEST_NOWHERE
= 0x0004;
54 // on the button associated with an item.
55 static const int wxTREE_HITTEST_ONITEMBUTTON
= 0x0008;
56 // on the bitmap associated with an item.
57 static const int wxTREE_HITTEST_ONITEMICON
= 0x0010;
58 // on the ident associated with an item.
59 static const int wxTREE_HITTEST_ONITEMIDENT
= 0x0020;
60 // on the label (string) associated with an item.
61 static const int wxTREE_HITTEST_ONITEMLABEL
= 0x0040;
62 // on the right of the label associated with an item.
63 static const int wxTREE_HITTEST_ONITEMRIGHT
= 0x0080;
64 // on the label (string) associated with an item.
65 //static const int wxTREE_HITTEST_ONITEMSTATEICON = 0x0100;
66 // on the left of the wxTreeCtrl.
67 static const int wxTREE_HITTEST_TOLEFT
= 0x0200;
68 // on the right of the wxTreeCtrl.
69 static const int wxTREE_HITTEST_TORIGHT
= 0x0400;
70 // on the upper part (first half) of the item.
71 static const int wxTREE_HITTEST_ONITEMUPPERPART
= 0x0800;
72 // on the lower part (second half) of the item.
73 static const int wxTREE_HITTEST_ONITEMLOWERPART
= 0x1000;
75 // anywhere on the item
76 static const int wxTREE_HITTEST_ONITEM
= wxTREE_HITTEST_ONITEMICON
|
77 wxTREE_HITTEST_ONITEMLABEL
;
79 // -----------------------------------------------------------------------------
80 // forward declaration
81 // -----------------------------------------------------------------------------
84 class wxGenericTreeItem
;
88 // -----------------------------------------------------------------------------
89 // wxTreeItemId - unique identifier of a tree element
90 // -----------------------------------------------------------------------------
92 class WXDLLEXPORT wxTreeItemId
94 friend class wxTreeCtrl
;
95 friend class wxTreeEvent
;
98 // 0 is invalid value for HTREEITEM
99 wxTreeItemId() { m_pItem
= 0; }
101 // default copy ctor/assignment operator are ok for us
104 // is this a valid tree item?
105 bool IsOk() const { return m_pItem
!= 0; }
107 // deprecated: only for compatibility
108 wxTreeItemId(long itemId
) { m_pItem
= (wxGenericTreeItem
*)itemId
; }
109 operator long() const { return (long)m_pItem
; }
111 //protected: // not for gcc
112 // for wxTreeCtrl usage only
113 wxTreeItemId(wxGenericTreeItem
*pItem
) { m_pItem
= pItem
; }
115 wxGenericTreeItem
*m_pItem
;
118 WX_DECLARE_OBJARRAY(wxTreeItemId
, wxArrayTreeItemIds
);
120 // ----------------------------------------------------------------------------
121 // wxTreeItemData is some (arbitrary) user class associated with some item.
123 // Because the objects of this class are deleted by the tree, they should
124 // always be allocated on the heap!
125 // ----------------------------------------------------------------------------
126 class WXDLLEXPORT wxTreeItemData
: public wxClientData
128 friend class wxTreeCtrl
;
130 // creation/destruction
131 // --------------------
135 // default copy ctor/assignment operator are ok
137 // accessor: get the item associated with us
138 const wxTreeItemId
& GetId() const { return m_pItem
; }
139 void SetId(const wxTreeItemId
& id
) { m_pItem
= id
; }
142 wxTreeItemId m_pItem
;
145 // -----------------------------------------------------------------------------
146 // wxTreeEvent - the event generated by the tree control
147 // -----------------------------------------------------------------------------
148 class WXDLLEXPORT wxTreeEvent
: public wxNotifyEvent
150 friend class wxTreeCtrl
;
152 wxTreeEvent(wxEventType commandType
= wxEVT_NULL
, int id
= 0);
155 // get the item on which the operation was performed or the newly
156 // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events
157 wxTreeItemId
GetItem() const { return m_item
; }
159 // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously
161 wxTreeItemId
GetOldItem() const { return m_itemOld
; }
163 // the point where the mouse was when the drag operation started (for
164 // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only)
165 wxPoint
GetPoint() const { return m_pointDrag
; }
167 // keyboard code (for wxEVT_COMMAND_TREE_KEY_DOWN only)
168 int GetCode() const { return m_code
; }
170 // set return code for wxEVT_COMMAND_TREE_ITEM_{EXPAND|COLLAPS}ING events
171 // call this to forbid the change in item status
172 void Veto() { m_code
= TRUE
; }
174 // for implementation usage only
175 bool WasVetoed() const { return m_code
; }
178 // @@ we could save some space by using union here
184 DECLARE_DYNAMIC_CLASS(wxTreeEvent
)
187 typedef void (wxEvtHandler::*wxTreeEventFunction
)(wxTreeEvent
&);
189 // ----------------------------------------------------------------------------
190 // macros for handling tree control events
191 // ----------------------------------------------------------------------------
193 // GetItem() returns the item being dragged, GetPoint() the mouse coords
194 #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
195 #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
197 // GetItem() returns the itme whose label is being edited
198 #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
199 #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
201 // provide/update information about GetItem() item
202 #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
203 #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
205 // GetItem() is the item being expanded/collapsed, the "ING" versions can use
206 #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
207 #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
208 #define EVT_TREE_ITEM_COLLAPSED(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
209 #define EVT_TREE_ITEM_COLLAPSING(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
211 // GetOldItem() is the item which had the selection previously, GetItem() is
212 // the item which acquires selection
213 #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
214 #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
216 // GetCode() returns the key code
217 // NB: this is the only message for which GetItem() is invalid (you may get the
218 // item from GetSelection())
219 #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
221 // GetItem() returns the item being deleted, the associated data (if any) will
222 // be deleted just after the return of this event handler (if any)
223 #define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
225 // GetItem() returns the item that was activated (double click, enter, space)
226 #define EVT_TREE_ITEM_ACTIVATED(id, fn) { wxEVT_COMMAND_TREE_ITEM_ACTIVATED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
228 // -----------------------------------------------------------------------------
229 // wxTreeCtrl - the tree control
230 // -----------------------------------------------------------------------------
232 class WXDLLEXPORT wxTreeCtrl
: public wxScrolledWindow
237 wxTreeCtrl() { Init(); }
239 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
240 const wxPoint
& pos
= wxDefaultPosition
,
241 const wxSize
& size
= wxDefaultSize
,
242 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
243 const wxValidator
&validator
= wxDefaultValidator
,
244 const wxString
& name
= wxTreeCtrlNameStr
)
246 Create(parent
, id
, pos
, size
, style
, validator
, name
);
249 virtual ~wxTreeCtrl();
251 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
252 const wxPoint
& pos
= wxDefaultPosition
,
253 const wxSize
& size
= wxDefaultSize
,
254 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
255 const wxValidator
&validator
= wxDefaultValidator
,
256 const wxString
& name
= wxTreeCtrlNameStr
);
261 // get the total number of items in the control
262 size_t GetCount() const;
264 // indent is the number of pixels the children are indented relative to
265 // the parents position. SetIndent() also redraws the control
267 unsigned int GetIndent() const { return m_indent
; }
268 void SetIndent(unsigned int indent
);
270 // spacing is the number of pixels between the start and the Text
271 unsigned int GetSpacing() const { return m_spacing
; }
272 void SetSpacing(unsigned int spacing
);
274 // image list: these functions allow to associate an image list with
275 // the control and retrieve it. Note that the control does _not_ delete
276 // the associated image list when it's deleted in order to allow image
277 // lists to be shared between different controls.
279 // The normal image list is for the icons which correspond to the
280 // normal tree item state (whether it is selected or not).
281 // Additionally, the application might choose to show a state icon
282 // which corresponds to an app-defined item state (for example,
283 // checked/unchecked) which are taken from the state image list.
284 wxImageList
*GetImageList() const;
285 wxImageList
*GetStateImageList() const;
287 void SetImageList(wxImageList
*imageList
);
288 void SetStateImageList(wxImageList
*imageList
);
290 // Functions to work with tree ctrl items.
295 // retrieve items label
296 wxString
GetItemText(const wxTreeItemId
& item
) const;
297 // get the normal item image
298 int GetItemImage(const wxTreeItemId
& item
) const;
299 // get the selected item image
300 int GetItemSelectedImage(const wxTreeItemId
& item
) const;
301 // get the data associated with the item
302 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
308 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
309 // set the normal item image
310 void SetItemImage(const wxTreeItemId
& item
, int image
);
311 // set the selected item image
312 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
);
313 // associate some data with the item
314 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
316 // force appearance of [+] button near the item. This is useful to
317 // allow the user to expand the items which don't have any children now
318 // - but instead add them only when needed, thus minimizing memory
319 // usage and loading time.
320 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
322 // the item will be shown in bold
323 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
325 // item status inquiries
326 // ---------------------
328 // is the item visible (it might be outside the view or not expanded)?
329 bool IsVisible(const wxTreeItemId
& item
) const;
330 // does the item has any children?
331 bool HasChildren(const wxTreeItemId
& item
) const
332 { return ItemHasChildren(item
); }
333 bool ItemHasChildren(const wxTreeItemId
& item
) const;
334 // is the item expanded (only makes sense if HasChildren())?
335 bool IsExpanded(const wxTreeItemId
& item
) const;
336 // is this item currently selected (the same as has focus)?
337 bool IsSelected(const wxTreeItemId
& item
) const;
338 // is item text in bold font?
339 bool IsBold(const wxTreeItemId
& item
) const;
341 // number of children
342 // ------------------
344 // if 'recursively' is FALSE, only immediate children count, otherwise
345 // the returned number is the number of all items in this branch
346 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
351 // wxTreeItemId.IsOk() will return FALSE if there is no such item
353 // get the root tree item
354 wxTreeItemId
GetRootItem() const { return m_anchor
; }
356 // get the item currently selected (may return NULL if no selection)
357 wxTreeItemId
GetSelection() const { return m_current
; }
359 // get the items currently selected, return the number of such item
360 size_t GetSelections(wxArrayTreeItemIds
&) const;
362 // get the parent of this item (may return NULL if root)
363 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
365 // for this enumeration function you must pass in a "cookie" parameter
366 // which is opaque for the application but is necessary for the library
367 // to make these functions reentrant (i.e. allow more than one
368 // enumeration on one and the same object simultaneously). Of course,
369 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
372 // get the first child of this item
373 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const;
374 // get the next child
375 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& cookie
) const;
376 // get the last child of this item - this method doesn't use cookies
377 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
379 // get the next sibling of this item
380 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
381 // get the previous sibling
382 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
384 // get first visible item
385 wxTreeItemId
GetFirstVisibleItem() const;
386 // get the next visible item: item must be visible itself!
387 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
388 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
389 // get the previous visible item: item must be visible itself!
390 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
395 // add the root node to the tree
396 wxTreeItemId
AddRoot(const wxString
& text
,
397 int image
= -1, int selectedImage
= -1,
398 wxTreeItemData
*data
= NULL
);
400 // insert a new item in as the first child of the parent
401 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
402 const wxString
& text
,
403 int image
= -1, int selectedImage
= -1,
404 wxTreeItemData
*data
= NULL
);
406 // insert a new item after a given one
407 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
408 const wxTreeItemId
& idPrevious
,
409 const wxString
& text
,
410 int image
= -1, int selectedImage
= -1,
411 wxTreeItemData
*data
= NULL
);
413 // insert a new item in as the last child of the parent
414 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
415 const wxString
& text
,
416 int image
= -1, int selectedImage
= -1,
417 wxTreeItemData
*data
= NULL
);
419 // delete this item and associated data if any
420 void Delete(const wxTreeItemId
& item
);
421 // delete all children (but don't delete the item itself)
422 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
423 void DeleteChildren(const wxTreeItemId
& item
);
424 // delete all items from the tree
425 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
426 void DeleteAllItems();
429 void Expand(const wxTreeItemId
& item
);
430 // collapse the item without removing its children
431 void Collapse(const wxTreeItemId
& item
);
432 // collapse the item and remove all children
433 void CollapseAndReset(const wxTreeItemId
& item
);
434 // toggles the current state
435 void Toggle(const wxTreeItemId
& item
);
437 // remove the selection from currently selected item (if any)
441 void SelectItem(const wxTreeItemId
& item
, bool unselect_others
=TRUE
, bool extended_select
=FALSE
);
442 // make sure this item is visible (expanding the parent item and/or
443 // scrolling to this item if necessary)
444 void EnsureVisible(const wxTreeItemId
& item
);
445 // scroll to this item (but don't expand its parent)
446 void ScrollTo(const wxTreeItemId
& item
);
448 // The first function is more portable (because easier to implement
449 // on other platforms), but the second one returns some extra info.
450 wxTreeItemId
HitTest(const wxPoint
& point
)
451 { int dummy
; return HitTest(point
, dummy
); }
452 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
454 // start editing the item label: this (temporarily) replaces the item
455 // with a one line edit control. The item will be selected if it hadn't
456 // been before. textCtrlClass parameter allows you to create an edit
457 // control of arbitrary user-defined class deriving from wxTextCtrl.
458 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
459 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
460 // returns the same pointer as StartEdit() if the item is being edited,
461 // NULL otherwise (it's assumed that no more than one item may be
462 // edited simultaneously)
463 wxTextCtrl
* GetEditControl() const;
464 // end editing and accept or discard the changes to item label
465 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
468 // this function is called to compare 2 items and should return -1, 0
469 // or +1 if the first item is less than, equal to or greater than the
470 // second one. The base class version performs alphabetic comparaison
471 // of item labels (GetText)
472 virtual int OnCompareItems(const wxTreeItemId
& item1
,
473 const wxTreeItemId
& item2
);
474 // sort the children of this item using OnCompareItems
476 // NB: this function is not reentrant and not MT-safe (FIXME)!
477 void SortChildren(const wxTreeItemId
& item
);
480 void OnPaint( wxPaintEvent
&event
);
481 void OnSetFocus( wxFocusEvent
&event
);
482 void OnKillFocus( wxFocusEvent
&event
);
483 void OnChar( wxKeyEvent
&event
);
484 void OnMouse( wxMouseEvent
&event
);
485 void OnIdle( wxIdleEvent
&event
);
488 void SendDeleteEvent(wxGenericTreeItem
*itemBeingDeleted
);
490 // Draw Special Information
491 void DrawBorder(wxTreeItemId
& item
);
492 void DrawLine(wxTreeItemId
& item
, bool below
);
495 friend class wxGenericTreeItem
;
497 wxGenericTreeItem
*m_anchor
;
498 wxGenericTreeItem
*m_current
, *m_key_current
;
501 int m_xScroll
,m_yScroll
;
502 unsigned int m_indent
;
503 unsigned int m_spacing
;
506 wxBrush
*m_hilightBrush
;
507 wxImageList
*m_imageListNormal
,
511 // the common part of all ctors
515 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
517 const wxString
& text
,
518 int image
, int selectedImage
,
519 wxTreeItemData
*data
);
521 void AdjustMyScrollbars();
522 int GetLineHeight(wxGenericTreeItem
*item
) const;
523 void PaintLevel( wxGenericTreeItem
*item
, wxDC
& dc
, int level
, int &y
);
524 void PaintItem( wxGenericTreeItem
*item
, wxDC
& dc
);
526 void CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
);
527 void CalculatePositions();
528 void CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
);
530 void RefreshSubtree( wxGenericTreeItem
*item
);
531 void RefreshLine( wxGenericTreeItem
*item
);
533 void FillArray(wxGenericTreeItem
*, wxArrayTreeItemIds
&) const;
534 void SelectItemRange( wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
);
535 bool TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
536 bool TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
537 void UnselectAllChildren( wxGenericTreeItem
*item
);
540 DECLARE_EVENT_TABLE()
541 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
544 #endif // _GENERIC_TREECTRL_H_