Recent

Author Topic: TMemo scrolls up when something is assigned to its SelText property  (Read 16172 times)

fedkad

  • Full Member
  • ***
  • Posts: 179
I have posted a reply to an old thread (http://forum.lazarus.freepascal.org/index.php/topic,9643.0.html) which is related to my problem, but the area where that thread was initially posted is not correct, so I repost my question here.

On a long memo (of type TMemo) which has more data that can fit on its viewable area, when the vertical scroll bar is at the top position (that is, at the top of the memo's display area you see its first line) and you make something like Memo.SelText:='blabla'; there is no problem. If you make the same when the scroll bar is not at the top, the scroll bar moves to the top regardless of the position of the SelStart point, which in some cases moves out of the scope.

If you use a workaround like Memo.SelStart:=Memo.SelStart; the scrollbar still tries to move to the top, but this movement is as much as the selection point stays in view, but is displayed at the bottom (last line) of the viewable area of the memo.

Normally no movement should occur, as in Delphi.
« Last Edit: September 08, 2016, 09:30:27 am by fedkad »
Lazarus 4.6 / FPC 3.2.2 on latest amd/arm_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

ASerge

  • Hero Member
  • *****
  • Posts: 2503
Re: TMemo scrolls up when something is assigned to its SelText property
« Reply #1 on: September 04, 2016, 07:38:01 am »
Delphi use native Windows command EM_REPLACESEL. But Lazarus use common code with SelStart, which need addtitional step on Windows:
Code: Pascal  [Select][+][-]
  1. SendMessage(Memo1.Handle, EM_SCROLLCARET, 0, 0);

fedkad

  • Full Member
  • ***
  • Posts: 179
Re: TMemo scrolls up when something is assigned to its SelText property
« Reply #2 on: September 04, 2016, 02:56:02 pm »
Thank you ASerge for your response.

I tested with SendMessage(Memo.Handle, EM_SCROLLCARET, 0, 0) as you suggested; but the result was similar to Memo.SelStart:=Memo.SelStart that I described before: the scrollbar still tries to move to the top, but this movement is as much as the selection point stays in view, but is displayed at the bottom (last line) of the viewable area of the memo.

I other words:

Code: Pascal  [Select][+][-]
  1.   Memo.SelText := 'blabla';
  2.   Memo.SelStart := Memo.SelStart; // to prevent memo scrolling up

and

Code: Pascal  [Select][+][-]
  1.   Memo.SelText := 'blabla';
  2.   SendMessage(Memo.Handle, EM_SCROLLCARET, 0, 0);

behave similarly.

Any other ideas?
Lazarus 4.6 / FPC 3.2.2 on latest amd/arm_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

ASerge

  • Hero Member
  • *****
  • Posts: 2503
Re: TMemo scrolls up when something is assigned to its SelText property
« Reply #3 on: September 04, 2016, 07:53:43 pm »
I test simple in new project:
Add Memo, set ScrollBar = sbVertical, add many lines to Lines
Add Button and fill OnClick by
Code: Pascal  [Select][+][-]
  1. Memo.SelText := 'blabla';
  2. SendMessage(Memo.Handle, EM_SCROLLCARET, 0, 0);
Run app, move cursor in memo so, that top lines of text not visible. Click button - new blabla added without any problem, text stay in view and top lines not visible.
May be i don't understand you?

fedkad

  • Full Member
  • ***
  • Posts: 179
Re: TMemo scrolls up when something is assigned to its SelText property
« Reply #4 on: September 05, 2016, 09:16:47 am »
Please, while doing this test move the scroll bar so that the insertion point stays somewhere in the middle of the viewable area. Ensure that there are many lines above and below the viewable area. Then click the button.

Normally, no movement should occur (assuming that you insert a single-line short string as in the example). But, in Lazarus you will see an upward movement in the scroll bar so that the insertion point (the newly inserted text) goes to the bottom of the viewable area.

Thank you.

Note: Compare this by doing another test: Do as explained in the first paragraph, but instead of clicking the button, now try to paste a short string ('blabla') using Ctrl+V. The memo box behaves normally (no scroll bar movement).
« Last Edit: September 05, 2016, 09:21:32 am by fedkad »
Lazarus 4.6 / FPC 3.2.2 on latest amd/arm_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

ASerge

  • Hero Member
  • *****
  • Posts: 2503
Re: TMemo scrolls up when something is assigned to its SelText property
« Reply #5 on: September 05, 2016, 07:27:59 pm »
OK. My solution is not good, because after two steps cursor move to top, and then back, but only to position of cursor (not full back). So text is scrolled (not scrolled, if it's last visible line).
But on Windows you can use Delphi solution. Add this code and use later SelStart as is:
Code: Pascal  [Select][+][-]
  1. {$IFDEF WINDOWS}
  2. type
  3.   TDelphiMemo = class helper for TMemo
  4.   strict private
  5.     function GetSelText: string;
  6.     procedure SetSelText(const Text: string);
  7.   public
  8.     property SelText: string read GetSelText write SetSelText;
  9.   end;
  10.  
  11. function TDelphiMemo.GetSelText: string;
  12. begin
  13.   Result := inherited;
  14. end;
  15.  
  16. procedure TDelphiMemo.SetSelText(const Text: string);
  17. begin
  18.   {$PUSH}
  19.   {$WARN 4055 OFF Hint: Conversion between ordinals and pointers is not portable}
  20.   SendMessage(Handle, EM_REPLACESEL, -1, LPARAM(PChar(Text)));
  21.   {$POP}
  22. end;              
  23. {$ENDIF WINDOWS}            
  24.  

fedkad

  • Full Member
  • ***
  • Posts: 179
Re: TMemo scrolls up when something is assigned to its SelText property
« Reply #6 on: September 06, 2016, 12:02:43 pm »
Thank you very much for your effort!

Yes, I am on Windows; but I wonder if there is any drawback of your workaround besides portability...

Thanks again.
Lazarus 4.6 / FPC 3.2.2 on latest amd/arm_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

ASerge

  • Hero Member
  • *****
  • Posts: 2503
Re: TMemo scrolls up when something is assigned to its SelText property
« Reply #7 on: September 06, 2016, 05:37:35 pm »
...if there is any drawback of your workaround besides portability...
Only additional code. And, by the way, portability is protected by {$IFDEF WINDOWS}

fedkad

  • Full Member
  • ***
  • Posts: 179
Re: TMemo scrolls up when something is assigned to its SelText property
« Reply #8 on: September 07, 2016, 12:44:08 pm »
Thank you for the clarification. I found a problem though.

When the pasted string contains non-ASCII (UTF-8 encoded) characters, then wrong data is displayed in the memo.

For example:

Code: Pascal  [Select][+][-]
  1. Memo.SelText := 'ÇÇÇ';

will result in the following being inserted to the memo:

Code: Text  [Select][+][-]
  1. ÇÇÇ

Lazarus 4.6 / FPC 3.2.2 on latest amd/arm_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

Bart

  • Hero Member
  • *****
  • Posts: 5731
    • Bart en Mariska's Webstek
Re: TMemo scrolls up when something is assigned to its SelText property
« Reply #9 on: September 07, 2016, 03:34:53 pm »
On a long memo (of type TMemo) which has more data that can fit on its viewable area, when the vertical scroll bar is at the top position (that is, at the top of the memo's display area you see its first line) and you make something like Memo.SelText:='blabla'; there is no problem. If you make the same when to scroll bar is not at the top, the scroll bar moves to the top regardless of the position of the SelStart point, which in some cases moves out of the scope.

There already was a bugreport about this (http://bugs.freepascal.org/view.php?id=8665) which was closed as "duplicate" (and so was it's duplicate entry).
I re-opened the bugreport and attached a demo project.

Bart

ASerge

  • Hero Member
  • *****
  • Posts: 2503
Re: TMemo scrolls up when something is assigned to its SelText property
« Reply #10 on: September 07, 2016, 06:14:20 pm »
When the pasted string contains non-ASCII (UTF-8 encoded) characters, then wrong data is displayed in the memo.
Next patch: SendMessageW(Handle, EM_REPLACESEL, -1, LPARAM(PUnicodeChar(UnicodeString(Text))));

fedkad

  • Full Member
  • ***
  • Posts: 179
Re: TMemo scrolls up when something is assigned to its SelText property
« Reply #11 on: September 08, 2016, 09:28:58 am »
Next patch: SendMessageW(Handle, EM_REPLACESEL, -1, LPARAM(PUnicodeChar(UnicodeString(Text))));

Many thanks!
Lazarus 4.6 / FPC 3.2.2 on latest amd/arm_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

fedkad

  • Full Member
  • ***
  • Posts: 179
Re: TMemo scrolls up when something is assigned to its SelText property
« Reply #12 on: September 08, 2016, 09:30:09 am »
There already was a bugreport about this (http://bugs.freepascal.org/view.php?id=8665) which was closed as "duplicate" (and so was it's duplicate entry).
I re-opened the bugreport and attached a demo project.

Bart

Bart. Thank you!

I do think that this must be considered as a bug and (hopefully) fixed.
Lazarus 4.6 / FPC 3.2.2 on latest amd/arm_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

Bart

  • Hero Member
  • *****
  • Posts: 5731
    • Bart en Mariska's Webstek
Re: TMemo scrolls up when something is assigned to its SelText property
« Reply #13 on: September 08, 2016, 12:12:41 pm »
I do think that this must be considered as a bug and (hopefully) fixed.

Yes, it's a bug.

Bart

fedkad

  • Full Member
  • ***
  • Posts: 179
Re: TMemo scrolls up when something is assigned to its SelText property
« Reply #14 on: November 28, 2016, 05:26:35 pm »
I would like to inform you that this problem still persists in Lazarus version 1.6.2 (Win64). I think it is not fixed yet.
Lazarus 4.6 / FPC 3.2.2 on latest amd/arm_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

 

TinyPortal © 2005-2018