Patch for fixing a bug at TCDWidgetSet.SaveDC
- Lazarus/FPC Version: Lazarus 2.3.0 (rev main-2_3-1386-g23b2324f9f) FPC 3.2.3 x86_64-linux-gtk2
- Operating System: any
- CPU / Bitness: any
What happens
By lagprogramming from forum: https://forum.lazarus.freepascal.org/index.php/topic,62913.msg476026.html#msg476026
According to the comments found all over the interfaces, the function should return the index assigned to OR 0 if DC is not valid. The problem is that the original code returns the index of TLazCanvas.SaveState, which can be zero. This makes a valid TLazCanvas.SaveState zero index to be interpreted as if DC is not valid. The patch mimics the solution found in function TCocoaContext.SaveDC: Integer; The function result is the index in the list + 1. This means that the first SaveDC will be one, not zero.
diff --git a/lcl/interfaces/customdrawn/customdrawnwinapi.inc b/lcl/interfaces/customdrawn/customdrawnwinapi.inc
index 0a12ac2d82..dbdcb15667 100644
--- a/lcl/interfaces/customdrawn/customdrawnwinapi.inc
+++ b/lcl/interfaces/customdrawn/customdrawnwinapi.inc
@@ -5033,10 +5033,13 @@ begin
{$ifdef VerboseQTWinAPI}
WriteLn('Trace:> [WinAPI RestoreDC] DC=', dbghex(DC),' SavedDC=',SavedDC);
{$Endif}
- Result := False;
- if not IsValidDC(DC) then Exit;
+ if (SavedDC=0) or (not IsValidDC(DC)) then Exit(False);
+
+ if SavedDC>0 then
+ LazDC.RestoreState(SavedDC-1)
+ else
+ LazDC.RestoreState(SavedDC);
- LazDC.RestoreState(SavedDC);
Result := True;
{$ifdef VerboseQTWinAPI}
WriteLn('Trace:< [WinAPI RestoreDC]');
@@ -5079,7 +5082,7 @@ begin
exit;
end;
- Result := LazDC.SaveState();
+ Result := LazDC.SaveState()+1;
{$ifdef VerboseQTWinAPI}
WriteLn('Trace:< [WinAPI SaveDC] result=', Result);
Edited by Alexey Torgashin