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 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxTreeEvent
);
37 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_BEGIN_RDRAG
, wxTreeEvent
);
38 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, wxTreeEvent
);
39 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, wxTreeEvent
);
40 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_DELETE_ITEM
, wxTreeEvent
);
41 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_GET_INFO
, wxTreeEvent
);
42 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_SET_INFO
, wxTreeEvent
);
43 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxTreeEvent
);
44 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, wxTreeEvent
);
45 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxTreeEvent
);
46 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, wxTreeEvent
);
47 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_SEL_CHANGED
, wxTreeEvent
);
48 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_SEL_CHANGING
, wxTreeEvent
);
49 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_KEY_DOWN
, wxTreeEvent
);
50 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, wxTreeEvent
);
51 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, wxTreeEvent
);
52 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
, wxTreeEvent
);
53 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_END_DRAG
, wxTreeEvent
);
54 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
);
55 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, wxTreeEvent
);
56 wxDEFINE_EVENT( wxEVT_COMMAND_TREE_ITEM_MENU
, wxTreeEvent
);
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
64 wxTreeEvent::wxTreeEvent(wxEventType commandType
,
66 const wxTreeItemId
& item
)
67 : wxNotifyEvent(commandType
, tree
->GetId()),
70 m_editCancelled
= false;
75 SetClientObject(tree
->GetItemData(item
));
78 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
79 : wxNotifyEvent(commandType
, id
)
82 m_editCancelled
= false;
85 wxTreeEvent::wxTreeEvent(const wxTreeEvent
& event
)
86 : wxNotifyEvent(event
)
88 m_evtKey
= event
.m_evtKey
;
89 m_item
= event
.m_item
;
90 m_itemOld
= event
.m_itemOld
;
91 m_pointDrag
= event
.m_pointDrag
;
92 m_label
= event
.m_label
;
93 m_editCancelled
= event
.m_editCancelled
;
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 wxTreeCtrlBase::~wxTreeCtrlBase()
102 if (m_ownsImageListNormal
)
103 delete m_imageListNormal
;
104 if (m_ownsImageListState
)
105 delete m_imageListState
;
108 void wxTreeCtrlBase::SetItemState(const wxTreeItemId
& item
, int state
)
110 if ( state
== wxTREE_ITEMSTATE_NEXT
)
112 int current
= GetItemState(item
);
113 if ( current
== wxTREE_ITEMSTATE_NONE
)
116 if ( m_imageListState
&& state
>= m_imageListState
->GetImageCount() )
119 else if ( state
== wxTREE_ITEMSTATE_PREV
)
121 int current
= GetItemState(item
);
122 if ( current
== wxTREE_ITEMSTATE_NONE
)
126 state
= m_imageListState
? m_imageListState
->GetImageCount() - 1 : 0;
128 // else: wxTREE_ITEMSTATE_NONE depending on platform
130 DoSetItemState(item
, state
);
134 wxGetBestTreeSize(const wxTreeCtrlBase
* treeCtrl
, wxTreeItemId id
, wxSize
& size
)
138 if ( treeCtrl
->GetBoundingRect(id
, rect
, true /* just the item */) )
140 // Translate to logical position so we get the full extent
141 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
142 rect
.x
+= treeCtrl
->GetScrollPos(wxHORIZONTAL
);
143 rect
.y
+= treeCtrl
->GetScrollPos(wxVERTICAL
);
146 size
.IncTo(wxSize(rect
.GetRight(), rect
.GetBottom()));
149 wxTreeItemIdValue cookie
;
150 for ( wxTreeItemId item
= treeCtrl
->GetFirstChild(id
, cookie
);
152 item
= treeCtrl
->GetNextChild(id
, cookie
) )
154 wxGetBestTreeSize(treeCtrl
, item
, size
);
158 wxSize
wxTreeCtrlBase::DoGetBestSize() const
162 // this doesn't really compute the total bounding rectangle of all items
163 // but a not too bad guess of it which has the advantage of not having to
164 // examine all (potentially hundreds or thousands) items in the control
166 if (GetQuickBestSize())
168 for ( wxTreeItemId item
= GetRootItem();
170 item
= GetLastChild(item
) )
174 // last parameter is "true" to get only the dimensions of the text
175 // label, we don't want to get the entire item width as it's determined
176 // by the current size
177 if ( GetBoundingRect(item
, rect
, true) )
179 if ( size
.x
< rect
.x
+ rect
.width
)
180 size
.x
= rect
.x
+ rect
.width
;
181 if ( size
.y
< rect
.y
+ rect
.height
)
182 size
.y
= rect
.y
+ rect
.height
;
186 else // use precise, if potentially slow, size computation method
188 // iterate over all items recursively
189 wxTreeItemId idRoot
= GetRootItem();
191 wxGetBestTreeSize(this, idRoot
, size
);
194 // need some minimal size even for empty tree
195 if ( !size
.x
|| !size
.y
)
196 size
= wxControl::DoGetBestSize();
200 size
+= GetWindowBorderSize();
208 void wxTreeCtrlBase::ExpandAll()
213 ExpandAllChildren(GetRootItem());
216 void wxTreeCtrlBase::ExpandAllChildren(const wxTreeItemId
& item
)
219 // expand this item first, this might result in its children being added on
221 if ( item
!= GetRootItem() || !HasFlag(wxTR_HIDE_ROOT
) )
223 //else: expanding hidden root item is unsupported and unnecessary
225 // then (recursively) expand all the children
226 wxTreeItemIdValue cookie
;
227 for ( wxTreeItemId idCurr
= GetFirstChild(item
, cookie
);
229 idCurr
= GetNextChild(item
, cookie
) )
231 ExpandAllChildren(idCurr
);
236 void wxTreeCtrlBase::CollapseAll()
241 CollapseAllChildren(GetRootItem());
244 void wxTreeCtrlBase::CollapseAllChildren(const wxTreeItemId
& item
)
247 // first (recursively) collapse all the children
248 wxTreeItemIdValue cookie
;
249 for ( wxTreeItemId idCurr
= GetFirstChild(item
, cookie
);
251 idCurr
= GetNextChild(item
, cookie
) )
253 CollapseAllChildren(idCurr
);
256 // then collapse this element too
261 bool wxTreeCtrlBase::IsEmpty() const
263 return !GetRootItem().IsOk();
266 #endif // wxUSE_TREECTRL