From 0c024f25e61e39eb03d5bbd9c499252d65ddc575 Mon Sep 17 00:00:00 2001 From: blikblum Date: Mon, 1 Mar 2010 18:16:55 +0000 Subject: [PATCH] * Use the font orientation feature to draw rotate text instead of using an intermediary rotated bitmap * Add urotatebitmap to the package file so the ide can track changes in it (Changes acknowledged by eugen) git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1173 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/gradcontrols/gradcontrols.lpk | 6 +- components/gradcontrols/gradcontrols.pas | 9 +- components/gradcontrols/src/ugradbtn.pas | 79 +++++-------- components/gradcontrols/src/urotatebitmap.pas | 110 ++++-------------- 4 files changed, 65 insertions(+), 139 deletions(-) diff --git a/components/gradcontrols/gradcontrols.lpk b/components/gradcontrols/gradcontrols.lpk index d8f0b9058..f2a990f21 100644 --- a/components/gradcontrols/gradcontrols.lpk +++ b/components/gradcontrols/gradcontrols.lpk @@ -15,7 +15,7 @@ - + @@ -26,6 +26,10 @@ + + + + diff --git a/components/gradcontrols/gradcontrols.pas b/components/gradcontrols/gradcontrols.pas index 08bb53293..0785cb28a 100644 --- a/components/gradcontrols/gradcontrols.pas +++ b/components/gradcontrols/gradcontrols.pas @@ -1,16 +1,13 @@ -{ Diese Datei wurde automatisch von Lazarus erzeugt. Sie darf nicht bearbeitet - werden! - Dieser Quelltext dient nur dem Übersetzen und Installieren des Packages. +{ This file was automatically created by Lazarus. do not edit ! + This source is only used to compile and install the package. } unit gradcontrols; interface - - uses - ugradtabcontrol, ugradbtn, LazarusPackageIntf; + ugradtabcontrol, ugradbtn, uRotateBitmap, LazarusPackageIntf; implementation diff --git a/components/gradcontrols/src/ugradbtn.pas b/components/gradcontrols/src/ugradbtn.pas index 4bff2e922..afe79233c 100644 --- a/components/gradcontrols/src/ugradbtn.pas +++ b/components/gradcontrols/src/ugradbtn.pas @@ -46,7 +46,6 @@ type FNormalBackgroundCache, FHotBackgroundCache, FDownBackgroundCache, FDisabledBackgroundCache : TBitmap; FRotatedGlyph : TRotatedGlyph; - FRotatedText : TRotatedText; FTextGlyphSpacing: Integer; FGradientType : TGradientType; FShowFocusBorder, FOnlyBackground, @@ -216,7 +215,6 @@ end; procedure TGradButton.TextChanged; begin inherited TextChanged; - FRotatedText.Text := Caption; if FAutoWidth then UpdateButton @@ -287,9 +285,15 @@ begin tempGS.cy:=FRotatedGlyph.Height; end; - //tempTS := Canvas.TextExtent(Caption); - tempTS.cx:= FRotatedText.Width; - tempTS.cy:= FRotatedText.Height; + tempTS := bm.Canvas.TextExtent(Caption); + if FRotateDirection <> rdNormal then + begin + FTextSize.cx := tempTS.cy; + FTextSize.cy := tempTS.cx; + tempTS := FTextSize; + end + else + FTextSize := tempTS; tempBL := FButtonLayout; @@ -356,9 +360,6 @@ begin WritePoint(FGlyphPoint); {$ENDIF} - //tempTS := Canvas.TextExtent(Caption); - - FTextSize:=tempTS; FGlyphSize:=tempGS; end; @@ -366,11 +367,11 @@ function TGradButton.GetAutoWidth: Integer; begin if FShowGlyph then begin if FButtonLayout in [blGlyphLeft,blGlyphRight] then - Result := FRotatedText.Width+ FRotatedGlyph.Width+FTextGlyphSpacing+FAutoWidthBorderSpacing + Result := FTextSize.cx+ FRotatedGlyph.Width+FTextGlyphSpacing+FAutoWidthBorderSpacing else - Result := Max(FRotatedText.Width,FRotatedGlyph.Width)+FAutoWidthBorderSpacing; + Result := Max(FTextSize.cx,FRotatedGlyph.Width)+FAutoWidthBorderSpacing; end else begin - Result := FRotatedText.Width+FAutoWidthBorderSpacing; + Result := FTextSize.cx+FAutoWidthBorderSpacing; end; end; @@ -378,11 +379,11 @@ function TGradButton.GetAutoHeight: Integer; begin if FShowGlyph then begin if FButtonLayout in [blGlyphTop,blGlyphBottom] then - Result := FRotatedText.Height+ FRotatedGlyph.Height+FTextGlyphSpacing+FAutoHeightBorderSpacing + Result := FTextSize.cy+ FRotatedGlyph.Height+FTextGlyphSpacing+FAutoHeightBorderSpacing else - Result := Max(FRotatedText.Height,FRotatedGlyph.Height)+FAutoHeightBorderSpacing; + Result := Max(FTextSize.cy,FRotatedGlyph.Height)+FAutoHeightBorderSpacing; end else begin - Result := FRotatedText.Height+FAutoHeightBorderSpacing; + Result := FTextSize.cy+FAutoHeightBorderSpacing; end; end; @@ -676,7 +677,6 @@ begin FRotateDirection:=Value; //Rotate and Cache - FRotatedText.Direction:=FRotateDirection; FRotatedGlyph.Direction:=FRotateDirection; UpdatePositions; @@ -861,7 +861,6 @@ begin FRotatedGlyph := TRotatedGlyph.Create; FRotatedGlyph.OnChange := @GlyphChanged; - FRotatedText := TRotatedText.Create; FButtonLayout:=blGlyphLeft; FGlyphBackgroundColor:=clWhite; @@ -873,6 +872,7 @@ begin FBorderSides:=[bsTopLine,bsBottomLine,bsLeftLine,bsRightLine]; bm := TBitmap.Create; + bm.Canvas.Brush.Style := bsClear; UpdateBackground; @@ -885,8 +885,6 @@ begin bm.Free; //DebugLn('FRotatedGlyph.Free'); FRotatedGlyph.Free; - //DebugLn('FRotatedText.Free'); - FRotatedText.Free; //DebugLn('FBackground.Free'); FBackground.Free; //DebugLn('FNormalBackgroundCache.Free'); @@ -992,7 +990,7 @@ procedure TGradButton.FontChanged(Sender: TObject); begin inherited FontChanged(Sender); - FRotatedText.Font := Font; + bm.Canvas.Font := Font; UpdatePositions; end; @@ -1031,19 +1029,12 @@ end; procedure TGradButton.Paint; var - radius, r,t,p,x,y : Integer; - rt,pr : TRect; - t1,t2,t3 : TColor; - s : String; - r1,r2 : Extended; - tempState : TButtonState; - temp : TBitmap; + TextOffset : Integer; + tempState: TButtonState; begin if not HasParent then Exit; - tempState:=FState; - with bm do begin Width := Self.Width; @@ -1055,13 +1046,10 @@ begin Canvas.Brush.Color:=clBlack; Canvas.FillRect(0,0,Width, Height); - p := 0; - - if tempState = bsDown then - p := 1; - - if not FEnabled then tempState := bsDisabled; - + if not FEnabled then + tempState := bsDisabled + else + tempState := FState; case tempState of bsUp : Canvas.Draw(0,0,FNormalBackgroundCache); bsDown: Canvas.Draw(0,0,FDownBackgroundCache); @@ -1069,28 +1057,25 @@ begin else Canvas.Draw(0,0,FDisabledBackgroundCache); end; - if Caption <> '' then - FRotatedText.Draw(bm.Canvas, FTextPoint.x+p, FTextPoint.y+p); + TextOffset := IfThen(tempState = bsDown, 1); - if FShowGlyph AND FRotatedGlyph.IsBitmapStored then + DrawRotatedText(Canvas, FTextPoint.x + TextOffset, FTextPoint.y + TextOffset, + FTextSize.cx, FTextSize.cy, Caption, FRotateDirection); + + if FShowGlyph and FRotatedGlyph.IsBitmapStored then begin - if not FEnabled then - tempState := bsDisabled - else - tempState := FState; - - FRotatedGlyph.State:=tempState; - FRotatedGlyph.Draw(bm.Canvas, FGlyphPoint.x+p, FGlyphPoint.y+p); + FRotatedGlyph.State := FState; + FRotatedGlyph.Draw(bm.Canvas, FGlyphPoint.x+TextOffset, FGlyphPoint.y+TextOffset); end; if not (csDesigning in ComponentState) then - if FFocused AND FShowFocusBorder then + if FFocused and FShowFocusBorder then Canvas.DrawFocusRect(RECT(FBackgroundRect.Left+2, FBackgroundRect.Top+2, FBackgroundRect.Right-2, FBackgroundRect.Bottom-2)); end; Canvas.Draw(0,0,bm); - + inherited Paint; end; diff --git a/components/gradcontrols/src/urotatebitmap.pas b/components/gradcontrols/src/urotatebitmap.pas index 93876e223..d27860ab6 100644 --- a/components/gradcontrols/src/urotatebitmap.pas +++ b/components/gradcontrols/src/urotatebitmap.pas @@ -74,25 +74,11 @@ type property Transparent; end; - { TRotatedText } - - TRotatedText = class (TCustomRotatedBitmap) - private - FText : String; - procedure SetFont(const AValue: TFont); - procedure SetText(const Value: String); - procedure UpdateText; - protected - function GetWidth: Integer; override; - function GetHeight: Integer; override; - public - constructor Create; override; - property Font: TFont write SetFont; - property Text: String read FText write SetText; - end; - function CreateRotatedBitmap(SrcImage: TRasterImage; Direction: TRotateDirection): TBitmap; +procedure DrawRotatedText(Canvas: TCanvas; X, Y, TextWidth, TextHeight: Integer; + const Text: String; Direction: TRotateDirection); + implementation uses @@ -140,6 +126,28 @@ begin NormalImg.Free; end; +procedure DrawRotatedText(Canvas: TCanvas; X, Y, TextWidth, TextHeight: Integer; + const Text: String; Direction: TRotateDirection); +begin + case Direction of + rdNormal: + begin + Canvas.Font.Orientation := 0; + Canvas.TextOut(X, Y, Text); + end; + rdLeft: + begin + Canvas.Font.Orientation := 900; + Canvas.TextOut(X, Y + TextHeight, Text); + end; + rdRight: + begin + Canvas.Font.Orientation := -900; + Canvas.TextOut(X + TextWidth, Y, Text); + end; + end; +end; + { TCustomRotatedBitmap } function TCustomRotatedBitmap.GetBitmap: TBitmap; @@ -284,73 +292,5 @@ begin FGlyph.Draw(Canvas, R, P, FButtonState, Transparent, 0); end; -{ TRotatedText } - -procedure TRotatedText.SetText(const Value: String); -begin - if Value = FText then - Exit; - FText := Value; - UpdateText; -end; - -procedure TRotatedText.SetFont(const AValue: TFont); -begin - FNormalBitmap.Canvas.Font := AValue; - FNormalBitmap.Canvas.Font.Quality := fqNonAntialiased; - - UpdateText; -end; - -procedure TRotatedText.UpdateText; -var - TextSize : TSize; - TransColor: TColor; -begin - FNormalBitmap.Canvas.Font.Quality := fqNonAntialiased; - - with FNormalBitmap, Canvas do - begin - TextSize := TextExtent(FText); - {$if defined(LCLWin32) or defined (LCLQt)} - //win32 and Qt does not comput correct text extent when Italic style is set. - //gtk1/2 does not support Italic at all - if fsItalic in Font.Style then - Inc(TextSize.cx, 4); - {$endif} - SetSize(TextSize.cx, TextSize.cy); - if Font.Color <> clFuchsia then - TransColor := clFuchsia - else - TransColor := clWhite; - Brush.Color := TransColor; - FillRect(0, 0, FNormalBitmap.Width, FNormalBitmap.Height); - TextOut(0, 0, FText); - Mask(TransColor); - end; -end; - -function TRotatedText.GetWidth: Integer; -begin - if FText <> '' then - Result := inherited GetWidth - else - Result := 0; -end; - -function TRotatedText.GetHeight: Integer; -begin - if FText <> '' then - Result := inherited GetHeight - else - Result := 0; -end; - -constructor TRotatedText.Create; -begin - inherited Create; - Transparent := True; -end; - end.