TListBox flickers when scrolling (laz 1.4.0) lazarus / 31-LCL ---------------------------------------------------------------------- ncs-sniper · 2015-05-17 13:05 Hi all! I have the following problem - in one of my programs, I use TListBox component to show some data to the user - items are manually drawn (lbOwnerDrawFixed -> OnItemDraw), doublebuffered is set to true. Everything was fine in Lazarus 1.2.6, but now I have installed version 1.4.0 and the component flickers when scrolling. It seems the newly shown items are first drawn empty and then, a fraction of second later, redrawn in OnItemDraw event. As I said, it was not happening when compiled in Lazarus 1.2.6 (exactly the same code). I have also noticed that this does not happen in TCheckListBox (in the same app). Here is OnDrawItem handler (nothing interesting there I think): Code: [Select]procedure TfSupportedGamesForm.lbGamesListDrawItem(Control: TWinControl;   Index: Integer; Rect: TRect; State: TOwnerDrawState); const   clSideBar       = $00EFEFEF;   clSideBarBinded = clLime;   clHorSplit      = clSilver;   DefIconSize     = 32;   LeftBarWidth    = 6; var   TempGameData: TGameData;   WorkBitmap:   TBitmap;   WorkRect:     TRect; begin TempGameData := ACCManager.GamesDataManager.GameData[Index]; WorkBitmap := TBitmap.Create; try   WorkBitmap.Width := Rect.Right - Rect.Left;   WorkBitmap.Height := Rect.Bottom - Rect.Top;   WorkRect := Classes.Rect(0,0,WorkBitmap.Width,WorkBitmap.Height);   with WorkBitmap.Canvas do     begin       If (odSelected in State) then         begin           Brush.Color := clSideBar;           Pen.Color := clSideBar;         end       else         begin           Brush.Color := lbGamesList.Color;           Pen.Color := lbGamesList.Color;         end;       Rectangle(WorkRect);       If fGameIsBinded and IsEqualGUID(fBindedGame,TempGameData.Identifier) then         begin           Brush.Color := clSideBarBinded;           Pen.Color := clSideBarBinded;         end       else         begin           Brush.Color := clSideBar;           Pen.Color := clSideBar;         end;       Rectangle(WorkRect.Left,WorkRect.Top,WorkRect.Left + LeftBarWidth,WorkRect.Bottom);       Brush.Style := bsClear;       Font := lbGamesList.Font;       Font.Style := [fsBold];       TextOut(WorkRect.Left + LeftBarWidth + DefIconSize + 6,               WorkRect.Top + 4,TempGameData.Title);       Font.Style := [];       TextOut(WorkRect.Left + LeftBarWidth + DefIconSize + 6,               WorkRect.Bottom - 4 - TextHeight(TempGameData.Info),TempGameData.Info);       Pen.Color := clHorSplit;       MoveTo(WorkRect.Left,WorkRect.Bottom - 1);       LineTo(WorkRect.Right,WorkRect.Bottom - 1);       Draw(WorkRect.Left + LeftBarWidth + 2,WorkRect.Top + (WorkRect.Bottom - WorkRect.Top - DefIconSize) div 2,            ACCManager.GamesDataManager.GameIcons.GetIcon(TempGameData.Icon));     end;   lbGamesList.Canvas.Draw(Rect.Left,Rect.Top,WorkBitmap);   If (odFocused in State) then lbGamesList.Canvas.DrawFocusRect(Rect); finally   WorkBitmap.Free; end; end;    Full source of the program can be found here: https://github.com/ncs- sniper/AdjustableCruiseControl. This particular part of the program is in this unit https://github.com/ncs-sniper/AdjustableCruiseControl/blo b/master/MainProgram/SupportedGamesForm.pas Was the component changed? And is there any solution to this? ---------------------------------------------------------------------- balazsszekely · 2015-05-17 15:59 I see no flickering. Tested with 1.4 and 1.5(SVN version), XP and Win7(both 32 bit). ---------------------------------------------------------------------- ncs-sniper · 2015-05-17 16:05 Depends on what have you tested it on, if you have fast enough computer, the redraw is so fast you can't notice it. I have tried it on other systems than my primary, and it is definitely there. ---------------------------------------------------------------------- balazsszekely · 2015-05-17 16:44 I'm not saying it's not there, I just cannot reproduce it. I did adjust my virtual machine settings(1 CPU, 512 MB RAM), still no flickering. Anyway you can find TListBox source in .../lcl/stdctrls.pp and /lcl/include/customlistbox.inc. Compare 1.2.6 with 1.4 and see what is the difference if any. ---------------------------------------------------------------------- ncs-sniper · 2015-05-17 16:54 I have done a small test, I have put sleep(100) at the start of drawitem event handler, and there IS a difference in behavior. In program compiled in 1.2.6, the handler is called BEFORE the listbox is visibly scrolled, so when new items are about to be shown, they are already rendered. But in program compiled in 1.4.0, the listbox is scrolled, the new items are rendered empty, and only then the handler is called. This redraw is causing it. I have just tried clear installation in virtualbox, and it is the same, so my lazarus installation is not broken. I will look into the sources, but right now, it seems to me like a bug. ---------------------------------------------------------------------- balazsszekely · 2015-05-17 17:07 QuoteI will look into the sources, but right now, it seems to me like a bug.Yes, it's possible. If you find a bug, please report it here: http://bugs.freepascal.org/main_page.php Attach a small example where the issue is easily reproducible. Alternatively, you can create a patch if you like. ---------------------------------------------------------------------- ncs-sniper · 2015-05-17 19:26 For now, I have not found anything that could explain it. At least not in TListBox/TCustomListBox. BTW is there any way how to step into LCL source while debugging? EDIT 2015-05-18 00:21 It seems to be happening only in windows, when I do the same in linux (lubuntu 15.04 32bit) it works as expected. ---------------------------------------------------------------------- ncs-sniper · 2015-05-20 00:32 So, there is no one able to tell me what might be wrong? Only information I have got is that user GetMem does not see the flickering. That is nice and good for him, but in the end, totally useless information. I have compiled the same source on the same system, 1.2.6 produced program where the flickering does not happen, and 1.4.0 produced program that flickers (both tried on WinXP 64b, WinXP 32b and Win7 64b). Don't tell me there was no change in the LCL! BTW it is best visible when you rapidly scroll the listbox using scrollbar. I am not some programming guru, so reading through the whole LCL in search for hidden errors is beyond me. ---------------------------------------------------------------------- BitBangerUSA · 2015-05-20 01:09 there have been changes in the LCL. i have seen behavior changes in my program using DBLookupListbox between 1.2.6 and 1.4. while not the same-same as you have experienced, it is somewhat in the same ball-park. i have some idea that i may not be doing something not in the best way and will be working on re-writing that unit of my program. you should be able to debug step through the LCL using F7/F8 - no workee for you? you might want to post a small example that demos the problem, as suggested. ---------------------------------------------------------------------- ncs-sniper · 2015-05-20 01:45 As for stepping the LCL - I have recompiled it (the LCL) with -g, now it is possible. But there are no changes in the path I have investigated. Writing an example is problematic. I have not been able to produce the same glitch reliably (happens only sometimes) in simple code. Probably because in simplest example, the redraw is so fast I can't notice it too. But I will surely try again. For now, the whole project where it happens is available on github and there should be no problem for anyone to compile and try it. ---------------------------------------------------------------------- balazsszekely · 2015-05-20 03:07 Quote@ncs-sniper That is nice and good for him, but in the end, totally useless information.I don't know about the useless part, but at least I did download your program and I ran a few test just to help you! Sorry but I cannot fight with an invisible enemy(again, I'm not saying the flickering is not there, I just cannot reproduce it). Can somebody else confirm that flickering? Until then, here is what you can do: 1. The developers(some of them) are not very active on this forum. You can try to send a mail to Lazarus Mailing List(http://lists.lazarus.freepascal.org/mailman/listinfo). Unfortunately without a reliable example I  doubt they will be able to help you. 2. For now stay with 1.2.6 3. Replace TListBox with something else(VirtualStringTree, TreeView without buttons, lines, root). Ps: Please stop complaining about not getting help. We all do this in our free time and there is more important issues than TListBox flickering. ---------------------------------------------------------------------- wp · 2015-05-20 09:10 Just to add more "totally useless informtion": No flicker on my system (Win 7 64 bit, i5 processor, built-in graphics, Laz 1.5/fpc 2.6.4). ---------------------------------------------------------------------- ncs-sniper · 2015-05-20 14:22 Easy guys, I really DO appreciate any feedback. Fact that someone does not see my problem is something I can certainly work with, but common, you must know I does not move me any closer to a solution. I am also open to an option that something is wrong in my laz. installation. I just can't see what. When I compile the same source in fresh installation in virtualbox, the same happens. I have considered staying with 1.2.6, but there are things I really like in 1.4. As for replacing the component - I would like to keep it as simple as possible, but it might be a solution, I am just not there yet. I have moved from delphi to lazarus because I thought there will be less problems, and then this... Yes, I will try to create some example. But I doubt it will be simple, not every problem manifests itself in simple situation. ---------------------------------------------------------------------- wp · 2015-05-20 14:36 Looking carefully, there is something like a flicker in the last line of the listbox when scrolling down, and in the first line when scrolling up. Is this what you mean? If not describe exactly what you are doing. ---------------------------------------------------------------------- ncs-sniper · 2015-05-20 15:09 You mean in last line of the shown items, or last item of entire listbox? EDIT - you have edited and answered it before I have finished this post ; yes, exactly that. As I already wrote, when I scroll the listbox very quickly (both up and down), the items that were not visible and are about to be shown are not rendered (the ondraw... handler is not called) when such item is shown on the screen. It is first rendered empty (probably with background color, but I am not sure, have to try it) and only then the handler is called. In 1.2.6, the handler was called before the item have to be shown. ---------------------------------------------------------------------- wp · 2015-05-20 16:07 OK - here's a demo which shows the issue: Compile and run, just drag the scrollbar into the center and then quickly drag it up and down by 1 cm. You'll see occasional flickering of the first and last visible lines. Confirmed with Laz trunk/fpc 2.6.4 and with Laz 1.4/fpc 2.6.4. I can also confirm your observation that this did not occur earlier - I only have Laz 1.2 now, but there is no flicker with this version. I looked through the svn logs, but did no find an obvious change. ncs-sniper, please file a bug report, you can attach this demo if you want. ---------------------------------------------------------------------- BitBangerUSA · 2015-05-20 16:41 tested wp's demo with Lazarus 1.2.4 / FPC 2.6.4 / Windows 7 Home Premium 64 bit - no flickering seen. wp has gone 'above and beyond' on this one. ---------------------------------------------------------------------- wp · 2015-05-20 16:54 Flicker is gone when the listbox responds to the WMEraseBackground message without actually clearing the background - see modified listbox in the right of the form in the attached demo. BUT: the old version did not catch this message either but is working correctly. The bug must be somewhere else. ---------------------------------------------------------------------- balazsszekely · 2015-05-20 17:05 I give up, I see no difference between listbox1 and listbox2. ---------------------------------------------------------------------- ncs-sniper · 2015-05-20 17:12 I am glad at least someone was able to reproduce this. Yes, ignoring WMEraseBackground really helps. I have also created more severe test (items are filled with pseudorandom noise, so redraw takes a lot of processing power and therefore time). Can anyone please test it? ---------------------------------------------------------------------- BitBangerUSA · 2015-05-20 17:24 tested ncs-sniper prog - no flickering seen (run of the mill system) with Laz 1.2.4 / FPC 2.6.4 / Windwos 7 Home 64 bit. much flickering on high-end system using Laz 1.4. also got a dialog about heap on stopping the prog on high-end system - i've no clue as to importance. ---------------------------------------------------------------------- wp · 2015-05-20 17:25 Yes, very dramatic effect, and no flicker with Laz 1.2 Quotealso got a dialog about heap on stopping the prog on high-end system - i've no clue as to importanceThis is because heap trace is checked in the compiler options (Project options / Compiler options / Debugging / Use heaptrc unit...) ---------------------------------------------------------------------- ncs-sniper · 2015-05-20 17:29 BitBangerUSA - it is set to be compiled in debug mode with heap tracking, that is why the window shows up. Just ignore it. So at least two other users can see it too. Thanks for testing! ---------------------------------------------------------------------- BitBangerUSA · 2015-05-20 17:34 wp and ncs-sniper - thanks for the info - i hadn't looked... ---------------------------------------------------------------------- airpas · 2015-05-20 17:52 i see annoying flickering while scrolling or resizing the window , i use lazarus 1.5 / fpc 3.1.1 win7 32bits also when using splitters too ,  i filed a report about that while ago ---------------------------------------------------------------------- wp · 2015-05-20 19:19 airpas, I think your issue is different. It was reported for Laz 1.0.11 - I still have a 1.0 version lying around which does not show the listbox flicker. ---------------------------------------------------------------------- airpas · 2015-05-20 19:39 my issue was with splitter . i think the problem of flickering present in windows only . lcl-gtk/qt seems fine ---------------------------------------------------------------------- wp · 2015-05-20 20:16 In r47451, a lot of work was done with the EraseBackground message in January. Maybe the bug slipped in this way. I filed a bug report http://bugs.freepascal.org/view.php?id=28146 ---------------------------------------------------------------------- ncs-sniper · 2015-05-20 21:10 Many thanks for filing that bug report! I have again tried it in linux (Lubuntu) - just to be sure, this time with the noise. And no, it really does not happen in linux. ---------------------------------------------------------------------- ncs-sniper · 2015-05-22 21:33 I am watching the bug report (http://bugs.freepascal.org/view.php?id=28146) - catching the WM_EraseBackground does not solve it, it just hides it. I have tried this solution with interposer for TListBox in my project mentioned in the first post, and it does not flicker only because the individual items are very similar. If you have items that are visually distinct, it will flicker again (or better put, will produce artifacts). ---------------------------------------------------------------------- wp · 2015-05-22 22:33 In the attachment, there is a modified version of your "extreme" demo, again with the "no-erase-background" listbox for comparison at the right - I don't see any flicker, in contrast to the standard listbox at the left. ---------------------------------------------------------------------- ncs-sniper · 2015-05-23 01:04 No, there is no flicker, but that does not mean it is ok. You simply cannot distinguish individual items, if you could, you would see it. The problem as I see it is not that the background is cleared, the main problem, and I wrote it many times, is that the new item if first shown, and only after that the draw handler is called. Therefore, when the background is cleared, it flickers because the newly shown item is first rendered empty (cleared) and then drawn in handler. When you ignore the clearing, the program just move the list up or down and leaves the old item where it was, and that old item is then redrawn in the handler. If the old item visually differs from the newly drawn, it might again cause problems. ---------------------------------------------------------------------- wp · 2015-05-23 08:24 I am close to giving up on this one... Quote that the new item if first shown, and only after that the draw handler is called.I read it as often as you wrote it, and as often I did not understand it. How can the new item be shown without calling the OnDrawItem handler? The main question is: why does the CheckListbox behave correctly, but not the standard Listbox. The CheckListbox is just the standard listbox with a dedicated DrawItem method, just what we are doing. [EDIT] Looking into the sources of TCheckListbox I saw now that it implements a separate handler for the WM_EraseBackground message in the win32 widget set. I copied this handler into the standard listbox, now the "flicker" (as I call it - I never saw the one you are referring to) is gone, and compile your program (maybe you should rebuild the LCL). Could you check this with your program? In the attachment you'll find a zip file containing the patched "win32wschecklst.pp" and "win32wsstdctrls.pp" files. Copy them to the folder "lcl\interfaces\win32" of your Lazarus installation (rename the original ones in order to be able to restore them). ---------------------------------------------------------------------- sin_dragan · 2015-05-24 00:14 @wp +1 for example, very nice demonstration, thank you. I confirm that your example works (right listbox with no flickering). Also "fix" that you posted solves the problem. I didn't need to rebuild lazarus, just recompile the project ---------------------------------------------------------------------- ncs-sniper · 2015-05-24 00:25 QuoteI read it as often as you wrote it, and as often I did not understand it. How can the new item be shown without calling the OnDrawItem handler?Sorry, my mistake, I was confused by something else. Your patch really helps. I think this is the solution we are looking for. Good work! ---------------------------------------------------------------------- wp · 2015-05-24 08:33 Are you referring to the "win32.zip" a few postings above? This is what I would like to use because it is the code that works with TCheckListbox. BUT: It disables background erasing only for 1 column; so, if there are more columns I'd guess that there is flicker again. ncs-sniper, could you check this? Set the ColumnCount of your listbox to 2 or greater and look for flicker again. ---------------------------------------------------------------------- ncs-sniper · 2015-05-24 18:04 Yes, I was reffering to win32.zip. Unfortunatelly, you are right, it flickers again when listbox is set to more than one column. And even the TCheckListBox flickers when set to more than one column. ---------------------------------------------------------------------- wp · 2015-05-25 15:16 At the moment, I don't have an idea how this could be avoided: I commented the "(TCustomListBox(WindowInfo^.WinControl).Columns < 2)" condition from the "if", but this results in incorrect background painting in case of a partially filled multi-column listbox. The same happens after adding more conditions to the "if" in order to find a better handling for multi-column listboxes. Since multi-column listboxes are not so popular I will propose the patched Win32WSStdCtrls as a workaround in the bugtracker. Not perfect, but better than nothing... ---------------------------------------------------------------------- ncs-sniper · 2015-05-26 18:14 I completely agree. Till now, I wasn't even aware there is such property. ---------------------------------------------------------------------- ncs-sniper · 2015-10-17 21:03 Well, Lazarus 1.4.4 - it is happenning again, and now it cannot be fixed by interposer ---------------------------------------------------------------------- wp · 2015-10-17 22:06 Please post a demo where this flicker can be seen. This issue is very delicate and needs careful validation, the patch was partly reverted because we had cases which damaged standard painting of the listbox. ---------------------------------------------------------------------- ncs-sniper · 2015-10-17 23:30 I know previous fix had problems. I have nothing against reverting as long as it is not trading one bug for another. What kind of demo should I post? The old test cases are still the same with the same results and the project mentioned in the first post is still alive. Here you have small video (quality is not good but the flickering is visible) https://www.dropbox.com/s/bqtnibrarsa4xju/flicker.mp4?dl=0. ---------------------------------------------------------------------- wp · 2015-10-18 00:51 I cannot debug a video... The two demos attached to the old bug report (http://bugs.freepascal.org/view.php?id=28146) which had been showing massive flicker before applying the patch do not show flicker on my Win7 system with Laz 1.4.4. So, I fear there's nothing I can do here. Are you sure that your graphics driver is up to date and correct? Do you see the flicker on every computer? ---------------------------------------------------------------------- ncs-sniper · 2015-10-18 01:38 It works without problem when compiled in delphi or older laz. So I doubt it is related to system. Of course you don't see it when there is this 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 if (WindowsVersion <= wvXP) or not ThemeServices.ThemesEnabled then...else  (old rendering code from 1.4.2)..it is not happenning on win7 even for me. ---------------------------------------------------------------------- wp · 2015-10-18 08:54 Please specify your operating system. XP? XP had to be excluded from the patch because of massive background painting issues which EVERYBODY sees. ---------------------------------------------------------------------- ncs-sniper · 2015-10-18 14:42 Yes, XP, both 32 and 64bit. I don't know much about LCL, but isn't it posssible to run default rendering only when OnDrawItem is not assigned and listbox does not have ownerdraw* style? Something like 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     WM_ERASEBKGND:      begin        WindowInfo := GetWin32WindowInfo(Window);        if ((WindowsVersion <= wvXP) or not ThemeServices.ThemesEnabled)           and not (TCustomListBox(WindowInfo^.WinControl).Style in [lbOwnerDrawFixed,lbOwnerDrawVariable]) then          // Standardbehavior for XP, or no themes          Result := CallDefaultWindowProc(Window, Msg, WParam, LParam)        else        begin          // Avoid unnecessary background paints to avoid flickering of the listbox           Count := SendMessage(Window, LB_GETCOUNT, 0, 0);          if Assigned(WindowInfo^.WinControl) and          (TCustomListBox(WindowInfo^.WinControl).Columns < 2) and          (Count <> LB_ERR) and (SendMessage(Window, LB_GETITEMRECT, Count - 1, Windows.LParam(@ARect)) <> LB_ERR) then          begin          Top := ARect.Bottom;            Windows.GetClientRect(Window, ARect);            ARect.Top := Top;            if not IsRectEmpty(ARect) then              Windows.FillRect(HDC(WParam), ARect, WindowInfo^.WinControl.Brush.Reference.Handle);            Result := 1;          end          else            Result := CallDefaultWindowProc(Window, Msg, WParam, LParam);          Exit;      end;      end;     I have not tested it properly, but it looks harmless to me. ---------------------------------------------------------------------- wp · 2015-10-18 16:25 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     WM_ERASEBKGND:      begin        WindowInfo := GetWin32WindowInfo(Window);        if ((WindowsVersion <= wvXP) or not ThemeServices.ThemesEnabled) then        begin          if Assigned(WindowInfo^.WinControl) and not             (TCustomListbox(WindowInfo^.WinControl).Style in [lbOwnerDrawFixed, lbOwnerDrawVariable])          then begin            // Standardbehavior for XP, or no themes, no OwnerDraw            Result := CallDefaultWindowProc(Window, Msg, WParam, LParam);            exit;          end        end;        // Avoid unnecessary background paints to avoid flickering of the listbox        Count := SendMessage(Window, LB_GETCOUNT, 0, 0);        if Assigned(WindowInfo^.WinControl) and          (TCustomListBox(WindowInfo^.WinControl).Columns < 2) and          (Count <> LB_ERR) and (SendMessage(Window, LB_GETITEMRECT, Count - 1, Windows.LParam(@ARect)) <> LB_ERR) then        begin          Top := ARect.Bottom;          Windows.GetClientRect(Window, ARect);          ARect.Top := Top;          if not IsRectEmpty(ARect) then            Windows.FillRect(HDC(WParam), ARect, WindowInfo^.WinControl.Brush.Reference.Handle);          Result := 1;        end        else          Result := CallDefaultWindowProc(Window, Msg, WParam, LParam);        Exit;      end; This is better, your code may crash if the WinInfo^.WinControl is nil -- no idea, if this can happen. I don't have XP, only on a virtual machine. The modification seems to run fine there. Please test thoroughly on your "real" XP - there are not so many XP users out there any more. Test also if non-ownerdrawn or unthemed listboxes are painted correctly with erased background. Post the result here, and I'll submit the new version as a patch to bugtracker. ---------------------------------------------------------------------- ncs-sniper · 2015-10-18 17:27 Ok, will do tonight. But I can test it only on XP SP3 32b, XP 64b in virtual machine, Vista SP2 HomePrem. 32b and Win7 64b virtual (can't remember exact version). Do you thing it will be enough? ---------------------------------------------------------------------- wp · 2015-10-18 17:29 Certainly, more than I had expected ---------------------------------------------------------------------- ncs-sniper · 2015-10-19 17:21 Ok, I have tested it on attached program. I have tried it both with themes enabled and disabled where possible and of course 64bit builds. I have used code from your post with one change - it seems the background painting bug is manifesting itself even on winXP 64bit, which are AFAIK based on Win. Server 2003, so i have changed it like so: 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 WindowsVersion <= wvServer2003 The results are: Win XP SP3 32b - ok Win XP 64b in VirtualBox - ok (after the mentioned change) Win Vista Home Premium SP1 32b - ok Win 7 Ultimate SP1 64b in VirtualBox - ok ---------------------------------------------------------------------- wp · 2015-10-19 17:44 Perfect. I reopened the old bug report, Juha will certainly apply the patch soon. ---------------------------------------------------------------------- ncs-sniper · 2015-10-19 20:49 Many thanks!