home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l391 / 2.ddi / HELP.BA$ / HELP.bin
Encoding:
Text File  |  1992-08-19  |  26.9 KB  |  706 lines

  1. ' ------------------------------------------------------------------------
  2. ' Visual Basic for MS-DOS Help Toolkit
  3. '
  4. ' The Help Toolkit (HELP.BAS, HELPF.FRM, and HELPUTIL.FRM)
  5. ' makes it easy to add a hypertext Help system to your
  6. ' applications.
  7. '
  8. ' To use the Help Toolkit in your programs, include
  9. ' HELP.BAS, HELPF.FRM, and HELPUTIL.FRM in your program or
  10. ' use the supplied library (HELP.LIB, HELPA.LIB - AltMath
  11. ' version) and Quick library (HELP.QLB) and call the
  12. ' appropriate routines to load and display your help topics.
  13. ' Forms and code modules that call Help routines
  14. ' must include HELP.BI ('$INCLUDE: 'HELP.BI').
  15. '
  16. ' Help files can be written in any ASCII text editor or
  17. ' in Visual Basic itself.  Refer to the sample Help file
  18. ' HELPDEMO.TXT as an example of the Help file format.
  19. ' HELPDEMO.TXT also contains full information on creating
  20. ' Help files and using the Help Toolkit to display and
  21. ' navigate them.
  22. '
  23. ' The Help ToolKit consists of the following routines:
  24. '     HelpRegister   - initializes the Help system and loads Help file
  25. '     HelpShowTopic  - displays specified Help topic
  26. '     HelpClose      - closes Help file
  27. '     HelpSetOptions - sets Help display options
  28. '     HelpSearch     - invokes Help Search dialog to enable topic search
  29. ' Detailed descriptions of these procedures are contained
  30. ' in the comment headers above each.
  31. '
  32. ' Copyright (C) 1982-1992 Microsoft Corporation
  33. '
  34. ' You have a royalty-free right to use, modify, reproduce
  35. ' and distribute the sample applications and toolkits provided with
  36. ' Visual Basic for MS-DOS (and/or any modified version)
  37. ' in any way you find useful, provided that you agree that
  38. ' Microsoft has no warranty, obligations or liability for
  39. ' any of the sample applications or toolkits.
  40. ' ------------------------------------------------------------------------
  41.  
  42. ' Include file containing procedure declarations.
  43. '$INCLUDE: 'HELP.BI'
  44.  
  45. '$FORM frmHelpUtils         ' Form for Search, History, and Change dialogs
  46. '$FORM frmHelpMain          ' Help window
  47.  
  48. TYPE IndexType              ' Used to index Help topics
  49.     StartFilePtr AS LONG    ' Position within the topic's file
  50.     NumLines AS INTEGER     ' Number of lines in the Help topic
  51. END TYPE
  52.  
  53. TYPE PointType
  54.     X AS INTEGER            ' Horizontal location
  55.     Y AS INTEGER            ' Vertical location
  56. END TYPE
  57.  
  58. ' Variables common to HELP.BAS, HELPF.FRM, and HELPUTIL.FRM.
  59. COMMON SHARED /HelpLib/ DialogBackcolor AS INTEGER ' Background color for Help dialog boxes (Search, Copy, History)
  60. COMMON SHARED /HelpLib/ DialogForecolor AS INTEGER ' Foreground color for Help dialog boxes (Search, Copy, History)
  61.  
  62. ' Variables common to HELP.BAS and HELPF.FRM.
  63. COMMON SHARED /HelpLib/ Topic() AS STRING       ' Lines of the current Help topic
  64. COMMON SHARED /HelpLib/ LeftChar AS INTEGER     ' Leftmost character of the Help topic given the horizonal scroll position
  65. COMMON SHARED /HelpLib/ StartLine AS INTEGER    ' Top line of the Help topic given the vertical scroll position
  66. COMMON SHARED /HelpLib/ MaxLineLen AS INTEGER   ' Longest line of the Help topic
  67. COMMON SHARED /HelpLib/ HelpIndexPtr() AS IndexType ' Table that stores data about each Help topic
  68. COMMON SHARED /HelpLib/ HelpIndexTopics() AS STRING ' Array of Help topic names.  Used with the HelpIndexPtr table.
  69. COMMON SHARED /HelpLib/ HelpFileNum AS INTEGER  ' Logical file number assigned to the Help file by HelpRegister
  70. COMMON SHARED /HelpLib/ HelpTopicStack()  AS STRING ' Array of the last 20 Help topics shown
  71. COMMON SHARED /HelpLib/ HelpTopicStackPtr  AS INTEGER ' Number of topics in the HelpTopicStack
  72. COMMON SHARED /HelpLib/ TopicFound AS STRING    ' The Help topic being shown
  73. COMMON SHARED /HelpLib/ MaxHistoryStack AS INTEGER ' Maximum allowed size of the HelpTopicStack.  HelpRegister sets to 20.
  74. COMMON SHARED /HelpLib/ UnloadOnClose AS INTEGER ' Flag that determines if closing the Help form terminates the Help system.
  75. COMMON SHARED /HelpLib/ KillHelp AS INTEGER     ' Flag that HelpClose sets to terminate the Help form
  76. COMMON SHARED /HelpLib/ CursorPos AS PointType  ' Position of the currently selected Help link within a topic
  77. COMMON SHARED /HelpLib/ ButtonBarHeight AS INTEGER ' Number of lines to reserve at the top of the form for the button bar.  0 or 3.
  78. COMMON SHARED /HelpLib/ CloseOnEscape AS INTEGER ' If TRUE, pressing ESC will close the Help window
  79.  
  80. DEFINT A-Z
  81.  
  82. CONST FALSE = 0
  83. CONST TRUE = NOT FALSE
  84. CONST TabSpaces = 4                 ' Number of spaces that a TAB represents
  85.  
  86. DIM SHARED SelectedTopicColor AS INTEGER ' The foreground color of Help links
  87. DIM SHARED SpaceString AS STRING    ' A string of blank spaces used by the HelpPrintText procedure
  88. DIM SHARED HelpFileName AS STRING   ' Help file name.
  89.  
  90.  
  91. '----------------------------------------------------
  92. ' Sample usage of the Help Toolkit. This code is
  93. ' only executed if HELP.BAS is the start-up file.
  94. ' Parameter information for each Help procedure is
  95. ' contained in the header comments for the procedure.
  96. '----------------------------------------------------
  97.  
  98. ' Set the title bar background color to magenta (5),
  99. ' the screen background color to blue (1), and the
  100. ' access key color to red (4).
  101. screen.controlpanel(16) = 5
  102. screen.controlpanel(5) = 1
  103. screen.controlpanel(0) = 4
  104.  
  105. ' Initialize the Help system and load specified Help file.
  106. ' HelpLoaded% returns whether or not the Help file was
  107. ' loaded successfully.
  108. HelpRegister "HELPDEMO.TXT", HelpLoaded%
  109.  
  110. ' HelpLoaded% will be FALSE if HelpRegister fails.
  111. IF HelpLoaded% THEN
  112.     ' Set Help color and button display options.
  113.     ' Calls to this routine are optional as Help will
  114.     ' use its defaults if this call is ommitted.
  115.     HelpSetOptions 3, 0, 7, 0, 7, 11, 2
  116.  
  117.     ' Show the table of contents topic for the Help file.
  118.     ' Help will remain visible until window is closed
  119.     ' by the user.  Help topics can be changed by the user
  120.     ' once the Help window is made visible with the first
  121.     ' topic displayed.
  122.     HelpShowTopic "Contents"
  123. END IF
  124.  
  125. ' Help close and unload routine.
  126. '
  127. ' Closes help file and unloads help form.
  128. ' This routine should be called when the
  129. ' parent application terminates. If the parent
  130. ' routine has no END statement (e.g. it stops when
  131. ' the user closes the last form), the last Form_Unload
  132. ' must call this procedure.  Otherwise, the application
  133. ' will not end because frmHelpMain will still be loaded.
  134. '
  135. SUB HelpClose ()
  136.     KillHelp = TRUE
  137.     UNLOAD frmHelpMain
  138. END SUB
  139.  
  140. ' Internal routine that returns the greater of two integers.
  141. '
  142. FUNCTION HelpMax (int1 AS INTEGER, int2 AS INTEGER) AS INTEGER
  143.     IF int1 > int2 THEN
  144.         HelpMax = int1
  145.     ELSE
  146.         HelpMax = int2
  147.     END IF
  148. END FUNCTION
  149.  
  150. ' Internal routine that returns the lower of two integers.
  151. '
  152. FUNCTION HelpMin (int1 AS INTEGER, int2 AS INTEGER) AS INTEGER
  153.     IF int1 > int2 THEN
  154.         HelpMin = int2
  155.     ELSE
  156.         HelpMin = int1
  157.     END IF
  158. END FUNCTION
  159.  
  160. ' Help topic print routine.
  161. '
  162. ' Internal routine that prints Help topic text.
  163. ' Called when a topic is first shown and when
  164. ' the form is scrolled.
  165. '
  166. ' Parameters:
  167. '   TopLine   - The first element (line) of the topic
  168. '               array to be displayed.
  169. '   LeftPos   - The first character of each topic line
  170. '               to be displayed.
  171. '
  172. SUB HelpPrintText (TopLine AS INTEGER, LeftPos AS INTEGER)
  173.     MaxLines = UBOUND(Topic)    ' The number of lines in the Help topic.
  174.     IF TopLine > MaxLines THEN EXIT SUB
  175.     LastPrintedLine = TopLine + frmHelpMain.pctBackground.ScaleHeight ' The last line of the Help topic that will be printed.
  176.  
  177.     IF LastPrintedLine > MaxLines THEN
  178.         ' Ensures that the routine does not attempt to print more lines than the topic size
  179.         LastPrintedLine = MaxLines
  180.     END IF
  181.  
  182.     blanklines = frmHelpMain.pctBackground.ScaleHeight - (LastPrintedLine - TopLine - ButtonBarHeight) ' Number of blank lines on the form.
  183.     FormWidth = frmHelpMain.pctBackground.ScaleWidth ' Local variable copy of the form width.  Used to minimize property access.
  184.     frmHelpMain.pctBackground.CurrentY = 0 ' Start printing at the top of the picture control
  185.  
  186.     ' Print the necessary topic lines.
  187.     FOR i = TopLine TO LastPrintedLine
  188.         frmHelpMain.pctBackground.PRINT MID$(Topic(i) + SpaceString, LeftPos, FormWidth)
  189.     NEXT i
  190.  
  191.     ' Print as many blank lines as required.
  192.     DO WHILE blanklines > 0
  193.         blanklines = blanklines - 1
  194.         frmHelpMain.pctBackground.PRINT SpaceString
  195.     LOOP
  196.  
  197.     ' Set the common variables StartLine and LeftChar based
  198.     ' on parameters passed to this routine.
  199.     StartLine = TopLine
  200.     LeftChar = LeftPos
  201.  
  202.     ' Do a quick check to see if there is a highlighted
  203.     ' link.  CursorPos.Y will be 0 if the users has not
  204.     ' pressed TAB or Shift-TAB in this topic.  Skips a lot
  205.     ' of code if there is no selected Help link.
  206.     '
  207.     IF CursorPos.Y > 0 THEN
  208.         DIM PrintTopicFound AS STRING ' String containing the selected Help link
  209.  
  210.         ' Is the selected link within the currently visible
  211.         ' vertical region of the form?
  212.         IF CursorPos.Y >= StartLine AND CursorPos.Y <= LastPrintedLine THEN
  213.             ' Yes.
  214.             
  215.             ' Now see if it's in the visible horizontal
  216.             ' region
  217.             IF CursorPos.X >= LeftPos AND CursorPos.X <= LeftPos + FormWidth THEN
  218.                 ' The entire link text will fit horizontally
  219.                 frmHelpMain.pctBackground.CurrentX = CursorPos.X - LeftPos
  220.                 PrintTopicFound = TopicFound
  221.             ELSEIF CursorPos.X + LEN(TopicFound) >= LeftPos AND CursorPos.X <= LeftPos + FormWidth THEN
  222.                 ' Part of the link text will fit horizontally
  223.                 frmHelpMain.pctBackground.CurrentX = 0
  224.                 PrintTopicFound = MID$(TopicFound, LeftChar - CursorPos.X + 1)
  225.             END IF
  226.  
  227.             ' Print the link text in the SelectedTopicColor
  228.             ' if it is in the visible region.
  229.             IF PrintTopicFound <> "" THEN
  230.                 frmHelpMain.pctBackground.CurrentY = CursorPos.Y - StartLine
  231.                 RealColor = frmHelpMain.pctBackground.Forecolor
  232.                 frmHelpMain.pctBackground.Forecolor = SelectedTopicColor
  233.                 frmHelpMain.pctBackground.PRINT PrintTopicFound
  234.                 frmHelpMain.pctBackground.Forecolor = RealColor
  235.             END IF
  236.         END IF
  237.     END IF
  238.  
  239.     ' Set the scroll bar values.
  240.     frmHelpMain.vscHelp.Value = StartLine
  241.     frmHelpMain.hscHelp.Value = LeftChar
  242.  
  243.     ' Make sure the Help form is visible.
  244.     frmHelpMain.SHOW
  245. END SUB
  246.  
  247. ' Help initialization routine.
  248. '
  249. ' Initializes the Help Toolkit by scanning through
  250. ' the specified Help file to create a table of
  251. ' all Help topics. This routine MUST be
  252. ' called BEFORE using any other Help Toolkit
  253. ' routines.
  254. '
  255. ' Parameters:
  256. '   HelpFile$ - The MS-DOS file name of the Help file.
  257. '               Must include file extensions (e.g. HELPDEMO.TXT).
  258. '   Success% -  Signals whether Help initialization was successful.
  259. '
  260. ' Topics must be defined in the Help file as follows
  261. ' for HelpRegister to recognize them.  See HELPDEMO.TXT
  262. ' for a complete example.
  263. '      .TOPIC:
  264. '      topic_name
  265. '      Topic Text Goes Here....
  266. '
  267. SUB HelpRegister (HelpFile$, Success%)
  268.     Success% = FALSE
  269.  
  270.     ' Default option settings.  HelpSetOptions can override.
  271.     ButtonBarHeight = 3
  272.     SelectedTopicColor = screen.controlpanel(0)
  273.     DialogBackcolor = 7
  274.     DialogForecolor = 0
  275.     
  276.     ' Assume there are 50 help topics in the file.
  277.     MaxTopics = 50
  278.     REDIM HelpIndexPtr(MaxTopics)  AS IndexType
  279.     REDIM HelpIndexTopics(MaxTopics) AS STRING
  280.  
  281.     HelpFileNum = FREEFILE
  282.     
  283.     ON LOCAL ERROR GOTO HelpFileNotFound
  284.         OPEN HelpFile$ FOR INPUT AS HelpFileNum
  285.     ON LOCAL ERROR GOTO 0
  286.  
  287.     DO WHILE NOT EOF(HelpFileNum)
  288.         LINE INPUT #HelpFileNum, temp$
  289.         IF RTRIM$(temp$) = ".TOPIC:" THEN
  290.             ' Now that it hit a .TOPIC:, it knows how
  291.             ' many lines were in the previous
  292.             ' topic so goes back and fills in
  293.             ' the # of lines for the PREVIOUS
  294.             ' topic.  Will fill element 0, by the way.
  295.             HelpIndexPtr(NumTopics).NumLines = LinesRead
  296.             
  297.             ' Now clear LinesRead for this new
  298.             ' topic and increment the number of topics.
  299.             LinesRead = 0
  300.             NumTopics = NumTopics + 1
  301.  
  302.             ' Are the arrays large enough given the number
  303.             ' Help topics?
  304.             IF NumTopics > MaxTopics THEN
  305.                 ' No.  More topics than current array
  306.                 ' allocation so grow the arrays by another
  307.                 ' 50 elements.
  308.                 MaxTopics = MaxTopics + 50
  309.                 ON LOCAL ERROR GOTO HelpOutOfMemory
  310.                     REDIM PRESERVE HelpIndexPtr(MaxTopics) AS IndexType
  311.                     REDIM PRESERVE HelpIndexTopics(MaxTopics) AS STRING
  312.                 ON LOCAL ERROR GOTO 0
  313.             END IF
  314.  
  315.             ' The next line in the file must be the topic
  316.             ' name.
  317.             LINE INPUT #HelpFileNum, HelpIndexTopics(NumTopics)
  318.  
  319.             ' Remember where the Help topic actually
  320.             ' starts so HelpShowTopic can jump to it
  321.             ' immediately when a jump is requested.
  322.             HelpIndexPtr(NumTopics).StartFilePtr = SEEK(HelpFileNum)
  323.         ELSE
  324.             ' Line was not a Topic marker or the first line
  325.             ' after a topic marker.  Increment the number
  326.             ' of lines in this topic.
  327.             LinesRead = LinesRead + 1
  328.         END IF
  329.     LOOP
  330.  
  331.     ' Abort the registration process if no Help topics
  332.     ' were found.
  333.     IF NumTopics = 0 THEN
  334.         MSGBOX HelpFile$ + " is not a valid Help file.", 0, "Help"
  335.         HelpFileNum = 0
  336.         EXIT SUB
  337.     END IF
  338.  
  339.     ' Completes the table entries for the last topic
  340.     HelpIndexPtr(NumTopics).NumLines = LinesRead
  341.  
  342.     ' Shrink the arrays to the exact size required
  343.     ON LOCAL ERROR GOTO HelpOutOfMemory
  344.         REDIM PRESERVE HelpIndexPtr(NumTopics) AS IndexType
  345.         REDIM PRESERVE HelpIndexTopics(NumTopics) AS STRING
  346.     ON LOCAL ERROR GOTO 0
  347.  
  348.     ' Initialize variables
  349.     HelpTopicStackPtr = 0
  350.     frmHelpMain.cmdButtonBar(0).Enabled = TRUE ' Enable the Contents button
  351.     frmHelpMain.cmdButtonBar(1).Enabled = TRUE ' Enable the Search button
  352.     MaxHistoryStack = 20
  353.     ON LOCAL ERROR GOTO HelpOutOfMemory
  354.         REDIM HelpTopicStack(MaxHistoryStack)  AS STRING
  355.     ON LOCAL ERROR GOTO 0
  356.  
  357.     SpaceString = STRING$(screen.width, 32)
  358.     IF HelpFileNum > 0 THEN
  359.         Success% = TRUE
  360.  
  361.         ' Store a fully qualified HelpFile name
  362.         ' in case the Help file needs to be
  363.         ' re-registered later.  The full path
  364.         ' name is stored so the Help Toolkit can
  365.         ' always find the file even if an application
  366.         ' causes CURDIR$ to change.
  367.         '
  368.         IF INSTR(HelpFile$, "\") = 0 THEN
  369.             ' No slash in the file name so local
  370.             ' file name was provided.  Add path
  371.             ' info.
  372.             IF RIGHT$(CURDIR$, 1) = "\" THEN
  373.                 HelpFileName = CURDIR$ + HelpFile$
  374.             ELSE
  375.                 HelpFileName = CURDIR$ + "\" + HelpFile$
  376.             END IF
  377.         ELSE
  378.             ' Path info was already provided
  379.             HelpFileName = HelpFile$
  380.         END IF
  381.     END IF
  382. EXIT SUB
  383.  
  384. ' Local error handlers
  385. HelpOutOfMemory:
  386.     MSGBOX "Insufficient memory to display Help.", 0, "Help"
  387.     HelpClose
  388.     EXIT SUB
  389.  
  390. HelpFileNotFound:
  391.     ' Construct a fully qualified file name
  392.     IF RIGHT$(CURDIR$, 1) = "\" THEN
  393.         UseFile$ = CURDIR$ + HelpFile$
  394.     ELSE
  395.         UseFile$ = CURDIR$ + "\" + HelpFile$
  396.     END IF
  397.  
  398.     MSGBOX "Help file " + UseFile$ + " not found.", 0, "Help"
  399.     HelpClose
  400.     EXIT SUB
  401. END SUB
  402.  
  403. ' Help search routine.
  404. '
  405. ' Displays Help Search dialog and searchs for
  406. ' selected Help topic.
  407. '
  408. SUB HelpSearch ()
  409.     ' Confirm that a Help file is open.
  410.     IF HelpFileNum = 0 THEN
  411.         ' No open Help file.  HelpFileName will
  412.         ' be null if HelpRegister was never successful
  413.         ' but will have a valid file name if a Help
  414.         ' file was registered correctly but an error
  415.         ' such as Insufficient Memory to Display Help
  416.         ' caused Help to close.  Also occurs if
  417.         ' HelpSearch is called after the Help
  418.         ' form is unloaded (e.g. if HelpSetOptions
  419.         ' set UnloadOnClose to TRUE).
  420.         IF HelpFileName = "" THEN
  421.             MSGBOX "Help Search can only be used after registering a valid Help file.", 0, "Help"
  422.             EXIT SUB
  423.         ELSE
  424.             ' Register the Help file again.
  425.             HelpRegister HelpFileName, HelpLoaded
  426.             IF HelpLoaded = FALSE THEN
  427.                 MSGBOX "Help Search can only be used after registering a valid Help file.", 0, "Help"
  428.                 EXIT SUB
  429.             END IF
  430.         END IF
  431.     END IF
  432.  
  433.     ' Fill the search list box with all the Help
  434.     ' topics.  LstSearch.Sorted = TRUE so the topics will
  435.     ' be listed alphabetically.
  436.     FOR i = 1 TO UBOUND(HelpIndexTopics)
  437.         frmHelpUtils.lstSearch.ADDITEM HelpIndexTopics(i)
  438.     NEXT i
  439.  
  440.     ' Set the list index to the top.
  441.     frmHelpUtils.lstSearch.ListIndex = 0
  442.  
  443.     ' Show the Search form modally.
  444.     frmHelpUtils.SHOW 1
  445.  
  446.     ' The search form's tag property will be set
  447.     ' to the topic the user selected or null if Cancel
  448.     ' was selected.
  449.     RequestedTopic$ = frmHelpUtils.Tag
  450.     UNLOAD frmHelpUtils
  451.  
  452.     ' pctBackground must always have the focus so keys can
  453.     ' be trapped.
  454.     IF frmHelpMain.visible THEN frmHelpMain.pctBackground.SETFOCUS
  455.  
  456.     ' Show the selected topic.
  457.     IF RequestedTopic$ <> "" THEN
  458.         HelpShowTopic RequestedTopic$
  459.     END IF
  460. END SUB
  461.  
  462. ' Help option setting routine.
  463. '
  464. ' Sets colors and shows/hides buttons on the
  465. ' Help window's button bar.  This routine is
  466. ' optional as default settings are provided.
  467. '
  468. ' Parameters:
  469. '   BColor      - Sets help window background color (frmHelpMain.pctBackground.BackColor)
  470. '   FColor      - Sets help window foreground color (frmHelpMain.pctBackground.ForeColor)
  471. '   DBColor     - Sets the background color for Help dialogs (Search, Copy, History).
  472. '   DFColor     - Sets the foreground color for Help dialogs (Search, Copy, History).
  473. '   ButtonColor - Sets the background color for the five
  474. '                 help window button bar.  Set SCREEN.CONTROLPANEL(4)
  475. '                 to change button foreground color.
  476. '   SelectColor - Sets the color of the link markers.
  477. '   Flags       - Bit flags set the VISIBLE status
  478. '                 of the five command buttons and
  479. '                 enable other Help Toolit options.
  480. '                 Add the values below to show buttons
  481. '                 and enable options.
  482. '
  483. '                 Function               Value    Default
  484. '                 ---------------------  -----    -------
  485. '                 ESC closes Help            1       0 (Escape key does not close help window)
  486. '                 UNLOAD Help at Form close  2       0 (Help form in made invisible, not unloaded when form is closed)
  487. '                 No Contents Button         4       0 (Contents button is visible)
  488. '                 No Search Button           8       0 (Search button is visible)
  489. '                 No Back Button            16       0 (Back button is visible)
  490. '                 No History Button         32       0 (History button is visible)
  491. '                 No Copy Button            64       0 (Copy button is visible)
  492. '
  493. '                 For example, Flags = 65 allows ESC to
  494. '                 close Help and hides the Copy button.
  495. '                 Space for the button bar is only reserved
  496. '                 at the top frmHelpMain if at least one button is visible.
  497. '
  498. SUB HelpSetOptions (bcolor AS INTEGER, fcolor AS INTEGER, dbColor AS INTEGER, dfColor AS INTEGER, buttoncolor AS INTEGER, SelectColor AS INTEGER, Flags AS INTEGER)
  499.     ' Trap to ensure HelpRegister has already been called.
  500.     IF HelpFileNum = 0 THEN
  501.         MSGBOX "You must call the HelpRegister procedure before calling HelpSetOptions.  Proposed settings ignored.", 0, "Help"
  502.         EXIT SUB
  503.     END IF
  504.  
  505.     ' Confirm that the proposed color settings are valid.
  506.     ' If so, set the appropriate properties or variables.
  507.     IF bcolor >= 0 AND bcolor <= 15 THEN frmHelpMain.pctBackground.Backcolor = bcolor
  508.     IF fcolor >= 0 AND fcolor <= 15 THEN frmHelpMain.pctBackground.Forecolor = fcolor
  509.     IF SelectColor >= 0 AND SelectColor <= 15 THEN SelectedTopicColor = SelectColor
  510.     IF dbColor >= 0 AND dbColor <= 15 THEN DialogBackcolor = dbColor
  511.     IF dfColor >= 0 AND dfColor <= 15 THEN DialogForecolor = dfColor
  512.  
  513.     IF buttoncolor >= 0 AND buttoncolor <= 15 THEN
  514.         frmHelpMain.Backcolor = buttoncolor
  515.         FOR i = 0 TO 4
  516.             frmHelpMain.cmdButtonBar(i).Backcolor = buttoncolor
  517.         NEXT i
  518.     END IF
  519.     
  520.     ' Hide or show buttons as requested
  521.     FOR i = 0 TO 4
  522.         IF (Flags AND 2 ^ (i + 3)) = FALSE THEN
  523.             frmHelpMain.cmdButtonBar(i).Left = 13 * NumButtons
  524.             frmHelpMain.cmdButtonBar(i).visible = TRUE
  525.             NumButtons = NumButtons + 1
  526.         ELSE
  527.             frmHelpMain.cmdButtonBar(i).visible = FALSE
  528.         END IF
  529.     NEXT i
  530.  
  531.     ' Allow space for the button bar if at least one button
  532.     ' is shown.
  533.     IF NumButtons = 0 THEN
  534.         ButtonBarHeight = 0
  535.     ELSE
  536.         ButtonBarHeight = 3
  537.     END IF
  538.  
  539.     CloseOnEscape = Flags AND 1
  540.     UnloadOnClose = Flags AND 2
  541.     
  542. END SUB
  543.  
  544. ' Help topic display routine.
  545. '
  546. ' Displays the requested topic on the Help form.
  547. '
  548. ' Parameter:
  549. '   ProvidedTopic$ - The name of the topic that should be shown.
  550. '
  551. SUB HelpShowTopic (ProvidedTopic$)
  552.     ' Confirm that a Help file has been registered and
  553.     ' that non-null topic has been provided.
  554.     IF ProvidedTopic$ = "" THEN
  555.         MSGBOX "A Help topic must be supplied", 0, "Help"
  556.         EXIT SUB
  557.     END IF
  558.  
  559.     ' Confirm that a Help file is open.
  560.     IF HelpFileNum = 0 THEN
  561.         ' No open Help file.  HelpFileName will
  562.         ' be null if HelpRegister was never successful
  563.         ' but will have a valid file name if a Help
  564.         ' file was registered correctly but an error
  565.         ' such as Insufficient Memory to Display Help
  566.         ' caused Help to close.  Also occurs if
  567.         ' HelpShowTopic is called after the Help
  568.         ' form is unloaded (e.g. if HelpSetOptions
  569.         ' set UnloadOnClose to TRUE).
  570.         IF HelpFileName = "" THEN
  571.             MSGBOX "You must call the HelpRegister procedure before calling HelpShowTopic.", 0, "Help"
  572.             EXIT SUB
  573.         ELSE
  574.             ' Register the Help file again.
  575.             HelpRegister HelpFileName, HelpLoaded
  576.             IF HelpLoaded = FALSE THEN
  577.                 MSGBOX "You must call the HelpRegister procedure before calling HelpShowTopic.", 0, "Help"
  578.                 EXIT SUB
  579.             END IF
  580.         END IF
  581.     END IF
  582.  
  583.     ' Convert the topic request to uppercase to make
  584.     ' matching easier then scan the HelpIndex table
  585.     ' for that topic.
  586.     '
  587.     RequestedTopic$ = UCASE$(ProvidedTopic$)
  588.     TotalTopics = UBOUND(HelpIndexTopics)
  589.     TopicNum = 1
  590.     EndNow = FALSE
  591.     DO UNTIL EndNow
  592.         IF UCASE$(HelpIndexTopics(TopicNum)) = RequestedTopic$ THEN
  593.             EndNow = TRUE
  594.         ELSE
  595.             TopicNum = TopicNum + 1
  596.             IF TopicNum > TotalTopics THEN
  597.                 EndNow = TRUE
  598.                 TopicNum = 0
  599.             END IF
  600.         END IF
  601.     LOOP
  602.  
  603.     ' Exit if the topic does not exist.
  604.     IF TopicNum = 0 THEN
  605.         MSGBOX "Help topic '" + ProvidedTopic$ + "' not found.", 0, "Help"
  606.         EXIT SUB
  607.     END IF
  608.  
  609.     ' Seek to the topic's position in the Help file.
  610.     SEEK HelpFileNum, HelpIndexPtr(TopicNum).StartFilePtr
  611.     
  612.     ' REDIM the topic array so that it's the exact size
  613.     ' needed to store the requested topic.
  614.     MaxLines = HelpIndexPtr(TopicNum).NumLines
  615.  
  616.     ON LOCAL ERROR GOTO HelpShowOutOfMemory
  617.         REDIM Topic(1 TO MaxLines)  AS STRING
  618.     
  619.         ' Load the topic lines into the topic array
  620.         MaxLineLen = 0
  621.         FOR i = 1 TO MaxLines
  622.             LINE INPUT #HelpFileNum, Topic(i)
  623.  
  624.             ' Convert tabs (CHR$(9)) to spaces.
  625.             TabPos = INSTR(Topic(i), CHR$(9))
  626.             DO WHILE TabPos > 0
  627.                 Topic(i) = LEFT$(Topic(i), TabPos - 1) + STRING$(TabSpaces - (TabPos MOD TabSpaces), " ") + MID$(Topic(i), TabPos + 1)
  628.                 TabPos = INSTR(TabPos + TabSpaces - 1, Topic(i), CHR$(9))
  629.             LOOP
  630.             IF LEN(Topic(i)) > MaxLineLen THEN MaxLineLen = LEN(Topic(i))
  631.         NEXT i
  632.     ON LOCAL ERROR GOTO 0
  633.  
  634.     ' Set misc. values
  635.     frmHelpMain.hscHelp.Value = 1
  636.     frmHelpMain.hscHelp.min = 1
  637.     frmHelpMain.hscHelp.max = HelpMax(MaxLineLen - frmHelpMain.pctBackground.ScaleWidth + 1, 1)
  638.     frmHelpMain.vscHelp.max = HelpMax(MaxLines - frmHelpMain.pctBackground.ScaleHeight + 1, 1)
  639.     frmHelpMain.vscHelp.Value = 1
  640.     frmHelpMain.vscHelp.min = 1
  641.     frmHelpMain.Caption = "Help: " + ProvidedTopic$
  642.     TopicCursor.Y = 0
  643.     IF frmHelpMain.ScaleHeight > 3 THEN frmHelpMain.vscHelp.LargeChange = frmHelpMain.pctBackground.ScaleHeight
  644.     
  645.     ' Update the history stack
  646.     HelpTopicStackPtr = HelpTopicStackPtr + 1
  647.  
  648.     ' Only store the last MaxHistoryStack of
  649.     ' Help topics.
  650.     IF HelpTopicStackPtr > MaxHistoryStack THEN
  651.         'Would overflow.  Drop the least recent entry.
  652.         FOR i = 2 TO MaxHistoryStack
  653.             HelpTopicStack(i - 1) = HelpTopicStack(i)
  654.         NEXT i
  655.         HelpTopicStackPtr = MaxHistoryStack
  656.     END IF
  657.  
  658.     ' Add this topic to the Help stack
  659.     HelpTopicStack(HelpTopicStackPtr) = HelpIndexTopics(TopicNum)
  660.  
  661.     ' Enable/disable buttons based on the size of the
  662.     ' Help stack
  663.     SELECT CASE HelpTopicStackPtr
  664.     CASE 1
  665.         ' This is the first topic.  Both
  666.         ' BACK (2) and History(3) should be
  667.         ' disabled.
  668.         frmHelpMain.cmdButtonBar(2).Enabled = FALSE
  669.         frmHelpMain.cmdButtonBar(3).Enabled = FALSE
  670.     CASE 2
  671.         ' This is the second topic.
  672.         ' BACK (2) should be enabled but History(3)
  673.         ' isn't necessary.
  674.         frmHelpMain.cmdButtonBar(2).Enabled = TRUE
  675.         frmHelpMain.cmdButtonBar(3).Enabled = FALSE
  676.     CASE IS > 2
  677.         ' More than 2 topics have been shown so enabled
  678.         ' both Back and History.
  679.         frmHelpMain.cmdButtonBar(2).Enabled = TRUE
  680.         frmHelpMain.cmdButtonBar(3).Enabled = TRUE
  681.     END SELECT
  682.  
  683.     ' Restore the Help form to its former size/position
  684.     ' if it is currently minimized.
  685.     IF frmHelpMain.WindowState = 1 THEN
  686.         frmHelpMain.WindowState = 0
  687.     END IF
  688.  
  689.     ' Refresh the form to ensure that new button bar
  690.     ' states take effect.
  691.  
  692.     ' Actually display the topic, starting at
  693.     ' the top and far left
  694.     CursorPos.X = 0
  695.     CursorPos.Y = 0
  696.     HelpPrintText 1, 1
  697. EXIT SUB
  698.  
  699. HelpShowOutOfMemory:
  700.     MSGBOX "Insufficient memory to display Help.", 0, "Help"
  701.     HelpClose
  702.     EXIT SUB
  703.  
  704. END SUB
  705.  
  706.