]> git.saurik.com Git - wxWidgets.git/blame - src/common/treebase.cpp
graphics context additions and merging graphics bitmap additions from Kevin O.
[wxWidgets.git] / src / common / treebase.cpp
CommitLineData
484523cf
JS
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
65571936 9// Licence: wxWindows licence
484523cf
JS
10/////////////////////////////////////////////////////////////////////////////
11
12// =============================================================================
13// declarations
14// =============================================================================
15
16// -----------------------------------------------------------------------------
17// headers
18// -----------------------------------------------------------------------------
19
484523cf
JS
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
1e6feb95 24 #pragma hdrstop
484523cf
JS
25#endif
26
1e6feb95
VZ
27#if wxUSE_TREECTRL
28
8cee4a30 29#include "wx/treectrl.h"
42e53e77 30#include "wx/imaglist.h"
ed2ec17c
GT
31
32// ----------------------------------------------------------------------------
33// events
34// ----------------------------------------------------------------------------
35
36DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_DRAG)
37DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_RDRAG)
38DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT)
39DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_END_LABEL_EDIT)
40DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_DELETE_ITEM)
41DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_GET_INFO)
42DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_SET_INFO)
43DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_EXPANDED)
44DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_EXPANDING)
45DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_COLLAPSED)
46DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_COLLAPSING)
47DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_SEL_CHANGED)
48DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_SEL_CHANGING)
49DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_KEY_DOWN)
50DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_ACTIVATED)
51DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK)
52DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK)
53DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_END_DRAG)
ae8c4b33 54DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK)
156194e1 55DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP)
f7c6f947 56DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_MENU)
ed2ec17c 57
484523cf
JS
58// ----------------------------------------------------------------------------
59// Tree event
60// ----------------------------------------------------------------------------
61
09f277d6 62IMPLEMENT_ABSTRACT_CLASS(wxTreeEvent, wxNotifyEvent)
b8fbf1a0
RD
63
64
09f277d6
VZ
65wxTreeEvent::wxTreeEvent(wxEventType commandType,
66 wxTreeCtrlBase *tree,
67 const wxTreeItemId& item)
68 : wxNotifyEvent(commandType, tree->GetId()),
69 m_item(item)
484523cf 70{
cb719f2e 71 m_editCancelled = false;
09f277d6
VZ
72
73 SetEventObject(tree);
74
75 if ( item.IsOk() )
76 SetClientObject(tree->GetItemData(item));
484523cf
JS
77}
78
49b6ebb7
RD
79wxTreeEvent::wxTreeEvent(wxEventType commandType, int id)
80 : wxNotifyEvent(commandType, id)
81{
82 m_itemOld = 0l;
83 m_editCancelled = false;
84}
85
0cd936a4
WS
86wxTreeEvent::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
8cee4a30
VZ
97// ----------------------------------------------------------------------------
98// wxTreeCtrlBase
99// ----------------------------------------------------------------------------
100
101wxTreeCtrlBase::~wxTreeCtrlBase()
102{
103 if (m_ownsImageListNormal)
104 delete m_imageListNormal;
105 if (m_ownsImageListState)
106 delete m_imageListState;
107}
108
fbf8436c
VZ
109static void
110wxGetBestTreeSize(const wxTreeCtrlBase* treeCtrl, wxTreeItemId id, wxSize& size)
7c384067
JS
111{
112 wxRect rect;
70934138 113
fbf8436c 114 if ( treeCtrl->GetBoundingRect(id, rect, true /* just the item */) )
7c384067 115 {
9e7642ae 116 // Translate to logical position so we get the full extent
9766efc3 117#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
9e7642ae
JS
118 rect.x += treeCtrl->GetScrollPos(wxHORIZONTAL);
119 rect.y += treeCtrl->GetScrollPos(wxVERTICAL);
9766efc3 120#endif
9e7642ae 121
fbf8436c 122 size.IncTo(wxSize(rect.GetRight(), rect.GetBottom()));
7c384067
JS
123 }
124
125 wxTreeItemIdValue cookie;
126 for ( wxTreeItemId item = treeCtrl->GetFirstChild(id, cookie);
127 item.IsOk();
3e748fca 128 item = treeCtrl->GetNextChild(id, cookie) )
7c384067
JS
129 {
130 wxGetBestTreeSize(treeCtrl, item, size);
131 }
132}
133
8cee4a30
VZ
134wxSize wxTreeCtrlBase::DoGetBestSize() const
135{
136 wxSize size;
137
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
70934138 141
7c384067 142 if (GetQuickBestSize())
8cee4a30 143 {
7c384067
JS
144 for ( wxTreeItemId item = GetRootItem();
145 item.IsOk();
146 item = GetLastChild(item) )
8cee4a30 147 {
7c384067
JS
148 wxRect rect;
149
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) )
154 {
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;
159 }
8cee4a30
VZ
160 }
161 }
fbf8436c
VZ
162 else // use precise, if potentially slow, size computation method
163 {
164 // iterate over all items recursively
16f32ee7
VZ
165 wxTreeItemId idRoot = GetRootItem();
166 if ( idRoot.IsOk() )
167 wxGetBestTreeSize(this, idRoot, size);
fbf8436c
VZ
168 }
169
8cee4a30
VZ
170 // need some minimal size even for empty tree
171 if ( !size.x || !size.y )
172 size = wxControl::DoGetBestSize();
b24069f2 173 else
54a4121a
VZ
174 {
175 // Add border size
176 size += GetWindowBorderSize();
177
b24069f2 178 CacheBestSize(size);
54a4121a 179 }
8cee4a30
VZ
180
181 return size;
182}
183
70934138
VZ
184void wxTreeCtrlBase::ExpandAll()
185{
9248adc8
VZ
186 if ( IsEmpty() )
187 return;
188
70934138
VZ
189 ExpandAllChildren(GetRootItem());
190}
191
192void wxTreeCtrlBase::ExpandAllChildren(const wxTreeItemId& item)
193{
624f89c2 194 Freeze();
70934138
VZ
195 // expand this item first, this might result in its children being added on
196 // the fly
e660f922
VZ
197 if ( item != GetRootItem() || !HasFlag(wxTR_HIDE_ROOT) )
198 Expand(item);
199 //else: expanding hidden root item is unsupported and unnecessary
70934138
VZ
200
201 // then (recursively) expand all the children
202 wxTreeItemIdValue cookie;
203 for ( wxTreeItemId idCurr = GetFirstChild(item, cookie);
204 idCurr.IsOk();
205 idCurr = GetNextChild(item, cookie) )
206 {
207 ExpandAllChildren(idCurr);
208 }
624f89c2 209 Thaw();
70934138
VZ
210}
211
9248adc8
VZ
212void wxTreeCtrlBase::CollapseAll()
213{
214 if ( IsEmpty() )
215 return;
216
217 CollapseAllChildren(GetRootItem());
218}
219
220void wxTreeCtrlBase::CollapseAllChildren(const wxTreeItemId& item)
221{
624f89c2 222 Freeze();
9248adc8
VZ
223 // first (recursively) collapse all the children
224 wxTreeItemIdValue cookie;
225 for ( wxTreeItemId idCurr = GetFirstChild(item, cookie);
226 idCurr.IsOk();
227 idCurr = GetNextChild(item, cookie) )
228 {
229 CollapseAllChildren(idCurr);
230 }
231
232 // then collapse this element too
233 Collapse(item);
624f89c2 234 Thaw();
9248adc8
VZ
235}
236
237bool wxTreeCtrlBase::IsEmpty() const
238{
239 return !GetRootItem().IsOk();
240}
241
1e6feb95
VZ
242#endif // wxUSE_TREECTRL
243