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"
22 #include "wx/string.h"
23 #include "wx/object.h"
25 #include "wx/scrolwin.h"
26 #include "wx/textctrl.h"
28 #include "wx/dynarray.h"
30 #include "wx/treebase.h"
32 // -----------------------------------------------------------------------------
33 // forward declaration
34 // -----------------------------------------------------------------------------
36 class WXDLLEXPORT wxImageList
;
37 class WXDLLEXPORT wxGenericTreeItem
;
39 class WXDLLEXPORT wxTreeItemData
;
41 class WXDLLEXPORT wxTreeRenameTimer
;
42 class WXDLLEXPORT wxTreeTextCtrl
;
44 // -----------------------------------------------------------------------------
45 // wxGenericTreeCtrl - the tree control
46 // -----------------------------------------------------------------------------
48 class WXDLLEXPORT wxGenericTreeCtrl
: public wxScrolledWindow
53 wxGenericTreeCtrl() { Init(); }
55 wxGenericTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
56 const wxPoint
& pos
= wxDefaultPosition
,
57 const wxSize
& size
= wxDefaultSize
,
59 long style
= wxTR_MAC_BUTTONS
| wxTR_NO_LINES
| wxTR_ROW_LINES
,
61 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
63 const wxValidator
&validator
= wxDefaultValidator
,
64 const wxString
& name
= wxTreeCtrlNameStr
)
67 Create(parent
, id
, pos
, size
, style
, validator
, name
);
70 virtual ~wxGenericTreeCtrl();
72 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
73 const wxPoint
& pos
= wxDefaultPosition
,
74 const wxSize
& size
= wxDefaultSize
,
76 long style
= wxTR_MAC_BUTTONS
| wxTR_NO_LINES
| wxTR_ROW_LINES
,
78 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
80 const wxValidator
&validator
= wxDefaultValidator
,
81 const wxString
& name
= wxTreeCtrlNameStr
);
86 // get the total number of items in the control
87 size_t GetCount() const;
89 // indent is the number of pixels the children are indented relative to
90 // the parents position. SetIndent() also redraws the control
92 unsigned int GetIndent() const { return m_indent
; }
93 void SetIndent(unsigned int indent
);
95 // spacing is the number of pixels between the start and the Text
96 unsigned int GetSpacing() const { return m_spacing
; }
97 void SetSpacing(unsigned int spacing
);
99 // image list: these functions allow to associate an image list with
100 // the control and retrieve it. Note that when assigned with
101 // SetImageList, the control does _not_ delete
102 // the associated image list when it's deleted in order to allow image
103 // lists to be shared between different controls. If you use
104 // AssignImageList, the control _does_ delete the image list.
106 // The normal image list is for the icons which correspond to the
107 // normal tree item state (whether it is selected or not).
108 // Additionally, the application might choose to show a state icon
109 // which corresponds to an app-defined item state (for example,
110 // checked/unchecked) which are taken from the state image list.
111 wxImageList
*GetImageList() const;
112 wxImageList
*GetStateImageList() const;
114 void SetImageList(wxImageList
*imageList
);
115 void SetStateImageList(wxImageList
*imageList
);
116 void AssignImageList(wxImageList
*imageList
);
117 void AssignStateImageList(wxImageList
*imageList
);
119 // Functions to work with tree ctrl items.
124 // retrieve items label
125 wxString
GetItemText(const wxTreeItemId
& item
) const;
126 // get one of the images associated with the item (normal by default)
127 int GetItemImage(const wxTreeItemId
& item
,
128 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
129 // get the data associated with the item
130 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
136 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
137 // get one of the images associated with the item (normal by default)
138 void SetItemImage(const wxTreeItemId
& item
, int image
,
139 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
140 // associate some data with the item
141 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
143 // force appearance of [+] button near the item. This is useful to
144 // allow the user to expand the items which don't have any children now
145 // - but instead add them only when needed, thus minimizing memory
146 // usage and loading time.
147 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
149 // the item will be shown in bold
150 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
152 // set the items text colour
153 void SetItemTextColour(const wxTreeItemId
& item
, const wxColour
& col
);
155 // set the items background colour
156 void SetItemBackgroundColour(const wxTreeItemId
& item
, const wxColour
& col
);
158 // set the items font (should be of the same height for all items)
159 void SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
);
161 // set the window font
162 virtual bool SetFont( const wxFont
&font
);
164 // item status inquiries
165 // ---------------------
167 // is the item visible (it might be outside the view or not expanded)?
168 bool IsVisible(const wxTreeItemId
& item
) const;
169 // does the item has any children?
170 bool HasChildren(const wxTreeItemId
& item
) const
171 { return ItemHasChildren(item
); }
172 bool ItemHasChildren(const wxTreeItemId
& item
) const;
173 // is the item expanded (only makes sense if HasChildren())?
174 bool IsExpanded(const wxTreeItemId
& item
) const;
175 // is this item currently selected (the same as has focus)?
176 bool IsSelected(const wxTreeItemId
& item
) const;
177 // is item text in bold font?
178 bool IsBold(const wxTreeItemId
& item
) const;
180 // number of children
181 // ------------------
183 // if 'recursively' is FALSE, only immediate children count, otherwise
184 // the returned number is the number of all items in this branch
185 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
190 // wxTreeItemId.IsOk() will return FALSE if there is no such item
192 // get the root tree item
193 wxTreeItemId
GetRootItem() const { return m_anchor
; }
195 // get the item currently selected (may return NULL if no selection)
196 wxTreeItemId
GetSelection() const { return m_current
; }
198 // get the items currently selected, return the number of such item
199 size_t GetSelections(wxArrayTreeItemIds
&) const;
201 // get the parent of this item (may return NULL if root)
202 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
204 // for this enumeration function you must pass in a "cookie" parameter
205 // which is opaque for the application but is necessary for the library
206 // to make these functions reentrant (i.e. allow more than one
207 // enumeration on one and the same object simultaneously). Of course,
208 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
211 // get the first child of this item
212 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const;
213 // get the next child
214 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& cookie
) const;
215 // get the last child of this item - this method doesn't use cookies
216 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
218 // get the next sibling of this item
219 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
220 // get the previous sibling
221 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
223 // get first visible item
224 wxTreeItemId
GetFirstVisibleItem() const;
225 // get the next visible item: item must be visible itself!
226 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
227 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
228 // get the previous visible item: item must be visible itself!
229 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
231 // Only for internal use right now, but should probably be public
232 wxTreeItemId
GetNext(const wxTreeItemId
& item
) const;
233 wxTreeItemId
GetPrev(const wxTreeItemId
& item
) const;
238 // add the root node to the tree
239 wxTreeItemId
AddRoot(const wxString
& text
,
240 int image
= -1, int selectedImage
= -1,
241 wxTreeItemData
*data
= NULL
);
243 // insert a new item in as the first child of the parent
244 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
245 const wxString
& text
,
246 int image
= -1, int selectedImage
= -1,
247 wxTreeItemData
*data
= NULL
);
249 // insert a new item after a given one
250 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
251 const wxTreeItemId
& idPrevious
,
252 const wxString
& text
,
253 int image
= -1, int selectedImage
= -1,
254 wxTreeItemData
*data
= NULL
);
256 // insert a new item before the one with the given index
257 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
259 const wxString
& text
,
260 int image
= -1, int selectedImage
= -1,
261 wxTreeItemData
*data
= NULL
);
263 // insert a new item in as the last child of the parent
264 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
265 const wxString
& text
,
266 int image
= -1, int selectedImage
= -1,
267 wxTreeItemData
*data
= NULL
);
269 // delete this item and associated data if any
270 void Delete(const wxTreeItemId
& item
);
271 // delete all children (but don't delete the item itself)
272 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
273 void DeleteChildren(const wxTreeItemId
& item
);
274 // delete all items from the tree
275 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
276 void DeleteAllItems();
279 void Expand(const wxTreeItemId
& item
);
280 // expand this item and all subitems recursively
281 void ExpandAll(const wxTreeItemId
& item
);
282 // collapse the item without removing its children
283 void Collapse(const wxTreeItemId
& item
);
284 // collapse the item and remove all children
285 void CollapseAndReset(const wxTreeItemId
& item
);
286 // toggles the current state
287 void Toggle(const wxTreeItemId
& item
);
289 // remove the selection from currently selected item (if any)
293 void SelectItem(const wxTreeItemId
& item
, bool unselect_others
=TRUE
, bool extended_select
=FALSE
);
294 // make sure this item is visible (expanding the parent item and/or
295 // scrolling to this item if necessary)
296 void EnsureVisible(const wxTreeItemId
& item
);
297 // scroll to this item (but don't expand its parent)
298 void ScrollTo(const wxTreeItemId
& item
);
300 // The first function is more portable (because easier to implement
301 // on other platforms), but the second one returns some extra info.
302 wxTreeItemId
HitTest(const wxPoint
& point
)
303 { int dummy
; return HitTest(point
, dummy
); }
304 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
306 // get the bounding rectangle of the item (or of its label only)
307 bool GetBoundingRect(const wxTreeItemId
& item
,
309 bool textOnly
= FALSE
) const;
311 // Start editing the item label: this (temporarily) replaces the item
312 // with a one line edit control. The item will be selected if it hadn't
314 void EditLabel( const wxTreeItemId
& item
) { Edit( item
); }
315 void Edit( const wxTreeItemId
& item
);
318 // this function is called to compare 2 items and should return -1, 0
319 // or +1 if the first item is less than, equal to or greater than the
320 // second one. The base class version performs alphabetic comparaison
321 // of item labels (GetText)
322 virtual int OnCompareItems(const wxTreeItemId
& item1
,
323 const wxTreeItemId
& item2
);
324 // sort the children of this item using OnCompareItems
326 // NB: this function is not reentrant and not MT-safe (FIXME)!
327 void SortChildren(const wxTreeItemId
& item
);
329 // deprecated functions: use Set/GetItemImage directly
330 // get the selected item image
331 int GetItemSelectedImage(const wxTreeItemId
& item
) const
332 { return GetItemImage(item
, wxTreeItemIcon_Selected
); }
333 // set the selected item image
334 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
335 { SetItemImage(item
, image
, wxTreeItemIcon_Selected
); }
337 // implementation only from now on
340 void OnPaint( wxPaintEvent
&event
);
341 void OnSetFocus( wxFocusEvent
&event
);
342 void OnKillFocus( wxFocusEvent
&event
);
343 void OnChar( wxKeyEvent
&event
);
344 void OnMouse( wxMouseEvent
&event
);
345 void OnIdle( wxIdleEvent
&event
);
347 // implementation helpers
348 void SendDeleteEvent(wxGenericTreeItem
*itemBeingDeleted
);
350 void DrawBorder(const wxTreeItemId
& item
);
351 void DrawLine(const wxTreeItemId
& item
, bool below
);
354 friend class wxGenericTreeItem
;
355 friend class wxTreeRenameTimer
;
356 friend class wxTreeTextCtrl
;
361 wxGenericTreeItem
*m_anchor
;
362 wxGenericTreeItem
*m_current
, *m_key_current
, *m_currentEdit
;
365 int m_xScroll
,m_yScroll
;
366 unsigned int m_indent
;
367 unsigned int m_spacing
;
370 wxBrush
*m_hilightBrush
;
371 wxImageList
*m_imageListNormal
,
373 bool m_ownsImageListNormal
,
374 m_ownsImageListState
;
378 bool m_isDragging
; // true between BEGIN/END drag events
379 wxGenericTreeItem
*m_dropTarget
;
380 wxCursor m_oldCursor
; // cursor is changed while dragging
381 wxGenericTreeItem
*m_oldSelection
;
383 wxTimer
*m_renameTimer
;
385 bool m_lastOnSame
; // last click on the same item as prev
386 wxString m_renameRes
;
388 // the common part of all ctors
392 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
394 const wxString
& text
,
395 int image
, int selectedImage
,
396 wxTreeItemData
*data
);
399 void AdjustMyScrollbars();
401 int GetLineHeight(wxGenericTreeItem
*item
) const;
402 void PaintLevel( wxGenericTreeItem
*item
, wxDC
& dc
, int level
, int &y
);
403 void PaintItem( wxGenericTreeItem
*item
, wxDC
& dc
);
405 void CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
);
406 void CalculatePositions();
407 void CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
);
409 void RefreshSubtree( wxGenericTreeItem
*item
);
410 void RefreshLine( wxGenericTreeItem
*item
);
412 void OnRenameTimer();
413 void OnRenameAccept();
415 void FillArray(wxGenericTreeItem
*, wxArrayTreeItemIds
&) const;
416 void SelectItemRange( wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
);
417 bool TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
418 bool TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
419 void UnselectAllChildren( wxGenericTreeItem
*item
);
421 void DrawDropEffect(wxGenericTreeItem
*item
);
424 DECLARE_EVENT_TABLE()
425 DECLARE_DYNAMIC_CLASS(wxGenericTreeCtrl
)
428 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
430 * wxTreeCtrl has to be a real class or we have problems with
431 * the run-time information.
434 class WXDLLEXPORT wxTreeCtrl
: public wxGenericTreeCtrl
437 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
441 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
442 const wxPoint
& pos
= wxDefaultPosition
,
443 const wxSize
& size
= wxDefaultSize
,
444 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
445 const wxValidator
&validator
= wxDefaultValidator
,
446 const wxString
& name
= wxTreeCtrlNameStr
)
448 Create(parent
, id
, pos
, size
, style
, validator
, name
);
455 #endif // wxUSE_TREECTRL
457 #endif // _GENERIC_TREECTRL_H_