1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/msw/treectrl.h
3 // Purpose: wxTreeCtrl class
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10/10/98
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_MSW_TREECTRL_H_
12 #define _WX_MSW_TREECTRL_H_
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
20 #include "wx/textctrl.h"
21 #include "wx/dynarray.h"
22 #include "wx/treebase.h"
23 #include "wx/hashmap.h"
26 // Cygwin windows.h defines these identifiers
32 class WXDLLIMPEXP_FWD_CORE wxImageList
;
33 class WXDLLIMPEXP_FWD_CORE wxDragImage
;
34 struct WXDLLIMPEXP_FWD_CORE wxTreeViewItem
;
36 #if WXWIN_COMPATIBILITY_2_6
37 // NB: all the following flags are for compatbility only and will be removed in the
39 // flags for deprecated InsertItem() variant (their values are the same as of
40 // TVI_FIRST and TVI_LAST)
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 // ----------------------------------------------------------------------------
52 class WXDLLIMPEXP_CORE wxTreeCtrl
: public wxTreeCtrlBase
57 wxTreeCtrl() { Init(); }
59 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
60 const wxPoint
& pos
= wxDefaultPosition
,
61 const wxSize
& size
= wxDefaultSize
,
62 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
63 const wxValidator
& validator
= wxDefaultValidator
,
64 const wxString
& name
= wxTreeCtrlNameStr
)
66 Create(parent
, id
, pos
, size
, style
, validator
, name
);
69 virtual ~wxTreeCtrl();
71 bool Create(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
72 const wxPoint
& pos
= wxDefaultPosition
,
73 const wxSize
& size
= wxDefaultSize
,
74 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
75 const wxValidator
& validator
= wxDefaultValidator
,
76 const wxString
& name
= wxTreeCtrlNameStr
);
78 // implement base class pure virtuals
79 // ----------------------------------
81 virtual unsigned int GetCount() const;
83 virtual unsigned int GetIndent() const;
84 virtual void SetIndent(unsigned int indent
);
86 virtual void SetImageList(wxImageList
*imageList
);
87 virtual void SetStateImageList(wxImageList
*imageList
);
89 virtual wxString
GetItemText(const wxTreeItemId
& item
) const;
90 virtual int GetItemImage(const wxTreeItemId
& item
,
91 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
92 virtual wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
93 virtual wxColour
GetItemTextColour(const wxTreeItemId
& item
) const;
94 virtual wxColour
GetItemBackgroundColour(const wxTreeItemId
& item
) const;
95 virtual wxFont
GetItemFont(const wxTreeItemId
& item
) const;
97 virtual void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
98 virtual void SetItemImage(const wxTreeItemId
& item
, int image
,
99 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
100 virtual void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
101 virtual void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= true);
102 virtual void SetItemBold(const wxTreeItemId
& item
, bool bold
= true);
103 virtual void SetItemDropHighlight(const wxTreeItemId
& item
,
104 bool highlight
= true);
105 virtual void SetItemTextColour(const wxTreeItemId
& item
,
106 const wxColour
& col
);
107 virtual void SetItemBackgroundColour(const wxTreeItemId
& item
,
108 const wxColour
& col
);
109 virtual void SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
);
111 // item status inquiries
112 // ---------------------
114 virtual bool IsVisible(const wxTreeItemId
& item
) const;
115 virtual bool ItemHasChildren(const wxTreeItemId
& item
) const;
116 virtual bool IsExpanded(const wxTreeItemId
& item
) const;
117 virtual bool IsSelected(const wxTreeItemId
& item
) const;
118 virtual bool IsBold(const wxTreeItemId
& item
) const;
120 virtual size_t GetChildrenCount(const wxTreeItemId
& item
,
121 bool recursively
= true) const;
126 virtual wxTreeItemId
GetRootItem() const;
127 virtual wxTreeItemId
GetSelection() const;
128 virtual size_t GetSelections(wxArrayTreeItemIds
& selections
) const;
129 virtual wxTreeItemId
GetFocusedItem() const;
131 virtual void ClearFocusedItem();
132 virtual void SetFocusedItem(const wxTreeItemId
& item
);
135 virtual wxTreeItemId
GetItemParent(const wxTreeItemId
& item
) const;
136 virtual wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
137 wxTreeItemIdValue
& cookie
) const;
138 virtual wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
139 wxTreeItemIdValue
& cookie
) const;
140 virtual wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
142 virtual wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
143 virtual wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
145 virtual wxTreeItemId
GetFirstVisibleItem() const;
146 virtual wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
147 virtual wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
152 virtual wxTreeItemId
AddRoot(const wxString
& text
,
153 int image
= -1, int selectedImage
= -1,
154 wxTreeItemData
*data
= NULL
);
156 virtual void Delete(const wxTreeItemId
& item
);
157 virtual void DeleteChildren(const wxTreeItemId
& item
);
158 virtual void DeleteAllItems();
160 virtual void Expand(const wxTreeItemId
& item
);
161 virtual void Collapse(const wxTreeItemId
& item
);
162 virtual void CollapseAndReset(const wxTreeItemId
& item
);
163 virtual void Toggle(const wxTreeItemId
& item
);
165 virtual void Unselect();
166 virtual void UnselectAll();
167 virtual void SelectItem(const wxTreeItemId
& item
, bool select
= true);
168 virtual void SelectChildren(const wxTreeItemId
& parent
);
170 virtual void EnsureVisible(const wxTreeItemId
& item
);
171 virtual void ScrollTo(const wxTreeItemId
& item
);
173 virtual wxTextCtrl
*EditLabel(const wxTreeItemId
& item
,
174 wxClassInfo
* textCtrlClass
= wxCLASSINFO(wxTextCtrl
));
175 virtual wxTextCtrl
*GetEditControl() const;
176 virtual void EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
177 bool discardChanges
= false)
179 DoEndEditLabel(discardChanges
);
182 virtual void SortChildren(const wxTreeItemId
& item
);
184 virtual bool GetBoundingRect(const wxTreeItemId
& item
,
186 bool textOnly
= false) const;
191 virtual wxVisualAttributes
GetDefaultAttributes() const
193 return GetClassDefaultAttributes(GetWindowVariant());
196 static wxVisualAttributes
197 GetClassDefaultAttributes(wxWindowVariant variant
= wxWINDOW_VARIANT_NORMAL
);
200 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
201 virtual WXLRESULT
MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
202 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
203 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
204 virtual bool MSWShouldPreProcessMessage(WXMSG
* msg
);
206 // override some base class virtuals
207 virtual bool SetBackgroundColour(const wxColour
&colour
);
208 virtual bool SetForegroundColour(const wxColour
&colour
);
210 // returns true if the platform should explicitly apply a theme border
211 virtual bool CanApplyThemeBorder() const { return false; }
214 // Implement "update locking" in a custom way for this control.
215 virtual void DoFreeze();
216 virtual void DoThaw();
218 virtual void DoSetSize(int x
, int y
,
219 int width
, int height
,
220 int sizeFlags
= wxSIZE_AUTO
);
222 // SetImageList helper
223 void SetAnyImageList(wxImageList
*imageList
, int which
);
225 // refresh a single item
226 void RefreshItem(const wxTreeItemId
& item
);
229 void DoEndEditLabel(bool discardChanges
= false);
231 virtual int DoGetItemState(const wxTreeItemId
& item
) const;
232 virtual void DoSetItemState(const wxTreeItemId
& item
, int state
);
234 virtual wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
236 const wxString
& text
,
237 int image
, int selectedImage
,
238 wxTreeItemData
*data
);
239 virtual wxTreeItemId
DoInsertAfter(const wxTreeItemId
& parent
,
240 const wxTreeItemId
& idPrevious
,
241 const wxString
& text
,
242 int image
= -1, int selImage
= -1,
243 wxTreeItemData
*data
= NULL
);
244 virtual wxTreeItemId
DoTreeHitTest(const wxPoint
& point
, int& flags
) const;
246 // obtain the user data for the lParam member of TV_ITEM
247 class wxTreeItemParam
*GetItemParam(const wxTreeItemId
& item
) const;
249 // update the event to include the items client data and pass it to
250 // HandleWindowEvent(), return true if it processed it
251 bool HandleTreeEvent(wxTreeEvent
& event
) const;
253 // pass the event to HandleTreeEvent() and return true if the event was
254 // either unprocessed or not vetoed
255 bool IsTreeEventAllowed(wxTreeEvent
& event
) const
257 return !HandleTreeEvent(event
) || event
.IsAllowed();
260 // generate a wxEVT_KEY_DOWN event from the specified WPARAM/LPARAM values
261 // having the same meaning as for WM_KEYDOWN, return true if it was
263 bool MSWHandleTreeKeyDownEvent(WXWPARAM wParam
, WXLPARAM lParam
);
265 // handle a key event in a multi-selection control, should be only called
266 // for keys which can be used to change the selection
268 // return true if the key was processed, false otherwise
269 bool MSWHandleSelectionKey(unsigned vkey
);
272 // data used only while editing the item label:
273 wxTextCtrl
*m_textCtrl
; // text control in which it is edited
274 wxTreeItemId m_idEdited
; // the item being edited
277 // the common part of all ctors
281 bool DoGetItem(wxTreeViewItem
*tvItem
) const;
282 void DoSetItem(wxTreeViewItem
*tvItem
);
284 void DoExpand(const wxTreeItemId
& item
, int flag
);
286 void DoSelectItem(const wxTreeItemId
& item
, bool select
= true);
287 void DoUnselectItem(const wxTreeItemId
& item
);
288 void DoToggleItemSelection(const wxTreeItemId
& item
);
290 void DoUnselectAll();
291 void DoSelectChildren(const wxTreeItemId
& parent
);
293 void DeleteTextCtrl();
295 // return true if the item is the hidden root one (i.e. it's the root item
296 // and the tree has wxTR_HIDE_ROOT style)
297 bool IsHiddenRoot(const wxTreeItemId
& item
) const;
300 // check if the given flags (taken from TV_HITTESTINFO structure)
301 // indicate a position "on item": this is less trivial than just checking
302 // for TVHT_ONITEM because we consider that points to the left and right of
303 // item text are also "on item" when wxTR_FULL_ROW_HIGHLIGHT is used as the
304 // item visually spans the entire breadth of the window then
305 bool MSWIsOnItem(unsigned flags
) const;
308 // the hash storing the items attributes (indexed by item ids)
309 wxMapTreeAttr m_attrs
;
311 // true if the hash above is not empty
316 wxDragImage
*m_dragImage
;
319 // Virtual root item, if wxTR_HIDE_ROOT is set.
320 void* m_pVirtualRoot
;
322 // the starting item for selection with Shift
323 wxTreeItemId m_htSelStart
, m_htClickedItem
;
326 // whether dragging has started
329 // whether focus was lost between subsequent clicks of a single item
332 // set when we are changing selection ourselves (only used in multi
334 bool m_changingSelection
;
336 // whether we need to trigger a state image click event
337 bool m_triggerStateImageClick
;
339 // whether we need to deselect other items on mouse up
340 bool m_mouseUpDeselect
;
342 // The size to restore the control to when it is thawed, see DoThaw().
345 friend class wxTreeItemIndirectData
;
346 friend class wxTreeSortHelper
;
348 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
349 wxDECLARE_NO_COPY_CLASS(wxTreeCtrl
);
352 #endif // wxUSE_TREECTRL
354 #endif // _WX_MSW_TREECTRL_H_