wxTreeLayout provides layout of simple trees with one root node, drawn left-to-right, with user-defined spacing between nodes.
wxTreeLayout is an abstract class that must be subclassed. The programmer defines various member functions which will access whatever data structures are appropriate for the application, and wxTreeLayout uses these when laying out the tree.
Nodes are identified by long integer identifiers. The derived class communicates the actual tree structure to wxTreeLayout by defining wxTreeLayout::GetChildren and wxTreeLayout::GetNodeParent functions.
The application should call wxTreeLayout::DoLayout to do the tree layout. Depending on how the derived class has been defined, either wxTreeLayout::Draw must be called (for example by the OnPaint member of a wxScrolledWindow) or the application-defined drawing code should be called as normal.
For example, if you have an image drawing system already defined, you may want wxTreeLayout to position existing node images in that system. So you just need a way for wxTreeLayout to set the node image positions according to the layout algorithm, and the rest will be done by your own image drawing system.
The algorithm is due to Gabriel Robins [1], a linear-time algorithm originally implemented in LISP for AI applications.
The original algorithm has been modified so that both X and Y planes are calculated simultaneously, increasing efficiency slightly. The basic code is only a page or so long.
Below is the example tree generated by the program test.cc.
Derived from
wxObject
See also
Members
wxTreeLayout::wxTreeLayout
wxTreeLayout::ActivateNode
wxTreeLayout::CalcLayout
wxTreeLayout::DoLayout
wxTreeLayout::Draw
wxTreeLayout::DrawBranch
wxTreeLayout::DrawBranches
wxTreeLayout::DrawNode
wxTreeLayout::DrawNodes
wxTreeLayout::GetChildren
wxTreeLayout::GetNextNode
wxTreeLayout::GetNodeName
wxTreeLayout::GetNodeSize
wxTreeLayout::GetNodeParent
wxTreeLayout::GetNodeX
wxTreeLayout::GetNodeY
wxTreeLayout::GetLeftMargin
wxTreeLayout::GetOrientation
wxTreeLayout::GetTopMargin
wxTreeLayout::GetTopNode
wxTreeLayout::GetXSpacing
wxTreeLayout::GetYSpacing
wxTreeLayout::Initialize
wxTreeLayout::NodeActive
wxTreeLayout::SetNodeName
wxTreeLayout::SetNodeX
wxTreeLayout::SetNodeY
wxTreeLayout::SetOrientation
wxTreeLayout::SetTopNode
wxTreeLayout::SetSpacing
wxTreeLayout::SetMargins
wxTreeLayout()
Constructor.
void ActivateNode(long id, bool active)
Define this so wxTreeLayout can turn nodes on and off for drawing purposes (not all nodes may be connected in the tree). See also wxTreeLayout::NodeActive.
void CalcLayout(long id, int level)
Private function for laying out a branch.
void DoLayout(wxDC& dc, long topNode = -1)
Calculates the layout for the tree, optionally specifying the top node.
void Draw(wxDC& dc)
Call this to let wxTreeLayout draw the tree itself, once the layout has been calculated with wxTreeLayout::DoLayout.
void DrawBranch(long from, long to, wxDC& dc)
Defined by wxTreeLayout to draw an arc between two nodes.
void DrawBranches(wxDC& dc)
Defined by wxTreeLayout to draw the arcs between nodes.
void DrawNode(long id, wxDC& dc)
Defined by wxTreeLayout to draw a node.
void DrawNodes(wxDC& dc)
Defined by wxTreeLayout to draw the nodes.
void GetChildren(long id, wxList &list)
Must be defined to return the children of node id in the given list of integers.
long GetNextNode(long id)
Must be defined to return the next node after id, so that wxTreeLayout can iterate through all relevant nodes. The ordering is not important. The function should return -1 if there are no more nodes.
wxString GetNodeName(long id) const
May optionally be defined to get a node's name (for example if leaving the drawing to wxTreeLayout).
void GetNodeSize(long id, long* x, long* y) const
Can be defined to indicate a node's size, or left to wxTreeLayout to use the name as an indication of size.
long GetNodeParent(long id) const
Must be defined to return the parent node of id. The function should return -1 if there is no parent.
long GetNodeX(long id) const
Must be defined to return the current X position of the node. Note that coordinates are assumed to be at the top-left of the node so some conversion may be necessary for your application.
long GetNodeY(long id) const
Must be defined to return the current Y position of the node. Note that coordinates are assumed to be at the top-left of the node so some conversion may be necessary for your application.
long GetLeftMargin() const
Gets the left margin set with wxTreeLayout::SetMargins.
bool GetOrientation() const
Gets the orientation: TRUE means top-to-bottom, FALSE means left-to-right (the default).
long GetTopMargin() const
Gets the top margin set with wxTreeLayout::SetMargins.
long GetTopNode() const
wxTreeLayout calls this to get the top of the tree. Don't redefine this; call wxTreeLayout::SetTopNode instead before calling wxTreeLayout::DoLayout.
long GetXSpacing() const
Gets the horizontal spacing between nodes.
long GetYSpacing() const
Gets the vertical spacing between nodes.
void Initialize()
Initializes wxTreeLayout. Call from application or overridden Initialize or constructor.
bool NodeActive(long id)
Define this so wxTreeLayout can know which nodes are to be drawn (not all nodes may be connected in the tree). See also wxTreeLayout::ActivateNode.
void SetNodeName(long id, const wxString& name)
May optionally be defined to set a node's name.
void SetNodeX(long id, long x)
Must be defined to set the current X position of the node. Note that coordinates are assumed to be at the top-left of the node so some conversion may be necessary for your application.
void SetNodeY(long id, long y)
Must be defined to set the current Y position of the node. Note that coordinates are assumed to be at the top-left of the node so some conversion may be necessary for your application.
void SetOrientation(bool orientation)
Sets the tree orientation: TRUE means top-to-bottom, FALSE means left-to-right (the default).
void SetTopNode(long id)
Call this to identify the top of the tree to wxTreeLayout.
void SetSpacing(long x, long y)
Sets the horizontal and vertical spacing between nodes in the tree.
void SetMargins(long x, long y)
Sets the left and top margins of the whole tree.