1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Base wxTreeCtrl classes
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Robert Roebling, Julian Smart et al
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treectrl.h"
30 #include "wx/imaglist.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_DRAG
)
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_RDRAG
)
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
)
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_END_LABEL_EDIT
)
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_DELETE_ITEM
)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_GET_INFO
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_SET_INFO
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_EXPANDED
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_EXPANDING
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
)
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_SEL_CHANGED
)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_SEL_CHANGING
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_KEY_DOWN
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
)
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_END_DRAG
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
)
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
)
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_MENU
)
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 IMPLEMENT_ABSTRACT_CLASS(wxTreeEvent
, wxNotifyEvent
)
65 wxTreeEvent::wxTreeEvent(wxEventType commandType
,
67 const wxTreeItemId
& item
)
68 : wxNotifyEvent(commandType
, tree
->GetId()),
71 m_editCancelled
= false;
76 SetClientObject(tree
->GetItemData(item
));
79 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
80 : wxNotifyEvent(commandType
, id
)
83 m_editCancelled
= false;
86 wxTreeEvent::wxTreeEvent(const wxTreeEvent
& event
)
87 : wxNotifyEvent(event
)
89 m_evtKey
= event
.m_evtKey
;
90 m_item
= event
.m_item
;
91 m_itemOld
= event
.m_itemOld
;
92 m_pointDrag
= event
.m_pointDrag
;
93 m_label
= event
.m_label
;
94 m_editCancelled
= event
.m_editCancelled
;
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 wxTreeCtrlBase::~wxTreeCtrlBase()
103 if (m_ownsImageListNormal
)
104 delete m_imageListNormal
;
105 if (m_ownsImageListState
)
106 delete m_imageListState
;
110 wxGetBestTreeSize(const wxTreeCtrlBase
* treeCtrl
, wxTreeItemId id
, wxSize
& size
)
114 if ( treeCtrl
->GetBoundingRect(id
, rect
, true /* just the item */) )
116 // Translate to logical position so we get the full extent
117 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
118 rect
.x
+= treeCtrl
->GetScrollPos(wxHORIZONTAL
);
119 rect
.y
+= treeCtrl
->GetScrollPos(wxVERTICAL
);
122 size
.IncTo(wxSize(rect
.GetRight(), rect
.GetBottom()));
125 wxTreeItemIdValue cookie
;
126 for ( wxTreeItemId item
= treeCtrl
->GetFirstChild(id
, cookie
);
128 item
= treeCtrl
->GetNextChild(id
, cookie
) )
130 wxGetBestTreeSize(treeCtrl
, item
, size
);
134 wxSize
wxTreeCtrlBase::DoGetBestSize() const
138 // this doesn't really compute the total bounding rectangle of all items
139 // but a not too bad guess of it which has the advantage of not having to
140 // examine all (potentially hundreds or thousands) items in the control
142 if (GetQuickBestSize())
144 for ( wxTreeItemId item
= GetRootItem();
146 item
= GetLastChild(item
) )
150 // last parameter is "true" to get only the dimensions of the text
151 // label, we don't want to get the entire item width as it's determined
152 // by the current size
153 if ( GetBoundingRect(item
, rect
, true) )
155 if ( size
.x
< rect
.x
+ rect
.width
)
156 size
.x
= rect
.x
+ rect
.width
;
157 if ( size
.y
< rect
.y
+ rect
.height
)
158 size
.y
= rect
.y
+ rect
.height
;
162 else // use precise, if potentially slow, size computation method
164 // iterate over all items recursively
165 wxTreeItemId idRoot
= GetRootItem();
167 wxGetBestTreeSize(this, idRoot
, size
);
170 // need some minimal size even for empty tree
171 if ( !size
.x
|| !size
.y
)
172 size
= wxControl::DoGetBestSize();
176 size
+= GetWindowBorderSize();
184 void wxTreeCtrlBase::ExpandAll()
189 ExpandAllChildren(GetRootItem());
192 void wxTreeCtrlBase::ExpandAllChildren(const wxTreeItemId
& item
)
194 // expand this item first, this might result in its children being added on
196 if ( item
!= GetRootItem() || !HasFlag(wxTR_HIDE_ROOT
) )
198 //else: expanding hidden root item is unsupported and unnecessary
200 // then (recursively) expand all the children
201 wxTreeItemIdValue cookie
;
202 for ( wxTreeItemId idCurr
= GetFirstChild(item
, cookie
);
204 idCurr
= GetNextChild(item
, cookie
) )
206 ExpandAllChildren(idCurr
);
210 void wxTreeCtrlBase::CollapseAll()
215 CollapseAllChildren(GetRootItem());
218 void wxTreeCtrlBase::CollapseAllChildren(const wxTreeItemId
& item
)
220 // first (recursively) collapse all the children
221 wxTreeItemIdValue cookie
;
222 for ( wxTreeItemId idCurr
= GetFirstChild(item
, cookie
);
224 idCurr
= GetNextChild(item
, cookie
) )
226 CollapseAllChildren(idCurr
);
229 // then collapse this element too
233 bool wxTreeCtrlBase::IsEmpty() const
235 return !GetRootItem().IsOk();
238 #endif // wxUSE_TREECTRL