Author Topic: Another line editor  (Read 4280 times)

0 Members and 1 Guest are viewing this topic.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Another line editor
« on: April 13, 2021, 01:25:32 am »
This one uses MEM. Easy control of the cursor with MEM. Code is easier to understand

will add BACKSPACE   later

Code: QB64: [Select]
  1. DIM Handle AS LONG
  2. DIM curWidth AS INTEGER
  3. DIM curPosX AS INTEGER
  4. DIM curOffset AS _UNSIGNED LONG
  5. DIM CharWidth AS INTEGER
  6. DIM printPos AS INTEGER
  7.  
  8. Text = "Some text to edit"
  9.  
  10. Handle = _NEWIMAGE(400, 400, 32)
  11. SCREEN Handle
  12. M = _MEMIMAGE(Handle)
  13.  
  14. 'DIM a AS _UNSIGNED _BYTE
  15.  
  16. PRINT "Move cursor with left / right arrow"
  17.  
  18. PRINT "Insert, Append, Delete characters"
  19. PRINT Text
  20.  
  21. curWidth = (_PRINTWIDTH(CHR$(a)) - 2)
  22. CharWidth = _PRINTWIDTH(CHR$(a))
  23.  
  24. curPosX = _PRINTWIDTH(Text)
  25. curOffset = _WIDTH * (15 + 64) + curPosX
  26.  
  27.  
  28. GOSUB curon
  29.  
  30.  
  31.     DO
  32.         _LIMIT 100
  33.         kh = _KEYHIT
  34.         IF kh > 0 THEN EXIT DO
  35.     LOOP
  36.  
  37.     'PRINT kh
  38.     IF kh > 31 AND kh < 127 AND LEN(Text) < printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'APPEND
  39.         Text = Text + CHR$(kh)
  40.         curPosX = _PRINTWIDTH(Text)
  41.         curOffset = _WIDTH * (15 + 64) + curPosX
  42.         LOCATE 5, 1: PRINT Text
  43.         GOSUB curon
  44.     END IF
  45.  
  46.     IF kh > 31 AND kh < 127 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'INSERT
  47.         Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos, LEN(Text) - printPos + 1)
  48.         curPosX = curPosX + CharWidth
  49.         curOffset = _WIDTH * (15 + 64) + curPosX '
  50.         LOCATE 5, 1: PRINT Text
  51.         GOSUB curon
  52.     END IF
  53.  
  54.     IF kh = 21248 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) <= _WIDTH THEN 'DELETE
  55.         Text = LEFT$(Text, printPos - 1) + MID$(Text, printPos + 1, LEN(Text) - printPos + 1)
  56.         curOffset = _WIDTH * (15 + 64) + curPosX '
  57.         LOCATE 5, 1: PRINT Text + " "
  58.         GOSUB curon
  59.     END IF
  60.  
  61.     IF kh = 27 THEN END
  62.  
  63.     IF kh = 19200 AND curPosX > 0 THEN 'left arrow
  64.         GOSUB curoff
  65.         curPosX = curPosX - CharWidth
  66.         curOffset = _WIDTH * (15 + 64) + curPosX
  67.         GOSUB curon
  68.     END IF
  69.  
  70.     IF kh = 19712 AND curPosX < _PRINTWIDTH(Text) AND _PRINTWIDTH(Text) < _WIDTH THEN 'RIGHT ARROW
  71.         GOSUB curoff
  72.         curPosX = curPosX + CharWidth
  73.         curOffset = _WIDTH * (15 + 64) + curPosX
  74.         GOSUB curon
  75.     END IF
  76.  
  77. '****
  78. curon:
  79. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  80.     b = 255
  81.     _MEMPUT M, M.OFFSET + T, b
  82.  
  83.     g = 255
  84.     _MEMPUT M, M.OFFSET + T + 1, g
  85.  
  86.     r = 0
  87.     _MEMPUT M, M.OFFSET + T + 2, r
  88. printPos = curPosX / CharWidth + 1
  89.  
  90. '****
  91. curoff:
  92.  
  93. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  94.  
  95.     b = 0
  96.     _MEMPUT M, M.OFFSET + T, b
  97.  
  98.     g = 0
  99.     _MEMPUT M, M.OFFSET + T + 1, g
  100.  
  101.     r = 0
  102.     _MEMPUT M, M.OFFSET + T + 2, r
  103. printPos = curPosX / CharWidth + 1
  104.  


Added backspace

Code: QB64: [Select]
  1. DIM Handle AS LONG
  2. DIM curWidth AS INTEGER
  3. DIM curPosX AS INTEGER
  4. DIM curOffset AS _UNSIGNED LONG
  5. DIM CharWidth AS INTEGER
  6. DIM printPos AS INTEGER
  7.  
  8. Text = "Some text to edit"
  9.  
  10. Handle = _NEWIMAGE(400, 400, 32)
  11. SCREEN Handle
  12. M = _MEMIMAGE(Handle)
  13.  
  14. 'DIM a AS _UNSIGNED _BYTE
  15.  
  16. PRINT "Move cursor with left / right arrow"
  17.  
  18. PRINT "Insert, Append, Delete, Backspace characters"
  19. PRINT Text
  20.  
  21. curWidth = (_PRINTWIDTH(CHR$(a)) - 2)
  22. CharWidth = _PRINTWIDTH(CHR$(a))
  23.  
  24. curPosX = _PRINTWIDTH(Text)
  25. curOffset = _WIDTH * (15 + 64) + curPosX
  26.  
  27.  
  28. GOSUB curon
  29.  
  30.  
  31.     DO
  32.         _LIMIT 100
  33.         kh = _KEYHIT
  34.         IF kh > 0 THEN EXIT DO
  35.     LOOP
  36.  
  37.     IF kh = 27 THEN END
  38.  
  39.     IF kh = 8 AND curPosX > 0 THEN 'BACKSPACE
  40.         GOSUB curoff
  41.         Text = LEFT$(Text, printPos - 2) + MID$(Text, printPos, LEN(Text) - (printPos - 1))
  42.         curPosX = curPosX - CharWidth
  43.         curOffset = _WIDTH * (15 + 64) + curPosX
  44.         LOCATE 5, 1: PRINT Text + " "
  45.         GOSUB curon
  46.     END IF
  47.  
  48.     IF kh > 31 AND kh < 127 AND LEN(Text) < printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'APPEND
  49.         Text = Text + CHR$(kh)
  50.         curPosX = _PRINTWIDTH(Text)
  51.         curOffset = _WIDTH * (15 + 64) + curPosX
  52.         LOCATE 5, 1: PRINT Text
  53.         GOSUB curon
  54.     END IF
  55.  
  56.     IF kh > 31 AND kh < 127 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'INSERT
  57.         Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos, LEN(Text) - printPos + 1)
  58.         curPosX = curPosX + CharWidth
  59.         curOffset = _WIDTH * (15 + 64) + curPosX '
  60.         LOCATE 5, 1: PRINT Text
  61.         GOSUB curon
  62.     END IF
  63.  
  64.     IF kh = 21248 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) <= _WIDTH THEN 'DELETE
  65.         Text = LEFT$(Text, printPos - 1) + MID$(Text, printPos + 1, LEN(Text) - printPos + 1)
  66.         curOffset = _WIDTH * (15 + 64) + curPosX '
  67.         LOCATE 5, 1: PRINT Text + " "
  68.         GOSUB curon
  69.     END IF
  70.  
  71.     IF kh = 19200 AND curPosX > 0 THEN 'left arrow
  72.         GOSUB curoff
  73.         curPosX = curPosX - CharWidth
  74.         curOffset = _WIDTH * (15 + 64) + curPosX
  75.         GOSUB curon
  76.     END IF
  77.  
  78.     IF kh = 19712 AND curPosX < _PRINTWIDTH(Text) AND _PRINTWIDTH(Text) < _WIDTH THEN 'RIGHT ARROW
  79.         GOSUB curoff
  80.         curPosX = curPosX + CharWidth
  81.         curOffset = _WIDTH * (15 + 64) + curPosX
  82.         GOSUB curon
  83.     END IF
  84.  
  85. '****
  86. curon:
  87. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  88.     b = 255
  89.     _MEMPUT M, M.OFFSET + T, b
  90.  
  91.     g = 255
  92.     _MEMPUT M, M.OFFSET + T + 1, g
  93.  
  94.     r = 0
  95.     _MEMPUT M, M.OFFSET + T + 2, r
  96. printPos = curPosX / CharWidth + 1
  97.  
  98. '****
  99. curoff:
  100.  
  101. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  102.  
  103.     b = 0
  104.     _MEMPUT M, M.OFFSET + T, b
  105.  
  106.     g = 0
  107.     _MEMPUT M, M.OFFSET + T + 1, g
  108.  
  109.     r = 0
  110.     _MEMPUT M, M.OFFSET + T + 2, r
  111. printPos = curPosX / CharWidth + 1
  112.  

« Last Edit: April 13, 2021, 03:03:03 am by NOVARSEG »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Another line editor
« Reply #1 on: April 14, 2021, 12:05:39 am »
A blinking cursor would be nice.  Need ideas

A useful tip  from https://en.wikipedia.org/wiki/Cursor_(user_interface)#Text_cursor

Quote
The blinking of the text cursor is usually temporarily suspended when it is being moved; otherwise, the cursor may change position when it is not visible, making its location difficult to follow.


Blinking cursor added

Code: QB64: [Select]
  1. DIM Handle AS LONG
  2. DIM curWidth AS INTEGER
  3. DIM curPosX AS INTEGER
  4. DIM curOffset AS _UNSIGNED LONG
  5. DIM CharWidth AS INTEGER
  6. DIM printPos AS INTEGER
  7.  
  8. Text = "Some text to edit"
  9.  
  10. Handle = _NEWIMAGE(400, 400, 32)
  11. SCREEN Handle
  12. M = _MEMIMAGE(Handle)
  13.  
  14.  
  15.  
  16. PRINT "Move cursor with left / right arrow"
  17.  
  18. PRINT "Insert, Append, Delete, Backspace characters"
  19. PRINT Text
  20.  
  21. curWidth = (_PRINTWIDTH(CHR$(a)) - 2)
  22. CharWidth = _PRINTWIDTH(CHR$(a))
  23.  
  24. curPosX = _PRINTWIDTH(Text)
  25. curOffset = _WIDTH * (15 + 64) + curPosX
  26.  
  27.  
  28. GOSUB curon
  29. F = 0
  30.     T1 = TIMER
  31.     DO
  32.  
  33.         _LIMIT 100
  34.  
  35.         IF TIMER - T1 > .3 AND F = 0 THEN
  36.             F = 1
  37.             T1 = TIMER
  38.             GOSUB curoff
  39.         END IF
  40.  
  41.         IF TIMER - T1 > .3 AND F = 1 THEN
  42.             F = 0
  43.             T1 = TIMER
  44.             GOSUB curon
  45.         END IF
  46.  
  47.         kh = _KEYHIT
  48.         IF kh > 0 THEN GOSUB curon: EXIT DO
  49.     LOOP
  50.  
  51.  
  52.     IF kh = 27 THEN END
  53.  
  54.     IF kh = 8 AND curPosX > 0 THEN 'BACKSPACE
  55.         GOSUB curoff
  56.         Text = LEFT$(Text, printPos - 2) + MID$(Text, printPos, LEN(Text) - (printPos - 1))
  57.         curPosX = curPosX - CharWidth
  58.         curOffset = _WIDTH * (15 + 64) + curPosX
  59.         LOCATE 5, 1: PRINT Text + " "
  60.         GOSUB curon
  61.     END IF
  62.  
  63.     IF kh > 31 AND kh < 127 AND LEN(Text) < printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'APPEND
  64.         Text = Text + CHR$(kh)
  65.         curPosX = _PRINTWIDTH(Text)
  66.         curOffset = _WIDTH * (15 + 64) + curPosX
  67.         LOCATE 5, 1: PRINT Text
  68.         GOSUB curon
  69.     END IF
  70.  
  71.     IF kh > 31 AND kh < 127 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'INSERT
  72.         Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos, LEN(Text) - printPos + 1)
  73.         curPosX = curPosX + CharWidth
  74.         curOffset = _WIDTH * (15 + 64) + curPosX '
  75.         LOCATE 5, 1: PRINT Text
  76.         GOSUB curon
  77.     END IF
  78.  
  79.     IF kh = 21248 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) <= _WIDTH THEN 'DELETE
  80.         Text = LEFT$(Text, printPos - 1) + MID$(Text, printPos + 1, LEN(Text) - printPos + 1)
  81.         curOffset = _WIDTH * (15 + 64) + curPosX '
  82.         LOCATE 5, 1: PRINT Text + " "
  83.         GOSUB curon
  84.     END IF
  85.  
  86.     IF kh = 19200 AND curPosX > 0 THEN 'LEFT ARROW
  87.         GOSUB curoff
  88.         curPosX = curPosX - CharWidth
  89.         curOffset = _WIDTH * (15 + 64) + curPosX
  90.         GOSUB curon
  91.     END IF
  92.  
  93.     IF kh = 19712 AND curPosX < _PRINTWIDTH(Text) AND _PRINTWIDTH(Text) < _WIDTH THEN 'RIGHT ARROW
  94.         GOSUB curoff
  95.         curPosX = curPosX + CharWidth
  96.         curOffset = _WIDTH * (15 + 64) + curPosX
  97.         GOSUB curon
  98.     END IF
  99.  
  100. '****
  101. curon:
  102. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  103.     b = 255
  104.     _MEMPUT M, M.OFFSET + T, b
  105.  
  106.     g = 255
  107.     _MEMPUT M, M.OFFSET + T + 1, g
  108.  
  109.     r = 0
  110.     _MEMPUT M, M.OFFSET + T + 2, r
  111. printPos = curPosX / CharWidth + 1
  112.  
  113. '****
  114. curoff:
  115.  
  116. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  117.  
  118.     b = 0
  119.     _MEMPUT M, M.OFFSET + T, b
  120.  
  121.     g = 0
  122.     _MEMPUT M, M.OFFSET + T + 1, g
  123.  
  124.     r = 0
  125.     _MEMPUT M, M.OFFSET + T + 2, r
  126. printPos = curPosX / CharWidth + 1
  127.  




« Last Edit: April 14, 2021, 01:14:00 am by NOVARSEG »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Another line editor
« Reply #2 on: April 14, 2021, 03:17:45 am »
Move text with up / down arrows


Code: QB64: [Select]
  1. DIM Handle AS LONG
  2. DIM curWidth AS INTEGER
  3. DIM curPosX AS INTEGER
  4. DIM curOffset AS _UNSIGNED LONG
  5. DIM CharWidth AS INTEGER
  6. DIM printPos AS INTEGER
  7. DIM pixelDown AS INTEGER
  8. DIM lines AS INTEGER
  9. lines = 5 * 16
  10.  
  11.  
  12. Text = "Some text to edit"
  13.  
  14. Handle = _NEWIMAGE(400, 400, 32)
  15. SCREEN Handle
  16. M = _MEMIMAGE(Handle)
  17.  
  18.  
  19.  
  20. PRINT "Move cursor with left / right arrow"
  21.  
  22. PRINT "Insert, Append, Delete, Backspace characters"
  23. PRINT "Move text with up / down arrow."
  24. PRINT Text
  25.  
  26. curWidth = (_PRINTWIDTH(CHR$(a)) - 2)
  27.  
  28. CharHeight = _FONTHEIGHT
  29. CharWidth = _PRINTWIDTH(CHR$(a))
  30.  
  31.  
  32. curPosX = _PRINTWIDTH(Text)
  33. curOffset = _WIDTH * (15 + lines) + curPosX 'initial
  34.  
  35.  
  36. GOSUB curon
  37. F = 0
  38.     T1 = TIMER
  39.     DO
  40.  
  41.         _LIMIT 100
  42.  
  43.         IF TIMER - T1 > .3 AND F = 0 THEN
  44.             F = 1
  45.             T1 = TIMER
  46.             GOSUB curoff
  47.         END IF
  48.  
  49.         IF TIMER - T1 > .3 AND F = 1 THEN
  50.             F = 0
  51.             T1 = TIMER
  52.             GOSUB curon
  53.         END IF
  54.  
  55.         kh = _KEYHIT
  56.         IF kh > 0 THEN GOSUB curon: EXIT DO
  57.     LOOP
  58.     'PRINT kh
  59.  
  60.     IF kh = 27 THEN END
  61.  
  62.     IF kh = 8 AND curPosX > 0 THEN 'BACKSPACE
  63.         GOSUB curoff
  64.         Text = LEFT$(Text, printPos - 2) + MID$(Text, printPos, LEN(Text) - (printPos - 1))
  65.         curPosX = curPosX - CharWidth
  66.         curOffset = _WIDTH * (15 + lines + pixelDown) + curPosX
  67.         _PRINTSTRING (0, lines + pixelDown), Text + " "
  68.         GOSUB curon
  69.     END IF
  70.  
  71.     IF kh > 31 AND kh < 127 AND LEN(Text) < printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'APPEND
  72.         Text = Text + CHR$(kh)
  73.         curPosX = _PRINTWIDTH(Text)
  74.         curOffset = _WIDTH * (15 + lines + pixelDown) + curPosX
  75.         _PRINTSTRING (0, lines + pixelDown), Text
  76.         GOSUB curon
  77.     END IF
  78.  
  79.     IF kh > 31 AND kh < 127 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'INSERT
  80.         Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos, LEN(Text) - printPos + 1)
  81.         curPosX = curPosX + CharWidth
  82.         curOffset = _WIDTH * (15 + lines + pixelDown) + curPosX '
  83.         _PRINTSTRING (0, lines + pixelDown), Text
  84.         GOSUB curon
  85.     END IF
  86.  
  87.     IF kh = 21248 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) <= _WIDTH THEN 'DELETE
  88.         Text = LEFT$(Text, printPos - 1) + MID$(Text, printPos + 1, LEN(Text) - printPos + 1)
  89.         curOffset = _WIDTH * (15 + lines + pixelDown) + curPosX '
  90.         _PRINTSTRING (0, lines + pixelDown), Text + " "
  91.         GOSUB curon
  92.     END IF
  93.  
  94.     IF kh = 19200 AND curPosX > 0 THEN 'LEFT ARROW
  95.         GOSUB curoff
  96.         curPosX = curPosX - CharWidth
  97.         curOffset = _WIDTH * (15 + lines + pixelDown) + curPosX
  98.         GOSUB curon
  99.     END IF
  100.  
  101.     IF kh = 19712 AND curPosX < _PRINTWIDTH(Text) AND _PRINTWIDTH(Text) < _WIDTH THEN 'RIGHT ARROW
  102.         GOSUB curoff
  103.         curPosX = curPosX + CharWidth
  104.         curOffset = _WIDTH * (15 + lines + pixelDown) + curPosX
  105.         GOSUB curon
  106.     END IF
  107.  
  108.     IF kh = 20480 AND lines + pixelDown + CharHeight < _HEIGHT THEN 'DOWN ARROW
  109.         pixelDown = pixelDown + 1
  110.         GOSUB curoff
  111.         curOffset = _WIDTH * (15 + lines + pixelDown) + curPosX
  112.         _PRINTSTRING (0, lines + pixelDown), Text
  113.         GOSUB curon
  114.     END IF
  115.  
  116.     IF kh = 18432 AND lines + pixelDown > lines THEN 'UP ARROW
  117.         pixelDown = pixelDown - 1
  118.         GOSUB curoff
  119.         curOffset = _WIDTH * (15 + lines + pixelDown) + curPosX
  120.         _PRINTSTRING (0, lines + pixelDown), Text
  121.         GOSUB curon
  122.     END IF
  123.  
  124.  
  125. '****
  126. curon:
  127. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  128.     b = 255
  129.     _MEMPUT M, M.OFFSET + T, b
  130.  
  131.     g = 255
  132.     _MEMPUT M, M.OFFSET + T + 1, g
  133.  
  134.     r = 0
  135.     _MEMPUT M, M.OFFSET + T + 2, r
  136. printPos = curPosX / CharWidth + 1
  137.  
  138. '****
  139. curoff:
  140.  
  141. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  142.  
  143.     b = 0
  144.     _MEMPUT M, M.OFFSET + T, b
  145.  
  146.     g = 0
  147.     _MEMPUT M, M.OFFSET + T + 1, g
  148.  
  149.     r = 0
  150.     _MEMPUT M, M.OFFSET + T + 2, r
  151. printPos = curPosX / CharWidth + 1
  152.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Another line editor
« Reply #3 on: April 14, 2021, 01:40:08 pm »
The use of up down arrows in code above is stupid (sorry) IMHO.

Wouldn't it be more useful to edit a string array and use up and down for access of the different lines?
That would be a step closer to a full editor.

The rest of the functions working well for my little test, could add home and end to line navigating.

Looks like cursor problem is worked out, nice.

Have you worked out one for insert (line at base of insert point) and type over (usually a full box character size) to toggle between?
« Last Edit: April 14, 2021, 02:46:09 pm by bplus »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Another line editor
« Reply #4 on: April 14, 2021, 09:20:26 pm »
@bplus
Yes it might look stupid at present.   I started out the idea trying to code a FORM FILLER but my code soon got clunky and I had to start all over again. The line editor is some basic code.  Move text one pixel at a time anywhere on the screen.   Most word processor can't move text like that. Of course programs like MS WORD have some nice features.  Im not trying to duplicate MS WORD - too complicated.

Yes I have thought about string arrays and that is what I tried in FORM FILLER.  I ran into some problems with arrays, such as, does each paragraph have it's own array?   The line editor does not have arrays at present. i want to treat any text that is on a line independent of other text on another line.   To move a paragraph does not require an array.  I could use hidden characters to link lines of text together so it would function like an array.   MS WORD has a thing called GROUP where text or images can be grouped together and then the whole group can be moved.

 Anyway that is some exotic code and I'm concentrating on the basics right now.

yep, the blinking cursor took me some time to figure out.  TIMER was the solution.

Oh INSERT does work. TYPEOVER hey I forgot that!  With a toggle ?   OK




   

« Last Edit: April 14, 2021, 09:35:00 pm by NOVARSEG »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Another line editor
« Reply #5 on: April 16, 2021, 04:00:58 am »
Added Typeover mode .

Code: QB64: [Select]
  1. DIM Handle AS LONG
  2. DIM curWidth AS INTEGER
  3. DIM curPosX AS INTEGER
  4. DIM curOffset AS _UNSIGNED LONG
  5. DIM CharWidth AS INTEGER
  6. DIM printPos AS INTEGER
  7. DIM insertKeyToggle AS _UNSIGNED _BIT
  8. DIM pixelUpDown AS INTEGER
  9. DIM lines AS INTEGER
  10. lines = 5 * 16
  11.  
  12.  
  13. Text = "Some text to edit"
  14.  
  15. Handle = _NEWIMAGE(400, 400, 32)
  16. SCREEN Handle
  17. M = _MEMIMAGE(Handle)
  18.  
  19.  
  20.  
  21. PRINT "Move cursor with left / right arrow"
  22.  
  23. PRINT "Insert / Type over, Append, Delete, Backspace"
  24.  
  25. PRINT "Move text with up / down arrow. ESC to exit"
  26. PRINT Text
  27.  
  28. curWidth = (_PRINTWIDTH(CHR$(a)) - 2)
  29.  
  30. CharHeight = _FONTHEIGHT
  31. CharWidth = _PRINTWIDTH(CHR$(a))
  32.  
  33.  
  34. curPosX = _PRINTWIDTH(Text)
  35. curOffset = _WIDTH * (15 + lines) + curPosX 'initial
  36.  
  37.  
  38. GOSUB curon
  39. F = 0
  40.  
  41.     T1 = TIMER
  42.     DO
  43.  
  44.         _LIMIT 100
  45.  
  46.         kh = _KEYHIT
  47.  
  48.         IF kh = 20992 AND insertKeyToggle = 0 AND LEN(Text) >= printPos THEN
  49.             insertKeyToggle = 1 'TYPEOVER mode
  50.             kh = 0
  51.             T1 = TIMER
  52.             GOSUB curoff
  53.             GOSUB cursolidon
  54.             F = 0
  55.         END IF
  56.  
  57.         IF kh = 20992 AND insertKeyToggle = 1 AND LEN(Text) >= printPos THEN
  58.             insertKeyToggle = 0 'INSERT MODE
  59.             kh = 0
  60.             T1 = TIMER
  61.             GOSUB cursolidoff
  62.             GOSUB curon
  63.             F = 0
  64.         END IF
  65.  
  66.         IF insertKeyToggle = 1 THEN
  67.             IF TIMER - T1 > .3 AND F = 1 THEN
  68.                 F = 0
  69.                 T1 = TIMER
  70.                 GOSUB cursolidon
  71.             END IF
  72.         END IF
  73.  
  74.         IF insertKeyToggle = 1 THEN
  75.             IF TIMER - T1 > .3 AND F = 0 THEN
  76.                 F = 1
  77.                 T1 = TIMER
  78.                 GOSUB cursolidoff
  79.             END IF
  80.         END IF
  81.  
  82.         IF insertKeyToggle = 0 THEN
  83.             IF TIMER - T1 > .3 AND F = 0 THEN
  84.                 F = 1
  85.                 T1 = TIMER
  86.                 GOSUB curoff
  87.             END IF
  88.             IF TIMER - T1 > .3 AND F = 1 THEN
  89.                 F = 0
  90.                 T1 = TIMER
  91.                 GOSUB curon
  92.             END IF
  93.         END IF
  94.  
  95.         IF kh > 0 AND insertKeyToggle = 1 THEN GOSUB cursolidon: EXIT DO
  96.         IF kh > 0 AND insertKeyToggle = 0 THEN GOSUB curon: EXIT DO
  97.     LOOP
  98.  
  99.  
  100.     ' PRINT kh
  101.  
  102.     IF kh = 27 THEN END
  103.  
  104.     IF kh = 8 AND curPosX > 0 THEN 'BACKSPACE
  105.         IF insertKeyToggle = 1 THEN GOSUB cursolidoff: insertKeyToggle = 0: GOTO LL3
  106.         GOSUB curoff
  107.         Text = LEFT$(Text, printPos - 2) + MID$(Text, printPos, LEN(Text) - (printPos - 1))
  108.         curPosX = curPosX - CharWidth
  109.         curOffset = _WIDTH * (15 + lines + pixelDown) + curPosX
  110.         _PRINTSTRING (0, lines + pixelUpDown), Text + " "
  111.         GOSUB curon
  112.         LL3:
  113.     END IF
  114.     '****
  115.  
  116.     '****
  117.     IF kh > 31 AND kh < 127 AND LEN(Text) < printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'APPEND
  118.         Text = Text + CHR$(kh)
  119.         curPosX = _PRINTWIDTH(Text)
  120.         curOffset = _WIDTH * (15 + lines + pixelDown) + curPosX
  121.         _PRINTSTRING (0, lines + pixelUpDown), Text
  122.         GOSUB curon
  123.     END IF
  124.     '****
  125.  
  126.     '****
  127.     IF kh > 31 AND kh < 127 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'INSERT TYPE OVER
  128.         IF insertKeyToggle = 0 THEN 'INSERT MODE
  129.             Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos, LEN(Text) - printPos + 1)
  130.             curPosX = curPosX + CharWidth
  131.             curOffset = _WIDTH * (15 + lines + pixelDown) + curPosX
  132.             _PRINTSTRING (0, lines + pixelUpDown), Text
  133.             GOSUB curon
  134.         END IF
  135.  
  136.         IF insertKeyToggle = 1 AND LEN(Text) = printPos THEN 'cursor is pointing to last character
  137.             insertKeyToggle = 0
  138.             GOSUB cursolidoff
  139.             Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos + 1, LEN(Text) - (printPos))
  140.             curPosX = curPosX + CharWidth
  141.             curOffset = _WIDTH * (15 + lines + pixelDown) + curPosX
  142.             _PRINTSTRING (0, lines + pixelUpDown), Text + " "
  143.             GOSUB curon
  144.         END IF
  145.  
  146.         IF insertKeyToggle = 1 AND LEN(Text) > printPos THEN 'cursor is pointing to any char except last char
  147.             GOSUB cursolidoff
  148.             Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos + 1, LEN(Text) - (printPos))
  149.             curPosX = curPosX + CharWidth
  150.             curOffset = _WIDTH * (15 + lines + pixelDown) + curPosX
  151.             _PRINTSTRING (0, lines + pixelUpDown), Text + " "
  152.             GOSUB cursolidon
  153.         END IF
  154.     END IF 'END INSERT OR TYPE OVER
  155.  
  156.     '****
  157.  
  158.     '****
  159.  
  160.     IF kh = 21248 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) <= _WIDTH THEN 'DELETE
  161.         IF insertKeyToggle = 1 THEN GOSUB cursolidoff: insertKeyToggle = 0: GOTO LL1
  162.  
  163.         Text = LEFT$(Text, printPos - 1) + MID$(Text, printPos + 1, LEN(Text) - printPos + 1)
  164.         curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX '
  165.         _PRINTSTRING (0, lines + pixelDown), Text + " "
  166.         GOSUB curon
  167.         LL1:
  168.     END IF
  169.  
  170.     '****
  171.  
  172.     '****
  173.  
  174.     IF kh = 19200 AND curPosX > 0 THEN 'LEFT ARROW
  175.  
  176.         IF insertKeyToggle = 1 AND LEN(Text) >= printPos THEN
  177.             GOSUB cursolidoff
  178.             curPosX = curPosX - CharWidth
  179.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  180.             GOSUB cursolidon
  181.         END IF
  182.  
  183.         IF insertKeyToggle = 0 THEN
  184.             GOSUB curoff
  185.             curPosX = curPosX - CharWidth
  186.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  187.             GOSUB curon
  188.         END IF
  189.  
  190.     END IF 'END LEFT ARROW
  191.  
  192.     '****
  193.  
  194.     '****
  195.     IF kh = 19712 AND curPosX < _PRINTWIDTH(Text) AND _PRINTWIDTH(Text) < _WIDTH THEN 'RIGHT ARROW
  196.  
  197.         IF insertKeyToggle = 1 AND LEN(Text) = printPos THEN
  198.             insertKeyToggle = 0
  199.             GOSUB cursolidoff
  200.             curPosX = curPosX + CharWidth
  201.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  202.             GOSUB curon
  203.             GOTO LL2
  204.         END IF
  205.  
  206.         IF insertKeyToggle = 1 AND LEN(Text) > printPos THEN
  207.             GOSUB cursolidoff
  208.             curPosX = curPosX + CharWidth
  209.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  210.             GOSUB cursolidon
  211.         END IF
  212.  
  213.         IF insertKeyToggle = 0 THEN
  214.             GOSUB curoff
  215.             curPosX = curPosX + CharWidth
  216.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  217.             GOSUB curon
  218.         END IF
  219.         LL2:
  220.  
  221.     END IF 'END RIGHT ARROW
  222.     '****
  223.  
  224.     '****
  225.     IF kh = 20480 AND lines + pixelUpDown + CharHeight < _HEIGHT THEN 'DOWN ARROW
  226.  
  227.         IF insertKeyToggle = 1 THEN
  228.             pixelUpDown = pixelUpDown + 1
  229.             GOSUB cursolidoff
  230.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  231.             _PRINTSTRING (0, lines + pixelUpDown), Text
  232.             GOSUB cursolidon
  233.         END IF
  234.  
  235.         IF insertKeyToggle = 0 THEN
  236.             pixelUpDown = pixelUpDown + 1
  237.             GOSUB curoff
  238.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  239.             _PRINTSTRING (0, lines + pixelUpDown), Text
  240.             GOSUB curon
  241.         END IF
  242.     END IF
  243.     '**** END DOWN ARROW
  244.  
  245.     '****
  246.     IF kh = 18432 AND lines + pixelUpDown > lines THEN 'UP ARROW
  247.  
  248.         IF insertKeyToggle = 1 THEN
  249.             pixelUpDown = pixelUpDown - 1
  250.             GOSUB cursolidoff
  251.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  252.             _PRINTSTRING (0, lines + pixelUpDown), Text
  253.             GOSUB cursolidon
  254.         END IF
  255.         IF insertKeyToggle = 0 THEN
  256.             pixelUpDown = pixelUpDown - 1
  257.             GOSUB curoff
  258.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  259.             _PRINTSTRING (0, lines + pixelUpDown), Text
  260.             GOSUB curon
  261.         END IF
  262.     END IF
  263.     '**** END UP ARROW
  264.  
  265. '****
  266.  
  267. '****
  268. cursolidon: 'Cursor for TYPE OVER mode  - ON
  269. FOR y = 0 TO CharHeight - 1
  270.     newoffset = curOffset - y * _WIDTH
  271.  
  272.     FOR T = newoffset * 4 TO newoffset * 4 + curWidth * 4 STEP 4
  273.  
  274.         b = _MEMGET(M, M.OFFSET + T, _UNSIGNED _BYTE)
  275.         b = b XOR 255
  276.         _MEMPUT M, M.OFFSET + T, b
  277.  
  278.         g = _MEMGET(M, M.OFFSET + T + 1, _UNSIGNED _BYTE)
  279.         g = g XOR 255
  280.         _MEMPUT M, M.OFFSET + T + 1, g
  281.  
  282.         r = _MEMGET(M, M.OFFSET + T + 2, _UNSIGNED _BYTE)
  283.         r = r XOR 0
  284.         _MEMPUT M, M.OFFSET + T + 2, r
  285.     NEXT T
  286.  
  287. printPos = curPosX / CharWidth + 1
  288. '****
  289. cursolidoff: 'Cursor for TYPE OVER mode - OFF
  290. FOR y = 0 TO CharHeight
  291.     newoffset = curOffset - y * _WIDTH
  292.  
  293.     FOR T = newoffset * 4 TO newoffset * 4 + curWidth * 4 STEP 4
  294.         b = _MEMGET(M, M.OFFSET + T, _UNSIGNED _BYTE)
  295.         g = _MEMGET(M, M.OFFSET + T + 1, _UNSIGNED _BYTE)
  296.         r = _MEMGET(M, M.OFFSET + T + 2, _UNSIGNED _BYTE)
  297.         IF b = 255 AND g = 255 AND r = 0 THEN b = 0: g = 0: r = 0
  298.         IF b = 0 AND g = 0 AND r = 255 THEN b = 255: g = 255: r = 255
  299.         _MEMPUT M, M.OFFSET + T, b
  300.         _MEMPUT M, M.OFFSET + T + 1, g
  301.         _MEMPUT M, M.OFFSET + T + 2, r
  302.     NEXT T
  303. printPos = curPosX / CharWidth + 1
  304. '****
  305.  
  306. curon:
  307. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  308.     b = 255
  309.     _MEMPUT M, M.OFFSET + T, b
  310.  
  311.     g = 255
  312.     _MEMPUT M, M.OFFSET + T + 1, g
  313.  
  314.     r = 0
  315.     _MEMPUT M, M.OFFSET + T + 2, r
  316. printPos = curPosX / CharWidth + 1
  317.  
  318. '****
  319. curoff:
  320.  
  321. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  322.  
  323.     b = 0
  324.     _MEMPUT M, M.OFFSET + T, b
  325.  
  326.     g = 0
  327.     _MEMPUT M, M.OFFSET + T + 1, g
  328.  
  329.     r = 0
  330.     _MEMPUT M, M.OFFSET + T + 2, r
  331. printPos = curPosX / CharWidth + 1
  332.  

« Last Edit: April 16, 2021, 04:14:36 am by NOVARSEG »

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: Another line editor
« Reply #6 on: April 16, 2021, 05:56:02 am »
It's a one line text editor to replace luxuriously the normal inputbox command, but with only a maximum of 60 characters ???
Don't understand what is this for...
Why not yes ?

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Another line editor
« Reply #7 on: April 16, 2021, 04:41:53 pm »
Quote
It's a one line text editor to replace luxuriously the normal inputbox command, but with only a maximum of 60 characters ???
Don't understand what is this for...

euklides

Will add more lines later.  Trying to link single lines of text together to form paragraphs without arrays.   The original idea was to make  something like a PDF filler.   Basically I need really good control of text on the screen. Also multiple pages of text are needed with scrolling.

I'm looking for ideas on what kind of features would be most useful.  Maybe I will get GROUP working.  It's quite a challenge moving a paragraph without arrays?


bug fix. some variables were not correct

Code: QB64: [Select]
  1. DIM Handle AS LONG
  2. DIM curWidth AS INTEGER
  3. DIM curPosX AS INTEGER
  4. DIM curOffset AS _UNSIGNED LONG
  5. DIM CharWidth AS INTEGER
  6. DIM printPos AS INTEGER
  7. DIM insertKeyToggle AS _UNSIGNED _BIT
  8. DIM pixelUpDown AS INTEGER
  9. DIM lines AS INTEGER
  10. lines = 5 * 16
  11.  
  12.  
  13. Text = "Some text to edit"
  14.  
  15. Handle = _NEWIMAGE(400, 400, 32)
  16. SCREEN Handle
  17. M = _MEMIMAGE(Handle)
  18.  
  19.  
  20.  
  21. PRINT "Move cursor with left / right arrow"
  22.  
  23. PRINT "Insert / Type over, Append, Delete, Backspace"
  24.  
  25. PRINT "Move text with up / down arrow. ESC to exit"
  26. PRINT Text
  27.  
  28. curWidth = (_PRINTWIDTH(CHR$(a)) - 2)
  29.  
  30. CharHeight = _FONTHEIGHT
  31. CharWidth = _PRINTWIDTH(CHR$(a))
  32.  
  33.  
  34. curPosX = _PRINTWIDTH(Text)
  35. curOffset = _WIDTH * (15 + lines) + curPosX 'initial
  36.  
  37.  
  38. GOSUB curon
  39. F = 0
  40.  
  41.     T1 = TIMER
  42.     DO
  43.  
  44.         _LIMIT 100
  45.  
  46.         kh = _KEYHIT
  47.  
  48.         IF kh = 20992 AND insertKeyToggle = 0 AND LEN(Text) >= printPos THEN
  49.             insertKeyToggle = 1 'TYPEOVER mode
  50.             kh = 0
  51.             T1 = TIMER
  52.             GOSUB curoff
  53.             GOSUB cursolidon
  54.             F = 0
  55.         END IF
  56.  
  57.         IF kh = 20992 AND insertKeyToggle = 1 AND LEN(Text) >= printPos THEN
  58.             insertKeyToggle = 0 'INSERT MODE
  59.             kh = 0
  60.             T1 = TIMER
  61.             GOSUB cursolidoff
  62.             GOSUB curon
  63.             F = 0
  64.         END IF
  65.  
  66.         IF insertKeyToggle = 1 THEN
  67.             IF TIMER - T1 > .3 AND F = 1 THEN
  68.                 F = 0
  69.                 T1 = TIMER
  70.                 GOSUB cursolidon
  71.             END IF
  72.         END IF
  73.  
  74.         IF insertKeyToggle = 1 THEN
  75.             IF TIMER - T1 > .3 AND F = 0 THEN
  76.                 F = 1
  77.                 T1 = TIMER
  78.                 GOSUB cursolidoff
  79.             END IF
  80.         END IF
  81.  
  82.         IF insertKeyToggle = 0 THEN
  83.             IF TIMER - T1 > .3 AND F = 0 THEN
  84.                 F = 1
  85.                 T1 = TIMER
  86.                 GOSUB curoff
  87.             END IF
  88.             IF TIMER - T1 > .3 AND F = 1 THEN
  89.                 F = 0
  90.                 T1 = TIMER
  91.                 GOSUB curon
  92.             END IF
  93.         END IF
  94.  
  95.         IF kh > 0 AND insertKeyToggle = 1 THEN GOSUB cursolidon: EXIT DO
  96.         IF kh > 0 AND insertKeyToggle = 0 THEN GOSUB curon: EXIT DO
  97.     LOOP
  98.  
  99.  
  100.     ' PRINT kh
  101.  
  102.     IF kh = 27 THEN END
  103.  
  104.     IF kh = 8 AND curPosX > 0 THEN 'BACKSPACE
  105.         IF insertKeyToggle = 1 THEN GOSUB cursolidoff: insertKeyToggle = 0: GOTO LL3
  106.         GOSUB curoff
  107.         Text = LEFT$(Text, printPos - 2) + MID$(Text, printPos, LEN(Text) - (printPos - 1))
  108.         curPosX = curPosX - CharWidth
  109.         curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  110.         _PRINTSTRING (0, lines + pixelUpDown), Text + " "
  111.         GOSUB curon
  112.         LL3:
  113.     END IF
  114.     '****
  115.  
  116.     '****
  117.     IF kh > 31 AND kh < 127 AND LEN(Text) < printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'APPEND
  118.         Text = Text + CHR$(kh)
  119.         curPosX = _PRINTWIDTH(Text)
  120.         curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  121.         _PRINTSTRING (0, lines + pixelUpDown), Text
  122.         GOSUB curon
  123.     END IF
  124.     '****
  125.  
  126.     '****
  127.     IF kh > 31 AND kh < 127 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'INSERT TYPE OVER
  128.         IF insertKeyToggle = 0 THEN 'INSERT MODE
  129.             Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos, LEN(Text) - printPos + 1)
  130.             curPosX = curPosX + CharWidth
  131.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  132.             _PRINTSTRING (0, lines + pixelUpDown), Text
  133.             GOSUB curon
  134.         END IF
  135.  
  136.         IF insertKeyToggle = 1 AND LEN(Text) = printPos THEN 'cursor is pointing to last character
  137.             insertKeyToggle = 0
  138.             GOSUB cursolidoff
  139.             Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos + 1, LEN(Text) - (printPos))
  140.             curPosX = curPosX + CharWidth
  141.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  142.             _PRINTSTRING (0, lines + pixelUpDown), Text + " "
  143.             GOSUB curon
  144.         END IF
  145.  
  146.         IF insertKeyToggle = 1 AND LEN(Text) > printPos THEN 'cursor is pointing to any char except last char
  147.             GOSUB cursolidoff
  148.             Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos + 1, LEN(Text) - (printPos))
  149.             curPosX = curPosX + CharWidth
  150.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  151.             _PRINTSTRING (0, lines + pixelUpDown), Text + " "
  152.             GOSUB cursolidon
  153.         END IF
  154.     END IF 'END INSERT OR TYPE OVER
  155.  
  156.     '****
  157.  
  158.     '****
  159.  
  160.     IF kh = 21248 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) <= _WIDTH THEN 'DELETE
  161.         IF insertKeyToggle = 1 THEN GOSUB cursolidoff: insertKeyToggle = 0: GOTO LL1
  162.  
  163.         Text = LEFT$(Text, printPos - 1) + MID$(Text, printPos + 1, LEN(Text) - printPos + 1)
  164.         curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX '
  165.         _PRINTSTRING (0, lines + pixelUpDown), Text + " "
  166.         GOSUB curon
  167.         LL1:
  168.     END IF
  169.  
  170.     '****
  171.  
  172.     '****
  173.  
  174.     IF kh = 19200 AND curPosX > 0 THEN 'LEFT ARROW
  175.  
  176.         IF insertKeyToggle = 1 AND LEN(Text) >= printPos THEN
  177.             GOSUB cursolidoff
  178.             curPosX = curPosX - CharWidth
  179.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  180.             GOSUB cursolidon
  181.         END IF
  182.  
  183.         IF insertKeyToggle = 0 THEN
  184.             GOSUB curoff
  185.             curPosX = curPosX - CharWidth
  186.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  187.             GOSUB curon
  188.         END IF
  189.  
  190.     END IF 'END LEFT ARROW
  191.  
  192.     '****
  193.  
  194.     '****
  195.     IF kh = 19712 AND curPosX < _PRINTWIDTH(Text) AND _PRINTWIDTH(Text) < _WIDTH THEN 'RIGHT ARROW
  196.  
  197.         IF insertKeyToggle = 1 AND LEN(Text) = printPos THEN
  198.             insertKeyToggle = 0
  199.             GOSUB cursolidoff
  200.             curPosX = curPosX + CharWidth
  201.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  202.             GOSUB curon
  203.             GOTO LL2
  204.         END IF
  205.  
  206.         IF insertKeyToggle = 1 AND LEN(Text) > printPos THEN
  207.             GOSUB cursolidoff
  208.             curPosX = curPosX + CharWidth
  209.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  210.             GOSUB cursolidon
  211.         END IF
  212.  
  213.         IF insertKeyToggle = 0 THEN
  214.             GOSUB curoff
  215.             curPosX = curPosX + CharWidth
  216.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  217.             GOSUB curon
  218.         END IF
  219.         LL2:
  220.  
  221.     END IF 'END RIGHT ARROW
  222.     '****
  223.  
  224.     '****
  225.     IF kh = 20480 AND lines + pixelUpDown + CharHeight < _HEIGHT THEN 'DOWN ARROW
  226.  
  227.         IF insertKeyToggle = 1 THEN
  228.             pixelUpDown = pixelUpDown + 1
  229.             GOSUB cursolidoff
  230.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  231.             _PRINTSTRING (0, lines + pixelUpDown), Text
  232.             GOSUB cursolidon
  233.         END IF
  234.  
  235.         IF insertKeyToggle = 0 THEN
  236.             pixelUpDown = pixelUpDown + 1
  237.             GOSUB curoff
  238.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  239.             _PRINTSTRING (0, lines + pixelUpDown), Text
  240.             GOSUB curon
  241.         END IF
  242.     END IF
  243.     '**** END DOWN ARROW
  244.  
  245.     '****
  246.     IF kh = 18432 AND lines + pixelUpDown > lines THEN 'UP ARROW
  247.  
  248.         IF insertKeyToggle = 1 THEN
  249.             pixelUpDown = pixelUpDown - 1
  250.             GOSUB cursolidoff
  251.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  252.             _PRINTSTRING (0, lines + pixelUpDown), Text
  253.             GOSUB cursolidon
  254.         END IF
  255.         IF insertKeyToggle = 0 THEN
  256.             pixelUpDown = pixelUpDown - 1
  257.             GOSUB curoff
  258.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  259.             _PRINTSTRING (0, lines + pixelUpDown), Text
  260.             GOSUB curon
  261.         END IF
  262.     END IF
  263.     '**** END UP ARROW
  264.  
  265. '****
  266.  
  267. '****
  268. cursolidon: 'Cursor for TYPE OVER mode  - ON
  269. FOR y = 0 TO CharHeight - 1
  270.     newoffset = curOffset - y * _WIDTH
  271.  
  272.     FOR T = newoffset * 4 TO newoffset * 4 + curWidth * 4 STEP 4
  273.  
  274.         b = _MEMGET(M, M.OFFSET + T, _UNSIGNED _BYTE)
  275.         b = b XOR 255
  276.         _MEMPUT M, M.OFFSET + T, b
  277.  
  278.         g = _MEMGET(M, M.OFFSET + T + 1, _UNSIGNED _BYTE)
  279.         g = g XOR 255
  280.         _MEMPUT M, M.OFFSET + T + 1, g
  281.  
  282.         r = _MEMGET(M, M.OFFSET + T + 2, _UNSIGNED _BYTE)
  283.         r = r XOR 0
  284.         _MEMPUT M, M.OFFSET + T + 2, r
  285.     NEXT T
  286.  
  287. printPos = curPosX / CharWidth + 1
  288. '****
  289. cursolidoff: 'Cursor for TYPE OVER mode - OFF
  290. FOR y = 0 TO CharHeight
  291.     newoffset = curOffset - y * _WIDTH
  292.  
  293.     FOR T = newoffset * 4 TO newoffset * 4 + curWidth * 4 STEP 4
  294.         b = _MEMGET(M, M.OFFSET + T, _UNSIGNED _BYTE)
  295.         g = _MEMGET(M, M.OFFSET + T + 1, _UNSIGNED _BYTE)
  296.         r = _MEMGET(M, M.OFFSET + T + 2, _UNSIGNED _BYTE)
  297.         IF b = 255 AND g = 255 AND r = 0 THEN b = 0: g = 0: r = 0
  298.         IF b = 0 AND g = 0 AND r = 255 THEN b = 255: g = 255: r = 255
  299.         _MEMPUT M, M.OFFSET + T, b
  300.         _MEMPUT M, M.OFFSET + T + 1, g
  301.         _MEMPUT M, M.OFFSET + T + 2, r
  302.     NEXT T
  303. printPos = curPosX / CharWidth + 1
  304. '****
  305.  
  306. curon:
  307. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  308.     b = 255
  309.     _MEMPUT M, M.OFFSET + T, b
  310.  
  311.     g = 255
  312.     _MEMPUT M, M.OFFSET + T + 1, g
  313.  
  314.     r = 0
  315.     _MEMPUT M, M.OFFSET + T + 2, r
  316. printPos = curPosX / CharWidth + 1
  317.  
  318. '****
  319. curoff:
  320.  
  321. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  322.  
  323.     b = 0
  324.     _MEMPUT M, M.OFFSET + T, b
  325.  
  326.     g = 0
  327.     _MEMPUT M, M.OFFSET + T + 1, g
  328.  
  329.     r = 0
  330.     _MEMPUT M, M.OFFSET + T + 2, r
  331. printPos = curPosX / CharWidth + 1
  332.  


« Last Edit: April 16, 2021, 11:41:12 pm by NOVARSEG »

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: Another line editor
« Reply #8 on: April 17, 2021, 08:47:04 am »
To create a word processing program, you need at least:
- a dimensioned variable (rows column) which contains the whole text document;
- a display module which presents part of the text on the visible screen
- an interactive part (inkey$, mouse...input, etc.) which impacts the screen and the dimensionned variable too
- lot of options (save, load, copy, aso... )

If you don't follow this way, you will not overcome this application...
A did such application in 80's... 
Good luck !!!


Why not yes ?

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Another line editor
« Reply #9 on: April 17, 2021, 04:13:01 pm »
Many good suggestions

The code so far shows basic line editing features with some graphical functions.

   The challenge here is to add more features without getting too complex. Yes arrays are useful but since we have MEMGET, maybe we can take snapshots of text and then move them as a group.

MS WORD has UNGROUP function too.



« Last Edit: April 17, 2021, 04:18:48 pm by NOVARSEG »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Another line editor
« Reply #10 on: April 17, 2021, 09:01:10 pm »
OK at the very least my single line text editor has a DARK background.   Try that with the DUMBED DOWN NOTEPAD or WORD PAD

cant even set margins in NOTE PAD anymore.    software has gotten real bad.  Time for change

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Another line editor
« Reply #11 on: April 17, 2021, 09:06:13 pm »
Well if you were to print something out on paper, white background is best.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Another line editor
« Reply #12 on: April 17, 2021, 09:10:14 pm »
of course.  it saves ink too.

i will correct about NOTE PAD.    If you click on the minimize "square thingy" then you can use "sizing handles"  but NOTEPAD does not have a black background feature.   Imagine if it did!

So many other wise good word processors won't let you change colors.  Even with WINDOWS 10 you need a uni degree to do so.   GOOGLE how do you change colors in windows 10 and you get thousands of hits with every arm chair expert saying DO THIS and damage your computer.

« Last Edit: April 17, 2021, 09:23:19 pm by NOVARSEG »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Another line editor
« Reply #13 on: April 18, 2021, 02:19:31 am »
Mouse cursor working

Code sometimes crashes  -"memory region out of range"  trying to debug

Some times_MOUSEX returns  -1 when the program first starts.  Mouse buffer overun?

OK both MOUSEX and MOUSEY return -1 at start of program - if mouse is moved just after "START" is clicked

Code: QB64: [Select]
  1. DIM Handle AS LONG
  2. DIM curWidth AS INTEGER
  3. DIM curPosX AS INTEGER
  4. DIM curOffset AS _UNSIGNED LONG
  5. DIM newcurOffset AS _UNSIGNED LONG
  6. DIM mouseOffset AS _UNSIGNED LONG
  7. DIM newmouseOffset AS _UNSIGNED LONG
  8.  
  9.  
  10. DIM CharWidth AS INTEGER
  11. DIM printPos AS INTEGER
  12. DIM insertKeyToggle AS _UNSIGNED _BIT
  13. DIM pixelUpDown AS INTEGER
  14.  
  15. DIM lines AS INTEGER
  16. lines = 5 * 16
  17.  
  18.  
  19. Text = "Some text to edit"
  20.  
  21. Handle = _NEWIMAGE(400, 400, 32)
  22. SCREEN Handle
  23. M = _MEMIMAGE(Handle)
  24.  
  25.  
  26.  
  27. PRINT "Move cursor with left / right arrow"
  28.  
  29. PRINT "Insert / Type over, Append, Delete, Backspace"
  30.  
  31. PRINT "Move text with up / down arrow. ESC to exit"
  32. PRINT Text
  33.  
  34. curWidth = (_PRINTWIDTH(CHR$(a)) - 2)
  35.  
  36. CharHeight = _FONTHEIGHT
  37. CharWidth = _PRINTWIDTH(CHR$(a))
  38.  
  39.  
  40. curPosX = _PRINTWIDTH(Text)
  41. curOffset = _WIDTH * (15 + lines) + curPosX 'initial
  42.  
  43.  
  44. GOSUB curon
  45. F = 0
  46.  
  47.     T1 = TIMER
  48.     DO
  49.  
  50.         _LIMIT 1000
  51.  
  52.         IF _MOUSEINPUT = -1 THEN
  53.  
  54.             X = _MOUSEX
  55.             Y = _MOUSEY
  56.             GOSUB curmouseoff
  57.             IF Y + CharHeight < _HEIGHT THEN mouseOffset = Y * _WIDTH + X
  58.             GOSUB curmouseon
  59.         END IF
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.         kh = _KEYHIT
  67.  
  68.         IF kh = 20992 AND insertKeyToggle = 0 AND LEN(Text) >= printPos THEN
  69.             insertKeyToggle = 1 'TYPEOVER mode
  70.             kh = 0
  71.             T1 = TIMER
  72.             GOSUB curoff
  73.             GOSUB cursolidon
  74.             F = 0
  75.         END IF
  76.  
  77.         IF kh = 20992 AND insertKeyToggle = 1 AND LEN(Text) >= printPos THEN
  78.             insertKeyToggle = 0 'INSERT MODE
  79.             kh = 0
  80.             T1 = TIMER
  81.             GOSUB cursolidoff
  82.             GOSUB curon
  83.             F = 0
  84.         END IF
  85.  
  86.         IF insertKeyToggle = 1 THEN
  87.             IF TIMER - T1 > .3 AND F = 1 THEN
  88.                 F = 0
  89.                 T1 = TIMER
  90.                 GOSUB cursolidon
  91.             END IF
  92.         END IF
  93.  
  94.         IF insertKeyToggle = 1 THEN
  95.             IF TIMER - T1 > .3 AND F = 0 THEN
  96.                 F = 1
  97.                 T1 = TIMER
  98.                 GOSUB cursolidoff
  99.             END IF
  100.         END IF
  101.  
  102.         IF insertKeyToggle = 0 THEN
  103.             IF TIMER - T1 > .3 AND F = 0 THEN
  104.                 F = 1
  105.                 T1 = TIMER
  106.                 GOSUB curoff
  107.             END IF
  108.             IF TIMER - T1 > .3 AND F = 1 THEN
  109.                 F = 0
  110.                 T1 = TIMER
  111.                 GOSUB curon
  112.             END IF
  113.         END IF
  114.  
  115.         IF kh > 0 AND insertKeyToggle = 1 THEN GOSUB cursolidon: EXIT DO
  116.         IF kh > 0 AND insertKeyToggle = 0 THEN GOSUB curon: EXIT DO
  117.     LOOP
  118.  
  119.  
  120.     ' PRINT kh
  121.  
  122.     IF kh = 27 THEN END
  123.  
  124.     IF kh = 8 AND curPosX > 0 THEN 'BACKSPACE
  125.         IF insertKeyToggle = 1 THEN GOSUB cursolidoff: insertKeyToggle = 0: GOTO LL3
  126.         GOSUB curoff
  127.         Text = LEFT$(Text, printPos - 2) + MID$(Text, printPos, LEN(Text) - (printPos - 1))
  128.         curPosX = curPosX - CharWidth
  129.         curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  130.         _PRINTSTRING (0, lines + pixelUpDown), Text + " "
  131.         GOSUB curon
  132.         LL3:
  133.     END IF
  134.     '****
  135.  
  136.     '****
  137.     IF kh > 31 AND kh < 127 AND LEN(Text) < printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'APPEND
  138.         Text = Text + CHR$(kh)
  139.         curPosX = _PRINTWIDTH(Text)
  140.         curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  141.         _PRINTSTRING (0, lines + pixelUpDown), Text
  142.         GOSUB curon
  143.     END IF
  144.     '****
  145.  
  146.     '****
  147.     IF kh > 31 AND kh < 127 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'INSERT TYPE OVER
  148.         IF insertKeyToggle = 0 THEN 'INSERT MODE
  149.             Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos, LEN(Text) - printPos + 1)
  150.             curPosX = curPosX + CharWidth
  151.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  152.             _PRINTSTRING (0, lines + pixelUpDown), Text
  153.             GOSUB curon
  154.         END IF
  155.  
  156.         IF insertKeyToggle = 1 AND LEN(Text) = printPos THEN 'cursor is pointing to last character
  157.             insertKeyToggle = 0
  158.             GOSUB cursolidoff
  159.             Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos + 1, LEN(Text) - (printPos))
  160.             curPosX = curPosX + CharWidth
  161.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  162.             _PRINTSTRING (0, lines + pixelUpDown), Text + " "
  163.             GOSUB curon
  164.         END IF
  165.  
  166.         IF insertKeyToggle = 1 AND LEN(Text) > printPos THEN 'cursor is pointing to any char except last char
  167.             GOSUB cursolidoff
  168.             Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos + 1, LEN(Text) - (printPos))
  169.             curPosX = curPosX + CharWidth
  170.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  171.             _PRINTSTRING (0, lines + pixelUpDown), Text + " "
  172.             GOSUB cursolidon
  173.         END IF
  174.     END IF 'END INSERT OR TYPE OVER
  175.  
  176.     '****
  177.  
  178.     '****
  179.  
  180.     IF kh = 21248 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) <= _WIDTH THEN 'DELETE
  181.         IF insertKeyToggle = 1 THEN GOSUB cursolidoff: insertKeyToggle = 0: GOTO LL1
  182.  
  183.         Text = LEFT$(Text, printPos - 1) + MID$(Text, printPos + 1, LEN(Text) - printPos + 1)
  184.         curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX '
  185.         _PRINTSTRING (0, lines + pixelUpDown), Text + " "
  186.         GOSUB curon
  187.         LL1:
  188.     END IF
  189.  
  190.     '****
  191.  
  192.     '****
  193.  
  194.     IF kh = 19200 AND curPosX > 0 THEN 'LEFT ARROW
  195.  
  196.         IF insertKeyToggle = 1 AND LEN(Text) >= printPos THEN
  197.             GOSUB cursolidoff
  198.             curPosX = curPosX - CharWidth
  199.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  200.             GOSUB cursolidon
  201.         END IF
  202.  
  203.         IF insertKeyToggle = 0 THEN
  204.             GOSUB curoff
  205.             curPosX = curPosX - CharWidth
  206.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  207.             GOSUB curon
  208.         END IF
  209.  
  210.     END IF 'END LEFT ARROW
  211.  
  212.     '****
  213.  
  214.     '****
  215.     IF kh = 19712 AND curPosX < _PRINTWIDTH(Text) AND _PRINTWIDTH(Text) < _WIDTH THEN 'RIGHT ARROW
  216.  
  217.         IF insertKeyToggle = 1 AND LEN(Text) = printPos THEN
  218.             insertKeyToggle = 0
  219.             GOSUB cursolidoff
  220.             curPosX = curPosX + CharWidth
  221.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  222.             GOSUB curon
  223.             GOTO LL2
  224.         END IF
  225.  
  226.         IF insertKeyToggle = 1 AND LEN(Text) > printPos THEN
  227.             GOSUB cursolidoff
  228.             curPosX = curPosX + CharWidth
  229.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  230.             GOSUB cursolidon
  231.         END IF
  232.  
  233.         IF insertKeyToggle = 0 THEN
  234.             GOSUB curoff
  235.             curPosX = curPosX + CharWidth
  236.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  237.             GOSUB curon
  238.         END IF
  239.         LL2:
  240.  
  241.     END IF 'END RIGHT ARROW
  242.     '****
  243.  
  244.     '****
  245.     IF kh = 20480 AND lines + pixelUpDown + CharHeight < _HEIGHT THEN 'DOWN ARROW
  246.  
  247.         IF insertKeyToggle = 1 THEN
  248.             pixelUpDown = pixelUpDown + 1
  249.             GOSUB cursolidoff
  250.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  251.             _PRINTSTRING (0, lines + pixelUpDown), Text
  252.             GOSUB cursolidon
  253.         END IF
  254.  
  255.         IF insertKeyToggle = 0 THEN
  256.             pixelUpDown = pixelUpDown + 1
  257.             GOSUB curoff
  258.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  259.             _PRINTSTRING (0, lines + pixelUpDown), Text
  260.             GOSUB curon
  261.         END IF
  262.     END IF
  263.     '**** END DOWN ARROW
  264.  
  265.     '****
  266.     IF kh = 18432 AND lines + pixelUpDown > lines THEN 'UP ARROW
  267.  
  268.         IF insertKeyToggle = 1 THEN
  269.             pixelUpDown = pixelUpDown - 1
  270.             GOSUB cursolidoff
  271.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  272.             _PRINTSTRING (0, lines + pixelUpDown), Text
  273.             GOSUB cursolidon
  274.         END IF
  275.         IF insertKeyToggle = 0 THEN
  276.             pixelUpDown = pixelUpDown - 1
  277.             GOSUB curoff
  278.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  279.             _PRINTSTRING (0, lines + pixelUpDown), Text
  280.             GOSUB curon
  281.         END IF
  282.     END IF
  283.     '**** END UP ARROW
  284.  
  285. '****
  286.  
  287. '****
  288. curmouseon:
  289. FOR Z = 0 TO CharHeight - 1
  290.     newmouseOffset = mouseOffset + Z * _WIDTH
  291.     FOR T = newmouseOffset * 4 TO newmouseOffset * 4 + curWidth * 4 STEP 4
  292.         b = _MEMGET(M, M.OFFSET + T, _UNSIGNED _BYTE)
  293.         g = _MEMGET(M, M.OFFSET + T + 1, _UNSIGNED _BYTE)
  294.         r = _MEMGET(M, M.OFFSET + T + 2, _UNSIGNED _BYTE)
  295.         IF b = 255 AND g = 255 AND r = 255 THEN b = 0: g = 0: r = 255
  296.         IF b = 0 AND g = 0 AND r = 0 THEN b = 255: g = 255: r = 0
  297.         _MEMPUT M, M.OFFSET + T, b
  298.         _MEMPUT M, M.OFFSET + T + 1, g
  299.         _MEMPUT M, M.OFFSET + T + 2, r
  300.     NEXT T
  301.  
  302. '****
  303. curmouseoff:
  304. FOR Z = 0 TO CharHeight - 1
  305.     newmouseOffset = mouseOffset + Z * _WIDTH
  306.  
  307.     FOR T = newmouseOffset * 4 TO newmouseOffset * 4 + curWidth * 4 STEP 4
  308.         'locate 100,100: print t
  309.         b = _MEMGET(M, M.OFFSET + T, _UNSIGNED _BYTE)
  310.         g = _MEMGET(M, M.OFFSET + T + 1, _UNSIGNED _BYTE)
  311.         r = _MEMGET(M, M.OFFSET + T + 2, _UNSIGNED _BYTE)
  312.         IF b = 0 AND g = 0 AND r = 255 THEN b = 255: g = 255: r = 255
  313.         IF b = 255 AND g = 255 AND r = 0 THEN b = 0: g = 0: r = 0
  314.         _MEMPUT M, M.OFFSET + T, b
  315.         _MEMPUT M, M.OFFSET + T + 1, g
  316.         _MEMPUT M, M.OFFSET + T + 2, r
  317.     NEXT T
  318.  
  319.  
  320. '****
  321. cursolidon: 'Cursor for TYPE OVER mode  - ON
  322. FOR Z = 0 TO CharHeight - 1
  323.     newcurOffset = curOffset - Z * _WIDTH
  324.  
  325.     FOR T = newcurOffset * 4 TO newcurOffset * 4 + curWidth * 4 STEP 4
  326.  
  327.         b = _MEMGET(M, M.OFFSET + T, _UNSIGNED _BYTE)
  328.         b = b XOR 255
  329.         _MEMPUT M, M.OFFSET + T, b
  330.  
  331.         g = _MEMGET(M, M.OFFSET + T + 1, _UNSIGNED _BYTE)
  332.         g = g XOR 255
  333.         _MEMPUT M, M.OFFSET + T + 1, g
  334.  
  335.         r = _MEMGET(M, M.OFFSET + T + 2, _UNSIGNED _BYTE)
  336.         r = r XOR 0
  337.         _MEMPUT M, M.OFFSET + T + 2, r
  338.     NEXT T
  339.  
  340. printPos = curPosX / CharWidth + 1
  341.  
  342. '****
  343. cursolidoff: 'Cursor for TYPE OVER mode - OFF
  344. FOR Z = 0 TO CharHeight
  345.     newcurOffset = curOffset - Z * _WIDTH
  346.  
  347.     FOR T = newcurOffset * 4 TO newcurOffset * 4 + curWidth * 4 STEP 4
  348.         b = _MEMGET(M, M.OFFSET + T, _UNSIGNED _BYTE)
  349.         g = _MEMGET(M, M.OFFSET + T + 1, _UNSIGNED _BYTE)
  350.         r = _MEMGET(M, M.OFFSET + T + 2, _UNSIGNED _BYTE)
  351.         IF b = 255 AND g = 255 AND r = 0 THEN b = 0: g = 0: r = 0
  352.         IF b = 0 AND g = 0 AND r = 255 THEN b = 255: g = 255: r = 255
  353.         _MEMPUT M, M.OFFSET + T, b
  354.         _MEMPUT M, M.OFFSET + T + 1, g
  355.         _MEMPUT M, M.OFFSET + T + 2, r
  356.     NEXT T
  357. printPos = curPosX / CharWidth + 1
  358. '****
  359.  
  360. curon:
  361. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  362.     b = 255
  363.     _MEMPUT M, M.OFFSET + T, b
  364.  
  365.     g = 255
  366.     _MEMPUT M, M.OFFSET + T + 1, g
  367.  
  368.     r = 0
  369.     _MEMPUT M, M.OFFSET + T + 2, r
  370. printPos = curPosX / CharWidth + 1
  371.  
  372. '****
  373. curoff:
  374.  
  375. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  376.  
  377.     b = 0
  378.     _MEMPUT M, M.OFFSET + T, b
  379.  
  380.     g = 0
  381.     _MEMPUT M, M.OFFSET + T + 1, g
  382.  
  383.     r = 0
  384.     _MEMPUT M, M.OFFSET + T + 2, r
  385. printPos = curPosX / CharWidth + 1
  386.  


It wont crash now.  added
IF X < 0 THEN EXIT DO
 IF Y < 0 THEN EXIT DO

Code: QB64: [Select]
  1. DIM Handle AS LONG
  2. DIM curWidth AS INTEGER
  3. DIM curPosX AS INTEGER
  4. DIM curOffset AS _UNSIGNED LONG
  5. DIM newcurOffset AS _UNSIGNED LONG
  6. DIM mouseOffset AS _UNSIGNED LONG
  7. DIM newmouseOffset AS _UNSIGNED LONG
  8.  
  9.  
  10. DIM CharWidth AS INTEGER
  11. DIM printPos AS INTEGER
  12. DIM insertKeyToggle AS _UNSIGNED _BIT
  13. DIM pixelUpDown AS INTEGER
  14.  
  15. DIM lines AS INTEGER
  16. lines = 5 * 16
  17.  
  18.  
  19. Text = "Some text to edit"
  20.  
  21. Handle = _NEWIMAGE(400, 400, 32)
  22. SCREEN Handle
  23. M = _MEMIMAGE(Handle)
  24.  
  25.  
  26.  
  27. PRINT "Move cursor with left / right arrow"
  28.  
  29. PRINT "Insert / Type over, Append, Delete, Backspace"
  30.  
  31. PRINT "Move text with up / down arrow. ESC to exit"
  32. PRINT Text
  33.  
  34. curWidth = (_PRINTWIDTH(CHR$(a)) - 2)
  35.  
  36. CharHeight = _FONTHEIGHT
  37. CharWidth = _PRINTWIDTH(CHR$(a))
  38.  
  39.  
  40. curPosX = _PRINTWIDTH(Text)
  41. curOffset = _WIDTH * (15 + lines) + curPosX 'initial
  42.  
  43.  
  44. GOSUB curon
  45. F = 0
  46.  
  47.  
  48.     X = 0
  49.     T1 = TIMER
  50.     DO
  51.  
  52.         _LIMIT 1000
  53.  
  54.         IF _MOUSEINPUT = -1 THEN
  55.             X = _MOUSEX
  56.             Y = _MOUSEY
  57.             IF X < 0 THEN EXIT DO
  58.             IF Y < 0 THEN EXIT DO
  59.             GOSUB curmouseoff
  60.             IF Y + CharHeight < _HEIGHT THEN mouseOffset = Y * _WIDTH + X
  61.             GOSUB curmouseon
  62.         END IF
  63.  
  64.  
  65.         kh = _KEYHIT
  66.  
  67.         IF kh = 20992 AND insertKeyToggle = 0 AND LEN(Text) >= printPos THEN
  68.             insertKeyToggle = 1 'TYPEOVER mode
  69.             kh = 0
  70.             T1 = TIMER
  71.             GOSUB curoff
  72.             GOSUB cursolidon
  73.             F = 0
  74.         END IF
  75.  
  76.         IF kh = 20992 AND insertKeyToggle = 1 AND LEN(Text) >= printPos THEN
  77.             insertKeyToggle = 0 'INSERT MODE
  78.             kh = 0
  79.             T1 = TIMER
  80.             GOSUB cursolidoff
  81.             GOSUB curon
  82.             F = 0
  83.         END IF
  84.  
  85.         IF insertKeyToggle = 1 THEN
  86.             IF TIMER - T1 > .3 AND F = 1 THEN
  87.                 F = 0
  88.                 T1 = TIMER
  89.                 GOSUB cursolidon
  90.             END IF
  91.         END IF
  92.  
  93.         IF insertKeyToggle = 1 THEN
  94.             IF TIMER - T1 > .3 AND F = 0 THEN
  95.                 F = 1
  96.                 T1 = TIMER
  97.                 GOSUB cursolidoff
  98.             END IF
  99.         END IF
  100.  
  101.         IF insertKeyToggle = 0 THEN
  102.             IF TIMER - T1 > .3 AND F = 0 THEN
  103.                 F = 1
  104.                 T1 = TIMER
  105.                 GOSUB curoff
  106.             END IF
  107.             IF TIMER - T1 > .3 AND F = 1 THEN
  108.                 F = 0
  109.                 T1 = TIMER
  110.                 GOSUB curon
  111.             END IF
  112.         END IF
  113.  
  114.         IF kh > 0 AND insertKeyToggle = 1 THEN GOSUB cursolidon: EXIT DO
  115.         IF kh > 0 AND insertKeyToggle = 0 THEN GOSUB curon: EXIT DO
  116.     LOOP
  117.  
  118.  
  119.     ' PRINT kh
  120.  
  121.     IF kh = 27 THEN END
  122.  
  123.     IF kh = 8 AND curPosX > 0 THEN 'BACKSPACE
  124.         IF insertKeyToggle = 1 THEN GOSUB cursolidoff: insertKeyToggle = 0: GOTO LL3
  125.         GOSUB curoff
  126.         Text = LEFT$(Text, printPos - 2) + MID$(Text, printPos, LEN(Text) - (printPos - 1))
  127.         curPosX = curPosX - CharWidth
  128.         curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  129.         _PRINTSTRING (0, lines + pixelUpDown), Text + " "
  130.         GOSUB curon
  131.         LL3:
  132.     END IF
  133.     '****
  134.  
  135.     '****
  136.     IF kh > 31 AND kh < 127 AND LEN(Text) < printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'APPEND
  137.         Text = Text + CHR$(kh)
  138.         curPosX = _PRINTWIDTH(Text)
  139.         curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  140.         _PRINTSTRING (0, lines + pixelUpDown), Text
  141.         GOSUB curon
  142.     END IF
  143.     '****
  144.  
  145.     '****
  146.     IF kh > 31 AND kh < 127 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) + CharWidth < _WIDTH THEN 'INSERT TYPE OVER
  147.         IF insertKeyToggle = 0 THEN 'INSERT MODE
  148.             Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos, LEN(Text) - printPos + 1)
  149.             curPosX = curPosX + CharWidth
  150.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  151.             _PRINTSTRING (0, lines + pixelUpDown), Text
  152.             GOSUB curon
  153.         END IF
  154.  
  155.         IF insertKeyToggle = 1 AND LEN(Text) = printPos THEN 'cursor is pointing to last character
  156.             insertKeyToggle = 0
  157.             GOSUB cursolidoff
  158.             Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos + 1, LEN(Text) - (printPos))
  159.             curPosX = curPosX + CharWidth
  160.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  161.             _PRINTSTRING (0, lines + pixelUpDown), Text + " "
  162.             GOSUB curon
  163.         END IF
  164.  
  165.         IF insertKeyToggle = 1 AND LEN(Text) > printPos THEN 'cursor is pointing to any char except last char
  166.             GOSUB cursolidoff
  167.             Text = LEFT$(Text, printPos - 1) + CHR$(kh) + MID$(Text, printPos + 1, LEN(Text) - (printPos))
  168.             curPosX = curPosX + CharWidth
  169.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  170.             _PRINTSTRING (0, lines + pixelUpDown), Text + " "
  171.             GOSUB cursolidon
  172.         END IF
  173.     END IF 'END INSERT OR TYPE OVER
  174.  
  175.     '****
  176.  
  177.     '****
  178.  
  179.     IF kh = 21248 AND LEN(Text) >= printPos AND _PRINTWIDTH(Text) <= _WIDTH THEN 'DELETE
  180.         IF insertKeyToggle = 1 THEN GOSUB cursolidoff: insertKeyToggle = 0: GOTO LL1
  181.  
  182.         Text = LEFT$(Text, printPos - 1) + MID$(Text, printPos + 1, LEN(Text) - printPos + 1)
  183.         curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX '
  184.         _PRINTSTRING (0, lines + pixelUpDown), Text + " "
  185.         GOSUB curon
  186.         LL1:
  187.     END IF
  188.  
  189.     '****
  190.  
  191.     '****
  192.  
  193.     IF kh = 19200 AND curPosX > 0 THEN 'LEFT ARROW
  194.  
  195.         IF insertKeyToggle = 1 AND LEN(Text) >= printPos THEN
  196.             GOSUB cursolidoff
  197.             curPosX = curPosX - CharWidth
  198.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  199.             GOSUB cursolidon
  200.         END IF
  201.  
  202.         IF insertKeyToggle = 0 THEN
  203.             GOSUB curoff
  204.             curPosX = curPosX - CharWidth
  205.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  206.             GOSUB curon
  207.         END IF
  208.  
  209.     END IF 'END LEFT ARROW
  210.  
  211.     '****
  212.  
  213.     '****
  214.     IF kh = 19712 AND curPosX < _PRINTWIDTH(Text) AND _PRINTWIDTH(Text) < _WIDTH THEN 'RIGHT ARROW
  215.  
  216.         IF insertKeyToggle = 1 AND LEN(Text) = printPos THEN
  217.             insertKeyToggle = 0
  218.             GOSUB cursolidoff
  219.             curPosX = curPosX + CharWidth
  220.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  221.             GOSUB curon
  222.             GOTO LL2
  223.         END IF
  224.  
  225.         IF insertKeyToggle = 1 AND LEN(Text) > printPos THEN
  226.             GOSUB cursolidoff
  227.             curPosX = curPosX + CharWidth
  228.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  229.             GOSUB cursolidon
  230.         END IF
  231.  
  232.         IF insertKeyToggle = 0 THEN
  233.             GOSUB curoff
  234.             curPosX = curPosX + CharWidth
  235.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  236.             GOSUB curon
  237.         END IF
  238.         LL2:
  239.  
  240.     END IF 'END RIGHT ARROW
  241.     '****
  242.  
  243.     '****
  244.     IF kh = 20480 AND lines + pixelUpDown + CharHeight < _HEIGHT THEN 'DOWN ARROW
  245.  
  246.         IF insertKeyToggle = 1 THEN
  247.             pixelUpDown = pixelUpDown + 1
  248.             GOSUB cursolidoff
  249.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  250.             _PRINTSTRING (0, lines + pixelUpDown), Text
  251.             GOSUB cursolidon
  252.         END IF
  253.  
  254.         IF insertKeyToggle = 0 THEN
  255.             pixelUpDown = pixelUpDown + 1
  256.             GOSUB curoff
  257.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  258.             _PRINTSTRING (0, lines + pixelUpDown), Text
  259.             GOSUB curon
  260.         END IF
  261.     END IF
  262.     '**** END DOWN ARROW
  263.  
  264.     '****
  265.     IF kh = 18432 AND lines + pixelUpDown > lines THEN 'UP ARROW
  266.  
  267.         IF insertKeyToggle = 1 THEN
  268.             pixelUpDown = pixelUpDown - 1
  269.             GOSUB cursolidoff
  270.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  271.             _PRINTSTRING (0, lines + pixelUpDown), Text
  272.             GOSUB cursolidon
  273.         END IF
  274.         IF insertKeyToggle = 0 THEN
  275.             pixelUpDown = pixelUpDown - 1
  276.             GOSUB curoff
  277.             curOffset = _WIDTH * (15 + lines + pixelUpDown) + curPosX
  278.             _PRINTSTRING (0, lines + pixelUpDown), Text
  279.             GOSUB curon
  280.         END IF
  281.     END IF
  282.     '**** END UP ARROW
  283.  
  284. '****
  285.  
  286. '****
  287. curmouseon:
  288. FOR Z = 0 TO CharHeight - 1
  289.     newmouseOffset = mouseOffset + Z * _WIDTH
  290.     FOR T = newmouseOffset * 4 TO newmouseOffset * 4 + curWidth * 4 STEP 4
  291.         b = _MEMGET(M, M.OFFSET + T, _UNSIGNED _BYTE)
  292.         g = _MEMGET(M, M.OFFSET + T + 1, _UNSIGNED _BYTE)
  293.         r = _MEMGET(M, M.OFFSET + T + 2, _UNSIGNED _BYTE)
  294.         IF b = 255 AND g = 255 AND r = 255 THEN b = 0: g = 0: r = 255
  295.         IF b = 0 AND g = 0 AND r = 0 THEN b = 255: g = 255: r = 0
  296.         _MEMPUT M, M.OFFSET + T, b
  297.         _MEMPUT M, M.OFFSET + T + 1, g
  298.         _MEMPUT M, M.OFFSET + T + 2, r
  299.     NEXT T
  300.  
  301. '****
  302. curmouseoff:
  303. FOR Z = 0 TO CharHeight - 1
  304.     newmouseOffset = mouseOffset + Z * _WIDTH
  305.  
  306.     FOR T = newmouseOffset * 4 TO newmouseOffset * 4 + curWidth * 4 STEP 4
  307.         'locate 100,100: print t
  308.         b = _MEMGET(M, M.OFFSET + T, _UNSIGNED _BYTE)
  309.         g = _MEMGET(M, M.OFFSET + T + 1, _UNSIGNED _BYTE)
  310.         r = _MEMGET(M, M.OFFSET + T + 2, _UNSIGNED _BYTE)
  311.         IF b = 0 AND g = 0 AND r = 255 THEN b = 255: g = 255: r = 255
  312.         IF b = 255 AND g = 255 AND r = 0 THEN b = 0: g = 0: r = 0
  313.         _MEMPUT M, M.OFFSET + T, b
  314.         _MEMPUT M, M.OFFSET + T + 1, g
  315.         _MEMPUT M, M.OFFSET + T + 2, r
  316.     NEXT T
  317.  
  318.  
  319. '****
  320. cursolidon: 'Cursor for TYPE OVER mode  - ON
  321. FOR Z = 0 TO CharHeight - 1
  322.     newcurOffset = curOffset - Z * _WIDTH
  323.  
  324.     FOR T = newcurOffset * 4 TO newcurOffset * 4 + curWidth * 4 STEP 4
  325.  
  326.         b = _MEMGET(M, M.OFFSET + T, _UNSIGNED _BYTE)
  327.         b = b XOR 255
  328.         _MEMPUT M, M.OFFSET + T, b
  329.  
  330.         g = _MEMGET(M, M.OFFSET + T + 1, _UNSIGNED _BYTE)
  331.         g = g XOR 255
  332.         _MEMPUT M, M.OFFSET + T + 1, g
  333.  
  334.         r = _MEMGET(M, M.OFFSET + T + 2, _UNSIGNED _BYTE)
  335.         r = r XOR 0
  336.         _MEMPUT M, M.OFFSET + T + 2, r
  337.     NEXT T
  338.  
  339. printPos = curPosX / CharWidth + 1
  340.  
  341. '****
  342. cursolidoff: 'Cursor for TYPE OVER mode - OFF
  343. FOR Z = 0 TO CharHeight
  344.     newcurOffset = curOffset - Z * _WIDTH
  345.  
  346.     FOR T = newcurOffset * 4 TO newcurOffset * 4 + curWidth * 4 STEP 4
  347.         b = _MEMGET(M, M.OFFSET + T, _UNSIGNED _BYTE)
  348.         g = _MEMGET(M, M.OFFSET + T + 1, _UNSIGNED _BYTE)
  349.         r = _MEMGET(M, M.OFFSET + T + 2, _UNSIGNED _BYTE)
  350.         IF b = 255 AND g = 255 AND r = 0 THEN b = 0: g = 0: r = 0
  351.         IF b = 0 AND g = 0 AND r = 255 THEN b = 255: g = 255: r = 255
  352.         _MEMPUT M, M.OFFSET + T, b
  353.         _MEMPUT M, M.OFFSET + T + 1, g
  354.         _MEMPUT M, M.OFFSET + T + 2, r
  355.     NEXT T
  356. printPos = curPosX / CharWidth + 1
  357. '****
  358.  
  359. curon:
  360. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  361.     b = 255
  362.     _MEMPUT M, M.OFFSET + T, b
  363.  
  364.     g = 255
  365.     _MEMPUT M, M.OFFSET + T + 1, g
  366.  
  367.     r = 0
  368.     _MEMPUT M, M.OFFSET + T + 2, r
  369. printPos = curPosX / CharWidth + 1
  370.  
  371. '****
  372. curoff:
  373.  
  374. FOR T = curOffset * 4 TO curOffset * 4 + curWidth * 4 STEP 4
  375.  
  376.     b = 0
  377.     _MEMPUT M, M.OFFSET + T, b
  378.  
  379.     g = 0
  380.     _MEMPUT M, M.OFFSET + T + 1, g
  381.  
  382.     r = 0
  383.     _MEMPUT M, M.OFFSET + T + 2, r
  384. printPos = curPosX / CharWidth + 1

If no one noticed yet,  I don't use the ELSE  statement.  I find it creates far more confusion than it solves. In other words, if you feel inclined to use ELSE, simply change the  code so you DON'T have to use it.

Also there is not one FUNCTION or SUB in my code.  GOSUBS are easier to code  less DIMimg and no overhead. Variables are automatically global.


« Last Edit: April 18, 2021, 03:48:29 am by NOVARSEG »

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: Another line editor
« Reply #14 on: April 18, 2021, 02:42:08 am »
I am wondering ..is there anyone who try to use native windows edit control and made editor.
for more features you can subclass edit control too.
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////