made TColorButton a TCustomSpeedButton descendant from Tom Gregorovic

git-svn-id: trunk@10161 -
This commit is contained in:
mattias 2006-11-02 19:44:07 +00:00
parent 567453b914
commit 062208ae24
4 changed files with 201 additions and 74 deletions

View File

@ -287,7 +287,7 @@ type
FLayout: TButtonLayout;
FMargin: integer;
FMouseInControl: Boolean;
FShortcut: Longint;
FShortcut: TShortCut;
FShowAccelChar: boolean;
FSpacing: integer;
FTransparent: Boolean;
@ -326,6 +326,11 @@ type
procedure ActionChange(Sender: TObject; CheckDefaults: Boolean); override;
function GetActionLinkClass: TControlActionLinkClass; override;
procedure Loaded; override;
protected
function GetGlyphSize(PaintRect: TRect): TSize; virtual;
function GetTextSize(PaintRect: TRect): TSize; virtual;
function DrawGlyph(ACanvas: TCanvas; const AClient: TRect; const AOffset: TPoint;
AState: TButtonState; ATransparent: Boolean; BiDiFlags: Longint): TRect; virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;

View File

@ -225,40 +225,68 @@ type
{ TColorButton }
TColorButton = class(TGraphicControl)
TColorButton = class(TCustomSpeedButton)
private
FBorderWidth: integer;
FBorderWidth: Integer;
FButtonColorAutoSize: Boolean;
FButtonColorSize: Integer;
FButtonColor: TColor;
FColorDialog: TColorDialog;
FOnColorChanged: TNotifyEvent;
procedure SetBorderWidth(const AValue: integer);
function IsButtonColorAutoSizeStored: boolean;
procedure SetBorderWidth(const AValue: Integer);
procedure SetButtonColor(const AValue: TColor);
procedure SetButtonColorAutoSize(const AValue: Boolean);
procedure SetButtonColorSize(const AValue: Integer);
protected
procedure Click; override;
procedure Paint; override;
procedure SetButtonColor(Value: TColor);
procedure ShowColorDialog; virtual;
function GetGlyphSize(PaintRect: TRect): TSize; override;
function DrawGlyph(ACanvas: TCanvas; const AClient: TRect; const AOffset: TPoint;
AState: TButtonState; ATransparent: Boolean; BiDiFlags: Longint): TRect; override;
public
constructor Create(AnOwner: TComponent); override;
destructor Destroy; Override;
published
property Action;
property Align;
property Anchors;
property AllowAllUp;
property BorderSpacing;
property BorderWidth: integer read FBorderWidth write SetBorderWidth;
property ButtonColor:TColor read FButtonColor write SetButtonColor;
property BorderWidth: Integer read FBorderWidth write SetBorderWidth;
property ButtonColorAutoSize: Boolean read FButtonColorAutoSize
write SetButtonColorAutoSize
stored IsButtonColorAutoSizeStored;
property ButtonColorSize: Integer read FButtonColorSize write SetButtonColorSize;
property ButtonColor: TColor read FButtonColor write SetButtonColor;
property ColorDialog: TColorDialog read FColorDialog write FColorDialog;
property Constraints;
property Caption;
property Color;
property Down;
property Enabled;
property Flat;
property Font;
property Hint;
property OnChangeBounds;
property Layout;
property Margin;
property Spacing;
property Transparent;
property Visible;
property OnClick;
property OnColorChanged: TNotifyEvent read FOnColorChanged
write FOnColorChanged;
property OnDblClick;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnPaint;
property OnResize;
property OnChangeBounds;
property ShowHint;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property Visible;
end;

View File

@ -23,8 +23,10 @@
constructor TColorButton.Create(AnOwner: TComponent);
begin
Inherited Create(AnOwner);
FBorderWidth:=2;
SetInitialBounds(1,1,75,25);
FButtonColorSize := 16;
FBorderWidth := 2;
FButtonColorAutoSize := True;
SetInitialBounds(1, 1, 75, 25);
end;
destructor TColorButton.Destroy;
@ -32,56 +34,106 @@ Begin
inherited Destroy;
end;
procedure TColorButton.Paint;
var
ARect: TRect;
procedure TColorButton.SetButtonColor(const AValue: TColor);
begin
ARect:=Bounds(0, 0, Width, Height);
with Canvas do begin
Frame3d(ARect,FBorderWidth,bvRaised);
//debugln('TColorButton.Paint A ',dbgs(ARect));
InflateRect(ARect,-FBorderWidth-2,-FBorderWidth-2);
//debugln('TColorButton.Paint B ',dbgs(ARect));
Brush.Color:=clBlack;
FillRect(ARect);
InflateRect(ARect,-1,-1);
Brush.Color:=ButtonColor;
FillRect(ARect);
end;
inherited Paint;
end;
procedure TColorButton.SetButtonColor(Value:TColor);
begin
if Value=FButtonColor then exit;
FButtonColor:=Value;
if AValue = FButtonColor then Exit;
FButtonColor := AValue;
if Assigned(FOnColorChanged) and (not (csLoading in ComponentState)) then
FOnColorChanged(Self);
Invalidate;
end;
procedure TColorButton.SetBorderWidth(const AValue: Integer);
begin
if FBorderWidth = AValue then Exit;
FBorderWidth := AValue;
if FButtonColorAutoSize then Invalidate;
end;
function TColorButton.IsButtonColorAutoSizeStored: boolean;
begin
Result := FButtonColorAutoSize = False;
end;
procedure TColorButton.SetButtonColorAutoSize(const AValue: Boolean);
begin
if FButtonColorAutoSize = AValue then Exit;
FButtonColorAutoSize := AValue;
Invalidate;
end;
procedure TColorButton.ShowColorDialog;
var NewColor: TColor;
var
NewColor: TColor;
FreeDialog: Boolean;
begin
if FColorDialog<>nil then exit;
if not Enabled then exit;
NewColor:=ButtonColor;
FColorDialog:=TColorDialog.Create(Application);
if not Enabled then Exit;
FreeDialog := FColorDialog = nil;
if FColorDialog = nil then FColorDialog := TColorDialog.Create(Application);
try
FColorDialog.Color:=ButtonColor;
NewColor := ButtonColor;
FColorDialog.Color := ButtonColor;
if FColorDialog.Execute then
NewColor:=FColorDialog.Color;
NewColor := FColorDialog.Color;
finally
FColorDialog.Free;
FColorDialog:=nil;
if FreeDialog then FreeAndNil(FColorDialog);
end;
ButtonColor:=NewColor;
ButtonColor := NewColor;
end;
procedure TColorButton.SetBorderWidth(const AValue: integer);
function TColorButton.GetGlyphSize(PaintRect: TRect): TSize;
var
T: TSize;
S: Integer;
begin
if FBorderWidth=AValue then exit;
FBorderWidth:=AValue;
if ButtonColorAutoSize then
begin
T := GetTextSize(PaintRect);
if Caption = '' then S := 0
else S := Spacing;
if Layout in [blGlyphLeft, blGlyphRight] then
begin
Result.CX := PaintRect.Right - PaintRect.Left - 2 * BorderWidth - S - T.CX;
Result.CY := PaintRect.Bottom - PaintRect.Top - 2 * BorderWidth;
end
else
begin
Result.CX := PaintRect.Right - PaintRect.Left - 2 * BorderWidth;
Result.CY := PaintRect.Bottom - PaintRect.Top - 2 * BorderWidth - S - T.CY;
end;
end
else
begin
Result.CX := ButtonColorSize;
Result.CY := ButtonColorSize;
end;
end;
function TColorButton.DrawGlyph(ACanvas: TCanvas; const AClient: TRect;
const AOffset: TPoint; AState: TButtonState; ATransparent: Boolean;
BiDiFlags: Longint): TRect;
var
Size: TSize;
begin
Canvas.Pen.Color := clBlack;
Canvas.Brush.Color := ButtonColor;
Size := GetGlyphSize(AClient);
Result:=Bounds(AClient.Left + AOffset.X, AClient.Top + AOffset.Y,
Size.CX - 1, Size.CY - 1);
Canvas.Rectangle(Result);
end;
procedure TColorButton.SetButtonColorSize(const AValue: Integer);
begin
if FButtonColorSize = AValue then Exit;
FButtonColorSize := AValue;
Invalidate;
end;
@ -91,4 +143,5 @@ begin
ShowColorDialog;
end;
// included by buttons.pp

View File

@ -264,8 +264,9 @@ end;
------------------------------------------------------------------------------}
procedure TCustomSpeedButton.RealSetText(const Value: TCaption);
begin
if Caption=Value then exit;
if Caption = Value then Exit;
inherited RealSetText(Value);
Invalidate;
end;
@ -413,7 +414,7 @@ var
PaintRect: TRect;
GlyphWidth, GlyphHeight: Integer;
Offset, OffsetCap: TPoint;
ClientSize, TotalSize, TextSize: TSize;
ClientSize, TotalSize, TextSize, GlyphSize: TSize;
//BrushStyle : TBrushStyle;
M, S : integer;
TXTStyle : TTextStyle;
@ -469,39 +470,25 @@ begin
Canvas.FillRect(PaintRect);
end;
GlyphWidth:= TButtonGlyph(FGlyph).Glyph.Width;
GlyphSize := GetGlyphSize(PaintRect);
GlyphWidth := GlyphSize.CX;
if TButtonGlyph(FGlyph).NumGlyphs > 1 then
GlyphWidth:=GlyphWidth div NumGlyphs;
GlyphHeight:=TButtonGlyph(FGlyph).Glyph.Height;
GlyphHeight := GlyphSize.CY;
ClientSize.cx:= PaintRect.Right - PaintRect.Left;
ClientSize.cy:= PaintRect.Bottom - PaintRect.Top;
if Caption <> '' then begin
TMP := Caption;
TXTStyle := Canvas.TextStyle;
TXTStyle.Opaque := False;
TXTStyle.Clipping := True;
TXTStyle.ShowPrefix := ShowAccelChar;
TXTStyle.Alignment := taLeftJustify;
TXTStyle.Layout := tlTop;
TXTStyle.SystemFont := Canvas.Font.IsDefault;//Match System Default Style
SIndex := DeleteAmpersands(TMP);
fRect:=PaintRect;
Flags:=DT_CalcRect;
if not TXTStyle.SingleLine then inc(Flags,DT_WordBreak);
TextSize := GetTextSize(PaintRect);
DrawText(canvas.Handle,pChar(TMP),Length(TMP),fRect,flags);
TextSize.cy:=fRect.bottom-fRect.top;
TextSize.cx:=fRect.right-fRect.left;
if Caption <> '' then
begin
TMP := Caption;
SIndex := DeleteAmpersands(TMP);
If SIndex > 0 then
If SIndex <= Length(TMP) then begin
FShortcut := Ord(TMP[SIndex]);
end;
end
else begin
TextSize.cx:= 0;
TextSize.cy:= 0;
end;
if (GlyphWidth = 0) or (GlyphHeight = 0)
@ -569,8 +556,16 @@ begin
end;
end;
FGlyph.Draw(Canvas, PaintRect, Offset, FState, Transparent, 0);
if Caption <> '' then begin
DrawGlyph(Canvas, PaintRect, Offset, FState, Transparent, 0);
if Caption <> '' then
begin
TXTStyle := Canvas.TextStyle;
TXTStyle.Opaque := False;
TXTStyle.Clipping := True;
TXTStyle.ShowPrefix := ShowAccelChar;
TXTStyle.Alignment := taLeftJustify;
TXTStyle.Layout := tlTop;
TXTStyle.SystemFont := Canvas.Font.IsDefault;//Match System Default Style
With PaintRect, OffsetCap do begin
Left := Left + X;
@ -751,6 +746,52 @@ begin
if FDownBuffered then SetDown(FDownBuffered);
end;
function TCustomSpeedButton.GetGlyphSize(PaintRect: TRect): TSize;
begin
Result.CX := TButtonGlyph(FGlyph).Glyph.Width;
Result.CY := TButtonGlyph(FGlyph).Glyph.Height;
end;
function TCustomSpeedButton.GetTextSize(PaintRect: TRect): TSize;
var
TMP: String;
TXTStyle: TTextStyle;
Flags: Cardinal;
begin
if Caption <> '' then
begin
TMP := Caption;
TXTStyle := Canvas.TextStyle;
TXTStyle.Opaque := False;
TXTStyle.Clipping := True;
TXTStyle.ShowPrefix := ShowAccelChar;
TXTStyle.Alignment := taLeftJustify;
TXTStyle.Layout := tlTop;
TXTStyle.SystemFont := Canvas.Font.IsDefault;//Match System Default Style
DeleteAmpersands(TMP);
Flags := DT_CalcRect;
if not TXTStyle.SingleLine then Inc(Flags, DT_WordBreak);
DrawText(Canvas.Handle, PChar(TMP), Length(TMP), PaintRect, Flags);
Result.CY := PaintRect.Bottom - PaintRect.Top;
Result.CX := PaintRect.Right - PaintRect.Left;
end
else
begin
Result.CX:= 0;
Result.CX:= 0;
end;
end;
function TCustomSpeedButton.DrawGlyph(ACanvas: TCanvas; const AClient: TRect;
const AOffset: TPoint; AState: TButtonState; ATransparent: Boolean;
BiDiFlags: Longint): TRect;
begin
if Assigned(FGlyph) then
Result := FGlyph.Draw(ACanvas, AClient, AOffset, AState, ATransparent, BiDiFlags);
end;
{------------------------------------------------------------------------------
Method: TCustomSpeedButton.CMEnabledChanged
Params: Message: