Fix tree size calculation by using logical item position
[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 static void
110 wxGetBestTreeSize(const wxTreeCtrlBase* treeCtrl, wxTreeItemId id, wxSize& size)
111 {
112 wxRect rect;
113
114 if ( treeCtrl->GetBoundingRect(id, rect, true /* just the item */) )
115 {
116 // Translate to logical position so we get the full extent
117 rect.x += treeCtrl->GetScrollPos(wxHORIZONTAL);
118 rect.y += treeCtrl->GetScrollPos(wxVERTICAL);
119
120 size.IncTo(wxSize(rect.GetRight(), rect.GetBottom()));
121 }
122
123 wxTreeItemIdValue cookie;
124 for ( wxTreeItemId item = treeCtrl->GetFirstChild(id, cookie);
125 item.IsOk();
126 item = treeCtrl->GetNextChild(id, cookie) )
127 {
128 wxGetBestTreeSize(treeCtrl, item, size);
129 }
130 }
131
132 wxSize wxTreeCtrlBase::DoGetBestSize() const
133 {
134 wxSize size;
135
136 // this doesn't really compute the total bounding rectangle of all items
137 // but a not too bad guess of it which has the advantage of not having to
138 // examine all (potentially hundreds or thousands) items in the control
139
140 if (GetQuickBestSize())
141 {
142 for ( wxTreeItemId item = GetRootItem();
143 item.IsOk();
144 item = GetLastChild(item) )
145 {
146 wxRect rect;
147
148 // last parameter is "true" to get only the dimensions of the text
149 // label, we don't want to get the entire item width as it's determined
150 // by the current size
151 if ( GetBoundingRect(item, rect, true) )
152 {
153 if ( size.x < rect.x + rect.width )
154 size.x = rect.x + rect.width;
155 if ( size.y < rect.y + rect.height )
156 size.y = rect.y + rect.height;
157 }
158 }
159 }
160 else // use precise, if potentially slow, size computation method
161 {
162 // iterate over all items recursively
163 wxGetBestTreeSize(this, GetRootItem(), size);
164 }
165
166 // need some minimal size even for empty tree
167 if ( !size.x || !size.y )
168 size = wxControl::DoGetBestSize();
169 else
170 {
171 // Add border size
172 size += GetWindowBorderSize();
173
174 CacheBestSize(size);
175 }
176
177 return size;
178 }
179
180 void wxTreeCtrlBase::ExpandAll()
181 {
182 ExpandAllChildren(GetRootItem());
183 }
184
185 void wxTreeCtrlBase::ExpandAllChildren(const wxTreeItemId& item)
186 {
187 // expand this item first, this might result in its children being added on
188 // the fly
189 Expand(item);
190
191 // then (recursively) expand all the children
192 wxTreeItemIdValue cookie;
193 for ( wxTreeItemId idCurr = GetFirstChild(item, cookie);
194 idCurr.IsOk();
195 idCurr = GetNextChild(item, cookie) )
196 {
197 ExpandAllChildren(idCurr);
198 }
199 }
200
201 #endif // wxUSE_TREECTRL
202