'===========================================================================
' Subject: PB SCROLL BOX W/HI-LITE MARKER     Date: 02-10-97 (16:52)       
'  Author: Peter Scatliffe                    Code: PB                     
'  Origin: Peter_Scatliffe@CompuServe.com   Packet: MENU.ABC
'===========================================================================
'_|_|_|   SCRLBOX2.BAS
'_|_|_|   This program demonstrates the use of a configurable scroll
'_|_|_|   box with hi-lite marker.
'_|_|_|   It will read and display itself when it is run.
'_|_|_|   Released to the   PUBLIC DOMAIN   by Kurt Kuzba.
'_|_|_|   Modified (& still PUBLIC DOMAIN) February 1997 by Peter Scatliffe
'_|_|_|   to:
'_|_|_|    - Powerbasic 3.0c
'_|_|_|
'_|_|_|    - Allowing the passing of a LowerLimit (LowLim%) so that you can
'_|_|_|      select a range in the middle of a string array (say elements
'_|_|_|      10 to 50.  The highest element used is set by HighLim%.
'_|_|_|
'_|_|_|    - I also pass the array to be displayed as a parameter rather
'_|_|_|      than as a global variable because it is more flexible.
'_|_|_|   If you want the unused portion of the screen filling remove the
'_|_|_|   5 rem's below.

DECLARE FUNCTION ScrollBox% (Passed(1) as string, LowLim%, HiLim%, high%, wide%, LIN%, COL%,Options%)

TYPE textline
   l AS STRING * 80
END TYPE

dim txt(800) as string
dim Selected as integer
dim t as integer

OPEN "scrlbox2.bas" FOR INPUT AS #1
 t = 0
 WHILE ((t < 800) AND (NOT EOF(1)))
   LINE INPUT #1, txt(t)
   t = t + 1
 WEND
CLOSE 1

Selected = ScrollBox%( txt(), 1, t-1, 10, 50, 5, 10, 0 )

COLOR 2, 0: CLS
if Selected% < 0 then
  PRINT "You hit ESCAPE"; Selected
  else
  PRINT "You hit ENTER on array item"; Selected
end if
END
'_|_|_|   end   SCROLBOX.BAS


'**************************************************************************
'This function will display a list box of items in the string array
Passed()
'  - LowLim and High Lim allow you to limit the list parts of the array
'  - High & Wide define the number of rows High and columns wide.
'  - Lin and Col define the position of the top left hand corner
'  - Options =
'               0 no sort
'               1 alpha/numeric sort ascending (case sensitive)
'               2 alpha/numeric sort ascending (ignore case)
'               3 alpha/numeric sort descending (case sensitive)
'               4 alpha/numeric sort descending (ignore case)
'    NB The array you pass if you sort will be changed.  If you only
'       select part if the array to display, then only the selected part
'       part will be sorted.
'The function returns -1 if ESCAP was pressed, or the element number if
'RETURN was pressed

FUNCTION ScrollBox% (Passed(1) as string,LowLim as integer, HiLim as integer, High as integer, Wide as integer, Lin as integer, Col as integer, Options as integer)

   dim Sel as integer
   dim Top as integer
   dim ex as integer
   dim s as string
   dim t as integer
   dim p as integer
   dim pp as string

   local Sel, Top, ex, s, t, p, pp



   select case LowLim
     case < lbound(Passed())
       LowLim = lbound(Passed())
     case = 0
       LowLim = lbound(Passed())
   end select

   select case HiLim
     case = 0
       HiLim = lbound(Passed())
     case > ubound(Passed())
       HiLim = ubound(Passed())
     case <= LowLim
       HiLim = ubound(Passed())
   end select

   HiLim = HiLim - LowLim + 1

   select case Options
     case = 1
       array sort Passed(LowLim) for HiLim
     case = 2
       array sort Passed(LowLim) for HiLim, collate ucase
     case = 3
       array sort Passed(LowLim) for HiLim, descend
     case = 4
       array sort Passed(LowLim) for HiLim, collate ucase, descend

   end select

   COLOR 7, 0: CLS : Sel = 0: Top = 0: ex = HiLim
   COLOR 9, 1: LOCATE 1, 1, 0: s = STRING$(Wide, " ")
rem   FOR t = 0 TO High + 1
rem      LOCATE Lin + t, Col
rem      PRINT STRING$(Wide + 4, CHR$(177));
rem   NEXT
   Lin = Lin + 1: Col = Col + 2
   WHILE HiLim = ex
      COLOR 9, 1: LOCATE Lin - 1, Col
rem      PRINT MID$(STR$(Sel + 1), 2) + STRING$(3, CHR$(177))
      FOR t = 0 TO (High - 1)
         LOCATE Lin + t, Col: COLOR 1, 3
         IF t + Top = Sel THEN COLOR 0, 7
         PRINT mid$(Passed(Top + t + LowLim) + s, 1, Wide);
      NEXT
      p = 0: pp = " HPIQGO" + CHR$(13) + CHR$(27)
      WHILE p < 2: p = INSTR(pp, RIGHT$(INKEY$, 1)): WEND
      SELECT CASE p
         CASE = 2   'CURSOR UP
            IF Sel > 0 THEN Sel = Sel - 1
            IF Sel < Top THEN Top = Sel
         CASE = 3   'CURSOR DOWN
            IF Sel < (HiLim - 1) THEN Sel = Sel + 1
            IF Sel > (Top + (High - 2)) THEN Top = Sel - High + 1
         CASE = 4   'PAGE UP
            Top = Top - High: Sel = Sel - High
            IF Top < 0 THEN Top = 0
            IF Sel < LowLim THEN Sel = 0
         CASE = 5   'PAGE DOWN
            Top = Top + High: Sel = Sel + High
            IF Top >= HiLim - High THEN Top = HiLim - High
            IF Top < 0 THEN Top = 0
            IF Sel >= HiLim THEN Sel = HiLim - 1
         CASE = 6   'HOME
            Top = 0: Sel = 0
         CASE = 7   'END
            Sel = HiLim - 1: Top = Sel - High + 1
            IF Top < 0 THEN Top = 0
         CASE = 8   'ENTER
            HiLim = Sel
         CASE = 9   'ESCAPE
            HiLim = -1
            Sel = -1 - LowLim
      END SELECT
   WEND
   ScrollBox% = Sel + LowLim
END FUNCTION
