manual of ttk::treeview(3tk)
pathname detach itemList
Unlinks all of the specified items in itemList from the tree.
The items and all of their descendants are still present and may
be reinserted at another point in the tree but will not be dis‐
played. The root item may not be detached. See also: delete.
I first tried
$treeview exists item
and found it returns true for both detached items and regular items.
Then I tried
$treeview parent item
Realized detached item always returns empty string {}, but then, regular
items who are direct children of root item also returns empty string,
thus not distinguished.
Then I tried
$treeview index item
hoping detached item returns some special value. It returns zero. But
since index start from zero, the first children item also returns zero,
thus not distinguished.
Of course I tried to google and look up tcl.tk wiki pages before all
these studies. So it seems I did pretty much home work and had to left
this to the wisdom of usenet.
As a last resort, I can always keep a variable to hold list of detached
items the moment I detach them, but then it's not very good programming,
because then the program who uses the widget knows a bit too much how
the widget works.
Thanks in advance!
There is at present no way to distinguish detached items from
direct children of the root. [$tv parent item] returns "" for
"no parent", but "" is also the name of the root node.
(Known problem, I just didn't recognize it as a problem
until it was too late to fix.)
> Then I tried
> $treeview index item
> hoping detached item returns some special value. It returns zero.
That's a possibility I hadn't considered -- that'd probably work.
Which would work better: returning -1 or the empty string
for the index of detached items?
Also note that items can be children of a detached item.
Those won't count as "detached", but will not be viewable either.
Do you need to detect that condition as well?
A side question: what are you using detached items for?
I added that method mostly because I thought it might be
useful and it was to implement, but I haven't found a use
case for them myself.
--Joe English
Hello Joe,
although I am using treectrl, I would use it to set nodes visible/
invisible according to a filter criteria.
Ruediger
Just a bit more info: So the filter ('*CUR*') would select the all
nodes that have this string in it. All other nodes shall still be
managed by the treeview and showed again when they match a new filter
string.
An method like elide in text widget would work well for me too. In fact
it is perhaps better than detach, because it makes tcl/ttk easier to
learn for those who already managed text widget.
Did you make a wrapper widget (mega widget) out of this need? Just think
if you did, you probably can post it to tcl.tk wiki and share with
others (I would really find it useful), before next version have better
solution.
Having seen no answer I guess I had to write anew.
I am a bit curious, as tck 8.5 includes ttk, there should be many
discussion related to ttk widgets on tcl.tk website, and ttk/tile might
close & move their discussion forum to tcl.tk. But there are few
discussion of ttk widgets there on wiki.tcl.tk, thus I am not even sure
if I should post my humble wrapper script there. Perhaps I should post
to tcl.ttk? (Just joking)
Here is what I made, as tcl newbie I must have a lot of mistakes.
rename $f.tree $f.tree.internal
# ------- proc -------
# change the behavior of this treeview widget so that
# it returns an index of -1 when the item is detached.
# original idea is from
# news:slrni0q8m2....@eurydice.office.flightlab.com
# --------------------
proc $f.tree {args} [string map [list WIDGET $f.tree] {
static detached
if {! [info exist detached]} {set detached [list]}
switch [lindex $args 0] {
"index" {
if {[lindex $args 1] in $detached} {
return -1
} else {
return [eval WIDGET.internal $args]
}
}
"move" {
if {[lindex $args 1] in $detached} {
set detached [lsearch -all -inline -exact -not $detached [lindex $args 1]]
}
return [eval WIDGET.internal $args]
}
"detach" {
set detached [concat $detached [lrange $args 1 end]]
return [eval WIDGET.internal $args]
}
"default" { return [eval WIDGET.internal $args] }
}
}]
}