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 "treectlg.h"
21 #include "wx/scrolwin.h"
24 // -----------------------------------------------------------------------------
25 // forward declaration
26 // -----------------------------------------------------------------------------
28 class WXDLLEXPORT wxImageList
;
29 class WXDLLEXPORT wxGenericTreeItem
;
31 class WXDLLEXPORT wxTreeItemData
;
33 class WXDLLEXPORT wxTreeRenameTimer
;
34 class WXDLLEXPORT wxTreeTextCtrl
;
36 // -----------------------------------------------------------------------------
37 // wxGenericTreeCtrl - the tree control
38 // -----------------------------------------------------------------------------
40 class WXDLLEXPORT wxGenericTreeCtrl
: public wxScrolledWindow
45 wxGenericTreeCtrl() { Init(); }
47 wxGenericTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
48 const wxPoint
& pos
= wxDefaultPosition
,
49 const wxSize
& size
= wxDefaultSize
,
50 long style
= wxTR_DEFAULT_STYLE
,
51 const wxValidator
&validator
= wxDefaultValidator
,
52 const wxString
& name
= wxTreeCtrlNameStr
)
55 Create(parent
, id
, pos
, size
, style
, validator
, name
);
58 virtual ~wxGenericTreeCtrl();
60 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
61 const wxPoint
& pos
= wxDefaultPosition
,
62 const wxSize
& size
= wxDefaultSize
,
63 long style
= wxTR_DEFAULT_STYLE
,
64 const wxValidator
&validator
= wxDefaultValidator
,
65 const wxString
& name
= wxTreeCtrlNameStr
);
70 // get the total number of items in the control
71 size_t GetCount() const;
73 // indent is the number of pixels the children are indented relative to
74 // the parents position. SetIndent() also redraws the control
76 unsigned int GetIndent() const { return m_indent
; }
77 void SetIndent(unsigned int indent
);
79 // spacing is the number of pixels between the start and the Text
80 unsigned int GetSpacing() const { return m_spacing
; }
81 void SetSpacing(unsigned int spacing
);
83 // image list: these functions allow to associate an image list with
84 // the control and retrieve it. Note that when assigned with
85 // SetImageList, the control does _not_ delete
86 // the associated image list when it's deleted in order to allow image
87 // lists to be shared between different controls. If you use
88 // AssignImageList, the control _does_ delete the image list.
90 // The normal image list is for the icons which correspond to the
91 // normal tree item state (whether it is selected or not).
92 // Additionally, the application might choose to show a state icon
93 // which corresponds to an app-defined item state (for example,
94 // checked/unchecked) which are taken from the state image list.
95 wxImageList
*GetImageList() const;
96 wxImageList
*GetStateImageList() const;
97 wxImageList
*GetButtonsImageList() const;
99 void SetImageList(wxImageList
*imageList
);
100 void SetStateImageList(wxImageList
*imageList
);
101 void SetButtonsImageList(wxImageList
*imageList
);
102 void AssignImageList(wxImageList
*imageList
);
103 void AssignStateImageList(wxImageList
*imageList
);
104 void AssignButtonsImageList(wxImageList
*imageList
);
106 // Functions to work with tree ctrl items.
111 // retrieve item's label
112 wxString
GetItemText(const wxTreeItemId
& item
) const;
113 // get one of the images associated with the item (normal by default)
114 int GetItemImage(const wxTreeItemId
& item
,
115 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
116 // get the data associated with the item
117 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
123 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
124 // get one of the images associated with the item (normal by default)
125 void SetItemImage(const wxTreeItemId
& item
, int image
,
126 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
127 // associate some data with the item
128 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
130 // force appearance of [+] button near the item. This is useful to
131 // allow the user to expand the items which don't have any children now
132 // - but instead add them only when needed, thus minimizing memory
133 // usage and loading time.
134 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
136 // the item will be shown in bold
137 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
139 // set the item's text colour
140 void SetItemTextColour(const wxTreeItemId
& item
, const wxColour
& col
);
142 // set the item's background colour
143 void SetItemBackgroundColour(const wxTreeItemId
& item
, const wxColour
& col
);
145 // set the item's font (should be of the same height for all items)
146 void SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
);
148 // set the window font
149 virtual bool SetFont( const wxFont
&font
);
151 // set the styles. No need to specify a GetWindowStyle here since
152 // the base wxWindow member function will do it for us
153 void SetWindowStyle(const long styles
);
155 // item status inquiries
156 // ---------------------
158 // is the item visible (it might be outside the view or not expanded)?
159 bool IsVisible(const wxTreeItemId
& item
) const;
160 // does the item has any children?
161 bool HasChildren(const wxTreeItemId
& item
) const
162 { return ItemHasChildren(item
); }
163 bool ItemHasChildren(const wxTreeItemId
& item
) const;
164 // is the item expanded (only makes sense if HasChildren())?
165 bool IsExpanded(const wxTreeItemId
& item
) const;
166 // is this item currently selected (the same as has focus)?
167 bool IsSelected(const wxTreeItemId
& item
) const;
168 // is item text in bold font?
169 bool IsBold(const wxTreeItemId
& item
) const;
170 // does the layout include space for a button?
172 // number of children
173 // ------------------
175 // if 'recursively' is FALSE, only immediate children count, otherwise
176 // the returned number is the number of all items in this branch
177 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
182 // wxTreeItemId.IsOk() will return FALSE if there is no such item
184 // get the root tree item
185 wxTreeItemId
GetRootItem() const { return m_anchor
; }
187 // get the item currently selected (may return NULL if no selection)
188 wxTreeItemId
GetSelection() const { return m_current
; }
190 // get the items currently selected, return the number of such item
191 size_t GetSelections(wxArrayTreeItemIds
&) const;
193 // get the parent of this item (may return NULL if root)
194 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
196 // for this enumeration function you must pass in a "cookie" parameter
197 // which is opaque for the application but is necessary for the library
198 // to make these functions reentrant (i.e. allow more than one
199 // enumeration on one and the same object simultaneously). Of course,
200 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
203 // get the first child of this item
204 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const;
205 // get the next child
206 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& cookie
) const;
207 // get the last child of this item - this method doesn't use cookies
208 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
210 // get the next sibling of this item
211 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
212 // get the previous sibling
213 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
215 // get first visible item
216 wxTreeItemId
GetFirstVisibleItem() const;
217 // get the next visible item: item must be visible itself!
218 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
219 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
220 // get the previous visible item: item must be visible itself!
221 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
223 // Only for internal use right now, but should probably be public
224 wxTreeItemId
GetNext(const wxTreeItemId
& item
) const;
225 wxTreeItemId
GetPrev(const wxTreeItemId
& item
) const;
230 // add the root node to the tree
231 wxTreeItemId
AddRoot(const wxString
& text
,
232 int image
= -1, int selectedImage
= -1,
233 wxTreeItemData
*data
= NULL
);
235 // insert a new item in as the first child of the parent
236 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
237 const wxString
& text
,
238 int image
= -1, int selectedImage
= -1,
239 wxTreeItemData
*data
= NULL
);
241 // insert a new item after a given one
242 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
243 const wxTreeItemId
& idPrevious
,
244 const wxString
& text
,
245 int image
= -1, int selectedImage
= -1,
246 wxTreeItemData
*data
= NULL
);
248 // insert a new item before the one with the given index
249 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
251 const wxString
& text
,
252 int image
= -1, int selectedImage
= -1,
253 wxTreeItemData
*data
= NULL
);
255 // insert a new item in as the last child of the parent
256 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
257 const wxString
& text
,
258 int image
= -1, int selectedImage
= -1,
259 wxTreeItemData
*data
= NULL
);
261 // delete this item and associated data if any
262 void Delete(const wxTreeItemId
& item
);
263 // delete all children (but don't delete the item itself)
264 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
265 void DeleteChildren(const wxTreeItemId
& item
);
266 // delete all items from the tree
267 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
268 void DeleteAllItems();
271 void Expand(const wxTreeItemId
& item
);
272 // expand this item and all subitems recursively
273 void ExpandAll(const wxTreeItemId
& item
);
274 // collapse the item without removing its children
275 void Collapse(const wxTreeItemId
& item
);
276 // collapse the item and remove all children
277 void CollapseAndReset(const wxTreeItemId
& item
);
278 // toggles the current state
279 void Toggle(const wxTreeItemId
& item
);
281 // remove the selection from currently selected item (if any)
285 void SelectItem(const wxTreeItemId
& item
, bool unselect_others
=TRUE
, bool extended_select
=FALSE
);
286 // make sure this item is visible (expanding the parent item and/or
287 // scrolling to this item if necessary)
288 void EnsureVisible(const wxTreeItemId
& item
);
289 // scroll to this item (but don't expand its parent)
290 void ScrollTo(const wxTreeItemId
& item
);
291 void AdjustMyScrollbars();
293 // The first function is more portable (because easier to implement
294 // on other platforms), but the second one returns some extra info.
295 wxTreeItemId
HitTest(const wxPoint
& point
)
296 { int dummy
; return HitTest(point
, dummy
); }
297 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
299 // get the bounding rectangle of the item (or of its label only)
300 bool GetBoundingRect(const wxTreeItemId
& item
,
302 bool textOnly
= FALSE
) const;
304 // Start editing the item label: this (temporarily) replaces the item
305 // with a one line edit control. The item will be selected if it hadn't
307 void EditLabel( const wxTreeItemId
& item
) { Edit( item
); }
308 void Edit( const wxTreeItemId
& item
);
311 // this function is called to compare 2 items and should return -1, 0
312 // or +1 if the first item is less than, equal to or greater than the
313 // second one. The base class version performs alphabetic comparaison
314 // of item labels (GetText)
315 virtual int OnCompareItems(const wxTreeItemId
& item1
,
316 const wxTreeItemId
& item2
);
317 // sort the children of this item using OnCompareItems
319 // NB: this function is not reentrant and not MT-safe (FIXME)!
320 void SortChildren(const wxTreeItemId
& item
);
322 // deprecated functions: use Set/GetItemImage directly
323 // get the selected item image
324 int GetItemSelectedImage(const wxTreeItemId
& item
) const
325 { return GetItemImage(item
, wxTreeItemIcon_Selected
); }
326 // set the selected item image
327 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
328 { SetItemImage(item
, image
, wxTreeItemIcon_Selected
); }
330 // implementation only from now on
333 void OnPaint( wxPaintEvent
&event
);
334 void OnSetFocus( wxFocusEvent
&event
);
335 void OnKillFocus( wxFocusEvent
&event
);
336 void OnChar( wxKeyEvent
&event
);
337 void OnMouse( wxMouseEvent
&event
);
338 void OnIdle( wxIdleEvent
&event
);
340 // implementation helpers
341 void SendDeleteEvent(wxGenericTreeItem
*itemBeingDeleted
);
343 void DrawBorder(const wxTreeItemId
& item
);
344 void DrawLine(const wxTreeItemId
& item
, bool below
);
347 friend class wxGenericTreeItem
;
348 friend class wxTreeRenameTimer
;
349 friend class wxTreeTextCtrl
;
354 wxGenericTreeItem
*m_anchor
;
355 wxGenericTreeItem
*m_current
, *m_key_current
, *m_currentEdit
;
356 unsigned short m_indent
;
357 unsigned short m_spacing
;
360 wxBrush
*m_hilightBrush
,
361 *m_hilightUnfocusedBrush
;
364 bool m_ownsImageListNormal
,
365 m_ownsImageListState
,
366 m_ownsImageListButtons
;
367 bool m_isDragging
; // true between BEGIN/END drag events
369 bool m_lastOnSame
; // last click on the same item as prev
370 wxImageList
*m_imageListNormal
,
376 wxGenericTreeItem
*m_dropTarget
;
377 wxCursor m_oldCursor
; // cursor is changed while dragging
378 wxGenericTreeItem
*m_oldSelection
;
380 wxTimer
*m_renameTimer
;
381 wxString m_renameRes
;
383 // the common part of all ctors
387 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
389 const wxString
& text
,
390 int image
, int selectedImage
,
391 wxTreeItemData
*data
);
392 bool HasButtons(void) const
393 { return (m_imageListButtons
!= NULL
)
394 || HasFlag(wxTR_TWIST_BUTTONS
|wxTR_HAS_BUTTONS
); }
397 void CalculateLineHeight();
398 int GetLineHeight(wxGenericTreeItem
*item
) const;
399 void PaintLevel( wxGenericTreeItem
*item
, wxDC
& dc
, int level
, int &y
);
400 void PaintItem( wxGenericTreeItem
*item
, wxDC
& dc
);
402 void CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
);
403 void CalculatePositions();
404 void CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
);
406 void RefreshSubtree( wxGenericTreeItem
*item
);
407 void RefreshLine( wxGenericTreeItem
*item
);
409 // redraw all selected items
410 void RefreshSelected();
412 // RefreshSelected() recursive helper
413 void RefreshSelectedUnder(wxGenericTreeItem
*item
);
415 void OnRenameTimer();
416 void OnRenameAccept();
418 void FillArray(wxGenericTreeItem
*, wxArrayTreeItemIds
&) const;
419 void SelectItemRange( wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
);
420 bool TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
421 bool TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
422 void UnselectAllChildren( wxGenericTreeItem
*item
);
424 void DrawDropEffect(wxGenericTreeItem
*item
);
427 DECLARE_EVENT_TABLE()
428 DECLARE_DYNAMIC_CLASS(wxGenericTreeCtrl
)
431 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
433 * wxTreeCtrl has to be a real class or we have problems with
434 * the run-time information.
437 class WXDLLEXPORT wxTreeCtrl
: public wxGenericTreeCtrl
439 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
444 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
445 const wxPoint
& pos
= wxDefaultPosition
,
446 const wxSize
& size
= wxDefaultSize
,
447 long style
= wxTR_DEFAULT_STYLE
,
448 const wxValidator
&validator
= wxDefaultValidator
,
449 const wxString
& name
= wxTreeCtrlNameStr
)
450 : wxGenericTreeCtrl(parent
, id
, pos
, size
, style
, validator
, name
)
454 #endif // !__WXMSW__ || __WIN16__ || __WXUNIVERSAL__
456 #endif // wxUSE_TREECTRL
458 #endif // _GENERIC_TREECTRL_H_