File: RPGHelp.tcl

package info (click to toggle)
roleplaying 2.0-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 16,620 kB
  • ctags: 1,039
  • sloc: tcl: 7,155; cpp: 2,709; ansic: 2,227; makefile: 559; sh: 330; csh: 3
file content (1027 lines) | stat: -rw-r--r-- 28,724 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
#* 
#* ------------------------------------------------------------------
#* Role PlayingDB V2.0 by Deepwoods Software
#* ------------------------------------------------------------------
#* RPGHelp.tcl - Help functions
#* Created by Robert Heller on Sun Mar 28 00:57:42 1999
#* ------------------------------------------------------------------
#* Modification History: 
#* $Log: RPGHelp.tcl,v $
#* Revision 1.2  1999/07/13 01:29:16  heller
#* Fix documentation: spelling, punctuation, etc.
#*
#* Revision 1.1  1999/04/20 13:54:42  heller
#* Initial revision
#*
#* ------------------------------------------------------------------
#* Contents:
#* ------------------------------------------------------------------
#*  
#*     Role Playing DB -- A database package that creates and maintains
#* 		       a database of RPG characters, monsters, treasures,
#* 		       spells, and playing environments.
#* 
#*     Copyright (C) 1995,1998,1999  Robert Heller D/B/A Deepwoods Software
#* 			51 Locke Hill Road
#* 			Wendell, MA 01379-9728
#* 
#*     This program is free software; you can redistribute it and/or modify
#*     it under the terms of the GNU General Public License as published by
#*     the Free Software Foundation; either version 2 of the License, or
#*     (at your option) any later version.
#* 
#*     This program is distributed in the hope that it will be useful,
#*     but WITHOUT ANY WARRANTY; without even the implied warranty of
#*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#*     GNU General Public License for more details.
#* 
#*     You should have received a copy of the GNU General Public License
#*     along with this program; if not, write to the Free Software
#*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#* 
#*  
#* 

#@Chapter:RPGHelp.tcl -- Online Help subsystem
#@Label:RPGHelp.tcl
#$Id: RPGHelp.tcl,v 1.2 1999/07/13 01:29:16 heller Rel1 $
# This file contains the code to implement the Online Help subsystem.

global HelpDir
# This is the path to the help file directory.  It is computed from
# the script library directory.
# [index] HelpDir!global variable

catch {set HelpDir "[file join [file dirname $LibDir] Help]"}

global HLinePattern
# This is the help file pattern to pick up a help header line.

set HLinePattern {^([0-9]+)[ 	](.*)$}

global HelpHistoryList
# This is the help history list.

set HelpHistoryList {}

global HelpHistoryListIndex
# This is the help history index.

set HelpHistoryListIndex -1

proc TkIsLoadedP {} {
# This procedure checks to see  if the Tk package is loaded -- this is only
# used to protect the global binding code to allow pkg_mkIndex to work
# properly.

  foreach p [info loaded] {
    if {[string compare "[lindex $p 1]" {Tk}] == 0} {return 1}
  }
  return 0
}

if {[TkIsLoadedP] == 1} {

# Normal, non-destructive Text bindings (lifted from text.tcl)

bind HelpText <1> {
    tkTextButton1 %W %x %y
    %W tag remove sel 0.0 end
}
bind HelpText <B1-Motion> {
    set tkPriv(x) %x
    set tkPriv(y) %y
    tkTextSelectTo %W %x %y
}
bind HelpText <Double-1> {
    set tkPriv(selectMode) word
    tkTextSelectTo %W %x %y
    catch {%W mark set insert sel.first}
}
bind HelpText <Triple-1> {
    set tkPriv(selectMode) line
    tkTextSelectTo %W %x %y
    catch {%W mark set insert sel.first}
}
bind HelpText <Shift-1> {
    tkTextResetAnchor %W @%x,%y
    set tkPriv(selectMode) char
    tkTextSelectTo %W %x %y
}
bind HelpText <Double-Shift-1>	{
    set tkPriv(selectMode) word
    tkTextSelectTo %W %x %y
}
bind HelpText <Triple-Shift-1>	{
    set tkPriv(selectMode) line
    tkTextSelectTo %W %x %y
}
bind HelpText <B1-Leave> {
    set tkPriv(x) %x
    set tkPriv(y) %y
    tkTextAutoScan %W
}
bind HelpText <B1-Enter> {
    tkCancelRepeat
}
bind HelpText <ButtonRelease-1> {
    tkCancelRepeat
}
bind HelpText <Control-1> {
    %W mark set insert @%x,%y
}
bind HelpText <Left> {
    tkTextSetCursor %W insert-1c
}
bind HelpText <Right> {
    tkTextSetCursor %W insert+1c
}
bind HelpText <Up> {
    tkTextSetCursor %W [tkTextUpDownLine %W -1]
}
bind HelpText <Down> {
    tkTextSetCursor %W [tkTextUpDownLine %W 1]
}
bind HelpText <Shift-Left> {
    tkTextKeySelect %W [%W index {insert - 1c}]
}
bind HelpText <Shift-Right> {
    tkTextKeySelect %W [%W index {insert + 1c}]
}
bind HelpText <Shift-Up> {
    tkTextKeySelect %W [tkTextUpDownLine %W -1]
}
bind HelpText <Shift-Down> {
    tkTextKeySelect %W [tkTextUpDownLine %W 1]
}
bind HelpText <Control-Left> {
    tkTextSetCursor %W [tkTextPrevPos %W insert tcl_startOfPreviousWord]
}
bind HelpText <Control-Right> {
    tkTextSetCursor %W [tkTextNextWord %W insert]
}
bind HelpText <Control-Up> {
    tkTextSetCursor %W [tkTextPrevPara %W insert]
}
bind HelpText <Control-Down> {
    tkTextSetCursor %W [tkTextNextPara %W insert]
}
bind HelpText <Shift-Control-Left> {
    tkTextKeySelect %W [tkTextPrevPos %W insert tcl_startOfPreviousWord]
}
bind HelpText <Shift-Control-Right> {
    tkTextKeySelect %W [tkTextNextWord %W insert]
}
bind HelpText <Shift-Control-Up> {
    tkTextKeySelect %W [tkTextPrevPara %W insert]
}
bind HelpText <Shift-Control-Down> {
    tkTextKeySelect %W [tkTextNextPara %W insert]
}
bind HelpText <Prior> {
    tkTextSetCursor %W [tkTextScrollPages %W -1]
}
bind HelpText <Shift-Prior> {
    tkTextKeySelect %W [tkTextScrollPages %W -1]
}
bind HelpText <Next> {
    tkTextSetCursor %W [tkTextScrollPages %W 1]
}
bind HelpText <Shift-Next> {
    tkTextKeySelect %W [tkTextScrollPages %W 1]
}
bind HelpText <Control-Prior> {
    %W xview scroll -1 page
}
bind HelpText <Control-Next> {
    %W xview scroll 1 page
}

bind HelpText <Home> {
    tkTextSetCursor %W {insert linestart}
}
bind HelpText <Shift-Home> {
    tkTextKeySelect %W {insert linestart}
}
bind HelpText <End> {
    tkTextSetCursor %W {insert lineend}
}
bind HelpText <Shift-End> {
    tkTextKeySelect %W {insert lineend}
}
bind HelpText <Control-Home> {
    tkTextSetCursor %W 1.0
}
bind HelpText <Control-Shift-Home> {
    tkTextKeySelect %W 1.0
}
bind HelpText <Control-End> {
    tkTextSetCursor %W {end - 1 char}
}
bind HelpText <Control-Shift-End> {
    tkTextKeySelect %W {end - 1 char}
}
bind HelpText <Tab> {
  focus [tk_focusNext %W]
}
bind HelpText <Control-Tab> {
    focus [tk_focusNext %W]
}
bind HelpText <Control-Shift-Tab> {
    focus [tk_focusPrev %W]
}
bind HelpText <Control-i> {
    focus [tk_focusNext %W]
}
bind HelpText <Control-space> {
    %W mark set anchor insert
}
bind HelpText <Select> {
    %W mark set anchor insert
}
bind HelpText <Control-Shift-space> {
    set tkPriv(selectMode) char
    tkTextKeyExtend %W insert
}
bind HelpText <Shift-Select> {
    set tkPriv(selectMode) char
    tkTextKeyExtend %W insert
}
bind HelpText <Control-slash> {
    %W tag add sel 1.0 end
}
bind HelpText <Control-backslash> {
    %W tag remove sel 1.0 end
}
bind HelpText <<Copy>> {
    tk_textCopy %W
}
# Additional emacs-like bindings:

bind HelpText <Control-a> {
    if {!$tk_strictMotif} {
	tkTextSetCursor %W {insert linestart}
    }
}
bind HelpText <Control-b> {
    if {!$tk_strictMotif} {
	tkTextSetCursor %W insert-1c
    }
}
bind HelpText <Control-d> {
    if {!$tk_strictMotif} {
	%W delete insert
    }
}
bind HelpText <Control-e> {
    if {!$tk_strictMotif} {
	tkTextSetCursor %W {insert lineend}
    }
}
bind HelpText <Control-f> {
    if {!$tk_strictMotif} {
	tkTextSetCursor %W insert+1c
    }
}
bind HelpText <Control-n> {
    if {!$tk_strictMotif} {
	tkTextSetCursor %W [tkTextUpDownLine %W 1]
    }
}
bind HelpText <Control-o> {
    if {!$tk_strictMotif} {
	%W insert insert \n
	%W mark set insert insert-1c
    }
}
bind HelpText <Control-p> {
    if {!$tk_strictMotif} {
	tkTextSetCursor %W [tkTextUpDownLine %W -1]
    }
}
if {$tcl_platform(platform) != "windows"} {
bind HelpText <Control-v> {
    if {!$tk_strictMotif} {
	tkTextScrollPages %W 1
    }
}
}
bind HelpText <Meta-b> {
    if {!$tk_strictMotif} {
	tkTextSetCursor %W [tkTextPrevPos %W insert tcl_startOfPreviousWord]
    }
}
bind HelpText <Meta-f> {
    if {!$tk_strictMotif} {
	tkTextSetCursor %W [tkTextNextWord %W insert]
    }
}
bind HelpText <Meta-less> {
    if {!$tk_strictMotif} {
	tkTextSetCursor %W 1.0
    }
}
bind HelpText <Meta-greater> {
    if {!$tk_strictMotif} {
	tkTextSetCursor %W end-1c
    }
}
# Macintosh only bindings:

# if text black & highlight black -> text white, other text the same
if {$tcl_platform(platform) == "macintosh"} {
bind HelpText <FocusIn> {
    %W tag configure sel -borderwidth 0
    %W configure -selectbackground systemHighlight -selectforeground systemHighlightText
}
bind HelpText <FocusOut> {
    %W tag configure sel -borderwidth 1
    %W configure -selectbackground white -selectforeground black
}
bind HelpText <Option-Left> {
    tkTextSetCursor %W [tkTextPrevPos %W insert tcl_startOfPreviousWord]
}
bind HelpText <Option-Right> {
    tkTextSetCursor %W [tkTextNextWord %W insert]
}
bind HelpText <Option-Up> {
    tkTextSetCursor %W [tkTextPrevPara %W insert]
}
bind HelpText <Option-Down> {
    tkTextSetCursor %W [tkTextNextPara %W insert]
}
bind HelpText <Shift-Option-Left> {
    tkTextKeySelect %W [tkTextPrevPos %W insert tcl_startOfPreviousWord]
}
bind HelpText <Shift-Option-Right> {
    tkTextKeySelect %W [tkTextNextWord %W insert]
}
bind HelpText <Shift-Option-Up> {
    tkTextKeySelect %W [tkTextPrevPara %W insert]
}
bind HelpText <Shift-Option-Down> {
    tkTextKeySelect %W [tkTextNextPara %W insert]
}

# End of Mac only bindings
}


# Hyperhelp bindings

bind HelpText <g> {
  rpgHelpTextHHGoto %W %x %y
}

bind HelpText <G> {
  rpgHelpTextHHGoto %W %x %y
}

bind HelpText <s> {
  rpgHelpTextHHSearch %W %x %y
}

bind HelpText <S> {
  rpgHelpTextHHSearch %W %x %y
}

bind HelpText <r> {
  rpgHelpTextHHRSearch %W %x %y
}

bind HelpText <R> {
  rpgHelpTextHHRSearch %W %x %y
}

}

proc rpgHelpTextHHGoto {widget x y} {
# Function to implement the g/G key binding.

  global HelpWindowInput
  set HelpWindowInput 0
  .helpWindow.command.prompt configure -text "Goto:"
  set oldFocus [focus]
  set oldGrab [grab current .helpWindow]
  if {$oldGrab != ""} {
    set grabStatus [grab status $oldGrab]
  }
  .helpWindow.command.input delete 0 end
  grab .helpWindow.command.input
  focus .helpWindow.command.input
  tkwait variable HelpWindowInput
  catch {focus $oldFocus}
  grab release .helpWindow.command.input
  if {$oldGrab != ""} {  
    if {$grabStatus == "global"} {
      grab -global $oldGrab
    } else {
      grab $oldGrab
    }
  }
  HelpTopic "[.helpWindow.command.input get]"
}

proc rpgHelpTextHHSearch {widget x y} {
# Function to implement the s/S key binding.

  global HelpWindowInput
  set HelpWindowInput 0
  .helpWindow.command.prompt configure -text "Search forward:"
  set oldFocus [focus]
  set oldGrab [grab current .helpWindow]
  if {$oldGrab != ""} {
    set grabStatus [grab status $oldGrab]
  }
  .helpWindow.command.input delete 0 end
  grab .helpWindow.command.input
  focus .helpWindow.command.input
  tkwait variable HelpWindowInput
  catch {focus $oldFocus}
  grab release .helpWindow.command.input
  if {$oldGrab != ""} {  
    if {$grabStatus == "global"} {
      grab -global $oldGrab
    } else {
      grab $oldGrab
    }
  }
  set string "[.helpWindow.command.input get]"
  set found [.helpWindow.indexText.helpTextFrame.helpText search -forwards -nocase -- \
    $string insert end]
  if {[string length "$found"] == 0} {
    .helpWindow.modeStatus configure -text "'$string' not found!"
  } else {
    .helpWindow.indexText.helpTextFrame.helpText mark set insert $found
    .helpWindow.indexText.helpTextFrame.helpText see $found
  }
}

proc rpgHelpTextHHRSearch {widget x y} {
# Function to implement the r/R key binding.

  global HelpWindowInput
  set HelpWindowInput 0
  .helpWindow.command.prompt configure -text "Search backward:"
  set oldFocus [focus]
  set oldGrab [grab current .helpWindow]
  if {$oldGrab != ""} {
    set grabStatus [grab status $oldGrab]
  }
  .helpWindow.command.input delete 0 end
  grab .helpWindow.command.input
  focus .helpWindow.command.input
  tkwait variable HelpWindowInput
  catch {focus $oldFocus}
  grab release .helpWindow.command.input
  if {$oldGrab != ""} {  
    if {$grabStatus == "global"} {
      grab -global $oldGrab
    } else {
      grab $oldGrab
    }
  }
  set string "[.helpWindow.command.input get]"
  set found [.helpWindow.indexText.helpTextFrame.helpText search -backwards -nocase -- \
    $string insert 1.0]
  if {[string length "$found"] == 0} {
    .helpWindow.modeStatus configure -text "'$string' not found!"
  } else {
    .helpWindow.indexText.helpTextFrame.helpText mark set insert $found
    .helpWindow.indexText.helpTextFrame.helpText see $found
  }
}

proc helptext {wname args} {
# Create a helptext widget.
#
# <in> wname -- the name of the widget.
# <in> args  -- options.

  text $wname
  eval [concat $wname configure $args]
  set bts [bindtags $wname]
  set ti [lsearch $bts {Text}]
  set bts [lreplace $bts $ti $ti HelpText]
  bindtags $wname $bts
  return $wname
}

proc CreateHelpWindow {} {
# Procedure to create the help window.

  if {[winfo exists .helpWindow]} {
    wm deiconify .helpWindow
    return
  }

# .helpWindow
# The above line makes pasting MUCH easier for me.
# It contains the pathname of the cutted widget.
# Tcl version: 8.0 (Tcl/Tk/XF)
# Tk version: 8.0
# XF version: 4.0
#

  # build widget .helpWindow
  if {"[info procs XFEdit]" != ""} {
    catch "XFDestroy .helpWindow"
  } {
    catch "destroy .helpWindow"
  }
  toplevel .helpWindow -class Help

  # Window manager configurations
  wm positionfrom .helpWindow ""
  wm sizefrom .helpWindow ""
  wm maxsize .helpWindow 1000 1000
  wm minsize .helpWindow 10 10
  wm title .helpWindow {RPG Help Window}


  # build widget .helpWindow.indexText
  frame .helpWindow.indexText \
    -borderwidth {2} \
    -relief {ridge}

  # build widget .helpWindow.indexText.indexLBFrame
  frame .helpWindow.indexText.indexLBFrame \
    -borderwidth {2}

  # build widget .helpWindow.indexText.indexLBFrame.lbysFrame
  frame .helpWindow.indexText.indexLBFrame.lbysFrame

  # build widget .helpWindow.indexText.indexLBFrame.lbysFrame.lbFrame
  frame .helpWindow.indexText.indexLBFrame.lbysFrame.lbFrame \
    -borderwidth {2}

  # build widget .helpWindow.indexText.indexLBFrame.lbysFrame.lbFrame.indexLB
  listbox .helpWindow.indexText.indexLBFrame.lbysFrame.lbFrame.indexLB \
    -xscrollcommand {.helpWindow.indexText.indexLBFrame.frame2.frame5.scrollbar10 set} \
    -yscrollcommand {.helpWindow.indexText.indexLBFrame.lbysFrame.frame4.scrollbar9 set}
  bind .helpWindow.indexText.indexLBFrame.lbysFrame.lbFrame.indexLB <Double-1> \
	{HelpTopic [string trim [%W get [%W nearest %y]]]}

  # build widget .helpWindow.indexText.indexLBFrame.lbysFrame.frame4
  frame .helpWindow.indexText.indexLBFrame.lbysFrame.frame4 \
    -borderwidth {2}

  # build widget .helpWindow.indexText.indexLBFrame.lbysFrame.frame4.scrollbar9
  scrollbar .helpWindow.indexText.indexLBFrame.lbysFrame.frame4.scrollbar9 \
    -command {.helpWindow.indexText.indexLBFrame.lbysFrame.lbFrame.indexLB yview} \
    -width {13}

  # build widget .helpWindow.indexText.indexLBFrame.frame2
  frame .helpWindow.indexText.indexLBFrame.frame2 \
    -borderwidth {1}

  # build widget .helpWindow.indexText.indexLBFrame.frame2.frame5
  frame .helpWindow.indexText.indexLBFrame.frame2.frame5 \
    -borderwidth {2}

  # build widget .helpWindow.indexText.indexLBFrame.frame2.frame5.scrollbar10
  scrollbar .helpWindow.indexText.indexLBFrame.frame2.frame5.scrollbar10 \
    -command {.helpWindow.indexText.indexLBFrame.lbysFrame.lbFrame.indexLB xview} \
    -orient {horizontal} \
    -width {13}

  # build widget .helpWindow.indexText.indexLBFrame.frame2.frame6
  frame .helpWindow.indexText.indexLBFrame.frame2.frame6 \
    -borderwidth {2}

  # build widget .helpWindow.indexText.indexLBFrame.frame2.frame6.frame11
  frame .helpWindow.indexText.indexLBFrame.frame2.frame6.frame11 \
    -borderwidth {2} \
    -height {13} \
    -width {16}

  # build widget .helpWindow.indexText.helpTextFrame
  frame .helpWindow.indexText.helpTextFrame \
    -relief {raised}

  # build widget .helpWindow.indexText.helpTextFrame.scrollbar1
  scrollbar .helpWindow.indexText.helpTextFrame.scrollbar1 \
    -command {.helpWindow.indexText.helpTextFrame.helpText yview} \
    -relief {raised}

  # build widget .helpWindow.indexText.helpTextFrame.helpText
  helptext .helpWindow.indexText.helpTextFrame.helpText \
    -wrap {word} \
    -yscrollcommand {.helpWindow.indexText.helpTextFrame.scrollbar1 set}

  # build widget .helpWindow.modeStatus
  label .helpWindow.modeStatus -justify left -anchor w

  # build widget .helpWindow.command
  frame .helpWindow.command \
    -borderwidth {2} \
    -relief {ridge}

  # build widget .helpWindow.command.prompt
  label .helpWindow.command.prompt

  # build widget .helpWindow.command.input
  entry .helpWindow.command.input \
    -width {0}
  bind .helpWindow.command.input <Return> {
	global HelpWindowInput
	set HelpWindowInput 1
  }

  # build widget .helpWindow.buttons
  frame .helpWindow.buttons \
    -borderwidth {2}

  # build widget .helpWindow.buttons.button7
  button .helpWindow.buttons.button7 \
    -padx {9} \
    -pady {3} \
    -text {Close} \
    -command {wm withdraw .helpWindow} 
  wm protocol .helpWindow WM_DELETE_WINDOW {wm withdraw .helpWindow}

  # build widget .helpWindow.buttons.button9
  button .helpWindow.buttons.button9 \
    -padx {9} \
    -pady {3} \
    -text {Back} \
    -command {HelpBackTopic}

  # build widget .helpWindow.buttons.button10
  button .helpWindow.buttons.button10 \
    -padx {9} \
    -pady {3} \
    -text {Forward} \
    -command {HelpForwardTopic}

  # build widget .helpWindow.buttons.button8
  button .helpWindow.buttons.button8 \
    -padx {9} \
    -pady {3} \
    -text {Help} \
    -command {HelpTopic Help}

  # pack master .helpWindow.indexText
  pack configure .helpWindow.indexText.indexLBFrame \
    -expand 1 \
    -fill both \
    -side left
  pack configure .helpWindow.indexText.helpTextFrame \
    -expand 1 \
    -fill both \
    -side right

  # pack master .helpWindow.indexText.indexLBFrame
  pack configure .helpWindow.indexText.indexLBFrame.lbysFrame \
    -expand 1 \
    -fill both
  pack configure .helpWindow.indexText.indexLBFrame.frame2 \
    -fill x \
    -side bottom

  # pack master .helpWindow.indexText.indexLBFrame.lbysFrame
  pack configure .helpWindow.indexText.indexLBFrame.lbysFrame.lbFrame \
    -expand 1 \
    -fill both \
    -side left
  pack configure .helpWindow.indexText.indexLBFrame.lbysFrame.frame4 \
    -fill y \
    -side right

  # pack master .helpWindow.indexText.indexLBFrame.lbysFrame.lbFrame
  pack configure .helpWindow.indexText.indexLBFrame.lbysFrame.lbFrame.indexLB \
    -expand 1 \
    -fill both \
    -side left

  # pack master .helpWindow.indexText.indexLBFrame.lbysFrame.frame4
  pack configure .helpWindow.indexText.indexLBFrame.lbysFrame.frame4.scrollbar9 \
    -expand 1 \
    -fill y

  # pack master .helpWindow.indexText.indexLBFrame.frame2
  pack configure .helpWindow.indexText.indexLBFrame.frame2.frame5 \
    -expand 1 \
    -fill both \
    -side left
  pack configure .helpWindow.indexText.indexLBFrame.frame2.frame6 \
    -fill y \
    -side right

  # pack master .helpWindow.indexText.indexLBFrame.frame2.frame5
  pack configure .helpWindow.indexText.indexLBFrame.frame2.frame5.scrollbar10 \
    -expand 1 \
    -fill x \
    -side left

  # pack master .helpWindow.indexText.indexLBFrame.frame2.frame6
  pack configure .helpWindow.indexText.indexLBFrame.frame2.frame6.frame11 \
    -expand 1 \
    -fill both

  # pack master .helpWindow.indexText.helpTextFrame
  pack configure .helpWindow.indexText.helpTextFrame.scrollbar1 \
    -fill y \
    -side right
  pack configure .helpWindow.indexText.helpTextFrame.helpText \
    -expand 1 \
    -fill both

  # pack master .helpWindow.command
  pack configure .helpWindow.command.prompt \
    -side left
  pack configure .helpWindow.command.input \
    -expand 1 \
    -fill x \
    -side right

  # pack master .helpWindow.buttons
  pack configure .helpWindow.buttons.button7 \
    -expand 1 \
    -side left
  pack configure .helpWindow.buttons.button9 \
    -expand 1 \
    -side left
  pack configure .helpWindow.buttons.button10 \
    -expand 1 \
    -side left
  pack configure .helpWindow.buttons.button8 \
    -expand 1 \
    -side right

  # pack master .helpWindow
  pack configure .helpWindow.indexText \
    -expand 1 \
    -fill both
  pack configure .helpWindow.modeStatus \
    -fill x
  pack configure .helpWindow.command \
    -fill x
  pack configure .helpWindow.buttons \
    -fill x

  .helpWindow.indexText.helpTextFrame.helpText insert end {}
  .helpWindow.command.input insert end {}
  focus .helpWindow.indexText.helpTextFrame.helpText

  global HelpDir
  set index [file join $HelpDir hh.index]
  if {[file readable $index]} {
    global HelpList HelpIndex
    set HelpList {}
    set ifp [open $index r]
    while {[gets $ifp line] >= 0} {
      set indexKey [lindex $line 0]
      set fileInfo [lindex $line 1]
      set HelpIndex($indexKey) "$fileInfo"
      lappend HelpList "$indexKey"
      set HKL [split "$indexKey" {>}]
      set prefix [dup { } [expr ([llength $HKL] - 1) * 2]]
      .helpWindow.indexText.indexLBFrame.lbysFrame.lbFrame.indexLB insert end \
	"$prefix[lindex $HKL end]"
    }
    close $ifp
  }
# end of widget tree

}

proc dup {str count} {
# Function to dup a string.

  set dup {}
  for {set i 0} {$i < $count} {incr i} {append dup "$str"}
  return "$dup"
}

proc HelpBackTopic {} {
# Procedure to go ``back'' in the history list.

  global HelpHistoryList HelpHistoryListIndex
  if {$HelpHistoryListIndex > 0} {
    incr HelpHistoryListIndex -1
    HelpTopic [lindex $HelpHistoryList $HelpHistoryListIndex] 0
  }
}

proc HelpForwardTopic {} {
# Procedure to go ``forward'' in the history list.

  global HelpHistoryList HelpHistoryListIndex
  if {$HelpHistoryListIndex < [expr [llength $HelpHistoryList] - 1]} {
    incr HelpHistoryListIndex
    HelpTopic [lindex $HelpHistoryList $HelpHistoryListIndex] 0
  }
}

proc HelpTopic {{topic {}} {updateHistory 1}} {
# This procedure pops up a help dialog.
# <in> topic -- the topic to get help on.

  CreateHelpWindow
  global HelpList HelpIndex HelpDir HLinePattern 
  global HelpHistoryList HelpHistoryListIndex

  if {$updateHistory == 1} {
    if {$HelpHistoryListIndex >= 0} {
      set HelpHistoryList [lrange $HelpHistoryList 0 $HelpHistoryListIndex]
      lappend HelpHistoryList "$topic"
      incr HelpHistoryListIndex      
    } else {
      set HelpHistoryList [list "$topic"]
      set HelpHistoryListIndex 0
    }
  }

  set an [array names HelpIndex "*$topic"]
  if {[string length "$an"] == 0} {
    .helpWindow.modeStatus configure -text "$topic not found"
  } else {
    .helpWindow.indexText.helpTextFrame.helpText delete 1.0 end
    set an [lindex $an 0]
    set index [lsearch -exact $HelpList $an]
    .helpWindow.indexText.indexLBFrame.lbysFrame.lbFrame.indexLB see $index
    .helpWindow.indexText.indexLBFrame.lbysFrame.lbFrame.indexLB selection clear 0 end
    .helpWindow.indexText.indexLBFrame.lbysFrame.lbFrame.indexLB selection set $index
    set fileInfo [set HelpIndex($an)]
    set file [file join $HelpDir [lindex $fileInfo 0]]
    set fpos [lindex $fileInfo 1]
    set buffer {}
    if {[catch [list open $file r] fp]} {
      .helpWindow.indexText.helpTextFrame.helpText insert end "Can't load $topic: $fp"
      return
    }
    seek $fp $fpos start
    set headline [gets $fp]
    if {[regsub {^[0-9]+} "$headline" {} newheadline] > 0} {
      set headline [string trim "$newheadline"]
    }
    while {[gets $fp line] >= 0} {
      if {[regexp "$HLinePattern" "$line" whole d1 d2] > 0} {break}
      append buffer "\n$line"
    }
    close $fp
    
    HeaderFormat .helpWindow.indexText.helpTextFrame.helpText "$headline"
    BodyFormat .helpWindow.indexText.helpTextFrame.helpText "$buffer"
    .helpWindow.indexText.helpTextFrame.helpText mark set insert 1.0
    .helpWindow.indexText.helpTextFrame.helpText see 1.0
  }
}

proc HeaderFormat {text hline} {
# Procedure to format a header line.

  global tk_version
  if {$tk_version < 8.0} {
    regsub -nocase medium [$text cget -font] bold x
  } else {
    regsub -nocase normal [font actual [$text cget -font]] bold x
  }
  $text tag configure header -font "$x"
  $text insert end "$hline" header
}

proc BodyFormat {text body} {
# Procedure to format the body.

  global HelpDir
  $text tag configure link -foreground blue -underline 1
  set abspos 0
  while {[regexp -indices "\[<\{\[\]" "$body" ii] > 0} {
    set i [lindex $ii 0]
    set prefix [string range "$body" 0 [expr $i - 1]]
    if {[string compare "[string index $body $i]" {<}] == 0} {
      set j [string first {>} "$body"]
      $text insert end "$prefix" normal
      set link [string range "$body" [expr $i + 1] [expr $j - 1]]
      set body [string range "$body" [expr $j + 1] end]
      set pos [expr $abspos + $i]
      incr abspos [expr $j + 1]
      $text insert end "$link" [list link link$pos]
      $text tag bind link$pos <1> [list HelpTopic "$link"]
    } elseif {[string compare "[string index $body $i]" {[}] == 0} {
      set j [string first {]} "$body"]
      $text insert end "$prefix" normal
      set head [string range "$body" [expr $i + 1] [expr $j - 1]]
      set body [string range "$body" [expr $j + 1] end]
      set pos [expr $abspos + $i]
      incr abspos [expr $j + 1]
      $text insert end "$head" header
    } else {
      set j [string first "\}" "$body"]
      $text insert end "$prefix" normal
      set imagefile [file join $HelpDir \
			[string range "$body" [expr $i + 1] [expr $j - 1]]]
      set body [string range "$body" [expr $j + 1] end]
      set pos [expr $abspos + $i]
      image create photo himg$pos -format ppm -file $imagefile
      incr abspos [expr $j + 1]
      $text image create end -image himg$pos -align top
    }
  }
  $text insert end "$body" normal
}
    

proc BindTagsAll {} {
# Procedure to set up the XmTrackingLocate bind tag.

  BindTagsW .
}

proc BindTagsW {w} {
# Helper procedure to set up the XmTrackingLocate bind tag.

  if {[lsearch -exact [bindtags $w] XmTrackingLocateTag] < 0} {
    bindtags $w [concat XmTrackingLocateTag [bindtags $w]]
  }
  foreach c [winfo children $w] {
    BindTagsW $c
  }
}
  
proc XmTrackingLocate {widget cursor} {
# Procedure to implement Motif's XmTrackingLocate function.

  global XmTrackingLocateInfo
  BindTagsAll
  set tl [winfo toplevel $widget]
  set XmTrackingLocateInfo(cursor) "[$tl cget -cursor]"
  set XmTrackingLocateInfo(widget) {}
  set XmTrackingLocateInfo(grab) "[grab current $tl]"
  if {[string length "$XmTrackingLocateInfo(grab)"] > 0} {
    set XmTrackingLocateInfo(grabType) "[grab status $XmTrackingLocateInfo(grab)]"
  }
  $tl configure -cursor "$cursor"
  bind XmTrackingLocateTag <ButtonPress-1> "XmTrackingLocateClick $tl %X %Y;break"
  grab -global $tl
  tkwait variable XmTrackingLocateInfo(widget)
  grab release $tl
  if {[string length "$XmTrackingLocateInfo(grab)"] > 0} {
    if {"$XmTrackingLocateInfo(grabType)" == "global"} {
      grab set -global $XmTrackingLocateInfo(grab)
    } else {
      grab set $XmTrackingLocateInfo(grab)
    }
  }
  return $XmTrackingLocateInfo(widget)  
}

proc XmTrackingLocateClick {tl X Y} {
# XmTrackingLocate binding function.

  global XmTrackingLocateInfo 
  bind XmTrackingLocateTag <ButtonPress-1> {}
  $tl configure -cursor "$XmTrackingLocateInfo(cursor)"
  set XmTrackingLocateInfo(widget) "[winfo containing $X $Y]"
}
  


proc HelpContext {{widget .}} {
# This procedure pops up an ``On Context'' help dialog.

  set widget "[XmTrackingLocate $widget question_arrow]"
  if {[string length "$widget"] > 0} {
    HelpWindow $widget
  }
}

proc HelpWindow {{widget .}} {
# This procedure pops up help for the current toplevel.

  CreateHelpWindow

  set tl [winfo toplevel $widget]
  set tlClass [winfo class $tl]

  regsub \\$tl $widget tl$tlClass. widgetCL

  while {[regsub -all {\.\.} $widgetCL {.} widgetCL1] > 0} {
    set widgetCL $widgetCL1
  }
  while {[regsub -all {\.$}  $widgetCL {}  widgetCL1] > 0} {
    set widgetCL $widgetCL1
  }

  global HelpIndex

  set topicL [split $widgetCL {.}]
  while {[llength $topicL] > 0} {
    set topic [join $topicL {.}]
    set an [array names HelpIndex "*$topic"]
    if {[string length "$an"] > 0} break;
    set topicL [lrange $topicL 0 [expr [llength $topicL] - 2]]
  }
  if {[llength $topicL] > 0} {
    HelpTopic $topic
  } else {
    HelpTopic $widgetCL
  }
}




package provide RPGHelp 1.0