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