SoftwareSphere Home

The TreeView library

The TreeView library is a library for displaying tree structures in the same way of the Windows tree views, but it is developed from the ground up and is not just an extension of the Windows Tree Control.

The TreeView library provides a concrete and somewhat better alternative to the Windows Tree Control. It is made by four classes: CTreeNode, CTreeModel, CTreeFormat and CTreeWnd. It is currently based on MFC but uses only the MFC fundamental classes and not the CTreeCtrl class.

The following picture is the tree window showing a part of a file system tree.

Description

In this library the tree structure is contained in separate objects of class CTreeNode or classes derived from it. A CTreeNode is a small object which has the pointers to its parent node, to the first and last of its children nodes and to the next and previous sibling node. A CTreeNode has a label, some icons, some flags and a custom data value.

The tree structure is attached to the tree window by means of a CTreeModel object which provides just some other functions relatively to CTreeNode.

The separation of the content from the view its important for code reusability.

You can for example create a tree with a class derived from CTreeNode to represent your data. Actually the tree can contain instances of different classes provided they all are descendants of CTreeNode.

The TreeView library results to be very efficient in memory and time. The tree window scrolling is fast and smooth, the tree can receive millions of nodes in a fraction of the time required by the Windows Tree Control.

CTreeNode

This class represents a tree node, it has the pointers to the parent node, the first and last child node, the previous and next sibling node. It has a string label, two integer to contain two icon indices, some flags and an unsigned integer to contain a custom data or pointer. When a CTreeNode is deleted it deletes also it children nodes so an entire tree is deleted by deleting its root.

CTreeModel

The CTreeModel class represents a tree container which is passed to a CTreeWnd to be shown. A tree model contains one single root node, however the tree is created by explicitly constructing CTreeNode objects, or objects of classes derived from it, and appending them in the desired way.

CTreeFormat

This class contains the graphical options for the CTreeWnd. A CTreeWnd must have a CTreeFormat pointer. The format options are the following:

HasButtons bool Tells if the treewnd shows buttons to the left of items with children
HasLines bool Tells if the treewnd shows dotted lines from nodes to children
HasLinesAtRoot bool Tells if the treewnd displays lines which go to the root nodes
HasIcons bool Tells if the treewnd displays an icon to the left of the node labels
TextColor Color The normal label text color
BackColor Color The window background color
HighlightBackColor Color The background color of the selected nodes
HighlightTextColor Color The text color of the selected nodes
LineColor Color The color of the tree lines and button lines
LabelFont Font The font for the node labels
Indent Integer (non negative) The value in pixels by which the items are indented
LeftMargin Integer (non negative) The left margin in pixels before the tree nodes or lines
RowHeight Integer (non negative) The height of each item

Current limitations

The following features are still in development but will be ready soon:

Comparison with CTreeCtrl

The following table contains the time in seconds required to create the specified number of nodes for both the TreeView library (TVL) and the Windows tree control (WTC). Of course, when the time is zero it means that the real time is less than 1 second.

Test 1
N° of nodes Creation Deletion
WTC TVL WTC TVL
100 0 0 0 0
200 0 0 0 0
500 0 0 0 0
1000 0 0 0 0
2000 0 0 0 0
5000 1 1 5 1
10000 5 1 21 1
20000 24 0 84 0
50000 158 0 463 0
100000 628 0 1842 0
Test 2
N° of nodes Creation Deletion
WTC TVL WTC TVL
100 0 0 0 0
200 0 0 0 0
500 0 0 0 0
1000 0 0 0 0
2000 0 0 0 0
5000 1 1 1 0
10000 0 4 0 0
20000 20 0 0 0
50000 131 1 0 0
100000 527 0 1 0

The first test has been executed on a Pentium III 1000 Mhz with 384 MB Ram and Windows 98 S.E. and the second on a AMD Sempron 1500 Mhz with 256 MB Ram and Windows XP Professional with Service Pack 2.

We can see that in Windows XP the deletion procedure of the tree control has been corrected, but for the insertion the problem persists.

It is clear that the Windows Tree Control isn't implemented very well and its slowness for large number of items is a problem in some applications. With the TreeView Library these problems don't exist.

More information

This is a screenshot of the demo program.

You can download the demo program from here. The zip file contains also the source code of the demo program to show how the classes of the library are used.

The Tree View Library is available complete of its source code and documentation.

For more information write to mail@softwaresphere.com or leave a message with this form.