4 # @brief Panel to construct and run Bsoft command lines
6 # @author Bernard Heymann
7 # @date Created: 19990822
8 # @date Modified: 20191023
10 set font {Helvetica 14}
11 set bigfont {Helvetica 24}
12 set smallfont {Helvetica 10}
14 set bshow_script $Bsoft/tcltk
15 set bshow_lib $Bsoft/lib
16 set bsoft_bin $Bsoft/bin
18 proc saveCommand { filename mode } {
21 if { [catch {open $filename $mode} f] } { return }
29 # A file is selected for reading or writing using a standard dialog
32 # operation "save" or "append".
34 proc fileDialog { operation } {
35 set filename [tk_getSaveFile]
37 if { [string length $filename] < 1 } { return }
39 if { [string compare $operation "save"] == 0 } {
40 saveCommand $filename "w"
41 } elseif { [string compare $operation "append"] == 0 } {
42 saveCommand $filename "a"
47 # Constructs the command line from a list containing the program name,
48 # the options, and the file names
51 # c the entry for the command
52 # f the frame for files
53 # t the text with options
55 proc constructCommand { c f t } {
56 set cmd [lindex [$c get] 0]
58 foreach w [winfo children $t] {
59 if [winfo exists $w.label] {
60 upvar [$w.label cget -variable] on
61 set opt [$w.label cget -text]
63 append cmd " " $opt " " [$w.e1 get]
68 foreach w [winfo children $f] {
69 upvar [$w.label cget -variable] on
71 append cmd " " [$w.name get]
80 # Parses the usage line
83 # c the entry for the command
84 # f the frame for files
85 # t the text with options
88 proc parseUsageLine { c f t line } {
92 label .usage -font $font -justify left -text $line -background yellow
94 set words [split $line]
95 for { set i 3 } { $i < [llength $words] } { incr i } {
98 set fn [lindex $words $i]
99 checkbutton $w.label -text $fn -variable $fn -relief flat \
100 -background cyan -command "constructCommand $c $f $t"
102 entry $w.name -width 50
103 button $w.button -text "Browse ..." -command "getFilename $w.name"
104 pack $w.label $w.name $w.button -side left -pady 1 -padx 4
105 pack $w -side top -fill x -expand no
106 bind $w.name <KeyRelease> "constructCommand $c $f $t"
109 if { $i > 3 } { pack $f -side bottom -pady 2 -fill x -expand no }
110 pack .usage -side bottom -pady 2 -anchor w
114 # Parses one line starting with an option
117 # c the entry for the command
118 # f the frame for files
119 # t the text with options
122 proc parseOptionLine { c f t line } {
123 global font smallfont
124 if [regexp "^--" $line] { return }
126 set words [split $line]
127 set b [lindex $words 0]
128 set v [lindex $words 1]
131 foreach word [lrange $words 2 end] {
139 entry $w.e1 -width [string length $v]
142 checkbutton $w.label -text "$b" -variable $b -relief flat \
143 -background cyan -command "constructCommand $c $f $t"
147 pack $w.label $w.e1 -side left -pady 1 -padx 4
149 pack $w.label -side left -pady 1 -padx 4
152 $t window create end -window $w -padx 4 -pady 1
153 $t insert end [join $d " "]
156 bind $w.e1 <KeyRelease> "constructCommand $c $f $t"
160 # Parses a command line to fill in options
166 proc parseCommand { t cmd } {
169 if ![string first "-" $word] {
171 # set word [string trimleft $word "-"]
172 foreach w [winfo children $t] {
173 if [regexp -- $word $w] {
175 if { [$w.e1 get] > " " } {
190 # Select the program and get the options from the program
196 proc selectProgram { w cmd } {
201 if [string equal "." $w] { set wn "" }
214 set prog [lindex $cmd 0]
215 # puts "Program: $prog"
216 if { [ string first "brun" $cmd ] > -1 } { return }
217 if ![ file executable $prog ] { return }
219 catch { exec $prog } result
227 pack $g -side bottom -expand yes -fill both -padx 1 -pady 1
229 grid rowconfig $g 0 -weight 1 -minsize 0
230 grid columnconfig $g 0 -weight 1 -minsize 0
232 text $t -wrap none -relief sunken -bd 2 \
233 -xscrollcommand "$wn.hscroll set" \
234 -yscrollcommand "$wn.vscroll set" \
235 -setgrid 1 -highlightthickness 0 -pady 2 -padx 3
237 scrollbar $wn.hscroll -orient horizontal -command "$t xview"
238 scrollbar $wn.vscroll -orient vertical -command "$t yview"
240 grid $t -padx 1 -in $g -pady 1 \
241 -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
242 grid $wn.vscroll -in $g -padx 1 -pady 1 \
243 -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
244 grid $wn.hscroll -in $g -padx 1 -pady 1 \
245 -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news
247 $t insert end "Description:\n"
248 foreach line [split $result "\n"] {
249 if ![string first "-" $line] {
250 parseOptionLine $c.e $f $t $line
251 } elseif ![string first "Usage" $line] {
252 parseUsageLine $c.e $f $t $line
254 $t insert end "$line\n"
262 # Generate the program interface
268 proc programInterface { w cl } {
271 wm title $w "Bsoft program interface"
274 if [string equal "." $w] { set wn "" }
276 $w configure -menu $wn.menu
278 menu $wn.menu -tearoff 0
279 menu $wn.menu.file -tearoff 0
280 $wn.menu add cascade -label "File" -menu $wn.menu.file -underline 0
281 $wn.menu.file add command -label "Save ..." -command { fileDialog "save" } \
282 -underline 0 -accelerator "Ctrl-s"
283 $wn.menu.file add command -label "Append ..." -command { fileDialog "append" } \
284 -underline 0 -accelerator "Ctrl-a"
285 $wn.menu.file add command -label "Quit" -command { exit } -underline 0 \
286 -accelerator "Ctrl-q"
288 bind all <Control-q> { exit }
289 bind all <Command-q> { exit }
293 set command "Select a program and fill in the options"
295 if [string length $cl] {
296 set command "$bsoft_bin/$cl"
298 append p [lindex $cl 0]
299 if [ file executable $p ] { set program $p }
301 # puts "Command: $command"
302 # puts "Program: $program"
306 entry $c.e -width 100 -xscrollcommand "$c.s set" -textvariable command
307 scrollbar $c.s -relief sunken -orient horiz -command "$c.e xview"
308 pack $c.s $c.e -side bottom -fill x -expand 1 -padx 1m -pady 1
311 menubutton $wn.buttons.program -text "Program" -underline 0 -direction below \
312 -menu $wn.buttons.program.m -relief raised -width 20 \
313 -textvariable program
314 menu $wn.buttons.program.m -tearoff 0
315 set file_list [lsort [glob [file join $bsoft_bin/*]]]
317 foreach p [split $file_list] {
318 set prog [file tail $p]
319 if { [file executable $p] && [file isfile $p] } {
320 $wn.buttons.program.m add command -label $prog \
321 -command "selectProgram $w $p" -columnbreak [expr ($i % 20) == 0]
326 if [string length $command] { selectProgram $w $command }
328 # set logfile "b.log"
330 button $wn.buttons.run -text Execute -command { executeCommand $command "b.log" }
331 button $wn.buttons.dismiss -text Dismiss -command "destroy $w"
333 pack $wn.buttons.program $wn.buttons.run $wn.buttons.dismiss -side left -expand yes
334 pack $wn.buttons $c -side top -fill x -pady 2m -expand no
336 bind . <Return> { selectProgram . [.cmd.e get] }