Setting TMemo.VertScrollBar.Position does not scroll the text field lazarus / 31-LCL ---------------------------------------------------------------------- henrik · 2015-11-22 01:36 This issue has been brought up multiple times over the past many years, but none of the suggested solutions seem to work. Therefore I will bring it up again and add as detailed info as possible, in the hope that someone will be able to reproduce it and find a real solution. I need to scroll the text in a TMemo automatically. To do this I set the value of TMemo.VertScrollBox.Position. Setting Position moves the thumb of the ScrollBar, but the text does not scroll. I am using Lazarus version 1.4.2, FreePascal version 2.6.4, and I am compiling and executing the program on Windows 8 ver 6.3. Please note that I have tried this on earlier versions of Lazarus and Windows with the same result. I have not tried it on Linux, so it might be a Windows only problem. Here is a quick program which shows the problem: Create a new application. Add a Tmemo and a TButton on the form. Set the TMemo.ScrollBars property to ssAutoVertical. Create a OnPress handler for the TButton. Create a OnCreate handler for the TForm. Enter this code in the two handlers: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin  Memo1.VertScrollBar.Positi on:=Memo1.VertScrollBar.Position+1;end; procedure TForm1.FormCreate(Sender: TObject);var  i:integer;begin  for i:=1 to 99 do    Memo1.Lines.Add(format('Line %d', [i]));end;  Now run the program, and press the button a number of times. If it works like it does for me, then you will notice that the thumb of the scroll bar moves down, but the text doesn't scroll. Now move the mouse over the thumb, and give it a click, you will now see that the text jumps to the line which matches the scroll bar position. The attached png shows two screen shots of the program. On the left screen shot I pressed the button 46 times, so the thumb is half way down the scroll bar, but the text field still shows the top line. On the right screen shot the scroll bar is still at position 46, and I have clicked the thumb with the mouse. Now the text field is aligned with the scroll bar and top line is line 46. I really hope someone can fix this, or come up with a decent workaround, because my current workaround is a bit of a pain. ---------------------------------------------------------------------- balazsszekely · 2015-11-22 07:16 Confirmed! You should fill a bug report here: http://bugs.freepascal.org/main_page.php. Please attach a small demo application(zip), which reproduce the problem. Until the bug it's fixed, use the following code as a workaround: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  SendMessage(Memo1.Handle, WM_VSCROLL, SB_LINEDOWN, 0);   Just don't forget to add windows to the uses clauses. It should be the first in the list. ---------------------------------------------------------------------- henrik · 2015-11-22 11:26 Thank you GetMem I have now filed a bug-report, as you suggested. In my real application I need random access to up to 4000 lines, so I expanded your workaround a bit and ended up with this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   newPos:=Memo1.VertScrollBar.Position+10;   {$ifdef WINDOWS}  SendMessage(Memo1.Handle, EM_LINESCROLL, 0, newPos - Memo1.VertScrollBar.Position);  {$else}  Memo1.VertScrollBar.Position:=newPos;  {$endif} ---------------------------------------------------------------------- balazsszekely · 2015-11-22 19:52 QuoteI have now filed a bug-report, as you suggestedThank you! Unfortunately it's not working under linux(GTK2) and OSX either. ---------------------------------------------------------------------- Bart · 2015-11-22 23:01 Does it (setting scrollbar position) work in Delphi? Bart ---------------------------------------------------------------------- balazsszekely · 2015-11-23 05:31 @Bart Delphi does not have a HorzScrollBar/VertScrollBar property, so setting the Position it's not possible directly, only through SendMessage/Perfrom.  However Memo1.VertScrollBar.Position(Lazarus) it should work in my opinion. Place a large enough image inside a TScrollBox, then change the position of the scrollbar, the images is scrolled nicely(see attachment). ---------------------------------------------------------------------- JuhaManninen · 2015-11-23 14:19 See my note in  http://bugs.freepascal.org/view.php?id=29067 ---------------------------------------------------------------------- zeljko · 2015-11-23 14:24 Quote from: JuhaManninen on November 23, 2015, 03:19:36 pmSee my note in  http://bugs.freepascal.org/view.php?id=29067 GetScrollinfo() ,SetScrollinfo() from lcl winapi does not work ? ---------------------------------------------------------------------- JuhaManninen · 2015-11-23 16:44 Quote from: zeljko on November 23, 2015, 03:24:28 pmGetScrollinfo() ,SetScrollinfo() from lcl winapi does not work ? No. Actually SetScrollinfo is already called at the end of TControlScrollBar.SetPosition. Even this does not work : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMemoScrollbar.SetControlPosition;begin  if Kind = sbVertical then    TCustomMemo(FControl).ScrollBy(0, FPosition - FPrevPosition)  else    TCustomMemo(FControl).ScrollBy(FPosition - FPrevPosition, 0);end;  This is frustrating. I fail almost always when I try to fix something in LCL. Can you please look at it. ---------------------------------------------------------------------- balazsszekely · 2015-11-23 17:09 @Juha I just test this and it's not working either: Memo1.ScrollBy(0, 4) With delphi it's working fine. PS: It seems that there are more then one issue with TMemo. ---------------------------------------------------------------------- balazsszekely · 2015-11-24 11:21 @Juha I studied the code today and I found the following: 1. The relevant scrolling code it's widgetset dependent 2.  It's located in the following units: a. Windows(Win32WSForms.pp) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function ScrollWindowPtr(hWnd:HWND; XAmount:longint; YAmount:longint; lpRect: pointer; lpClipRect: pointer):WINBOOL; stdcall; external 'user32' name 'ScrollWindow'; class procedure TWin32WSScrollingWinControl.ScrollBy(  const AWinControl: TScrollingWinControl; const DeltaX, DeltaY: integer);begin  if Windows.IsWindowVisible(AWinControl.Handle) then  ScrollWindowPtr(AWinControl.Handle, DeltaX, DeltaY, nil, nil);end;    b. Carbon(CarbonWSForms.pp) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i class procedure TCarbonWSScrollingWinControl.ScrollBy(const AWinControl: TScrollingWinControl; const DeltaX, DeltaY: integer);begin  if not CheckHandle(AWinControl, Self, 'ScrollBy') then Exit;   TCarbonWidget(AWinControl.Handle).ScrollBy(DeltaX, DeltaY);  AWinControl.Invalidate;end; c. GTK2(GTK2WSForms.pp) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i class procedure TGtk2WSScrollingWinControl.ScrollBy(  const AWinControl: TScrollingWinControl; const DeltaX, DeltaY: integer);var  Scrolled: PGtkScrolledWindow;  Adjustment: PGtkAdjustment;  h, v: Double;  NewPos: Double;begin  if not AWinControl.HandleAllocated then exit;  Scrolled := GTK_SCROLLED_WINDOW({%H-}Pointer(AWinControl.Handle));  if not GTK_IS_SCROLLED_WINDOW(Scrolled) then    exit;  Adjustment := gtk_scrolled_window_get_hadjustment(Scrolled);  if Adjustment <> nil then  begin    h := gtk_adjustment_get_value(Adjustment);    NewPos := Adjustment^.upper - Adjustment^.page_size;    if h - DeltaX <= NewPos then      NewPos := h - DeltaX;    gtk_adjustment_set_value(Adjustment, NewPos);  end;  Adjustment := gtk_scrolled_window_get_vadjustment(Scrolled);  if Adjustment <> nil then  begin    v := gtk_adjustment_get_value(Adjustment);    NewPos := Adjustment^.upper - Adjustment^.page_size;    if v - DeltaY <= NewPos then      NewPos := v - DeltaY;    gtk_adjustment_set_value(Adjustment, NewPos);  end;  AWinControl.Invalidate;end;                         d. ...and so on 3. It's triggered from WSForms.pp 4. The code is only implemented for TScrollingWinControl but it also works for TWinControls(TMemo for example), I tested this by moving the relevant code in a separate unit. 5. The implementation would be a non-trivial one,  besides there is a workaround(see above). ---------------------------------------------------------------------- zeljko · 2015-11-24 11:43 Quote from: JuhaManninen on November 23, 2015, 05:44:24 pmQuote from: zeljko on November 23, 2015, 03:24:28 pmGetScrollinfo() ,SetScrollinfo() from lcl winapi does not work ? No. Actually SetScrollinfo is already called at the end of TControlScrollBar.SetPosition. Even this does not work : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMemoScrollbar.SetControlPosition;begin  if Kind = sbVertical then    TCustomMemo(FControl).ScrollBy(0, FPosition - FPrevPosition)  else    TCustomMemo(FControl).ScrollBy(FPosition - FPrevPosition, 0);end;  This is frustrating. I fail almost always when I try to fix something in LCL. Can you please look at it. It works with qt-lcl Just try example provided with that issue ---------------------------------------------------------------------- JuhaManninen · 2015-11-24 14:49 Quote from: zeljko on November 24, 2015, 12:43:52 pmIt works with qt- lcl Just try example provided with that issue It works because SetScrollinfo is called at the end of TControlScrollBar.SetPosition, and for some reason that is enough with QT-LCL. It is not enough with other widgetsets. TCustomMemo.ScrollBy is not implemented. Calling it makes no difference. I tried to implement it using the SendMessage() workarounds but they only work on Windows. According to GetMem the implementation in TScrollingWinControl works also for TWinControl. Maybe there should be a widgetset TScrollingHelper class which is used by TScrollingWinControl and TCustomMemo and maybe others. Does somebody want to implement it? It is non-trivial yet it is pure refactoring and the chance for success is high. I can try it later (not now) if nobody else will. ---------------------------------------------------------------------- zeljko · 2015-11-24 15:33 Quote from: JuhaManninen on November 24, 2015, 03:49:38 pmQuote from: zeljko on November 24, 2015, 12:43:52 pmIt works with qt-lcl Just try example provided with that issue It works because SetScrollinfo is called at the end of TControlScrollBar.SetPosition, and for some reason that is enough with QT-LCL. It is not enough with other widgetsets. TCustomMemo.ScrollBy is not implemented. Calling it makes no difference. I tried to implement it using the SendMessage() workarounds but they only work on Windows. According to GetMem the implementation in TScrollingWinControl works also for TWinControl. Maybe there should be a widgetset TScrollingHelper class which is used by TScrollingWinControl and TCustomMemo and maybe others. Does somebody want to implement it? It is non-trivial yet it is pure refactoring and the chance for success is high. I can try it later (not now) if nobody else will. Maybe just add Scrollby() into widgetset implementation of TWSCustomMemo and fix it inside widgetsets. ---------------------------------------------------------------------- JuhaManninen · 2015-11-24 16:31 Quote from: zeljko on November 24, 2015, 04:33:15 pmMaybe just add Scrollby() into widgetset implementation of TWSCustomMemo and fix it inside widgetsets. That means duplicating the code. That's why I thought about a helper class. Maybe global functions could be used, too. ---------------------------------------------------------------------- balazsszekely · 2015-11-25 11:49 QuoteThat's why I thought about a helper class. Maybe global functions could be used, too.I did implement it today for windows and it's working fine. The content of the memo is scrolled nicely, but then I realized there is absolutely no reason to scroll the memo with a specific value. The point is the memo's scrollbar is set in a such way that you can only scroll it line by line. The amount it depends on the line's height. For example if the line height is 17px, it make no sense to scroll 24 px, because the scrollbar and the memo's content get out of sync. Probably this is the reason why in delphi the "manual" scrolling was removed. Under lGTK2/ Carbon that's not true. The memo's scrollbar can take any value. The bottom line is you cannot solve this issue with a helper class/global function. I'm afraid it has to be implemented separately in each widgetset. PS: SendMessage/PostMessage works under linux too, but it has to be implemented a little bit differently. See examples-->messages-->project1.lpi ---------------------------------------------------------------------- JuhaManninen · 2015-11-29 19:29 I implemented TCustomMemo.ScrollBy together with fixing issue:   http://bugs.freepascal.org/view.php?id=29067 See my comment there. Now I need help to test the changes, especially the widgetsets I could not test myself. Somehow I missed the last comment from GetMem, sorry. GetMem, what do you think of my solution for Windows? At least Memo.ScrollBy does something now. Earlier it did nothing. ---------------------------------------------------------------------- balazsszekely · 2015-11-29 19:32 QuoteNow I need help to test the changes, especially the widgetsets I could not test myself.It's not scrolling on windows. You should combine WM_HSCROLL with SB_LEFT/SB_RIGHT and VM_VSCROLL with SB_LINEDOWN/SB_LINEUP. Another solution would be EM_LINESCROLL(see henrik response: http://forum.lazarus.freepascal.org/index.php/topic,3 0468.msg193957.html#msg193957). Tested with this and it's working fine. QuoteGetMem, what do you think of my solution for Windows? At least Memo.ScrollBy does something now. Earlier it did nothing.I agree, it's much better now then without scrolling, although in windows it' s a little bit confusing, the following code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Memo1.VertScrollBar.Postion := Memo1.VertScrollBar.Postion + 1; Will change the position with 17 or so. ---------------------------------------------------------------------- JuhaManninen · 2015-11-29 21:08 Quote from: GetMem on November 29, 2015, 08:32:06 pmIt's not scrolling on windows. I applied a new patch from Ondrej in r50525. Please test again. QuoteI agree, it's much better now then without scrolling, although in windows it' s a little bit confusing, the following code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Memo1.VertScrollBar.Postion := Memo1.VertScrollBar.Postion + 1; Will change the position with 17 or so. How to improve it? ---------------------------------------------------------------------- balazsszekely · 2015-11-29 21:23 QuoteI applied a new patch from Ondrej in r50525. Please test again.It works now. QuoteHow to improve it?I have no idea what would be the ideal solution. Anyway it's a great improvment. Thanks Juha!