4 # @brief Window to show text information
6 # @author Bernard Heymann
7 # @date Created: 20010516
8 # @date Modified: 20130123
12 ## @brief Open a text editing window
14 # @param w window name to contain ".text" window.
16 proc openTextWindow { w } {
20 if [string equal $w "."] { set wn "" }
22 # puts "Window name: $wn"
24 if { ![winfo exists $w] || $w == "." } {
25 if { $w != "." } { toplevel $w }
28 # puts "Text window menu: $m"
30 $m add cascade -menu $m.file -label "File" -underline 0
31 menu $m.file -tearoff 0
33 $m.file add command -label "Open" \
34 -command "openTextFile $twin" -underline 0
35 $m.file add command -label "Save" \
36 -command [ list "saveTextFile" $twin $textfile ] -underline 0
37 $m.file add command -label "Save as ..." \
38 -command "saveAsTextFile $twin" -underline 0
39 $m.file add command -label "Close" \
40 -command "$wn.text delete 1.0 end" -underline 0
43 pack $wn.frame -expand yes -fill both -padx 1 -pady 1
44 text $wn.text -width 80 -height 60 -wrap none -undo 1 -background white\
45 -xscrollcommand "$wn.xscroll set" \
46 -yscrollcommand "$wn.yscroll set" \
47 -setgrid 1 -highlightthickness 0 -pady 2 -padx 3
48 scrollbar $wn.xscroll -command "$wn.text xview" \
49 -highlightthickness 0 -orient horizontal
50 scrollbar $wn.yscroll -command "$wn.text yview" \
51 -highlightthickness 0 -orient vertical
53 grid $wn.text -in $wn.frame -padx 1 -pady 1 \
54 -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
55 grid $wn.xscroll -in $wn.frame -padx 1 -pady 1 \
56 -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news
57 grid $wn.yscroll -in $wn.frame -padx 1 -pady 1 \
58 -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
59 grid rowconfig $wn.frame 0 -weight 1 -minsize 0
60 grid columnconfig $wn.frame 0 -weight 1 -minsize 0
66 wm title $w "Text Editor"
68 bind $w <Control-z> { $wn.text edit undo }
69 bind $w <Control-Z> { $wn.text edit redo }
70 bind $w <Command-Z> { $wn.text edit redo }
71 bind $w <Control-w> { destroy $w }
72 bind $w <Command-w> { destroy $w }
76 ## @brief Opens a text window with the supplied information
78 # @param theinfo text information.
80 proc showInfo { theinfo } {
82 set wtxt [openTextWindow $w]
83 wm title $w "Bsoft information window"
84 wm iconname $w "Bsoft info"
86 $wtxt insert 1.0 $theinfo
87 $wtxt mark set insert 1.0
91 ## @brief Reads a text file for editing
93 # @param filename file name (including path).
95 proc editTextFile { filename } {
96 if { [string length $filename] < 1 } { return }
98 readTextFile [openTextWindow $w] $filename
99 set thename [file tail $filename]
101 wm iconname $w $thename
104 ## @brief Opens a text file for editing
106 # @param wtxt text window.
108 proc openTextFile { wtxt } {
109 set filename [tk_getOpenFile -initialdir [pwd]]
110 # set filename [file tail $filename]
111 # puts "Open in $wtxt: $filename"
112 readTextFile $wtxt $filename
115 ## @brief Reads a text file
117 # @param wtxt a text window
118 # @param filename file name (including path).
120 proc readTextFile { wtxt filename } {
122 if { [string length $filename] < 1 } { return }
123 if { [catch {open $filename r} ftxt] } {
124 tk_messageBox -message "File $filename not opened!" -icon error -type ok
127 set thetext [read $ftxt]
130 $wtxt insert 1.0 $thetext
131 set textfile $filename
134 ## @brief Saves a text file using its current name
136 # @param wtxt a text window
137 # @param filename file name (including path).
139 proc saveTextFile { wtxt filename } {
141 if { [string length $filename] < 1 } { return }
142 if { [catch {open $filename w} ftxt] } { return }
143 set thetext [$wtxt get 1.0 end]
146 set textfile $filename
150 ## @brief Saves a text file with a new name
152 # @param wtxt a text window
154 proc saveAsTextFile { wtxt } {
156 set filename [tk_getSaveFile -initialdir [pwd] -initialfile [file tail $textfile]]
157 saveTextFile $wtxt $filename
161 ## @brief Updates text originating from an open channel
163 # @param wtxt a text window
164 # @param chan the channel
166 proc updateText { wtxt chan } {
167 $wtxt configure -state normal
168 $wtxt insert end [read $chan]
169 $wtxt configure -state disabled
172 puts "Closing text output channel"
174 $wtxt configure -state normal
178 ## @brief Executes a command and sends the output to a channel for updating
180 # @param cmd the command (with arguments)
181 # @param logfile a text file to write to.
183 proc executeCommand { cmd logfile } {
185 set p [lindex $cmd 0]
186 if [ file executable $p ] {
187 set textfile $logfile
188 puts "Executing: $cmd"
189 set wtxt [showInfo "$cmd\n\n"]
190 set chan [open |[concat $cmd 2>@1]]
191 fconfigure $chan -blocking 0 -buffering line
192 fileevent $chan readable [list updateText $wtxt $chan]
194 puts "$p not executable!"
198 ## @brief Gets a file name entry
200 # @param w an entry window
202 proc getFilename { w } {
203 set name [tk_getOpenFile]
210 ## @brief Gets a directory entry
212 # @param w an entry window
214 proc getDirectoryName { w } {
215 set dir [tk_chooseDirectory \
216 -initialdir ~ -title "Choose a directory"]