Recent

Author Topic: Tmemo scrolls to the left with no reason  (Read 2830 times)

fedkad

  • Full Member
  • ***
  • Posts: 176
Tmemo scrolls to the left with no reason
« on: September 13, 2020, 04:29:28 pm »
Create a Tmemo with the size of say 300 by 300 pixels. Enter two lines at design time into the memo; the first line with 10 characters, the second line with 100 characters.

Programmatically run the following code (for example by assigning to a Button's onClick event):

Code: Pascal  [Select][+][-]
  1. Memo1.SelStart := 4;

The memo will scroll to the left so that the caret (or the fifth character of the first line) goes to the leftmost edge of the memo.

There is no reason for the memo to scroll, because the whole first line fits in the memo's width. This does not happen in Windows. It happens only in Linux.

I tried also the following with no difference:

Code: Pascal  [Select][+][-]
  1. Memo1.Lines.BeginUpdate;
  2. try
  3.   Memo1.SelStart := 4;
  4. finally
  5.   Memo1.Lines.EndUpdate;
  6. end;

How can I prevent the unnecessary scrolling, so that Lazarus Memo behaves the same as in Windows?

Note that, I do want to put the caret after the 4th character position, but I do not want the Memo to scroll unnecessarily, unless the caret goes out of view.
Lazarus 2.2.6 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Tmemo scrolls to the left with no reason
« Reply #1 on: September 13, 2020, 04:55:09 pm »
You need to specify the INTERFACE you are using GTK or QT?

I think you should report this as a bug because that does appear to be not proper.

It may also have something to do with WordWrap feature too.. But those interfaces implement the Memo in their own way so I guess they don't behave the same.
The only true wisdom is knowing you know nothing

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: Tmemo scrolls to the left with no reason
« Reply #2 on: September 13, 2020, 05:13:56 pm »
An alternative might be setting a value in CaretPos instead of using SelStart.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

fedkad

  • Full Member
  • ***
  • Posts: 176
Re: Tmemo scrolls to the left with no reason
« Reply #3 on: September 13, 2020, 06:14:07 pm »
@jamie

It is the official Lazarus IDE Version #: 2.0.10 / x86 64-linux-qtk2. Several previous versions had the same problem too.

@dsiders

When I replace Memo1.SelStart:=4; with Memo1.CaretPos:=point(4,0); the result is exactly the same.
« Last Edit: January 23, 2022, 01:41:01 pm by fedkad »
Lazarus 2.2.6 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Tmemo scrolls to the left with no reason
« Reply #4 on: September 13, 2020, 07:02:32 pm »
Hi!

It seems to be buggy:

Even this workaround fails:
Setting the horizontal scrollBar position does not work.
Code: Pascal  [Select][+][-]
  1. Memo1.HorzScrollBar.Position := 0;
  2.  
This does not work.
The scroll position stays unchanged hiding the first 5 letters.

Winni

Also Laz 2.010 gtk2, Lin64

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Tmemo scrolls to the left with no reason
« Reply #5 on: September 13, 2020, 07:39:07 pm »
I know the implementation of the Tmemo in other targets does not match the behavior of Windows. Its been known for some time now..

 There are a couple of pit falls in the windows version that are not likable so those pitfalls were not implemented in Linux targets however, it makes code non-compatible when doing this..

 From what I know about the history of Lazarus (LCL) it is suppose to mimic windows/Delphi which etc.

 But it does not.

 case example.

 Lines in the Lines property in windows do not match the actual view of the control if there are wrap arounds taking place so that means different steps are needed to actual figure that out.

 In Linux The wrap around of lines shows as an new line, not the same part of the previous line and so on.

 So I believe maybe that code in the Linux side should be fixed.
The only true wisdom is knowing you know nothing

fedkad

  • Full Member
  • ***
  • Posts: 176
Re: Tmemo scrolls to the left with no reason
« Reply #6 on: September 17, 2020, 06:27:18 pm »
Hi!

It seems to be buggy:

Even this workaround fails:
Setting the horizontal scrollBar position does not work.
Code: Pascal  [Select][+][-]
  1. Memo1.HorzScrollBar.Position := 0;
This does not work.
The scroll position stays unchanged hiding the first 5 letters.

Winni

Also Laz 2.010 gtk2, Lin64

It does move though, when there are two actions on HorzScrollBar. Try the following code:

Code: Pascal  [Select][+][-]
  1. Memo1.HorzScrollBar.Position := 1;
  2. Memo1.HorzScrollBar.Position := 0;

This is of course another bug (I think), which may be easier to address.



I filed a bug report for my problem at https://bugs.freepascal.org/view.php?id=37764

FYI
Lazarus 2.2.6 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

fedkad

  • Full Member
  • ***
  • Posts: 176
Re: Tmemo scrolls to the left with no reason
« Reply #7 on: August 25, 2021, 12:32:44 pm »
Failing to find another solution, I developed the following procedure, which is only a rough approximation of what is expected:

Code: Pascal  [Select][+][-]
  1. procedure FixScrollError (const mem : Tmemo);
  2. {$ifdef Linux}
  3. // Only for the Linux bug
  4. var
  5.   x, y : Integer;
  6.   bmp : Graphics.TBitmap; // Graphics.TBitmap, not Windows.TBitmap
  7.   str : String = 'x';
  8. {$endif}
  9. begin
  10. {$ifdef Linux}
  11.   bmp := Graphics.TBitmap.Create;
  12.   try
  13.     bmp.Canvas.Font.Assign(mem.font);
  14.     x := bmp.Canvas.TextWidth(str)  * mem.caretpos.X;
  15.     y := bmp.Canvas.TextHeight(str) * mem.caretpos.Y;
  16.   finally
  17.     bmp.Free;
  18.   end;
  19.   //writeln (x:8, y:8, mem.ClientWidth:8, mem.ClientHeight:8);
  20.   Application.ProcessMessages;
  21.   mem.Scrollby(x,y);
  22.   mem.Scrollby(-x+(mem.ClientWidth div 2), -y+(mem.ClientHeight div 2))
  23. {$endif}
  24.   ;
  25. end;  // FixScrollError  
  26.  

I have to call it in relevant places, like after setting the SelText and SelStart methods of a "memo".

Please, feel free to comment on this!
Lazarus 2.2.6 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

fedkad

  • Full Member
  • ***
  • Posts: 176
Re: Tmemo scrolls to the left with no reason
« Reply #8 on: January 23, 2022, 01:51:53 pm »
Same behavior in Lazarus Version 2.2.0.
Lazarus 2.2.6 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

 

TinyPortal © 2005-2018