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 InsertItem() variant
44 #define wxTREE_INSERT_FIRST 0xFFFF0001
45 #define wxTREE_INSERT_LAST 0xFFFF0002
47 // hash storing attributes for our items
48 WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr
*, wxMapTreeAttr
);
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
53 class WXDLLEXPORT wxTreeCtrl
: public wxControl
58 wxTreeCtrl() { Init(); }
60 wxTreeCtrl( wxWindow
* pParent
61 ,wxWindowID vId
= wxID_ANY
62 ,const wxPoint
& rPos
= wxDefaultPosition
63 ,const wxSize
& rSize
= wxDefaultSize
64 ,long lStyle
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
65 ,const wxValidator
& rValidator
= wxDefaultValidator
66 ,const wxString
& rsName
= wxTreeCtrlNameStr
78 virtual ~wxTreeCtrl();
80 bool Create( wxWindow
* pParent
81 ,wxWindowID vId
= wxID_ANY
82 ,const wxPoint
& rPos
= wxDefaultPosition
83 ,const wxSize
& rSize
= wxDefaultSize
84 ,long lStyle
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
85 ,const wxValidator
& rValidator
= wxDefaultValidator
86 ,const wxString
& rsName
= wxTreeCtrlNameStr
95 // Get the total number of items in the control
97 size_t GetCount(void) const;
100 // Indent is the number of pixels the children are indented relative to
101 // the parents position. SetIndent() also redraws the control
104 unsigned int GetIndent(void) const;
105 void SetIndent(unsigned int uIndent
);
108 // Spacing is the number of pixels between the start and the Text
110 unsigned int GetSpacing(void) const { return 18; } // return wxGTK default
111 void SetSpacing(unsigned int uSpacing
) { }
114 // Image list: these functions allow to associate an image list with
115 // the control and retrieve it. Note that the control does _not_ delete
116 // the associated image list when it's deleted in order to allow image
117 // lists to be shared between different controls.
119 // OS/2 doesn't really use imagelists as MSW does, but since the MSW
120 // control is the basis for this one, until I decide how to get rid of
121 // the need for them they are here for now.
123 wxImageList
* GetImageList(void) const;
124 wxImageList
* GetStateImageList(void) const;
126 void AssignImageList(wxImageList
* pImageList
);
127 void AssignStateImageList(wxImageList
* pImageList
);
128 void SetImageList(wxImageList
* pImageList
);
129 void SetStateImageList(wxImageList
* pImageList
);
132 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
133 // member functions of wxTreeItem because they must know the tree the item
134 // belongs to for Windows implementation and storing the pointer to
135 // wxTreeCtrl in each wxTreeItem is just too much waste.
140 wxString
GetItemText(const wxTreeItemId
& rItem
) const;
141 void SetItemText( const wxTreeItemId
& rItem
142 ,const wxString
& rsText
146 // One of the images associated with the item (normal by default)
148 int GetItemImage( const wxTreeItemId
& rItem
149 ,wxTreeItemIcon vWhich
= wxTreeItemIcon_Normal
151 void SetItemImage( const wxTreeItemId
& rItem
153 ,wxTreeItemIcon vWhich
= wxTreeItemIcon_Normal
157 // Data associated with the item
159 wxTreeItemData
* GetItemData(const wxTreeItemId
& rItem
) const;
160 void SetItemData( const wxTreeItemId
& rItem
161 ,wxTreeItemData
* pData
165 // Item's text colour
167 wxColour
GetItemTextColour(const wxTreeItemId
& rItem
) const;
168 void SetItemTextColour( const wxTreeItemId
& rItem
169 ,const wxColour
& rColor
173 // Item's background colour
175 wxColour
GetItemBackgroundColour(const wxTreeItemId
& rItem
) const;
176 void SetItemBackgroundColour( const wxTreeItemId
& rItem
177 ,const wxColour
& rColour
183 wxFont
GetItemFont(const wxTreeItemId
& rItem
) const;
184 void SetItemFont( const wxTreeItemId
& rItem
189 // Force appearance of [+] button near the item. This is useful to
190 // allow the user to expand the items which don't have any children now
191 // - but instead add them only when needed, thus minimizing memory
192 // usage and loading time.
194 void SetItemHasChildren( const wxTreeItemId
& rItem
199 // The item will be shown in bold
201 void SetItemBold( const wxTreeItemId
& rItem
206 // The item will be shown with a drop highlight
208 void SetItemDropHighlight( const wxTreeItemId
& rItem
209 ,bool bHighlight
= true
213 // Item status inquiries
214 // ---------------------
218 // Is the item visible (it might be outside the view or not expanded)?
220 bool IsVisible(const wxTreeItemId
& rItem
) const;
223 // Does the item has any children?
225 bool ItemHasChildren(const wxTreeItemId
& rItem
) const;
228 // Is the item expanded (only makes sense if HasChildren())?
230 bool IsExpanded(const wxTreeItemId
& rItem
) const;
233 // Is this item currently selected (the same as has focus)?
235 bool IsSelected(const wxTreeItemId
& rItem
) const;
238 // Is item text in bold font?
240 bool IsBold(const wxTreeItemId
& rItem
) const;
243 // Number of children
244 // ------------------
248 // If 'bRecursively' is false, only immediate children count, otherwise
249 // the returned number is the number of all items in this branch
251 size_t GetChildrenCount( const wxTreeItemId
& rItem
252 ,bool bRecursively
= true
261 // Get the root tree item
263 wxTreeItemId
GetRootItem(void) const;
266 // Get the item currently selected (may return NULL if no selection)
268 wxTreeItemId
GetSelection(void) const;
271 // Get the items currently selected, return the number of such item
273 size_t GetSelections(wxArrayTreeItemIds
& rSelections
) const;
276 // Get the parent of this item (may return NULL if root)
278 wxTreeItemId
GetItemParent(const wxTreeItemId
& rItem
) const;
280 #if WXWIN_COMPATIBILITY_2_2
281 // deprecated: Use GetItemParent instead.
282 wxDEPRECATED( wxTreeItemId
GetParent(const wxTreeItemId
& item
) const);
284 // Expose the base class method hidden by the one above. Not deprecatable.
285 wxWindow
*GetParent() const { return wxControl::GetParent(); }
286 #endif // WXWIN_COMPATIBILITY_2_2
288 // for this enumeration function you must pass in a "cookie" parameter
289 // which is opaque for the application but is necessary for the library
290 // to make these functions reentrant (i.e. allow more than one
291 // enumeration on one and the same object simultaneously). Of course,
292 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
295 // get the first child of this item
296 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
297 wxTreeItemIdValue
& cookie
) const;
298 // get the next child
299 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
300 wxTreeItemIdValue
& cookie
) const;
303 // Get the last child of this item - this method doesn't use cookies
305 wxTreeItemId
GetLastChild(const wxTreeItemId
& rItem
) const;
308 // Get the next sibling of this item
310 wxTreeItemId
GetNextSibling(const wxTreeItemId
& rItem
) const;
313 // Get the previous sibling
315 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& rItem
) const;
318 // Get first visible item
320 wxTreeItemId
GetFirstVisibleItem(void) const;
323 // Get the next visible item: item must be visible itself!
324 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
326 wxTreeItemId
GetNextVisible(const wxTreeItemId
& rItem
) const;
329 // Get the previous visible item: item must be visible itself!
331 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& rItem
) const;
339 // Add the root node to the tree
341 wxTreeItemId
AddRoot( const wxString
& rsText
343 ,int nSelectedImage
= -1
344 ,wxTreeItemData
* pData
= NULL
348 // Insert a new item in as the first child of the parent
350 wxTreeItemId
PrependItem( const wxTreeItemId
& rParent
351 ,const wxString
& rsText
353 ,int nSelectedImage
= -1
354 ,wxTreeItemData
* pData
= NULL
358 // Insert a new item after a given one
360 wxTreeItemId
InsertItem( const wxTreeItemId
& rParent
361 ,const wxTreeItemId
& rIdPrevious
362 ,const wxString
& rsText
364 ,int nSelectedImage
= -1
365 ,wxTreeItemData
* pData
= NULL
369 // Insert a new item before the one with the given index
371 wxTreeItemId
InsertItem( const wxTreeItemId
& pParent
373 ,const wxString
& rsText
375 ,int nSelectedImage
= -1
376 ,wxTreeItemData
* pData
= NULL
380 // Insert a new item in as the last child of the parent
382 wxTreeItemId
AppendItem( const wxTreeItemId
& rParent
383 ,const wxString
& rsText
385 ,int nSelectedImage
= -1
386 ,wxTreeItemData
* pData
= NULL
390 // Delete this item and associated data if any
392 void Delete(const wxTreeItemId
& rItem
);
395 // Delete all children (but don't delete the item itself)
397 void DeleteChildren(const wxTreeItemId
& rItem
);
400 // Delete all items from the tree
402 void DeleteAllItems(void);
407 void Expand(const wxTreeItemId
& rItem
);
410 // Collapse the item without removing its children
412 void Collapse(const wxTreeItemId
& rItem
);
415 // Collapse the item and remove all children
417 void CollapseAndReset(const wxTreeItemId
& rItem
);
420 // Toggles the current state
422 void Toggle(const wxTreeItemId
& rItem
);
425 // Remove the selection from currently selected item (if any)
430 // Unselect all items (only makes sense for multiple selection control)
432 void UnselectAll(void);
437 void SelectItem(const wxTreeItemId
& rItem
);
440 // Make sure this item is visible (expanding the parent item and/or
441 // scrolling to this item if necessary)
443 void EnsureVisible(const wxTreeItemId
& rItem
);
446 // Scroll to this item (but don't expand its parent)
448 void ScrollTo(const wxTreeItemId
& rItem
);
451 // OS/2 does not use a separate edit field for editting text. Here for
452 // interface compatibility, only.
454 wxTextCtrl
* EditLabel( const wxTreeItemId
& rItem
455 ,wxClassInfo
* pTextCtrlClass
= CLASSINFO(wxTextCtrl
)
459 // returns NULL for OS/2 in ALL cases
461 wxTextCtrl
* GetEditControl(void) const {return (wxTextCtrl
*)NULL
;}
464 // End editing and accept or discard the changes to item label
466 void EndEditLabel( const wxTreeItemId
& rItem
467 ,bool bDiscardChanges
= false
476 // This function is called to compare 2 items and should return -1, 0
477 // or +1 if the first item is less than, equal to or greater than the
478 // second one. The base class version performs alphabetic comparaison
479 // of item labels (GetText)
481 virtual int OnCompareItems( const wxTreeItemId
& rItem1
482 ,const wxTreeItemId
& rItem2
486 // Sort the children of this item using OnCompareItems
488 void SortChildren(const wxTreeItemId
& rItem
);
496 // Determine to which item (if any) belongs the given point (the
497 // coordinates specified are relative to the client area of tree ctrl)
498 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
502 // The first function is more portable (because easier to implement
503 // on other platforms), but the second one returns some extra info.
505 wxTreeItemId
HitTest(const wxPoint
& rPoint
)
506 { int nDummy
= 0; return HitTest(rPoint
, nDummy
); }
507 wxTreeItemId
HitTest( const wxPoint
& rPoint
512 // Get the bounding rectangle of the item (or of its label only)
514 bool GetBoundingRect( const wxTreeItemId
& rItem
516 ,bool bTextOnly
= false
523 #if WXWIN_COMPATIBILITY_2_4
524 // These methods are deprecated and will be removed in future versions of
525 // wxWidgets, they're here for compatibility only, don't use them in new
526 // code (the comments indicate why these methods are now useless and how to
531 // Use Expand, Collapse, CollapseAndReset or Toggle
533 wxDEPRECATED( void ExpandItem( const wxTreeItemId
& rItem
538 // Use AddRoot, PrependItem or AppendItem
540 wxDEPRECATED( wxTreeItemId
InsertItem( const wxTreeItemId
& pParent
541 ,const wxString
& rsText
544 ,long lInsertAfter
= wxTREE_INSERT_LAST
548 // Use Set/GetImageList and Set/GetStateImageList
550 wxDEPRECATED( wxImageList
* GetImageList(int nVal
) const );
551 wxDEPRECATED( void SetImageList(wxImageList
* pImageList
, int nVal
) );
554 // Use Set/GetItemImage directly
556 wxDEPRECATED( int GetItemSelectedImage(const wxTreeItemId
& rItem
) const );
557 wxDEPRECATED( void SetItemSelectedImage(const wxTreeItemId
& rItem
, int nImage
) );
560 // For this enumeration function you must pass in a "cookie" parameter
561 // which is opaque for the application but is necessary for the library
562 // to make these functions reentrant (i.e. allow more than one
563 // enumeration on one and the same object simultaneously). Of course,
564 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
569 // Get the first child of this item
571 wxDEPRECATED( wxTreeItemId
GetFirstChild( const wxTreeItemId
& rItem
576 // Get the next child
578 wxDEPRECATED( wxTreeItemId
GetNextChild( const wxTreeItemId
& rItem
581 #endif // WXWIN_COMPATIBILITY_2_4
588 virtual MRESULT
OS2WindowProc( WXUINT uMsg
592 virtual bool OS2Command( WXUINT uParam
595 // virtual bool OMSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
598 // Override some base class virtuals
600 virtual bool SetBackgroundColour(const wxColour
& rColour
);
601 virtual bool SetForegroundColour(const wxColour
& rColour
);
604 // Get/set the check state for the item (only for wxTR_MULTIPLE)
606 bool IsItemChecked(const wxTreeItemId
& rItem
) const;
607 void SetItemCheck( const wxTreeItemId
& rItem
613 // SetImageList helper
615 void SetAnyImageList( wxImageList
* pImageList
620 // Refresh a single item
622 void RefreshItem(const wxTreeItemId
& rItem
);
624 wxImageList
* m_pImageListNormal
; // images for tree elements
625 wxImageList
* m_pImageListState
; // special images for app defined states
626 bool m_bOwnsImageListNormal
;
627 bool m_bOwnsImageListState
;
632 // The common part of all ctors
639 inline bool DoGetItem(wxTreeViewItem
* pTvItem
) const;
640 inline void DoSetItem(wxTreeViewItem
* pTvItem
);
642 inline void DoExpand( const wxTreeItemId
& rItem
645 wxTreeItemId
DoInsertItem( const wxTreeItemId
& pParent
646 ,wxTreeItemId hInsertAfter
647 ,const wxString
& rsText
650 ,wxTreeItemData
* pData
652 int DoGetItemImageFromData( const wxTreeItemId
& rItem
653 ,wxTreeItemIcon vWhich
655 void DoSetItemImageFromData( const wxTreeItemId
& rItem
657 ,wxTreeItemIcon vWhich
659 void DoSetItemImages( const wxTreeItemId
& rItem
663 void DeleteTextCtrl() { };
666 // support for additional item images which we implement using
667 // wxTreeItemIndirectData technique - see the comments in msw/treectrl.cpp
669 void SetIndirectItemData( const wxTreeItemId
& rItem
670 ,class wxTreeItemIndirectData
* pData
672 bool HasIndirectData(const wxTreeItemId
& rItem
) const;
673 bool IsDataIndirect(wxTreeItemData
* pData
) const
674 { return pData
&& pData
->GetId().m_pItem
== 0; }
677 // The hash storing the items attributes (indexed by items ids)
679 wxMapTreeAttr m_vAttrs
;
682 // true if the hash above is not empty
689 wxDragImage
* m_pDragImage
;
691 // Virtual root item, if wxTR_HIDE_ROOT is set.
692 // void* m_pVirtualRoot;
694 // the starting item for selection with Shift
695 // WXHTREEITEM m_htSelStart;
697 friend class wxTreeItemIndirectData
;
698 friend class wxTreeSortHelper
;
700 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
701 DECLARE_NO_COPY_CLASS(wxTreeCtrl
)
702 }; // end of CLASS wxTreeCtrl
704 #endif // wxUSE_TREECTRL