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 // ----------------------------------------------------------------------------
20 #pragma interface "treectrl.h"
25 #include "wx/textctrl.h"
26 #include "wx/dynarray.h"
27 #include "wx/treebase.h"
28 #include "wx/hashmap.h"
30 // the type for "untyped" data
31 typedef long wxDataType
;
34 class WXDLLEXPORT wxImageList
;
35 class WXDLLEXPORT wxDragImage
;
36 struct WXDLLEXPORT wxTreeViewItem
;
38 // a callback function used for sorting tree items, it should return -1 if the
39 // first item precedes the second, +1 if the second precedes the first or 0 if
43 // flags for deprecated `Expand(int action)'
47 wxTREE_EXPAND_COLLAPSE
,
48 wxTREE_EXPAND_COLLAPSE_RESET
,
52 // flags for deprecated InsertItem() variant
53 #define wxTREE_INSERT_FIRST 0xFFFF0001
54 #define wxTREE_INSERT_LAST 0xFFFF0002
56 // hash storing attributes for our items
57 WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr
*, wxMapTreeAttr
);
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
62 class WXDLLEXPORT wxTreeCtrl
: public wxControl
67 wxTreeCtrl() { Init(); }
69 wxTreeCtrl( wxWindow
* pParent
71 ,const wxPoint
& rPos
= wxDefaultPosition
72 ,const wxSize
& rSize
= wxDefaultSize
73 ,long lStyle
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
74 ,const wxValidator
& rValidator
= wxDefaultValidator
75 ,const wxString
& rsName
= wxTreeCtrlNameStr
87 virtual ~wxTreeCtrl();
89 bool Create( wxWindow
* pParent
91 ,const wxPoint
& rPos
= wxDefaultPosition
92 ,const wxSize
& rSize
= wxDefaultSize
93 ,long lStyle
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
94 ,const wxValidator
& rValidator
= wxDefaultValidator
95 ,const wxString
& rsName
= wxTreeCtrlNameStr
104 // Get the total number of items in the control
106 size_t GetCount(void) const;
109 // Indent is the number of pixels the children are indented relative to
110 // the parents position. SetIndent() also redraws the control
113 unsigned int GetIndent(void) const;
114 void SetIndent(unsigned int uIndent
);
117 // Spacing is the number of pixels between the start and the Text
119 unsigned int GetSpacing(void) const { return 18; } // return wxGTK default
120 void SetSpacing(unsigned int uSpacing
) { }
123 // Image list: these functions allow to associate an image list with
124 // the control and retrieve it. Note that the control does _not_ delete
125 // the associated image list when it's deleted in order to allow image
126 // lists to be shared between different controls.
128 // OS/2 doesn't really use imagelists as MSW does, but since the MSW
129 // control is the basis for this one, until I decide how to get rid of
130 // the need for them they are here for now.
132 wxImageList
* GetImageList(void) const;
133 wxImageList
* GetStateImageList(void) const;
135 void AssignImageList(wxImageList
* pImageList
);
136 void AssignStateImageList(wxImageList
* pImageList
);
137 void SetImageList(wxImageList
* pImageList
);
138 void SetStateImageList(wxImageList
* pImageList
);
141 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
142 // member functions of wxTreeItem because they must know the tree the item
143 // belongs to for Windows implementation and storing the pointer to
144 // wxTreeCtrl in each wxTreeItem is just too much waste.
149 wxString
GetItemText(const wxTreeItemId
& rItem
) const;
150 void SetItemText( const wxTreeItemId
& rItem
151 ,const wxString
& rsText
155 // One of the images associated with the item (normal by default)
157 int GetItemImage( const wxTreeItemId
& rItem
158 ,wxTreeItemIcon vWhich
= wxTreeItemIcon_Normal
160 void SetItemImage( const wxTreeItemId
& rItem
162 ,wxTreeItemIcon vWhich
= wxTreeItemIcon_Normal
166 // Data associated with the item
168 wxTreeItemData
* GetItemData(const wxTreeItemId
& rItem
) const;
169 void SetItemData( const wxTreeItemId
& rItem
170 ,wxTreeItemData
* pData
174 // Item's text colour
176 wxColour
GetItemTextColour(const wxTreeItemId
& rItem
) const;
177 void SetItemTextColour( const wxTreeItemId
& rItem
178 ,const wxColour
& rColor
182 // Item's background colour
184 wxColour
GetItemBackgroundColour(const wxTreeItemId
& rItem
) const;
185 void SetItemBackgroundColour( const wxTreeItemId
& rItem
186 ,const wxColour
& rColour
192 wxFont
GetItemFont(const wxTreeItemId
& rItem
) const;
193 void SetItemFont( const wxTreeItemId
& rItem
198 // Force appearance of [+] button near the item. This is useful to
199 // allow the user to expand the items which don't have any children now
200 // - but instead add them only when needed, thus minimizing memory
201 // usage and loading time.
203 void SetItemHasChildren( const wxTreeItemId
& rItem
208 // The item will be shown in bold
210 void SetItemBold( const wxTreeItemId
& rItem
215 // The item will be shown with a drop highlight
217 void SetItemDropHighlight( const wxTreeItemId
& rItem
218 ,bool bHighlight
= true
222 // Item status inquiries
223 // ---------------------
227 // Is the item visible (it might be outside the view or not expanded)?
229 bool IsVisible(const wxTreeItemId
& rItem
) const;
232 // Does the item has any children?
234 bool ItemHasChildren(const wxTreeItemId
& rItem
) const;
237 // Is the item expanded (only makes sense if HasChildren())?
239 bool IsExpanded(const wxTreeItemId
& rItem
) const;
242 // Is this item currently selected (the same as has focus)?
244 bool IsSelected(const wxTreeItemId
& rItem
) const;
247 // Is item text in bold font?
249 bool IsBold(const wxTreeItemId
& rItem
) const;
252 // Number of children
253 // ------------------
257 // If 'bRecursively' is false, only immediate children count, otherwise
258 // the returned number is the number of all items in this branch
260 size_t GetChildrenCount( const wxTreeItemId
& rItem
261 ,bool bRecursively
= true
270 // Get the root tree item
272 wxTreeItemId
GetRootItem(void) const;
275 // Get the item currently selected (may return NULL if no selection)
277 wxTreeItemId
GetSelection(void) const;
280 // Get the items currently selected, return the number of such item
282 size_t GetSelections(wxArrayTreeItemIds
& rSelections
) const;
285 // Get the parent of this item (may return NULL if root)
287 wxTreeItemId
GetItemParent(const wxTreeItemId
& rItem
) const;
289 #if WXWIN_COMPATIBILITY_2_2
290 // deprecated: Use GetItemParent instead.
291 wxDEPRECATED( wxTreeItemId
GetParent(const wxTreeItemId
& item
) const);
293 // Expose the base class method hidden by the one above. Not deprecatable.
294 wxWindow
*GetParent() const { return wxControl::GetParent(); }
295 #endif // WXWIN_COMPATIBILITY_2_2
297 // for this enumeration function you must pass in a "cookie" parameter
298 // which is opaque for the application but is necessary for the library
299 // to make these functions reentrant (i.e. allow more than one
300 // enumeration on one and the same object simultaneously). Of course,
301 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
304 // get the first child of this item
305 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
306 wxTreeItemIdValue
& cookie
) const;
307 // get the next child
308 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
309 wxTreeItemIdValue
& cookie
) const;
312 // Get the last child of this item - this method doesn't use cookies
314 wxTreeItemId
GetLastChild(const wxTreeItemId
& rItem
) const;
317 // Get the next sibling of this item
319 wxTreeItemId
GetNextSibling(const wxTreeItemId
& rItem
) const;
322 // Get the previous sibling
324 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& rItem
) const;
327 // Get first visible item
329 wxTreeItemId
GetFirstVisibleItem(void) const;
332 // Get the next visible item: item must be visible itself!
333 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
335 wxTreeItemId
GetNextVisible(const wxTreeItemId
& rItem
) const;
338 // Get the previous visible item: item must be visible itself!
340 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& rItem
) const;
348 // Add the root node to the tree
350 wxTreeItemId
AddRoot( const wxString
& rsText
352 ,int nSelectedImage
= -1
353 ,wxTreeItemData
* pData
= NULL
357 // Insert a new item in as the first child of the parent
359 wxTreeItemId
PrependItem( const wxTreeItemId
& rParent
360 ,const wxString
& rsText
362 ,int nSelectedImage
= -1
363 ,wxTreeItemData
* pData
= NULL
367 // Insert a new item after a given one
369 wxTreeItemId
InsertItem( const wxTreeItemId
& rParent
370 ,const wxTreeItemId
& rIdPrevious
371 ,const wxString
& rsText
373 ,int nSelectedImage
= -1
374 ,wxTreeItemData
* pData
= NULL
378 // Insert a new item before the one with the given index
380 wxTreeItemId
InsertItem( const wxTreeItemId
& pParent
382 ,const wxString
& rsText
384 ,int nSelectedImage
= -1
385 ,wxTreeItemData
* pData
= NULL
389 // Insert a new item in as the last child of the parent
391 wxTreeItemId
AppendItem( const wxTreeItemId
& rParent
392 ,const wxString
& rsText
394 ,int nSelectedImage
= -1
395 ,wxTreeItemData
* pData
= NULL
399 // Delete this item and associated data if any
401 void Delete(const wxTreeItemId
& rItem
);
404 // Delete all children (but don't delete the item itself)
406 void DeleteChildren(const wxTreeItemId
& rItem
);
409 // Delete all items from the tree
411 void DeleteAllItems(void);
416 void Expand(const wxTreeItemId
& rItem
);
419 // Collapse the item without removing its children
421 void Collapse(const wxTreeItemId
& rItem
);
424 // Collapse the item and remove all children
426 void CollapseAndReset(const wxTreeItemId
& rItem
);
429 // Toggles the current state
431 void Toggle(const wxTreeItemId
& rItem
);
434 // Remove the selection from currently selected item (if any)
439 // Unselect all items (only makes sense for multiple selection control)
441 void UnselectAll(void);
446 void SelectItem(const wxTreeItemId
& rItem
);
449 // Make sure this item is visible (expanding the parent item and/or
450 // scrolling to this item if necessary)
452 void EnsureVisible(const wxTreeItemId
& rItem
);
455 // Scroll to this item (but don't expand its parent)
457 void ScrollTo(const wxTreeItemId
& rItem
);
460 // OS/2 does not use a separate edit field for editting text. Here for
461 // interface compatibility, only.
463 wxTextCtrl
* EditLabel( const wxTreeItemId
& rItem
464 ,wxClassInfo
* pTextCtrlClass
= CLASSINFO(wxTextCtrl
)
468 // returns NULL for OS/2 in ALL cases
470 wxTextCtrl
* GetEditControl(void) const {return (wxTextCtrl
*)NULL
;}
473 // End editing and accept or discard the changes to item label
475 void EndEditLabel( const wxTreeItemId
& rItem
476 ,bool bDiscardChanges
= false
485 // This function is called to compare 2 items and should return -1, 0
486 // or +1 if the first item is less than, equal to or greater than the
487 // second one. The base class version performs alphabetic comparaison
488 // of item labels (GetText)
490 virtual int OnCompareItems( const wxTreeItemId
& rItem1
491 ,const wxTreeItemId
& rItem2
495 // Sort the children of this item using OnCompareItems
497 void SortChildren(const wxTreeItemId
& rItem
);
505 // Determine to which item (if any) belongs the given point (the
506 // coordinates specified are relative to the client area of tree ctrl)
507 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
511 // The first function is more portable (because easier to implement
512 // on other platforms), but the second one returns some extra info.
514 wxTreeItemId
HitTest(const wxPoint
& rPoint
)
515 { int nDummy
= 0; return HitTest(rPoint
, nDummy
); }
516 wxTreeItemId
HitTest( const wxPoint
& rPoint
521 // Get the bounding rectangle of the item (or of its label only)
523 bool GetBoundingRect( const wxTreeItemId
& rItem
525 ,bool bTextOnly
= false
532 #if WXWIN_COMPATIBILITY_2_4
533 // These methods are deprecated and will be removed in future versions of
534 // wxWidgets, they're here for compatibility only, don't use them in new
535 // code (the comments indicate why these methods are now useless and how to
540 // Use Expand, Collapse, CollapseAndReset or Toggle
542 wxDEPRECATED( void ExpandItem( const wxTreeItemId
& rItem
547 // Use AddRoot, PrependItem or AppendItem
549 wxDEPRECATED( wxTreeItemId
InsertItem( const wxTreeItemId
& pParent
550 ,const wxString
& rsText
553 ,long lInsertAfter
= wxTREE_INSERT_LAST
557 // Use Set/GetImageList and Set/GetStateImageList
559 wxDEPRECATED( wxImageList
* GetImageList(int nVal
) const );
560 wxDEPRECATED( void SetImageList(wxImageList
* pImageList
, int nVal
) );
563 // Use Set/GetItemImage directly
565 wxDEPRECATED( int GetItemSelectedImage(const wxTreeItemId
& rItem
) const );
566 wxDEPRECATED( void SetItemSelectedImage(const wxTreeItemId
& rItem
, int nImage
) );
569 // For this enumeration function you must pass in a "cookie" parameter
570 // which is opaque for the application but is necessary for the library
571 // to make these functions reentrant (i.e. allow more than one
572 // enumeration on one and the same object simultaneously). Of course,
573 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
578 // Get the first child of this item
580 wxDEPRECATED( wxTreeItemId
GetFirstChild( const wxTreeItemId
& rItem
585 // Get the next child
587 wxDEPRECATED( wxTreeItemId
GetNextChild( const wxTreeItemId
& rItem
590 #endif // WXWIN_COMPATIBILITY_2_4
597 virtual MRESULT
OS2WindowProc( WXUINT uMsg
601 virtual bool OS2Command( WXUINT uParam
604 // virtual bool OMSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
607 // Override some base class virtuals
609 virtual bool SetBackgroundColour(const wxColour
& rColour
);
610 virtual bool SetForegroundColour(const wxColour
& rColour
);
613 // Get/set the check state for the item (only for wxTR_MULTIPLE)
615 bool IsItemChecked(const wxTreeItemId
& rItem
) const;
616 void SetItemCheck( const wxTreeItemId
& rItem
622 // SetImageList helper
624 void SetAnyImageList( wxImageList
* pImageList
629 // Refresh a single item
631 void RefreshItem(const wxTreeItemId
& rItem
);
633 wxImageList
* m_pImageListNormal
; // images for tree elements
634 wxImageList
* m_pImageListState
; // special images for app defined states
635 bool m_bOwnsImageListNormal
;
636 bool m_bOwnsImageListState
;
641 // The common part of all ctors
648 inline bool DoGetItem(wxTreeViewItem
* pTvItem
) const;
649 inline void DoSetItem(wxTreeViewItem
* pTvItem
);
651 inline void DoExpand( const wxTreeItemId
& rItem
654 wxTreeItemId
DoInsertItem( const wxTreeItemId
& pParent
655 ,wxTreeItemId hInsertAfter
656 ,const wxString
& rsText
659 ,wxTreeItemData
* pData
661 int DoGetItemImageFromData( const wxTreeItemId
& rItem
662 ,wxTreeItemIcon vWhich
664 void DoSetItemImageFromData( const wxTreeItemId
& rItem
666 ,wxTreeItemIcon vWhich
668 void DoSetItemImages( const wxTreeItemId
& rItem
672 void DeleteTextCtrl() { };
675 // support for additional item images which we implement using
676 // wxTreeItemIndirectData technique - see the comments in msw/treectrl.cpp
678 void SetIndirectItemData( const wxTreeItemId
& rItem
679 ,class wxTreeItemIndirectData
* pData
681 bool HasIndirectData(const wxTreeItemId
& rItem
) const;
682 bool IsDataIndirect(wxTreeItemData
* pData
) const
683 { return pData
&& pData
->GetId().m_pItem
== 0; }
686 // The hash storing the items attributes (indexed by items ids)
688 wxMapTreeAttr m_vAttrs
;
691 // true if the hash above is not empty
698 wxDragImage
* m_pDragImage
;
700 // Virtual root item, if wxTR_HIDE_ROOT is set.
701 // void* m_pVirtualRoot;
703 // the starting item for selection with Shift
704 // WXHTREEITEM m_htSelStart;
706 friend class wxTreeItemIndirectData
;
707 friend class wxTreeSortHelper
;
709 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
710 DECLARE_NO_COPY_CLASS(wxTreeCtrl
)
711 }; // end of CLASS wxTreeCtrl
713 #endif // wxUSE_TREECTRL