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"
29 // the type for "untyped" data
30 typedef long wxDataType
;
33 class WXDLLEXPORT wxImageList
;
34 class WXDLLEXPORT wxDragImage
;
35 struct WXDLLEXPORT wxTreeViewItem
;
37 // a callback function used for sorting tree items, it should return -1 if the
38 // first item precedes the second, +1 if the second precedes the first or 0 if
42 // flags for deprecated `Expand(int action)'
46 wxTREE_EXPAND_COLLAPSE
,
47 wxTREE_EXPAND_COLLAPSE_RESET
,
51 // flags for deprecated InsertItem() variant
52 #define wxTREE_INSERT_FIRST 0xFFFF0001
53 #define wxTREE_INSERT_LAST 0xFFFF0002
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
58 class WXDLLEXPORT wxTreeCtrl
: public wxControl
63 wxTreeCtrl() { Init(); }
65 wxTreeCtrl( wxWindow
* pParent
67 ,const wxPoint
& rPos
= wxDefaultPosition
68 ,const wxSize
& rSize
= wxDefaultSize
69 ,long lStyle
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
70 ,const wxValidator
& rValidator
= wxDefaultValidator
71 ,const wxString
& rsName
= wxTreeCtrlNameStr
83 virtual ~wxTreeCtrl();
85 bool Create( wxWindow
* pParent
87 ,const wxPoint
& rPos
= wxDefaultPosition
88 ,const wxSize
& rSize
= wxDefaultSize
89 ,long lStyle
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
90 ,const wxValidator
& rValidator
= wxDefaultValidator
91 ,const wxString
& rsName
= wxTreeCtrlNameStr
100 // Get the total number of items in the control
102 size_t GetCount(void) const;
105 // Indent is the number of pixels the children are indented relative to
106 // the parents position. SetIndent() also redraws the control
109 unsigned int GetIndent(void) const;
110 void SetIndent(unsigned int uIndent
);
113 // Spacing is the number of pixels between the start and the Text
115 unsigned int GetSpacing(void) const { return 18; } // return wxGTK default
116 void SetSpacing(unsigned int uSpacing
) { }
119 // Image list: these functions allow to associate an image list with
120 // the control and retrieve it. Note that the control does _not_ delete
121 // the associated image list when it's deleted in order to allow image
122 // lists to be shared between different controls.
124 // OS/2 doesn't really use imagelists as MSW does, but since the MSW
125 // control is the basis for this one, until I decide how to get rid of
126 // the need for them they are here for now.
128 wxImageList
* GetImageList(void) const;
129 wxImageList
* GetStateImageList(void) const;
131 void AssignImageList(wxImageList
* pImageList
);
132 void AssignStateImageList(wxImageList
* pImageList
);
133 void SetImageList(wxImageList
* pImageList
);
134 void SetStateImageList(wxImageList
* pImageList
);
137 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
138 // member functions of wxTreeItem because they must know the tree the item
139 // belongs to for Windows implementation and storing the pointer to
140 // wxTreeCtrl in each wxTreeItem is just too much waste.
145 wxString
GetItemText(const wxTreeItemId
& rItem
) const;
146 void SetItemText( const wxTreeItemId
& rItem
147 ,const wxString
& rsText
151 // One of the images associated with the item (normal by default)
153 int GetItemImage( const wxTreeItemId
& rItem
154 ,wxTreeItemIcon vWhich
= wxTreeItemIcon_Normal
156 void SetItemImage( const wxTreeItemId
& rItem
158 ,wxTreeItemIcon vWhich
= wxTreeItemIcon_Normal
162 // Data associated with the item
164 wxTreeItemData
* GetItemData(const wxTreeItemId
& rItem
) const;
165 void SetItemData( const wxTreeItemId
& rItem
166 ,wxTreeItemData
* pData
170 // Item's text colour
172 wxColour
GetItemTextColour(const wxTreeItemId
& rItem
) const;
173 void SetItemTextColour( const wxTreeItemId
& rItem
174 ,const wxColour
& rColor
178 // Item's background colour
180 wxColour
GetItemBackgroundColour(const wxTreeItemId
& rItem
) const;
181 void SetItemBackgroundColour( const wxTreeItemId
& rItem
182 ,const wxColour
& rColour
188 wxFont
GetItemFont(const wxTreeItemId
& rItem
) const;
189 void SetItemFont( const wxTreeItemId
& rItem
194 // Force appearance of [+] button near the item. This is useful to
195 // allow the user to expand the items which don't have any children now
196 // - but instead add them only when needed, thus minimizing memory
197 // usage and loading time.
199 void SetItemHasChildren( const wxTreeItemId
& rItem
204 // The item will be shown in bold
206 void SetItemBold( const wxTreeItemId
& rItem
211 // The item will be shown with a drop highlight
213 void SetItemDropHighlight( const wxTreeItemId
& rItem
214 ,bool bHighlight
= TRUE
218 // Item status inquiries
219 // ---------------------
223 // Is the item visible (it might be outside the view or not expanded)?
225 bool IsVisible(const wxTreeItemId
& rItem
) const;
228 // Does the item has any children?
230 bool ItemHasChildren(const wxTreeItemId
& rItem
) const;
233 // Is the item expanded (only makes sense if HasChildren())?
235 bool IsExpanded(const wxTreeItemId
& rItem
) const;
238 // Is this item currently selected (the same as has focus)?
240 bool IsSelected(const wxTreeItemId
& rItem
) const;
243 // Is item text in bold font?
245 bool IsBold(const wxTreeItemId
& rItem
) const;
248 // Number of children
249 // ------------------
253 // If 'recursively' is FALSE, only immediate children count, otherwise
254 // the returned number is the number of all items in this branch
256 size_t GetChildrenCount( const wxTreeItemId
& rItem
257 ,bool bRecursively
= TRUE
266 // Get the root tree item
268 wxTreeItemId
GetRootItem(void) const;
271 // Get the item currently selected (may return NULL if no selection)
273 wxTreeItemId
GetSelection(void) const;
276 // Get the items currently selected, return the number of such item
278 size_t GetSelections(wxArrayTreeItemIds
& rSelections
) const;
281 // Get the parent of this item (may return NULL if root)
283 wxTreeItemId
GetItemParent(const wxTreeItemId
& rItem
) const;
285 #if WXWIN_COMPATIBILITY_2_2
286 // deprecated: Use GetItemParent instead.
287 wxTreeItemId
GetParent(const wxTreeItemId
& rItem
) const
288 { return GetItemParent(rItem
); }
290 // Expose the base class method hidden by the one above.
291 wxWindow
* GetParent(void) const { return wxControl::GetParent(); }
292 #endif // WXWIN_COMPATIBILITY_2_2
295 // For this enumeration function you must pass in a "cookie" parameter
296 // which is opaque for the application but is necessary for the library
297 // to make these functions reentrant (i.e. allow more than one
298 // enumeration on one and the same object simultaneously). Of course,
299 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
304 // Get the first child of this item
306 wxTreeItemId
GetFirstChild( const wxTreeItemId
& rItem
311 // Get the next child
313 wxTreeItemId
GetNextChild( const wxTreeItemId
& rItem
318 // Get the last child of this item - this method doesn't use cookies
320 wxTreeItemId
GetLastChild(const wxTreeItemId
& rItem
) const;
323 // Get the next sibling of this item
325 wxTreeItemId
GetNextSibling(const wxTreeItemId
& rItem
) const;
328 // Get the previous sibling
330 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& rItem
) const;
333 // Get first visible item
335 wxTreeItemId
GetFirstVisibleItem(void) const;
338 // Get the next visible item: item must be visible itself!
339 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
341 wxTreeItemId
GetNextVisible(const wxTreeItemId
& rItem
) const;
344 // Get the previous visible item: item must be visible itself!
346 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& rItem
) const;
354 // Add the root node to the tree
356 wxTreeItemId
AddRoot( const wxString
& rsText
358 ,int nSelectedImage
= -1
359 ,wxTreeItemData
* pData
= NULL
363 // Insert a new item in as the first child of the parent
365 wxTreeItemId
PrependItem( const wxTreeItemId
& rParent
366 ,const wxString
& rsText
368 ,int nSelectedImage
= -1
369 ,wxTreeItemData
* pData
= NULL
373 // Insert a new item after a given one
375 wxTreeItemId
InsertItem( const wxTreeItemId
& rParent
376 ,const wxTreeItemId
& rIdPrevious
377 ,const wxString
& rsText
379 ,int nSelectedImage
= -1
380 ,wxTreeItemData
* pData
= NULL
384 // Insert a new item before the one with the given index
386 wxTreeItemId
InsertItem( const wxTreeItemId
& pParent
388 ,const wxString
& rsText
390 ,int nSelectedImage
= -1
391 ,wxTreeItemData
* pData
= NULL
395 // Insert a new item in as the last child of the parent
397 wxTreeItemId
AppendItem( const wxTreeItemId
& rParent
398 ,const wxString
& rsText
400 ,int nSelectedImage
= -1
401 ,wxTreeItemData
* pData
= NULL
405 // Delete this item and associated data if any
407 void Delete(const wxTreeItemId
& rItem
);
410 // Delete all children (but don't delete the item itself)
412 void DeleteChildren(const wxTreeItemId
& rItem
);
415 // Delete all items from the tree
417 void DeleteAllItems(void);
422 void Expand(const wxTreeItemId
& rItem
);
425 // Collapse the item without removing its children
427 void Collapse(const wxTreeItemId
& rItem
);
430 // Collapse the item and remove all children
432 void CollapseAndReset(const wxTreeItemId
& rItem
);
435 // Toggles the current state
437 void Toggle(const wxTreeItemId
& rItem
);
440 // Remove the selection from currently selected item (if any)
445 // Unselect all items (only makes sense for multiple selection control)
447 void UnselectAll(void);
452 void SelectItem(const wxTreeItemId
& rItem
);
455 // Make sure this item is visible (expanding the parent item and/or
456 // scrolling to this item if necessary)
458 void EnsureVisible(const wxTreeItemId
& rItem
);
461 // Scroll to this item (but don't expand its parent)
463 void ScrollTo(const wxTreeItemId
& rItem
);
466 // OS/2 does not use a separate edit field for editting text. Here for
467 // interface compatibility, only.
469 wxTextCtrl
* EditLabel( const wxTreeItemId
& rItem
470 ,wxClassInfo
* pTextCtrlClass
= CLASSINFO(wxTextCtrl
)
474 // returns NULL for OS/2 in ALL cases
476 wxTextCtrl
* GetEditControl(void) const {return (wxTextCtrl
*)NULL
;}
479 // End editing and accept or discard the changes to item label
481 void EndEditLabel( const wxTreeItemId
& rItem
482 ,bool bDiscardChanges
= FALSE
491 // This function is called to compare 2 items and should return -1, 0
492 // or +1 if the first item is less than, equal to or greater than the
493 // second one. The base class version performs alphabetic comparaison
494 // of item labels (GetText)
496 virtual int OnCompareItems( const wxTreeItemId
& rItem1
497 ,const wxTreeItemId
& rItem2
501 // Sort the children of this item using OnCompareItems
503 void SortChildren(const wxTreeItemId
& rItem
);
511 // Determine to which item (if any) belongs the given point (the
512 // coordinates specified are relative to the client area of tree ctrl)
513 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
517 // The first function is more portable (because easier to implement
518 // on other platforms), but the second one returns some extra info.
520 wxTreeItemId
HitTest(const wxPoint
& rPoint
)
521 { int nDummy
= 0; return HitTest(rPoint
, nDummy
); }
522 wxTreeItemId
HitTest( const wxPoint
& rPoint
527 // Get the bounding rectangle of the item (or of its label only)
529 bool GetBoundingRect( const wxTreeItemId
& rItem
531 ,bool bTextOnly
= FALSE
538 // These methods are deprecated and will be removed in future versions of
539 // wxWindows, they're here for compatibility only, don't use them in new
540 // code (the comments indicate why these methods are now useless and how to
545 // Use Expand, Collapse, CollapseAndReset or Toggle
547 void ExpandItem( const wxTreeItemId
& rItem
552 // Use AddRoot, PrependItem or AppendItem
554 wxTreeItemId
InsertItem( const wxTreeItemId
& pParent
555 ,const wxString
& rsText
558 ,long lInsertAfter
= wxTREE_INSERT_LAST
562 // Use Set/GetImageList and Set/GetStateImageList
564 wxImageList
* GetImageList(int nVal
) const
565 { return GetImageList(); }
566 void SetImageList(wxImageList
* pImageList
, int nVal
)
567 { SetImageList(pImageList
); }
570 // Use Set/GetItemImage directly
572 int GetItemSelectedImage(const wxTreeItemId
& rItem
) const
573 { return GetItemImage(rItem
, wxTreeItemIcon_Selected
); }
574 void SetItemSelectedImage(const wxTreeItemId
& rItem
, int nImage
)
575 { SetItemImage(rItem
, nImage
, wxTreeItemIcon_Selected
); }
582 virtual MRESULT
OS2WindowProc( WXUINT uMsg
586 virtual bool OS2Command( WXUINT uParam
589 // virtual bool OMSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
592 // Override some base class virtuals
594 virtual bool SetBackgroundColour(const wxColour
& rColour
);
595 virtual bool SetForegroundColour(const wxColour
& rColour
);
598 // Get/set the check state for the item (only for wxTR_MULTIPLE)
600 bool IsItemChecked(const wxTreeItemId
& rItem
) const;
601 void SetItemCheck( const wxTreeItemId
& rItem
607 // SetImageList helper
609 void SetAnyImageList( wxImageList
* pImageList
614 // Refresh a single item
616 void RefreshItem(const wxTreeItemId
& rItem
);
618 wxImageList
* m_pImageListNormal
; // images for tree elements
619 wxImageList
* m_pImageListState
; // special images for app defined states
620 bool m_bOwnsImageListNormal
;
621 bool m_bOwnsImageListState
;
626 // The common part of all ctors
633 inline bool DoGetItem(wxTreeViewItem
* pTvItem
) const;
634 inline void DoSetItem(wxTreeViewItem
* pTvItem
);
636 inline void DoExpand( const wxTreeItemId
& rItem
639 wxTreeItemId
DoInsertItem( const wxTreeItemId
& pParent
640 ,wxTreeItemId hInsertAfter
641 ,const wxString
& rsText
644 ,wxTreeItemData
* pData
646 int DoGetItemImageFromData( const wxTreeItemId
& rItem
647 ,wxTreeItemIcon vWhich
649 void DoSetItemImageFromData( const wxTreeItemId
& rItem
651 ,wxTreeItemIcon vWhich
653 void DoSetItemImages( const wxTreeItemId
& rItem
657 void DeleteTextCtrl() { };
660 // support for additional item images which we implement using
661 // wxTreeItemIndirectData technique - see the comments in msw/treectrl.cpp
663 void SetIndirectItemData( const wxTreeItemId
& rItem
664 ,class wxTreeItemIndirectData
* pData
666 bool HasIndirectData(const wxTreeItemId
& rItem
) const;
667 bool IsDataIndirect(wxTreeItemData
* pData
) const
668 { return pData
&& pData
->GetId().m_pItem
== 0; }
671 // The hash storing the items attributes (indexed by items ids)
673 wxHashTable m_vAttrs
;
676 // TRUE if the hash above is not empty
683 wxDragImage
* m_pDragImage
;
685 // Virtual root item, if wxTR_HIDE_ROOT is set.
686 // void* m_pVirtualRoot;
688 // the starting item for selection with Shift
689 // WXHTREEITEM m_htSelStart;
691 friend class wxTreeItemIndirectData
;
692 friend class wxTreeSortHelper
;
694 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
695 DECLARE_NO_COPY_CLASS(wxTreeCtrl
)
696 }; // end of CLASS wxTreeCtrl
698 #endif // wxUSE_TREECTRL