{==============================================================================

  Blend Controls
  Copyright (C) 2000-2001 by Evgeny Kryukov
  All rights reserved

  All contents of this file and all other files included in this archive
  are Copyright (C) 2001 Evgeny Kryukov. Use and/or distribution of
  them requires acceptance of the License Agreement.

  See License.txt for licence information

===============================================================================}

unit EpIICheckBoxs;

{$I EpIIVer.Inc}
{$T-,W-,X+,P+}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Dialogs,
  StdCtrls, Forms, KsUtils, KsBitmap, KsControls, KsCheckBoxs,
  EpIIStyles, EpIIVersion;

type

{ TEpCheckBox class }

  TEpCheckBox = class(TKsCustomCheckBox)
  private
    FShowGlyph: boolean;
    FBoxBackground: TEpBackground;
    FBoxBorder: TepBorder;
    FGlyphColor: TColor;
    FBoxWidth: integer;
    procedure DoChange(Sender: TObject);

    function GetVersion: TEpVersion;
    procedure SetVersion(const Value: TEpVersion);
    procedure SetBoxBackground(const Value: TEpBackground);
    procedure SetBoxBorder(const Value: TepBorder);
    procedure SetShowGlyph(const Value: boolean);
    procedure SetGlyphColor(const Value: TColor);
    procedure SetBoxWidth(const Value: integer);
  protected
    procedure PaintBox; override;
    function GetBoxSize: TPoint; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Blend;
    property BlendValue;
    { It's possible to draw the box background for control. Based on TEpBackground class.
      See Also:
        TEpBackground
    }
    property BoxBackground: TEpBackground read FBoxBackground write SetBoxBackground;
    { It's possible to draw the box border for control. Based on TEpBorder class.
      See Also:
        TEpBorder
    }
    property BoxBorder: TepBorder read FBoxBorder write SetBoxBorder;
    { Specifies the box width and height }
    property BoxWidth: integer read FBoxWidth write SetBoxWidth;
    property Caption;
    property Checked;
    { Specifies the box glyph color}
    property GlyphColor: TColor read FGlyphColor write SetGlyphColor;
    property Font;
    property ShowGlyph: boolean read FShowGlyph write SetShowGlyph;
    property Spacing;
    property State;
    property Transparent;
    property Version: TEpVersion read GetVersion write SetVersion
      stored false;
  end;

{ TEpRadioButton }

  TEpRadioButton = class(TKsCustomRadioButton)
  private
    FShowGlyph: boolean;
    FBoxWidth: integer;
    FGlyphColor: TColor;
    FBoxBackground: TEpBackground;
    FBoxBorder: TepBorder;
    procedure DoChange(Sender: TObject);

    function GetVersion: TEpVersion;
    procedure SetVersion(const Value: TEpVersion);
    procedure SetBoxBackground(const Value: TEpBackground);
    procedure SetBoxBorder(const Value: TepBorder);
    procedure SetBoxWidth(const Value: integer);
    procedure SetGlyphColor(const Value: TColor);
    procedure SetShowGlyph(const Value: boolean);
  protected
    procedure PaintBox; override;
    function GetBoxSize: TPoint; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Blend;
    property BlendValue;
    { It's possible to draw the box background for control. Based on TEpBackground class.
      See Also:
        TEpBackground
    }
    property BoxBackground: TEpBackground read FBoxBackground write SetBoxBackground;
    { It's possible to draw the box border for control. Based on TEpBorder class.
      See Also:
        TEpBorder
    }
    property BoxBorder: TepBorder read FBoxBorder write SetBoxBorder;
    { Specifies the box width and height }
    property BoxWidth: integer read FBoxWidth write SetBoxWidth;
    property Caption;
    property Checked;
    property GroupIndex;
    { Specifies the box glyph color}
    property GlyphColor: TColor read FGlyphColor write SetGlyphColor;
    property Font;
    property ShowGlyph: boolean read FShowGlyph write SetShowGlyph;
    property Spacing;
    property Version: TEpVersion read GetVersion write SetVersion
      stored false;
  end;

implementation {===============================================================}

{ TEpCheckBox }

constructor TEpCheckBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FShowGlyph := true;
  FBoxWidth := 15;

  FBoxBackground := TEpBackground.Create;
  FBoxBackground.OnChange := DoChange;
  FBoxBorder := TEpBorder.Create;
  FBoxBorder.OnChange := DoChange;
end;

destructor TEpCheckBox.Destroy;
begin
  FBoxBackground.Free;
  FBoxBorder.Free;
  inherited Destroy;
end;

procedure TEpCheckBox.PaintBox;
var
  CheckColor: TKColor;
  R: TRect;
  B: TKsBitmap;
  i, j: integer;
begin
  if FBoxBorder = nil then Exit;

  { Draw box }
  R := Rect(0, 0, GetBoxSize.X, GetBoxSize.Y);
  RectVCenter(R, Rect(0, 0, FWidth, FHeight));

  FBoxBackground.Draw(Buffer, R);
  FBoxBorder.Draw(Buffer, R, 0);

  { Draw Glyph }
  if FShowGlyph and (State <> cbUnchecked) then
  begin
    if not Enabled then
      CheckColor := ckBtnFace
    else
      CheckColor := KColor(FGlyphColor);

    InflateRect(R, -FBoxBorder.Width - 2, -FBoxBorder.Width - 2);
    { Draw Glyph }

    { Draw \ }
    Buffer.MoveTo(R.Left, R.Top);
    Buffer.LineTo(R.Right, R.Bottom, CheckColor);
    Buffer.MoveTo(R.Left+1, R.Top);
    Buffer.LineTo(R.Right, R.Bottom-1, CheckColor);
    Buffer.MoveTo(R.Left, R.Top+1);
    Buffer.LineTo(R.Right-1, R.Bottom, CheckColor);

    { Draw / }
    Buffer.MoveTo(R.Left, R.Bottom-1);
    Buffer.LineTo(R.Right, R.Top-1, CheckColor);
    Buffer.MoveTo(R.Left, R.Bottom-2);
    Buffer.LineTo(R.Right-1, R.Top-1, CheckColor);
    Buffer.MoveTo(R.Left+1, R.Bottom-1);
    Buffer.LineTo(R.Right, R.Top, CheckColor);
  end;
end;

procedure TEpCheckBox.DoChange(Sender: TObject);
begin
  Invalidate;
end;

function TEpCheckBox.GetBoxSize: TPoint;
begin
  Result := Point(FBoxWidth, FBoxWidth); 
end;

{ Properties }

function TEpCheckBox.GetVersion: TEpVersion;
begin
  Result := EpVersion;
end;

procedure TEpCheckBox.SetVersion(const Value: TEpVersion);
begin
end;

procedure TEpCheckBox.SetBoxBackground(const Value: TEpBackground);
begin
  FBoxBackground.Assign(Value);
end;

procedure TEpCheckBox.SetBoxBorder(const Value: TepBorder);
begin
  FBoxBorder.Assign(Value);
end;

procedure TEpCheckBox.SetBoxWidth(const Value: integer);
begin
  if FBoxWidth <> Value then
  begin
    FBoxWidth := Value;
    Invalidate;
  end;
end;

procedure TEpCheckBox.SetShowGlyph(const Value: boolean);
begin
  if FShowGlyph <> Value then
  begin
    FShowGlyph := Value;
    Invalidate;
  end;
end;

procedure TEpCheckBox.SetGlyphColor(const Value: TColor);
begin
  if FGlyphColor <> Value then
  begin
    FGlyphColor := Value;
    Invalidate;
  end;
end;

{ TEpRadioButton =========================================================}

constructor TEpRadioButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FShowGlyph := true;
  FBoxWidth := 15;

  FBoxBackground := TEpBackground.Create;
  FBoxBackground.OnChange := DoChange;
  FBoxBorder := TEpBorder.Create;
  FBoxBorder.OnChange := DoChange;
end;

destructor TEpRadioButton.Destroy;
begin
  FBoxBackground.Free;
  FBoxBorder.Free;
  inherited Destroy;
end;

procedure TEpRadioButton.PaintBox;
var
  CheckColor: TKColor;
  R: TRect;
  B: TKsBitmap;
  i, j: integer;
begin
  if FBoxBorder = nil then Exit;

  { Draw box }
  R := Rect(0, 0, GetBoxSize.X, GetBoxSize.Y);
  RectVCenter(R, Rect(0, 0, FWidth, FHeight));

  FBoxBackground.Draw(Buffer, R);
  FBoxBorder.Draw(Buffer, R, 0);

  { Draw Glyph }
  if FShowGlyph and (Checked) then
  begin
    if not Enabled then
      CheckColor := ckBtnFace
    else
      CheckColor := KColor(FGlyphColor);

    InflateRect(R, -FBoxBorder.Width - 2, -FBoxBorder.Width - 2);

    { Draw Glyph }
    Buffer.FillRoundRect(R, 3, CheckColor);
  end;
end;

function TEpRadioButton.GetBoxSize: TPoint;
begin
  Result := Point(FBoxWidth, FBoxWidth);
end;

procedure TEpRadioButton.DoChange(Sender: TObject);
begin
  Invalidate;
end;

{ Properties }

function TEpRadioButton.GetVersion: TEpVersion;
begin
  Result := EpVersion;
end;

procedure TEpRadioButton.SetVersion(const Value: TEpVersion);
begin
end;

procedure TEpRadioButton.SetBoxBackground(const Value: TEpBackground);
begin
  FBoxBackground.Assign(Value);
end;

procedure TEpRadioButton.SetBoxBorder(const Value: TepBorder);
begin
  FBoxBorder.Assign(Value);
end;

procedure TEpRadioButton.SetBoxWidth(const Value: integer);
begin
  if FBoxWidth <> Value then
  begin
    FBoxWidth := Value;
    Invalidate;
  end;
end;

procedure TEpRadioButton.SetGlyphColor(const Value: TColor);
begin
  if FGlyphColor <> Value then
  begin
    FGlyphColor := Value;
    Invalidate;
  end;
end;

procedure TEpRadioButton.SetShowGlyph(const Value: boolean);
begin
  if FShowGlyph <> Value then
  begin
    FShowGlyph := Value;
    Invalidate;
  end;
end;

end.
