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"
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"
28 #include "wx/treebase.h"
30 // -----------------------------------------------------------------------------
31 // forward declaration
32 // -----------------------------------------------------------------------------
34 class WXDLLEXPORT wxImageList
;
35 class WXDLLEXPORT wxGenericTreeItem
;
37 class WXDLLEXPORT wxTreeItemData
;
39 class WXDLLEXPORT wxTreeRenameTimer
;
40 class WXDLLEXPORT wxTreeTextCtrl
;
42 // -----------------------------------------------------------------------------
43 // wxGenericTreeCtrl - the tree control
44 // -----------------------------------------------------------------------------
46 class WXDLLEXPORT wxGenericTreeCtrl
: public wxScrolledWindow
51 wxGenericTreeCtrl() { Init(); }
53 wxGenericTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
54 const wxPoint
& pos
= wxDefaultPosition
,
55 const wxSize
& size
= wxDefaultSize
,
57 long style
= wxTR_MAC_BUTTONS
| wxTR_NO_LINES
| wxTR_ROW_LINES
,
59 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
61 const wxValidator
&validator
= wxDefaultValidator
,
62 const wxString
& name
= wxTreeCtrlNameStr
)
65 Create(parent
, id
, pos
, size
, style
, validator
, name
);
68 virtual ~wxGenericTreeCtrl();
70 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
71 const wxPoint
& pos
= wxDefaultPosition
,
72 const wxSize
& size
= wxDefaultSize
,
74 long style
= wxTR_MAC_BUTTONS
| wxTR_NO_LINES
| wxTR_ROW_LINES
,
76 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
78 const wxValidator
&validator
= wxDefaultValidator
,
79 const wxString
& name
= wxTreeCtrlNameStr
);
84 // get the total number of items in the control
85 size_t GetCount() const;
87 // indent is the number of pixels the children are indented relative to
88 // the parents position. SetIndent() also redraws the control
90 unsigned int GetIndent() const { return m_indent
; }
91 void SetIndent(unsigned int indent
);
93 // spacing is the number of pixels between the start and the Text
94 unsigned int GetSpacing() const { return m_spacing
; }
95 void SetSpacing(unsigned int spacing
);
97 // image list: these functions allow to associate an image list with
98 // the control and retrieve it. Note that when assigned with
99 // SetImageList, the control does _not_ delete
100 // the associated image list when it's deleted in order to allow image
101 // lists to be shared between different controls. If you use
102 // AssignImageList, the control _does_ delete the image list.
104 // The normal image list is for the icons which correspond to the
105 // normal tree item state (whether it is selected or not).
106 // Additionally, the application might choose to show a state icon
107 // which corresponds to an app-defined item state (for example,
108 // checked/unchecked) which are taken from the state image list.
109 wxImageList
*GetImageList() const;
110 wxImageList
*GetStateImageList() const;
112 void SetImageList(wxImageList
*imageList
);
113 void SetStateImageList(wxImageList
*imageList
);
114 void AssignImageList(wxImageList
*imageList
);
115 void AssignStateImageList(wxImageList
*imageList
);
117 // Functions to work with tree ctrl items.
122 // retrieve items label
123 wxString
GetItemText(const wxTreeItemId
& item
) const;
124 // get one of the images associated with the item (normal by default)
125 int GetItemImage(const wxTreeItemId
& item
,
126 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
127 // get the data associated with the item
128 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
134 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
135 // get one of the images associated with the item (normal by default)
136 void SetItemImage(const wxTreeItemId
& item
, int image
,
137 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
138 // associate some data with the item
139 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
141 // force appearance of [+] button near the item. This is useful to
142 // allow the user to expand the items which don't have any children now
143 // - but instead add them only when needed, thus minimizing memory
144 // usage and loading time.
145 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
147 // the item will be shown in bold
148 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
150 // set the items text colour
151 void SetItemTextColour(const wxTreeItemId
& item
, const wxColour
& col
);
153 // set the items background colour
154 void SetItemBackgroundColour(const wxTreeItemId
& item
, const wxColour
& col
);
156 // set the items font (should be of the same height for all items)
157 void SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
);
159 // set the window font
160 virtual bool SetFont( const wxFont
&font
);
162 // item status inquiries
163 // ---------------------
165 // is the item visible (it might be outside the view or not expanded)?
166 bool IsVisible(const wxTreeItemId
& item
) const;
167 // does the item has any children?
168 bool HasChildren(const wxTreeItemId
& item
) const
169 { return ItemHasChildren(item
); }
170 bool ItemHasChildren(const wxTreeItemId
& item
) const;
171 // is the item expanded (only makes sense if HasChildren())?
172 bool IsExpanded(const wxTreeItemId
& item
) const;
173 // is this item currently selected (the same as has focus)?
174 bool IsSelected(const wxTreeItemId
& item
) const;
175 // is item text in bold font?
176 bool IsBold(const wxTreeItemId
& item
) const;
178 // number of children
179 // ------------------
181 // if 'recursively' is FALSE, only immediate children count, otherwise
182 // the returned number is the number of all items in this branch
183 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
188 // wxTreeItemId.IsOk() will return FALSE if there is no such item
190 // get the root tree item
191 wxTreeItemId
GetRootItem() const { return m_anchor
; }
193 // get the item currently selected (may return NULL if no selection)
194 wxTreeItemId
GetSelection() const { return m_current
; }
196 // get the items currently selected, return the number of such item
197 size_t GetSelections(wxArrayTreeItemIds
&) const;
199 // get the parent of this item (may return NULL if root)
200 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
202 // for this enumeration function you must pass in a "cookie" parameter
203 // which is opaque for the application but is necessary for the library
204 // to make these functions reentrant (i.e. allow more than one
205 // enumeration on one and the same object simultaneously). Of course,
206 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
209 // get the first child of this item
210 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const;
211 // get the next child
212 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& cookie
) const;
213 // get the last child of this item - this method doesn't use cookies
214 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
216 // get the next sibling of this item
217 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
218 // get the previous sibling
219 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
221 // get first visible item
222 wxTreeItemId
GetFirstVisibleItem() const;
223 // get the next visible item: item must be visible itself!
224 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
225 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
226 // get the previous visible item: item must be visible itself!
227 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
229 // Only for internal use right now, but should probably be public
230 wxTreeItemId
GetNext(const wxTreeItemId
& item
) const;
231 wxTreeItemId
GetPrev(const wxTreeItemId
& item
) const;
236 // add the root node to the tree
237 wxTreeItemId
AddRoot(const wxString
& text
,
238 int image
= -1, int selectedImage
= -1,
239 wxTreeItemData
*data
= NULL
);
241 // insert a new item in as the first child of the parent
242 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
243 const wxString
& text
,
244 int image
= -1, int selectedImage
= -1,
245 wxTreeItemData
*data
= NULL
);
247 // insert a new item after a given one
248 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
249 const wxTreeItemId
& idPrevious
,
250 const wxString
& text
,
251 int image
= -1, int selectedImage
= -1,
252 wxTreeItemData
*data
= NULL
);
254 // insert a new item before the one with the given index
255 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
257 const wxString
& text
,
258 int image
= -1, int selectedImage
= -1,
259 wxTreeItemData
*data
= NULL
);
261 // insert a new item in as the last child of the parent
262 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
263 const wxString
& text
,
264 int image
= -1, int selectedImage
= -1,
265 wxTreeItemData
*data
= NULL
);
267 // delete this item and associated data if any
268 void Delete(const wxTreeItemId
& item
);
269 // delete all children (but don't delete the item itself)
270 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
271 void DeleteChildren(const wxTreeItemId
& item
);
272 // delete all items from the tree
273 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
274 void DeleteAllItems();
277 void Expand(const wxTreeItemId
& item
);
278 // expand this item and all subitems recursively
279 void ExpandAll(const wxTreeItemId
& item
);
280 // collapse the item without removing its children
281 void Collapse(const wxTreeItemId
& item
);
282 // collapse the item and remove all children
283 void CollapseAndReset(const wxTreeItemId
& item
);
284 // toggles the current state
285 void Toggle(const wxTreeItemId
& item
);
287 // remove the selection from currently selected item (if any)
291 void SelectItem(const wxTreeItemId
& item
, bool unselect_others
=TRUE
, bool extended_select
=FALSE
);
292 // make sure this item is visible (expanding the parent item and/or
293 // scrolling to this item if necessary)
294 void EnsureVisible(const wxTreeItemId
& item
);
295 // scroll to this item (but don't expand its parent)
296 void ScrollTo(const wxTreeItemId
& item
);
298 // The first function is more portable (because easier to implement
299 // on other platforms), but the second one returns some extra info.
300 wxTreeItemId
HitTest(const wxPoint
& point
)
301 { int dummy
; return HitTest(point
, dummy
); }
302 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
304 // get the bounding rectangle of the item (or of its label only)
305 bool GetBoundingRect(const wxTreeItemId
& item
,
307 bool textOnly
= FALSE
) const;
309 // Start editing the item label: this (temporarily) replaces the item
310 // with a one line edit control. The item will be selected if it hadn't
312 void EditLabel( const wxTreeItemId
& item
) { Edit( item
); }
313 void Edit( const wxTreeItemId
& item
);
316 // this function is called to compare 2 items and should return -1, 0
317 // or +1 if the first item is less than, equal to or greater than the
318 // second one. The base class version performs alphabetic comparaison
319 // of item labels (GetText)
320 virtual int OnCompareItems(const wxTreeItemId
& item1
,
321 const wxTreeItemId
& item2
);
322 // sort the children of this item using OnCompareItems
324 // NB: this function is not reentrant and not MT-safe (FIXME)!
325 void SortChildren(const wxTreeItemId
& item
);
327 // deprecated functions: use Set/GetItemImage directly
328 // get the selected item image
329 int GetItemSelectedImage(const wxTreeItemId
& item
) const
330 { return GetItemImage(item
, wxTreeItemIcon_Selected
); }
331 // set the selected item image
332 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
333 { SetItemImage(item
, image
, wxTreeItemIcon_Selected
); }
335 // implementation only from now on
338 void OnPaint( wxPaintEvent
&event
);
339 void OnSetFocus( wxFocusEvent
&event
);
340 void OnKillFocus( wxFocusEvent
&event
);
341 void OnChar( wxKeyEvent
&event
);
342 void OnMouse( wxMouseEvent
&event
);
343 void OnIdle( wxIdleEvent
&event
);
345 // implementation helpers
346 void SendDeleteEvent(wxGenericTreeItem
*itemBeingDeleted
);
348 void DrawBorder(const wxTreeItemId
& item
);
349 void DrawLine(const wxTreeItemId
& item
, bool below
);
352 friend class wxGenericTreeItem
;
353 friend class wxTreeRenameTimer
;
354 friend class wxTreeTextCtrl
;
359 wxGenericTreeItem
*m_anchor
;
360 wxGenericTreeItem
*m_current
, *m_key_current
, *m_currentEdit
;
363 int m_xScroll
,m_yScroll
;
364 unsigned int m_indent
;
365 unsigned int m_spacing
;
368 wxBrush
*m_hilightBrush
;
369 wxImageList
*m_imageListNormal
,
371 bool m_ownsImageListNormal
,
372 m_ownsImageListState
;
376 bool m_isDragging
; // true between BEGIN/END drag events
377 wxGenericTreeItem
*m_dropTarget
;
378 wxCursor m_oldCursor
; // cursor is changed while dragging
379 wxGenericTreeItem
*m_oldSelection
;
381 wxTimer
*m_renameTimer
;
383 bool m_lastOnSame
; // last click on the same item as prev
384 wxString m_renameRes
;
386 // the common part of all ctors
390 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
392 const wxString
& text
,
393 int image
, int selectedImage
,
394 wxTreeItemData
*data
);
397 void AdjustMyScrollbars();
399 int GetLineHeight(wxGenericTreeItem
*item
) const;
400 void PaintLevel( wxGenericTreeItem
*item
, wxDC
& dc
, int level
, int &y
);
401 void PaintItem( wxGenericTreeItem
*item
, wxDC
& dc
);
403 void CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
);
404 void CalculatePositions();
405 void CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
);
407 void RefreshSubtree( wxGenericTreeItem
*item
);
408 void RefreshLine( wxGenericTreeItem
*item
);
410 void OnRenameTimer();
411 void OnRenameAccept();
413 void FillArray(wxGenericTreeItem
*, wxArrayTreeItemIds
&) const;
414 void SelectItemRange( wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
);
415 bool TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
416 bool TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
417 void UnselectAllChildren( wxGenericTreeItem
*item
);
419 void DrawDropEffect(wxGenericTreeItem
*item
);
422 DECLARE_EVENT_TABLE()
423 DECLARE_DYNAMIC_CLASS(wxGenericTreeCtrl
)
426 #if !defined(__WXMSW__) || defined(__WIN16__)
428 * wxTreeCtrl has to be a real class or we have problems with
429 * the run-time information.
432 class WXDLLEXPORT wxTreeCtrl
: public wxGenericTreeCtrl
435 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
439 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
440 const wxPoint
& pos
= wxDefaultPosition
,
441 const wxSize
& size
= wxDefaultSize
,
442 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
443 const wxValidator
&validator
= wxDefaultValidator
,
444 const wxString
& name
= wxTreeCtrlNameStr
)
446 Create(parent
, id
, pos
, size
, style
, validator
, name
);
453 #endif // _GENERIC_TREECTRL_H_