* take into account the border to calc text position

* clean up the text draw code

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1438 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
blikblum
2011-01-13 19:20:29 +00:00
parent c744f5a467
commit ec95fda70d

View File

@ -44,9 +44,6 @@ uses
LCLIntf, LCLType, SysUtils, Classes, Graphics, Controls, Forms, LCLIntf, LCLType, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, GraphType, Db, DBCtrls, LMessages; Dialogs, GraphType, Db, DBCtrls, LMessages;
Const
DefaultStyle = DT_SINGLELINE or DT_END_ELLIPSIS or DT_EXPANDTABS;
type type
{ Direction of fill } { Direction of fill }
TFillDirection = (fdLeftToRight, fdRightToLeft, ftTopToBottom, ftBottomToTop); TFillDirection = (fdLeftToRight, fdRightToLeft, ftTopToBottom, ftBottomToTop);
@ -75,7 +72,6 @@ type
procedure OnFontChanged(Sender: TObject); procedure OnFontChanged(Sender: TObject);
protected protected
{ Protected declarations } { Protected declarations }
procedure Assign(Source: TPersistent); override;
procedure SetCaption(const Value: TCaption); virtual; procedure SetCaption(const Value: TCaption); virtual;
procedure SetFont(Value: TFont); virtual; procedure SetFont(Value: TFont); virtual;
procedure SetMarginLeft(Value: Integer); virtual; procedure SetMarginLeft(Value: Integer); virtual;
@ -85,6 +81,7 @@ type
{ Public declarations } { Public declarations }
constructor Create(AOwner: TCustomNetGradient); overload; constructor Create(AOwner: TCustomNetGradient); overload;
destructor Destroy; override; destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
published published
{ Published-Deklarationen } { Published-Deklarationen }
property Caption: TCaption read FCaption write SetCaption; property Caption: TCaption read FCaption write SetCaption;
@ -411,36 +408,14 @@ var
BeginRGBValue : array[0..2] of Byte; { Begin RGB values } BeginRGBValue : array[0..2] of Byte; { Begin RGB values }
RGBDifference : array[0..2] of integer; { Difference between begin and end } RGBDifference : array[0..2] of integer; { Difference between begin and end }
{ RGB values } { RGB values }
ColorBand , rp,cr: TRect; { Color band rectangular coordinates } ColorBand , rp: TRect; { Color band rectangular coordinates }
I , x : Integer; { Color band index } I : Integer; { Color band index }
R : Byte; { Color band Red value } R : Byte; { Color band Red value }
G : Byte; { Color band Green value } G : Byte; { Color band Green value }
B : Byte; { Color band Blue value } B : Byte; { Color band Blue value }
WorkBmp : TBitmap; { Off screen working bitmap } WorkBmp : TBitmap; { Off screen working bitmap }
DrawStyle : LongInt;
TS : TTextStyle; TS : TTextStyle;
GlyphOffs : Integer; BorderOffset: Integer;
//SbCapOffs : Integer;
procedure DoDrawText(const Text: string; ACanvas: TCanvas; AFont: TFont; var Rect: TRect; Flags: LongInt);
begin
with ACanvas do
begin
Font.Assign(AFont);
Rect.Left:=Rect.Left; //**
if not(Enabled) then
begin
OffsetRect(Rect, 1, 1);
Font.Color := clBtnHighlight;
DrawText(Handle, PChar(Text), Length(Text), Rect, Flags);
OffsetRect(Rect, -1, -1); //**
Font.Color := clBtnShadow;
DrawText(Handle, PChar(Text), Length(Text), Rect, Flags);
end
else
DrawText(Handle, PChar(Text), Length(Text), Rect, Flags);
end;
end;
begin begin
{ Create the working bitmap and set its width and height } { Create the working bitmap and set its width and height }
@ -540,118 +515,84 @@ begin
{ Copy the working bitmap to the main canvas } { Copy the working bitmap to the main canvas }
Canvas.Draw(0, 0, WorkBmp); Canvas.Draw(0, 0, WorkBmp);
// <TextOut> if FFlatBorder then
//Canvas.Brush.Style:= bsClear; begin
Canvas.Font.Assign(FFont); BorderOffset := BorderWidth;
if BorderWidth > 0 then
begin
Canvas.Pen.Width := BorderWidth;
Canvas.Pen.EndCap := pecSquare;
Canvas.Pen.Color := FBorderColor;
//see if there's a better way of drawing a rectangle since
// in BorderWidth >= 3 glitches occurs
Canvas.Polyline(GetBorderPoints(rp));
end;
end
else
begin
BorderOffset := 0;
if FBevelOuter <> bvNone then
begin
Canvas.Frame3D(rp, 1, FBevelOuter);
Inc(BorderOffset);
end;
if FBevelInner <> bvNone then
begin
Canvas.Frame3D(rp, 1, FBevelInner);
Inc(BorderOffset);
end;
end;
//Canvas.Textout(FTextLeft, FTextTop, FCaption); *** Enzo *** Implemetation Canvas.Font.Assign(FFont);
if FFlatBorder then TS := Canvas.TextStyle;
begin TS.Alignment := FAlignment;
if BorderWidth > 0 then TS.Layout := FLayout;
begin TS.Opaque := False;
Canvas.Pen.Width := BorderWidth; TS.SystemFont := Canvas.Font.IsDefault;
Canvas.Pen.EndCap := pecSquare;
Canvas.Pen.Color := FBorderColor;
//see if there's a better way of drawing a rectangle since
// in BorderWidth >= 3 glitches occurs
Canvas.Polyline(GetBorderPoints(rp));
end;
end
else
begin
if FBevelOuter <> bvNone then
Canvas.Frame3D(rp, 1, FBevelOuter);
if FBevelInner <> bvNone then
Canvas.Frame3D(rp, 1, FBevelInner);
end;
if Caption <> '' then begin if Caption <> '' then
Font.Assign(Self.Font); begin
Rp := GetClientRect; Rp := GetClientRect;
Inc(Rp.Left, FTextLeft); //5=FMargin InflateRect(Rp, -BorderOffset, -BorderOffset);
//if FGlyph.DisplayAlign = daLeft then
// Inc(Rp.Left, GlyphOffs)
//else
// Dec(R.Right, GlyphOffs);
//Dec(R.Right, SbCapOffs);
case FAlignment of
taLeftJustify : begin
DrawStyle := DefaultStyle or DT_LEFT;
Inc(Rp.Left, FTextLeft);
end;
taRightJustify : begin
DrawStyle := DefaultStyle or DT_RIGHT;
Inc(Rp.Right, FTextLeft);
end; case FAlignment of
taCenter : begin taLeftJustify:
DrawStyle := DefaultStyle or DT_CENTER; Inc(Rp.Left, FTextLeft);
Inc(Rp.Left, FTextLeft); taRightJustify:
end; Dec(Rp.Right, FTextLeft);
end; taCenter:
//DrawStyle := DefaultStyle or FAlignment; //Enzo DT_LEFT; Inc(Rp.Left, FTextLeft * 2);
end;
TS := Canvas.TextStyle; case FLayout of
TS.Alignment:= CaptionAlignment; tlTop:
//TS.Layout:= tlCenter; Inc(rp.Top, FTextTop);
TS.Opaque:= false; tlBottom:
TS.Clipping:= false; Dec(rp.Bottom, FTextTop);
TS.SystemFont:=Canvas.Font.IsDefault; tlCenter:
Inc(rp.Top, FTextTop * 2);
end;
CR := Rp; Canvas.TextRect(rp, rp.Left, rp.Top, Caption, TS);
//cr.left := cr.Left + FTextLeft; end;
//rp.left :=rp.left + FTextLeft;
rp.Top:= rp.Top + FTextTop;
if FLayout <> tlTop then begin
X := Canvas.TextHeight('W'); if FSubCaption.Caption <> '' then
DoDrawText(Caption, WorkBmp.Canvas, Self.Font, CR, DrawStyle or DT_CALCRECT); begin
if FLayout = tlBottom then OffsetRect(Rp, 0, Height - CR.Bottom-2) Canvas.Font.Assign(FSubCaption.Font);
else OffsetRect(Rp, 0, (Height - CR.Bottom) div 2); Rp := GetClientRect;
end; InflateRect(Rp, -BorderOffset, -BorderOffset);
// if Rp.Right - Rp.Left >= Canvas.TextWidth(Caption[1]) then TS.Alignment := taRightJustify;
Canvas.TextRect(rp, rp.Left, rp.Top, Caption, TS); Dec(Rp.Right, FSubCaption.MarginLeft);
case FLayout of
tlTop:
end; Inc(rp.Top, FSubCaption.MarginTop);
tlBottom:
Dec(rp.Bottom, FSubCaption.MarginTop);
//*** Enzo **** SubCaption tlCenter:
Inc(Rp.Top, FSubCaption.MarginTop * 2);
if (FSubCaption.Caption <> '') then begin end;
GlyphOffs := 0; Canvas.TextRect(rp, rp.Left, rp.Top, FSubCaption.Caption, TS);
//SbCapOffs := 0; end;
Canvas.Font.Assign(FSubCaption.Font);
Rp := GetClientRect;
//DrawStyle := DefaultStyle or DT_RIGHT;
// if FLayout <> tlTop then begin
// rp:=GetClientRect;
X := canvas.TextHeight('W');
Inc(Rp.Left, ClientWidth - canvas.TextWidth(FSubCaption.Caption) - FSubCaption.MarginLeft);
if Rp.Left < SubCaption.FMarginLeft + GlyphOffs then Rp.Left := SubCaption.FMarginLeft + GlyphOffs;
//SbCapOffs := Rp.Right - Rp.Left;
CR := Rp;
Inc(cr.Left, SubCaption.FMarginLeft);
rp.Top:= rp.Top+SubCaption.FMarginTop;
DoDrawText(FSubCaption.Caption, WorkBmp.Canvas, Self.Font, CR, DT_CALCRECT);
if FLayout = tlBottom then begin
OffsetRect(Rp, 0, Height - X - Abs(canvas.TextHeight('W') - X) div 2);
end else if FLayout = tlTop then begin
Font.Assign(Self.Font);
end else
OffsetRect(Rp, 0, (Height - CR.Bottom) div 2);
Canvas.TextRect(rp, rp.Left, rp.Top, FSubCaption.Caption);
end;
// Border Outer and Inner
// Canvas.Frame3d(rp, 1, bvRaised);
// </TextOut>
{ Release the working bitmap resources } { Release the working bitmap resources }
WorkBmp.Free; WorkBmp.Free;