lazarus/lcl/include/controlcanvas.inc
maxim e7b88a6816 Merged revision(s) 53250-53254 #4161b1f5e1-#4161b1f5e1, 53274 #b8c9f6d510, 53306 #e0a1a0b0a2, 53324 #d725d7f542 from trunk:
LCL: destroy menu handle on remove. Issue #30806, patch by Michl
........
LCL: headercontrol: implement ChangeScale, issue #30812, patch by Anton Kavalenka
........
lcl: controlcanvas: fix TControlCanvas.CreateHandle debugln, issue #30003
........
LCL: TGraphicControl: always call FreeHandle - HandleAllocated doesn't take FDeviceContext into consideration. Issue #30003, patch by Michl
........
LCL: destroy TGraphicControl.Canvas.Handle when control is removed from parent. Issue #30003
........
LCL: controlcanvas: protect TControlCanvas.CreateHandle debugln with an IFDEF
........
LCL Fix access violation when deleting a TMainMenu after r53250 #4161b1f5e1. Issue #0030882. Patch by Michl.
........
lcl: win32: ignore keyup without keydown at program start. issue #30836
........

git-svn-id: branches/fixes_1_6@53358 -
2016-11-12 12:58:55 +00:00

129 lines
4.0 KiB
PHP

{%MainUnit ../controls.pp}
{******************************************************************************
TControlCanvas
******************************************************************************
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
{------------------------------------------------------------------------------
Method: TControlCanvas.SetControl
Params: AControl: The control this canvas belongs to
Returns: Nothing
Sets the owner of this canvas
------------------------------------------------------------------------------}
procedure TControlCanvas.SetControl(AControl: TControl);
begin
if FControl <> AControl then
begin
FreeHandle;
FControl := AControl;
end;
end;
function TControlCanvas.GetDefaultColor(const ADefaultColorType: TDefaultColorType): TColor;
begin
if Assigned(FControl) then
Result := FControl.GetDefaultColor(ADefaultColorType)
else
Result := inherited GetDefaultColor(ADefaultColorType);
end;
{------------------------------------------------------------------------------
Method: TControlCanvas.Create
Params: none
Returns: Nothing
Constructor for the class.
------------------------------------------------------------------------------}
constructor TControlCanvas.Create;
begin
inherited Create;
FDeviceContext := 0;
FControl := nil;
FWindowHandle := 0;
end;
{------------------------------------------------------------------------------
Method: TControlCanvas.Destroy
Params: None
Returns: Nothing
Destructor for the class.
------------------------------------------------------------------------------}
destructor TControlCanvas.Destroy;
begin
FreeHandle;
inherited Destroy;
end;
{------------------------------------------------------------------------------
Method: TControlCanvas.CreateHandle
Params: None
Returns: Nothing
Creates the handle ( = object).
------------------------------------------------------------------------------}
procedure TControlCanvas.CreateHandle;
var
WinControl: TWinControl;
begin
//DebugLn('[TControlCanvas.CreateHandle] ',FControl<>nil,' DC=',DbgS(FDeviceContext,8),' WinHandle=',DbgS(FWindowHandle,8));
if FControl = nil then
inherited CreateHandle
else
begin
{$IFDEF VerboseCanvas}
if not ControlIsPainting and
(WidgetSet.GetLCLCapability(lcCanDrawOutsideOnPaint) = LCL_CAPABILITY_NO) then
debugln(['TControlCanvas.CreateHandle WARNING: accessing the canvas of '+DbgSName(FControl)+' is not supported outside of paint message']);
{$ENDIF}
if (FDeviceContext = 0) then
begin
// access to window handle can cause another TControlCanvas.CreateHandle
// as result we get a resource leak. To prevent this require handle before
// accessing it
if FControl is TWinControl then
WinControl := TWinControl(FControl)
else
WinControl := FControl.Parent;
WinControl.HandleNeeded;
end;
if FDeviceContext = 0 then
begin
// store the handle locally since we need it to check (and do not
// want to fire creation events)
FDeviceContext := FControl.GetDeviceContext(FWindowHandle);
end;
Handle := FDeviceContext;
end;
end;
{------------------------------------------------------------------------------
Method: TControlCanvas.FreeHandle
Params: None
Returns: Nothing
Frees the handle
------------------------------------------------------------------------------}
procedure TControlCanvas.FreeHandle;
begin
inherited;
if FDeviceContext <> 0 then
begin
ReleaseDC(FWindowHandle, FDeviceContext);
FDeviceContext := 0;
end;
end;
function TControlCanvas.ControlIsPainting: boolean;
begin
Result := Assigned(FControl) and FControl.IsProcessingPaintMsg;
end;