Skip to content

Commit

Permalink
Fixed #65: Caption for StyledBitBtn with wordwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
carloBarazzetta committed Nov 30, 2024
1 parent fea5b95 commit 0ec9a81
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
27 changes: 16 additions & 11 deletions source/Vcl.ButtonStylesAttributes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,8 @@ procedure CalcImageAndTextRect(const ASurfaceRect: TRect;
IW, IH, IX, IY: Integer;
LImageAlignment: TImageAlignment;
begin
//Calculate Image and Text Rect for Drawing using ImageAlignment and ImageMargins
//for StyledButton and StyledGraphicButton
if ACaption = '' then
LImageAlignment := iaCenter
else
Expand Down Expand Up @@ -1722,6 +1724,8 @@ procedure CalcImageAndTextRect(const ACanvas: TCanvas;
LLayout: TButtonLayout;
LMargin, LSpacing: Integer;
begin
//Calculate Image and Text Rect for Drawing using ButtonLayout, Margin and Spacing
//For StyledSpeedButton and StyledBitBtn
LLayout := ALayout;
if (ABiDiFlags and DT_RIGHT) = DT_RIGHT then
begin
Expand Down Expand Up @@ -1835,7 +1839,8 @@ procedure CalcImageAndTextRect(const ACanvas: TCanvas;
Inc(AGlyphPos.X, ASurfaceRect.Left + AOffset.X);
Inc(AGlyphPos.Y, ASurfaceRect.Top + AOffset.Y);

OffsetRect(ATextBounds, LTextPos.X + ASurfaceRect.Left + AOffset.X, LTextPos.Y + ASurfaceRect.Top + AOffset.Y);
OffsetRect(ATextBounds, LTextPos.X + ASurfaceRect.Left + AOffset.X,
LTextPos.Y + ASurfaceRect.Top + AOffset.Y);
end;

procedure DrawIconFromCommandLinkRes(ACanvas: TCanvas; ARect: TRect;
Expand Down Expand Up @@ -2044,7 +2049,7 @@ procedure DrawButtonText(const ACanvas: TCanvas;
const PreserveBorders: Boolean = True);
var
R: TRect;
OldBKMode: Integer;
LOldBKMode: Integer;
begin
(* for test
ACanvas.Brush.Color := clYellow;
Expand All @@ -2065,7 +2070,7 @@ procedure DrawButtonText(const ACanvas: TCanvas;
else
OffsetRect(R, (ARect.Width - R.Width) div 2, (ARect.Height - R.Height) div 2);
end;
OldBKMode := SetBkMode(ACanvas.Handle, Winapi.Windows.TRANSPARENT);
LOldBKMode := SetBkMode(ACanvas.Handle, Winapi.Windows.TRANSPARENT);
try
if PreserveBorders then
begin
Expand All @@ -2075,13 +2080,13 @@ procedure DrawButtonText(const ACanvas: TCanvas;
R.Top := ARect.Top + ABorderWidth + ASpacing ;
if R.Bottom > ARect.Bottom - ABorderWidth - Aspacing then
R.Bottom := ARect.Bottom - ABorderWidth - Aspacing;
(* for test
ACanvas.Brush.Color := clRed;
ACanvas.FillRect(R);
ACanvas.Pen.Color := clBlue;
ACanvas.Pen.Width := 1;
ACanvas.Rectangle(R);
*)
(* for test
ACanvas.Brush.Color := clRed;
ACanvas.FillRect(R);
ACanvas.Pen.Color := clBlue;
ACanvas.Pen.Width := 1;
ACanvas.Rectangle(R);
*)
Winapi.Windows.DrawText(ACanvas.Handle, PChar(AText),
Length(AText), R, ABidiFlags or DT_END_ELLIPSIS);
end
Expand All @@ -2097,7 +2102,7 @@ procedure DrawButtonText(const ACanvas: TCanvas;
Length(AText), R, ABidiFlags);
end;
finally
SetBkMode(ACanvas.Handle, OldBKMode);
SetBkMode(ACanvas.Handle, LOldBKMode);
end;
end;

Expand Down
24 changes: 21 additions & 3 deletions source/Vcl.StyledButton.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,7 @@ TStyledBitBtn = class(TCustomStyledButton)
property ModalResult;
property NotificationBadge;
property NumGlyphs;
property Style: TButtonStyle read FStyle write FStyle;
property Style: TButtonStyle read FStyle write FStyle default bsAutoDetect;
property Spacing default 4;
property TabOrder;
property TabStop;
Expand Down Expand Up @@ -2654,6 +2654,7 @@ procedure TStyledButtonRender.DrawCaptionAndImage(const ACanvas: TCanvas;
LUseImageList: Boolean;
LGlyphPos: TPoint;
LCaption: TCaption;
LOldBKMode, LMaxBorderWidth: Integer;
begin
if FShowCaption then
LCaption := GetCaptionToDraw
Expand Down Expand Up @@ -2785,8 +2786,24 @@ procedure TStyledButtonRender.DrawCaptionAndImage(const ACanvas: TCanvas;
end;
end
else
DrawButtonText(ACanvas, LCaption, FCaptionAlignment, FSpacing, CalcMaxBorderWidth,
LTextRect, LTextFlags);
begin
LMaxBorderWidth := CalcMaxBorderWidth;
if FUseButtonLayout then
begin
LOldBKMode := SetBkMode(ACanvas.Handle, Winapi.Windows.TRANSPARENT);
try
Winapi.Windows.DrawText(ACanvas.Handle, PChar(LCaption),
Length(LCaption), LTextRect, LTextFlags or DT_END_ELLIPSIS);
finally
SetBkMode(ACanvas.Handle, LOldBKMode);
end;
end
else
begin
DrawButtonText(ACanvas, LCaption, FCaptionAlignment, FSpacing, LMaxBorderWidth,
LTextRect, LTextFlags);
end;
end;
end;

procedure TStyledButtonRender.DrawButton(const ACanvas: TCanvas;
Expand Down Expand Up @@ -6311,6 +6328,7 @@ procedure TCustomStyledButton.WMPaint(var Message: TMessage);
constructor TStyledBitBtn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FStyle := bsAutoDetect;
FRender.FUseButtonLayout := True;
FRender.Spacing := 4;
end;
Expand Down

0 comments on commit 0ec9a81

Please sign in to comment.