]>
Commit | Line | Data |
---|---|---|
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 | ||
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) | |
ae8c4b33 | 54 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK) |
156194e1 | 55 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP) |
f7c6f947 | 56 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_MENU) |
ed2ec17c | 57 | |
484523cf JS |
58 | // ---------------------------------------------------------------------------- |
59 | // Tree event | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
09f277d6 | 62 | IMPLEMENT_ABSTRACT_CLASS(wxTreeEvent, wxNotifyEvent) |
b8fbf1a0 RD |
63 | |
64 | ||
09f277d6 VZ |
65 | wxTreeEvent::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 |
79 | wxTreeEvent::wxTreeEvent(wxEventType commandType, int id) |
80 | : wxNotifyEvent(commandType, id) | |
81 | { | |
82 | m_itemOld = 0l; | |
83 | m_editCancelled = false; | |
84 | } | |
85 | ||
0cd936a4 WS |
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 | ||
8cee4a30 VZ |
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 | ||
fbf8436c VZ |
109 | static void |
110 | wxGetBestTreeSize(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 | { |
fbf8436c | 116 | size.IncTo(wxSize(rect.GetRight(), rect.GetBottom())); |
7c384067 JS |
117 | } |
118 | ||
119 | wxTreeItemIdValue cookie; | |
120 | for ( wxTreeItemId item = treeCtrl->GetFirstChild(id, cookie); | |
121 | item.IsOk(); | |
3e748fca | 122 | item = treeCtrl->GetNextChild(id, cookie) ) |
7c384067 JS |
123 | { |
124 | wxGetBestTreeSize(treeCtrl, item, size); | |
125 | } | |
126 | } | |
127 | ||
8cee4a30 VZ |
128 | wxSize wxTreeCtrlBase::DoGetBestSize() const |
129 | { | |
130 | wxSize size; | |
131 | ||
132 | // this doesn't really compute the total bounding rectangle of all items | |
133 | // but a not too bad guess of it which has the advantage of not having to | |
134 | // examine all (potentially hundreds or thousands) items in the control | |
70934138 | 135 | |
7c384067 | 136 | if (GetQuickBestSize()) |
8cee4a30 | 137 | { |
7c384067 JS |
138 | for ( wxTreeItemId item = GetRootItem(); |
139 | item.IsOk(); | |
140 | item = GetLastChild(item) ) | |
8cee4a30 | 141 | { |
7c384067 JS |
142 | wxRect rect; |
143 | ||
144 | // last parameter is "true" to get only the dimensions of the text | |
145 | // label, we don't want to get the entire item width as it's determined | |
146 | // by the current size | |
147 | if ( GetBoundingRect(item, rect, true) ) | |
148 | { | |
149 | if ( size.x < rect.x + rect.width ) | |
150 | size.x = rect.x + rect.width; | |
151 | if ( size.y < rect.y + rect.height ) | |
152 | size.y = rect.y + rect.height; | |
153 | } | |
8cee4a30 VZ |
154 | } |
155 | } | |
fbf8436c VZ |
156 | else // use precise, if potentially slow, size computation method |
157 | { | |
158 | // iterate over all items recursively | |
7c384067 | 159 | wxGetBestTreeSize(this, GetRootItem(), size); |
fbf8436c VZ |
160 | } |
161 | ||
8cee4a30 VZ |
162 | // need some minimal size even for empty tree |
163 | if ( !size.x || !size.y ) | |
164 | size = wxControl::DoGetBestSize(); | |
b24069f2 VZ |
165 | else |
166 | CacheBestSize(size); | |
8cee4a30 VZ |
167 | |
168 | return size; | |
169 | } | |
170 | ||
70934138 VZ |
171 | void wxTreeCtrlBase::ExpandAll() |
172 | { | |
173 | ExpandAllChildren(GetRootItem()); | |
174 | } | |
175 | ||
176 | void wxTreeCtrlBase::ExpandAllChildren(const wxTreeItemId& item) | |
177 | { | |
178 | // expand this item first, this might result in its children being added on | |
179 | // the fly | |
180 | Expand(item); | |
181 | ||
182 | // then (recursively) expand all the children | |
183 | wxTreeItemIdValue cookie; | |
184 | for ( wxTreeItemId idCurr = GetFirstChild(item, cookie); | |
185 | idCurr.IsOk(); | |
186 | idCurr = GetNextChild(item, cookie) ) | |
187 | { | |
188 | ExpandAllChildren(idCurr); | |
189 | } | |
190 | } | |
191 | ||
1e6feb95 VZ |
192 | #endif // wxUSE_TREECTRL |
193 |