home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / database / optmen.zip / OPT.PRG < prev    next >
Text File  |  1987-02-22  |  9KB  |  271 lines

  1. *
  2. *
  3. * Opt - Display menu of choices (maximum of 8) (VERSION 1.0)
  4. *
  5. * by Anthony T. DeHart - GENIE (ATDEHART)
  6. *
  7. *
  8. * Requires: choice - Array of menu selection descriptions
  9. *           maxopt - Number of options to be displayed in menu (not including
  10. *                    the quit option (element 9 but displayed as 0)
  11. *           pntr   - selection number of the option to highlight initially
  12. *                    (allows you to keep the last option selected highlighted
  13. *                     if you have to redraw the menu again)
  14. *           answer - the selection number of the choice made by the user
  15. *
  16. * Assumes:  Global variables reverse, highlight, and normal are setup
  17. *              see OPTSETUP
  18. *           Does not clear the screen so you must do that if you want it done
  19. *           MaxOpt <= 8 (no more than 8 menu items, can be modified for more
  20. *              but if you need more then you should probably break down
  21. *              your menus a little more)
  22. *
  23. * Action:   Draws a double column menu with up to 8 selections (not including
  24. *           option 0 which should be exit or return to calling menu) and
  25. *           centers it on the screen with boxes, etc.  Selection is done using
  26. *           a light bar with the cursor keys and the return key. Used in
  27. *           conjuction with lscrtop (see demo prg's included with arc file)
  28. *           creates a very professional looking menu. My only complaint is
  29. *           that on a regular PC is that it is not instantaneous
  30. *           when displayed. This is the first version and it probably could
  31. *           use some improvement.
  32. *
  33. * Usage:    I'm not to concerned with what is done with this routine but if
  34. *           you do modify it and want to repost it please notify me first
  35. *           via GENIE or DARWIN BBS. Also, I expect that you will also give
  36. *           credit where credit is due and leave my name and Genie ID here
  37. *           in the prg also. Have fun and take a look at some of the other
  38. *           files that go along with the demo prg. LSCRTOP, ASK_YN, PLAC_MSG
  39. *           are neat little programs and LSCRTOP looks nice when used with
  40. *           this routine.
  41. *                            Anthony T. DeHart
  42. *                            ATD Software
  43. *                            189 S. Keystone Ave.
  44. *                            Sayre, PA 18840
  45. *                            717-888-7439
  46. *                            GENIE (ATDEHART)
  47. *
  48. *
  49.  
  50. Parameters choice, maxopt, pntr, answer
  51.  
  52. ***** Declare arrays
  53.  
  54. private X_Cursor, Y_Cursor
  55. declare X_Cursor[9]
  56. declare Y_Cursor[9]
  57.  
  58. ***** Pad menu strings with spaces up to max length of longest choice
  59.  
  60. maxLen = len(alltrim(choice[9]))
  61. for counter = 1 to maxopt
  62.    if len(alltrim(choice[counter])) > maxLen
  63.       maxLen = len(alltrim(choice[counter]))
  64.    endif
  65. next
  66. if maxLen + 9 > 40
  67.    maxLen = maxLen - ((maxLen + 9) - 40)
  68. endif
  69.  
  70. Y_Base = 9   && Y location of center of menu
  71. L_Column = 20 - int((maxLen + 8) / 2)  && Left column position
  72. R_Column = 60 - int((maxLen + 8) / 2)  && Right column position
  73. Top_Bar = '┌'+replicate('─',3)+'┬'+replicate('─',maxLen+2)+'┐'
  74. Mid_Bar = '├'+replicate('─',3)+'┼'+replicate('─',maxLen+2)+'┤'
  75. Bot_Bar = '└'+replicate('─',3)+'┴'+replicate('─',maxLen+2)+'┘'
  76.  
  77. * Find X and Y locations for the options and stick in array
  78.  
  79. cutoff = int((maxopt/2)+.5)  && Last menu item in left column
  80.  
  81. Y_Base = Y_Base + (4 - cutoff)  && Center meny on screen
  82.  
  83. ***** Assign X and Y location in array for each menu item and Draw Menu
  84.  
  85. ***** Left Column
  86.  
  87. for counter = 1 to cutoff
  88.    X_Cursor[counter] = L_Column
  89.    Y_Cursor[counter] = Y_Base + ((counter - 1) * 2)
  90.    Choice[counter] = left(choice[counter]+space(maxLen+2),maxLen)
  91.    if counter = 1
  92.       @ Y_Cursor[counter]-1,X_Cursor[counter] say Top_Bar
  93.    else
  94.       @ Y_Cursor[counter]-1,X_Cursor[counter] say Mid_Bar
  95.    endif
  96.    @ Y_Cursor[counter],X_Cursor[counter] say '│ '+alltrim(str(counter,1))+' │ '+choice[counter]+' │'
  97.    if counter = cutoff
  98.       @ Y_Cursor[counter]+1,X_Cursor[counter] say Bot_Bar
  99.    endif
  100. next
  101.  
  102. ***** Right Column
  103.  
  104. for counter = cutoff+1 to maxopt
  105.    X_Cursor[counter] = R_Column
  106.    Y_Cursor[counter] = Y_Base + ((counter - (cutoff+1)) * 2)
  107.    Choice[counter] = left(choice[counter]+space(maxLen+2),maxLen)
  108.    if counter = cutoff+1
  109.       @ Y_Cursor[counter]-1,X_Cursor[counter] say Top_Bar
  110.    else
  111.       @ Y_Cursor[counter]-1,X_Cursor[counter] say Mid_Bar
  112.    endif
  113.    @ Y_Cursor[counter],X_Cursor[counter] say '│ '+alltrim(str(counter,1))+' │ '+choice[counter]+' │'
  114.    if counter = maxopt
  115.       @ Y_Cursor[counter]+1,X_Cursor[counter] say Bot_Bar
  116.    endif
  117. next
  118.  
  119. ***** Menu selection 0 Location and Draw It
  120.  
  121. X_Cursor[9] = L_Column + int((R_Column - L_Column)/2)
  122. Y_Cursor[9] = Y_Base + 1 + (cutoff*2)
  123. Choice[9] = left(choice[9]+space(maxLen+2),maxLen)
  124. @ Y_Cursor[9]-1,X_Cursor[9] say Top_Bar
  125. @ Y_Cursor[9],X_Cursor[9] say '│ 0 │ '+choice[9]+' │'
  126. @ Y_Cursor[9]+1,X_Cursor[9] say Bot_Bar
  127.  
  128. ***** Draw Instructions
  129.  
  130. store 'Move cursor ' + chr(25) + ' ' + chr(24) + ' ' + chr(27) + ' ' + chr(26) +  ' to highlight choice and press ' + chr(17) + chr(217) to message1
  131. store 'or press number of selection and ' + chr(17) + chr(217) + ' to select menu option' to message2
  132. @ 21,0 to 24,79 double
  133. @ 22,(80 - len(message1))/2 say message1
  134. @ 23,(80 - len(message2))/2 say message2
  135.  
  136.  
  137.  
  138. answer = 0  && Selection number when return is pressed
  139. oldpntr = 0  && Menu item highlighted before cursor is moved
  140. if pntr = 0  && Menu item to be highlighted on screen (current cursor pos)
  141.    pntr = 1
  142. endif
  143. if pntr = 9
  144.    pntr = 0
  145.    oldpntr = 1
  146. endif
  147. curspntr = pntr  && Actual pntr to cursor selection
  148. LastColumn = 0 && Last Column that cursor was in
  149.  
  150. do while .T.
  151.    if oldpntr # curspntr  && If cursor moved then update position
  152.       if oldpntr = 0
  153.      oldpntr = 9
  154.       endif
  155.       if curspntr = 0
  156.      curspntr = 9
  157.       endif
  158. ***** Unhighlight old position
  159.       set color to &normal
  160.       @ Y_Cursor[oldpntr],X_Cursor[oldpntr]+6 say choice[oldpntr]
  161. ***** Highlight new position
  162.       set color to &reverse
  163.       @ Y_Cursor[curspntr],X_Cursor[curspntr]+6 say choice[curspntr]
  164.       set color to &normal
  165.       @ Y_Cursor[curspntr],X_Cursor[curspntr]+2 say ''
  166.       if curspntr = 9
  167.      curspntr = 0
  168.       endif
  169.       if oldpntr # 9
  170.      LastColumn = X_Cursor[oldpntr]
  171.       endif
  172.       oldpntr = curspntr
  173.    endif
  174.  
  175. ***** Get keypress and update time on screen
  176.    KeyCode = 0
  177.    do while Keycode = 0
  178.       Keycode = inkey(1)
  179.       stime = time()
  180.       @ 2,71 say stime
  181.       if curspntr # 0
  182.      @ Y_Cursor[curspntr],X_Cursor[curspntr]+2 say ''
  183.       else
  184.      @ Y_Cursor[9],X_Cursor[9]+2 say ''
  185.       endif
  186.    enddo
  187.    do case
  188.       case Keycode = 13 && Return
  189.        answer = curspntr
  190.        exit
  191.  
  192. ***** Movement routines are a little more complex than usual
  193. ***** Rather than describe it, run the program and move the cursor around
  194. ***** using the up and down arrows and you'll see why all the if statements
  195. ***** and case checking.
  196.  
  197.       case Keycode = 5 && Up Arrow
  198.        if curspntr = 0
  199.           do case
  200.          case LastColumn = L_Column
  201.             curspntr = maxopt
  202.             loop
  203.          case LastColumn = R_Column
  204.             curspntr = cutoff
  205.             loop
  206.           endcase
  207.        endif
  208.        if curspntr = cutoff + 1
  209.           curspntr = 0
  210.           loop
  211.        endif
  212.        curspntr = curspntr - 1
  213.        loop
  214.       case Keycode = 24 && Down Arrow
  215.        if curspntr = 0 .and. LastColumn = L_Column
  216.           curspntr = cutoff + 1
  217.           loop
  218.        endif
  219.        if (curspntr = maxopt) .or. (curspntr = cutoff)
  220.           curspntr = 0
  221.           loop
  222.        endif
  223.        curspntr = curspntr + 1
  224.        loop
  225.       case Keycode = 4 && Right Arrow
  226.        if curspntr # 0
  227.           if curspntr <= cutoff
  228.          curspntr = curspntr + cutoff
  229.           endif
  230.           if curspntr > maxopt
  231.          curspntr = maxopt
  232.           endif
  233.        else
  234.           curspntr = maxopt
  235.        endif
  236.       case Keycode = 19 && Left Arrow
  237.        if curspntr # 0
  238.           if curspntr > cutoff
  239.          curspntr = curspntr - cutoff
  240.           endif
  241.        else
  242.           curspntr = cutoff
  243.        endif
  244.  
  245. ***** Can also select by using the number of the routine and hitting return
  246.  
  247.       case chr(Keycode) >= '0' .and. chr(Keycode) <= alltrim(str(maxopt,1))
  248.        curspntr = val(chr(Keycode))
  249.    endcase
  250. enddo
  251.  
  252. if answer = 0
  253.    answer = 9
  254. endif
  255.  
  256. ***** Unhighlight description and highlight actual number of description
  257.  
  258. set color to &reverse
  259. if answer # 9
  260.    @ Y_Cursor[answer], X_Cursor[answer]+2 say alltrim(str(answer,1))
  261. else
  262.    @ Y_Cursor[answer], X_Cursor[answer]+2 say '0'
  263. endif
  264. set color to &normal
  265. @ Y_Cursor[answer], X_Cursor[answer]+6 say choice[answer]
  266.  
  267. return
  268.  
  269.  
  270.  
  271.