OSDN Git Service

Please enter the commit message for your changes. Lines starting
[eos/hostdependX86LINUX64.git] / util / X86LINUX64 / lib / tk8.6 / 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 type [file type $f]
43         set id [$tree insert $node end -text [file tail $f] \
44                 -values [list $f $type]]
45
46         if {$type eq "directory"} {
47             ## Make it so that this node is openable
48             $tree insert $id 0 -text dummy ;# a dummy
49             $tree item $id -text [file tail $f]/
50
51         } elseif {$type eq "file"} {
52             set size [file size $f]
53             ## Format the file size nicely
54             if {$size >= 1024*1024*1024} {
55                 set size [format %.1f\ GB [expr {$size/1024/1024/1024.}]]
56             } elseif {$size >= 1024*1024} {
57                 set size [format %.1f\ MB [expr {$size/1024/1024.}]]
58             } elseif {$size >= 1024} {
59                 set size [format %.1f\ kB [expr {$size/1024.}]]
60             } else {
61                 append size " bytes"
62             }
63             $tree set $id size $size
64         }
65     }
66
67     # Stop this code from rerunning on the current node
68     $tree set $node type processedDirectory
69 }
70
71 ## Create the tree and set it up
72 ttk::treeview $w.tree -columns {fullpath type size} -displaycolumns {size} \
73         -yscroll "$w.vsb set" -xscroll "$w.hsb set"
74 ttk::scrollbar $w.vsb -orient vertical -command "$w.tree yview"
75 ttk::scrollbar $w.hsb -orient horizontal -command "$w.tree xview"
76 $w.tree heading \#0 -text "Directory Structure"
77 $w.tree heading size -text "File Size"
78 $w.tree column size -stretch 0 -width 70
79 populateRoots $w.tree
80 bind $w.tree <<TreeviewOpen>> {populateTree %W [%W focus]}
81
82 ## Arrange the tree and its scrollbars in the toplevel
83 lower [ttk::frame $w.dummy]
84 pack $w.dummy -fill both -expand 1
85 grid $w.tree $w.vsb -sticky nsew -in $w.dummy
86 grid $w.hsb -sticky nsew -in $w.dummy
87 grid columnconfigure $w.dummy 0 -weight 1
88 grid rowconfigure $w.dummy 0 -weight 1