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 #if WXWIN_COMPATIBILITY_2_6
40 // flags for deprecated InsertItem() variant
41 #define wxTREE_INSERT_FIRST 0xFFFF0001
42 #define wxTREE_INSERT_LAST 0xFFFF0002
45 // hash storing attributes for our items
46 WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr
*, wxMapTreeAttr
);
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
51 class WXDLLEXPORT wxTreeCtrl
: public wxControl
56 wxTreeCtrl() { Init(); }
58 wxTreeCtrl( wxWindow
* pParent
59 ,wxWindowID vId
= wxID_ANY
60 ,const wxPoint
& rPos
= wxDefaultPosition
61 ,const wxSize
& rSize
= wxDefaultSize
62 ,long lStyle
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
63 ,const wxValidator
& rValidator
= wxDefaultValidator
64 ,const wxString
& rsName
= wxTreeCtrlNameStr
76 virtual ~wxTreeCtrl();
78 bool Create( wxWindow
* pParent
79 ,wxWindowID vId
= wxID_ANY
80 ,const wxPoint
& rPos
= wxDefaultPosition
81 ,const wxSize
& rSize
= wxDefaultSize
82 ,long lStyle
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
83 ,const wxValidator
& rValidator
= wxDefaultValidator
84 ,const wxString
& rsName
= wxTreeCtrlNameStr
93 // Get the total number of items in the control
95 virtual unsigned int GetCount(void) const;
98 // Indent is the number of pixels the children are indented relative to
99 // the parents position. SetIndent() also redraws the control
102 unsigned int GetIndent(void) const;
103 void SetIndent(unsigned int uIndent
);
106 // Spacing is the number of pixels between the start and the Text
108 unsigned int GetSpacing(void) const { return 18; } // return wxGTK default
109 void SetSpacing(unsigned int uSpacing
) { }
112 // Image list: these functions allow to associate an image list with
113 // the control and retrieve it. Note that the control does _not_ delete
114 // the associated image list when it's deleted in order to allow image
115 // lists to be shared between different controls.
117 // OS/2 doesn't really use imagelists as MSW does, but since the MSW
118 // control is the basis for this one, until I decide how to get rid of
119 // the need for them they are here for now.
121 wxImageList
* GetImageList(void) const;
122 wxImageList
* GetStateImageList(void) const;
124 void AssignImageList(wxImageList
* pImageList
);
125 void AssignStateImageList(wxImageList
* pImageList
);
126 void SetImageList(wxImageList
* pImageList
);
127 void SetStateImageList(wxImageList
* pImageList
);
130 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
131 // member functions of wxTreeItem because they must know the tree the item
132 // belongs to for Windows implementation and storing the pointer to
133 // wxTreeCtrl in each wxTreeItem is just too much waste.
138 wxString
GetItemText(const wxTreeItemId
& rItem
) const;
139 void SetItemText( const wxTreeItemId
& rItem
140 ,const wxString
& rsText
144 // One of the images associated with the item (normal by default)
146 int GetItemImage( const wxTreeItemId
& rItem
147 ,wxTreeItemIcon vWhich
= wxTreeItemIcon_Normal
149 void SetItemImage( const wxTreeItemId
& rItem
151 ,wxTreeItemIcon vWhich
= wxTreeItemIcon_Normal
155 // Data associated with the item
157 wxTreeItemData
* GetItemData(const wxTreeItemId
& rItem
) const;
158 void SetItemData( const wxTreeItemId
& rItem
159 ,wxTreeItemData
* pData
163 // Item's text colour
165 wxColour
GetItemTextColour(const wxTreeItemId
& rItem
) const;
166 void SetItemTextColour( const wxTreeItemId
& rItem
167 ,const wxColour
& rColor
171 // Item's background colour
173 wxColour
GetItemBackgroundColour(const wxTreeItemId
& rItem
) const;
174 void SetItemBackgroundColour( const wxTreeItemId
& rItem
175 ,const wxColour
& rColour
181 wxFont
GetItemFont(const wxTreeItemId
& rItem
) const;
182 void SetItemFont( const wxTreeItemId
& rItem
187 // Force appearance of [+] button near the item. This is useful to
188 // allow the user to expand the items which don't have any children now
189 // - but instead add them only when needed, thus minimizing memory
190 // usage and loading time.
192 void SetItemHasChildren( const wxTreeItemId
& rItem
197 // The item will be shown in bold
199 void SetItemBold( const wxTreeItemId
& rItem
204 // The item will be shown with a drop highlight
206 void SetItemDropHighlight( const wxTreeItemId
& rItem
207 ,bool bHighlight
= true
211 // Item status inquiries
212 // ---------------------
216 // Is the item visible (it might be outside the view or not expanded)?
218 bool IsVisible(const wxTreeItemId
& rItem
) const;
221 // Does the item has any children?
223 bool ItemHasChildren(const wxTreeItemId
& rItem
) const;
226 // Is the item expanded (only makes sense if HasChildren())?
228 bool IsExpanded(const wxTreeItemId
& rItem
) const;
231 // Is this item currently selected (the same as has focus)?
233 bool IsSelected(const wxTreeItemId
& rItem
) const;
236 // Is item text in bold font?
238 bool IsBold(const wxTreeItemId
& rItem
) const;
241 // Number of children
242 // ------------------
246 // If 'bRecursively' is false, only immediate children count, otherwise
247 // the returned number is the number of all items in this branch
249 size_t GetChildrenCount( const wxTreeItemId
& rItem
250 ,bool bRecursively
= true
259 // Get the root tree item
261 wxTreeItemId
GetRootItem(void) const;
264 // Get the item currently selected (may return NULL if no selection)
266 wxTreeItemId
GetSelection(void) const;
269 // Get the items currently selected, return the number of such item
271 size_t GetSelections(wxArrayTreeItemIds
& rSelections
) const;
274 // Get the parent of this item (may return NULL if root)
276 wxTreeItemId
GetItemParent(const wxTreeItemId
& rItem
) const;
278 // for this enumeration function you must pass in a "cookie" parameter
279 // which is opaque for the application but is necessary for the library
280 // to make these functions reentrant (i.e. allow more than one
281 // enumeration on one and the same object simultaneously). Of course,
282 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
285 // get the first child of this item
286 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
287 wxTreeItemIdValue
& cookie
) const;
288 // get the next child
289 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
290 wxTreeItemIdValue
& cookie
) const;
293 // Get the last child of this item - this method doesn't use cookies
295 wxTreeItemId
GetLastChild(const wxTreeItemId
& rItem
) const;
298 // Get the next sibling of this item
300 wxTreeItemId
GetNextSibling(const wxTreeItemId
& rItem
) const;
303 // Get the previous sibling
305 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& rItem
) const;
308 // Get first visible item
310 wxTreeItemId
GetFirstVisibleItem(void) const;
313 // Get the next visible item: item must be visible itself!
314 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
316 wxTreeItemId
GetNextVisible(const wxTreeItemId
& rItem
) const;
319 // Get the previous visible item: item must be visible itself!
321 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& rItem
) const;
329 // Add the root node to the tree
331 wxTreeItemId
AddRoot( const wxString
& rsText
333 ,int nSelectedImage
= -1
334 ,wxTreeItemData
* pData
= NULL
338 // Insert a new item in as the first child of the parent
340 wxTreeItemId
PrependItem( const wxTreeItemId
& rParent
341 ,const wxString
& rsText
343 ,int nSelectedImage
= -1
344 ,wxTreeItemData
* pData
= NULL
348 // Insert a new item after a given one
350 wxTreeItemId
InsertItem( const wxTreeItemId
& rParent
351 ,const wxTreeItemId
& rIdPrevious
352 ,const wxString
& rsText
354 ,int nSelectedImage
= -1
355 ,wxTreeItemData
* pData
= NULL
359 // Insert a new item before the one with the given index
361 wxTreeItemId
InsertItem( const wxTreeItemId
& pParent
363 ,const wxString
& rsText
365 ,int nSelectedImage
= -1
366 ,wxTreeItemData
* pData
= NULL
370 // Insert a new item in as the last child of the parent
372 wxTreeItemId
AppendItem( const wxTreeItemId
& rParent
373 ,const wxString
& rsText
375 ,int nSelectedImage
= -1
376 ,wxTreeItemData
* pData
= NULL
380 // Delete this item and associated data if any
382 void Delete(const wxTreeItemId
& rItem
);
385 // Delete all children (but don't delete the item itself)
387 void DeleteChildren(const wxTreeItemId
& rItem
);
390 // Delete all items from the tree
392 void DeleteAllItems(void);
397 void Expand(const wxTreeItemId
& rItem
);
400 // Collapse the item without removing its children
402 void Collapse(const wxTreeItemId
& rItem
);
405 // Collapse the item and remove all children
407 void CollapseAndReset(const wxTreeItemId
& rItem
);
410 // Toggles the current state
412 void Toggle(const wxTreeItemId
& rItem
);
415 // Remove the selection from currently selected item (if any)
420 // Unselect all items (only makes sense for multiple selection control)
422 void UnselectAll(void);
427 void SelectItem(const wxTreeItemId
& rItem
);
430 // Make sure this item is visible (expanding the parent item and/or
431 // scrolling to this item if necessary)
433 void EnsureVisible(const wxTreeItemId
& rItem
);
436 // Scroll to this item (but don't expand its parent)
438 void ScrollTo(const wxTreeItemId
& rItem
);
441 // OS/2 does not use a separate edit field for editting text. Here for
442 // interface compatibility, only.
444 wxTextCtrl
* EditLabel( const wxTreeItemId
& rItem
445 ,wxClassInfo
* pTextCtrlClass
= CLASSINFO(wxTextCtrl
)
449 // returns NULL for OS/2 in ALL cases
451 wxTextCtrl
* GetEditControl(void) const {return (wxTextCtrl
*)NULL
;}
454 // End editing and accept or discard the changes to item label
456 void EndEditLabel( const wxTreeItemId
& rItem
457 ,bool bDiscardChanges
= false
466 // This function is called to compare 2 items and should return -1, 0
467 // or +1 if the first item is less than, equal to or greater than the
468 // second one. The base class version performs alphabetic comparaison
469 // of item labels (GetText)
471 virtual int OnCompareItems( const wxTreeItemId
& rItem1
472 ,const wxTreeItemId
& rItem2
476 // Sort the children of this item using OnCompareItems
478 void SortChildren(const wxTreeItemId
& rItem
);
486 // Determine to which item (if any) belongs the given point (the
487 // coordinates specified are relative to the client area of tree ctrl)
488 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
492 // The first function is more portable (because easier to implement
493 // on other platforms), but the second one returns some extra info.
495 wxTreeItemId
HitTest(const wxPoint
& rPoint
)
496 { int nDummy
= 0; return HitTest(rPoint
, nDummy
); }
497 wxTreeItemId
HitTest( const wxPoint
& rPoint
502 // Get the bounding rectangle of the item (or of its label only)
504 bool GetBoundingRect( const wxTreeItemId
& rItem
506 ,bool bTextOnly
= false
514 virtual MRESULT
OS2WindowProc( WXUINT uMsg
518 virtual bool OS2Command( WXUINT uParam
521 // virtual bool OMSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
524 // Override some base class virtuals
526 virtual bool SetBackgroundColour(const wxColour
& rColour
);
527 virtual bool SetForegroundColour(const wxColour
& rColour
);
530 // Get/set the check state for the item (only for wxTR_MULTIPLE)
532 bool IsItemChecked(const wxTreeItemId
& rItem
) const;
533 void SetItemCheck( const wxTreeItemId
& rItem
539 // SetImageList helper
541 void SetAnyImageList( wxImageList
* pImageList
546 // Refresh a single item
548 void RefreshItem(const wxTreeItemId
& rItem
);
550 wxImageList
* m_pImageListNormal
; // images for tree elements
551 wxImageList
* m_pImageListState
; // special images for app defined states
552 bool m_bOwnsImageListNormal
;
553 bool m_bOwnsImageListState
;
558 // The common part of all ctors
565 inline bool DoGetItem(wxTreeViewItem
* pTvItem
) const;
566 inline void DoSetItem(wxTreeViewItem
* pTvItem
);
568 inline void DoExpand( const wxTreeItemId
& rItem
571 wxTreeItemId
DoInsertItem( const wxTreeItemId
& pParent
572 ,wxTreeItemId hInsertAfter
573 ,const wxString
& rsText
576 ,wxTreeItemData
* pData
578 int DoGetItemImageFromData( const wxTreeItemId
& rItem
579 ,wxTreeItemIcon vWhich
581 void DoSetItemImageFromData( const wxTreeItemId
& rItem
583 ,wxTreeItemIcon vWhich
585 void DoSetItemImages( const wxTreeItemId
& rItem
589 void DeleteTextCtrl() { };
592 // support for additional item images which we implement using
593 // wxTreeItemIndirectData technique - see the comments in msw/treectrl.cpp
595 void SetIndirectItemData( const wxTreeItemId
& rItem
596 ,class wxTreeItemIndirectData
* pData
598 bool HasIndirectData(const wxTreeItemId
& rItem
) const;
599 bool IsDataIndirect(wxTreeItemData
* pData
) const
600 { return pData
&& pData
->GetId().m_pItem
== 0; }
603 // The hash storing the items attributes (indexed by items ids)
605 wxMapTreeAttr m_vAttrs
;
608 // true if the hash above is not empty
615 wxDragImage
* m_pDragImage
;
617 // Virtual root item, if wxTR_HIDE_ROOT is set.
618 // void* m_pVirtualRoot;
620 // the starting item for selection with Shift
621 // WXHTREEITEM m_htSelStart;
623 friend class wxTreeItemIndirectData
;
624 friend class wxTreeSortHelper
;
626 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
627 DECLARE_NO_COPY_CLASS(wxTreeCtrl
)
628 }; // end of CLASS wxTreeCtrl
630 #endif // wxUSE_TREECTRL