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

  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 KsSkinCheckBoxs;

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

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Dialogs,
  StdCtrls, Forms, KsUtils, KsBitmap, KsControls, KsCheckBoxs,
  KsSkinVersion, KsSkinObjects, KsSkinSource, KsSkinEngine;

type

{ TSeSkinCheckBox class }

{ A TSeSkinCheckBox component presents an option for the user. The user can check the box to select the option, or uncheck it to deselect the option. }
  TSeSkinCheckBox = class(TKsCustomCheckBox)
  private
    FSkinEngine: TSeSkinEngine;
    FSkinObject: string;
    FSkinCheckBox: TSeSkinObject;
    function GetVersion: TSeSkinVersion;
    procedure SetVersion(const Value: TSeSkinVersion);
    procedure SetSkinEngine(const Value: TSeSkinEngine);
    procedure SetSkinObject(const Value: string);
  protected
    procedure WMInvalidateSkinObject(var Msg: TMessage); message WM_INVALIDATESKINOBJECT;
    procedure WMBeforeChange(var Msg: TMessage); message WM_BEFORECHANGE;
    procedure WMSkinChange(var Msg: TMessage); message WM_SKINCHANGE;

    function UseSkin: boolean;
    { overrides }
    function GetBoxSize: TPoint; override;
    procedure PaintBox; override;
    { VCL protected  }
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    constructor Create(AOwner: TComponent); override;
    procedure Loaded; override;
    destructor Destroy; override;
  published
    property Caption;
    property Checked;
    property Spacing;
    property State;
    { Link to TSeSkinEngine component. Specifies the appearance and behavior by using specified skins from TSeSkinEngine components
      See Also
        TSeSkinEngine
    }
    property SkinEngine: TSeSkinEngine read FSkinEngine write SetSkinEngine;
    { Specifies the SkinObject name, your can change default value to set custom skin object for the control
      For Example:
        For TSeSkinButton the SkinObject property set to 'Button', but your can use you own name - example 'My Button' 
    }
    property SkinObject: string read FSkinObject write SetSkinObject;
    property Version: TSeSkinVersion read GetVersion write SetVersion
      stored false;
  end;

{ TSeSkinRadioButton }

{ Use TSeSkinRadioButton to add a radio button to a form. Radio buttons present a set of mutually exclusive options to the user-that is, only one radio button in a set can be selected at a time.  }
  TSeSkinRadioButton = class(TKsCustomRadioButton)
  private
    FSkinEngine: TSeSkinEngine;
    FSkinObject: string;
    FSkinRadioButton: TSeSkinObject;
    function GetVersion: TSeSkinVersion;
    procedure SetVersion(const Value: TSeSkinVersion);
    procedure SetSkinEngine(const Value: TSeSkinEngine);
    procedure SetSkinObject(const Value: string);
  protected
    procedure WMInvalidateSkinObject(var Msg: TMessage); message WM_INVALIDATESKINOBJECT;
    procedure WMBeforeChange(var Msg: TMessage); message WM_BEFORECHANGE;
    procedure WMSkinChange(var Msg: TMessage); message WM_SKINCHANGE;

    function UseSkin: boolean;
    { overrides }
    function GetBoxSize: TPoint; override;
    procedure PaintBox; override;
    { VCL protected  }
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    constructor Create(AOwner: TComponent); override;
    procedure Loaded; override;
    destructor Destroy; override;
  published
    property Caption;
    property Checked;
    property GroupIndex;
    property Spacing;
    { Link to TSeSkinEngine component. Specifies the appearance and behavior by using specified skins from TSeSkinEngine components
      See Also
        TSeSkinEngine
    }
    property SkinEngine: TSeSkinEngine read FSkinEngine write SetSkinEngine;
    { Specifies the SkinObject name, your can change default value to set custom skin object for the control
      For Example:
        For TSeSkinButton the SkinObject property set to 'Button', but your can use you own name - example 'My Button' 
    }
    property SkinObject: string read FSkinObject write SetSkinObject;
    property Version: TSeSkinVersion read GetVersion write SetVersion
      stored false;
  end;

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

{ TSeSkinCheckBox }

constructor TSeSkinCheckBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FSkinObject := 'CheckBox';
end;

destructor TSeSkinCheckBox.Destroy;
begin
  if FSkinCheckBox <> nil then FSkinCheckBox.Free;
  inherited Destroy;
end;

procedure TSeSkinCheckBox.Loaded;
begin
  inherited;
{  SkinEngine := FSkinEngine; }
end;

function TSeSkinCheckBox.UseSkin: boolean;
begin
  if (csDestroying in ComponentState) or (csLoading in ComponentState) then
    Result := false
  else
    if (FSkinEngine <> nil) and (FSkinEngine.SkinSource <> nil) and
       (not FSkinEngine.SkinSource.IsChanging) and
       (FSkinEngine.SkinSource.Count > 0) and
       (FSkinEngine.SkinSource.GetObjectByName(FSkinObject) <> nil) and
       (FSkinCheckBox <> nil)
    then
      Result := true
    else
      Result := false;
end;

function TSeSkinCheckBox.GetBoxSize: TPoint;
begin
  if UseSkin and (FSkinCheckBox.FindObjectByName('Checked') <> nil) then
    Result := Point(FSkinCheckBox.FindObjectByName('Checked').Width,
      FSkinCheckBox.FindObjectByName('Checked').Height)
  else
    Result := inherited GetBoxSize;
end;

procedure TSeSkinCheckBox.PaintBox;
var
  R: TRect;
  DrawState: TSeState;
  SkinObject: TSeSkinObject;
begin
  if UseSkin then
  begin
    R := Rect(0, 0, GetBoxSize.X, GetBoxSize.Y);
    if Alignment = taLeftJustify then
      RectVCenter(R, Rect(0, 0, FWidth, FHeight))
    else
      RectVCenter(R, Rect(FWidth-GetBoxSize.X, 0, FWidth, FHeight));

    if not Enabled then
      DrawState := ssDisabled
    else
      if MouseInControl then
        DrawState := ssHot
      else
        if Focused then
          DrawState := ssFocused
        else
          DrawState := ssNormal;

    case State of
      cbChecked: SkinObject := FSkinCheckBox.FindObjectByName('Checked');
      cbGrayed: SkinObject := FSkinCheckBox.FindObjectByName('Mixed');
    else
      SkinObject := FSkinCheckBox.FindObjectByName('Unchecked');
    end;

    if SkinObject <> nil then
    begin
      SkinObject.State := DrawState;
      SkinObject.BoundsRect := R;
      SkinObject.Draw(Buffer);
    end;

    if FSkinCheckBox.FindObjectByName('Text') <> nil then
    begin
      SkinObject := FSkinCheckBox.FindObjectByName('Text');
      SkinObject.State := DrawState;
      ControlFont.Assign(SkinObject.Font);
    end
    else
      ControlFont.Assign(SkinObject.Font);
  end
  else
    inherited;
end;

procedure TSeSkinCheckBox.WMInvalidateSkinObject(var Msg: TMessage);
begin
  Invalidate;
end;

procedure TSeSkinCheckBox.WMBeforeChange(var Msg: TMessage);
begin
  if Pointer(Msg.LParam) = nil then Exit;
  if TSeSkinEngine(Msg.LParam) <> FSkinEngine then Exit;

  if FSkinCheckBox <> nil then FSkinCheckBox.Free;
  FSkinCheckBox := nil;
end;

procedure TSeSkinCheckBox.WMSkinChange(var Msg: TMessage);
begin
  if Pointer(Msg.LParam) = nil then Exit;
  if TSeSkinEngine(Msg.LParam) <> FSkinEngine then Exit;

  SkinEngine := FSkinEngine;
end;

{ VCL }

procedure TSeSkinCheckBox.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited;
  if (Operation = opRemove) and (AComponent = FSkinEngine) then
    SkinEngine := nil;
end;

{ Properties }

procedure TSeSkinCheckBox.SetSkinEngine(const Value: TSeSkinEngine);
begin
  FSkinEngine := Value;

  if (FSkinEngine <> nil) and (FSkinEngine.SkinSource <> nil) and
     (not FSkinEngine.SkinSource.IsChanging) and
     (FSkinEngine.SkinSource.Count > 0) then
  begin
    if FSkinCheckBox <> nil then FSkinCheckBox.Free;
    FSkinCheckBox := nil;

    if FSkinEngine.SkinSource.GetObjectByName(FSkinObject) <> nil then
    begin
      FSkinCheckBox := FSkinEngine.SkinSource.GetObjectByName(FSkinObject).CreateCopy(nil);

      if FSkinCheckBox.FindObjectByName('Text') <> nil then
        Font.Assign(FSkinCheckBox.FindObjectByName('Text').Font)
      else
        Font.Assign(FSkinCheckBox.Font);
    end;

    if FSkinCheckBox <> nil then
      FSkinCheckBox.ParentControl := Self;
  end
  else
  begin
    if FSkinCheckBox <> nil then FSkinCheckBox.Free;
    FSkinCheckBox := nil;
  end;

  Invalidate;
end;

function TSeSkinCheckBox.GetVersion: TSeSkinVersion;
begin
  Result := sSeSkinVersion;
end;

procedure TSeSkinCheckBox.SetVersion(const Value: TSeSkinVersion);
begin
end;

{ TSeSkinRadioButton =========================================================}

constructor TSeSkinRadioButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FSkinObject := 'RadioButton';
end;

destructor TSeSkinRadioButton.Destroy;
begin
  if FSkinRadioButton <> nil then FSkinRadioButton.Free;
  inherited Destroy;
end;

procedure TSeSkinRadioButton.Loaded;
begin
  inherited;
  SkinEngine := FSkinEngine;
end;

function TSeSkinRadioButton.UseSkin: boolean;
begin
  if (csDestroying in ComponentState) or (csLoading in ComponentState) then
    Result := false
  else
    if (FSkinEngine <> nil) and (FSkinEngine.SkinSource <> nil) and
       (not FSkinEngine.SkinSource.IsChanging) and
       (FSkinEngine.SkinSource.Count > 0) and
       (FSkinEngine.SkinSource.GetObjectByName(FSkinObject) <> nil) and
       (FSkinRadioButton <> nil)
    then
      Result := true
    else
      Result := false;
end;

function TSeSkinRadioButton.GetBoxSize: TPoint;
begin
  if UseSkin and (FSkinRadioButton.FindObjectByName('Checked') <> nil) then
    Result := Point(FSkinRadioButton.FindObjectByName('Checked').Width,
      FSkinRadioButton.FindObjectByName('Checked').Height)
  else
    Result := inherited GetBoxSize;
end;

procedure TSeSkinRadioButton.PaintBox;
var
  R: TRect;
  DrawState: TSeState;
  SkinObject: TSeSkinObject;
begin
  if UseSkin then
  begin
    R := Rect(0, 0, GetBoxSize.X, GetBoxSize.Y);
    RectVCenter(R, Rect(0, 0, FWidth, FHeight));

    if not Enabled then
      DrawState := ssDisabled
    else
      if MouseInControl then
        DrawState := ssHot
      else
        if Focused then
          DrawState := ssFocused
        else
          DrawState := ssNormal;

    if Checked then
      SkinObject := FSkinRadioButton.FindObjectByName('Checked')
    else
      SkinObject := FSkinRadioButton.FindObjectByName('Unchecked');

    if SkinObject <> nil then
    begin
      SkinObject.State := DrawState;
      SkinObject.BoundsRect := R;
      SkinObject.Draw(Buffer);
    end;

    { Set Font }
    if FSkinRadioButton.FindObjectByName('Text') <> nil then
    begin
      SkinObject := FSkinRadioButton.FindObjectByName('Text');
      SkinObject.State := DrawState;
      ControlFont := SkinObject.Font;
    end
    else
      ControlFont := FSkinRadioButton.Font;
  end
  else
    inherited;
end;

procedure TSeSkinRadioButton.WMInvalidateSkinObject(var Msg: TMessage);
begin
  Invalidate;
end;

procedure TSeSkinRadioButton.WMBeforeChange(var Msg: TMessage);
begin
  if Pointer(Msg.LParam) = nil then Exit;
  if TSeSkinEngine(Msg.LParam) <> FSkinEngine then Exit;

  if FSkinRadioButton <> nil then FSkinRadioButton.Free;
  FSkinRadioButton := nil;
end;

procedure TSeSkinRadioButton.WMSkinChange(var Msg: TMessage);
begin
  if Pointer(Msg.LParam) = nil then Exit;
  if TSeSkinEngine(Msg.LParam) <> FSkinEngine then Exit;

  SkinEngine := FSkinEngine;
end;

procedure TSeSkinRadioButton.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited;
  if (Operation = opRemove) and (AComponent = FSkinEngine) then
    SkinEngine := nil;
end;

{ Properties }

function TSeSkinRadioButton.GetVersion: TSeSkinVersion;
begin
  Result := sSeSkinVersion;
end;

procedure TSeSkinRadioButton.SetVersion(const Value: TSeSkinVersion);
begin
end;

procedure TSeSkinRadioButton.SetSkinEngine(const Value: TSeSkinEngine);
begin
  FSkinEngine := Value;

  if (FSkinEngine <> nil) and (FSkinEngine.SkinSource <> nil) and
     (not FSkinEngine.SkinSource.IsChanging) and
     (FSkinEngine.SkinSource.Count > 0) then
  begin
    if FSkinRadioButton <> nil then FSkinRadioButton.Free;
    FSkinRadioButton := nil;

    if FSkinEngine.SkinSource.GetObjectByName(FSkinObject) <> nil then
      FSkinRadioButton := FSkinEngine.SkinSource.GetObjectByName(FSkinObject).CreateCopy(nil);

    if FSkinRadioButton <> nil then
      FSkinRadioButton.ParentControl := Self;
  end
  else
  begin
    if FSkinRadioButton <> nil then FSkinRadioButton.Free;
    FSkinRadioButton := nil;
  end;

  Invalidate;
end;

procedure TSeSkinCheckBox.SetSkinObject(const Value: string);
begin
  FSkinObject := Value;
end;

procedure TSeSkinRadioButton.SetSkinObject(const Value: string);
begin
  FSkinObject := Value;
end;

end.
