1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/os2/treectrl.h
3 // Purpose: wxTreeCtrl class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_TREECTRL_H_
13 #define _WX_TREECTRL_H_
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
21 #include "wx/textctrl.h"
22 #include "wx/dynarray.h"
23 #include "wx/treebase.h"
24 #include "wx/hashmap.h"
26 // the type for "untyped" data
27 typedef long wxDataType
;
30 class WXDLLEXPORT wxImageList
;
31 class WXDLLEXPORT wxDragImage
;
32 struct WXDLLEXPORT wxTreeViewItem
;
34 // a callback function used for sorting tree items, it should return -1 if the
35 // first item precedes the second, +1 if the second precedes the first or 0 if
39 // flags for deprecated InsertItem() variant
40 #define wxTREE_INSERT_FIRST 0xFFFF0001
41 #define wxTREE_INSERT_LAST 0xFFFF0002
43 // hash storing attributes for our items
44 WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr
*, wxMapTreeAttr
);
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
49 class WXDLLEXPORT wxTreeCtrl
: public wxControl
54 wxTreeCtrl() { Init(); }
56 wxTreeCtrl( wxWindow
* pParent
57 ,wxWindowID vId
= wxID_ANY
58 ,const wxPoint
& rPos
= wxDefaultPosition
59 ,const wxSize
& rSize
= wxDefaultSize
60 ,long lStyle
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
61 ,const wxValidator
& rValidator
= wxDefaultValidator
62 ,const wxString
& rsName
= wxTreeCtrlNameStr
74 virtual ~wxTreeCtrl();
76 bool Create( wxWindow
* pParent
77 ,wxWindowID vId
= wxID_ANY
78 ,const wxPoint
& rPos
= wxDefaultPosition
79 ,const wxSize
& rSize
= wxDefaultSize
80 ,long lStyle
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
81 ,const wxValidator
& rValidator
= wxDefaultValidator
82 ,const wxString
& rsName
= wxTreeCtrlNameStr
91 // Get the total number of items in the control
93 size_t GetCount(void) const;
96 // Indent is the number of pixels the children are indented relative to
97 // the parents position. SetIndent() also redraws the control
100 unsigned int GetIndent(void) const;
101 void SetIndent(unsigned int uIndent
);
104 // Spacing is the number of pixels between the start and the Text
106 unsigned int GetSpacing(void) const { return 18; } // return wxGTK default
107 void SetSpacing(unsigned int uSpacing
) { }
110 // Image list: these functions allow to associate an image list with
111 // the control and retrieve it. Note that the control does _not_ delete
112 // the associated image list when it's deleted in order to allow image
113 // lists to be shared between different controls.
115 // OS/2 doesn't really use imagelists as MSW does, but since the MSW
116 // control is the basis for this one, until I decide how to get rid of
117 // the need for them they are here for now.
119 wxImageList
* GetImageList(void) const;
120 wxImageList
* GetStateImageList(void) const;
122 void AssignImageList(wxImageList
* pImageList
);
123 void AssignStateImageList(wxImageList
* pImageList
);
124 void SetImageList(wxImageList
* pImageList
);
125 void SetStateImageList(wxImageList
* pImageList
);
128 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
129 // member functions of wxTreeItem because they must know the tree the item
130 // belongs to for Windows implementation and storing the pointer to
131 // wxTreeCtrl in each wxTreeItem is just too much waste.
136 wxString
GetItemText(const wxTreeItemId
& rItem
) const;
137 void SetItemText( const wxTreeItemId
& rItem
138 ,const wxString
& rsText
142 // One of the images associated with the item (normal by default)
144 int GetItemImage( const wxTreeItemId
& rItem
145 ,wxTreeItemIcon vWhich
= wxTreeItemIcon_Normal
147 void SetItemImage( const wxTreeItemId
& rItem
149 ,wxTreeItemIcon vWhich
= wxTreeItemIcon_Normal
153 // Data associated with the item
155 wxTreeItemData
* GetItemData(const wxTreeItemId
& rItem
) const;
156 void SetItemData( const wxTreeItemId
& rItem
157 ,wxTreeItemData
* pData
161 // Item's text colour
163 wxColour
GetItemTextColour(const wxTreeItemId
& rItem
) const;
164 void SetItemTextColour( const wxTreeItemId
& rItem
165 ,const wxColour
& rColor
169 // Item's background colour
171 wxColour
GetItemBackgroundColour(const wxTreeItemId
& rItem
) const;
172 void SetItemBackgroundColour( const wxTreeItemId
& rItem
173 ,const wxColour
& rColour
179 wxFont
GetItemFont(const wxTreeItemId
& rItem
) const;
180 void SetItemFont( const wxTreeItemId
& rItem
185 // Force appearance of [+] button near the item. This is useful to
186 // allow the user to expand the items which don't have any children now
187 // - but instead add them only when needed, thus minimizing memory
188 // usage and loading time.
190 void SetItemHasChildren( const wxTreeItemId
& rItem
195 // The item will be shown in bold
197 void SetItemBold( const wxTreeItemId
& rItem
202 // The item will be shown with a drop highlight
204 void SetItemDropHighlight( const wxTreeItemId
& rItem
205 ,bool bHighlight
= true
209 // Item status inquiries
210 // ---------------------
214 // Is the item visible (it might be outside the view or not expanded)?
216 bool IsVisible(const wxTreeItemId
& rItem
) const;
219 // Does the item has any children?
221 bool ItemHasChildren(const wxTreeItemId
& rItem
) const;
224 // Is the item expanded (only makes sense if HasChildren())?
226 bool IsExpanded(const wxTreeItemId
& rItem
) const;
229 // Is this item currently selected (the same as has focus)?
231 bool IsSelected(const wxTreeItemId
& rItem
) const;
234 // Is item text in bold font?
236 bool IsBold(const wxTreeItemId
& rItem
) const;
239 // Number of children
240 // ------------------
244 // If 'bRecursively' is false, only immediate children count, otherwise
245 // the returned number is the number of all items in this branch
247 size_t GetChildrenCount( const wxTreeItemId
& rItem
248 ,bool bRecursively
= true
257 // Get the root tree item
259 wxTreeItemId
GetRootItem(void) const;
262 // Get the item currently selected (may return NULL if no selection)
264 wxTreeItemId
GetSelection(void) const;
267 // Get the items currently selected, return the number of such item
269 size_t GetSelections(wxArrayTreeItemIds
& rSelections
) const;
272 // Get the parent of this item (may return NULL if root)
274 wxTreeItemId
GetItemParent(const wxTreeItemId
& rItem
) const;
276 #if WXWIN_COMPATIBILITY_2_2
277 // deprecated: Use GetItemParent instead.
278 wxDEPRECATED( wxTreeItemId
GetParent(const wxTreeItemId
& item
) const);
280 // Expose the base class method hidden by the one above. Not deprecatable.
281 wxWindow
*GetParent() const { return wxControl::GetParent(); }
282 #endif // WXWIN_COMPATIBILITY_2_2
284 // for this enumeration function you must pass in a "cookie" parameter
285 // which is opaque for the application but is necessary for the library
286 // to make these functions reentrant (i.e. allow more than one
287 // enumeration on one and the same object simultaneously). Of course,
288 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
291 // get the first child of this item
292 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
293 wxTreeItemIdValue
& cookie
) const;
294 // get the next child
295 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
296 wxTreeItemIdValue
& cookie
) const;
299 // Get the last child of this item - this method doesn't use cookies
301 wxTreeItemId
GetLastChild(const wxTreeItemId
& rItem
) const;
304 // Get the next sibling of this item
306 wxTreeItemId
GetNextSibling(const wxTreeItemId
& rItem
) const;
309 // Get the previous sibling
311 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& rItem
) const;
314 // Get first visible item
316 wxTreeItemId
GetFirstVisibleItem(void) const;
319 // Get the next visible item: item must be visible itself!
320 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
322 wxTreeItemId
GetNextVisible(const wxTreeItemId
& rItem
) const;
325 // Get the previous visible item: item must be visible itself!
327 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& rItem
) const;
335 // Add the root node to the tree
337 wxTreeItemId
AddRoot( const wxString
& rsText
339 ,int nSelectedImage
= -1
340 ,wxTreeItemData
* pData
= NULL
344 // Insert a new item in as the first child of the parent
346 wxTreeItemId
PrependItem( const wxTreeItemId
& rParent
347 ,const wxString
& rsText
349 ,int nSelectedImage
= -1
350 ,wxTreeItemData
* pData
= NULL
354 // Insert a new item after a given one
356 wxTreeItemId
InsertItem( const wxTreeItemId
& rParent
357 ,const wxTreeItemId
& rIdPrevious
358 ,const wxString
& rsText
360 ,int nSelectedImage
= -1
361 ,wxTreeItemData
* pData
= NULL
365 // Insert a new item before the one with the given index
367 wxTreeItemId
InsertItem( const wxTreeItemId
& pParent
369 ,const wxString
& rsText
371 ,int nSelectedImage
= -1
372 ,wxTreeItemData
* pData
= NULL
376 // Insert a new item in as the last child of the parent
378 wxTreeItemId
AppendItem( const wxTreeItemId
& rParent
379 ,const wxString
& rsText
381 ,int nSelectedImage
= -1
382 ,wxTreeItemData
* pData
= NULL
386 // Delete this item and associated data if any
388 void Delete(const wxTreeItemId
& rItem
);
391 // Delete all children (but don't delete the item itself)
393 void DeleteChildren(const wxTreeItemId
& rItem
);
396 // Delete all items from the tree
398 void DeleteAllItems(void);
403 void Expand(const wxTreeItemId
& rItem
);
406 // Collapse the item without removing its children
408 void Collapse(const wxTreeItemId
& rItem
);
411 // Collapse the item and remove all children
413 void CollapseAndReset(const wxTreeItemId
& rItem
);
416 // Toggles the current state
418 void Toggle(const wxTreeItemId
& rItem
);
421 // Remove the selection from currently selected item (if any)
426 // Unselect all items (only makes sense for multiple selection control)
428 void UnselectAll(void);
433 void SelectItem(const wxTreeItemId
& rItem
);
436 // Make sure this item is visible (expanding the parent item and/or
437 // scrolling to this item if necessary)
439 void EnsureVisible(const wxTreeItemId
& rItem
);
442 // Scroll to this item (but don't expand its parent)
444 void ScrollTo(const wxTreeItemId
& rItem
);
447 // OS/2 does not use a separate edit field for editting text. Here for
448 // interface compatibility, only.
450 wxTextCtrl
* EditLabel( const wxTreeItemId
& rItem
451 ,wxClassInfo
* pTextCtrlClass
= CLASSINFO(wxTextCtrl
)
455 // returns NULL for OS/2 in ALL cases
457 wxTextCtrl
* GetEditControl(void) const {return (wxTextCtrl
*)NULL
;}
460 // End editing and accept or discard the changes to item label
462 void EndEditLabel( const wxTreeItemId
& rItem
463 ,bool bDiscardChanges
= false
472 // This function is called to compare 2 items and should return -1, 0
473 // or +1 if the first item is less than, equal to or greater than the
474 // second one. The base class version performs alphabetic comparaison
475 // of item labels (GetText)
477 virtual int OnCompareItems( const wxTreeItemId
& rItem1
478 ,const wxTreeItemId
& rItem2
482 // Sort the children of this item using OnCompareItems
484 void SortChildren(const wxTreeItemId
& rItem
);
492 // Determine to which item (if any) belongs the given point (the
493 // coordinates specified are relative to the client area of tree ctrl)
494 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
498 // The first function is more portable (because easier to implement
499 // on other platforms), but the second one returns some extra info.
501 wxTreeItemId
HitTest(const wxPoint
& rPoint
)
502 { int nDummy
= 0; return HitTest(rPoint
, nDummy
); }
503 wxTreeItemId
HitTest( const wxPoint
& rPoint
508 // Get the bounding rectangle of the item (or of its label only)
510 bool GetBoundingRect( const wxTreeItemId
& rItem
512 ,bool bTextOnly
= false
519 #if WXWIN_COMPATIBILITY_2_4
520 // These methods are deprecated and will be removed in future versions of
521 // wxWidgets, they're here for compatibility only, don't use them in new
522 // code (the comments indicate why these methods are now useless and how to
527 // Use Expand, Collapse, CollapseAndReset or Toggle
529 wxDEPRECATED( void ExpandItem( const wxTreeItemId
& rItem
534 // Use AddRoot, PrependItem or AppendItem
536 wxDEPRECATED( wxTreeItemId
InsertItem( const wxTreeItemId
& pParent
537 ,const wxString
& rsText
540 ,long lInsertAfter
= wxTREE_INSERT_LAST
544 // Use Set/GetImageList and Set/GetStateImageList
546 wxDEPRECATED( wxImageList
* GetImageList(int nVal
) const );
547 wxDEPRECATED( void SetImageList(wxImageList
* pImageList
, int nVal
) );
550 // Use Set/GetItemImage directly
552 wxDEPRECATED( int GetItemSelectedImage(const wxTreeItemId
& rItem
) const );
553 wxDEPRECATED( void SetItemSelectedImage(const wxTreeItemId
& rItem
, int nImage
) );
556 // For this enumeration function you must pass in a "cookie" parameter
557 // which is opaque for the application but is necessary for the library
558 // to make these functions reentrant (i.e. allow more than one
559 // enumeration on one and the same object simultaneously). Of course,
560 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
565 // Get the first child of this item
567 wxDEPRECATED( wxTreeItemId
GetFirstChild( const wxTreeItemId
& rItem
572 // Get the next child
574 wxDEPRECATED( wxTreeItemId
GetNextChild( const wxTreeItemId
& rItem
577 #endif // WXWIN_COMPATIBILITY_2_4
584 virtual MRESULT
OS2WindowProc( WXUINT uMsg
588 virtual bool OS2Command( WXUINT uParam
591 // virtual bool OMSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
594 // Override some base class virtuals
596 virtual bool SetBackgroundColour(const wxColour
& rColour
);
597 virtual bool SetForegroundColour(const wxColour
& rColour
);
600 // Get/set the check state for the item (only for wxTR_MULTIPLE)
602 bool IsItemChecked(const wxTreeItemId
& rItem
) const;
603 void SetItemCheck( const wxTreeItemId
& rItem
609 // SetImageList helper
611 void SetAnyImageList( wxImageList
* pImageList
616 // Refresh a single item
618 void RefreshItem(const wxTreeItemId
& rItem
);
620 wxImageList
* m_pImageListNormal
; // images for tree elements
621 wxImageList
* m_pImageListState
; // special images for app defined states
622 bool m_bOwnsImageListNormal
;
623 bool m_bOwnsImageListState
;
628 // The common part of all ctors
635 inline bool DoGetItem(wxTreeViewItem
* pTvItem
) const;
636 inline void DoSetItem(wxTreeViewItem
* pTvItem
);
638 inline void DoExpand( const wxTreeItemId
& rItem
641 wxTreeItemId
DoInsertItem( const wxTreeItemId
& pParent
642 ,wxTreeItemId hInsertAfter
643 ,const wxString
& rsText
646 ,wxTreeItemData
* pData
648 int DoGetItemImageFromData( const wxTreeItemId
& rItem
649 ,wxTreeItemIcon vWhich
651 void DoSetItemImageFromData( const wxTreeItemId
& rItem
653 ,wxTreeItemIcon vWhich
655 void DoSetItemImages( const wxTreeItemId
& rItem
659 void DeleteTextCtrl() { };
662 // support for additional item images which we implement using
663 // wxTreeItemIndirectData technique - see the comments in msw/treectrl.cpp
665 void SetIndirectItemData( const wxTreeItemId
& rItem
666 ,class wxTreeItemIndirectData
* pData
668 bool HasIndirectData(const wxTreeItemId
& rItem
) const;
669 bool IsDataIndirect(wxTreeItemData
* pData
) const
670 { return pData
&& pData
->GetId().m_pItem
== 0; }
673 // The hash storing the items attributes (indexed by items ids)
675 wxMapTreeAttr m_vAttrs
;
678 // true if the hash above is not empty
685 wxDragImage
* m_pDragImage
;
687 // Virtual root item, if wxTR_HIDE_ROOT is set.
688 // void* m_pVirtualRoot;
690 // the starting item for selection with Shift
691 // WXHTREEITEM m_htSelStart;
693 friend class wxTreeItemIndirectData
;
694 friend class wxTreeSortHelper
;
696 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
697 DECLARE_NO_COPY_CLASS(wxTreeCtrl
)
698 }; // end of CLASS wxTreeCtrl
700 #endif // wxUSE_TREECTRL