## wxTreeLayout

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 ??, 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.

Figure 6.1: Example tree

Derived from

wxObject

See also

wxTreeLayoutStored

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::wxTreeLayout

wxTreeLayout()

Constructor.

### wxTreeLayout::ActivateNode

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.

### wxTreeLayout::CalcLayout

void CalcLayout(long id, int level)

Private function for laying out a branch.

### wxTreeLayout::DoLayout

void DoLayout(wxDC& dc, long topNode = -1)

Calculates the layout for the tree, optionally specifying the top node.

### wxTreeLayout::Draw

void Draw(wxDC& dc)

Call this to let wxTreeLayout draw the tree itself, once the layout has been calculated with wxTreeLayout::DoLayout.

### wxTreeLayout::DrawBranch

void DrawBranch(long from, long to, wxDC& dc)

Defined by wxTreeLayout to draw an arc between two nodes.

### wxTreeLayout::DrawBranches

void DrawBranches(wxDC& dc)

Defined by wxTreeLayout to draw the arcs between nodes.

### wxTreeLayout::DrawNode

void DrawNode(long id, wxDC& dc)

Defined by wxTreeLayout to draw a node.

### wxTreeLayout::DrawNodes

void DrawNodes(wxDC& dc)

Defined by wxTreeLayout to draw the nodes.

### wxTreeLayout::GetChildren

void GetChildren(long id, wxList &list)

Must be defined to return the children of node id in the given list of integers.

### wxTreeLayout::GetNextNode

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.

### wxTreeLayout::GetNodeName

wxString GetNodeName(long id) const

May optionally be defined to get a node's name (for example if leaving the drawing to wxTreeLayout).

### wxTreeLayout::GetNodeSize

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.

### wxTreeLayout::GetNodeParent

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.

### wxTreeLayout::GetNodeX

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.

### wxTreeLayout::GetNodeY

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.

### wxTreeLayout::GetLeftMargin

long GetLeftMargin() const

Gets the left margin set with wxTreeLayout::SetMargins.

### wxTreeLayout::GetOrientation

bool GetOrientation() const

Gets the orientation: TRUE means top-to-bottom, FALSE means left-to-right (the default).

### wxTreeLayout::GetTopMargin

long GetTopMargin() const

Gets the top margin set with wxTreeLayout::SetMargins.

### wxTreeLayout::GetTopNode

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.

### wxTreeLayout::GetXSpacing

long GetXSpacing() const

Gets the horizontal spacing between nodes.

### wxTreeLayout::GetYSpacing

long GetYSpacing() const

Gets the vertical spacing between nodes.

### wxTreeLayout::Initialize

void Initialize()

Initializes wxTreeLayout. Call from application or overridden Initialize or constructor.

### wxTreeLayout::NodeActive

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.

### wxTreeLayout::SetNodeName

void SetNodeName(long id, const wxString& name)

May optionally be defined to set a node's name.

### wxTreeLayout::SetNodeX

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.

### wxTreeLayout::SetNodeY

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.

### wxTreeLayout::SetOrientation

void SetOrientation(bool orientation)

Sets the tree orientation: TRUE means top-to-bottom, FALSE means left-to-right (the default).

### wxTreeLayout::SetTopNode

void SetTopNode(long id)

Call this to identify the top of the tree to wxTreeLayout.

### wxTreeLayout::SetSpacing

void SetSpacing(long x, long y)

Sets the horizontal and vertical spacing between nodes in the tree.

### wxTreeLayout::SetMargins

void SetMargins(long x, long y)

Sets the left and top margins of the whole tree.