]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treelay.cpp
compilation fix
[wxWidgets.git] / src / generic / treelay.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: treelay.h
3 // Purpose: wxTreeLayout class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 7/4/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "wxtree.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/dc.h"
25 #include "wx/event.h"
26 #endif
27
28 #if wxUSE_TREELAYOUT
29
30 #include "wx/treelay.h"
31
32 /*
33 * Abstract tree
34 *
35 */
36
37 IMPLEMENT_ABSTRACT_CLASS(wxTreeLayout, wxObject)
38
39 wxTreeLayout::wxTreeLayout()
40 {
41 m_xSpacing = 16;
42 m_ySpacing = 20;
43 m_topMargin = 5;
44 m_leftMargin = 5;
45 m_orientation = FALSE;
46 m_parentNode = 0;
47 }
48
49 void wxTreeLayout::DoLayout(wxDC& dc, long topId)
50 {
51 if (topId != -1)
52 SetTopNode(topId);
53
54 long actualTopId = GetTopNode();
55 long id = actualTopId;
56 while (id != -1)
57 {
58 SetNodeX(id, 0);
59 SetNodeY(id, 0);
60 ActivateNode(id, FALSE);
61 id = GetNextNode(id);
62 }
63 m_lastY = m_topMargin;
64 m_lastX = m_leftMargin;
65 CalcLayout(actualTopId, 0, dc);
66 }
67
68 void wxTreeLayout::Draw(wxDC& dc)
69 {
70 dc.Clear();
71 DrawBranches(dc);
72 DrawNodes(dc);
73 }
74
75 void wxTreeLayout::DrawNodes(wxDC& dc)
76 {
77 long id = GetTopNode();
78 while (id != -1)
79 {
80 if (NodeActive(id))
81 DrawNode(id, dc);
82 id = GetNextNode(id);
83 }
84 }
85
86 void wxTreeLayout::DrawBranches(wxDC& dc)
87 {
88 long id = GetTopNode();
89 while (id != -1)
90 {
91 if (GetNodeParent(id) > -1)
92 {
93 long parent = GetNodeParent(id);
94 if (NodeActive(parent))
95 DrawBranch(parent, id, dc);
96 }
97 id = GetNextNode(id);
98 }
99 }
100
101 void wxTreeLayout::DrawNode(long id, wxDC& dc)
102 {
103 wxChar buf[80];
104 wxString name(GetNodeName(id));
105 if (name != wxT(""))
106 wxSprintf(buf, wxT("%s"), (const wxChar*) name);
107 else
108 wxSprintf(buf, wxT("<unnamed>"));
109
110 long x = 80;
111 long y = 20;
112 dc.GetTextExtent(buf, &x, &y);
113 dc.DrawText(buf, GetNodeX(id), (long)(GetNodeY(id) - (y/2.0)));
114 }
115
116 void wxTreeLayout::DrawBranch(long from, long to, wxDC& dc)
117 {
118 long w, h;
119 GetNodeSize(from, &w, &h, dc);
120 dc.DrawLine(GetNodeX(from)+w, GetNodeY(from),
121 GetNodeX(to), GetNodeY(to));
122 }
123
124 void wxTreeLayout::Initialize(void)
125 {
126 }
127
128 void wxTreeLayout::GetNodeSize(long id, long *x, long *y, wxDC& dc)
129 {
130 wxString name(GetNodeName(id));
131 if (name != wxT(""))
132 dc.GetTextExtent(name, x, y);
133 else
134 {
135 *x = 70; *y = 20;
136 }
137 }
138
139 void wxTreeLayout::CalcLayout(long nodeId, int level, wxDC& dc)
140 {
141 wxList children;
142 GetChildren(nodeId, children);
143 int n = children.Number();
144
145 if (m_orientation == FALSE)
146 {
147 // Left to right
148 // X Calculations
149 if (level == 0)
150 SetNodeX(nodeId, m_leftMargin);
151 else
152 {
153 long x = 0;
154 long y = 0;
155 long parentId = GetNodeParent(nodeId);
156 if (parentId != -1)
157 GetNodeSize(parentId, &x, &y, dc);
158 SetNodeX(nodeId, (long)(GetNodeX(parentId) + m_xSpacing + x));
159 }
160
161 wxNode *node = children.First();
162 while (node)
163 {
164 CalcLayout((long)node->Data(), level+1, dc);
165 node = node->Next();
166 }
167
168 // Y Calculations
169 long averageY;
170 ActivateNode(nodeId, TRUE);
171
172 if (n > 0)
173 {
174 averageY = 0;
175 node = children.First();
176 while (node)
177 {
178 averageY += GetNodeY((long)node->Data());
179 node = node->Next();
180 }
181 averageY = averageY / n;
182 SetNodeY(nodeId, averageY);
183 }
184 else
185 {
186 SetNodeY(nodeId, m_lastY);
187 long x, y;
188 GetNodeSize(nodeId, &x, &y, dc);
189
190 m_lastY = m_lastY + y + m_ySpacing;
191 }
192 }
193 else
194 {
195 // Top to bottom
196
197 // Y Calculations
198 if (level == 0)
199 SetNodeY(nodeId, m_topMargin);
200 else
201 {
202 long x = 0;
203 long y = 0;
204 long parentId = GetNodeParent(nodeId);
205 if (parentId != -1)
206 GetNodeSize(parentId, &x, &y, dc);
207 SetNodeY(nodeId, (long)(GetNodeY(parentId) + m_ySpacing + y));
208 }
209
210 wxNode *node = children.First();
211 while (node)
212 {
213 CalcLayout((long)node->Data(), level+1, dc);
214 node = node->Next();
215 }
216
217 // X Calculations
218 long averageX;
219 ActivateNode(nodeId, TRUE);
220
221 if (n > 0)
222 {
223 averageX = 0;
224 node = children.First();
225 while (node)
226 {
227 averageX += GetNodeX((long)node->Data());
228 node = node->Next();
229 }
230 averageX = averageX / n;
231 SetNodeX(nodeId, averageX);
232 }
233 else
234 {
235 SetNodeX(nodeId, m_lastX);
236 long x, y;
237 GetNodeSize(nodeId, &x, &y, dc);
238
239 m_lastX = m_lastX + x + m_xSpacing;
240 }
241 }
242 }
243
244 /*
245 * Tree with storage
246 *
247 */
248
249 IMPLEMENT_DYNAMIC_CLASS(wxTreeLayoutStored, wxTreeLayout)
250
251 wxTreeLayoutStored::wxTreeLayoutStored(int n):wxTreeLayout()
252 {
253 m_nodes = NULL;
254 m_maxNodes = 0;
255 Initialize(n);
256 }
257
258 wxTreeLayoutStored::~wxTreeLayoutStored(void)
259 {
260 if (m_nodes)
261 delete[] m_nodes;
262 }
263
264 void wxTreeLayoutStored::Initialize(int n)
265 {
266 m_maxNodes = n;
267 wxTreeLayout::Initialize();
268 if (m_nodes) delete[] m_nodes;
269 m_nodes = new wxStoredNode[m_maxNodes];
270 int i;
271 for (i = 0; i < n; i++)
272 {
273 m_nodes[i].m_name = "";
274 m_nodes[i].m_active = FALSE;
275 m_nodes[i].m_parentId = -1;
276 m_nodes[i].m_x = 0;
277 m_nodes[i].m_y = 0;
278 }
279 m_num = 0;
280 }
281
282 long wxTreeLayoutStored::AddChild(const wxString& name, const wxString& parent)
283 {
284 if (m_num < (m_maxNodes -1 ))
285 {
286 long i = -1;
287 if (parent != wxT(""))
288 i = NameToId(parent);
289 else m_parentNode = m_num;
290
291 m_nodes[m_num].m_parentId = i;
292 m_nodes[m_num].m_name = name;
293 m_nodes[m_num].m_x = m_nodes[m_num].m_y = 0;
294 m_nodes[m_num].m_clientData = 0;
295 m_num ++;
296
297 return (m_num - 1);
298 }
299 else
300 return -1;
301 }
302
303 long wxTreeLayoutStored::NameToId(const wxString& name)
304 {
305 long i;
306 for (i = 0; i < m_num; i++)
307 if (name == m_nodes[i].m_name)
308 return i;
309 return -1;
310 }
311
312 void wxTreeLayoutStored::GetChildren(long id, wxList& list)
313 {
314 long currentId = GetTopNode();
315 while (currentId != -1)
316 {
317 if (id == GetNodeParent(currentId))
318 list.Append((wxObject *)currentId);
319 currentId = GetNextNode(currentId);
320 }
321 }
322
323 wxStoredNode* wxTreeLayoutStored::GetNode(long idx) const
324 {
325 wxASSERT(idx < m_num);
326
327 return &m_nodes[idx];
328 };
329
330 long wxTreeLayoutStored::GetNodeX(long id)
331 {
332 wxASSERT(id < m_num);
333
334 return (long)m_nodes[id].m_x;
335 }
336
337 long wxTreeLayoutStored::GetNodeY(long id)
338 {
339 wxASSERT(id < m_num);
340
341 return (long)m_nodes[id].m_y;
342 }
343
344 void wxTreeLayoutStored::SetNodeX(long id, long x)
345 {
346 wxASSERT(id < m_num);
347
348 m_nodes[id].m_x = (int)x;
349 }
350
351 void wxTreeLayoutStored::SetNodeY(long id, long y)
352 {
353 wxASSERT(id < m_num);
354
355 m_nodes[id].m_y = (int)y;
356 }
357
358 void wxTreeLayoutStored::SetNodeName(long id, const wxString& name)
359 {
360 wxASSERT(id < m_num);
361
362 m_nodes[id].m_name = name;
363 }
364
365 wxString wxTreeLayoutStored::GetNodeName(long id)
366 {
367 wxASSERT(id < m_num);
368
369 return m_nodes[id].m_name;
370 }
371
372 long wxTreeLayoutStored::GetNodeParent(long id)
373 {
374 if (id != -1)
375 {
376 wxASSERT(id < m_num);
377
378 return m_nodes[id].m_parentId;
379 }
380 else
381 return -1;
382 }
383
384 long wxTreeLayoutStored::GetNextNode(long id)
385 {
386 wxASSERT(id < m_num);
387
388 if ((id != -1) && (id < (m_num - 1)))
389 return id + 1;
390 else
391 return -1;
392 }
393
394 void wxTreeLayoutStored::SetClientData(long id, long clientData)
395 {
396 wxASSERT(id < m_num);
397
398 m_nodes[id].m_clientData = clientData;
399 }
400
401 long wxTreeLayoutStored::GetClientData(long id) const
402 {
403 wxASSERT(id < m_num);
404
405 return m_nodes[id].m_clientData;
406 }
407
408 void wxTreeLayoutStored::ActivateNode(long id, bool active)
409 {
410 wxASSERT(id < m_num);
411
412 m_nodes[id].m_active = active;
413 }
414
415 bool wxTreeLayoutStored::NodeActive(long id)
416 {
417 wxASSERT(id < m_num);
418
419 return m_nodes[id].m_active;
420 }
421
422 wxString wxTreeLayoutStored::HitTest(wxMouseEvent& event, wxDC& dc)
423 {
424 wxPoint pt = event.GetPosition();
425 wxCoord x = pt.x;
426 wxCoord y = pt.y;
427
428 int i;
429 for (i = 0; i < m_maxNodes; i++)
430 {
431 long width, height;
432 dc.GetTextExtent(m_nodes[i].m_name, &width, &height);
433
434 if ( (x >= (m_nodes[i].m_x-10)) && (x < (m_nodes[i].m_x + width+10)) &&
435 (y >= m_nodes[i].m_y-10) && (y < (m_nodes[i].m_y + height+10)) )
436 {
437 return m_nodes[i].m_name;
438 }
439 }
440
441 return wxString("");
442 }
443
444 #endif
445 // wxUSE_TREELAYOUT