home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a114 / 2.img / TOOLKIT / TOOLKIT.ZIP / POPUP2.SC < prev    next >
Encoding:
Text File  |  1990-08-25  |  13.0 KB  |  291 lines

  1. ; Copyright (c) 1988, 1989 Borland International.  All Rights Reserved.
  2. ;
  3. ; General permission to re-distribute all or part of this script is granted,
  4. ; provided that this statement, including the above copyright notice, is not
  5. ; removed.  You may add your own copyright notice to secure copyright
  6. ; protection for new matter that you add to this script, but Borland
  7. ; International will not support, nor assume any legal responsibility for,
  8. ; material added or changes made to this script.
  9. ;
  10. ; Revs.:  DCY 12/15/88
  11. ; ****************************************************************************
  12. ; SetPopup2 initializes variables required by Popup2 from data stored in a
  13. ; table.  It requires a table name and a field name from which to read menu
  14. ; item information.  Basically, it views and scans the given table, defining
  15. ; menu items as elements within an array.  It also determines the widest
  16. ; element of the array (not necessarily the width of the field), assigning it
  17. ; to another variable also required by Popup2.
  18. ;
  19. Proc SetPopup2(PopTbl,Fld)
  20. ;  Private;PopTbl,      ;Source table for items of menu
  21.           ;Fld,         ;Source field for items of menu
  22. ;  Global ;Item,        ;Array of items of menu
  23.           ;Width        ;Width of widest item
  24.  
  25.    Array Item[NRecords(PopTbl)]    ;Dimension Item array.  One item per record
  26.    View PopTbl                     ;  in PopTbl.
  27.    MoveTo Field Fld
  28.    Width = 0
  29.    If Search("A",FieldType()) = 0  ;If field is non-alphanumeric, convert it
  30.       Then Scan                    ; to a string value before assigning it
  31.               Item[[#]] = Strval([])
  32.               Width = Max(Len(Item[[#]]),Width)  ;Update max. width
  33.            Endscan
  34.       Else Scan
  35.               Item[[#]] = []
  36.               Width = Max(Len([]),Width)
  37.            Endscan
  38.    Endif
  39.  
  40. Endproc
  41.  
  42. ; Popup2 displays a similar popup-style menu to that of Popup.  However, it
  43. ; does have some enhancements and restrictions compared to Popup.  For
  44. ; example, with Popup2, a user can move to a selection by just pressing the
  45. ; first letter of a selection.  It there are no selections which begin with
  46. ; that first letter below the current menu position, Popup2 sounds a beep.
  47. ; You can also instruct Popup2 to highlight a specific menu item when it
  48. ; first displays a menu.  Popup2 also allows you to specify a title for the
  49. ; menu box as well as a custom top-two line prompt.  Lastly, it includes color
  50. ; support (you can define your own color set, see the code below).
  51. ;
  52. ; Unlike Popup, however, Popup2 stores only one menu item list in its menu
  53. ; item array (named Item).  You can either use SetPopup2 to fill the Item
  54. ; array from records in a table or you can define and fill the array
  55. ; yourself.  If you do so, you must assign the variable Width a value equal
  56. ; to the number of characters of the widest item in the Item array.  Because
  57. ; Popup2 stores only one menu item list at a time in the Item array, you'll
  58. ; need to redimension and reassign Item and Width each time you wish to
  59. ; display a different menu.  However, depending upon the size and number of
  60. ; different menus you wish to display, the resultant memory savings should
  61. ; more than balance the slight performance degradation.
  62. ;
  63. ; Popup2 dynamically centers the menu box title such that the entire title
  64. ; will always be displayed.  Note, however, that for performance Popup2 does
  65. ; no special error handling.  Thus you should ensure that the arguments you
  66. ; give it are indeed valid, i.e., the canvas coordinates must allow the entire
  67. ; menu to fit on the screen, the default item number must be within the legal
  68. ; range, etc.
  69.  
  70. Proc Popup2(R,C,VNum,DefItem,Title,Prompt1,Prompt2)
  71.    Private;R,           ;Row position of upper-left corner of menu box
  72.           ;C,           ;Column position of upper-left corner menu box
  73.           ;VNum,        ;Number of items to be displayed in one menu image
  74.           ;DefItem,     ;Item (number) to show
  75.           ;Title,       ;Title of popup box
  76.           ;Prompt1,     ;First prompt line
  77.           ;Prompt2,     ;Second prompt line
  78.            NItems,      ;Number of items in menu list
  79.            Char,        ;Keycode of last key pressed
  80.            MenuPos,     ;Current (row image) position within menu
  81.            CIndex,      ;Current choice index into Item
  82.            X,           ;Counter variable
  83.            PrmptColr,   ;Color attribute for prompt
  84.            BrdrColr,    ;Color attribute for box border
  85.            ListColr,    ;Color attribute for menu item list
  86.            SlctColr     ;Color attribute for current menu selection
  87. ;  Global ;Item,        ;Array of items of menu
  88.           ;Width        ;Width of widest item
  89.  
  90.    Echo Off                     ;Freeze workspace image
  91.    Cursor Off                   ;Hide blinking cursor
  92.    Canvas Off                   ;Disable immediate printing to canvas
  93.  
  94.    PrmptColr = SysColor(0)      ;Top two line prompt color
  95.    BrdrColr = SysColor(9)       ;Border color
  96.    ListColr = SysColor(17)      ;Menu list color
  97.    SlctColr = SysColor(18)      ;Current menu selection color
  98.  
  99.    Style Attribute PrmptColr
  100.    @ 0,0             ;Display prompt information
  101.    ?? Spaces(80)+Prompt2+Spaces(80-Len(Prompt2))
  102.    @ 0,0
  103.    ?? Prompt1
  104.  
  105.    Width = Max(Len(Title),Width)  ;Expand box width if title is too wide
  106.    NItems = ArraySize(Item)     ;Set number of items in list
  107.    If VNum > NItems
  108.       Then VNum = NItems
  109.    Endif
  110.  
  111.    Switch
  112.       Case DefItem < VNum :  ;Redraw top screen
  113.          Redraw = 0
  114.          MenuPos = DefItem
  115.       Case DefItem > NItems-VNum :  ;Redraw last screen
  116.          Redraw = NItems-VNum
  117.          MenuPos = DefItem-NItems+VNum
  118.       Otherwise :      ;Redraw intermediate screen
  119.          Redraw = DefItem-1  ;Place item at top of menu
  120.          MenuPos = 1
  121.    Endswitch
  122.  
  123.                                 ;Set default menu settings:
  124.    CIndex = DefItem                     ;First menu item
  125.    LastPos = MenuPos                    ;Last image position is current pos.
  126.    LastIdx = CIndex                     ;Last menu item index
  127.  
  128.    Style Attribute BrdrColr
  129.    SetMargin C
  130.    @ R,C                        ;Draw menu skeleton and initial image
  131.    ?? "╔",Fill("═",Width+2),"╗"
  132.     ? "║ ",Format("AC,W"+Strval(Width),Title)," ║"
  133.     ? "╟",Fill("─",Width+2),"╢"
  134.    For X From 1 To VNum
  135.     ? "║",Spaces(Width+2),"║"
  136.    Endfor
  137.     ?  "╚",Fill("═",Width+2),"╝"
  138.  
  139.    @ R+3,C+1
  140.    If Redraw = 0                     ;Records above?
  141.       Then ?? " "
  142.       Else ?? ""                    ; Yes- Show items are above
  143.    Endif
  144.    @ R+VNum+2,C+1
  145.    If Redraw+VNum = NItems           ;Records below?
  146.       Then ?? " "
  147.       Else ?? ""                    ; Yes- Show items are below
  148.    Endif
  149.  
  150.    @ R+2,C+2
  151.    SetMargin C+2
  152.    Style Attribute ListColr
  153.    For X From 1 To VNum
  154.     ? Item[X+Redraw]+Spaces(Width-Len(Item[X+Redraw]))
  155.    Endfor
  156.    SetMargin Off
  157.  
  158.    Canvas On                    ;Reenable immediate echoing to canvas
  159.  
  160.    Redraw = -1                       ;Disable menu image redraw
  161.  
  162.    While True
  163.  
  164.       Style Attribute SlctColr
  165.       @ MenuPos+R+2,C+2         ;Highlight current selection
  166.       ?? Item[CIndex]
  167.       Style Attribute ListColr
  168.  
  169.       Char = getchar()
  170.  
  171.       Switch
  172.          Case Char > 31:                        ;First character search?
  173.             For X From CIndex+1 to NItems       ;Search (down) item array
  174.                If Upper(Substr(Item[X],1,1)) = Upper(Chr(Char))
  175.                   Then QuitLoop                 ;Found a match
  176.                Endif
  177.             Endfor
  178.             If X = NItems+1                     ;Match not found
  179.                Then Beep
  180.                Else If MenuPos+X-CIndex > VNum  ;Is next item already visible?
  181.                        Then Switch              ; No-
  182.                                Case X < VNum :  ;Redraw top screen
  183.                                   Redraw = 0
  184.                                   MenuPos = X
  185.                                Case X > NItems-VNum :  ;Redraw last screen
  186.                                   Redraw = NItems-VNum
  187.                                   MenuPos = X-NItems+VNum
  188.                                Otherwise :      ;Redraw intermediate screen
  189.                                   Redraw = X-1  ;Place item at top of menu
  190.                                   MenuPos = 1
  191.                             Endswitch
  192.                        Else MenuPos = MenuPos+X-CIndex
  193.                             Redraw = -1
  194.                     Endif
  195.                     CIndex = X                  ;Update current item
  196.             Endif
  197.          Case Char = -72 :                      ;Up
  198.             If CIndex = 1                       ;Already at first item?
  199.                Then Beep
  200.                Else If MenuPos > 1              ;Can move within menu image?
  201.                        Then MenuPos = MenuPos-1 ; Yes- Move to previous item
  202.                        Else Redraw = CIndex-2   ; No-  Redraw entire menu
  203.                     Endif
  204.                     CIndex = CIndex-1           ;Update current item
  205.             Endif
  206.          Case Char = -80 :                      ;Down
  207.             If CIndex = NItems                  ;On last item?
  208.                Then Beep
  209.                     Loop
  210.                Else If MenuPos < VNum           ;Can move within menu image?
  211.                        Then MenuPos = MenuPos+1    ; Yes-  Move to next item
  212.                        Else Redraw = CIndex-VNum+1 ; No- Redraw entire menu
  213.                     Endif
  214.                     CIndex=CIndex+1             ;Update current item
  215.             Endif
  216.          Case Char = -71 :                      ;Home
  217.             If MenuPos <> CIndex                ;Already viewing top of menu?
  218.                Then Redraw = 0                  ; No-  Redraw top of menu
  219.             Endif
  220.             MenuPos = 1                         ;Position at first item
  221.             CIndex = 1                          ;Select first item
  222.          Case Char = -79 :                      ;End
  223.             If CIndex+VNum-MenuPos <> NItems    ;Already viewing end of menu?
  224.                Then Redraw = NItems-VNum        ; No-  Redraw end of menu
  225.             Endif
  226.             MenuPos = VNum                      ;Position at bottom of menu
  227.             CIndex = NItems                     ;Select last item
  228.          Case Char = -73 :                      ;PgUp
  229.             If MenuPos = CIndex                 ;Are we within first screen?
  230.                Then Beep                        ; Yes- Disallow PgUp
  231.                Else If CIndex-MenuPos-VNum > 0
  232.                        Then CIndex = CIndex-MenuPos-VNum+1
  233.                        Else CIndex = 1
  234.                     Endif
  235.                     Redraw = CIndex-1           ; No-  Redraw previous page
  236.                     MenuPos = 1                 ;      Position on that item
  237.             Endif
  238.          Case Char = -81 :                      ;PgDn
  239.             If CIndex+VNum-MenuPos = NItems ;Are we within last screen?
  240.                Then Beep                        ; Yes- Disallow PgDn
  241.                Else If NItems-VNum < CIndex+VNum-MenuPos
  242.                        Then CIndex = NItems-VNum+1
  243.                        Else CIndex = CIndex+VNum-MenuPos+1
  244.                     Endif
  245.                     Redraw = CIndex-1           ; No- Redraw next page
  246.                     MenuPos = 1                 ;     Position on that item
  247.             Endif
  248.          Case Char = 13 :                       ;Enter
  249.             Cursor Normal
  250.             Style
  251.             Return Item[CIndex]                 ;Return selection
  252.          Case Char = 27 :                       ;Esc
  253.             Cursor Normal
  254.             Style
  255.             Return ""                           ;Return null selection
  256.          Otherwise:                             ;Illegal key
  257.             Beep
  258.       Endswitch
  259.  
  260.       If Redraw = -1                            ;Need to redraw entire menu?
  261.          Then @ LastPos+R+2,C+2
  262.               ?? Item[LastIdx]
  263.          Else Canvas Off                        ;Disable immediate canvas echo
  264.               SetMargin C+2
  265.               @ R+2,C+2
  266.               For X from 1 to VNum              ;Redraw entire menu box
  267.                  ? Item[Redraw+X]+Spaces(Width-Len(Item[Redraw+X]))
  268.               Endfor
  269.               SetMargin Off
  270.               @ R+3,C+1
  271.               Style Attribute BrdrColr
  272.               If Redraw = 0                     ;Records above?
  273.                  Then ?? " "
  274.                  Else ?? ""                    ; Yes- Show items are above
  275.               Endif
  276.               @ R+VNum+2,C+1
  277.               If Redraw+VNum = NItems           ;Records below?
  278.                  Then ?? " "
  279.                  Else ?? ""                    ; Yes- Show items are below
  280.               Endif
  281.               Canvas On                         ;Enable immediate canvas echo
  282.               Redraw = -1
  283.       Endif
  284.  
  285.       LastPos = MenuPos                         ;Update last row position
  286.       LastIdx = CIndex                          ;Update last item index
  287.  
  288.    Endwhile
  289.  
  290. Endproc
  291.