margin.l = 4
margin.t = 2
' DEMO sample text block.-----------------------------------------------------
x$(1) = "qwerty"
x$(2) = "asdfg"
x$(3) = "zxcvbn"
x$(4) = "abcd"
x$(5) = "123456"
x$(6) = "qwerty"
x$(7) = "987654"
x$(8) = "mnbvcx"
x$(9) = "asdf"
x$(10) = "poiuyt"
x$(i + h * 10) = x$(i + h * 10) + x$(i + h * 10) + x$(i + h * 10) + x$(i + h * 10) + x$(i) + x$(i + h * 10) + x$(i + h * 10)
'======================== Needed variables for routine and demo.
dwidth
= LEN(x$
(1)) + 1 ' Must always be +1 so mod formula never results in a 0 as in a string the size of dwidth mod dwidth.page.h = 10 ' Page height.
noe% = 20 ' Number of text line entries.
'========================
c1% = 7: c2% = 0: h1% = 15: h2% = 1 ' Normal text and highlighted text colors.
h%
= 0:
GOSUB displaytext
LOCATE margin.t
+ 1, margin.l
+ 1, 1, 7, 30 ' / Demo sample text block.--------------------------------------------------
' ===========================================================================
' Highlight Routine----------------------------------------------------------
row
= CSRLIN - margin.t: col
= POS(0) - margin.l
DEF SEG = 0 ' Look for Shift key press.
null$
= INKEY$: b$
= "" ' Clear key buffer when left mouse button is held down and lock out keys.
IF LEN(b$
) OR mhl%
THEN ' Key press or mouse clicked / held down (mhl%).
hlkey% = -1 ' Left mouse button down or Shift key down with a highlighting key pressed.
mark% = (row - 1) * dwidth + col: markrow% = row: markcol% = col ' Highlight cursor position markers.
IF cutmrow%
= 0 THEN cutmrow%
= markrow%
+ scr: cutmcol%
= markcol%
' Disable highlight key when active and a key is pressed without Shift key held or
IF hlkey%
AND LEN(b$
) > 0 THEN ' Highlighted text and key press. Note: Left mouse click to clear text is in another routine. hlkey%
= 0: cutmrow%
= 0:
GOSUB clearmarkers
' Disable highlight and zero highlighting cursor marker. h%
= 0:
GOSUB displaytext
' Use this or some other routine to clear the highlighted text off the screen.
IF mhl%
THEN ' Left mouse button click or held highlighting. IF mhl1row%
= oldmhl1row%
THEN ' Lateral movement. Take no action here. Action is taken in the select case mov routine.
ELSE ' Simulate an up or down arrow key routines for mouse highlighting when changing rows. IF mhl1row%
< oldmhl1row%
THEN b$
= CHR$(0) + "H" ' Highlight upwards. IF mhl1row%
> oldmhl1row%
THEN b$
= CHR$(0) + "P" ' Highlight downwards.
IF mhl%
THEN ' Left mouse key highlighting. IF row
= mhl1row%
THEN ' Detect when the row in the loop matches the row marked to terminate the loop. SELECT CASE mhl1col%
' Now that the loop has terminated at the marked row, find the marked column. CASE 0 ' Column at loop exit is already at the marked column. CASE ELSE ' Set col variable to mouse column marker and zero out that marker along with the simulated key press. col = mhl1col%: mhl1col% = 0 ' New col position allows highlighter routine to highlight text on this row to this column.
b$ = "" ' Important. Must nullify variable to avoid case selection below.
SELECT CASE MID$(b$
, 2, 1) ' Cursor movement routine. May be replace or incorporated into outer cursor movement routines. row = row - 1
i%
= -1:
GOSUB redomarkers
row = row + 1: scr = scr - 1
h%
= 2:
GOSUB displaytext
row = row - 1
scr = scr - 1
h%
= 0:
GOSUB displaytext
row = row + 1
i%
= 1:
GOSUB redomarkers
row = row - 1
scr = scr + 1
h%
= page.h
- 1:
GOSUB displaytext
row = row + 1
scr = scr + 1
h%
= 0:
GOSUB displaytext
col = col + 1
row = row + 1: col = 1
i%
= 1:
GOSUB redomarkers
row = row - 1: scr = scr + 1
h%
= page.h
- 1:
GOSUB displaytext
row = row + 1: col = 1
scr = scr + 1: col = 1
h%
= 0:
GOSUB displaytext
LOCATE margin.t
+ row
, margin.l
+ col
col = col - 1
row = row - 1: col = dwidth - 1
i%
= -1:
GOSUB redomarkers
row = row + 1: scr = scr - 1
h%
= 2:
GOSUB displaytext
row
= row
- 1: col
= LEN(x$
(row
+ scr
)):
IF col
< dwidth
- 1 THEN col
= col
+ 1 scr = scr - 1
col
= LEN(x$
(row
+ scr
)):
IF col
< dwidth
- 1 THEN col
= col
+ 1 h%
= 0:
GOSUB displaytext
col = 1
col
= LEN(x$
(row
+ scr
)):
IF col
< dwidth
- 1 THEN col
= col
+ 1
LOCATE margin.t
+ row
, margin.l
+ col
' Note: Both row and col variables are relative and must be added to any left or top margin variables to appear in the proper row and column positions on the screen.
IF col
< dwidth
- 1 THEN col
= col
+ 1 LOCATE margin.t
+ row
, margin.l
+ col
' Note: Both row and col variables are relative and must be added to any left or top margin variables to appear in the proper row and column positions on the screen.
LOOP UNTIL mhl%
= 0 ' Exit for key press because mhl% is zero during key presses.
mw% = 0: autokey% = -1
mw% = 0
IF lb%
THEN ' Left mouse button pressed. IF mx%
> margin.l
AND mx%
<= margin.l
+ dwidth
- 1 THEN ' Cursor in-bounds. ii% = my% - margin.t
IF ii%
> page.h
THEN ii%
= page.h
col = mx% - margin.l
LOCATE margin.t
+ ii%
, margin.l
+ col
i%
= -1:
GOSUB redomarkers
row = row + 1: scr = scr - 1
h%
= 2:
GOSUB displaytext
LOCATE margin.t
+ ii%
, margin.l
+ col
col = mx% - margin.l
LOCATE margin.t
+ ii%
, margin.l
+ col
i%
= 1:
GOSUB redomarkers
row = row - 1: scr = scr + 1
h%
= page.h
- 1:
GOSUB displaytext
LOCATE margin.t
+ ii%
, margin.l
+ col
IF mhlclear%
THEN ' Check to see if highlighting should be removed. IF shift%
THEN ' Do not remove highlighted text. mhlclear% = 0
cutmrow%
= 0:
GOSUB clearmarkers
h%
= 0:
GOSUB displaytext
' Remove highlighted text.
IF shift%
OR (ii%
- 1) * dwidth
+ mx%
- margin.l
<> mhl1%
AND mhl1%
<> 0 THEN mhl% = -1 ' Left mouse key highlighting enabled when Shift key held and left mouse button click or when Shift held or not held if left mouse button is held while changing row/column (drag).
IF oldmhl1row%
= 0 THEN oldmhl1row%
= row
ELSE oldmhl1row%
= mhl1row%
mhl1row% = ii%: mhl1col% = mx% - margin.l: mhl1% = (ii% - 1) * dwidth + mx% - margin.l
IF mhl1%
<> (ii%
- 1) * dwidth
+ mx%
- margin.l
THEN mhl1% = (ii% - 1) * dwidth + mx% - margin.l
row = ii%: col = mx% - margin.l
LOCATE margin.t
+ row
, margin.l
+ col
ELSE ' Left mouse button unpressed or released. mhl1% = 0 ' Left mouse button released so zero the highlighting marker.
IF mark%
THEN mhlclear%
= -1 ' Will be triggered the next time the left mouse button is pressed to remove all highlighted text.
displaytext:
cutdrow% = row + scr: cutdcol% = col
CASE IS < cutdrow%: j%
= cutmrow%: j1%
= cutmcol%
CASE IS = cutdrow%
' Highlighting begins and ends on same row. Ex: Left and right arrow keys. j% = cutmrow%
IF cutmcol%
< cutdcol%
THEN j%
= cutmrow%: j1%
= cutmcol%
ELSE j%
= cutdrow%: j1%
= cutdcol%
' Right arrow vs left arrow directions. CASE IS > cutdrow%: j%
= cutdrow%: j1%
= cutdcol%
jtop%
= cutmrow%: jbot%
= cutdrow%:
IF jtop%
> jbot%
THEN SWAP jtop%
, jbot%
j% = 0
LOCATE margin.t
+ i%
, margin.l
+ 1 MID$(a1$
, 1) = x$
(scr
+ i%
) a2$
= x$
(scr
+ i%
): a3$
= SPACE$(dwidth
- LEN(a2$
)) x1 = c1%: x2 = c2%: x3 = c1%: x4 = c2%
IF cutmrow%
= cutdrow%
THEN ' Left/Right Highlighting on origination row. x3
= h1%: x4
= h2%: k%
= ABS(cutmcol%
- cutdcol%
) ELSE ' Left/Right/Up/Down Highlighting from row before scrolling row. x3 = h1%: x4 = h2%
j1% = 1: k% = col - 1
' Do nothing. Arrow left and up do not require any change in parameters here.
IF i%
+ scr
> jtop%
AND i%
+ scr
< jbot%
THEN j1% = 1: x3 = h1%: x4 = h2%
x3 = h1%: x4 = h2%
IF cutdrow%
< cutmrow%
THEN j1%
= 1: k%
= cutmcol%
- 1
highlighter:
COLOR h1%
, h2%
' Initiate highlight color. IF o%
= 0 THEN o%
= mark%
ELSE o%
= d%
' o% is the start marker or last marker. If zero, it is the same as the start marker (mark%) but if the highlighting process is ongoing, it is the same as the last highlighting marker (d%). d% = (row - 1) * dwidth + col ' Relative (margin independent) cursor destination marker.
o1%
= (o%
- (o%
MOD dwidth
)) / dwidth
+ 1 ' Relative (margin independent) cursor row origin.o2%
= o%
MOD dwidth
' Relative (margin independent) cursor column origin.d1% = row ' Relative (margin independent) cursor row destination. Note: row is also relative (margin independent).
d2% = col ' Relative (margin independent) cursor column destination. Note: col is also relative (margin independent).
IF d%
>= o%
THEN mov%
= 1 ELSE mov%
= -1 ' Difference between origin and destination markers determine if the movement is positive (right, down) or negative (left, up).
CASE 1 ' End, right, or down x1%
= o1%: x2%
= o2%:
LOCATE margin.t
+ x1%
, margin.l
+ x2%
' Highlighting on line above, from origin row.
IF x1%
= markrow%
AND o1%
= d1%
AND mhl%
THEN ' Mouse only when the origin row is the same as the original marker row and the destination row is the same as the origin row. This is right movement within the same line of text. ELSEIF x1%
= markrow%
AND ABS(o%
- d%
) > 1 AND mhl%
= 0 THEN ' End key press. Exclude mouse here, as down mouse highlighting is done in a conditional statement, below. GOSUB getcolor:
PRINT MID$(x$
(x1%
+ scr
), x2%
, ABS(o2%
- markcol%
));
' Print from origin column to initial marker column. GOSUB getcolor:
PRINT MID$(x$
(x1%
+ scr
), x2%
+ ABS(o2%
- markcol%
));
' Print from initial marker column to destination column. ELSE ' Right arrow key, down arrow key or mouse moving down. GOSUB getcolor:
PRINT MID$(x$
(x1%
+ scr
), x2%
, ABS(o1%
- d1%
) * dwidth
+ ABS(o2%
- d2%
));
' Trick method to determine length of highlighting.
IF o1%
- d1%
THEN ' Highlighting on current row after row above is finished. x1%
= row: x2%
= col:
LOCATE margin.t
+ x1%
, margin.l
+ 1 IF x1%
= markrow%
AND ABS(o%
- d%
) > 1 THEN ' Highlight the destination row if downward move is made after an upward highlight has been made. IF d2%
< markcol%
THEN i%
= d2%
ELSE i%
= markcol%
' Set i% to the furthest column. GOSUB getcolor:
PRINT MID$(x$
(x1%
+ scr
), 1, i%
- 1);
' unhighlight to the furthest column. ELSE ' Arrow down or mouse downward, either with original marker row on same line. GOSUB getcolor:
PRINT MID$(x$
(x1%
+ scr
), 1, d2%
- 1);
' Highlight from first column on current row to destination on current row.
CASE -1 ' Home, up, or Left. x1%
= row: x2%
= col:
LOCATE margin.t
+ x1%
, margin.l
+ x2%
IF x1%
= markrow%
AND o1%
= d1%
AND mhl%
THEN ' Mouse only when the origin row is the same as the original marker row and the destination row is the same as the origin row. This is left movement within the same line of text. ELSEIF x1%
= markrow%
AND ABS(o%
- d%
) > 1 THEN ' Mouse or arrow back up to highlighted line above. GOSUB getcolor:
PRINT MID$(x$
(x1%
+ scr
), x2%
, ABS(d2%
- markcol%
));
' Highlight from cursor to previously highlighted text to the right, if any. GOSUB getcolor:
PRINT MID$(x$
(x1%
+ scr
), x2%
+ ABS(d2%
- markcol%
));
' Unhighlight previous text to the right. ELSE ' Arrow left, arrow up or mouse upwards, either without any highlighting above. GOSUB getcolor:
PRINT MID$(x$
(x1%
+ scr
), x2%
, ABS(o1%
- d1%
) * dwidth
+ ABS(o2%
- d2%
));
' Trick method to determine length of highlighting.
IF o1%
<> d1%
AND d2%
<= o2%
THEN ' Bottom line with arrow up or mouse upwards. Note: Mouse initially moves straight up, and moves laterally in another pass. This is why o2% always equals d2% as with an arrow up move. x1%
= o1%: x2%
= o2%:
LOCATE margin.t
+ x1%
, margin.l
+ 1 IF markcol%
>= d2%
THEN i%
= d2%
ELSE i%
= markcol%
getcolor: ' Reads the screen under the cursor and reverses the colors.
clearmarkers:
' Clear most variables used in highlighting process. Some others are non-essential to conditions.
mark% = 0: o% = 0: mhl% = 0: mhl1% = 0: mhl1col% = 0: mhl1row% = 0: oldmhl1row% = 0: mhlclear% = 0
cutdrow% = 0
debugger:
PRINT "mark% = "; mark%; markrow%; markcol%;
" " PRINT "origin ="; o%; o1%; o2%;
" " PRINT "dest = "; d%; d1%; d2%
PRINT "moving "; mov$;
" " PRINT "yy% col"; yy%; xx%;
" " PRINT "yy% * col";
(yy%
- 1) * dwidth
+ col;
" " PRINT "cutmrow% "; cutmrow%; cutmcol%;
" " PRINT "cutdrow% "; cutdrow%; cutdcol%;
" " PRINT "i j scr "; i%; j%; scr;
" " PRINT "j1% k% "; j1%; k%;
" " PRINT "row col "; row; col;
" " PRINT "mhl1% "; mhl1%;
" " PRINT "oldmhl1 "; oldmhl1row%;
" " PRINT "mhl1row% "; mhl1row%;
" " PRINT "mhl1col% "; mhl1col%;
" "
redomarkers:
mark% = mark% - i% * dwidth
markrow% = markrow% - i%
d%
= (CSRLIN - margin.t
- 1 - i%
) * dwidth
+ POS(0) - margin.l
mhl1% = mhl1% - i% * dwidth
mhl1row% = mhl1row% - i%
oldmhl1row% = oldmhl1row% - i%