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 //those defines should only be done in generic/treectrl.h,
36 //because wxMSW doesn't allow mutiple selection
39 #define wxTR_SINGLE 0x0000
40 #define wxTR_MULTIPLE 0x0020
41 #define wxTR_EXTENDED 0x0040
42 #define wxTR_HAS_VARIABLE_ROW_HIGHT 0x0080
45 // -----------------------------------------------------------------------------
47 // -----------------------------------------------------------------------------
49 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
50 // where exactly the specified point is situated:
52 static const int wxTREE_HITTEST_ABOVE
= 0x0001;
53 static const int wxTREE_HITTEST_BELOW
= 0x0002;
54 static const int wxTREE_HITTEST_NOWHERE
= 0x0004;
55 // on the button associated with an item.
56 static const int wxTREE_HITTEST_ONITEMBUTTON
= 0x0008;
57 // on the bitmap associated with an item.
58 static const int wxTREE_HITTEST_ONITEMICON
= 0x0010;
59 // on the ident associated with an item.
60 static const int wxTREE_HITTEST_ONITEMIDENT
= 0x0020;
61 // on the label (string) associated with an item.
62 static const int wxTREE_HITTEST_ONITEMLABEL
= 0x0040;
63 // on the right of the label associated with an item.
64 static const int wxTREE_HITTEST_ONITEMRIGHT
= 0x0080;
65 // on the label (string) associated with an item.
66 //static const int wxTREE_HITTEST_ONITEMSTATEICON = 0x0100;
67 // on the left of the wxTreeCtrl.
68 static const int wxTREE_HITTEST_TOLEFT
= 0x0200;
69 // on the right of the wxTreeCtrl.
70 static const int wxTREE_HITTEST_TORIGHT
= 0x0400;
71 // on the upper part (first half) of the item.
72 static const int wxTREE_HITTEST_ONITEMUPPERPART
= 0x0800;
73 // on the lower part (second half) of the item.
74 static const int wxTREE_HITTEST_ONITEMLOWERPART
= 0x1000;
76 // anywhere on the item
77 static const int wxTREE_HITTEST_ONITEM
= wxTREE_HITTEST_ONITEMICON
|
78 wxTREE_HITTEST_ONITEMLABEL
;
80 // -----------------------------------------------------------------------------
81 // forward declaration
82 // -----------------------------------------------------------------------------
84 class WXDLLEXPORT wxImageList
;
85 class WXDLLEXPORT wxGenericTreeItem
;
87 class WXDLLEXPORT wxTreeItemData
;
89 class WXDLLEXPORT wxTreeRenameTimer
;
90 class WXDLLEXPORT wxTreeTextCtrl
;
92 // -----------------------------------------------------------------------------
93 // wxTreeItemId - unique identifier of a tree element
94 // -----------------------------------------------------------------------------
96 class WXDLLEXPORT wxTreeItemId
98 friend class wxTreeCtrl
;
99 friend class wxTreeEvent
;
102 // 0 is invalid value for HTREEITEM
103 wxTreeItemId() { m_pItem
= 0; }
105 // default copy ctor/assignment operator are ok for us
108 // is this a valid tree item?
109 bool IsOk() const { return m_pItem
!= 0; }
111 // deprecated: only for compatibility
112 wxTreeItemId(long itemId
) { m_pItem
= (wxGenericTreeItem
*)itemId
; }
113 operator long() const { return (long)m_pItem
; }
115 //protected: // not for gcc
116 // for wxTreeCtrl usage only
117 wxTreeItemId(wxGenericTreeItem
*pItem
) { m_pItem
= pItem
; }
119 wxGenericTreeItem
*m_pItem
;
122 WX_DECLARE_OBJARRAY(wxTreeItemId
, wxArrayTreeItemIds
);
124 // ----------------------------------------------------------------------------
125 // wxTreeItemData is some (arbitrary) user class associated with some item.
127 // Because the objects of this class are deleted by the tree, they should
128 // always be allocated on the heap!
129 // ----------------------------------------------------------------------------
131 class WXDLLEXPORT wxTreeItemData
: public wxClientData
133 friend class wxTreeCtrl
;
135 // creation/destruction
136 // --------------------
140 // default copy ctor/assignment operator are ok
142 // accessor: get the item associated with us
143 const wxTreeItemId
& GetId() const { return m_pItem
; }
144 void SetId(const wxTreeItemId
& id
) { m_pItem
= id
; }
147 wxTreeItemId m_pItem
;
150 //-----------------------------------------------------------------------------
151 // wxTreeRenameTimer (internal)
152 //-----------------------------------------------------------------------------
154 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
160 wxTreeRenameTimer( wxTreeCtrl
*owner
);
164 //-----------------------------------------------------------------------------
165 // wxTreeTextCtrl (internal)
166 //-----------------------------------------------------------------------------
168 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
170 DECLARE_DYNAMIC_CLASS(wxTreeTextCtrl
);
176 wxString m_startValue
;
179 wxTreeTextCtrl(void) {};
180 wxTreeTextCtrl( wxWindow
*parent
, const wxWindowID id
,
181 bool *accept
, wxString
*res
, wxTreeCtrl
*owner
,
182 const wxString
&value
= wxEmptyString
,
183 const wxPoint
&pos
= wxDefaultPosition
, const wxSize
&size
= wxDefaultSize
,
184 int style
= 0, const wxValidator
& validator
= wxDefaultValidator
,
185 const wxString
&name
= wxTextCtrlNameStr
);
186 void OnChar( wxKeyEvent
&event
);
187 void OnKillFocus( wxFocusEvent
&event
);
189 DECLARE_EVENT_TABLE()
192 // -----------------------------------------------------------------------------
193 // wxTreeCtrl - the tree control
194 // -----------------------------------------------------------------------------
196 class WXDLLEXPORT wxTreeCtrl
: public wxScrolledWindow
201 wxTreeCtrl() { Init(); }
203 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
204 const wxPoint
& pos
= wxDefaultPosition
,
205 const wxSize
& size
= wxDefaultSize
,
206 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
207 const wxValidator
&validator
= wxDefaultValidator
,
208 const wxString
& name
= wxTreeCtrlNameStr
)
210 Create(parent
, id
, pos
, size
, style
, validator
, name
);
213 virtual ~wxTreeCtrl();
215 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
216 const wxPoint
& pos
= wxDefaultPosition
,
217 const wxSize
& size
= wxDefaultSize
,
218 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
219 const wxValidator
&validator
= wxDefaultValidator
,
220 const wxString
& name
= wxTreeCtrlNameStr
);
225 // get the total number of items in the control
226 size_t GetCount() const;
228 // indent is the number of pixels the children are indented relative to
229 // the parents position. SetIndent() also redraws the control
231 unsigned int GetIndent() const { return m_indent
; }
232 void SetIndent(unsigned int indent
);
234 // spacing is the number of pixels between the start and the Text
235 unsigned int GetSpacing() const { return m_spacing
; }
236 void SetSpacing(unsigned int spacing
);
238 // image list: these functions allow to associate an image list with
239 // the control and retrieve it. Note that the control does _not_ delete
240 // the associated image list when it's deleted in order to allow image
241 // lists to be shared between different controls.
243 // The normal image list is for the icons which correspond to the
244 // normal tree item state (whether it is selected or not).
245 // Additionally, the application might choose to show a state icon
246 // which corresponds to an app-defined item state (for example,
247 // checked/unchecked) which are taken from the state image list.
248 wxImageList
*GetImageList() const;
249 wxImageList
*GetStateImageList() const;
251 void SetImageList(wxImageList
*imageList
);
252 void SetStateImageList(wxImageList
*imageList
);
254 // Functions to work with tree ctrl items.
259 // retrieve items label
260 wxString
GetItemText(const wxTreeItemId
& item
) const;
261 // get one of the images associated with the item (normal by default)
262 int GetItemImage(const wxTreeItemId
& item
,
263 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) 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 // get one of the images associated with the item (normal by default)
273 void SetItemImage(const wxTreeItemId
& item
, int image
,
274 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
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 items currently selected, return the number of such item
322 size_t GetSelections(wxArrayTreeItemIds
&) const;
324 // get the parent of this item (may return NULL if root)
325 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
327 // for this enumeration function you must pass in a "cookie" parameter
328 // which is opaque for the application but is necessary for the library
329 // to make these functions reentrant (i.e. allow more than one
330 // enumeration on one and the same object simultaneously). Of course,
331 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
334 // get the first child of this item
335 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const;
336 // get the next child
337 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& cookie
) const;
338 // get the last child of this item - this method doesn't use cookies
339 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
341 // get the next sibling of this item
342 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
343 // get the previous sibling
344 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
346 // get first visible item
347 wxTreeItemId
GetFirstVisibleItem() const;
348 // get the next visible item: item must be visible itself!
349 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
350 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
351 // get the previous visible item: item must be visible itself!
352 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
357 // add the root node to the tree
358 wxTreeItemId
AddRoot(const wxString
& text
,
359 int image
= -1, int selectedImage
= -1,
360 wxTreeItemData
*data
= NULL
);
362 // insert a new item in as the first child of the parent
363 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
364 const wxString
& text
,
365 int image
= -1, int selectedImage
= -1,
366 wxTreeItemData
*data
= NULL
);
368 // insert a new item after a given one
369 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
370 const wxTreeItemId
& idPrevious
,
371 const wxString
& text
,
372 int image
= -1, int selectedImage
= -1,
373 wxTreeItemData
*data
= NULL
);
375 // insert a new item in as the last child of the parent
376 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
377 const wxString
& text
,
378 int image
= -1, int selectedImage
= -1,
379 wxTreeItemData
*data
= NULL
);
381 // delete this item and associated data if any
382 void Delete(const wxTreeItemId
& item
);
383 // delete all children (but don't delete the item itself)
384 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
385 void DeleteChildren(const wxTreeItemId
& item
);
386 // delete all items from the tree
387 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
388 void DeleteAllItems();
391 void Expand(const wxTreeItemId
& item
);
392 // collapse the item without removing its children
393 void Collapse(const wxTreeItemId
& item
);
394 // collapse the item and remove all children
395 void CollapseAndReset(const wxTreeItemId
& item
);
396 // toggles the current state
397 void Toggle(const wxTreeItemId
& item
);
399 // remove the selection from currently selected item (if any)
403 void SelectItem(const wxTreeItemId
& item
, bool unselect_others
=TRUE
, bool extended_select
=FALSE
);
404 // make sure this item is visible (expanding the parent item and/or
405 // scrolling to this item if necessary)
406 void EnsureVisible(const wxTreeItemId
& item
);
407 // scroll to this item (but don't expand its parent)
408 void ScrollTo(const wxTreeItemId
& item
);
410 // The first function is more portable (because easier to implement
411 // on other platforms), but the second one returns some extra info.
412 wxTreeItemId
HitTest(const wxPoint
& point
)
413 { int dummy
; return HitTest(point
, dummy
); }
414 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
416 // Start editing the item label: this (temporarily) replaces the item
417 // with a one line edit control. The item will be selected if it hadn't
419 void EditLabel( const wxTreeItemId
& item
) { Edit( item
); }
420 void Edit( const wxTreeItemId
& item
);
423 // this function is called to compare 2 items and should return -1, 0
424 // or +1 if the first item is less than, equal to or greater than the
425 // second one. The base class version performs alphabetic comparaison
426 // of item labels (GetText)
427 virtual int OnCompareItems(const wxTreeItemId
& item1
,
428 const wxTreeItemId
& item2
);
429 // sort the children of this item using OnCompareItems
431 // NB: this function is not reentrant and not MT-safe (FIXME)!
432 void SortChildren(const wxTreeItemId
& item
);
434 // deprecated functions: use Set/GetItemImage directly
435 // get the selected item image
436 int GetItemSelectedImage(const wxTreeItemId
& item
) const
437 { return GetItemImage(item
, wxTreeItemIcon_Selected
); }
438 // set the selected item image
439 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
440 { SetItemImage(item
, image
, wxTreeItemIcon_Selected
); }
445 void OnPaint( wxPaintEvent
&event
);
446 void OnSetFocus( wxFocusEvent
&event
);
447 void OnKillFocus( wxFocusEvent
&event
);
448 void OnChar( wxKeyEvent
&event
);
449 void OnMouse( wxMouseEvent
&event
);
450 void OnIdle( wxIdleEvent
&event
);
453 void SendDeleteEvent(wxGenericTreeItem
*itemBeingDeleted
);
455 // Draw Special Information
456 void DrawBorder(wxTreeItemId
& item
);
457 void DrawLine(wxTreeItemId
& item
, bool below
);
460 friend class wxGenericTreeItem
;
461 friend class wxTreeRenameTimer
;
462 friend class wxTreeTextCtrl
;
464 wxGenericTreeItem
*m_anchor
;
465 wxGenericTreeItem
*m_current
, *m_key_current
, *m_currentEdit
;
468 int m_xScroll
,m_yScroll
;
469 unsigned int m_indent
;
470 unsigned int m_spacing
;
473 wxBrush
*m_hilightBrush
;
474 wxImageList
*m_imageListNormal
,
478 wxTimer
*m_renameTimer
;
480 wxString m_renameRes
;
482 // the common part of all ctors
486 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
488 const wxString
& text
,
489 int image
, int selectedImage
,
490 wxTreeItemData
*data
);
492 void AdjustMyScrollbars();
493 int GetLineHeight(wxGenericTreeItem
*item
) const;
494 void PaintLevel( wxGenericTreeItem
*item
, wxDC
& dc
, int level
, int &y
);
495 void PaintItem( wxGenericTreeItem
*item
, wxDC
& dc
);
497 void CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
);
498 void CalculatePositions();
499 void CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
);
501 void RefreshSubtree( wxGenericTreeItem
*item
);
502 void RefreshLine( wxGenericTreeItem
*item
);
504 void OnRenameTimer();
505 void OnRenameAccept();
507 void FillArray(wxGenericTreeItem
*, wxArrayTreeItemIds
&) const;
508 void SelectItemRange( wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
);
509 bool TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
510 bool TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
511 void UnselectAllChildren( wxGenericTreeItem
*item
);
514 DECLARE_EVENT_TABLE()
515 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
518 #endif // _GENERIC_TREECTRL_H_