#9591: Item state (icons) for wxTreeCtrl on any platform
[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_ABSTRACT_CLASS(wxTreeEvent, wxNotifyEvent)
63
64
65 wxTreeEvent::wxTreeEvent(wxEventType commandType,
66 wxTreeCtrlBase *tree,
67 const wxTreeItemId& item)
68 : wxNotifyEvent(commandType, tree->GetId()),
69 m_item(item)
70 {
71 m_editCancelled = false;
72
73 SetEventObject(tree);
74
75 if ( item.IsOk() )
76 SetClientObject(tree->GetItemData(item));
77 }
78
79 wxTreeEvent::wxTreeEvent(wxEventType commandType, int id)
80 : wxNotifyEvent(commandType, id)
81 {
82 m_itemOld = 0l;
83 m_editCancelled = false;
84 }
85
86 wxTreeEvent::wxTreeEvent(const wxTreeEvent & event)
87 : wxNotifyEvent(event)
88 {
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;
95 }
96
97 // ----------------------------------------------------------------------------
98 // wxTreeCtrlBase
99 // ----------------------------------------------------------------------------
100
101 wxTreeCtrlBase::~wxTreeCtrlBase()
102 {
103 if (m_ownsImageListNormal)
104 delete m_imageListNormal;
105 if (m_ownsImageListState)
106 delete m_imageListState;
107 }
108
109 void wxTreeCtrlBase::SetItemState(const wxTreeItemId& item, int state)
110 {
111 if ( state == wxTREE_ITEMSTATE_NEXT )
112 {
113 state = GetItemState(item) + 1;
114 if ( m_imageListState && state >= m_imageListState->GetImageCount() )
115 state = 0;
116 }
117 else if ( state == wxTREE_ITEMSTATE_PREV )
118 {
119 state = GetItemState(item) - 1;
120 if ( state == -1 )
121 state = m_imageListState ? m_imageListState->GetImageCount() - 1 : 0;
122 }
123 // else: wxTREE_ITEMSTATE_NONE depending on platform
124
125 DoSetItemState(item, state);
126 }
127
128 static void
129 wxGetBestTreeSize(const wxTreeCtrlBase* treeCtrl, wxTreeItemId id, wxSize& size)
130 {
131 wxRect rect;
132
133 if ( treeCtrl->GetBoundingRect(id, rect, true /* just the item */) )
134 {
135 // Translate to logical position so we get the full extent
136 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
137 rect.x += treeCtrl->GetScrollPos(wxHORIZONTAL);
138 rect.y += treeCtrl->GetScrollPos(wxVERTICAL);
139 #endif
140
141 size.IncTo(wxSize(rect.GetRight(), rect.GetBottom()));
142 }
143
144 wxTreeItemIdValue cookie;
145 for ( wxTreeItemId item = treeCtrl->GetFirstChild(id, cookie);
146 item.IsOk();
147 item = treeCtrl->GetNextChild(id, cookie) )
148 {
149 wxGetBestTreeSize(treeCtrl, item, size);
150 }
151 }
152
153 wxSize wxTreeCtrlBase::DoGetBestSize() const
154 {
155 wxSize size;
156
157 // this doesn't really compute the total bounding rectangle of all items
158 // but a not too bad guess of it which has the advantage of not having to
159 // examine all (potentially hundreds or thousands) items in the control
160
161 if (GetQuickBestSize())
162 {
163 for ( wxTreeItemId item = GetRootItem();
164 item.IsOk();
165 item = GetLastChild(item) )
166 {
167 wxRect rect;
168
169 // last parameter is "true" to get only the dimensions of the text
170 // label, we don't want to get the entire item width as it's determined
171 // by the current size
172 if ( GetBoundingRect(item, rect, true) )
173 {
174 if ( size.x < rect.x + rect.width )
175 size.x = rect.x + rect.width;
176 if ( size.y < rect.y + rect.height )
177 size.y = rect.y + rect.height;
178 }
179 }
180 }
181 else // use precise, if potentially slow, size computation method
182 {
183 // iterate over all items recursively
184 wxTreeItemId idRoot = GetRootItem();
185 if ( idRoot.IsOk() )
186 wxGetBestTreeSize(this, idRoot, size);
187 }
188
189 // need some minimal size even for empty tree
190 if ( !size.x || !size.y )
191 size = wxControl::DoGetBestSize();
192 else
193 {
194 // Add border size
195 size += GetWindowBorderSize();
196
197 CacheBestSize(size);
198 }
199
200 return size;
201 }
202
203 void wxTreeCtrlBase::ExpandAll()
204 {
205 if ( IsEmpty() )
206 return;
207
208 ExpandAllChildren(GetRootItem());
209 }
210
211 void wxTreeCtrlBase::ExpandAllChildren(const wxTreeItemId& item)
212 {
213 Freeze();
214 // expand this item first, this might result in its children being added on
215 // the fly
216 if ( item != GetRootItem() || !HasFlag(wxTR_HIDE_ROOT) )
217 Expand(item);
218 //else: expanding hidden root item is unsupported and unnecessary
219
220 // then (recursively) expand all the children
221 wxTreeItemIdValue cookie;
222 for ( wxTreeItemId idCurr = GetFirstChild(item, cookie);
223 idCurr.IsOk();
224 idCurr = GetNextChild(item, cookie) )
225 {
226 ExpandAllChildren(idCurr);
227 }
228 Thaw();
229 }
230
231 void wxTreeCtrlBase::CollapseAll()
232 {
233 if ( IsEmpty() )
234 return;
235
236 CollapseAllChildren(GetRootItem());
237 }
238
239 void wxTreeCtrlBase::CollapseAllChildren(const wxTreeItemId& item)
240 {
241 Freeze();
242 // first (recursively) collapse all the children
243 wxTreeItemIdValue cookie;
244 for ( wxTreeItemId idCurr = GetFirstChild(item, cookie);
245 idCurr.IsOk();
246 idCurr = GetNextChild(item, cookie) )
247 {
248 CollapseAllChildren(idCurr);
249 }
250
251 // then collapse this element too
252 Collapse(item);
253 Thaw();
254 }
255
256 bool wxTreeCtrlBase::IsEmpty() const
257 {
258 return !GetRootItem().IsOk();
259 }
260
261 #endif // wxUSE_TREECTRL