unit RulerPropertiesWin;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls;

type
  TStaticText = class(StdCtrls.TStaticText)
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  end;

  TRulerPropertiesFrm = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    lblFont: TStaticText;
    lblBg: TStaticText;
    lblWidth: TStaticText;
    stFontName: TStaticText;
    btnFontChange: TButton;
    cbBgColor: TColorBox;
    eWidth: TEdit;
    btnOk: TButton;
    btnCancel: TButton;
    btnApply: TButton;
    lblFg: TStaticText;
    cbFgColor: TColorBox;
    rbDefColors: TRadioButton;
    rbCustColors: TRadioButton;
    procedure btnOkClick(Sender: TObject);
    procedure btnApplyClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnFontChangeClick(Sender: TObject);
    procedure UpdateColorsEnabled(Sender: TObject);
    procedure cbBgColorChange(Sender: TObject);
    procedure rbDefColorsClick(Sender: TObject);
  private
    FOnApply: TNotifyEvent;
    procedure ApplySettings;
    procedure UpdateFontName;
    { Private declarations }
  public
    { Public declarations }
    PrFont: TFont;
    PrColor: TColor;
    PrWidth: integer;
    property OnApply: TNotifyEvent read FOnApply write FOnApply;
  end;

var
  RulerPropertiesFrm: TRulerPropertiesFrm;

implementation

{$R *.dfm}

procedure TRulerPropertiesFrm.ApplySettings;
var
  dummy: integer;
begin
  if rbDefColors.Checked then
    PrColor := clDefault
  else
  begin
    PrColor := cbBgColor.Selected;
    PrFont.Color := cbFgColor.Selected;
  end;
  if TryStrToInt(eWidth.Text, dummy) then
    PrWidth := dummy;
  if Assigned(FOnApply) then
    FOnApply(Self);
  btnApply.Enabled := false;
end;

procedure TRulerPropertiesFrm.btnApplyClick(Sender: TObject);
begin
  ApplySettings;
end;

procedure TRulerPropertiesFrm.btnFontChangeClick(Sender: TObject);
begin
  with TFontDialog.Create(nil) do
    try
      Font.Assign(PrFont);
      Options := [];
      if Execute then
      begin
        PrFont.Assign(Font);
        UpdateFontName;
        btnApply.Enabled := true;
      end;
    finally
      Free;
    end;
end;

procedure TRulerPropertiesFrm.btnOkClick(Sender: TObject);
begin
  ApplySettings;
end;

procedure TRulerPropertiesFrm.cbBgColorChange(Sender: TObject);
begin
  btnApply.Enabled := true;
end;

procedure TRulerPropertiesFrm.FormCreate(Sender: TObject);
begin
  PrFont := TFont.Create;
end;

procedure TRulerPropertiesFrm.FormDestroy(Sender: TObject);
begin
  PrFont.Free;
end;

const
  STYLE_WITH_DEFAULT = [cbStandardColors, cbExtendedColors, cbSystemColors, cbIncludeDefault, cbCustomColor, cbPrettyNames];
  STYLE_WITHOUT_DEFAULT = [cbStandardColors, cbExtendedColors, cbSystemColors, cbCustomColor, cbPrettyNames];

procedure TRulerPropertiesFrm.UpdateColorsEnabled(Sender: TObject);
begin
  lblBg.Enabled := rbCustColors.Checked;
  cbBgColor.Enabled := rbCustColors.Checked;
  lblFg.Enabled := rbCustColors.Checked;
  cbFgColor.Enabled := rbCustColors.Checked;
end;

procedure TRulerPropertiesFrm.UpdateFontName;
begin
  stFontName.Caption := Format('%s, %d pt', [PrFont.Name, PrFont.Size]);
end;

procedure TRulerPropertiesFrm.FormShow(Sender: TObject);
begin
  UpdateFontName;

  if PrColor = clDefault then
    rbDefColors.Checked := true
  else
    rbCustColors.Checked := true;

  if rbCustColors.Checked then
  begin
    cbBgColor.Selected := PrColor;
    cbFgColor.Selected := PrFont.Color;
  end;

  eWidth.Text := IntToStr(PrWidth);

  btnApply.Enabled := false;
end;

procedure TRulerPropertiesFrm.rbDefColorsClick(Sender: TObject);
begin
  UpdateColorsEnabled(Self);
  btnApply.Enabled := true;
end;

{ TStaticText }

procedure TStaticText.CreateParams(var Params: TCreateParams);
begin
  inherited;
  Params.Style := Params.Style or SS_CENTERIMAGE;
end;

end.
