1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/treectlg.h
3 // Purpose: wxTreeCtrl class
4 // Author: Robert Roebling
8 // Copyright: (c) 1997,1998 Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _GENERIC_TREECTRL_H_
13 #define _GENERIC_TREECTRL_H_
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "treectlg.h"
21 #include "wx/scrolwin.h"
23 #include "wx/imaglist.h"
25 // -----------------------------------------------------------------------------
26 // forward declaration
27 // -----------------------------------------------------------------------------
29 class WXDLLEXPORT wxGenericTreeItem
;
31 class WXDLLEXPORT wxTreeItemData
;
33 class WXDLLEXPORT wxTreeRenameTimer
;
34 class WXDLLEXPORT wxTreeFindTimer
;
35 class WXDLLEXPORT wxTreeTextCtrl
;
36 class WXDLLEXPORT wxTextCtrl
;
38 // -----------------------------------------------------------------------------
39 // wxGenericTreeCtrl - the tree control
40 // -----------------------------------------------------------------------------
42 class WXDLLEXPORT wxGenericTreeCtrl
: public wxScrolledWindow
47 wxGenericTreeCtrl() { Init(); }
49 wxGenericTreeCtrl(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
50 const wxPoint
& pos
= wxDefaultPosition
,
51 const wxSize
& size
= wxDefaultSize
,
52 long style
= wxTR_DEFAULT_STYLE
,
53 const wxValidator
&validator
= wxDefaultValidator
,
54 const wxString
& name
= wxTreeCtrlNameStr
)
57 Create(parent
, id
, pos
, size
, style
, validator
, name
);
60 virtual ~wxGenericTreeCtrl();
62 bool Create(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
63 const wxPoint
& pos
= wxDefaultPosition
,
64 const wxSize
& size
= wxDefaultSize
,
65 long style
= wxTR_DEFAULT_STYLE
,
66 const wxValidator
&validator
= wxDefaultValidator
,
67 const wxString
& name
= wxTreeCtrlNameStr
);
72 // get the total number of items in the control
73 size_t GetCount() const;
75 // indent is the number of pixels the children are indented relative to
76 // the parents position. SetIndent() also redraws the control
78 unsigned int GetIndent() const { return m_indent
; }
79 void SetIndent(unsigned int indent
);
81 // spacing is the number of pixels between the start and the Text
82 unsigned int GetSpacing() const { return m_spacing
; }
83 void SetSpacing(unsigned int spacing
);
85 // image list: these functions allow to associate an image list with
86 // the control and retrieve it. Note that when assigned with
87 // SetImageList, the control does _not_ delete
88 // the associated image list when it's deleted in order to allow image
89 // lists to be shared between different controls. If you use
90 // AssignImageList, the control _does_ delete the image list.
92 // The normal image list is for the icons which correspond to the
93 // normal tree item state (whether it is selected or not).
94 // Additionally, the application might choose to show a state icon
95 // which corresponds to an app-defined item state (for example,
96 // checked/unchecked) which are taken from the state image list.
97 virtual wxImageList
*GetImageList() const;
98 virtual wxImageList
*GetStateImageList() const;
99 virtual wxImageList
*GetButtonsImageList() const;
101 virtual void SetImageList(wxImageList
*imageList
);
102 virtual void SetStateImageList(wxImageList
*imageList
);
103 virtual void SetButtonsImageList(wxImageList
*imageList
);
104 virtual void AssignImageList(wxImageList
*imageList
);
105 virtual void AssignStateImageList(wxImageList
*imageList
);
106 virtual void AssignButtonsImageList(wxImageList
*imageList
);
108 // Functions to work with tree ctrl items.
113 // retrieve item's label
114 wxString
GetItemText(const wxTreeItemId
& item
) const;
115 // get one of the images associated with the item (normal by default)
116 virtual int GetItemImage(const wxTreeItemId
& item
,
117 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
118 // get the data associated with the item
119 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
121 // get the item's text colour
122 wxColour
GetItemTextColour(const wxTreeItemId
& item
) const;
124 // get the item's background colour
125 wxColour
GetItemBackgroundColour(const wxTreeItemId
& item
) const;
127 // get the item's font
128 wxFont
GetItemFont(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 virtual 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 item's text colour
151 void SetItemTextColour(const wxTreeItemId
& item
, const wxColour
& col
);
153 // set the item's background colour
154 void SetItemBackgroundColour(const wxTreeItemId
& item
, const wxColour
& col
);
156 // set the item's 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 // set the styles. No need to specify a GetWindowStyle here since
163 // the base wxWindow member function will do it for us
164 void SetWindowStyle(const long styles
);
166 // item status inquiries
167 // ---------------------
169 // is the item visible (it might be outside the view or not expanded)?
170 bool IsVisible(const wxTreeItemId
& item
) const;
171 // does the item has any children?
172 bool HasChildren(const wxTreeItemId
& item
) const
173 { return ItemHasChildren(item
); }
174 bool ItemHasChildren(const wxTreeItemId
& item
) const;
175 // is the item expanded (only makes sense if HasChildren())?
176 bool IsExpanded(const wxTreeItemId
& item
) const;
177 // is this item currently selected (the same as has focus)?
178 bool IsSelected(const wxTreeItemId
& item
) const;
179 // is item text in bold font?
180 bool IsBold(const wxTreeItemId
& item
) const;
181 // does the layout include space for a button?
183 // number of children
184 // ------------------
186 // if 'recursively' is false, only immediate children count, otherwise
187 // the returned number is the number of all items in this branch
188 size_t GetChildrenCount(const wxTreeItemId
& item
,
189 bool recursively
= true) const;
194 // wxTreeItemId.IsOk() will return false if there is no such item
196 // get the root tree item
197 wxTreeItemId
GetRootItem() const { return m_anchor
; }
199 // get the item currently selected (may return NULL if no selection)
200 wxTreeItemId
GetSelection() const { return m_current
; }
202 // get the items currently selected, return the number of such item
203 size_t GetSelections(wxArrayTreeItemIds
&) const;
205 // get the parent of this item (may return NULL if root)
206 wxTreeItemId
GetItemParent(const wxTreeItemId
& item
) const;
208 #if WXWIN_COMPATIBILITY_2_2
209 // deprecated: Use GetItemParent instead.
210 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const
211 { return GetItemParent( item
); }
213 // Expose the base class method hidden by the one above.
214 wxWindow
*GetParent() const { return wxScrolledWindow::GetParent(); }
215 #endif // WXWIN_COMPATIBILITY_2_2
217 // for this enumeration function you must pass in a "cookie" parameter
218 // which is opaque for the application but is necessary for the library
219 // to make these functions reentrant (i.e. allow more than one
220 // enumeration on one and the same object simultaneously). Of course,
221 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
224 // get the first child of this item
225 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
226 wxTreeItemIdValue
& cookie
) const;
227 // get the next child
228 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
229 wxTreeItemIdValue
& cookie
) const;
230 // get the last child of this item - this method doesn't use cookies
231 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
233 // get the next sibling of this item
234 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
235 // get the previous sibling
236 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
238 // get first visible item
239 wxTreeItemId
GetFirstVisibleItem() const;
240 // get the next visible item: item must be visible itself!
241 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
242 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
243 // get the previous visible item: item must be visible itself!
244 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
246 // Only for internal use right now, but should probably be public
247 wxTreeItemId
GetNext(const wxTreeItemId
& item
) const;
252 // add the root node to the tree
253 virtual wxTreeItemId
AddRoot(const wxString
& text
,
254 int image
= -1, int selectedImage
= -1,
255 wxTreeItemData
*data
= NULL
);
257 // insert a new item in as the first child of the parent
258 virtual wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
259 const wxString
& text
,
260 int image
= -1, int selectedImage
= -1,
261 wxTreeItemData
*data
= NULL
);
263 // insert a new item after a given one
264 virtual wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
265 const wxTreeItemId
& idPrevious
,
266 const wxString
& text
,
267 int image
= -1, int selectedImage
= -1,
268 wxTreeItemData
*data
= NULL
);
270 // insert a new item before the one with the given index
271 virtual wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
273 const wxString
& text
,
274 int image
= -1, int selectedImage
= -1,
275 wxTreeItemData
*data
= NULL
);
277 // insert a new item in as the last child of the parent
278 virtual wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
279 const wxString
& text
,
280 int image
= -1, int selectedImage
= -1,
281 wxTreeItemData
*data
= NULL
);
283 // delete this item and associated data if any
284 void Delete(const wxTreeItemId
& item
);
285 // delete all children (but don't delete the item itself)
286 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
287 void DeleteChildren(const wxTreeItemId
& item
);
288 // delete all items from the tree
289 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
290 void DeleteAllItems();
293 void Expand(const wxTreeItemId
& item
);
294 // expand this item and all subitems recursively
295 void ExpandAll(const wxTreeItemId
& item
);
296 // collapse the item without removing its children
297 void Collapse(const wxTreeItemId
& item
);
298 // collapse the item and remove all children
299 void CollapseAndReset(const wxTreeItemId
& item
);
300 // toggles the current state
301 void Toggle(const wxTreeItemId
& item
);
303 // remove the selection from currently selected item (if any)
305 // or from the given one (multiselect mode only)
306 void UnselectItem(const wxTreeItemId
& item
) { SelectItem(item
, false); }
310 void SelectItem(const wxTreeItemId
& item
, bool select
= true);
311 // toggle the item selection
312 void ToggleItemSelection(const wxTreeItemId
& item
)
314 SelectItem(item
, !IsSelected(item
));
317 // make sure this item is visible (expanding the parent item and/or
318 // scrolling to this item if necessary)
319 void EnsureVisible(const wxTreeItemId
& item
);
320 // scroll to this item (but don't expand its parent)
321 void ScrollTo(const wxTreeItemId
& item
);
322 void AdjustMyScrollbars();
324 // The first function is more portable (because easier to implement
325 // on other platforms), but the second one returns some extra info.
326 wxTreeItemId
HitTest(const wxPoint
& point
)
327 { int dummy
; return HitTest(point
, dummy
); }
328 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
330 // get the bounding rectangle of the item (or of its label only)
331 bool GetBoundingRect(const wxTreeItemId
& item
,
333 bool textOnly
= false) const;
335 // Start editing the item label: this (temporarily) replaces the item
336 // with a one line edit control. The item will be selected if it hadn't
338 void EditLabel( const wxTreeItemId
& item
) { Edit( item
); }
339 void Edit( const wxTreeItemId
& item
);
340 // returns a pointer to the text edit control if the item is being
341 // edited, NULL otherwise (it's assumed that no more than one item may
342 // be edited simultaneously)
343 wxTextCtrl
* GetEditControl() const;
346 // this function is called to compare 2 items and should return -1, 0
347 // or +1 if the first item is less than, equal to or greater than the
348 // second one. The base class version performs alphabetic comparaison
349 // of item labels (GetText)
350 virtual int OnCompareItems(const wxTreeItemId
& item1
,
351 const wxTreeItemId
& item2
);
352 // sort the children of this item using OnCompareItems
354 // NB: this function is not reentrant and not MT-safe (FIXME)!
355 void SortChildren(const wxTreeItemId
& item
);
357 #if WXWIN_COMPATIBILITY_2_4
358 // deprecated functions: use Set/GetItemImage directly
359 int GetItemSelectedImage(const wxTreeItemId
& item
) const
360 { return GetItemImage(item
, wxTreeItemIcon_Selected
); }
361 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
362 { SetItemImage(item
, image
, wxTreeItemIcon_Selected
); }
364 // use the versions taking wxTreeItemIdValue cookies (note that
365 // GetNextChild() is not inside wxDEPRECATED on purpose, as otherwise we
366 // get twice as many warnings without any added benefit: it is always used
367 // with GetFirstChild() anyhow)
368 wxDEPRECATED( wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
369 long& cookie
) const );
370 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
372 #endif // WXWIN_COMPATIBILITY_2_4
374 virtual bool ShouldInheritColours() const { return false; }
376 // implementation only from now on
378 // overridden base class virtuals
379 virtual bool SetBackgroundColour(const wxColour
& colour
);
380 virtual bool SetForegroundColour(const wxColour
& colour
);
382 virtual void Freeze();
386 void OnPaint( wxPaintEvent
&event
);
387 void OnSetFocus( wxFocusEvent
&event
);
388 void OnKillFocus( wxFocusEvent
&event
);
389 void OnChar( wxKeyEvent
&event
);
390 void OnMouse( wxMouseEvent
&event
);
391 void OnGetToolTip( wxTreeEvent
&event
);
392 void OnInternalIdle( );
394 virtual wxSize
DoGetBestSize() const;
396 virtual wxVisualAttributes
GetDefaultAttributes() const
398 return GetClassDefaultAttributes(GetWindowVariant());
401 static wxVisualAttributes
402 GetClassDefaultAttributes(wxWindowVariant variant
= wxWINDOW_VARIANT_NORMAL
);
404 // implementation helpers
406 friend class wxGenericTreeItem
;
407 friend class wxTreeRenameTimer
;
408 friend class wxTreeFindTimer
;
409 friend class wxTreeTextCtrl
;
414 wxGenericTreeItem
*m_anchor
;
415 wxGenericTreeItem
*m_current
,
417 // A hint to select a parent item after deleting a child
419 unsigned short m_indent
;
420 unsigned short m_spacing
;
423 wxBrush
*m_hilightBrush
,
424 *m_hilightUnfocusedBrush
;
427 bool m_ownsImageListNormal
,
428 m_ownsImageListState
,
429 m_ownsImageListButtons
;
430 bool m_isDragging
; // true between BEGIN/END drag events
431 bool m_lastOnSame
; // last click on the same item as prev
432 wxImageList
*m_imageListNormal
,
439 wxGenericTreeItem
*m_dropTarget
;
440 wxCursor m_oldCursor
; // cursor is changed while dragging
441 wxGenericTreeItem
*m_oldSelection
;
442 wxGenericTreeItem
*m_underMouse
; // for visual effects
443 wxTreeTextCtrl
*m_textCtrl
;
445 wxTimer
*m_renameTimer
;
447 // incremental search data
448 wxString m_findPrefix
;
449 wxTimer
*m_findTimer
;
451 // the common part of all ctors
455 void SendDeleteEvent(wxGenericTreeItem
*itemBeingDeleted
);
457 void DrawBorder(const wxTreeItemId
& item
);
458 void DrawLine(const wxTreeItemId
& item
, bool below
);
459 void DrawDropEffect(wxGenericTreeItem
*item
);
461 void DoSelectItem(const wxTreeItemId
& id
,
462 bool unselect_others
= true,
463 bool extended_select
= false);
465 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
467 const wxString
& text
,
468 int image
, int selectedImage
,
469 wxTreeItemData
*data
);
471 // called by wxTextTreeCtrl when it marks itself for deletion
472 void ResetTextControl();
474 // find the first item starting with the given prefix after the given item
475 wxTreeItemId
FindItem(const wxTreeItemId
& id
, const wxString
& prefix
) const;
477 bool HasButtons() const { return HasFlag(wxTR_HAS_BUTTONS
); }
479 void CalculateLineHeight();
480 int GetLineHeight(wxGenericTreeItem
*item
) const;
481 void PaintLevel( wxGenericTreeItem
*item
, wxDC
& dc
, int level
, int &y
);
482 void PaintItem( wxGenericTreeItem
*item
, wxDC
& dc
);
484 void CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
);
485 void CalculatePositions();
486 void CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
);
488 void RefreshSubtree( wxGenericTreeItem
*item
);
489 void RefreshLine( wxGenericTreeItem
*item
);
491 // redraw all selected items
492 void RefreshSelected();
494 // RefreshSelected() recursive helper
495 void RefreshSelectedUnder(wxGenericTreeItem
*item
);
497 void OnRenameTimer();
498 bool OnRenameAccept(wxGenericTreeItem
*item
, const wxString
& value
);
499 void OnRenameCancelled(wxGenericTreeItem
*item
);
501 void FillArray(wxGenericTreeItem
*, wxArrayTreeItemIds
&) const;
502 void SelectItemRange( wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
);
503 bool TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
504 bool TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
);
505 void UnselectAllChildren( wxGenericTreeItem
*item
);
506 void ChildrenClosing(wxGenericTreeItem
* item
);
509 DECLARE_EVENT_TABLE()
510 DECLARE_DYNAMIC_CLASS(wxGenericTreeCtrl
)
511 DECLARE_NO_COPY_CLASS(wxGenericTreeCtrl
)
514 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
516 * wxTreeCtrl has to be a real class or we have problems with
517 * the run-time information.
520 class WXDLLEXPORT wxTreeCtrl
: public wxGenericTreeCtrl
522 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
527 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
528 const wxPoint
& pos
= wxDefaultPosition
,
529 const wxSize
& size
= wxDefaultSize
,
530 long style
= wxTR_DEFAULT_STYLE
,
531 const wxValidator
&validator
= wxDefaultValidator
,
532 const wxString
& name
= wxTreeCtrlNameStr
)
533 : wxGenericTreeCtrl(parent
, id
, pos
, size
, style
, validator
, name
)
537 #endif // !__WXMSW__ || __WXUNIVERSAL__
539 #endif // wxUSE_TREECTRL
541 #endif // _GENERIC_TREECTRL_H_