1 |
# SCCSid "$SunId$ LBL" |
2 |
# |
3 |
# Get help from a file, formatted like so: |
4 |
# |
5 |
# .category1.topic1 |
6 |
# |
7 |
# A bunch of unformatted text... |
8 |
# |
9 |
# .category2.topic2 |
10 |
# |
11 |
# etc. |
12 |
# |
13 |
|
14 |
# Calling program should define global help file directory as $helplib |
15 |
|
16 |
proc helplink {win file cat top} { # Create link for window(s) |
17 |
foreach w $win { |
18 |
bind $w <Control-Button-1> "gethelp $file $cat $top" |
19 |
} |
20 |
} |
21 |
|
22 |
set curhelp(file) {} |
23 |
|
24 |
proc gethelp {helpfile category topic} { # Open help window |
25 |
global curhelp helpfontwidth |
26 |
if {! [winfo exists .helpwin]} { # Set up window |
27 |
toplevel .helpwin |
28 |
wm minsize .helpwin 450 350 |
29 |
wm iconbitmap .helpwin question |
30 |
frame .helpwin.but -geometry 150x350 |
31 |
pack .helpwin.but -side right -fill none -expand no |
32 |
label .helpwin.but.lab -textvariable curhelp(title) |
33 |
place .helpwin.but.lab -relx .1667 -rely 0 |
34 |
helplink .helpwin.but.lab help help intro |
35 |
menubutton .helpwin.but.catb -relief raised -height 1 \ |
36 |
-text Category -menu .helpwin.but.catb.m |
37 |
menu .helpwin.but.catb.m |
38 |
place .helpwin.but.catb -relx .1667 -rely .1100 \ |
39 |
-relwidth .5000 -relheight .0500 |
40 |
helplink .helpwin.but.catb help help category |
41 |
menubutton .helpwin.but.topb -relief raised -height 1 \ |
42 |
-text Topic -menu .helpwin.but.topb.m |
43 |
menu .helpwin.but.topb.m |
44 |
place .helpwin.but.topb -relx .1667 -rely .2200 \ |
45 |
-relwidth .5000 -relheight .0500 |
46 |
helplink .helpwin.but.topb help help topic |
47 |
button .helpwin.but.srchb -text Grep -relief raised \ |
48 |
-height 1 -command {helpsearch $curhelp(search)} |
49 |
place .helpwin.but.srchb -relx .1667 -rely .3500 \ |
50 |
-relwidth .5000 -relheight .0500 |
51 |
helplink .helpwin.but.srchb help navigate search |
52 |
entry .helpwin.but.srche -relief sunken -insertofftime 0 \ |
53 |
-textvariable curhelp(search) |
54 |
place .helpwin.but.srche -relx .1667 -rely .4200 \ |
55 |
-relwidth .6667 -relheight .0500 |
56 |
bind .helpwin.but.srche <Return> {set curhelp(msg) {} |
57 |
helpupdate} |
58 |
helplink .helpwin.but.srche help navigate search |
59 |
label .helpwin.but.msg -textvariable curhelp(msg) |
60 |
place .helpwin.but.msg -relx .1667 -rely .4800 |
61 |
button .helpwin.but.next -text Next -relief raised -height 1 \ |
62 |
-command {eval helphist new $curhelp(next)} |
63 |
place .helpwin.but.next -relx .1667 -rely .7500 \ |
64 |
-relwidth .5 -relheight .0500 |
65 |
helplink .helpwin.but.next help navigate next |
66 |
button .helpwin.but.back -text Back -relief raised -height 1 \ |
67 |
-command "helphist back" |
68 |
place .helpwin.but.back -relx .1667 -rely .6500 \ |
69 |
-relwidth .5 -relheight .0500 |
70 |
helplink .helpwin.but.back help navigate back |
71 |
button .helpwin.but.forw -text Forward -relief raised -height 1\ |
72 |
-command "helphist forward" |
73 |
place .helpwin.but.forw -relx .1667 -rely .5500 \ |
74 |
-relwidth .5 -relheight .0500 |
75 |
helplink .helpwin.but.forw help navigate forward |
76 |
button .helpwin.but.done -text Done -relief raised -height 1 \ |
77 |
-command "destroy .helpwin ; helpopen {}" |
78 |
place .helpwin.but.done -relx .1667 -rely .9000 \ |
79 |
-relwidth .5 -relheight .0500 |
80 |
helplink .helpwin.but.done help help done |
81 |
text .helpwin.txt -wrap word -width 48 -height 20 -bd 2 \ |
82 |
-yscrollcommand ".helpwin.sb set" -relief raised \ |
83 |
-font -*-courier-medium-r-normal--14-*-*-*-*-*-iso8859-1 |
84 |
.helpwin.txt tag configure highlight \ |
85 |
-font -*-courier-bold-r-normal--14-*-*-*-*-*-iso8859-1 |
86 |
set helpfontwidth 9 |
87 |
scrollbar .helpwin.sb -relief flat \ |
88 |
-command ".helpwin.txt yview" |
89 |
pack .helpwin.sb -side right -fill y |
90 |
pack .helpwin.txt -expand yes -fill both |
91 |
helplink .helpwin.sb help helpwin scroll |
92 |
helplink .helpwin.txt help helpwin intro |
93 |
tkwait visibility .helpwin |
94 |
} elseif {! [winfo ismapped .helpwin]} { # map window |
95 |
wm deiconify .helpwin |
96 |
} else { # raise window |
97 |
raise .helpwin |
98 |
} |
99 |
focus .helpwin.but.srche |
100 |
helpopen $helpfile |
101 |
helphist new $category $topic |
102 |
} |
103 |
|
104 |
proc helpopen fname { # open the named help file |
105 |
global curhelp helplib |
106 |
if {"$fname" == "$curhelp(file)"} {return} |
107 |
if {"$curhelp(file)" != {}} { |
108 |
close $curhelp(fid) |
109 |
unset curhelp |
110 |
} |
111 |
if {[set curhelp(file) $fname] == {}} {return} |
112 |
# Complete file name as required |
113 |
if {! [info exists helplib]} { |
114 |
set helplib /usr/local/lib/tk/help |
115 |
} |
116 |
if {"[file rootname $fname]" == "$fname"} { |
117 |
append fname .hlp |
118 |
} |
119 |
if {! [string match {[~/.]*} $fname]} { |
120 |
set fname $helplib/$fname |
121 |
} |
122 |
wm title .helpwin $fname |
123 |
set curhelp(title) "[string toupper\ |
124 |
[file rootname [file tail $fname]]] HELP" |
125 |
wm iconname .helpwin [string tolower $curhelp(title)] |
126 |
.helpwin.txt configure -state normal |
127 |
.helpwin.txt delete 1.0 end |
128 |
.helpwin.txt insert end "Loading $fname..." |
129 |
update |
130 |
set curhelp(fid) [open $fname r] |
131 |
set curhelp(index) {} |
132 |
set catlist {} |
133 |
while {[gets $curhelp(fid) li] >= 0} { |
134 |
if [regexp -nocase {^\.([A-Z][A-Z0-9]*)\.([A-Z][A-Z0-9]*)$} \ |
135 |
$li dummy cat top] { |
136 |
lappend curhelp(index) "$cat $top [tell $curhelp(fid)]" |
137 |
if {[lsearch -exact $catlist $cat] < 0} { |
138 |
lappend catlist $cat |
139 |
} |
140 |
} |
141 |
} |
142 |
.helpwin.but.catb.m delete 0 last |
143 |
foreach cat $catlist { |
144 |
.helpwin.but.catb.m add command -label $cat \ |
145 |
-command "helphist new $cat intro" |
146 |
} |
147 |
set curhelp(category) None |
148 |
.helpwin.but.topb.m delete 0 last |
149 |
set curhelp(topic) None |
150 |
set curhelp(next) {} |
151 |
helphist clear |
152 |
} |
153 |
|
154 |
proc helpgoto {cat top} { # find selected category and topic |
155 |
global curhelp |
156 |
# Capitalize "just in case" |
157 |
set cat [string toupper $cat] |
158 |
set top [string toupper $top] |
159 |
# Change topic menu if category is changed |
160 |
set curhelp(topic) $top |
161 |
if {"$cat" != "$curhelp(category)"} { |
162 |
set curhelp(category) $cat |
163 |
set toplist {} |
164 |
set n [llength $curhelp(index)] |
165 |
for {set i 0} {$i < $n} {incr i} { |
166 |
if {"[string toupper [lindex [lindex \ |
167 |
$curhelp(index) $i] 0]]" == "$cat"} { |
168 |
lappend toplist [lindex [lindex \ |
169 |
$curhelp(index) $i] 1] |
170 |
} |
171 |
} |
172 |
.helpwin.but.topb.m delete 0 last |
173 |
foreach top $toplist { |
174 |
.helpwin.but.topb.m add command -label $top \ |
175 |
-command "helphist new \$curhelp(category) $top" |
176 |
} |
177 |
} |
178 |
helpupdate |
179 |
} |
180 |
|
181 |
proc helpupdate {} { # update help text window |
182 |
global curhelp helpfontwidth |
183 |
# Print category and topic |
184 |
set linelen [expr "[winfo width .helpwin.txt] / $helpfontwidth - 1"] |
185 |
.helpwin.txt configure -state normal |
186 |
.helpwin.txt delete 1.0 end |
187 |
set titlen [expr [string length $curhelp(category)] + \ |
188 |
[string length $curhelp(topic)] + 1] |
189 |
for {set i [expr ($linelen - $titlen)/2]} {[incr i -1] >= 0} {} { |
190 |
.helpwin.txt insert end { } |
191 |
} |
192 |
.helpwin.txt insert end "$curhelp(category) $curhelp(topic)\n" |
193 |
.helpwin.txt tag add highlight "1.0 lineend - $titlen c" "1.0 lineend" |
194 |
# Search for it in file |
195 |
if {"$curhelp(category) $curhelp(topic)" != |
196 |
"[string toupper $curhelp(next)]"} { |
197 |
for {set i [llength $curhelp(index)]} {[incr i -1] >= 0} {} { |
198 |
if {"$curhelp(category) $curhelp(topic)" == |
199 |
[lrange [string toupper [lindex \ |
200 |
$curhelp(index) $i]] 0 1]} { |
201 |
break |
202 |
} |
203 |
} |
204 |
if {$i < 0} { |
205 |
.helpwin.txt insert end "\nNo such help topic." |
206 |
.helpwin.txt configure -state disabled |
207 |
return ".$curhelp(category).$curhelp(topic) not found\ |
208 |
in $curhelp(file)" |
209 |
} |
210 |
seek $curhelp(fid) [lindex [lindex $curhelp(index) $i] 2] |
211 |
} |
212 |
# Load help text into our window |
213 |
set linepos 0 |
214 |
while {[set ll [gets $curhelp(fid) li]] >= 0 && ! [regexp -nocase \ |
215 |
{^\.([A-Z][A-Z0-9]*)\.([A-Z][A-Z0-9]*)$} \ |
216 |
$li dummy cat top]} { |
217 |
if {$ll == 0} { # paragraph |
218 |
if $linepos { |
219 |
.helpwin.txt insert end "\n\n" |
220 |
set linepos 0 |
221 |
} else { |
222 |
.helpwin.txt insert end "\n" |
223 |
} |
224 |
} else { # line |
225 |
.helpwin.txt insert end $li |
226 |
incr linepos $ll |
227 |
.helpwin.txt insert end { } |
228 |
incr linepos |
229 |
# Highlight search string match |
230 |
if {"$curhelp(search)" != {}} { |
231 |
if [regexp -nocase -indices \ |
232 |
$curhelp(search) $li mi] { |
233 |
.helpwin.txt tag add highlight\ |
234 |
"end - 1 c - $ll c\ |
235 |
+ [lindex $mi 0] c"\ |
236 |
"end - $ll c\ |
237 |
+ [lindex $mi 1] c" |
238 |
} |
239 |
} |
240 |
# Add extra space at the end of a sentence |
241 |
if [regexp {[.?!:][)"']?$} $li] { |
242 |
.helpwin.txt insert end { } |
243 |
incr linepos |
244 |
} |
245 |
} |
246 |
} |
247 |
# Highlight next category and topic |
248 |
if {$ll > 0} { |
249 |
.helpwin.txt insert end "Next: $cat $top" |
250 |
.helpwin.txt tag add highlight "end linestart" end |
251 |
set curhelp(next) "$cat $top" |
252 |
.helpwin.but.next configure -state normal |
253 |
} else { |
254 |
.helpwin.but.next configure -state disabled |
255 |
} |
256 |
.helpwin.txt configure -state disabled |
257 |
return {} |
258 |
} |
259 |
|
260 |
proc helphist {op args} { # access help history list |
261 |
global curhelp |
262 |
switch -exact $op { |
263 |
clear { |
264 |
set curhelp(histlst) {} |
265 |
set curhelp(histpos) 0 |
266 |
set gotoit 0 |
267 |
} |
268 |
new { |
269 |
set curhelp(histpos) \ |
270 |
[expr [llength $curhelp(histlst)] - 1] |
271 |
if {"[string toupper [lindex $curhelp(histlst) \ |
272 |
$curhelp(histpos)]]" |
273 |
!= "[string toupper $args]"} { |
274 |
lappend curhelp(histlst) $args |
275 |
incr curhelp(histpos) |
276 |
} |
277 |
set gotoit 1 |
278 |
} |
279 |
append { |
280 |
lappend curhelp(histlst) $args |
281 |
set gotoit 0 |
282 |
} |
283 |
back { |
284 |
incr curhelp(histpos) -1 |
285 |
set gotoit 1 |
286 |
} |
287 |
forward { |
288 |
incr curhelp(histpos) 1 |
289 |
set gotoit 1 |
290 |
} |
291 |
} |
292 |
if {$curhelp(histpos) + 1 >= [llength $curhelp(histlst)]} { |
293 |
.helpwin.but.forw configure -state disabled |
294 |
} else { |
295 |
.helpwin.but.forw configure -state normal |
296 |
} |
297 |
if {$curhelp(histpos) <= 0} { |
298 |
.helpwin.but.back configure -state disabled |
299 |
} else { |
300 |
.helpwin.but.back configure -state normal |
301 |
} |
302 |
if $gotoit { |
303 |
eval helpgoto [lindex $curhelp(histlst) $curhelp(histpos)] |
304 |
} |
305 |
} |
306 |
|
307 |
proc helpsearch word { # search for occurances of the given word |
308 |
global curhelp |
309 |
if {"$curhelp(search)" == {}} { |
310 |
set curhelp(msg) "No pattern." |
311 |
return 0 |
312 |
} |
313 |
set nmatches 0 |
314 |
set cat [lindex [lindex $curhelp(index) 0] 0] |
315 |
set top [lindex [lindex $curhelp(index) 0] 1] |
316 |
seek $curhelp(fid) [lindex [lindex $curhelp(index) 0] 2] |
317 |
set foundmatch 0 |
318 |
while {[gets $curhelp(fid) li] >= 0} { |
319 |
if [regexp -nocase {^\.([A-Z][A-Z0-9]*)\.([A-Z][A-Z0-9]*)$} \ |
320 |
$li dummy cat top] { |
321 |
set foundmatch 0 |
322 |
} elseif {! $foundmatch && \ |
323 |
[regexp -nocase $curhelp(search) $li]} { |
324 |
set foundmatch 1 |
325 |
if $nmatches { |
326 |
helphist append $cat $top |
327 |
} else { |
328 |
helphist new $cat $top |
329 |
} |
330 |
incr nmatches |
331 |
} |
332 |
} |
333 |
if $nmatches { |
334 |
set curhelp(msg) "$nmatches topic(s)." |
335 |
} else { |
336 |
set curhelp(msg) "Not found." |
337 |
} |
338 |
return $nmatches |
339 |
} |