_TITLE "getArrayItemClicked v4 mbox"
' Main testing and demo of the FUNCTION getArrayItemNumber%
'started 2018-08-31 B+ developing a general purpose display and select app of a string array
' in this version want
' 1 to be able to select item from larger arrays that take several pages to screen
' 2 highlite mouse over ?
' 3 colorize print? Eh! bloats parameters in call, if really want, modify function for app.
' Using dark blue on sky blue and reverse for highlite, pretty easy on eyes and sets off
' selection area.
' 4 keep modifiable for possibly adding fonts, I think that means use only locate and print,
' as _printstring works in pixels, no good for different fonts. (Neither is LOCATE)
' 2018-09-01 yea, finally got everything working and playing together
' oh yeah, to mouse click an escape along with expected escape button press.
' I might to build a frame around the list box control? (No, just a bar above and below)
'2018-09-03 OK everything working great! But I have added two lines to maxHeight.
' Do I adjust maxHeight to reflect actual height of box on screen, I think better do it.
' Oh heck that was easy! Just calc maxHeight off boxHeight!
' Clean up instructions for using the function.
' NEXT project is message box that doesn't need Windows OS calls,
' so I could offer help here with h keypress!
' Just tested a font with locate and print and locate does not work.
' So this is done until I get messageBox working.
'2018-09-04 post v2 bak 9-3_9PM
'2018-09-07 getArrayItem v4 mbox - time to test mbox with help window
CONST nArr
= 92 'ubound of array = actual amount of items if LBound = 1 CONST LB
= 1 'try different lower bounds not just 0, 1 CONST WW
= 1200 'Window Width CONST WH
= 600 'Window Height
'test string array, use indexes in lines for alignment to code for function
arr
(i
) = "This is arr item:" + STR$(i
)
cp "*** Mouse and Key Instructions ***"
cp "Mouse, mouse wheel, and arrow keys should work as expected for item selection."
cp "Press spacebar to select a highlighted item or just click it."
cp "Use number(s) + enter to select an array item by it's index number,"
cp "backspace will remove last number pressed, c will clear a number started."
cp "Numbers started are shown in bottom right PgDn bar."
cp "Enter will also select the highlighted item, if no number has been started."
cp "Home starts you at lowest array index, End highlights then highest index."
cp "Use PgUp and PgDn keys or bars to flip through pages of array items."
cp "Escape returns -1719 to allow a Cancel function and signal no slection."
locRow = 16: locCol = (WW / 8 - 25) / 2: boxWidth = 25: boxHeight = 17 '< displays 15 lines of array items
'boxHeight is actual screen space in character units, the display uses 2 of the lines for control bars.
'boxWidth will include item numbers displayed to left of array string item
CH = getArrayItemNumber(locRow, locCol, boxWidth, boxHeight, arr())
IF CH
= -1719 THEN cp
"You canceled selection process." ELSE cp
"You chose index (" + LTRIM$(STR$(CH
)) + ") = " + arr
(CH
)
'see this file
lnCnt = fLines("getArrayItem v4 mbox.bas", fArr()) '<<<<<<<<<<<<<<<<<<< or whatever name you gave this file AND SAVED!
CH = getArrayItemNumber(5, 5, 140, 30, fArr())
' Future Help Message Box for the function.
' "*** Mouse and Key Instructions ***"
'
' "Mouse, mouse wheel, and arrow keys should work as expected for item selection."
' "Press spacebar to select a highlighted item or just click it."
' "Use number(s) + enter to select an array item by it's index number,"
' "backspace will remove last number pressed, c will clear a number started."
' "Numbers started are shown in bottom right PgDn bar."
' "Enter will also select the highlighted item, if no number has been started."
' "Home starts you at lowest array index, End highlights then highest index."
' "Use PgUp and PgDn keys to flip through pages of array items."
'
' "Escape returns -1719 to allow a Cancel function and signal no slection."
FUNCTION getArrayItemNumber&
(locateRow
, locateColumn
, boxWidth
, boxHeight
, arr
() AS STRING) 'Notes: locateRow, locateColumn for top right corner of selection box on screen in characters for LOCATE.
'boxWidth and boxHeight are in character units, again for locate and print at correct places.
'All displaying is restricted to inside the box, which has PgUP and PgDn as top and bottom lines in the display.
'
maxWidth = boxWidth ' number of characters in box
maxHeight = boxHeight - 2 ' number of lines displayed of array at one time = 1 page
page = 0
hlite = 0 ' line in display ready for selection by spacebar or if no number is started, enter
clrStr$
= SPACE$(maxWidth
) 'clearing a display line
GOSUB update
' show the beginning of the array items for selection
'signal cancel selection process, exit sub with this unlikely index to signal canel
choice = -1719 'primes 7 and 8, not likely to be a select index of an array
DO 'until get a selection or demand exit
'handle the key stuff
IF kh&
= 13 THEN 'enter pressed check if number is being entered? ELSE 'clear b$ to show some response to enter b$
= "":
GOSUB update
'clear the value that doesn't work choice = hlite + page * maxHeight + lba 'must mean to select the highlighted item
IF kh&
= 27 THEN EXIT DO 'escape clause offered to Cancel selection process IF kh&
= 32 THEN choice
= hlite
+ page
* maxHeight
+ lba
'best way to choose highlighted selection IF kh&
= 8 THEN 'backspace to edit number SELECT CASE kh&
'choosing sections of array to display and highlighted item IF (page
+ 1) * maxHeight
+ lba
<= uba
THEN page
= page
+ 1:
GOSUB update
IF (page
- 1) * maxHeight
+ lba
>= lba
THEN page
= page
- 1:
GOSUB update
page
= page
- 1: hlite
= maxHeight
- 1:
GOSUB update
hlite
= hlite
- 1:
GOSUB update
IF (hlite
+ 1) + page
* maxHeight
+ lba
<= uba
THEN 'ok to move up IF hlite
+ 1 > maxHeight
- 1 THEN page
= page
+ 1: hlite
= 0:
GOSUB update
hlite
= hlite
+ 1:
GOSUB update
page
= 0: hlite
= 0:
GOSUB update
page
= INT((uba
- lba
) / maxHeight
): hlite
= maxHeight
- 1:
GOSUB update
'handle the mouse stuff
page
= page
- 1: hlite
= maxHeight
- 1:
GOSUB update
hlite
= hlite
- 1:
GOSUB update
IF (hlite
+ 1) + page
* maxHeight
+ lba
<= uba
THEN 'ok to move up IF hlite
+ 1 > maxHeight
- 1 THEN page
= page
+ 1: hlite
= 0:
GOSUB update
hlite
= hlite
+ 1:
GOSUB update
choice = my + page * maxHeight + lba - 1 'select item clicked
IF my
= 0 AND (mx
<= maxWidth
AND mx
>= maxWidth
- 2) THEN 'exit sign EXIT DO 'escape plan for mouse click top right corner of display box IF (page
- 1) * maxHeight
+ lba
>= lba
THEN page
= page
- 1:
GOSUB update
ELSEIF mx
>= 1 AND mx
<= maxWidth
AND my
= maxHeight
+ 1 THEN 'page down bar clicked IF (page
+ 1) * maxHeight
+ lba
<= uba
THEN page
= page
+ 1:
GOSUB update
ELSE ' mouse over highlighting, only if mouse has moved! IF my
- 1 <> hlite
AND (my
- 1 + page
* maxHeight
+ lba
<= uba
) THEN hlite = my - 1
lastmx = mx: lastmy = my
getArrayItemNumber& = choice
'display of array sections and controls on screen
update:
'fix hlite if it has dropped below last array item
WHILE hlite
+ page
* maxHeight
+ lba
> uba
hlite = hlite - 1
'main display of array items at page * maxHeight (lines high)
FOR row
= 0 TO maxHeight
- 1 IF hlite
= row
THEN COLOR _RGB(200, 200, 255), _RGB32(0, 0, 88) ELSE COLOR _RGB32(0, 0, 88), _RGB(200, 200, 255) index = row + page * maxHeight + lba
LOCATE locateRow
+ row
, locateColumn
'make page up and down bars to click, print PgUp / PgDn if available
'make exit sign for mouse click
LOCATE locateRow
- 1, locateColumn
+ maxWidth
- 3
'if a number selection has been started show it's build = b$
LOCATE locateRow
+ maxHeight
, locateColumn
+ maxWidth
- LEN(b$
) - 1
' These are needed only for the demo and testing of the function:
' It should be noted I had trouble using INPUT and INKEY$ after using the function being tested, ie inquiring about another test.
' Something about clearing keypresses (and enter specially).
PRINT: cp
"Go again? press y for yes, any other for no." k$ = ""
IF k$
= "y" THEN goAgain%
= -1
filecount% = 0
'PRINT filecount%, arr(filecount%)
filecount% = filecount% + 1
fLines = filecount% 'this file returns the number of lines loaded, 0 means file did not exist
t$ = "*** Mouse and Key Instructions ***"
m$ = "Mouse, mouse wheel, and arrow keys should work as expected for item selection." + n$
m$ = m$ + "Press spacebar to select a highlighted item or just click it." + n$
m$ = m$ + "Use number(s) + enter to select an array item by it's index number," + n$
m$ = m$ + "backspace will remove last number pressed, c will clear a number started." + n$
m$ = m$ + "Numbers started are shown in bottom right PgDn bar." + n$
m$ = m$ + "Enter will also select the highlighted item, if no number has been started." + n$
m$ = m$ + "Home starts you at lowest array index, End highlights then highest index." + n$
m$ = m$ + "Use PgUp and PgDn keys to flip through pages of array items." + n$ + n$
m$ = m$ + "Escape (returns -1719) to allow a Cancel function and signal no slection."
mBox m$, t$
'title$ limit is 57 chars, all lines are 58 chars max
' version bak 2018-09-07_3P
'title$ limit is 57 chars, all lines are 58 chars max
' version bak 2018-09-07_3P < fixing this version as it does not work well in another app
'using const WW for Window Width, WH for Window Height
'screen snapshot
'setup t$() to store strings with ti as index, linit 58 chars per line max, b$ is for build
REDIM t$
(0): ti
= 0: limit
= 58: b$
= "" 'are there any new line signals, CR, LF or both? take CRLF or LFCR as one break but dbl LF or CR means blank line
tail$ = "": ff = 0
FOR j
= LEN(b$
) TO 1 STEP -1 'backup until find a space, save the tail end for next line ff = 1 'found space flag
tail$ = d$ + tail$ 'the tail grows!
b$ = b$ + c$ 'just keep building the line
t$(ti) = b$
bxH = ti + 3: bxW = limit + 2
'draw message box
'now for the action
'convert to pixels the top left corner of box at moment
bxW = bxW * 8: bxH = bxH * 16
tlx = (WW - bxW) / 2: tly = (WH - bxH) / 2
lastx = tlx: lasty = tly
'now allow user to move it around or just read it
IF mx
>= tlx
AND mx
<= tlx
+ bxW
AND my
>= tly
AND my
<= tly
+ 16 THEN 'mouse down on title bar grabx = mx - tlx: graby = my - tly
IF mx
- grabx
>= 0 AND mx
- grabx
<= WW
- bxW
AND my
- graby
>= 0 AND my
- graby
<= WH
- bxH
THEN 'attempt to speed up with less updates
IF ((lastx
- (mx
- grabx
)) ^ 2 + (lasty
- (my
- graby
)) ^ 2) ^ .5 > 10 THEN tlx = mx - grabx: tly = my - graby
lastx = tlx: lasty = tly