| 1 |
package Tk::Tree; |
| 2 |
# Tree -- TixTree widget |
| 3 |
# |
| 4 |
# Derived from Tree.tcl in Tix 4.1 |
| 5 |
# |
| 6 |
# Chris Dean <[email protected]> |
| 7 |
# Changes: Renee Baecker <[email protected]> |
| 8 |
|
| 9 |
use vars qw($VERSION); |
| 10 |
$VERSION = '4.72'; # $Id: Tree.pm,v 1.4 2007/11/04 09:11:31 eserte Exp $ |
| 11 |
|
| 12 |
use Tk (); |
| 13 |
use Tk::Derived; |
| 14 |
use Tk::HList; |
| 15 |
use base qw(Tk::Derived Tk::HList); |
| 16 |
use strict; |
| 17 |
|
| 18 |
Construct Tk::Widget 'Tree'; |
| 19 |
|
| 20 |
sub Tk::Widget::ScrlTree { shift->Scrolled('Tree' => @_) } |
| 21 |
|
| 22 |
sub Populate |
| 23 |
{ |
| 24 |
my( $w, $args ) = @_; |
| 25 |
|
| 26 |
$w->SUPER::Populate( $args ); |
| 27 |
|
| 28 |
$w->ConfigSpecs( |
| 29 |
-ignoreinvoke => ['PASSIVE', 'ignoreInvoke', 'IgnoreInvoke', 0], |
| 30 |
-opencmd => ['CALLBACK', 'openCmd', 'OpenCmd', 'OpenCmd' ], |
| 31 |
-indicatorcmd => ['CALLBACK', 'indicatorCmd', 'IndicatorCmd', 'IndicatorCmd'], |
| 32 |
-closecmd => ['CALLBACK', 'closeCmd', 'CloseCmd', 'CloseCmd'], |
| 33 |
-indicator => ['SELF', 'indicator', 'Indicator', 1], |
| 34 |
-indent => ['SELF', 'indent', 'Indent', 20], |
| 35 |
-width => ['SELF', 'width', 'Width', 20], |
| 36 |
-itemtype => ['SELF', 'itemtype', 'Itemtype', 'imagetext'], |
| 37 |
-foreground => ['SELF'], |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
sub autosetmode |
| 42 |
{ |
| 43 |
my( $w ) = @_; |
| 44 |
$w->setmode(); |
| 45 |
} |
| 46 |
|
| 47 |
sub add_pathimage |
| 48 |
{ |
| 49 |
my ($w,$path,$imgopen,$imgclose) = @_; |
| 50 |
$imgopen ||= "minusarm"; |
| 51 |
$imgclose ||= "plusarm"; |
| 52 |
|
| 53 |
my $separator = $w->cget(-separator); |
| 54 |
|
| 55 |
$path =~ s/([\.?()|])/\\$1/g; |
| 56 |
$path =~ s/\$/\\\$/g; |
| 57 |
$path =~ s/\\\$$/\$/; |
| 58 |
$path =~ s/\*/[^$separator]+/g; |
| 59 |
|
| 60 |
push(@{$w->{Images}},[$path,$imgopen,$imgclose]); |
| 61 |
} |
| 62 |
|
| 63 |
sub child_entries |
| 64 |
{ |
| 65 |
my ($w,$path,$depth) = @_; |
| 66 |
|
| 67 |
my $level = 1; |
| 68 |
$depth ||= 1; |
| 69 |
$path ||= ''; |
| 70 |
|
| 71 |
my @children = $w->_get_childinfos($depth,$level,$path); |
| 72 |
|
| 73 |
return wantarray ? @children : scalar(@children); |
| 74 |
} |
| 75 |
|
| 76 |
sub _get_childinfos |
| 77 |
{ |
| 78 |
my ($w,$maxdepth,$level,$path) = @_; |
| 79 |
my @children = $w->infoChildren($path); |
| 80 |
my @tmp; |
| 81 |
|
| 82 |
if($level < $maxdepth) |
| 83 |
{ |
| 84 |
for my $child(@children) |
| 85 |
{ |
| 86 |
push(@tmp,$w->_get_childinfos($maxdepth,$level +1,$child)); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
push(@children,@tmp); |
| 91 |
|
| 92 |
return @children; |
| 93 |
} |
| 94 |
|
| 95 |
sub IndicatorCmd |
| 96 |
{ |
| 97 |
my( $w, $ent, $event ) = @_; |
| 98 |
|
| 99 |
my $mode = $w->getmode( $ent ); |
| 100 |
|
| 101 |
if ( $event eq '<Arm>' ) |
| 102 |
{ |
| 103 |
if ($mode eq 'open' ) |
| 104 |
{ |
| 105 |
#$w->_indicator_image( $ent, 'plusarm' ); |
| 106 |
$w->_open($ent); |
| 107 |
} |
| 108 |
else |
| 109 |
{ |
| 110 |
#$w->_indicator_image( $ent, 'minusarm' ); |
| 111 |
$w->_close($ent); |
| 112 |
} |
| 113 |
} |
| 114 |
elsif ( $event eq '<Disarm>' ) |
| 115 |
{ |
| 116 |
if ($mode eq 'open' ) |
| 117 |
{ |
| 118 |
#$w->_indicator_image( $ent, 'plus' ); |
| 119 |
$w->_open($ent); |
| 120 |
} |
| 121 |
else |
| 122 |
{ |
| 123 |
#$w->_indicator_image( $ent, 'minus' ); |
| 124 |
$w->_close($ent); |
| 125 |
} |
| 126 |
} |
| 127 |
elsif( $event eq '<Activate>' ) |
| 128 |
{ |
| 129 |
$w->Activate( $ent, $mode ); |
| 130 |
$w->Callback( -browsecmd => $ent ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
sub close |
| 135 |
{ |
| 136 |
my( $w, $ent ) = @_; |
| 137 |
my $mode = $w->getmode( $ent ); |
| 138 |
$w->Activate( $ent, $mode ) if( $mode eq 'close' ); |
| 139 |
} |
| 140 |
|
| 141 |
sub open |
| 142 |
{ |
| 143 |
my( $w, $ent ) = @_; |
| 144 |
my $mode = $w->getmode( $ent ); |
| 145 |
$w->Activate( $ent, $mode ) if( $mode eq 'open' ); |
| 146 |
} |
| 147 |
|
| 148 |
sub getmode |
| 149 |
{ |
| 150 |
my( $w, $ent ) = @_; |
| 151 |
|
| 152 |
return( 'none' ) unless $w->indicatorExists( $ent ); |
| 153 |
|
| 154 |
my $img = $w->_indicator_image( $ent ); |
| 155 |
if ($img eq "plus" || $img eq "plusarm" || grep{$img eq $_->[2]}@{$w->{Images}}) |
| 156 |
{ |
| 157 |
return( 'open' ); |
| 158 |
} |
| 159 |
return( 'close' ); |
| 160 |
} |
| 161 |
|
| 162 |
sub setmode |
| 163 |
{ |
| 164 |
my ($w,$ent,$mode) = @_; |
| 165 |
unless (defined $mode) |
| 166 |
{ |
| 167 |
$mode = 'none'; |
| 168 |
my @args; |
| 169 |
push(@args,$ent) if defined $ent; |
| 170 |
my @children = $w->infoChildren( @args ); |
| 171 |
if ( @children ) |
| 172 |
{ |
| 173 |
$mode = 'close'; |
| 174 |
foreach my $c (@children) |
| 175 |
{ |
| 176 |
$mode = 'open' if $w->infoHidden( $c ); |
| 177 |
$w->setmode( $c ); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
if (defined $ent) |
| 183 |
{ |
| 184 |
if ( $mode eq 'open' ) |
| 185 |
{ |
| 186 |
#$w->_indicator_image( $ent, 'plus' ); |
| 187 |
$w->_open($ent); |
| 188 |
} |
| 189 |
elsif ( $mode eq 'close' ) |
| 190 |
{ |
| 191 |
#$w->_indicator_image( $ent, 'minus' ); |
| 192 |
$w->_close($ent); |
| 193 |
} |
| 194 |
elsif( $mode eq 'none' ) |
| 195 |
{ |
| 196 |
$w->_indicator_image( $ent, undef ); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
sub _open |
| 202 |
{ |
| 203 |
my ($w,$ent) = @_; |
| 204 |
$w->_indicator_image( $ent, "plus" ); |
| 205 |
for my $entry (@{$w->{Images}}) |
| 206 |
{ |
| 207 |
if($ent =~ $entry->[0]) |
| 208 |
{ |
| 209 |
$w->_indicator_image( $ent, $entry->[2] ); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
sub _close |
| 215 |
{ |
| 216 |
my ($w,$ent) = @_; |
| 217 |
$w->_indicator_image( $ent, "minus" ); |
| 218 |
for my $entry (@{$w->{Images}}) |
| 219 |
{ |
| 220 |
if($ent =~ $entry->[0]) |
| 221 |
{ |
| 222 |
$w->_indicator_image( $ent, $entry->[1] ); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
sub Activate |
| 228 |
{ |
| 229 |
my( $w, $ent, $mode ) = @_; |
| 230 |
if ( $mode eq 'open' ) |
| 231 |
{ |
| 232 |
$w->Callback( -opencmd => $ent ); |
| 233 |
#$w->_indicator_image( $ent, 'minus' ); |
| 234 |
$w->_close($ent); |
| 235 |
} |
| 236 |
elsif ( $mode eq 'close' ) |
| 237 |
{ |
| 238 |
$w->Callback( -closecmd => $ent ); |
| 239 |
#$w->_indicator_image( $ent, 'plus' ); |
| 240 |
$w->_open($ent); |
| 241 |
} |
| 242 |
else |
| 243 |
{ |
| 244 |
|
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
sub OpenCmd |
| 249 |
{ |
| 250 |
my( $w, $ent ) = @_; |
| 251 |
# The default action |
| 252 |
foreach my $kid ($w->infoChildren( $ent )) |
| 253 |
{ |
| 254 |
$w->show( -entry => $kid ); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
sub CloseCmd |
| 259 |
{ |
| 260 |
my( $w, $ent ) = @_; |
| 261 |
|
| 262 |
# The default action |
| 263 |
foreach my $kid ($w->infoChildren( $ent )) |
| 264 |
{ |
| 265 |
$w->hide( -entry => $kid ); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
sub Command |
| 270 |
{ |
| 271 |
my( $w, $ent ) = @_; |
| 272 |
|
| 273 |
return if $w->{Configure}{-ignoreInvoke}; |
| 274 |
|
| 275 |
$w->Activate( $ent, $w->getmode( $ent ) ) if $w->indicatorExists( $ent ); |
| 276 |
} |
| 277 |
|
| 278 |
sub _indicator_image |
| 279 |
{ |
| 280 |
my( $w, $ent, $image ) = @_; |
| 281 |
my $data = $w->privateData(); |
| 282 |
if (@_ > 2) |
| 283 |
{ |
| 284 |
if (defined $image) |
| 285 |
{ |
| 286 |
$w->indicatorCreate( $ent, -itemtype => 'image' ) |
| 287 |
unless $w->indicatorExists($ent); |
| 288 |
$data->{$ent} = $image; |
| 289 |
$w->indicatorConfigure( $ent, -image => $w->Getimage( $image ) ); |
| 290 |
} |
| 291 |
else |
| 292 |
{ |
| 293 |
$w->indicatorDelete( $ent ) if $w->indicatorExists( $ent ); |
| 294 |
delete $data->{$ent}; |
| 295 |
} |
| 296 |
} |
| 297 |
return $data->{$ent}; |
| 298 |
} |
| 299 |
|
| 300 |
1; |
| 301 |
|
| 302 |
__END__ |
| 303 |
|
| 304 |
# Copyright (c) 1996, Expert Interface Technologies |
| 305 |
# See the file "license.terms" for information on usage and redistribution |
| 306 |
# of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 307 |
# |
| 308 |
# The file man.macros and some of the macros used by this file are |
| 309 |
# copyrighted: (c) 1990 The Regents of the University of California. |
| 310 |
# (c) 1994-1995 Sun Microsystems, Inc. |
| 311 |
# The license terms of the Tcl/Tk distrobution are in the file |
| 312 |
# license.tcl. |
| 313 |
|