# NAME # txtWin.w - create a simple text window. # # SYNOPSIS # Source txtWin.w # # DESCRIPTION # # AUTHOR # John Chambers # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Create a window for displaying text. It will have two scrollbars, and a # # little "*" button in the corner that will clear the window. It it withdrawn # # and not shown by default. You need a "wm deiconify $w" command to make it # # appear on the screen. We use this to create the .hdr and .src windows. The # # x arg is the command to attach to the '*' button in the lower-right corner. # # If there are extra args, they are used for text for a popup over the '*' # # button, and should be a brief explanation of what the x command does. # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # proc txtWin {w x args} { global D me toplevel $w -bd 0 wm withdraw $w frame $w.s -width 10 -height 10 -bd 0 pack $w.s -in $w -side right -fill y text $w.t -width 60 -height 10 -bd 0 -relief ridge \ -yscrollcommand "$w.s.y set" \ -xscrollcommand "$w.sbx set" \ -wrap none -bd 1 -relief flat scrollbar $w.s.y -command "$w.t yview" -width 8 -orient vertical scrollbar $w.sbx -command "$w.t xview" -width 8 -orient horizontal button $w.s.x -command $x -bd 1 -text * -padx 0 -pady 0 -highlightthickness 0 BindHelp $w.s.x $args pack $w.s.x -in $w.s -side bottom -fill x pack $w.s.y -in $w.s -side top -fill y -expand 1 pack $w.sbx -in $w -side bottom -fill x pack $w.t -in $w -side left -expand 1 -fill both } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Save the current message to a file different than $P. If args are present, # # they are taken as a command to be run to process the text. If there are no # # args, we pop up a window to ask the user for a file name. # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # proc txtSaveAs {w args} { global D P me txtPath if {$args == {}} { if {$D>1} {Msg "Create .txtsaveas window."} if {![info exists txtPath] || $txtPath == {}} {set txtPath [pwd]/} if [winfo exists .txtsaveas] {destroy .txtsaveas} toplevel .txtsaveas -bd 2 -relief raised wm title .txtsaveas "Save $w" frame .txtsaveas.fldr # label .txtsaveas.fldr.label -text To -anchor e entry .txtsaveas.fldr.flder -textvariable txtPath -width 40 bind .txtsaveas.fldr.flder "txtSave $w" frame .txtsaveas.bttn button .txtsaveas.bttn.move -text Save -command "txtSave $w" button .txtsaveas.bttn.no -text Cancel -command { grab release .txtsaveas wm withdraw .txtsaveas } # pack .txtsaveas.fldr.label -side left -in .txtsaveas.fldr pack .txtsaveas.fldr.flder -side left -in .txtsaveas.fldr -fill x -expand 1 pack .txtsaveas.bttn.move .txtsaveas.bttn.no -in .txtsaveas.bttn -side left -expand 1 pack .txtsaveas.fldr .txtsaveas.bttn -fill x -expand 1 wm deiconify .txtsaveas raise .txtsaveas } else { if {$D>1} {Msg "Send $w to \"$args\" process."} if [catch {open "| $args" w} f] { Msg "Can't run \"$args\" ($f)" return } Win2File $w $f # fileevent readable $f "txtSaveAsRdr $f" if {$D>1} {Msg "Sent $w to \"$args\" process."} } if {$D>1} {puts "$me/txtSaveAs: done."} } #proc txtSaveAsEvent {f} { # global D me # if {$D>1} {puts "$me/txtSaveAsRdr: Event."} # if {[puts $f "\n"] >= 0} { # if {$D>1} {puts "$me/txtSaveAsRdr: Wrote."} # } else { # if {$D>1} {puts "$me/txtSaveAsRdr: Close $f ..."} # close $f # if {$D>1} {puts "$me/txtSaveAsRdr: Closed $f."} # } #} # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Save a window. We trust that txtPath has already been set up to # tell us where to write the data. proc txtSave {w} { global D me env txtPath if {![info exists txtPath] || $txtPath == {}} { Msg "txtSave: No txtPath defined." return } if {$D>0} {Msg "Save $w to \"$txtPath\" ..."} if [catch {open $txtPath w} f] { Msg "Can't write \"$txtPath\" ($f)" return } Win2File $w $f } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Write the contents of a (text) widget to a file. We do it with one # big write. There are likely to be problems if the widget contains # objects other than text characters. proc Win2File {w f} { global D me set l 1 # set e [$w index end] # if {$D>1} {puts "$me/Win2File: $e lines in $w."} # while {$l < $e} { # set line [$w get $l.0 $l.end] # puts $f $line # incr l # } puts $f [$w get 1.0 end-1c] flush $f if {$D>1} {puts "$me/Win2File: fconfigure $f -blocking 0"} fconfigure $f -blocking 0 if {$D>1} {puts "$me/Win2File: Close $f ..."} close $f if {$D>1} {puts "$me/Win2File: Closed $f."} }