brush handle/reference rework

git-svn-id: trunk@13289 -
This commit is contained in:
paul 2007-12-12 04:51:15 +00:00
parent 8502f33c61
commit cde0108e94
4 changed files with 109 additions and 113 deletions

View File

@ -418,7 +418,6 @@ type
property OnChange: TNotifyEvent read FOnChange write FOnChange; property OnChange: TNotifyEvent read FOnChange write FOnChange;
end; end;
{ TFontHandleCacheDescriptor } { TFontHandleCacheDescriptor }
TFontHandleCacheDescriptor = class(TResourceCacheDescriptor) TFontHandleCacheDescriptor = class(TResourceCacheDescriptor)
@ -427,7 +426,6 @@ type
LongFontName: string; LongFontName: string;
end; end;
{ TFontHandleCache } { TFontHandleCache }
TFontHandleCache = class(TResourceCache) TFontHandleCache = class(TResourceCache)
@ -443,7 +441,6 @@ type
const LongFontName: string): TFontHandleCacheDescriptor; const LongFontName: string): TFontHandleCacheDescriptor;
end; end;
{ TFont } { TFont }
TFont = class(TFPCustomFont) TFont = class(TFPCustomFont)
@ -516,16 +513,8 @@ type
property Style: TFontStyles read GetStyle write SetStyle; property Style: TFontStyles read GetStyle write SetStyle;
end; end;
{ TPen } { TPen }
TPenData = record
Reference: TWSPenReference;
Color: TColor;
Width: Integer;
Style: TPenStyle;
end;
TPenHandleCache = class(TBlockResourceCache) TPenHandleCache = class(TBlockResourceCache)
protected protected
procedure RemoveItem(Item: TResourceCacheItem); override; procedure RemoveItem(Item: TResourceCacheItem); override;
@ -566,16 +555,8 @@ type
property Width default 1; property Width default 1;
end; end;
{ TBrush } { TBrush }
TBrushData = record
Handle: HBrush;
Color: TColor;
Bitmap: TBitmap;
Style: TBrushStyle;
end;
TBrushHandleCache = class(TBlockResourceCache) TBrushHandleCache = class(TBlockResourceCache)
protected protected
procedure RemoveItem(Item: TResourceCacheItem); override; procedure RemoveItem(Item: TResourceCacheItem); override;
@ -585,11 +566,15 @@ type
TBrush = class(TFPCustomBrush) TBrush = class(TFPCustomBrush)
private private
FHandle: HBrush;
FBrushHandleCached: boolean; FBrushHandleCached: boolean;
FColor: TColor; FColor: TColor;
FBitmap: TBitmap; FBitmap: TBitmap;
procedure FreeHandle; FReference: TWSBrushReference;
procedure FreeReference;
function GetHandle: HBRUSH;
function GetReference: TWSBrushReference;
procedure ReferenceNeeded;
procedure SetHandle(const Value: HBRUSH);
procedure DoChange(var Msg); message LM_CHANGED; procedure DoChange(var Msg); message LM_CHANGED;
protected protected
procedure DoAllocateResources; override; procedure DoAllocateResources; override;
@ -597,17 +582,16 @@ type
procedure DoCopyProps(From: TFPCanvasHelper); override; procedure DoCopyProps(From: TFPCanvasHelper); override;
procedure SetColor(const NewColor: TColor; const NewFPColor: TFPColor); virtual; procedure SetColor(const NewColor: TColor; const NewFPColor: TFPColor); virtual;
procedure SetFPColor(const AValue: TFPColor); override; procedure SetFPColor(const AValue: TFPColor); override;
function GetHandle: HBRUSH;
procedure SetBitmap(Value: TBitmap); procedure SetBitmap(Value: TBitmap);
procedure SetColor(Value: TColor); procedure SetColor(Value: TColor);
procedure SetHandle(const Value: HBRUSH); procedure SetStyle(Value: TBrushStyle); override;
Procedure SetStyle(Value: TBrushStyle); override;
public public
procedure Assign(Source: TPersistent); override; procedure Assign(Source: TPersistent); override;
constructor Create; override; constructor Create; override;
destructor Destroy; override; destructor Destroy; override;
property Bitmap: TBitmap read FBitmap write SetBitmap; property Bitmap: TBitmap read FBitmap write SetBitmap;
property Handle: HBRUSH read GetHandle write SetHandle; property Handle: HBRUSH read GetHandle write SetHandle; deprecated;
property Reference: TWSBrushReference read GetReference;
published published
property Color: TColor read FColor write SetColor default clWhite; property Color: TColor read FColor write SetColor default clWhite;
property Style default bsSolid; property Style default bsSolid;

View File

@ -40,9 +40,10 @@ end;
Sets the style of a brush Sets the style of a brush
------------------------------------------------------------------------------} ------------------------------------------------------------------------------}
Procedure TBrush.SetColor(Value : TColor); procedure TBrush.SetColor(Value : TColor);
begin begin
if FColor <> Value then SetColor(Value,TColorToFPColor(Value)); if FColor <> Value then
SetColor(Value, TColorToFPColor(Value));
end; end;
{------------------------------------------------------------------------------ {------------------------------------------------------------------------------
@ -52,10 +53,11 @@ end;
Sets the style of a brush Sets the style of a brush
------------------------------------------------------------------------------} ------------------------------------------------------------------------------}
Procedure TBrush.SetStyle(Value : TBrushStyle); procedure TBrush.SetStyle(Value : TBrushStyle);
begin begin
if Style <> Value then begin if Style <> Value then
FreeHandle; begin
FreeReference;
inherited SetStyle(Value); inherited SetStyle(Value);
Changed; Changed;
end; end;
@ -68,11 +70,11 @@ end;
Sets the style of a brush Sets the style of a brush
------------------------------------------------------------------------------} ------------------------------------------------------------------------------}
Procedure TBrush.SetBitmap(Value : TBitmap); procedure TBrush.SetBitmap(Value : TBitmap);
begin begin
if FBitmap <> Value if FBitmap <> Value then
then begin begin
FreeHandle; FreeReference;
FBitmap := Value; FBitmap := Value;
Changed; Changed;
end; end;
@ -89,9 +91,8 @@ constructor TBrush.Create;
begin begin
inherited Create; inherited Create;
FBitmap := nil; FBitmap := nil;
FHandle := 0;
FColor := clWhite; FColor := clWhite;
DelayAllocate:=true; DelayAllocate := True;
inherited SetStyle(bsSolid); inherited SetStyle(bsSolid);
end; end;
@ -104,7 +105,7 @@ end;
------------------------------------------------------------------------------} ------------------------------------------------------------------------------}
destructor TBrush.Destroy; destructor TBrush.Destroy;
begin begin
FreeHandle; FreeReference;
inherited Destroy; inherited Destroy;
end; end;
@ -115,10 +116,10 @@ end;
Copies the source brush to itself Copies the source brush to itself
------------------------------------------------------------------------------} ------------------------------------------------------------------------------}
Procedure TBrush.Assign(Source: TPersistent); procedure TBrush.Assign(Source: TPersistent);
begin
if Source is TBrush then
begin begin
if Source is TBrush
then begin
Bitmap := TBrush(Source).Bitmap; Bitmap := TBrush(Source).Bitmap;
SetColor(TBrush(Source).Color, TFPCanvasHelper(Source).FPColor); SetColor(TBrush(Source).Color, TFPCanvasHelper(Source).FPColor);
Style := TBrush(Source).Style; Style := TBrush(Source).Style;
@ -136,14 +137,12 @@ end;
------------------------------------------------------------------------------} ------------------------------------------------------------------------------}
procedure TBrush.SetHandle(const Value: HBRUSH); procedure TBrush.SetHandle(const Value: HBRUSH);
begin begin
if FHandle <> Value if FReference.Handle = Value then Exit;
then begin
FreeHandle; FreeReference;
FHandle := Value; FReference._lclHandle := Value;
//TODO: query new parameters
Changed; Changed;
end; end;
end;
{------------------------------------------------------------------------------ {------------------------------------------------------------------------------
Function: TBrush.GetHandle Function: TBrush.GetHandle
@ -153,17 +152,50 @@ end;
Creates a brush if needed Creates a brush if needed
------------------------------------------------------------------------------} ------------------------------------------------------------------------------}
function TBrush.GetHandle: HBRUSH; function TBrush.GetHandle: HBRUSH;
begin
Result := Reference.Handle;
end;
{------------------------------------------------------------------------------
Method: TBrush.FreeHandle
Params: none
Returns: Nothing
Frees a brush handle if needed
------------------------------------------------------------------------------}
procedure TBrush.FreeReference;
begin
if not FReference.Allocated then Exit;
Changing;
if FBrushHandleCached then
begin
BrushResourceCache.FindItem(FReference.Handle).DecreaseRefCount;
FBrushHandleCached := False;
end else
DeleteObject(FReference.Handle);
FReference._lclHandle := 0;
end;
function TBrush.GetReference: TWSBrushReference;
begin
ReferenceNeeded;
Result := FReference;
end;
procedure TBrush.ReferenceNeeded;
var var
LogBrush: TLogBrush; LogBrush: TLogBrush;
CachedBrush: TBlockResourceCacheDescriptor; CachedBrush: TBlockResourceCacheDescriptor;
begin begin
if FHandle = 0 if FReference.Allocated then Exit;
then begin
FillChar(LogBrush, SizeOf(LogBrush), 0); FillChar(LogBrush, SizeOf(LogBrush), 0);
with LogBrush do with LogBrush do
begin begin
if FBitmap <> nil if FBitmap <> nil then
then begin begin
lbStyle := BS_PATTERN; lbStyle := BS_PATTERN;
lbHatch := FBitmap.Handle; lbHatch := FBitmap.Handle;
end else end else
@ -179,45 +211,23 @@ begin
end; end;
lbColor := ColorRef(FColor); lbColor := ColorRef(FColor);
end; end;
CachedBrush := BrushResourceCache.FindDescriptor(@LogBrush); CachedBrush := BrushResourceCache.FindDescriptor(@LogBrush);
if CachedBrush <> nil then if CachedBrush <> nil then
begin begin
CachedBrush.Item.IncreaseRefCount; CachedBrush.Item.IncreaseRefCount;
FHandle := CachedBrush.Item.Handle; FReference._lclHandle := CachedBrush.Item.Handle;
end else end else
begin begin
if LogBrush.lbStyle <> BS_PATTERN then if LogBrush.lbStyle <> BS_PATTERN then
FHandle := CreateBrushIndirect(LogBrush) FReference._lclHandle := CreateBrushIndirect(LogBrush)
else else
FHandle := CreatePatternBrush(LogBrush.lbHatch); FReference._lclHandle := CreatePatternBrush(LogBrush.lbHatch);
BrushResourceCache.AddResource(FHandle,@LogBrush); BrushResourceCache.AddResource(FReference.Handle, @LogBrush);
end; end;
FBrushHandleCached := True; FBrushHandleCached := True;
end; end;
Result := FHandle;
end;
{------------------------------------------------------------------------------
Method: TBrush.FreeHandle
Params: none
Returns: Nothing
Frees a brushhandle if needed
------------------------------------------------------------------------------}
procedure TBrush.FreeHandle;
begin
if FHandle <> 0
then begin
if FBrushHandleCached then begin
BrushResourceCache.FindItem(FHandle).DecreaseRefCount;
FBrushHandleCached:=false;
end else
DeleteObject(FHandle);
FHandle := 0;
end;
end;
procedure TBrush.DoChange(var Msg); procedure TBrush.DoChange(var Msg);
begin begin
Changed; Changed;
@ -226,19 +236,20 @@ end;
procedure TBrush.DoAllocateResources; procedure TBrush.DoAllocateResources;
begin begin
inherited DoAllocateResources; inherited DoAllocateResources;
GetHandle; GetReference;
end; end;
procedure TBrush.DoDeAllocateResources; procedure TBrush.DoDeAllocateResources;
begin begin
FreeHandle; FreeReference;
inherited DoDeAllocateResources; inherited DoDeAllocateResources;
end; end;
procedure TBrush.DoCopyProps(From: TFPCanvasHelper); procedure TBrush.DoCopyProps(From: TFPCanvasHelper);
begin begin
if From is TBrush then begin if From is TBrush then
FreeHandle; begin
FreeReference;
inherited DoCopyProps(From); inherited DoCopyProps(From);
//TODO: query new parameters //TODO: query new parameters
Changed; Changed;
@ -248,8 +259,8 @@ end;
procedure TBrush.SetColor(const NewColor: TColor; const NewFPColor: TFPColor); procedure TBrush.SetColor(const NewColor: TColor; const NewFPColor: TFPColor);
begin begin
if (NewColor=Color) and (NewFPColor=FPColor) then exit; if (NewColor = Color) and (NewFPColor = FPColor) then Exit;
FreeHandle; FreeReference;
FColor := NewColor; FColor := NewColor;
inherited SetFPColor(NewFPColor); inherited SetFPColor(NewFPColor);
Changed; Changed;
@ -257,6 +268,6 @@ end;
procedure TBrush.SetFPColor(const AValue: TFPColor); procedure TBrush.SetFPColor(const AValue: TFPColor);
begin begin
if FPColor=AValue then exit; if FPColor <> AValue then
SetColor(FPColorToTColor(AValue), AValue); SetColor(FPColorToTColor(AValue), AValue);
end; end;

View File

@ -136,14 +136,14 @@ end;
Copies the source pen to itself Copies the source pen to itself
------------------------------------------------------------------------------} ------------------------------------------------------------------------------}
Procedure TPen.Assign(Source : Tpersistent); procedure TPen.Assign(Source : Tpersistent);
begin
if Source is TPen then
begin begin
if Source is TPen
then begin
Width := TPen(Source).Width; Width := TPen(Source).Width;
SetColor(TPen(Source).Color, TFPCanvasHelper(Source).FPColor); SetColor(TPen(Source).Color, TFPCanvasHelper(Source).FPColor);
Style := TPen(Source).Style; Style := TPen(Source).Style;
Mode := TPEn(Source).Mode; Mode := TPen(Source).Mode;
end end
else else
inherited Assign(Source); inherited Assign(Source);

View File

@ -82,6 +82,7 @@ type
end; end;
TWSBrushReference = object(TWSGDIObjReference) TWSBrushReference = object(TWSGDIObjReference)
property _lclHandle: THandle write FRef.Handle;
property Handle: THandle read FRef.Handle; property Handle: THandle read FRef.Handle;
end; end;