NANDAGOPAL
NANDAGOPAL

Reputation: 292

Command equivalent in TreeCtrl

I had constructed a tree vieew using Bwidgets and now i am trying to convert it to TreeCtrl, but i could not figure out the equivalent command in TreeCtrl for the Bwidget command:

$tree itemcget $node -data

Upvotes: 0

Views: 146

Answers (2)

NANDAGOPAL
NANDAGOPAL

Reputation: 292

Regarding the Question, My project leader came out with a very simple solution for attaching user specified data to the Treectrl nodes,

You create the node with the data embedded

$tree item element configure $itemID $columnID elemText -text $text -data $data

Then when u want to use the data for any purpose

set dataObj [$tree item element cget $itemID $columnID elemText -data]

Now the data is saved in dataObj and you can use it for any operations, manipulations, it gave me the exact functionality of Bwidget tree command -

$tree itemcget $node -data ------> which i thought was not directly possible in TreeCtrl.

I will post a sample program below for you to experiment it, and sorry about the formatting:

package require treectrl
package require TclOO

oo::class create Foo {
    method test { obj } {
        puts "This is otuput from test method in instant of class D. $obj"
    }
}


treectrl .t -showheader 0 -selectmode single -showroot 0 -yscrollcommand {.y set}
scrollbar .y -ori vert -command ".t yview"
pack .y  -side right -fill y
pack .t  -side right -fill both -expand 1
set columnID [.t column create -text "Column 0"]
.t configure -treecolumn $columnID

.t element create el1 text
.t element create el2 rect -showfocus yes

.t style create s1
.t style elements s1 [list el1 el2]

.t style layout s1 el2 -union el1

.t configure -defaultstyle s1

# easily add a node with text $text as a child of $parent (the root is specified by the string "root")

proc add_node {parent text data} {
    set itemID [.t item create -button yes ]
    .t item element configure $itemID 0 el1 -text $text -data $data
    .t item collapse $itemID
    .t item lastchild $parent $itemID
    return $itemID
}


set sample abcdef
set data1 $sample
set id1 [add_node root "This is data 1" $data1]
set id4 [add_node root "This is data 4" $data1]
set id5 [add_node root "This is data 5" $data1]
set id6 [add_node root "This is data 6" $data1]
set id7 [add_node root "This is data 7" $data1]
set id8 [add_node root "This is data 8" $data1]
set id9 [add_node root "This is data 9" $data1]
set id10 [add_node root "This is data 10" $data1]
set sample2 $id1
set sample3 $sample2

set dataObj [.t item element cget $sample3 0 el1 -data]
puts "--- $dataObj"


set dObj [Foo new]
set id2 [add_node $id1 "This is object Foo node" $dObj]

set dObj_1 [.t item element cget $id2 0 el1 -data]
$dObj test $dObj_1

Using in the following example the values dataObj and dObj_1 can be used not just within this program, but between multiple namespaces with the correct inclusion of packages and method calls.

Upvotes: 0

Donal Fellows
Donal Fellows

Reputation: 137587

There doesn't appear to be any mechanism for attaching user-specified data directly to a node in TkTreeCtrl. The simplest way to work around this is to store the data in an array instead that is indexed by the ID of the node (and the tree widget name if you're using more than one in your application).

# Set the value (assuming you're only making one item here)
set id [$tree item create ...]
set ::userdata($tree,$id) $yourDataItem
# Get the value for a particular item
set id [$tree item id $itemDesc]
puts "the data for $id is $::userdata($tree,$id)"
# Remove the value when removing the item
set id [$tree item id $itemDesc]
unset ::userdata($tree,$id)
$tree item delete $id

I've seen a number of people recommend wrapping TkTreeCtrl inside a class (e.g., Snit, TclOO, XOTcl) to make it simpler to use in specific cases. This is the sort of thing that would be nice wrapped…

Upvotes: 1

Related Questions