# Utilities2 # Gerald Banon, 1998, 1999 package provide dpi.inpe.br/banon/1998/08.02.08.56 2.1 # ---------------------------------------------------------------------- # DirectorySize # Return the content size in bytes of a directory proc DirectorySize {dir {size 0}} { set pwd [pwd] if [catch {cd $dir} err] { puts stderr $err return } set fileList [glob -nocomplain *] foreach file $fileList { set size [expr $size + [file size $file]] if [file isdirectory $file] { set size [DirectorySize [file join $dir $file] $size] } } cd $pwd return $size } # puts "[expr [DirectorySize c:/usuario/gerald/URLib] / 1024] Kbytes" # DirectorySize - end # ---------------------------------------------------------------------- # FindFile # inspired from Example 9-11 # Finding a file by name. # Return the the file path proc FindFile {filePathName startDir fileName} { upvar $filePathName filePath if [file exists [file join $startDir $fileName]] { set filePath [file join $startDir $fileName] return -code return } foreach file [glob -nocomplain [file join $startDir *]] { if [file isdirectory $file] { FindFile filePath [file join $startDir $file] \ $fileName } } } # set filePath "" # FindFile filePath C:/ netscape.exe # puts $filePath # => C:/Program Files/Netscape/Communicator/Program/netscape.exe # FindFile - end # ---------------------------------------------------------------------- # DirectoryMTime # Return the content modification time of a directory # time is in seccond proc DirectoryMTime {dir {time 0}} { set pwd [pwd] if [catch {cd $dir} err] { puts stderr $err return } set time [Max $time [file mtime $dir]] set fileList [glob -nocomplain *] foreach file $fileList { if [file isdirectory $file] { set time [DirectoryMTime [file join $dir $file] $time] } else { set time [Max $time [file mtime $file]] } } cd $pwd return $time } # DirectoryMTime - end # ---------------------------------------------------------------------- # DirectoryContent # build a list of all the files contained in a directory and its childs # flag == 0 means no limit to the number of files proc DirectoryContent {listName homePath dir {flag 0}} { upvar $listName list set fileList [glob -nocomplain $dir/*] foreach file $fileList { if [file isdirectory $file] { DirectoryContent list $homePath $file $flag } else { regsub $homePath/ $file {} file if {$flag && [llength $list] > 200} { return -code return } lappend list $file } } } #proc DirectoryContent {homePath dir {flag 0} {fileNameList {}}} { # set fileList [glob -nocomplain $dir/*] # foreach file $fileList { # if [file isdirectory $file] { # set fileNameList [DirectoryContent $homePath\ # $file $flag $fileNameList] # } else { # regsub $homePath/ $file {} file # if {$flag && [llength $fileNameList] > 200} { # return -code return # } # lappend fileNameList $file # } # } # return $fileNameList #} # set fileList "" # DirectoryContent fileList c:/tmp/doc2 c:/tmp/doc2 # puts $fileList # DirectoryContent - end # ---------------------------------------------------------------------- # DirectoryNewer # Return 1 if the directory content is newer than the referenceTime # content proc DirectoryNewer {dir referenceTime} { set pwd [pwd] if [catch {cd $dir} err] { puts stderr $err return } set mtime [clock format [file mtime $dir] \ -format %Y:%m.%d.%H.%M.%S] if {[string compare $mtime $referenceTime] > 0} { cd $pwd return 1 } set fileList [glob -nocomplain *] foreach file $fileList { if [file isdirectory $file] { if [DirectoryNewer [file join $dir $file] \ $referenceTime] { cd $pwd return 1 } } else { set mtime [clock format [file mtime $file] \ -format %Y:%m.%d.%H.%M.%S] if {[string compare $mtime $referenceTime] > 0} { cd $pwd return 1 } } } cd $pwd return 0 } # puts [clock format [clock seconds] -format %Y:%m.%d.%H.%M.%S] # puts [DirectoryNewer \ c:/usuario/gerald/URLib/col/dpi.inpe.br/banon/1998/08.02.08.56 \ 1999:01.28.16.44.39] # DirectoryNewer - end # ---------------------------------------------------------------------- # Warning proc Warning {program string {var1 {}} {var2 {}}} { upvar #0 {Text::URLib Service} title toplevel .warning -borderwidth 10 wm title .warning $title wm resizable .warning 1 0 set bg [.warning cget -bg] set t [text .warning.text -wrap word -fg black \ -relief flat -bg $bg] set font [lindex [lindex [$t configure -font] end] 0] $t configure -font {$font 10} pack $t -fill x ;# now -tabs is working properly # insert dialog eval [list ${program}ExtraDialog $t $string $var1 $var2] ComputeGeometry .warning $t 1c .8c return .warning } # Warning - end # ---------------------------------------------------------------------- # ProcessKeyForDialog proc ProcessKeyForDialog {button underline key} { global returnDialog set ib [llength $button] ;# number of buttons incr ib -1 set button0 [lindex $button 0] upvar #0 Text::$button0 textButton0 set underline0 [lindex $underline 0] if {$underline0 >= 0} { set letter [string index $textButton0 $underline0] if [regexp -nocase $letter $key] { set returnDialog 0 return } } if $ib { set button1 [lindex $button 1] upvar #0 Text::$button1 textButton1 set underline1 [lindex $underline 1] if {$underline1 >= 0} { set letter [string index $textButton1 $underline1] if [regexp -nocase $letter $key] { set returnDialog 1 return } } } } # ProcessKeyForDialog - end # ---------------------------------------------------------------------- # DisplayText # example: # $entryName == spPreference or ddDirectory # $varName == dd(result1) # $w == .ddhelp or .xxdirectory # $bg == #ffffcc # create is 0 or 1; 0 means don't create widget # (just update if it exists) - used by the SetBackGround procedure, # 1 means create # fill is 0 or 1; 0 means don't fill widget # (just create it if it doesn't exist and create == 1) - used by the # PerformCheck procedure (the fill will happen later through the # call to the CompleteEntry procedure), # 1 means fill # fileList is used in the XXRepository procedure proc DisplayText {entryWidget entryName varName w bg \ {create {1}} {fill {1}}} { if [winfo exists $w] { wm deiconify $w $w.f.t configure -state normal $w.f.t delete 1.0 end } else { if !$create {return} toplevel $w set W [winfo width .window] set H [winfo height .window] wm geometry $w 421x$H+[expr $W + 7]+0 if [regexp {help} $w] {set word Help} if {$w == ".xxdirectory"} {set word Check} if {$w == ".xxrepository"} {set word Check} upvar #0 "Text::URLib Service - $word" varText wm title $w $varText if [regexp {help} $w] { set word word } else { set word none } ScrolledText $w.f -wrap $word \ -bg $bg -fg black -cursor double_arrow \ -relief raised -borderwidth 2 -padx .2c -pady .2c # Buttons frame $w.sp -height .04c ;# extra space set width 2.2 set height .6 set b [frame $w.button -width 5c -height .6c] # Close Button set bclose [frame $b.close -width [format "%sc" $width] \ -height [format "%sc" $height]] button $bclose.close -command "destroy $w" -cursor hand2 ConfigText $bclose.close Close # Close Button - end # Reload Button set breload [frame $b.reload -width [format "%sc" $width] \ -height [format "%sc" $height]] button $breload.reload \ -command "PerformCheck $entryWidget $entryName \ $varName" \ -cursor hand2 ConfigText $breload.reload Reload # Reload Button - end # Buttons - end pack propagate $b false pack propagate $bclose false pack propagate $breload false if [regexp help $w] { pack $bclose -side top } else { pack $bclose -side left pack $breload -side right } pack $bclose.close -fill both pack $breload.reload -fill both pack $w.f -side top -fill y -expand true pack $w.sp -side top ;# extra space pack $b -side bottom -pady .2c regexp ... $entryWidget win ;# .dd bind $w <1> "ProcessButton1 $win" } # Fill Text if !$fill {return} regexp {.(...)(.*)} $w m first last set programName [string toupper $first]$last $programName $varName ;# DDHelp or XXDirectory or ... # Fill Text - end raise $w } proc ScrolledText {f args} { frame $f -borderwidth 2 -relief groove eval {text $f.t \ -xscrollcommand [list $f.xscroll set] \ -yscrollcommand [list $f.yscroll set]} $args scrollbar $f.xscroll -orient horizontal \ -command [list $f.t xview] scrollbar $f.yscroll -orient vertical \ -command [list $f.t yview] grid $f.t $f.yscroll -sticky news if ![regexp {help} $f] {grid $f.xscroll -sticky news} grid rowconfigure $f 0 -weight 1 grid columnconfigure $f 0 -weight 1 } # DisplayText - end # ---------------------------------------------------------------------- # Selector proc Selector {parent entryName varName index \ {listName {}} {flag {1}}} { # example: # set aMail [Selector $f.ent sp 1 environmentArray(spEMailAddressList)] # flag == 1 <==> pack the check button # flag == 0 <==> doesn't pack the check button global environmentArray global typeTable upvar #0 $entryName xxEntry set type $typeTable($xxEntry) regexp {\.[^.]*\.[^.]*} $parent parentLabel ;# .dd.dir set side .6c ;# button size set side2 1.15c set a [frame $parent.entry -borderwidth 2 -bg gray \ -relief sunken -height $side] frame $a.button1 -width $side -height $side frame $a.button2 -width $side -height $side frame $a.button3 -width $side -height $side frame $a.button4 -width $side -height $side frame $a.button5 -width $side -height $side frame $a.button6 -width $side -height $side button $a.button1.1 -text < \ -font {-family courier -size 11 -weight normal} \ -cursor hand2 menubutton $a.button2.2 -text > \ -font {-family courier -size 11 -weight normal} \ -cursor hand2 -relief raised -menu $a.button2.2.menu menubutton $a.button3.3 \ -font {-size 9 -weight normal} \ -cursor hand2 -relief raised -menu $a.button3.3.menu ConfigText $a.button3.3 S button $a.button4.4 -cursor hand2 \ -font {-size 9 -weight normal} # -font {times 9 roman} if [regexp {\.ent\.} $type] { ConfigText $a.button4.4 Remove } else { ConfigText $a.button4.4 C } button $a.button5.5 -cursor hand2 \ -font {-size 9 -weight normal} button $a.button6.6 -cursor hand2 \ -font {-size 9 -weight normal} if [regexp {\.rep\.} $type] { set bg [. cget -bg] set nc [NewColor . $bg] ConfigText $a.button5.5 Reverse # upvar #0 ${varName}Reverse$index reverse upvar #0 ${varName}Reverse reverse if $reverse { # repository first $a.button5.5 configure -bg $nc $a.button6.6 configure -state disabled } else { # key first $a.button5.5 configure -bg $bg } ConfigText $a.button6.6 Search upvar #0 ${varName}Search search if $search { $a.button6.6 configure -bg $nc } else { $a.button6.6 configure -bg $bg } } entry $a.entry -textvariable ${varName}(result$index) \ -relief flat \ -font {courier 9 roman} # SET THE ENTRY SetEntry $a ${xxEntry}Entry $varName result$index # SET THE ENTRY - end # close the window entry if [regexp {\.dir\.|\.rep\.} $parent] { upvar #0 ${varName}Choice$index choice if ![regexp {^dir|^rep} $choice] { DisableEntry $a ${varName}(result$index) } } # close the window entry - end menu $a.button2.2.menu -tearoff 0 \ -font {courier 9 roman} menu $a.button3.3.menu -tearoff 0 \ -font {courier 9 roman} # $a.button1.1 configure \ -command "ReduceEntry $a $entryName \ ${varName}(result$index) {$listName}" $a.button1.1 configure \ -command "ReduceEntry $a $entryName \ ${varName}(result$index)" if [regexp {\.ent\.} $type] { $a.button4.4 configure \ -command "RemoveEntry $a $entryName \ ${varName}(result$index)" } else { $a.button4.4 configure -command "PerformCheck $a.entry \ $entryName ${varName}(result$index)" } $a.button5.5 configure \ -command "ReverseEntry $a $entryName $varName $index" $a.button6.6 configure \ -command "SearchEntry $a $entryName $varName $index" pack propagate $a false pack $a.button1 -side left pack propagate $a.button1 false pack $a.button3 -side left pack propagate $a.button3 false pack $a.button2 -side left pack propagate $a.button2 false pack $a.entry -side left if $flag { pack $a.button4 -side right pack propagate $a.button4 false } if [regexp {\.rep\.} $type] { pack $a.button5 -side right pack $a.button6 -side right pack propagate $a.button5 false pack propagate $a.button6 false } pack $a.button1.1 -fill both pack $a.button2.2 -fill both pack $a.entry -fill both -expand true pack $a.button3.3 -fill both pack $a.button4.4 -fill both pack $a.button5.5 -fill both pack $a.button6.6 -fill both pack $a -side bottom -fill x -pady .2c # bind $a.entry \ "CompleteEntry %W $entryName ${varName}(result$index) \ {$listName} %A; break" bind $a.entry \ "CompleteEntry %W $entryName ${varName}(result$index) \ %A; break" bindtags $a.entry [list Entry $a.entry all] label $parentLabel.lb1 -bg #ffffcc -relief solid -borderwidth 1 ConfigText $parentLabel.lb1 " Backward " label $parentLabel.lb2 -bg #ffffcc -relief solid -borderwidth 1 ConfigText $parentLabel.lb2 " Forward " label $parentLabel.lb3 -bg #ffffcc -relief solid -borderwidth 1 ConfigText $parentLabel.lb3 \ " Select One of The More Recent Entries " label $parentLabel.lb4 -bg #ffffcc -relief solid -borderwidth 1 if [regexp {\.ent\.} $type] { ConfigText $parentLabel.lb4 " Remove the Current Entry " } else { ConfigText $parentLabel.lb4 " Check " } label $parentLabel.lb5 -bg #ffffcc -relief solid -borderwidth 1 ConfigText $parentLabel.lb5 \ " Reverse " label $parentLabel.lb6 -bg #ffffcc -relief solid -borderwidth 1 ConfigText $parentLabel.lb6 \ " Search " bind $a.button1 \ "DisplayMessage $parentLabel.lb1 $a nw 0 1 0 3" bind $a.button1 "DeleteMessage" bind $a.button1.1