1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/os2/treectrl.h
3 // Purpose: wxTreeCtrl class
4 // Author: David Webster
7 // Copyright: (c) David Webster
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_TREECTRL_H_
12 #define _WX_TREECTRL_H_
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
20 #include "wx/textctrl.h"
21 #include "wx/dynarray.h"
22 #include "wx/treebase.h"
23 #include "wx/hashmap.h"
25 // the type for "untyped" data
26 typedef long wxDataType
;
29 class WXDLLIMPEXP_CORE wxImageList
;
30 class WXDLLIMPEXP_CORE wxDragImage
;
31 struct WXDLLIMPEXP_FWD_CORE wxTreeViewItem
;
33 // a callback function used for sorting tree items, it should return -1 if the
34 // first item precedes the second, +1 if the second precedes the first or 0 if
38 #if WXWIN_COMPATIBILITY_2_6
39 // flags for deprecated InsertItem() variant
40 #define wxTREE_INSERT_FIRST 0xFFFF0001
41 #define wxTREE_INSERT_LAST 0xFFFF0002
44 // hash storing attributes for our items
45 WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr
*, wxMapTreeAttr
);
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
50 class WXDLLIMPEXP_CORE wxTreeCtrl
: public wxControl
55 wxTreeCtrl() { Init(); }
57 wxTreeCtrl( wxWindow
* pParent
58 ,wxWindowID vId
= wxID_ANY
59 ,const wxPoint
& rPos
= wxDefaultPosition
60 ,const wxSize
& rSize
= wxDefaultSize
61 ,long lStyle
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
62 ,const wxValidator
& rValidator
= wxDefaultValidator
63 ,const wxString
& rsName
= wxTreeCtrlNameStr
75 virtual ~wxTreeCtrl();
77 bool Create( wxWindow
* pParent
78 ,wxWindowID vId
= wxID_ANY
79 ,const wxPoint
& rPos
= wxDefaultPosition
80 ,const wxSize
& rSize
= wxDefaultSize
81 ,long lStyle
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
82 ,const wxValidator
& rValidator
= wxDefaultValidator
83 ,const wxString
& rsName
= wxTreeCtrlNameStr
92 // Get the total number of items in the control
94 virtual unsigned int GetCount(void) const;
97 // Indent is the number of pixels the children are indented relative to
98 // the parents position. SetIndent() also redraws the control
101 unsigned int GetIndent(void) const;
102 void SetIndent(unsigned int uIndent
);
105 // Spacing is the number of pixels between the start and the Text
107 unsigned int GetSpacing(void) const { return 18; } // return wxGTK default
108 void SetSpacing(unsigned int uSpacing
) { }
111 // Image list: these functions allow to associate an image list with
112 // the control and retrieve it. Note that the control does _not_ delete
113 // the associated image list when it's deleted in order to allow image
114 // lists to be shared between different controls.
116 // OS/2 doesn't really use imagelists as MSW does, but since the MSW
117 // control is the basis for this one, until I decide how to get rid of
118 // the need for them they are here for now.
120 wxImageList
* GetImageList(void) const;
121 wxImageList
* GetStateImageList(void) const;
123 void AssignImageList(wxImageList
* pImageList
);
124 void AssignStateImageList(wxImageList
* pImageList
);
125 void SetImageList(wxImageList
* pImageList
);
126 void SetStateImageList(wxImageList
* pImageList
);
129 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
130 // member functions of wxTreeItem because they must know the tree the item
131 // belongs to for Windows implementation and storing the pointer to
132 // wxTreeCtrl in each wxTreeItem is just too much waste.
137 wxString
GetItemText(const wxTreeItemId
& rItem
) const;
138 void SetItemText( const wxTreeItemId
& rItem
139 ,const wxString
& rsText
143 // One of the images associated with the item (normal by default)
145 int GetItemImage( const wxTreeItemId
& rItem
146 ,wxTreeItemIcon vWhich
= wxTreeItemIcon_Normal
148 void SetItemImage( const wxTreeItemId
& rItem
150 ,wxTreeItemIcon vWhich
= wxTreeItemIcon_Normal
154 // Data associated with the item
156 wxTreeItemData
* GetItemData(const wxTreeItemId
& rItem
) const;
157 void SetItemData( const wxTreeItemId
& rItem
158 ,wxTreeItemData
* pData
162 // Item's text colour
164 wxColour
GetItemTextColour(const wxTreeItemId
& rItem
) const;
165 void SetItemTextColour( const wxTreeItemId
& rItem
166 ,const wxColour
& rColor
170 // Item's background colour
172 wxColour
GetItemBackgroundColour(const wxTreeItemId
& rItem
) const;
173 void SetItemBackgroundColour( const wxTreeItemId
& rItem
174 ,const wxColour
& rColour
180 wxFont
GetItemFont(const wxTreeItemId
& rItem
) const;
181 void SetItemFont( const wxTreeItemId
& rItem
186 // Force appearance of [+] button near the item. This is useful to
187 // allow the user to expand the items which don't have any children now
188 // - but instead add them only when needed, thus minimizing memory
189 // usage and loading time.
191 void SetItemHasChildren( const wxTreeItemId
& rItem
196 // The item will be shown in bold
198 void SetItemBold( const wxTreeItemId
& rItem
203 // The item will be shown with a drop highlight
205 void SetItemDropHighlight( const wxTreeItemId
& rItem
206 ,bool bHighlight
= true
210 // Item status inquiries
211 // ---------------------
215 // Is the item visible (it might be outside the view or not expanded)?
217 bool IsVisible(const wxTreeItemId
& rItem
) const;
220 // Does the item has any children?
222 bool ItemHasChildren(const wxTreeItemId
& rItem
) const;
225 // Is the item expanded (only makes sense if HasChildren())?
227 bool IsExpanded(const wxTreeItemId
& rItem
) const;
230 // Is this item currently selected (the same as has focus)?
232 bool IsSelected(const wxTreeItemId
& rItem
) const;
235 // Is item text in bold font?
237 bool IsBold(const wxTreeItemId
& rItem
) const;
240 // Number of children
241 // ------------------
245 // If 'bRecursively' is false, only immediate children count, otherwise
246 // the returned number is the number of all items in this branch
248 size_t GetChildrenCount( const wxTreeItemId
& rItem
249 ,bool bRecursively
= true
258 // Get the root tree item
260 wxTreeItemId
GetRootItem(void) const;
263 // Get the item currently selected (may return NULL if no selection)
265 wxTreeItemId
GetSelection(void) const;
268 // Get the items currently selected, return the number of such item
270 size_t GetSelections(wxArrayTreeItemIds
& rSelections
) const;
273 // Get the parent of this item (may return NULL if root)
275 wxTreeItemId
GetItemParent(const wxTreeItemId
& rItem
) const;
277 // for this enumeration function you must pass in a "cookie" parameter
278 // which is opaque for the application but is necessary for the library
279 // to make these functions reentrant (i.e. allow more than one
280 // enumeration on one and the same object simultaneously). Of course,
281 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
284 // get the first child of this item
285 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
286 wxTreeItemIdValue
& cookie
) const;
287 // get the next child
288 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
289 wxTreeItemIdValue
& cookie
) const;
292 // Get the last child of this item - this method doesn't use cookies
294 wxTreeItemId
GetLastChild(const wxTreeItemId
& rItem
) const;
297 // Get the next sibling of this item
299 wxTreeItemId
GetNextSibling(const wxTreeItemId
& rItem
) const;
302 // Get the previous sibling
304 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& rItem
) const;
307 // Get first visible item
309 wxTreeItemId
GetFirstVisibleItem(void) const;
312 // Get the next visible item: item must be visible itself!
313 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
315 wxTreeItemId
GetNextVisible(const wxTreeItemId
& rItem
) const;
318 // Get the previous visible item: item must be visible itself!
320 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& rItem
) const;
328 // Add the root node to the tree
330 wxTreeItemId
AddRoot( const wxString
& rsText
332 ,int nSelectedImage
= -1
333 ,wxTreeItemData
* pData
= NULL
337 // Insert a new item in as the first child of the parent
339 wxTreeItemId
PrependItem( const wxTreeItemId
& rParent
340 ,const wxString
& rsText
342 ,int nSelectedImage
= -1
343 ,wxTreeItemData
* pData
= NULL
347 // Insert a new item after a given one
349 wxTreeItemId
InsertItem( const wxTreeItemId
& rParent
350 ,const wxTreeItemId
& rIdPrevious
351 ,const wxString
& rsText
353 ,int nSelectedImage
= -1
354 ,wxTreeItemData
* pData
= NULL
358 // Insert a new item before the one with the given index
360 wxTreeItemId
InsertItem( const wxTreeItemId
& pParent
362 ,const wxString
& rsText
364 ,int nSelectedImage
= -1
365 ,wxTreeItemData
* pData
= NULL
369 // Insert a new item in as the last child of the parent
371 wxTreeItemId
AppendItem( const wxTreeItemId
& rParent
372 ,const wxString
& rsText
374 ,int nSelectedImage
= -1
375 ,wxTreeItemData
* pData
= NULL
379 // Delete this item and associated data if any
381 void Delete(const wxTreeItemId
& rItem
);
384 // Delete all children (but don't delete the item itself)
386 void DeleteChildren(const wxTreeItemId
& rItem
);
389 // Delete all items from the tree
391 void DeleteAllItems(void);
396 void Expand(const wxTreeItemId
& rItem
);
399 // Collapse the item without removing its children
401 void Collapse(const wxTreeItemId
& rItem
);
404 // Collapse the item and remove all children
406 void CollapseAndReset(const wxTreeItemId
& rItem
);
409 // Toggles the current state
411 void Toggle(const wxTreeItemId
& rItem
);
414 // Remove the selection from currently selected item (if any)
419 // Unselect all items (only makes sense for multiple selection control)
421 void UnselectAll(void);
426 void SelectItem(const wxTreeItemId
& rItem
);
429 // Make sure this item is visible (expanding the parent item and/or
430 // scrolling to this item if necessary)
432 void EnsureVisible(const wxTreeItemId
& rItem
);
435 // Scroll to this item (but don't expand its parent)
437 void ScrollTo(const wxTreeItemId
& rItem
);
440 // OS/2 does not use a separate edit field for editting text. Here for
441 // interface compatibility, only.
443 wxTextCtrl
* EditLabel( const wxTreeItemId
& rItem
444 ,wxClassInfo
* pTextCtrlClass
= wxCLASSINFO(wxTextCtrl
)
448 // returns NULL for OS/2 in ALL cases
450 wxTextCtrl
* GetEditControl(void) const {return NULL
;}
453 // End editing and accept or discard the changes to item label
455 void EndEditLabel( const wxTreeItemId
& rItem
456 ,bool bDiscardChanges
= false
465 // This function is called to compare 2 items and should return -1, 0
466 // or +1 if the first item is less than, equal to or greater than the
467 // second one. The base class version performs alphabetic comparaison
468 // of item labels (GetText)
470 virtual int OnCompareItems( const wxTreeItemId
& rItem1
471 ,const wxTreeItemId
& rItem2
475 // Sort the children of this item using OnCompareItems
477 void SortChildren(const wxTreeItemId
& rItem
);
485 // Determine to which item (if any) belongs the given point (the
486 // coordinates specified are relative to the client area of tree ctrl)
487 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
491 // The first function is more portable (because easier to implement
492 // on other platforms), but the second one returns some extra info.
494 wxTreeItemId
HitTest(const wxPoint
& rPoint
)
495 { int nDummy
= 0; return HitTest(rPoint
, nDummy
); }
496 wxTreeItemId
HitTest( const wxPoint
& rPoint
501 // Get the bounding rectangle of the item (or of its label only)
503 bool GetBoundingRect( const wxTreeItemId
& rItem
505 ,bool bTextOnly
= false
513 virtual MRESULT
OS2WindowProc( WXUINT uMsg
517 virtual bool OS2Command( WXUINT uParam
520 // virtual bool OMSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
523 // Override some base class virtuals
525 virtual bool SetBackgroundColour(const wxColour
& rColour
);
526 virtual bool SetForegroundColour(const wxColour
& rColour
);
529 // Get/set the check state for the item (only for wxTR_MULTIPLE)
531 bool IsItemChecked(const wxTreeItemId
& rItem
) const;
532 void SetItemCheck( const wxTreeItemId
& rItem
538 // SetImageList helper
540 void SetAnyImageList( wxImageList
* pImageList
545 // Refresh a single item
547 void RefreshItem(const wxTreeItemId
& rItem
);
549 wxImageList
* m_pImageListNormal
; // images for tree elements
550 wxImageList
* m_pImageListState
; // special images for app defined states
551 bool m_bOwnsImageListNormal
;
552 bool m_bOwnsImageListState
;
557 // The common part of all ctors
564 inline bool DoGetItem(wxTreeViewItem
* pTvItem
) const;
565 inline void DoSetItem(wxTreeViewItem
* pTvItem
);
567 inline void DoExpand( const wxTreeItemId
& rItem
570 wxTreeItemId
DoInsertItem( const wxTreeItemId
& pParent
571 ,wxTreeItemId hInsertAfter
572 ,const wxString
& rsText
575 ,wxTreeItemData
* pData
577 int DoGetItemImageFromData( const wxTreeItemId
& rItem
578 ,wxTreeItemIcon vWhich
580 void DoSetItemImageFromData( const wxTreeItemId
& rItem
582 ,wxTreeItemIcon vWhich
584 void DoSetItemImages( const wxTreeItemId
& rItem
588 void DeleteTextCtrl() { }
591 // support for additional item images which we implement using
592 // wxTreeItemIndirectData technique - see the comments in msw/treectrl.cpp
594 void SetIndirectItemData( const wxTreeItemId
& rItem
595 ,class wxTreeItemIndirectData
* pData
597 bool HasIndirectData(const wxTreeItemId
& rItem
) const;
598 bool IsDataIndirect(wxTreeItemData
* pData
) const
599 { return pData
&& pData
->GetId().m_pItem
== 0; }
602 // The hash storing the items attributes (indexed by items ids)
604 wxMapTreeAttr m_vAttrs
;
607 // true if the hash above is not empty
614 wxDragImage
* m_pDragImage
;
616 // Virtual root item, if wxTR_HIDE_ROOT is set.
617 // void* m_pVirtualRoot;
619 // the starting item for selection with Shift
620 // WXHTREEITEM m_htSelStart;
622 friend class wxTreeItemIndirectData
;
623 friend class wxTreeSortHelper
;
625 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
626 wxDECLARE_NO_COPY_CLASS(wxTreeCtrl
);
627 }; // end of CLASS wxTreeCtrl
629 #endif // wxUSE_TREECTRL