]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/deprecated/treelay.cpp
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"
28 #include "wx/deprecated/setup.h"
32 #include "wx/deprecated/treelay.h"
39 IMPLEMENT_ABSTRACT_CLASS(wxTreeLayout
, wxObject
)
41 wxTreeLayout::wxTreeLayout()
47 m_orientation
= FALSE
;
51 void wxTreeLayout::DoLayout(wxDC
& dc
, long topId
)
56 long actualTopId
= GetTopNode();
57 long id
= actualTopId
;
62 ActivateNode(id
, FALSE
);
65 m_lastY
= m_topMargin
;
66 m_lastX
= m_leftMargin
;
67 CalcLayout(actualTopId
, 0, dc
);
70 void wxTreeLayout::Draw(wxDC
& dc
)
77 void wxTreeLayout::DrawNodes(wxDC
& dc
)
79 long id
= GetTopNode();
88 void wxTreeLayout::DrawBranches(wxDC
& dc
)
90 long id
= GetTopNode();
93 if (GetNodeParent(id
) > -1)
95 long parent
= GetNodeParent(id
);
96 if (NodeActive(parent
))
97 DrawBranch(parent
, id
, dc
);
103 void wxTreeLayout::DrawNode(long id
, wxDC
& dc
)
106 wxString
name(GetNodeName(id
));
108 wxSprintf(buf
, wxT("%s"), (const wxChar
*) name
);
110 wxSprintf(buf
, wxT("<unnamed>"));
114 dc
.GetTextExtent(buf
, &x
, &y
);
115 dc
.DrawText(buf
, GetNodeX(id
), (long)(GetNodeY(id
) - (y
/2.0)));
118 void wxTreeLayout::DrawBranch(long from
, long to
, wxDC
& dc
)
121 GetNodeSize(from
, &w
, &h
, dc
);
122 dc
.DrawLine(GetNodeX(from
)+w
, GetNodeY(from
),
123 GetNodeX(to
), GetNodeY(to
));
126 void wxTreeLayout::Initialize(void)
130 void wxTreeLayout::GetNodeSize(long id
, long *x
, long *y
, wxDC
& dc
)
132 wxString
name(GetNodeName(id
));
134 dc
.GetTextExtent(name
, x
, y
);
141 void wxTreeLayout::CalcLayout(long nodeId
, int level
, wxDC
& dc
)
144 GetChildren(nodeId
, children
);
145 int n
= children
.GetCount();
147 if (m_orientation
== FALSE
)
152 SetNodeX(nodeId
, m_leftMargin
);
157 long parentId
= GetNodeParent(nodeId
);
159 GetNodeSize(parentId
, &x
, &y
, dc
);
160 SetNodeX(nodeId
, (long)(GetNodeX(parentId
) + m_xSpacing
+ x
));
163 wxNode
*node
= children
.GetFirst();
166 CalcLayout((long)node
->GetData(), level
+1, dc
);
167 node
= node
->GetNext();
172 ActivateNode(nodeId
, TRUE
);
177 node
= children
.GetFirst();
180 averageY
+= GetNodeY((long)node
->GetData());
181 node
= node
->GetNext();
183 averageY
= averageY
/ n
;
184 SetNodeY(nodeId
, averageY
);
188 SetNodeY(nodeId
, m_lastY
);
190 GetNodeSize(nodeId
, &x
, &y
, dc
);
192 m_lastY
= m_lastY
+ y
+ m_ySpacing
;
201 SetNodeY(nodeId
, m_topMargin
);
206 long parentId
= GetNodeParent(nodeId
);
208 GetNodeSize(parentId
, &x
, &y
, dc
);
209 SetNodeY(nodeId
, (long)(GetNodeY(parentId
) + m_ySpacing
+ y
));
212 wxNode
*node
= children
.GetFirst();
215 CalcLayout((long)node
->GetData(), level
+1, dc
);
216 node
= node
->GetNext();
221 ActivateNode(nodeId
, TRUE
);
226 node
= children
.GetFirst();
229 averageX
+= GetNodeX((long)node
->GetData());
230 node
= node
->GetNext();
232 averageX
= averageX
/ n
;
233 SetNodeX(nodeId
, averageX
);
237 SetNodeX(nodeId
, m_lastX
);
239 GetNodeSize(nodeId
, &x
, &y
, dc
);
241 m_lastX
= m_lastX
+ x
+ m_xSpacing
;
251 IMPLEMENT_DYNAMIC_CLASS(wxTreeLayoutStored
, wxTreeLayout
)
253 wxTreeLayoutStored::wxTreeLayoutStored(int n
):wxTreeLayout()
260 wxTreeLayoutStored::~wxTreeLayoutStored(void)
266 void wxTreeLayoutStored::Initialize(int n
)
269 wxTreeLayout::Initialize();
270 if (m_nodes
) delete[] m_nodes
;
271 m_nodes
= new wxStoredNode
[m_maxNodes
];
273 for (i
= 0; i
< n
; i
++)
275 m_nodes
[i
].m_name
= wxT("");
276 m_nodes
[i
].m_active
= FALSE
;
277 m_nodes
[i
].m_parentId
= -1;
284 long wxTreeLayoutStored::AddChild(const wxString
& name
, const wxString
& parent
)
286 if (m_num
< (m_maxNodes
-1 ))
289 if (parent
!= wxT(""))
290 i
= NameToId(parent
);
291 else m_parentNode
= m_num
;
293 m_nodes
[m_num
].m_parentId
= i
;
294 m_nodes
[m_num
].m_name
= name
;
295 m_nodes
[m_num
].m_x
= m_nodes
[m_num
].m_y
= 0;
296 m_nodes
[m_num
].m_clientData
= 0;
305 long wxTreeLayoutStored::AddChild(const wxString
& name
, long parent
)
307 if (m_num
< (m_maxNodes
-1 ) && parent
< m_num
)
316 m_parentNode
= m_num
;
319 m_nodes
[m_num
].m_parentId
= i
;
320 m_nodes
[m_num
].m_name
= name
;
321 m_nodes
[m_num
].m_x
= m_nodes
[m_num
].m_y
= 0;
322 m_nodes
[m_num
].m_clientData
= 0;
331 long wxTreeLayoutStored::NameToId(const wxString
& name
)
334 for (i
= 0; i
< m_num
; i
++)
335 if (name
== m_nodes
[i
].m_name
)
340 void wxTreeLayoutStored::GetChildren(long id
, wxList
& list
)
342 long currentId
= GetTopNode();
343 while (currentId
!= -1)
345 if (id
== GetNodeParent(currentId
))
346 list
.Append((wxObject
*)currentId
);
347 currentId
= GetNextNode(currentId
);
351 wxStoredNode
* wxTreeLayoutStored::GetNode(long idx
) const
353 wxASSERT(idx
< m_num
);
355 return &m_nodes
[idx
];
358 long wxTreeLayoutStored::GetNodeX(long id
)
360 wxASSERT(id
< m_num
);
362 return (long)m_nodes
[id
].m_x
;
365 long wxTreeLayoutStored::GetNodeY(long id
)
367 wxASSERT(id
< m_num
);
369 return (long)m_nodes
[id
].m_y
;
372 void wxTreeLayoutStored::SetNodeX(long id
, long x
)
374 wxASSERT(id
< m_num
);
376 m_nodes
[id
].m_x
= (int)x
;
379 void wxTreeLayoutStored::SetNodeY(long id
, long y
)
381 wxASSERT(id
< m_num
);
383 m_nodes
[id
].m_y
= (int)y
;
386 void wxTreeLayoutStored::SetNodeName(long id
, const wxString
& name
)
388 wxASSERT(id
< m_num
);
390 m_nodes
[id
].m_name
= name
;
393 wxString
wxTreeLayoutStored::GetNodeName(long id
)
395 wxASSERT(id
< m_num
);
397 return m_nodes
[id
].m_name
;
400 long wxTreeLayoutStored::GetNodeParent(long id
)
404 wxASSERT(id
< m_num
);
406 return m_nodes
[id
].m_parentId
;
412 long wxTreeLayoutStored::GetNextNode(long id
)
414 wxASSERT(id
< m_num
);
416 if ((id
!= -1) && (id
< (m_num
- 1)))
422 void wxTreeLayoutStored::SetClientData(long id
, long clientData
)
424 wxASSERT(id
< m_num
);
426 m_nodes
[id
].m_clientData
= clientData
;
429 long wxTreeLayoutStored::GetClientData(long id
) const
431 wxASSERT(id
< m_num
);
433 return m_nodes
[id
].m_clientData
;
436 void wxTreeLayoutStored::ActivateNode(long id
, bool active
)
438 wxASSERT(id
< m_num
);
440 m_nodes
[id
].m_active
= active
;
443 bool wxTreeLayoutStored::NodeActive(long id
)
445 wxASSERT(id
< m_num
);
447 return m_nodes
[id
].m_active
;
450 wxString
wxTreeLayoutStored::HitTest(wxMouseEvent
& event
, wxDC
& dc
)
452 wxPoint pt
= event
.GetPosition();
457 for (i
= 0; i
< m_maxNodes
; i
++)
460 dc
.GetTextExtent(m_nodes
[i
].m_name
, &width
, &height
);
462 if ( (x
>= (m_nodes
[i
].m_x
-10)) && (x
< (m_nodes
[i
].m_x
+ width
+10)) &&
463 (y
>= m_nodes
[i
].m_y
-10) && (y
< (m_nodes
[i
].m_y
+ height
+10)) )
465 return m_nodes
[i
].m_name
;
469 return wxString( wxT("") );