ABU 21 Jun 2005
This article introduces some new general operations that can be applied to a BWidget::Tree.
BWidget's Tree (from now on, "bw-tree") lacks in operations for persistent-storage (see persistence) and for deep (recursive) copy
This article provides a tcl-code implementation and examples aimed to overcome these lacks.
Well, I think we can start with a botanical metaphor ...
If we think at a bw-tree as a live tree, we could start thinking at a dried-tree as a synthetized clone of a bw-tree, i.e. a new kind of a copy of (a part of) a bw-tree.
This dried-tree is not a bw-tree; it is a derived from a bw-tree, but it cannot be handled with the usual bw-tree methods.
But there are many things we can do with a dried-tree :
We can graft a dried-tree in a live tree. Once grafted, the dried-tree becomes a 'live' part of the tree.
Moreover, we can also store the dried-tree in a box (a file) so that the bw-tree can be revitalized when the application re-starts ( or in a new application )
That's the leading metaphor, now let's develope the idea ...
Let's call them dried-trees.
but they can also be used for grafting, that is, they can be inserted under a node of an existing bw-tree.
Summarizing, we need new methods to:
Before looking at the core tcl-code for these new operations, I think it is important to look at some use cases:
# # this is our basic sample tree # package require BWidget pack [Tree .tree] .tree insert end root a -text Aaa .tree insert end root b -text Bbb .tree insert end root c -text Ccc .tree insert end a a1 -text Aa1 .tree insert end a a2 -text Aa2 .tree insert end c c1 -text Cc1 .tree insert end c c2 -text Cc2 # some decorations ... set font1 {Courier 10 bold} set font2 {Times 10 italic} set font3 {Helvetica 10 bold} .tree itemconfigure a -font $font1 -fill blue .tree itemconfigure a1 -font $font1 -fill blue .tree itemconfigure a2 -font $font1 -fill blue .tree itemconfigure b -font $font2 -fill gray .tree itemconfigure c -font $font3 -fill red .tree itemconfigure c1 -font $font3 -fill red .tree itemconfigure c2 -font $font3 -fill red
.
# clone the 'c' subtree set myDriedTree [BWidget::Tree::clone .tree c]
BWidget::Tree::graft $myDriedTree .tree "a" 1
.
toplevel .new ; pack [Tree .new.tree] BWidget::Tree::graft $myDriedTree .new.tree root end
.
BWidget::Tree::store $myDriedTree driedA.dat
set myDriedTree [BWidget::Tree::load driedA.dat] # .. then you can start grafting in a bw-tree ....
## bwtree.tcl ## ## Copyright (c) 2005 <Irrational Numbers> : <[email protected]> ## ## This program is free software; you can redistibute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation. ## package require BWidget namespace eval BWidget::Tree { ### unnecessary code ### namespace export every cancel # constant. # This is a special nodeID representing the 'root' of the dried-tree. # Since "root" is a reserved ID for a bw-tree, and it is never copied, # we can use it in a dried-tree without fear of overlapping. variable pseudoRoot root proc _GetNodeOptions { tree node } { set res {} foreach c [$tree itemconfigure $node] { set option [lindex $c 0] set value [lindex $c 4] lappend res $option $value } return $res } # return value: a 'dried-tree' # i.e. a string containing a tcl-command # for rebuilding $node and all its descendants proc clone {tree nodeID} { variable pseudoRoot if { $nodeID == "root" } { set L "" foreach n [$tree nodes root] { append L [_clone $tree $n \$tgtIdx $pseudoRoot] append L {if { $tgtIdx != "end" } {incr tgtIdx}} \n } } else { set L [_clone $tree $nodeID \$tgtIdx $pseudoRoot] } return $L } proc _clone { tree nodeID idxString parent } { set options [_GetNodeOptions $tree $nodeID] set cmd [ format {set map(%s) [$tgtTree insert %s $map(%s) #auto %s ]} \ $nodeID $idxString $parent $options] append cmd \n foreach child [$tree nodes $nodeID] { append cmd [_clone $tree $child end $nodeID] } return $cmd } proc graft { driedTree tgtTree parent tgtIdx } { variable pseudoRoot set map($pseudoRoot) $parent # NOTE: $tgtTree, $tgtIdx are used by driedTree eval $driedTree } proc store { driedTree fileName } { set f [open $fileName w] puts $f "$driedTree" close $f } proc load { fileName } { set f [open $fileName r] set driedTree [read $f] close $f return $driedTree } } package provide BWidget::Tree 0.0
LIMITATIONS
This package (BWidget::Tree rev. 0.0) does not work if the cloned nodes contain some kind of reference to external objects.
In particular, nodes having the "-image" and "-window" options set will introduce some usage limitations...
.. other ?
ABU 21 Jun 2005 - Removed (commented) unnecessary code from the attached package.
Schnexel Wrrr, the BWidget tree is so stupid! Already the graphics says it all: Look at the Screenshots above: The children nodes´ lines branch before the parent node, so parent node and child node graphically have the same parent node (the +/- box). Is there any Tcl/Tk tree that one can take serious?
ABU I like BWidget Tree.
Which other tree-widgets have these features ?
chin - 2009-09-10 05:59:32
The Id of the node is not maintained, if the tree is saved and loaded.
ABU The Id of the new nodes is automatic; think it as a serial number. This is necessary to avoid duplicated IDs.