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 -cursor top_left_arrow |
28 |
wm minsize .helpwin 500 400 |
29 |
wm iconbitmap .helpwin question |
30 |
frame .helpwin.but -geometry 150x400 |
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 .0600 |
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 .0600 |
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 .0600 |
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 .0600 |
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 .0600 |
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 .0600 |
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 .0600 |
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 .0600 |
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 helpindex helplib |
106 |
if {"$fname" == "$curhelp(file)"} {return} |
107 |
if {"$curhelp(file)" != {}} { |
108 |
close $curhelp(fid) |
109 |
unset curhelp helpindex |
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(catlist) {} |
132 |
while {[gets $curhelp(fid) li] >= 0} { |
133 |
if [regexp -nocase {^\.([A-Z][A-Z0-9]*)\.([A-Z][A-Z0-9]*)$} \ |
134 |
$li dummy cat top] { |
135 |
lappend helpindex([string toupper $cat]) $top |
136 |
set helpindex([string toupper $cat,$top]) \ |
137 |
[tell $curhelp(fid)] |
138 |
if {[lsearch -exact $curhelp(catlist) $cat] < 0} { |
139 |
lappend curhelp(catlist) $cat |
140 |
} |
141 |
} |
142 |
} |
143 |
.helpwin.but.catb.m delete 0 last |
144 |
foreach cat $curhelp(catlist) { |
145 |
.helpwin.but.catb.m add command -label $cat \ |
146 |
-command "helphist new $cat intro" |
147 |
} |
148 |
set curhelp(category) None |
149 |
.helpwin.but.topb.m delete 0 last |
150 |
set curhelp(topic) None |
151 |
set curhelp(next) {} |
152 |
helphist clear |
153 |
} |
154 |
|
155 |
proc helpgoto {cat top} { # find selected category and topic |
156 |
global curhelp helpindex |
157 |
# Capitalize "just in case" |
158 |
set cat [string toupper $cat] |
159 |
set top [string toupper $top] |
160 |
# Change topic menu if category is changed |
161 |
set curhelp(topic) $top |
162 |
if {"$cat" != "$curhelp(category)"} { |
163 |
set curhelp(category) $cat |
164 |
.helpwin.but.topb.m delete 0 last |
165 |
foreach top $helpindex($cat) { |
166 |
.helpwin.but.topb.m add command -label $top \ |
167 |
-command "helphist new \$curhelp(category) $top" |
168 |
} |
169 |
} |
170 |
helpupdate |
171 |
} |
172 |
|
173 |
proc helpupdate {} { # update help text window |
174 |
global curhelp helpindex helpfontwidth |
175 |
# Print category and topic |
176 |
set linelen [expr "[winfo width .helpwin.txt] / $helpfontwidth - 1"] |
177 |
.helpwin.txt configure -state normal |
178 |
.helpwin.txt delete 1.0 end |
179 |
set titlen [expr [string length $curhelp(category)] + \ |
180 |
[string length $curhelp(topic)] + 1] |
181 |
for {set i [expr ($linelen - $titlen)/2]} {[incr i -1] >= 0} {} { |
182 |
.helpwin.txt insert end { } |
183 |
} |
184 |
.helpwin.txt insert end "$curhelp(category) $curhelp(topic)\n" |
185 |
.helpwin.txt tag add highlight "1.0 lineend - $titlen c" "1.0 lineend" |
186 |
# Search for it in file |
187 |
if {"$curhelp(category) $curhelp(topic)" != |
188 |
"[string toupper $curhelp(next)]"} { |
189 |
if [info exists helpindex($curhelp(category),$curhelp(topic))] { |
190 |
seek $curhelp(fid) $helpindex($curhelp(category),$curhelp(topic)) |
191 |
} else { |
192 |
.helpwin.txt insert end "\nNo such help topic." |
193 |
.helpwin.txt configure -state disabled |
194 |
return ".$curhelp(category).$curhelp(topic) not found\ |
195 |
in $curhelp(file)" |
196 |
} |
197 |
} |
198 |
# Load help text into our window |
199 |
set linepos 0 |
200 |
while {[set ll [gets $curhelp(fid) li]] >= 0 && ! [regexp -nocase \ |
201 |
{^\.([A-Z][A-Z0-9]*)\.([A-Z][A-Z0-9]*)$} \ |
202 |
$li dummy cat top]} { |
203 |
if {$ll == 0} { # paragraph |
204 |
if $linepos { |
205 |
.helpwin.txt insert end "\n\n" |
206 |
set linepos 0 |
207 |
} else { |
208 |
.helpwin.txt insert end "\n" |
209 |
} |
210 |
} else { # line |
211 |
.helpwin.txt insert end $li |
212 |
incr linepos $ll |
213 |
.helpwin.txt insert end { } |
214 |
incr linepos |
215 |
# Highlight search string match |
216 |
if {"$curhelp(search)" != {}} { |
217 |
if [regexp -nocase -indices \ |
218 |
$curhelp(search) $li mi] { |
219 |
.helpwin.txt tag add highlight\ |
220 |
"end - 1 c - $ll c\ |
221 |
+ [lindex $mi 0] c"\ |
222 |
"end - $ll c\ |
223 |
+ [lindex $mi 1] c" |
224 |
} |
225 |
} |
226 |
# Add extra space at the end of a sentence |
227 |
if [regexp {[.?!:][)"']?$} $li] { |
228 |
.helpwin.txt insert end { } |
229 |
incr linepos |
230 |
} |
231 |
} |
232 |
} |
233 |
# Highlight next category and topic |
234 |
if {$ll > 0} { |
235 |
.helpwin.txt insert end "Next: $cat $top" |
236 |
.helpwin.txt tag add highlight "end linestart" end |
237 |
set curhelp(next) "$cat $top" |
238 |
.helpwin.but.next configure -state normal |
239 |
} else { |
240 |
.helpwin.but.next configure -state disabled |
241 |
} |
242 |
.helpwin.txt configure -state disabled |
243 |
return {} |
244 |
} |
245 |
|
246 |
proc helphist {op args} { # access help history list |
247 |
global curhelp |
248 |
switch -exact $op { |
249 |
clear { |
250 |
set curhelp(histlst) {} |
251 |
set curhelp(histpos) 0 |
252 |
set gotoit 0 |
253 |
} |
254 |
new { |
255 |
set curhelp(histpos) \ |
256 |
[expr [llength $curhelp(histlst)] - 1] |
257 |
if {"[string toupper [lindex $curhelp(histlst) \ |
258 |
$curhelp(histpos)]]" |
259 |
!= "[string toupper $args]"} { |
260 |
lappend curhelp(histlst) $args |
261 |
incr curhelp(histpos) |
262 |
} |
263 |
set gotoit 1 |
264 |
} |
265 |
append { |
266 |
lappend curhelp(histlst) $args |
267 |
set gotoit 0 |
268 |
} |
269 |
back { |
270 |
incr curhelp(histpos) -1 |
271 |
set gotoit 1 |
272 |
} |
273 |
forward { |
274 |
incr curhelp(histpos) 1 |
275 |
set gotoit 1 |
276 |
} |
277 |
} |
278 |
if {$curhelp(histpos) + 1 >= [llength $curhelp(histlst)]} { |
279 |
.helpwin.but.forw configure -state disabled |
280 |
} else { |
281 |
.helpwin.but.forw configure -state normal |
282 |
} |
283 |
if {$curhelp(histpos) <= 0} { |
284 |
.helpwin.but.back configure -state disabled |
285 |
} else { |
286 |
.helpwin.but.back configure -state normal |
287 |
} |
288 |
if $gotoit { |
289 |
eval helpgoto [lindex $curhelp(histlst) $curhelp(histpos)] |
290 |
} |
291 |
} |
292 |
|
293 |
proc helpsearch word { # search for occurances of the given word |
294 |
global curhelp helpindex |
295 |
if {"$curhelp(search)" == {}} { |
296 |
set curhelp(msg) "No pattern." |
297 |
return 0 |
298 |
} |
299 |
set nmatches 0 |
300 |
set cat [lindex $curhelp(catlist) 0] |
301 |
set top [lindex $helpindex([string toupper $cat]) 0] |
302 |
set startpos [tell $curhelp(fid)] |
303 |
seek $curhelp(fid) $helpindex([string toupper $cat,$top]) |
304 |
set foundmatch 0 |
305 |
while {[gets $curhelp(fid) li] >= 0} { |
306 |
if [regexp -nocase {^\.([A-Z][A-Z0-9]*)\.([A-Z][A-Z0-9]*)$} \ |
307 |
$li dummy cat top] { |
308 |
set foundmatch 0 |
309 |
} elseif {! $foundmatch && \ |
310 |
[regexp -nocase $curhelp(search) $li]} { |
311 |
set foundmatch 1 |
312 |
if $nmatches { |
313 |
helphist append $cat $top |
314 |
} else { |
315 |
helphist new $cat $top |
316 |
} |
317 |
incr nmatches |
318 |
} |
319 |
} |
320 |
if $nmatches { |
321 |
set curhelp(msg) "$nmatches topic(s)." |
322 |
} else { |
323 |
set curhelp(msg) "Not found." |
324 |
} |
325 |
seek $curhelp(fid) $startpos |
326 |
return $nmatches |
327 |
} |