Author Topic: Well here's a highlighting routine I've been fiddling with for a couple of days.  (Read 3550 times)

0 Members and 1 Guest are viewing this topic.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
I probably have made a half-dozen of these, over the decades, but I always love reinventing the mouse wheel. This could probably use some tweaking, and I might look up the actual highlighting algorithm for changing color I developed some years ago. This one uses a screen reading cheat to determine the highlight / unhighlight color for the text segments. That, and I cut corners by using a loop routine for r/l mouse highlighting, rather than calculating the actual string conditions. There's also a bit of a cheat using the fact that QB64 allows parameters, which exceed the number of characters in a MID$() string; meaning PRINT MID$(a$, 6, 10000) Is accepted even if the a$ string is only 10-characters long.

Code: QB64: [Select]
  1. REM debug% = -1
  2. margin.l = 4
  3. margin.t = 2
  4.  
  5. ' DEMO sample text block.-----------------------------------------------------
  6. REDIM x$(20)
  7. x$(1) = "qwerty"
  8. x$(2) = "asdfg"
  9. x$(3) = "zxcvbn"
  10. x$(4) = "abcd"
  11. x$(5) = "123456"
  12. x$(6) = "qwerty"
  13. x$(7) = "987654"
  14. x$(8) = "mnbvcx"
  15. x$(9) = "asdf"
  16. x$(10) = "poiuyt"
  17.  
  18. FOR h = 0 TO 1
  19.     FOR i = 1 TO 10
  20.         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)
  21.     NEXT
  22.  
  23. '======================== Needed variables for routine and demo.
  24. 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.
  25. page.h = 10 ' Page height.
  26. noe% = 20 ' Number of text line entries.
  27. '========================
  28.  
  29. c1% = 7: c2% = 0: h1% = 15: h2% = 1 ' Normal text and highlighted text colors.
  30.  
  31. h% = 0: GOSUB displaytext
  32.  
  33. LOCATE margin.t + 1, margin.l + 1, 1, 7, 30
  34. ' / Demo sample text block.--------------------------------------------------
  35.  
  36. ' ===========================================================================
  37.  
  38. ' Highlight Routine----------------------------------------------------------
  39. row = CSRLIN - margin.t: col = POS(0) - margin.l
  40.  
  41.     _LIMIT 30
  42.  
  43.     DEF SEG = 0 ' Look for Shift key press.
  44.     IF PEEK(1047) MOD 16 = 1 OR PEEK(1047) MOD 16 = 2 THEN shift% = -1 ELSE shift% = 0
  45.     DEF SEG
  46.  
  47.     IF autokey% = 0 THEN b$ = INKEY$ ELSE autokey% = 0
  48.  
  49.     IF lb% THEN
  50.         null$ = INKEY$: b$ = "" ' Clear key buffer when left mouse button is held down and lock out keys.
  51.     END IF
  52.  
  53.     IF LEN(b$) OR mhl% THEN ' Key press or mouse clicked / held down (mhl%).
  54.         IF b$ = CHR$(13) AND debug% THEN CLS: RUN
  55.         IF b$ = CHR$(27) THEN h% = 0: GOSUB displaytext: _DELAY .4: SYSTEM
  56.  
  57.         IF mhl% OR shift% AND INSTR("KMHPOG", MID$(b$, 2, 1)) AND LEN(b$) = 2 THEN
  58.             hlkey% = -1 ' Left mouse button down or Shift key down with a highlighting key pressed.
  59.             IF mark% = 0 THEN
  60.                 mark% = (row - 1) * dwidth + col: markrow% = row: markcol% = col ' Highlight cursor position markers.
  61.                 IF cutmrow% = 0 THEN cutmrow% = markrow% + scr: cutmcol% = markcol%
  62.             END IF
  63.         ELSE
  64.             ' Disable highlight key when active and a key is pressed without Shift key held or
  65.             IF hlkey% AND LEN(b$) > 0 THEN ' Highlighted text and key press. Note: Left mouse click to clear text is in another routine.
  66.                 hlkey% = 0: cutmrow% = 0: GOSUB clearmarkers ' Disable highlight and zero highlighting cursor marker.
  67.                 h% = 0: GOSUB displaytext ' Use this or some other routine to clear the highlighted text off the screen.
  68.             END IF
  69.         END IF
  70.  
  71.         IF mhl% THEN ' Left mouse button click or held highlighting.
  72.             IF mhl1row% = oldmhl1row% THEN
  73.                 ' Lateral movement. Take no action here. Action is taken in the select case mov routine.
  74.             ELSE ' Simulate an up or down arrow key routines for mouse highlighting when changing rows.
  75.                 IF mhl1row% < oldmhl1row% THEN b$ = CHR$(0) + "H" ' Highlight upwards.
  76.                 IF mhl1row% > oldmhl1row% THEN b$ = CHR$(0) + "P" ' Highlight downwards.
  77.             END IF
  78.         END IF
  79.  
  80.         DO
  81.             IF mhl% THEN ' Left mouse key highlighting.
  82.                 IF row = mhl1row% THEN ' Detect when the row in the loop matches the row marked to terminate the loop.
  83.                     SELECT CASE mhl1col% ' Now that the loop has terminated at the marked row, find the marked column.
  84.                         CASE 0 ' Column at loop exit is already at the marked column.
  85.                             mhl% = 0: EXIT DO
  86.                         CASE ELSE ' Set col variable to mouse column marker and zero out that marker along with the simulated key press.
  87.                             col = mhl1col%: mhl1col% = 0 ' New col position allows highlighter routine to highlight text on this row to this column.
  88.                             b$ = "" ' Important. Must nullify variable to avoid case selection below.
  89.                     END SELECT
  90.                 END IF
  91.             END IF
  92.  
  93.             SELECT CASE MID$(b$, 2, 1) ' Cursor movement routine. May be replace or incorporated into outer cursor movement routines.
  94.                 CASE "H"
  95.                     IF row > 1 THEN
  96.                         row = row - 1
  97.                     ELSE
  98.                         IF scr > 0 THEN
  99.                             IF mark% THEN
  100.                                 i% = -1: GOSUB redomarkers
  101.                                 row = row + 1: scr = scr - 1
  102.                                 h% = 2: GOSUB displaytext
  103.                                 row = row - 1
  104.                             ELSE
  105.                                 scr = scr - 1
  106.                                 h% = 0: GOSUB displaytext
  107.                             END IF
  108.                         END IF
  109.                     END IF
  110.                 CASE "P"
  111.                     IF row < page.h THEN
  112.                         row = row + 1
  113.                     ELSE
  114.                         IF row + scr < noe% THEN
  115.                             IF mark% THEN
  116.                                 i% = 1: GOSUB redomarkers
  117.                                 row = row - 1
  118.                                 scr = scr + 1
  119.                                 h% = page.h - 1: GOSUB displaytext
  120.                                 row = row + 1
  121.                             ELSE
  122.                                 scr = scr + 1
  123.                                 h% = 0: GOSUB displaytext
  124.                             END IF
  125.                         END IF
  126.                     END IF
  127.                 CASE "M"
  128.                     IF col < LEN(x$(row + scr)) THEN
  129.                         col = col + 1
  130.                     ELSE
  131.                         IF row < page.h THEN
  132.                             row = row + 1: col = 1
  133.                         ELSE
  134.                             IF row + scr < noe% THEN
  135.                                 IF mark% THEN
  136.                                     i% = 1: GOSUB redomarkers
  137.                                     row = row - 1: scr = scr + 1
  138.                                     h% = page.h - 1: GOSUB displaytext
  139.                                     row = row + 1: col = 1
  140.                                 ELSE
  141.                                     scr = scr + 1: col = 1
  142.                                     h% = 0: GOSUB displaytext
  143.                                 END IF
  144.                                 LOCATE margin.t + row, margin.l + col
  145.                             END IF
  146.                         END IF
  147.                     END IF
  148.                 CASE "K"
  149.                     IF col > 1 THEN
  150.                         col = col - 1
  151.                     ELSE
  152.                         IF row > 1 THEN
  153.                             row = row - 1: col = dwidth - 1
  154.                         ELSE
  155.                             IF scr > 0 THEN
  156.                                 IF mark% THEN
  157.                                     i% = -1: GOSUB redomarkers
  158.                                     row = row + 1: scr = scr - 1
  159.                                     h% = 2: GOSUB displaytext
  160.                                     row = row - 1: col = LEN(x$(row + scr)): IF col < dwidth - 1 THEN col = col + 1
  161.                                 ELSE
  162.                                     scr = scr - 1
  163.                                     col = LEN(x$(row + scr)): IF col < dwidth - 1 THEN col = col + 1
  164.                                     h% = 0: GOSUB displaytext
  165.                                 END IF
  166.                             END IF
  167.                         END IF
  168.                     END IF
  169.                 CASE "G"
  170.                     col = 1
  171.                 CASE "O"
  172.                     col = LEN(x$(row + scr)): IF col < dwidth - 1 THEN col = col + 1
  173.             END SELECT
  174.  
  175.             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.
  176.  
  177.             IF mark% THEN GOSUB highlighter
  178.  
  179.             IF col > LEN(x$(row + scr)) THEN
  180.                 col = LEN(x$(row + scr))
  181.                 IF col < dwidth - 1 THEN col = col + 1
  182.                 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.
  183.             END IF
  184.  
  185.         LOOP UNTIL mhl% = 0 ' Exit for key press because mhl% is zero during key presses.
  186.     END IF
  187.  
  188.         mw% = mw% + _MOUSEWHEEL
  189.     WEND
  190.         my% = _MOUSEY
  191.         mx% = _MOUSEX
  192.         lb% = _MOUSEBUTTON(1)
  193.  
  194.     IF shift% AND mw% THEN
  195.         IF mw% > 0 THEN
  196.             b$ = CHR$(0) + "P"
  197.         ELSE
  198.             b$ = CHR$(0) + "H"
  199.         END IF
  200.         mw% = 0: autokey% = -1
  201.     ELSE
  202.         mw% = 0
  203.     END IF
  204.  
  205.     IF lb% THEN ' Left mouse button pressed.
  206.         IF mx% > margin.l AND mx% <= margin.l + dwidth - 1 THEN ' Cursor in-bounds.
  207.             ii% = my% - margin.t
  208.             IF ii% > page.h OR ii% <= 0 THEN
  209.                 IF ii% < 1 THEN ii% = 1
  210.                 IF ii% > page.h THEN ii% = page.h
  211.                 IF mark% THEN
  212.                     IF ii% = 1 THEN
  213.                         IF scr > 0 THEN
  214.                             col = mx% - margin.l
  215.                             LOCATE margin.t + ii%, margin.l + col
  216.                             i% = -1: GOSUB redomarkers
  217.                             row = row + 1: scr = scr - 1
  218.                             h% = 2: GOSUB displaytext
  219.                             LOCATE margin.t + ii%, margin.l + col
  220.                             row = CSRLIN - margin.t
  221.                         END IF
  222.                     ELSE
  223.                         IF ii% + scr < noe% THEN
  224.                             col = mx% - margin.l
  225.                             LOCATE margin.t + ii%, margin.l + col
  226.                             i% = 1: GOSUB redomarkers
  227.                             row = row - 1: scr = scr + 1
  228.                             h% = page.h - 1: GOSUB displaytext
  229.                             LOCATE margin.t + ii%, margin.l + col
  230.                             row = CSRLIN - margin.t
  231.                         END IF
  232.                     END IF
  233.                 END IF
  234.             END IF
  235.  
  236.             IF mhlclear% THEN ' Check to see if highlighting should be removed.
  237.                 IF shift% THEN ' Do not remove highlighted text.
  238.                     mhlclear% = 0
  239.                 ELSE
  240.                     cutmrow% = 0: GOSUB clearmarkers
  241.                     h% = 0: GOSUB displaytext ' Remove highlighted text.
  242.                 END IF
  243.             END IF
  244.  
  245.             IF shift% OR (ii% - 1) * dwidth + mx% - margin.l <> mhl1% AND mhl1% <> 0 THEN
  246.                 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).
  247.                 IF oldmhl1row% = 0 THEN oldmhl1row% = row ELSE oldmhl1row% = mhl1row%
  248.                 mhl1row% = ii%: mhl1col% = mx% - margin.l: mhl1% = (ii% - 1) * dwidth + mx% - margin.l
  249.             ELSE
  250.                 IF mhl1% <> (ii% - 1) * dwidth + mx% - margin.l THEN
  251.                     mhl1% = (ii% - 1) * dwidth + mx% - margin.l
  252.                     row = ii%: col = mx% - margin.l
  253.                     LOCATE margin.t + row, margin.l + col
  254.                 END IF
  255.             END IF
  256.         END IF
  257.     ELSE ' Left mouse button unpressed or released.
  258.         mhl1% = 0 ' Left mouse button released so zero the highlighting marker.
  259.         IF mark% THEN mhlclear% = -1 ' Will be triggered the next time the left mouse button is pressed to remove all highlighted text.
  260.     END IF
  261.  
  262. displaytext:
  263. yy% = CSRLIN: xx% = POS(0)
  264. IF cutmrow% THEN
  265.     cutdrow% = row + scr: cutdcol% = col
  266.     IF debug% THEN GOSUB debugger
  267.     SELECT CASE cutmrow%
  268.         CASE IS < cutdrow%: j% = cutmrow%: j1% = cutmcol%
  269.         CASE IS = cutdrow% ' Highlighting begins and ends on same row. Ex: Left and right arrow keys.
  270.             j% = cutmrow%
  271.             IF cutmcol% < cutdcol% THEN j% = cutmrow%: j1% = cutmcol% ELSE j% = cutdrow%: j1% = cutdcol% ' Right arrow vs left arrow directions.
  272.         CASE IS > cutdrow%: j% = cutdrow%: j1% = cutdcol%
  273.     END SELECT
  274.     jtop% = cutmrow%: jbot% = cutdrow%: IF jtop% > jbot% THEN SWAP jtop%, jbot%
  275.     IF debug% THEN GOSUB debugger
  276.     j% = 0
  277.  
  278. FOR i% = 1 TO page.h
  279.     LOCATE margin.t + i%, margin.l + 1
  280.     a1$ = SPACE$(dwidth)
  281.     MID$(a1$, 1) = x$(scr + i%)
  282.     a2$ = x$(scr + i%): a3$ = SPACE$(dwidth - LEN(a2$))
  283.     SELECT CASE j%
  284.         CASE 0
  285.             PRINT a1$;
  286.         CASE ELSE
  287.             IF debug% THEN GOSUB debugger
  288.             k% = LEN(a2$)
  289.             x1 = c1%: x2 = c2%: x3 = c1%: x4 = c2%
  290.  
  291.             IF i% = h% THEN
  292.                 IF cutmrow% = cutdrow% THEN ' Left/Right Highlighting on origination row.
  293.                     x3 = h1%: x4 = h2%: k% = ABS(cutmcol% - cutdcol%)
  294.                 ELSE ' Left/Right/Up/Down Highlighting from row before scrolling row.
  295.                     x3 = h1%: x4 = h2%
  296.                     IF cutdrow% > cutmrow% THEN
  297.                         j1% = 1: k% = col - 1
  298.                     ELSE
  299.                         ' Do nothing. Arrow left and up do not require any change in parameters here.
  300.                     END IF
  301.                 END IF
  302.             ELSE
  303.                 IF i% + scr > jtop% AND i% + scr < jbot% THEN
  304.                     j1% = 1: x3 = h1%: x4 = h2%
  305.                 ELSE
  306.                     IF i% + scr = cutmrow% THEN
  307.                         x3 = h1%: x4 = h2%
  308.                         IF cutdrow% < cutmrow% THEN j1% = 1: k% = cutmcol% - 1
  309.                     END IF
  310.                 END IF
  311.             END IF
  312.  
  313.             COLOR x1, x2: PRINT MID$(a2$, 1, j1% - 1);
  314.             COLOR x3, x4: PRINT MID$(a2$, j1%, k%);
  315.             COLOR c1%, c2%: PRINT MID$(a2$, j1% + k%, LEN(a2$) - k%) + a3$;
  316.  
  317.     END SELECT
  318. COLOR c1%, c2%
  319. LOCATE yy%, xx%
  320.  
  321. highlighter:
  322. yy% = CSRLIN: xx% = POS(0)
  323. COLOR h1%, h2% ' Initiate highlight color.
  324. 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%).
  325. d% = (row - 1) * dwidth + col ' Relative (margin independent) cursor destination marker.
  326. o1% = (o% - (o% MOD dwidth)) / dwidth + 1 ' Relative (margin independent) cursor row origin.
  327. o2% = o% MOD dwidth ' Relative (margin independent) cursor column origin.
  328. d1% = row ' Relative (margin independent) cursor row destination. Note: row is also relative (margin independent).
  329. d2% = col ' Relative (margin independent) cursor column destination. Note: col is also relative (margin independent).
  330. 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).
  331. IF debug% THEN GOSUB debugger
  332.  
  333.     CASE 1 ' End, right, or down
  334.         x1% = o1%: x2% = o2%: LOCATE margin.t + x1%, margin.l + x2%
  335.         ' Highlighting on line above, from origin row.
  336.         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.
  337.             i% = 0: DO UNTIL POS(0) = margin.l + d2% OR i% = LEN(x$(x1% + scr)): GOSUB getcolor: PRINT MID$(x$(x1% + scr), x2% + i%, 1);: i% = i% + 1: LOOP
  338.         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.
  339.             GOSUB getcolor: PRINT MID$(x$(x1% + scr), x2%, ABS(o2% - markcol%)); ' Print from origin column to initial marker column.
  340.             GOSUB getcolor: PRINT MID$(x$(x1% + scr), x2% + ABS(o2% - markcol%)); ' Print from initial marker column to destination column.
  341.         ELSE ' Right arrow key, down arrow key or mouse moving down.
  342.             GOSUB getcolor: PRINT MID$(x$(x1% + scr), x2%, ABS(o1% - d1%) * dwidth + ABS(o2% - d2%)); ' Trick method to determine length of highlighting.
  343.         END IF
  344.  
  345.         IF o1% - d1% THEN ' Highlighting on current row after row above is finished.
  346.             x1% = row: x2% = col: LOCATE margin.t + x1%, margin.l + 1
  347.             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.
  348.                 IF d2% < markcol% THEN i% = d2% ELSE i% = markcol% ' Set i% to the furthest column.
  349.                 GOSUB getcolor: PRINT MID$(x$(x1% + scr), 1, i% - 1); ' unhighlight to the furthest column.
  350.                 GOSUB getcolor: PRINT MID$(x$(x1% + scr), i%, ABS(d2% - i%));
  351.             ELSE ' Arrow down or mouse downward, either with original marker row on same line.
  352.                 GOSUB getcolor: PRINT MID$(x$(x1% + scr), 1, d2% - 1); ' Highlight from first column on current row to destination on current row.
  353.             END IF
  354.         END IF
  355.  
  356.     CASE -1 ' Home, up, or Left.
  357.         x1% = row: x2% = col: LOCATE margin.t + x1%, margin.l + x2%
  358.  
  359.         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.
  360.             IF mx% - margin.l < LEN(x$(x1% + scr)) THEN
  361.                 i% = 0: DO UNTIL POS(0) = margin.l + o2%: GOSUB getcolor: PRINT MID$(x$(x1% + scr), x2% + i%, 1);: i% = i% + 1: LOOP
  362.             END IF
  363.         ELSEIF x1% = markrow% AND ABS(o% - d%) > 1 THEN ' Mouse or arrow back up to highlighted line above.
  364.             GOSUB getcolor: PRINT MID$(x$(x1% + scr), x2%, ABS(d2% - markcol%)); ' Highlight from cursor to previously highlighted text to the right, if any.
  365.             GOSUB getcolor: PRINT MID$(x$(x1% + scr), x2% + ABS(d2% - markcol%)); ' Unhighlight previous text to the right.
  366.         ELSE ' Arrow left, arrow up or mouse upwards, either without any highlighting above.
  367.             GOSUB getcolor: PRINT MID$(x$(x1% + scr), x2%, ABS(o1% - d1%) * dwidth + ABS(o2% - d2%)); ' Trick method to determine length of highlighting.
  368.         END IF
  369.  
  370.         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.
  371.             x1% = o1%: x2% = o2%: LOCATE margin.t + x1%, margin.l + 1
  372.             IF x1% = markrow% THEN
  373.                 IF markcol% >= d2% THEN i% = d2% ELSE i% = markcol%
  374.                 GOSUB getcolor: PRINT MID$(x$(x1% + scr), 1, i% - 1);
  375.                 GOSUB getcolor: PRINT MID$(x$(x1% + scr), i%, ABS(i% - d2%));
  376.             ELSE
  377.                 GOSUB getcolor: PRINT MID$(x$(x1% + scr), 1, d2% - 1);
  378.             END IF
  379.         END IF
  380. COLOR c1%, c2%
  381. LOCATE yy%, xx%
  382.  
  383.  
  384. getcolor: ' Reads the screen under the cursor and reverses the colors.
  385. IF SCREEN(CSRLIN, POS(0), 1) <> 7 THEN COLOR c1%, c2% ELSE COLOR h1%, h2%
  386.  
  387. clearmarkers:
  388. ' Clear most variables used in highlighting process. Some others are non-essential to conditions.
  389. mark% = 0: o% = 0: mhl% = 0: mhl1% = 0: mhl1col% = 0: mhl1row% = 0: oldmhl1row% = 0: mhlclear% = 0
  390. cutdrow% = 0
  391.  
  392. debugger:
  393. ss% = CSRLIN: tt% = POS(0)
  394. LOCATE 1, 52
  395. PRINT "mark% = "; mark%; markrow%; markcol%; "   "
  396. LOCATE , 52
  397. PRINT "origin ="; o%; o1%; o2%; "   "
  398. LOCATE , 52
  399. PRINT "dest =  "; d%; d1%; d2%
  400. IF mov% > 0 THEN mov$ = "pos" ELSE IF mov% < 0 THEN mov$ = "neg" ELSE mov$ = "neutral"
  401. LOCATE , 52
  402. PRINT "moving   "; mov$; "   "
  403. LOCATE , 52
  404. PRINT "o2 - d2 ="; ABS(o1% - d1%); ABS(o2% - d2%); "   "
  405. LOCATE , 52
  406. PRINT "yy%   col"; yy%; xx%; "   "
  407. LOCATE , 52
  408. PRINT "yy% * col"; (yy% - 1) * dwidth + col; "   "
  409. LOCATE , 52
  410. PRINT "cutmrow% "; cutmrow%; cutmcol%; "   "
  411. LOCATE , 52
  412. PRINT "cutdrow% "; cutdrow%; cutdcol%; "   "
  413. LOCATE , 52
  414. PRINT "i j scr  "; i%; j%; scr; "   "
  415. LOCATE , 52
  416. PRINT "j1% k%   "; j1%; k%; "   "
  417. LOCATE , 52
  418. PRINT "row col  "; row; col; "   "
  419. LOCATE , 52
  420. PRINT "h%       "; h%; "   "
  421. LOCATE , 52
  422. PRINT "mhl1%    "; mhl1%; "   "
  423. LOCATE , 52
  424. PRINT "oldmhl1  "; oldmhl1row%; "   "
  425. LOCATE , 52
  426. PRINT "mhl1row% "; mhl1row%; "   "
  427. LOCATE , 52
  428. PRINT "mhl1col% "; mhl1col%; "   "
  429. LOCATE ss%, tt%
  430.  
  431. redomarkers:
  432. mark% = mark% - i% * dwidth
  433. markrow% = markrow% - i%
  434. d% = (CSRLIN - margin.t - 1 - i%) * dwidth + POS(0) - margin.l
  435.  
  436. IF mhl1% THEN
  437.     mhl1% = mhl1% - i% * dwidth
  438.     mhl1row% = mhl1row% - i%
  439.     oldmhl1row% = oldmhl1row% - i%
  440. IF debug% THEN GOSUB debugger

Pete

Edit: Added scrolling to highlighting.

Edit: Added middle wheel scroll when SHIFT key is held down and modified algorithm to allow for mixed text row lengths.
« Last Edit: November 24, 2020, 01:43:54 am by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

FellippeHeitor

  • Guest
Very fluid, Pete!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
CHR$(3)

I would imagine you have made similar routines, but possibly not in SCREEN 0? I sometimes get the urge to try routines like this in graphics mode, but then I sober up, and get back to business as usual.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

FellippeHeitor

  • Guest
I've had a working prototype of a graphical multiline textbox (still hidden in current InForm version, but inaccessible if you don't know where it is). Not having been able to sort a few issues had me forever postponing releasing it.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Well maybe seeing this little routine will help you to revisit your graphics model. These types of routines seem simple, when we use them in apps like Notepad, buggy as even it is, but coding them is surprisingly challenging. Now if you will excuse me, these three holes in the wall, I punched out yesterday, aren't going to "patch" themselves.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
One thing to watch for Pete:

    WHILE _MOUSEINPUT
        my% = _MOUSEY
        mx% = _MOUSEX
        lb% = _MOUSEBUTTON(1)
    WEND
 

The above may adversely affect run speeds for you.  My suggestion:

    WHILE _MOUSEINPUT: WEND
        my% = _MOUSEY
        mx% = _MOUSEX
        lb% = _MOUSEBUTTON(1)

Why assign and then reassign those values countless times inside a loop, when you’re only going to exit that loop with the final results??

The way you have it, slower PCs might lag up clearing the mouse input buffer — especially if the mouse moved repeatedly during a SLEEP, INPUT, or _DELAY segment of code.
 
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Funnily enough, I had it the way you posted, to begin with. I changed it when having one of the mouse calls not function, until I included it within the WHILE loop. I might go back and see if that was resolved, on this initial completed routine. If so, you bet, I'd love to have it back to the : WHILE _MOUSEINPUT: WEND. I'll have a look see in a couple of minutes, and get back to the thread...

Thanks,

Pete

Edit: Aha!, Now it will work with the preferred method. It's a head scratcher what went wrong in a much earlier unfinished version.  Thanks Steve!
« Last Edit: November 11, 2020, 06:06:44 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Nice snippet.  I'm saving it.  Thanks for posting.

- Dav

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Thanks Dav. I edited the original post tonight, to add scrolling while highlighting.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
I've been using the wrong mouse input method too.
Yet the cat didn't get fat (catching the slow mouse).
It works better if you plug it in.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Thanks Dav. I edited the original post tonight, to add scrolling while highlighting.

Pete

@Pete  When you say you added scrolling, do you mean with _MOUSEWHEEL?

If so, did you edit with correct code?

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
To clarify what I meant by scrolling, if you hold the left mouse button key down, and start scrolling down, past the last line of text, the screen will scroll, and the highlighting will remain.

Now the scroll wheel was something I added to the scrollbar snippet, but it is not included in this highlighting snippet. I just never highlight with the mouse wheel, but I checked, and I see if the left mouse button is held down while the wheel is used, it does indeed highlight text in other apps. I guess Shift + scroll wheel doesn't, because the mouse cursor is not set in place, as when the mouse button is depressed. So if i added it, it would apparently only be in conjunction with a left mouse button hold. I might consider that, if it doesn't involve any re-writing to accomplish it.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Shift + left-down + scroll will usually keep highlighting as you scroll, in most MS programs.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Quote
To clarify what I meant by scrolling, if you hold the left mouse button key down, and start scrolling down, past the last line of text, the screen will scroll, and the highlighting will remain.

Yes that works well! Wasn't it doing that originally? Oh wait, the whole screen now too, nice! Thanks

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Well this cake is done. I added mouse wheel highlighting. Just hold down a shift key and roll! The text row length varies from line to line, which did require some significant changes to the highlighting algorithm, and was necessary for any practical application. Highlighting while scrolling is also possible when the left mouse button is held with the mouse just above or below the first or last text rows. Mouse highlighting remains active even if the mouse is outside the text boundaries.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/