OSDN Git Service

Please enter the commit message for your changes. Lines starting
[eos/base.git] / util / src / TclTk / tk8.6.12 / library / demos / tree.tcl
1 # tree.tcl --
2 #
3 # This demonstration script creates a toplevel window containing a Ttk
4 # tree widget.
5
6 if {![info exists widgetDemo]} {
7     error "This script should be run from the \"widget\" demo."
8 }
9
10 package require Tk
11
12 set w .tree
13 catch {destroy $w}
14 toplevel $w
15 wm title $w "Directory Browser"
16 wm iconname $w "tree"
17 positionWindow $w
18
19 ## Explanatory text
20 ttk::label $w.msg -font $font -wraplength 4i -justify left -anchor n -padding {10 2 10 6} -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."
21 pack $w.msg -fill x
22
23 ## See Code / Dismiss
24 pack [addSeeDismiss $w.seeDismiss $w] -side bottom -fill x
25
26 ## Code to populate the roots of the tree (can be more than one on Windows)
27 proc populateRoots {tree} {
28     foreach dir [lsort -dictionary [file volumes]] {
29         populateTree $tree [$tree insert {} end -text $dir \
30                 -values [list $dir directory]]
31     }
32 }
33
34 ## Code to populate a node of the tree
35 proc populateTree {tree node} {
36     if {[$tree set $node type] ne "directory"} {
37         return
38     }
39     set path [$tree set $node fullpath]
40     $tree delete [$tree children $node]
41     foreach f [lsort -dictionary [glob -nocomplain -dir $path *]] {
42         set f [file normalize $f]
43         set type [file type $f]
44         set id [$tree insert $node end -text [file tail $f] \
45                 -values [list $f $type]]
46
47         if {$type eq "directory"} {
48             ## Make it so that this node is openable
49             $tree insert $id 0 -text dummy ;# a dummy
50             $tree item $id -text [file tail $f]/
51
52         } elseif {$type eq "file"} {
53             set size [file size $f]
54             ## Format the file size nicely
55             if {$size >= 1024*1024*1024} {
56                 set size [format %.1f\ GB [expr {$size/1024/1024/1024.}]]
57             } elseif {$size >= 1024*1024} {
58                 set size [format %.1f\ MB [expr {$size/1024/1024.}]]
59             } elseif {$size >= 1024} {
60                 set size [format %.1f\ kB [expr {$size/1024.}]]
61             } else {
62                 append size " bytes"
63             }
64             $tree set $id size $size
65         }
66     }
67
68     # Stop this code from rerunning on the current node
69     $tree set $node type processedDirectory
70 }
71
72 ## Create the tree and set it up
73 ttk::treeview $w.tree -columns {fullpath type size} -displaycolumns {size} \
74         -yscroll "$w.vsb set" -xscroll "$w.hsb set"
75 ttk::scrollbar $w.vsb -orient vertical -command "$w.tree yview"
76 ttk::scrollbar $w.hsb -orient horizontal -command "$w.tree xview"
77 $w.tree heading \#0 -text "Directory Structure"
78 $w.tree heading size -text "File Size"
79 $w.tree column size -width 70
80 populateRoots $w.tree
81 bind $w.tree <<TreeviewOpen>> {populateTree %W [%W focus]}
82
83 ## Arrange the tree and its scrollbars in the toplevel
84 lower [ttk::frame $w.dummy]
85 pack $w.dummy -fill both -expand 1
86 grid $w.tree $w.vsb -sticky nsew -in $w.dummy
87 grid $w.hsb -sticky nsew -in $w.dummy
88 grid columnconfigure $w.dummy 0 -weight 1
89 grid rowconfigure $w.dummy 0 -weight 1