Bsoft  2.1.0
Bernard's software package
brun_panel.tcl
Go to the documentation of this file.
1 ##
2 # @file brun_panel.tcl
3 #
4 # @brief Panel to construct and run Bsoft command lines
5 #
6 # @author Bernard Heymann
7 # @date Created: 19990822
8 # @date Modified: 20191023
9 
10 set font {Helvetica 14}
11 set bigfont {Helvetica 24}
12 set smallfont {Helvetica 10}
13 
14 set bshow_script $Bsoft/tcltk
15 set bshow_lib $Bsoft/lib
16 set bsoft_bin $Bsoft/bin
17 
18 proc saveCommand { filename mode } {
19  global command
20 
21  if { [catch {open $filename $mode} f] } { return }
22 
23  puts $f "$command\n"
24 
25  close $f
26 }
27 
28 # fileDialog
29 # A file is selected for reading or writing using a standard dialog
30 #
31 # Arguments:
32 # operation "save" or "append".
33 
34 proc fileDialog { operation } {
35  set filename [tk_getSaveFile]
36 
37  if { [string length $filename] < 1 } { return }
38 
39  if { [string compare $operation "save"] == 0 } {
40  saveCommand $filename "w"
41  } elseif { [string compare $operation "append"] == 0 } {
42  saveCommand $filename "a"
43  }
44 }
45 
46 # constructCommand
47 # Constructs the command line from a list containing the program name,
48 # the options, and the file names
49 #
50 # Arguments:
51 # c the entry for the command
52 # f the frame for files
53 # t the text with options
54 
55 proc constructCommand { c f t } {
56  set cmd [lindex [$c get] 0]
57 
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]
62  if { $on == 1 } {
63  append cmd " " $opt " " [$w.e1 get]
64  }
65  }
66  }
67 
68  foreach w [winfo children $f] {
69  upvar [$w.label cget -variable] on
70  if { $on == 1 } {
71  append cmd " " [$w.name get]
72  }
73  }
74 
75  $c delete 0 end
76  $c insert 0 $cmd
77 }
78 
79 # parseUsageLine
80 # Parses the usage line
81 #
82 # Arguments:
83 # c the entry for the command
84 # f the frame for files
85 # t the text with options
86 # line the line
87 
88 proc parseUsageLine { c f t line } {
89  global font
90 
91  destroy .usage
92  label .usage -font $font -justify left -text $line -background yellow
93 
94  set words [split $line]
95  for { set i 3 } { $i < [llength $words] } { incr i } {
96  set w $f.$i
97  frame $w
98  set fn [lindex $words $i]
99  checkbutton $w.label -text $fn -variable $fn -relief flat \
100  -background cyan -command "constructCommand $c $f $t"
101  $w.label deselect
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"
107  }
108 
109  if { $i > 3 } { pack $f -side bottom -pady 2 -fill x -expand no }
110  pack .usage -side bottom -pady 2 -anchor w
111 }
112 
113 # parseOptionLine
114 # Parses one line starting with an option
115 #
116 # Arguments:
117 # c the entry for the command
118 # f the frame for files
119 # t the text with options
120 # line the line
121 
122 proc parseOptionLine { c f t line } {
123  global font smallfont
124  if [regexp "^--" $line] { return }
125 
126  set words [split $line]
127  set b [lindex $words 0]
128  set v [lindex $words 1]
129 
130  set d ""
131  foreach word [lrange $words 2 end] {
132  if { $word != "" } {
133  lappend d $word
134  }
135  }
136 
137  set w $t.$b
138  frame $w
139  entry $w.e1 -width [string length $v]
140  $w.e1 insert 0 $v
141 
142  checkbutton $w.label -text "$b" -variable $b -relief flat \
143  -background cyan -command "constructCommand $c $f $t"
144  $w.label deselect
145 
146  if { $v > " " } {
147  pack $w.label $w.e1 -side left -pady 1 -padx 4
148  } else {
149  pack $w.label -side left -pady 1 -padx 4
150  }
151 
152  $t window create end -window $w -padx 4 -pady 1
153  $t insert end [join $d " "]
154  $t insert end "\n"
155 
156  bind $w.e1 <KeyRelease> "constructCommand $c $f $t"
157 }
158 
159 # parseCommand
160 # Parses a command line to fill in options
161 #
162 # Argument:
163 # t text window name
164 # cmd command line
165 
166 proc parseCommand { t cmd } {
167  set vf 0
168  foreach word $cmd {
169  if ![string first "-" $word] {
170  set vf 0
171 # set word [string trimleft $word "-"]
172  foreach w [winfo children $t] {
173  if [regexp -- $word $w] {
174  $w.label select
175  if { [$w.e1 get] > " " } {
176  set vf 1
177  }
178  break
179  }
180  }
181  } elseif { $vf } {
182  $w.e1 delete 0 end
183  $w.e1 insert 0 $word
184  set vf 0
185  }
186  }
187 }
188 
189 # selectProgram
190 # Select the program and get the options from the program
191 #
192 # Argument:
193 # w window name
194 # cmd command line
195 
196 proc selectProgram { w cmd } {
197  global font
198  global smallfont
199 
200  set wn $w
201  if [string equal "." $w] { set wn "" }
202 
203  set c $wn.cmd
204  set f $wn.file
205  set t $wn.txt
206  set g $wn.grid
207 
208  destroy $f
209  destroy $t
210  destroy $g
211  destroy $wn.hscroll
212  destroy $wn.vscroll
213 
214  set prog [lindex $cmd 0]
215 # puts "Program: $prog"
216  if { [ string first "brun" $cmd ] > -1 } { return }
217  if ![ file executable $prog ] { return }
218 
219  catch { exec $prog } result
220 
221  $c.e delete 0 end
222  $c.e insert 0 $cmd
223 
224  frame $f
225 
226  frame $g
227  pack $g -side bottom -expand yes -fill both -padx 1 -pady 1
228 
229  grid rowconfig $g 0 -weight 1 -minsize 0
230  grid columnconfig $g 0 -weight 1 -minsize 0
231 
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
236 
237  scrollbar $wn.hscroll -orient horizontal -command "$t xview"
238  scrollbar $wn.vscroll -orient vertical -command "$t yview"
239 
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
246 
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
253  } else {
254  $t insert end "$line\n"
255  }
256  }
257 
258  parseCommand $t $cmd
259 }
260 
261 # programInterface
262 # Generate the program interface
263 #
264 # Argument:
265 # w window name
266 # cl command line
267 
268 proc programInterface { w cl } {
269  global bsoft_bin
270 
271  wm title $w "Bsoft program interface"
272 
273  set wn $w
274  if [string equal "." $w] { set wn "" }
275 
276  $w configure -menu $wn.menu
277 
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"
287 
288  bind all <Control-q> { exit }
289  bind all <Command-q> { exit }
290 
291 # puts "cl: $cl"
292 
293  set command "Select a program and fill in the options"
294  set program ""
295  if [string length $cl] {
296  set command "$bsoft_bin/$cl"
297  set p "$bsoft_bin/"
298  append p [lindex $cl 0]
299  if [ file executable $p ] { set program $p }
300  }
301 # puts "Command: $command"
302 # puts "Program: $program"
303 
304  set c $wn.cmd
305  frame $c
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
309 
310  frame $wn.buttons
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/*]]]
316  set i 0
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]
322  incr i
323  }
324  }
325 
326  if [string length $command] { selectProgram $w $command }
327 
328 # set logfile "b.log"
329 
330  button $wn.buttons.run -text Execute -command { executeCommand $command "b.log" }
331  button $wn.buttons.dismiss -text Dismiss -command "destroy $w"
332 
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
335 
336  bind . <Return> { selectProgram . [.cmd.e get] }
337 }
338 
339