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 #include "wx/dynarray.h"
29 // -----------------------------------------------------------------------------
30 // forward declaration
31 // -----------------------------------------------------------------------------
33 class WXDLLEXPORT wxImageList
;
34 class WXDLLEXPORT wxGenericTreeItem
;
36 class WXDLLEXPORT wxTreeItemData
;
38 class WXDLLEXPORT wxTreeRenameTimer
;
39 class WXDLLEXPORT wxTreeTextCtrl
;
41 // -----------------------------------------------------------------------------
42 // wxTreeItemId - unique identifier of a tree element
43 // -----------------------------------------------------------------------------
45 class WXDLLEXPORT wxTreeItemId
47 friend class wxTreeCtrl
;
48 friend class wxTreeEvent
;
51 // 0 is invalid value for HTREEITEM
52 wxTreeItemId() { m_pItem
= 0; }
54 // default copy ctor/assignment operator are ok for us
57 // is this a valid tree item?
58 bool IsOk() const { return m_pItem
!= 0; }
60 // deprecated: only for compatibility
61 wxTreeItemId(long itemId
) { m_pItem
= (wxGenericTreeItem
*)itemId
; }
62 operator long() const { return (long)m_pItem
; }
64 //protected: // not for gcc
65 // for wxTreeCtrl usage only
66 wxTreeItemId(wxGenericTreeItem
*pItem
) { m_pItem
= pItem
; }
68 wxGenericTreeItem
*m_pItem
;
71 WX_DECLARE_OBJARRAY(wxTreeItemId
, wxArrayTreeItemIds
);
73 // ----------------------------------------------------------------------------
74 // wxTreeItemData is some (arbitrary) user class associated with some item.
76 // Because the objects of this class are deleted by the tree, they should
77 // always be allocated on the heap!
78 // ----------------------------------------------------------------------------
80 class WXDLLEXPORT wxTreeItemData
: public wxClientData
82 friend class wxTreeCtrl
;
84 // creation/destruction
85 // --------------------
89 // default copy ctor/assignment operator are ok
91 // accessor: get the item associated with us
92 const wxTreeItemId
& GetId() const { return m_pItem
; }
93 void SetId(const wxTreeItemId
& id
) { m_pItem
= id
; }
99 //-----------------------------------------------------------------------------
100 // wxTreeRenameTimer (internal)
101 //-----------------------------------------------------------------------------
103 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
109 wxTreeRenameTimer( wxTreeCtrl
*owner
);
113 //-----------------------------------------------------------------------------
114 // wxTreeTextCtrl (internal)
115 //-----------------------------------------------------------------------------
117 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
119 DECLARE_DYNAMIC_CLASS(wxTreeTextCtrl
);
125 wxString m_startValue
;
128 wxTreeTextCtrl(void) {};
129 wxTreeTextCtrl( wxWindow
*parent
, const wxWindowID id
,
130 bool *accept
, wxString
*res
, wxTreeCtrl
*owner
,
131 const wxString
&value
= wxEmptyString
,
132 const wxPoint
&pos
= wxDefaultPosition
, const wxSize
&size
= wxDefaultSize
,
135 const wxValidator
& validator
= wxDefaultValidator
,
137 const wxString
&name
= wxTextCtrlNameStr
);
138 void OnChar( wxKeyEvent
&event
);
139 void OnKillFocus( wxFocusEvent
&event
);
141 DECLARE_EVENT_TABLE()
144 // -----------------------------------------------------------------------------
145 // wxTreeCtrl - the tree control
146 // -----------------------------------------------------------------------------
148 class WXDLLEXPORT wxTreeCtrl
: public wxScrolledWindow
153 wxTreeCtrl() { Init(); }
155 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
156 const wxPoint
& pos
= wxDefaultPosition
,
157 const wxSize
& size
= wxDefaultSize
,
158 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
160 const wxValidator
&validator
= wxDefaultValidator
,
162 const wxString
& name
= wxTreeCtrlNameStr
)
164 Create(parent
, id
, pos
, size
, style
, validator
, name
);
167 virtual ~wxTreeCtrl();
169 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
170 const wxPoint
& pos
= wxDefaultPosition
,
171 const wxSize
& size
= wxDefaultSize
,
172 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
174 const wxValidator
&validator
= wxDefaultValidator
,
176 const wxString
& name
= wxTreeCtrlNameStr
);
181 // get the total number of items in the control
182 size_t GetCount() const;
184 // indent is the number of pixels the children are indented relative to
185 // the parents position. SetIndent() also redraws the control
187 unsigned int GetIndent() const { return m_indent
; }
188 void SetIndent(unsigned int indent
);
190 // spacing is the number of pixels between the start and the Text
191 unsigned int GetSpacing() const { return m_spacing
; }
192 void SetSpacing(unsigned int spacing
);
194 // image list: these functions allow to associate an image list with
195 // the control and retrieve it. Note that the control does _not_ delete
196 // the associated image list when it's deleted in order to allow image
197 // lists to be shared between different controls.
199 // The normal image list is for the icons which correspond to the
200 // normal tree item state (whether it is selected or not).
201 // Additionally, the application might choose to show a state icon
202 // which corresponds to an app-defined item state (for example,
203 // checked/unchecked) which are taken from the state image list.
204 wxImageList
*GetImageList() const;
205 wxImageList
*GetStateImageList() const;
207 void SetImageList(wxImageList
*imageList
);
208 void SetStateImageList(wxImageList
*imageList
);
210 // Functions to work with tree ctrl items.
215 // retrieve items label
216 wxString
GetItemText(const wxTreeItemId
& item
) const;
217 // get one of the images associated with the item (normal by default)
218 int GetItemImage(const wxTreeItemId
& item
,
219 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
220 // get the data associated with the item
221 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
227 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
228 // get one of the images associated with the item (normal by default)
229 void SetItemImage(const wxTreeItemId
& item
, int image
,
230 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
231 // associate some data with the item
232 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
234 // force appearance of [+] button near the item. This is useful to
235 // allow the user to expand the items which don't have any children now
236 // - but instead add them only when needed, thus minimizing memory
237 // usage and loading time.
238 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
240 // the item will be shown in bold
241 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
243 // set the items text colour
244 void SetItemTextColour(const wxTreeItemId
& item
, const wxColour
& col
);
246 // set the items background colour
247 void SetItemBackgroundColour(const wxTreeItemId
& item
, const wxColour
& col
);
249 // set the items font (should be of the same height for all items)
250 void SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
);
252 // item status inquiries
253 // ---------------------
255 // is the item visible (it might be outside the view or not expanded)?
256 bool IsVisible(const wxTreeItemId
& item
) const;
257 // does the item has any children?
258 bool HasChildren(const wxTreeItemId
& item
) const
259 { return ItemHasChildren(item
); }
260 bool ItemHasChildren(const wxTreeItemId
& item
) const;
261 // is the item expanded (only makes sense if HasChildren())?
262 bool IsExpanded(const wxTreeItemId
& item
) const;
263 // is this item currently selected (the same as has focus)?
264 bool IsSelected(const wxTreeItemId
& item
) const;
265 // is item text in bold font?
266 bool IsBold(const wxTreeItemId
& item
) const;
268 // number of children
269 // ------------------
271 // if 'recursively' is FALSE, only immediate children count, otherwise
272 // the returned number is the number of all items in this branch
273 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
278 // wxTreeItemId.IsOk() will return FALSE if there is no such item
280 // get the root tree item
281 wxTreeItemId
GetRootItem() const { return m_anchor
; }
283 // get the item currently selected (may return NULL if no selection)
284 wxTreeItemId
GetSelection() const { return m_current
; }
286 // get the items currently selected, return the number of such item
287 size_t GetSelections(wxArrayTreeItemIds
&) const;
289 // get the parent of this item (may return NULL if root)
290 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
292 // for this enumeration function you must pass in a "cookie" parameter
293 // which is opaque for the application but is necessary for the library
294 // to make these functions reentrant (i.e. allow more than one
295 // enumeration on one and the same object simultaneously). Of course,
296 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
299 // get the first child of this item
300 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const;
301 // get the next child
302 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& cookie
) const;
303 // get the last child of this item - this method doesn't use cookies
304 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
306 // get the next sibling of this item
307 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
308 // get the previous sibling
309 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
311 // get first visible item
312 wxTreeItemId
GetFirstVisibleItem() const;
313 // get the next visible item: item must be visible itself!
314 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
315 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
316 // get the previous visible item: item must be visible itself!
317 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
322 // add the root node to the tree
323 wxTreeItemId
AddRoot(const wxString
& text
,
324 int image
= -1, int selectedImage
= -1,
325 wxTreeItemData
*data
= NULL
);
327 // insert a new item in as the first child of the parent
328 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
329 const wxString
& text
,
330 int image
= -1, int selectedImage
= -1,
331 wxTreeItemData
*data
= NULL
);
333 // insert a new item after a given one
334 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
335 const wxTreeItemId
& idPrevious
,
336 const wxString
& text
,
337 int image
= -1, int selectedImage
= -1,
338 wxTreeItemData
*data
= NULL
);
340 // insert a new item before the one with the given index
341 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
343 const wxString
& text
,
344 int image
= -1, int selectedImage
= -1,
345 wxTreeItemData
*data
= NULL
);
347 // insert a new item in as the last child of the parent
348 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
349 const wxString
& text
,
350 int image
= -1, int selectedImage
= -1,
351 wxTreeItemData
*data
= NULL
);
353 // delete this item and associated data if any
354 void Delete(const wxTreeItemId
& item
);
355 // delete all children (but don't delete the item itself)
356 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
357 void DeleteChildren(const wxTreeItemId
& item
);
358 // delete all items from the tree
359 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
360 void DeleteAllItems();
363 void Expand(const wxTreeItemId
& item
);
364 // collapse the item without removing its children
365 void Collapse(const wxTreeItemId
& item
);
366 // collapse the item and remove all children
367 void CollapseAndReset(const wxTreeItemId
& item
);
368 // toggles the current state
369 void Toggle(const wxTreeItemId
& item
);
371 // remove the selection from currently selected item (if any)
375 void SelectItem(const wxTreeItemId
& item
, bool unselect_others
=TRUE
, bool extended_select
=FALSE
);
376 // make sure this item is visible (expanding the parent item and/or
377 // scrolling to this item if necessary)
378 void EnsureVisible(const wxTreeItemId
& item
);
379 // scroll to this item (but don't expand its parent)
380 void ScrollTo(const wxTreeItemId
& item
);
382 // The first function is more portable (because easier to implement
383 // on other platforms), but the second one returns some extra info.
384 wxTreeItemId
HitTest(const wxPoint
& point
)
385 { int dummy
; return HitTest(point
, dummy
); }
386 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
388 // Start editing the item label: this (temporarily) replaces the item
389 // with a one line edit control. The item will be selected if it hadn't
391 void EditLabel( const wxTreeItemId
& item
) { Edit( item
); }
392 void Edit( const wxTreeItemId
& item
);
395 // this function is called to compare 2 items and should return -1, 0
396 // or +1 if the first item is less than, equal to or greater than the
397 // second one. The base class version performs alphabetic comparaison
398 // of item labels (GetText)
399 virtual int OnCompareItems(const wxTreeItemId
& item1
,
400 const wxTreeItemId
& item2
);
401 // sort the children of this item using OnCompareItems
403 // NB: this function is not reentrant and not MT-safe (FIXME)!
404 void SortChildren(const wxTreeItemId
& item
);
406 // deprecated functions: use Set/GetItemImage directly
407 // get the selected item image
408 int GetItemSelectedImage(const wxTreeItemId
& item
) const
409 { return GetItemImage(item
, wxTreeItemIcon_Selected
); }
410 // set the selected item image
411 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
412 { SetItemImage(item
, image
, wxTreeItemIcon_Selected
); }
417 void OnPaint( wxPaintEvent
&event
);
418 void OnSetFocus( wxFocusEvent
&event
);
419 void OnKillFocus( wxFocusEvent
&event
);
420 void OnChar( wxKeyEvent
&event
);
421 void OnMouse( wxMouseEvent
&event
);
422 void OnIdle( wxIdleEvent
&event
);
425 void SendDeleteEvent(wxGenericTreeItem
*itemBeingDeleted
);
427 // Draw Special Information
428 void DrawBorder(wxTreeItemId
& item
);
429 void DrawLine(wxTreeItemId
& item
, bool below
);
432 friend class wxGenericTreeItem
;
433 friend class wxTreeRenameTimer
;
434 friend class wxTreeTextCtrl
;
439 wxGenericTreeItem
*m_anchor
;
440 wxGenericTreeItem
*m_current
, *m_key_current
, *m_currentEdit
;
443 int m_xScroll
,m_yScroll
;
444 unsigned int m_indent
;
445 unsigned int m_spacing
;
448 wxBrush
*m_hilightBrush
;
449 wxImageList
*m_imageListNormal
,
453 wxTimer
*m_renameTimer
;
455 wxString m_renameRes
;
457 // the common part of all ctors
461 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
463 const wxString
& text
,
464 int image
, int selectedImage
,
465 wxTreeItemData
*data
);
467 void AdjustMyScrollbars();
468 int GetLineHeight(wxGenericTreeItem
*item
) const;
469 void PaintLevel( wxGenericTreeItem
*item
, wxDC
& dc
, int level
, int &y
);
470 void PaintItem( wxGenericTreeItem
*item
, wxDC
& dc
);
472 void CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
);
473 void CalculatePositions();
474 void CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
);
476 void RefreshSubtree( wxGenericTreeItem
*item
);
477 void RefreshLine( wxGenericTreeItem
*item
);
479 void OnRenameTimer();
480 void OnRenameAccept();
482 void FillArray(wxGenericTreeItem
*, wxArrayTreeItemIds
&) const;
483 void SelectItemRange( wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
);
484 bool TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
485 bool TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
486 void UnselectAllChildren( wxGenericTreeItem
*item
);
489 DECLARE_EVENT_TABLE()
490 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
493 #endif // _GENERIC_TREECTRL_H_