unit kz.ButtonBar;
(*
Project: ButtonBar, basicly a Panel with 4 different autosized buttons on
Autor: KodeZwerg
Created with: Lazarus 2.2.4 (rev lazarus_2_2_4) FPC 3.2.2 x86_64-win64-win32/win64
Tested under: Windows 10 64bit / 64bit compile
Version: 1.1
Licence: TheUnlicence
History: 1.0, stable first release
1.1, added Font property
*)
{$mode ObjFPC}{$H+}
interface
uses
Classes , SysUtils,
Forms, Graphics,
Controls, ExtCtrls, StdCtrls;
type
TkzButtonBar = class(TWinControl)
strict private
FOwner: TComponent;
FParent: TControl;
FWinParent: TWinControl;
FPanel: TPanel;
FAlign: TAlign;
FButtonClick1,
FButtonClick2,
FButtonClick3,
FButtonClick4,
FOnResize: TNotifyEvent;
FButtonCaption1,
FButtonCaption2,
FButtonCaption3,
FButtonCaption4: TCaption;
FButton1,
FButton2,
FButton3,
FButton4: TButton;
FFont: TFont;
FLeft,
FTop,
FWidth,
FHeight: Integer;
FBorderSpacing: TControlBorderSpacing;
private
procedure SetAlign(const AValue: TAlign);
procedure SetLeft(const AValue: Integer);
procedure SetTop(const AValue: Integer);
procedure SetWidth(const AValue: Integer);
procedure SetHeight(const AValue: Integer);
procedure SetFont(const AValue: TFont);
procedure SetButtonCaption1(const AValue: TCaption);
procedure SetButtonCaption2(const AValue: TCaption);
procedure SetButtonCaption3(const AValue: TCaption);
procedure SetButtonCaption4(const AValue: TCaption);
procedure SetBorderSpacing(const AValue: TControlBorderSpacing);
procedure SetDefaults;
procedure CreatePanel;
procedure Resize;
procedure OnResize(ASender: TObject);
procedure OnButtonClick1(ASender: TObject);
procedure OnButtonClick2(ASender: TObject);
procedure OnButtonClick3(ASender: TObject);
procedure OnButtonClick4(ASender: TObject);
public
constructor Create(const AOwner: TComponent; const AParent: TControl); overload;
constructor Create(const AOwner: TComponent; const AParent: TWinControl); overload;
public
property Align: TAlign read FAlign write SetAlign default alNone;
property Left: Integer read FLeft write SetLeft default 0;
property Top: Integer read FTop write SetTop default 0;
property Width: Integer read FWidth write SetWidth default 200;
property Height: Integer read FHeight write SetHeight default 19;
property Font: TFont read FFont write SetFont;
property ButtonClick1: TNotifyEvent read FButtonClick1 write FButtonClick1;
property ButtonClick2: TNotifyEvent read FButtonClick2 write FButtonClick2;
property ButtonClick3: TNotifyEvent read FButtonClick3 write FButtonClick3;
property ButtonClick4: TNotifyEvent read FButtonClick4 write FButtonClick4;
property ButtonCaption1: TCaption read FButtonCaption1 write SetButtonCaption1;
property ButtonCaption2: TCaption read FButtonCaption2 write SetButtonCaption2;
property ButtonCaption3: TCaption read FButtonCaption3 write SetButtonCaption3;
property ButtonCaption4: TCaption read FButtonCaption4 write SetButtonCaption4;
property BorderSpacing: TControlBorderSpacing read FBorderSpacing write SetBorderSpacing;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('KodeZwerg', [TkzButtonBar]);
end;
constructor TkzButtonBar.Create(const AOwner: TComponent; const AParent: TControl);
begin
inherited Create(AOwner);
FOwner := AOwner;
FParent := AParent;
FWinParent := nil;
FBorderSpacing := nil;
FPanel := nil;
FButton1 := nil;
FButton2 := nil;
FButton3 := nil;
FButton4 := nil;
FFont := nil;
FButtonClick1 := nil;
FButtonClick2 := nil;
FButtonClick3 := nil;
FButtonClick4 := nil;
FOnResize := @OnResize;
SetDefaults;
CreatePanel;
end;
constructor TkzButtonBar.Create(const AOwner: TComponent; const AParent: TWinControl);
begin
inherited Create(AOwner);
FOwner := AOwner;
FParent := nil;
FWinParent := AParent;
FBorderSpacing := nil;
FPanel := nil;
FButton1 := nil;
FButton2 := nil;
FButton3 := nil;
FButton4 := nil;
FFont := nil;
FButtonClick1 := nil;
FButtonClick2 := nil;
FButtonClick3 := nil;
FButtonClick4 := nil;
FOnResize := @OnResize;
SetDefaults;
CreatePanel;
end;
procedure TkzButtonBar.OnResize(ASender: TObject);
begin
Resize;
end;
procedure TkzButtonBar.CreatePanel;
begin
FPanel := TPanel.Create(FOwner);
try
if (FParent <> nil) then
FPanel.Parent := TWinControl(FParent)
else
if (FWinParent <> nil) then
FPanel.Parent := FWinParent
else
FPanel.Parent := nil;
FPanel.Left := FLeft;
FPanel.Top := FTop;
FPanel.Width := FWidth;
FPanel.Height := FHeight;
FPanel.Align := FAlign;
FPanel.DoubleBuffered := True;
FPanel.BevelInner := bvNone;
FPanel.BevelOuter := bvNone;
FPanel.OnResize := FOnResize;
FPanel.ParentFont := True;
FFont := TFont.Create;
try
FFont := Screen.SystemFont;
finally
FPanel.ParentFont := False;
FPanel.Font := FFont;
end;
FBorderSpacing := TControlBorderSpacing.Create(FPanel, nil);
try
FBorderSpacing.Around := 5;
FBorderSpacing.CellAlignHorizontal := ccaFill;
FBorderSpacing.CellAlignVertical := ccaFill;
FBorderSpacing.InnerBorder := 0;
FBorderSpacing.Left := 0;
FBorderSpacing.Right := 0;
FBorderSpacing.Top := 0;
FBorderSpacing.Bottom := 0;
finally
FPanel.BorderSpacing := FBorderSpacing;
end;
FButton1 := TButton.Create(FPanel);
try
FButton1.Parent := FPanel;
FButton1.Align := alLeft;
FButton1.ParentFont := True;
FButton1.BorderSpacing.Left := FBorderSpacing.Around;
FButton1.BorderSpacing.Right := FBorderSpacing.Around;
finally
FButton1.Caption := FButtonCaption1;
FButton1.OnClick := @OnButtonClick1;
end;
FButton2 := TButton.Create(FPanel);
try
FButton2.Parent := FPanel;
FButton2.Align := alLeft;
FButton2.ParentFont := True;
FButton2.BorderSpacing.Left := FBorderSpacing.Around;
FButton2.BorderSpacing.Right := FBorderSpacing.Around;
finally
FButton2.Caption := FButtonCaption2;
FButton2.OnClick := @OnButtonClick2;
end;
FButton3 := TButton.Create(FPanel);
try
FButton3.Parent := FPanel;
FButton3.Align := alLeft;
FButton3.ParentFont := True;
FButton3.BorderSpacing.Left := FBorderSpacing.Around;
FButton3.BorderSpacing.Right := FBorderSpacing.Around;
finally
FButton3.Caption := FButtonCaption3;
FButton3.OnClick := @OnButtonClick3;
end;
FButton4 := TButton.Create(FPanel);
try
FButton4.Parent := FPanel;
FButton4.Align := alClient;
FButton4.ParentFont := True;
FButton4.BorderSpacing.Left := FBorderSpacing.Around;
FButton4.BorderSpacing.Right := FBorderSpacing.Around;
finally
FButton4.Caption := FButtonCaption4;
FButton4.OnClick := @OnButtonClick4;
end;
finally
FPanel.Caption := '';
Resize;
end;
end;
procedure TkzButtonBar.Resize;
begin
FLeft := FPanel.Left;
FTop := FPanel.Top;
FWidth := FPanel.Width;
FHeight := FPanel.Height;
if (FButton1 <> nil) then
begin
FButton1.Width := (FPanel.Width div 4) - (1 * FBorderSpacing.Around);
FButton1.Left := 1;
end;
if (FButton2 <> nil) then
begin
FButton2.Width := (FPanel.Width div 4) - (1 * FBorderSpacing.Around);
FButton2.Left := 2;
end;
if (FButton3 <> nil) then
begin
FButton3.Width := (FPanel.Width div 4) - (1 * FBorderSpacing.Around);
FButton3.Left := 3;
end;
if (FButton4 <> nil) then
begin
FButton4.Width := (FPanel.Width div 4) - (1 * FBorderSpacing.Around);
FButton4.Left := 4;
end;
end;
procedure TkzButtonBar.SetFont(const AValue: TFont);
begin
FFont := AValue;
if (FPanel <> nil) then
FPanel.Font := FFont;
end;
procedure TkzButtonBar.SetBorderSpacing(const AValue: TControlBorderSpacing);
begin
if (FBorderSpacing = nil) then
if (FPanel <> nil) then
try
FBorderSpacing := TControlBorderSpacing.Create(FPanel, nil);
finally
end
else
FBorderSpacing := TControlBorderSpacing.Create(nil, nil);
FBorderSpacing := AValue;
if (FPanel <> nil) then
FPanel.BorderSpacing := FBorderSpacing;
if (FButton1 <> nil) then
FButton1.BorderSpacing := FBorderSpacing;
if (FButton2 <> nil) then
FButton2.BorderSpacing := FBorderSpacing;
if (FButton3 <> nil) then
FButton3.BorderSpacing := FBorderSpacing;
if (FButton4 <> nil) then
FButton4.BorderSpacing := FBorderSpacing;
end;
procedure TkzButtonBar.SetDefaults;
begin
FAlign := alNone;
FLeft := 0;
FTop := 0;
FWidth := 200;
FHeight := 19;
FButtonCaption1 := '<<';
FButtonCaption2 := '<';
FButtonCaption3 := '>';
FButtonCaption4 := '>>';
end;
procedure TkzButtonBar.SetAlign(const AValue: TAlign);
begin
FAlign := AValue;
FPanel.Align := FAlign;
Resize;
end;
procedure TkzButtonBar.SetLeft(const AValue: Integer);
begin
FLeft := AValue;
FPanel.Left := FLeft;
end;
procedure TkzButtonBar.SetTop(const AValue: Integer);
begin
FTop := AValue;
FPanel.Top := FTop;
end;
procedure TkzButtonBar.SetWidth(const AValue: Integer);
begin
FWidth := AValue;
FPanel.Width := FWidth;
Resize;
end;
procedure TkzButtonBar.SetHeight(const AValue: Integer);
begin
FHeight := AValue;
FPanel.Height := FHeight;
Resize;
end;
procedure TkzButtonBar.SetButtonCaption1(const AValue: TCaption);
begin
FButtonCaption1 := AValue;
FButton1.Caption := FButtonCaption1;
end;
procedure TkzButtonBar.SetButtonCaption2(const AValue: TCaption);
begin
FButtonCaption2 := AValue;
FButton2.Caption := FButtonCaption2;
end;
procedure TkzButtonBar.SetButtonCaption3(const AValue: TCaption);
begin
FButtonCaption3 := AValue;
FButton3.Caption := FButtonCaption3;
end;
procedure TkzButtonBar.SetButtonCaption4(const AValue: TCaption);
begin
FButtonCaption4 := AValue;
FButton4.Caption := FButtonCaption4;
end;
procedure TkzButtonBar.OnButtonClick1(ASender: TObject);
begin
if Assigned(FButtonClick1) then
FButtonClick1(Self);
end;
procedure TkzButtonBar.OnButtonClick2(ASender: TObject);
begin
if Assigned(FButtonClick2) then
FButtonClick2(Self);
end;
procedure TkzButtonBar.OnButtonClick3(ASender: TObject);
begin
if Assigned(FButtonClick3) then
FButtonClick3(Self);
end;
procedure TkzButtonBar.OnButtonClick4(ASender: TObject);
begin
if Assigned(FButtonClick4) then
FButtonClick4(Self);
end;
end.