Upgrade to Tcl/Tk 8.5b2
[msysgit.git] / mingw / lib / tk8.5 / demos / tree.tcl
blob80f9612b0654ccd12dd9d674e09b0112c00a9cdd
1 # tree.tcl --
3 # This demonstration script creates a toplevel window containing a Ttk
4 # tree widget.
6 # RCS: @(#) $Id: tree.tcl,v 1.2 2007/10/23 06:31:16 das Exp $
8 if {![info exists widgetDemo]} {
9 error "This script should be run from the \"widget\" demo."
12 package require Tk
13 package require Ttk
15 set w .tree
16 catch {destroy $w}
17 toplevel $w
18 wm title $w "Directory Browser"
19 wm iconname $w "tree"
20 positionWindow $w
22 ## Explanatory text
23 ttk::label $w.msg -font $font -wraplength 4i -justify left -anchor n -text "Ttk is the new Tk themed widget set. One of the widgets it includes is a tree widget, which allows the user to browse a hierarchical data-set such as a filesystem. The tree widget not only allows for the tree part itself, but it also supports an arbitrary number of additional columns which can show additional data (in this case, the size of the files found in your filesystem). You can also change the width of the columns by dragging the boundary between them."
24 pack $w.msg -fill x
26 ## See Code / Dismiss
27 pack [addSeeDismiss $w.seeDismiss $w] -side bottom -fill x
29 ## Code to populate the roots of the tree (can be more than one on Windows)
30 proc populateRoots {tree} {
31 foreach dir [lsort -dictionary [file volumes]] {
32 populateTree $tree [$tree insert {} end -text $dir \
33 -values [list $dir directory]]
37 ## Code to populate a node of the tree
38 proc populateTree {tree node} {
39 if {[$tree set $node type] ne "directory"} {
40 return
42 set path [$tree set $node fullpath]
43 $tree delete [$tree children $node]
44 foreach f [lsort -dictionary [glob -nocomplain -dir $path *]] {
45 set type [file type $f]
46 set id [$tree insert $node end -text [file tail $f] \
47 -values [list $f $type]]
49 if {$type eq "directory"} {
50 ## Make it so that this node is openable
51 $tree insert $id 0 -text dummy ;# a dummy
52 $tree item $id -text [file tail $f]/
54 } elseif {$type eq "file"} {
55 set size [file size $f]
56 ## Format the file size nicely
57 if {$size >= 1024*1024*1024} {
58 set size [format %.1f\ GB [expr {$size/1024/1024/1024.}]]
59 } elseif {$size >= 1024*1024} {
60 set size [format %.1f\ MB [expr {$size/1024/1024.}]]
61 } elseif {$size >= 1024} {
62 set size [format %.1f\ kB [expr {$size/1024.}]]
63 } else {
64 append size " bytes"
66 $tree set $id size $size
70 # Stop this code from rerunning on the current node
71 $tree set $node type processedDirectory
74 ## Create the tree and set it up
75 ttk::treeview $w.tree -columns {fullpath type size} -displaycolumns {size} \
76 -yscroll "$w.vsb set" -xscroll "$w.hsb set"
77 if {[tk windowingsystem] ne "aqua"} {
78 ttk::scrollbar $w.vsb -orient vertical -command "$w.tree yview"
79 ttk::scrollbar $w.hsb -orient horizontal -command "$w.tree xview"
80 } else {
81 scrollbar $w.vsb -orient vertical -command "$w.tree yview"
82 scrollbar $w.hsb -orient horizontal -command "$w.tree xview"
84 $w.tree heading \#0 -text "Directory Structure"
85 $w.tree heading size -text "File Size"
86 $w.tree column size -stretch 0 -width 70
87 populateRoots $w.tree
88 bind $w.tree <<TreeviewOpen>> {populateTree %W [%W focus]}
90 ## Arrange the tree and its scrollbars in the toplevel
91 lower [ttk::frame $w.dummy]
92 pack $w.dummy -fill both -expand 1
93 grid $w.tree $w.vsb -sticky nsew -in $w.dummy
94 grid $w.hsb -sticky nsew -in $w.dummy
95 grid columnconfigure $w.dummy 0 -weight 1
96 grid rowconfigure $w.dummy 0 -weight 1