Recent

Author Topic: Selecting Memo lines drifts selection  (Read 777 times)

munair

  • Hero Member
  • *****
  • Posts: 884
  • compiler developer @SharpBASIC
    • SharpBASIC
Selecting Memo lines drifts selection
« on: May 15, 2025, 12:57:29 pm »
Selecting a line with the following code in the OnKeyUp event of a Memo drifts the selection past the beginning of each line > 0. So I adjusted the line break offset to -2 and the line length offset to -1. First I thought this was a Linux problem due to line ending differences with Windows (#10 vs #13#10), but the code seems to fix the same problem on both Linux (Debian 12/GTK2+) and Windows (7).

Code: Pascal  [Select][+][-]
  1.   // Get current line number
  2.   Line := Memo.CaretPos.Y;
  3.  
  4.   // prevent selection drift (both Windows and Linux)
  5.   LineBreakOffset := Length(sLineBreak) - 2;
  6.   LineLengthOffset := -1;
  7.  
  8.   // Calculate the starting character position of the line
  9.   LineStart := 0;
  10.   for I := 0 to Line - 1 do
  11.     LineStart := LineStart + Length(Memo.Lines[I]) + LineBreakOffset;
  12.  
  13.   // Get the length of the current line
  14.   LineLength := Length(Memo.Lines[Line]) + LineLengthOffset;
  15.   if LineLength < 0 then
  16.     LineLength := 0; // Prevent negative length
  17.  
  18.   // Select the line
  19.   Memo.SelStart := LineStart;
  20.   Memo.SelLength := LineLength;
  21.   Memo.SetFocus;

This seems more like an LCL issue.
« Last Edit: May 15, 2025, 01:00:16 pm by munair »
It's only logical.

ASerge

  • Hero Member
  • *****
  • Posts: 2423
Re: Selecting Memo lines drifts selection
« Reply #1 on: May 16, 2025, 10:49:32 pm »
Code: Pascal  [Select][+][-]
  1. procedure SelectLine(AMemo: TMemo; ALine: Integer);
  2. var
  3.   iEnd: Integer;
  4. begin
  5.   AMemo.CaretPos := Point(0, ALine + 1);
  6.   iEnd := AMemo.SelStart;
  7.   AMemo.CaretPos := Point(0, ALine);
  8.   AMemo.SelLength := iEnd - AMemo.SelStart;
  9. end;

munair

  • Hero Member
  • *****
  • Posts: 884
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Selecting Memo lines drifts selection
« Reply #2 on: May 16, 2025, 11:12:22 pm »
Line is undefined upon entry. Here is the procedure, which incorrectly assumed a difference between Linux and Windows.

Code: Pascal  [Select][+][-]
  1. function SelectMemoLine(Memo: TMemo; key: word = 0): string;
  2. var
  3.   Line: Integer;
  4.   LineStart: Integer;
  5.   LineLength: Integer;
  6.   I: Integer;
  7.   LineBreakOffset: Integer;
  8.   LineLengthOffset: Integer;
  9. begin
  10.  
  11.   result := '';
  12.  
  13.   if key = VK_DOWN then
  14.     Memo.CaretPos := Point(0, Memo.CaretPos.Y - 1);
  15.  
  16.   // Get the current line number from CaretPos
  17.   Line := Memo.CaretPos.Y;
  18.  
  19.   // Ensure the line number is valid
  20.   if (Line < 0) or (Line >= Memo.Lines.Count) then
  21.     Exit;
  22.  
  23.   // Platform-specific adjustments (no use; Linux settings must also be applied to Windows)
  24.   {$IFDEF LINUX}
  25.   LineBreakOffset := Length(sLineBreak) - 2;
  26.   LineLengthOffset := -1;
  27.   {$ELSE}
  28.   LineBreakOffset := Length(sLineBreak);
  29.   LineLengthOffset := 0;
  30.   {$ENDIF}
  31.  
  32.   // Calculate the starting character position of the line
  33.   LineStart := 0;
  34.   for I := 0 to Line - 1 do
  35.     LineStart := LineStart + Length(Memo.Lines[I]) + LineBreakOffset;
  36.  
  37.   // Get the length of the current line
  38.   LineLength := Length(Memo.Lines[Line]) + LineLengthOffset;
  39.   if LineLength < 0 then
  40.     LineLength := 0; // Prevent negative length
  41.  
  42.   // Select the line
  43.   Memo.SelStart := LineStart;
  44.   Memo.SelLength := LineLength;
  45.   Memo.SetFocus;
  46.  
  47.   result := Memo.Lines[Line];
  48. end;
« Last Edit: May 17, 2025, 10:08:00 pm by munair »
It's only logical.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6005
  • Compiler Developer
Re: Selecting Memo lines drifts selection
« Reply #3 on: May 17, 2025, 06:30:16 pm »
Line is undefined upon entry. Here is the procedure, which incorrectly assumed a difference between Linux and Windows.

Please use [code=pascal][/code]-tags as otherwise the forum software will interpret your code and also it improves readability.

munair

  • Hero Member
  • *****
  • Posts: 884
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Selecting Memo lines drifts selection
« Reply #4 on: May 17, 2025, 10:08:20 pm »
Line is undefined upon entry. Here is the procedure, which incorrectly assumed a difference between Linux and Windows.

Please use [code=pascal][/code]-tags as otherwise the forum software will interpret your code and also it improves readability.

Apologies, I corrected it.
It's only logical.

 

TinyPortal © 2005-2018