fix wxTimerEvent and wxTreeCtrl to use IMPLEMENT_DYNAMIC_CLASS macro
[wxWidgets.git] / src / common / treebase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: treebase.cpp
3 // Purpose: Base wxTreeCtrl classes
4 // Author: Julian Smart
5 // Created: 01/02/97
6 // Modified:
7 // Id: $Id$
8 // Copyright: (c) 1998 Robert Roebling, Julian Smart et al
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // =============================================================================
13 // declarations
14 // =============================================================================
15
16 // -----------------------------------------------------------------------------
17 // headers
18 // -----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_TREECTRL
28
29 #include "wx/treectrl.h"
30 #include "wx/imaglist.h"
31
32 // ----------------------------------------------------------------------------
33 // events
34 // ----------------------------------------------------------------------------
35
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)
57
58 // ----------------------------------------------------------------------------
59 // Tree event
60 // ----------------------------------------------------------------------------
61
62 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
63
64 wxTreeEvent::wxTreeEvent(wxEventType commandType,
65 wxTreeCtrlBase *tree,
66 const wxTreeItemId& item)
67 : wxNotifyEvent(commandType, tree->GetId()),
68 m_item(item)
69 {
70 m_editCancelled = false;
71
72 SetEventObject(tree);
73
74 if ( item.IsOk() )
75 SetClientObject(tree->GetItemData(item));
76 }
77
78 wxTreeEvent::wxTreeEvent(wxEventType commandType, int id)
79 : wxNotifyEvent(commandType, id)
80 {
81 m_itemOld = 0l;
82 m_editCancelled = false;
83 }
84
85 wxTreeEvent::wxTreeEvent(const wxTreeEvent & event)
86 : wxNotifyEvent(event)
87 {
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;
94 }
95
96 // ----------------------------------------------------------------------------
97 // wxTreeCtrlBase
98 // ----------------------------------------------------------------------------
99
100 wxTreeCtrlBase::~wxTreeCtrlBase()
101 {
102 if (m_ownsImageListNormal)
103 delete m_imageListNormal;
104 if (m_ownsImageListState)
105 delete m_imageListState;
106 }
107
108 void wxTreeCtrlBase::SetItemState(const wxTreeItemId& item, int state)
109 {
110 if ( state == wxTREE_ITEMSTATE_NEXT )
111 {
112 int current = GetItemState(item);
113 if ( current == wxTREE_ITEMSTATE_NONE )
114 return;
115 state = current + 1;
116 if ( m_imageListState && state >= m_imageListState->GetImageCount() )
117 state = 0;
118 }
119 else if ( state == wxTREE_ITEMSTATE_PREV )
120 {
121 int current = GetItemState(item);
122 if ( current == wxTREE_ITEMSTATE_NONE )
123 return;
124 state = current - 1;
125 if ( state == -1 )
126 state = m_imageListState ? m_imageListState->GetImageCount() - 1 : 0;
127 }
128 // else: wxTREE_ITEMSTATE_NONE depending on platform
129
130 DoSetItemState(item, state);
131 }
132
133 static void
134 wxGetBestTreeSize(const wxTreeCtrlBase* treeCtrl, wxTreeItemId id, wxSize& size)
135 {
136 wxRect rect;
137
138 if ( treeCtrl->GetBoundingRect(id, rect, true /* just the item */) )
139 {
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);
144 #endif
145
146 size.IncTo(wxSize(rect.GetRight(), rect.GetBottom()));
147 }
148
149 wxTreeItemIdValue cookie;
150 for ( wxTreeItemId item = treeCtrl->GetFirstChild(id, cookie);
151 item.IsOk();
152 item = treeCtrl->GetNextChild(id, cookie) )
153 {
154 wxGetBestTreeSize(treeCtrl, item, size);
155 }
156 }
157
158 wxSize wxTreeCtrlBase::DoGetBestSize() const
159 {
160 wxSize size;
161
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
165
166 if (GetQuickBestSize())
167 {
168 for ( wxTreeItemId item = GetRootItem();
169 item.IsOk();
170 item = GetLastChild(item) )
171 {
172 wxRect rect;
173
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) )
178 {
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;
183 }
184 }
185 }
186 else // use precise, if potentially slow, size computation method
187 {
188 // iterate over all items recursively
189 wxTreeItemId idRoot = GetRootItem();
190 if ( idRoot.IsOk() )
191 wxGetBestTreeSize(this, idRoot, size);
192 }
193
194 // need some minimal size even for empty tree
195 if ( !size.x || !size.y )
196 size = wxControl::DoGetBestSize();
197 else
198 {
199 // Add border size
200 size += GetWindowBorderSize();
201
202 CacheBestSize(size);
203 }
204
205 return size;
206 }
207
208 void wxTreeCtrlBase::ExpandAll()
209 {
210 if ( IsEmpty() )
211 return;
212
213 ExpandAllChildren(GetRootItem());
214 }
215
216 void wxTreeCtrlBase::ExpandAllChildren(const wxTreeItemId& item)
217 {
218 Freeze();
219 // expand this item first, this might result in its children being added on
220 // the fly
221 if ( item != GetRootItem() || !HasFlag(wxTR_HIDE_ROOT) )
222 Expand(item);
223 //else: expanding hidden root item is unsupported and unnecessary
224
225 // then (recursively) expand all the children
226 wxTreeItemIdValue cookie;
227 for ( wxTreeItemId idCurr = GetFirstChild(item, cookie);
228 idCurr.IsOk();
229 idCurr = GetNextChild(item, cookie) )
230 {
231 ExpandAllChildren(idCurr);
232 }
233 Thaw();
234 }
235
236 void wxTreeCtrlBase::CollapseAll()
237 {
238 if ( IsEmpty() )
239 return;
240
241 CollapseAllChildren(GetRootItem());
242 }
243
244 void wxTreeCtrlBase::CollapseAllChildren(const wxTreeItemId& item)
245 {
246 Freeze();
247 // first (recursively) collapse all the children
248 wxTreeItemIdValue cookie;
249 for ( wxTreeItemId idCurr = GetFirstChild(item, cookie);
250 idCurr.IsOk();
251 idCurr = GetNextChild(item, cookie) )
252 {
253 CollapseAllChildren(idCurr);
254 }
255
256 // then collapse this element too
257 Collapse(item);
258 Thaw();
259 }
260
261 bool wxTreeCtrlBase::IsEmpty() const
262 {
263 return !GetRootItem().IsOk();
264 }
265
266 #endif // wxUSE_TREECTRL