]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/treelay.cpp
fa5db2eceae911c21d4f21a26652b0ce08e2e1c8
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeLayout class
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "wxtree.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
30 #include "wx/treelay.h"
37 IMPLEMENT_ABSTRACT_CLASS(wxTreeLayout
, wxObject
)
39 wxTreeLayout::wxTreeLayout()
45 m_orientation
= FALSE
;
49 void wxTreeLayout::DoLayout(wxDC
& dc
, long topId
)
54 long actualTopId
= GetTopNode();
55 long id
= actualTopId
;
60 ActivateNode(id
, FALSE
);
63 m_lastY
= m_topMargin
;
64 m_lastX
= m_leftMargin
;
65 CalcLayout(actualTopId
, 0, dc
);
68 void wxTreeLayout::Draw(wxDC
& dc
)
75 void wxTreeLayout::DrawNodes(wxDC
& dc
)
77 long id
= GetTopNode();
86 void wxTreeLayout::DrawBranches(wxDC
& dc
)
88 long id
= GetTopNode();
91 if (GetNodeParent(id
) > -1)
93 long parent
= GetNodeParent(id
);
94 if (NodeActive(parent
))
95 DrawBranch(parent
, id
, dc
);
101 void wxTreeLayout::DrawNode(long id
, wxDC
& dc
)
104 wxString
name(GetNodeName(id
));
106 wxSprintf(buf
, wxT("%s"), (const wxChar
*) name
);
108 wxSprintf(buf
, wxT("<unnamed>"));
112 dc
.GetTextExtent(buf
, &x
, &y
);
113 dc
.DrawText(buf
, GetNodeX(id
), (long)(GetNodeY(id
) - (y
/2.0)));
116 void wxTreeLayout::DrawBranch(long from
, long to
, wxDC
& dc
)
119 GetNodeSize(from
, &w
, &h
, dc
);
120 dc
.DrawLine(GetNodeX(from
)+w
, GetNodeY(from
),
121 GetNodeX(to
), GetNodeY(to
));
124 void wxTreeLayout::Initialize(void)
128 void wxTreeLayout::GetNodeSize(long id
, long *x
, long *y
, wxDC
& dc
)
130 wxString
name(GetNodeName(id
));
132 dc
.GetTextExtent(name
, x
, y
);
139 void wxTreeLayout::CalcLayout(long nodeId
, int level
, wxDC
& dc
)
142 GetChildren(nodeId
, children
);
143 int n
= children
.Number();
145 if (m_orientation
== FALSE
)
150 SetNodeX(nodeId
, m_leftMargin
);
155 long parentId
= GetNodeParent(nodeId
);
157 GetNodeSize(parentId
, &x
, &y
, dc
);
158 SetNodeX(nodeId
, (long)(GetNodeX(parentId
) + m_xSpacing
+ x
));
161 wxNode
*node
= children
.First();
164 CalcLayout((long)node
->Data(), level
+1, dc
);
170 ActivateNode(nodeId
, TRUE
);
175 node
= children
.First();
178 averageY
+= GetNodeY((long)node
->Data());
181 averageY
= averageY
/ n
;
182 SetNodeY(nodeId
, averageY
);
186 SetNodeY(nodeId
, m_lastY
);
188 GetNodeSize(nodeId
, &x
, &y
, dc
);
190 m_lastY
= m_lastY
+ y
+ m_ySpacing
;
199 SetNodeY(nodeId
, m_topMargin
);
204 long parentId
= GetNodeParent(nodeId
);
206 GetNodeSize(parentId
, &x
, &y
, dc
);
207 SetNodeY(nodeId
, (long)(GetNodeY(parentId
) + m_ySpacing
+ y
));
210 wxNode
*node
= children
.First();
213 CalcLayout((long)node
->Data(), level
+1, dc
);
219 ActivateNode(nodeId
, TRUE
);
224 node
= children
.First();
227 averageX
+= GetNodeX((long)node
->Data());
230 averageX
= averageX
/ n
;
231 SetNodeX(nodeId
, averageX
);
235 SetNodeX(nodeId
, m_lastX
);
237 GetNodeSize(nodeId
, &x
, &y
, dc
);
239 m_lastX
= m_lastX
+ x
+ m_xSpacing
;
249 IMPLEMENT_DYNAMIC_CLASS(wxTreeLayoutStored
, wxTreeLayout
)
251 wxTreeLayoutStored::wxTreeLayoutStored(int n
):wxTreeLayout()
258 wxTreeLayoutStored::~wxTreeLayoutStored(void)
264 void wxTreeLayoutStored::Initialize(int n
)
267 wxTreeLayout::Initialize();
268 if (m_nodes
) delete[] m_nodes
;
269 m_nodes
= new wxStoredNode
[m_maxNodes
];
271 for (i
= 0; i
< n
; i
++)
273 m_nodes
[i
].m_name
= "";
274 m_nodes
[i
].m_active
= FALSE
;
275 m_nodes
[i
].m_parentId
= -1;
282 long wxTreeLayoutStored::AddChild(const wxString
& name
, const wxString
& parent
)
284 if (m_num
< (m_maxNodes
-1 ))
287 if (parent
!= wxT(""))
288 i
= NameToId(parent
);
289 else m_parentNode
= m_num
;
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;
303 long wxTreeLayoutStored::NameToId(const wxString
& name
)
306 for (i
= 0; i
< m_num
; i
++)
307 if (name
== m_nodes
[i
].m_name
)
312 void wxTreeLayoutStored::GetChildren(long id
, wxList
& list
)
314 long currentId
= GetTopNode();
315 while (currentId
!= -1)
317 if (id
== GetNodeParent(currentId
))
318 list
.Append((wxObject
*)currentId
);
319 currentId
= GetNextNode(currentId
);
323 wxStoredNode
* wxTreeLayoutStored::GetNode(long idx
) const
325 wxASSERT(idx
< m_num
);
327 return &m_nodes
[idx
];
330 long wxTreeLayoutStored::GetNodeX(long id
)
332 wxASSERT(id
< m_num
);
334 return (long)m_nodes
[id
].m_x
;
337 long wxTreeLayoutStored::GetNodeY(long id
)
339 wxASSERT(id
< m_num
);
341 return (long)m_nodes
[id
].m_y
;
344 void wxTreeLayoutStored::SetNodeX(long id
, long x
)
346 wxASSERT(id
< m_num
);
348 m_nodes
[id
].m_x
= (int)x
;
351 void wxTreeLayoutStored::SetNodeY(long id
, long y
)
353 wxASSERT(id
< m_num
);
355 m_nodes
[id
].m_y
= (int)y
;
358 void wxTreeLayoutStored::SetNodeName(long id
, const wxString
& name
)
360 wxASSERT(id
< m_num
);
362 m_nodes
[id
].m_name
= name
;
365 wxString
wxTreeLayoutStored::GetNodeName(long id
)
367 wxASSERT(id
< m_num
);
369 return m_nodes
[id
].m_name
;
372 long wxTreeLayoutStored::GetNodeParent(long id
)
376 wxASSERT(id
< m_num
);
378 return m_nodes
[id
].m_parentId
;
384 long wxTreeLayoutStored::GetNextNode(long id
)
386 wxASSERT(id
< m_num
);
388 if ((id
!= -1) && (id
< (m_num
- 1)))
394 void wxTreeLayoutStored::SetClientData(long id
, long clientData
)
396 wxASSERT(id
< m_num
);
398 m_nodes
[id
].m_clientData
= clientData
;
401 long wxTreeLayoutStored::GetClientData(long id
) const
403 wxASSERT(id
< m_num
);
405 return m_nodes
[id
].m_clientData
;
408 void wxTreeLayoutStored::ActivateNode(long id
, bool active
)
410 wxASSERT(id
< m_num
);
412 m_nodes
[id
].m_active
= active
;
415 bool wxTreeLayoutStored::NodeActive(long id
)
417 wxASSERT(id
< m_num
);
419 return m_nodes
[id
].m_active
;
422 wxString
wxTreeLayoutStored::HitTest(wxMouseEvent
& event
, wxDC
& dc
)
424 wxPoint pt
= event
.GetPosition();
429 for (i
= 0; i
< m_maxNodes
; i
++)
432 dc
.GetTextExtent(m_nodes
[i
].m_name
, &width
, &height
);
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)) )
437 return m_nodes
[i
].m_name
;