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 // wxTreeCtrl - the tree control
101 // -----------------------------------------------------------------------------
103 class WXDLLEXPORT wxTreeCtrl
: public wxScrolledWindow
108 wxTreeCtrl() { Init(); }
110 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
111 const wxPoint
& pos
= wxDefaultPosition
,
112 const wxSize
& size
= wxDefaultSize
,
113 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
114 const wxValidator
&validator
= wxDefaultValidator
,
115 const wxString
& name
= wxTreeCtrlNameStr
)
117 Create(parent
, id
, pos
, size
, style
, validator
, name
);
120 virtual ~wxTreeCtrl();
122 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
123 const wxPoint
& pos
= wxDefaultPosition
,
124 const wxSize
& size
= wxDefaultSize
,
125 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
126 const wxValidator
&validator
= wxDefaultValidator
,
127 const wxString
& name
= wxTreeCtrlNameStr
);
132 // get the total number of items in the control
133 size_t GetCount() const;
135 // indent is the number of pixels the children are indented relative to
136 // the parents position. SetIndent() also redraws the control
138 unsigned int GetIndent() const { return m_indent
; }
139 void SetIndent(unsigned int indent
);
141 // spacing is the number of pixels between the start and the Text
142 unsigned int GetSpacing() const { return m_spacing
; }
143 void SetSpacing(unsigned int spacing
);
145 // image list: these functions allow to associate an image list with
146 // the control and retrieve it. Note that the control does _not_ delete
147 // the associated image list when it's deleted in order to allow image
148 // lists to be shared between different controls.
150 // The normal image list is for the icons which correspond to the
151 // normal tree item state (whether it is selected or not).
152 // Additionally, the application might choose to show a state icon
153 // which corresponds to an app-defined item state (for example,
154 // checked/unchecked) which are taken from the state image list.
155 wxImageList
*GetImageList() const;
156 wxImageList
*GetStateImageList() const;
158 void SetImageList(wxImageList
*imageList
);
159 void SetStateImageList(wxImageList
*imageList
);
161 // Functions to work with tree ctrl items.
166 // retrieve items label
167 wxString
GetItemText(const wxTreeItemId
& item
) const;
168 // get one of the images associated with the item (normal by default)
169 int GetItemImage(const wxTreeItemId
& item
,
170 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
171 // get the data associated with the item
172 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
178 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
179 // get one of the images associated with the item (normal by default)
180 void SetItemImage(const wxTreeItemId
& item
, int image
,
181 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
182 // associate some data with the item
183 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
185 // force appearance of [+] button near the item. This is useful to
186 // allow the user to expand the items which don't have any children now
187 // - but instead add them only when needed, thus minimizing memory
188 // usage and loading time.
189 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
191 // the item will be shown in bold
192 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
194 // set the items text colour
195 void SetItemTextColour(const wxTreeItemId
& item
, const wxColour
& col
);
197 // set the items background colour
198 void SetItemBackgroundColour(const wxTreeItemId
& item
, const wxColour
& col
);
200 // set the items font (should be of the same height for all items)
201 void SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
);
203 // item status inquiries
204 // ---------------------
206 // is the item visible (it might be outside the view or not expanded)?
207 bool IsVisible(const wxTreeItemId
& item
) const;
208 // does the item has any children?
209 bool HasChildren(const wxTreeItemId
& item
) const
210 { return ItemHasChildren(item
); }
211 bool ItemHasChildren(const wxTreeItemId
& item
) const;
212 // is the item expanded (only makes sense if HasChildren())?
213 bool IsExpanded(const wxTreeItemId
& item
) const;
214 // is this item currently selected (the same as has focus)?
215 bool IsSelected(const wxTreeItemId
& item
) const;
216 // is item text in bold font?
217 bool IsBold(const wxTreeItemId
& item
) const;
219 // number of children
220 // ------------------
222 // if 'recursively' is FALSE, only immediate children count, otherwise
223 // the returned number is the number of all items in this branch
224 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
229 // wxTreeItemId.IsOk() will return FALSE if there is no such item
231 // get the root tree item
232 wxTreeItemId
GetRootItem() const { return m_anchor
; }
234 // get the item currently selected (may return NULL if no selection)
235 wxTreeItemId
GetSelection() const { return m_current
; }
237 // get the items currently selected, return the number of such item
238 size_t GetSelections(wxArrayTreeItemIds
&) const;
240 // get the parent of this item (may return NULL if root)
241 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
243 // for this enumeration function you must pass in a "cookie" parameter
244 // which is opaque for the application but is necessary for the library
245 // to make these functions reentrant (i.e. allow more than one
246 // enumeration on one and the same object simultaneously). Of course,
247 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
250 // get the first child of this item
251 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const;
252 // get the next child
253 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& cookie
) const;
254 // get the last child of this item - this method doesn't use cookies
255 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
257 // get the next sibling of this item
258 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
259 // get the previous sibling
260 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
262 // get first visible item
263 wxTreeItemId
GetFirstVisibleItem() const;
264 // get the next visible item: item must be visible itself!
265 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
266 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
267 // get the previous visible item: item must be visible itself!
268 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
273 // add the root node to the tree
274 wxTreeItemId
AddRoot(const wxString
& text
,
275 int image
= -1, int selectedImage
= -1,
276 wxTreeItemData
*data
= NULL
);
278 // insert a new item in as the first child of the parent
279 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
280 const wxString
& text
,
281 int image
= -1, int selectedImage
= -1,
282 wxTreeItemData
*data
= NULL
);
284 // insert a new item after a given one
285 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
286 const wxTreeItemId
& idPrevious
,
287 const wxString
& text
,
288 int image
= -1, int selectedImage
= -1,
289 wxTreeItemData
*data
= NULL
);
291 // insert a new item before the one with the given index
292 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
294 const wxString
& text
,
295 int image
= -1, int selectedImage
= -1,
296 wxTreeItemData
*data
= NULL
);
298 // insert a new item in as the last child of the parent
299 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
300 const wxString
& text
,
301 int image
= -1, int selectedImage
= -1,
302 wxTreeItemData
*data
= NULL
);
304 // delete this item and associated data if any
305 void Delete(const wxTreeItemId
& item
);
306 // delete all children (but don't delete the item itself)
307 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
308 void DeleteChildren(const wxTreeItemId
& item
);
309 // delete all items from the tree
310 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
311 void DeleteAllItems();
314 void Expand(const wxTreeItemId
& item
);
315 // expand this item and all subitems recursively
316 void ExpandAll(const wxTreeItemId
& item
);
317 // collapse the item without removing its children
318 void Collapse(const wxTreeItemId
& item
);
319 // collapse the item and remove all children
320 void CollapseAndReset(const wxTreeItemId
& item
);
321 // toggles the current state
322 void Toggle(const wxTreeItemId
& item
);
324 // remove the selection from currently selected item (if any)
328 void SelectItem(const wxTreeItemId
& item
, bool unselect_others
=TRUE
, bool extended_select
=FALSE
);
329 // make sure this item is visible (expanding the parent item and/or
330 // scrolling to this item if necessary)
331 void EnsureVisible(const wxTreeItemId
& item
);
332 // scroll to this item (but don't expand its parent)
333 void ScrollTo(const wxTreeItemId
& item
);
335 // The first function is more portable (because easier to implement
336 // on other platforms), but the second one returns some extra info.
337 wxTreeItemId
HitTest(const wxPoint
& point
)
338 { int dummy
; return HitTest(point
, dummy
); }
339 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
341 // Start editing the item label: this (temporarily) replaces the item
342 // with a one line edit control. The item will be selected if it hadn't
344 void EditLabel( const wxTreeItemId
& item
) { Edit( item
); }
345 void Edit( const wxTreeItemId
& item
);
348 // this function is called to compare 2 items and should return -1, 0
349 // or +1 if the first item is less than, equal to or greater than the
350 // second one. The base class version performs alphabetic comparaison
351 // of item labels (GetText)
352 virtual int OnCompareItems(const wxTreeItemId
& item1
,
353 const wxTreeItemId
& item2
);
354 // sort the children of this item using OnCompareItems
356 // NB: this function is not reentrant and not MT-safe (FIXME)!
357 void SortChildren(const wxTreeItemId
& item
);
359 // deprecated functions: use Set/GetItemImage directly
360 // get the selected item image
361 int GetItemSelectedImage(const wxTreeItemId
& item
) const
362 { return GetItemImage(item
, wxTreeItemIcon_Selected
); }
363 // set the selected item image
364 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
365 { SetItemImage(item
, image
, wxTreeItemIcon_Selected
); }
367 // implementation only from now on
370 void OnPaint( wxPaintEvent
&event
);
371 void OnSetFocus( wxFocusEvent
&event
);
372 void OnKillFocus( wxFocusEvent
&event
);
373 void OnChar( wxKeyEvent
&event
);
374 void OnMouse( wxMouseEvent
&event
);
375 void OnIdle( wxIdleEvent
&event
);
377 // implementation helpers
378 void SendDeleteEvent(wxGenericTreeItem
*itemBeingDeleted
);
380 void DrawBorder(const wxTreeItemId
& item
);
381 void DrawLine(const wxTreeItemId
& item
, bool below
);
384 friend class wxGenericTreeItem
;
385 friend class wxTreeRenameTimer
;
386 friend class wxTreeTextCtrl
;
391 wxGenericTreeItem
*m_anchor
;
392 wxGenericTreeItem
*m_current
, *m_key_current
, *m_currentEdit
;
395 int m_xScroll
,m_yScroll
;
396 unsigned int m_indent
;
397 unsigned int m_spacing
;
400 wxBrush
*m_hilightBrush
;
401 wxImageList
*m_imageListNormal
,
406 bool m_isDragging
; // true between BEGIN/END drag events
407 wxGenericTreeItem
*m_dropTarget
;
408 wxCursor m_oldCursor
; // cursor is changed while dragging
409 wxGenericTreeItem
*m_oldSelection
;
411 wxTimer
*m_renameTimer
;
413 bool m_lastOnSame
; // last click on the same item as prev
414 wxString m_renameRes
;
416 // the common part of all ctors
420 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
422 const wxString
& text
,
423 int image
, int selectedImage
,
424 wxTreeItemData
*data
);
426 void AdjustMyScrollbars();
427 int GetLineHeight(wxGenericTreeItem
*item
) const;
428 void PaintLevel( wxGenericTreeItem
*item
, wxDC
& dc
, int level
, int &y
);
429 void PaintItem( wxGenericTreeItem
*item
, wxDC
& dc
);
431 void CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
);
432 void CalculatePositions();
433 void CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
);
435 void RefreshSubtree( wxGenericTreeItem
*item
);
436 void RefreshLine( wxGenericTreeItem
*item
);
438 void OnRenameTimer();
439 void OnRenameAccept();
441 void FillArray(wxGenericTreeItem
*, wxArrayTreeItemIds
&) const;
442 void SelectItemRange( wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
);
443 bool TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
444 bool TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
445 void UnselectAllChildren( wxGenericTreeItem
*item
);
447 void DrawDropEffect(wxGenericTreeItem
*item
);
450 DECLARE_EVENT_TABLE()
451 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
454 #endif // _GENERIC_TREECTRL_H_