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

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

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

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Buttons,
  KsUtils, KsBitmap, KsControls, KsButtons, KsSkinVersion, KsSkinObjects,
  KsSkinSource, KsSkinEngine;

type

{ TSeSkinButton class }

{ Use TSeSkinButton to put a advanced button on a form. TSeSkinButton introduces KsSkinVeral properties to control its behavior in a dialog box setting. Users choose button controls to initiate actions. }
  TSeSkinButton = class(TKsCustomButton)
  private
    FSkinEngine: TSeSkinEngine;
    FSkinObject: string;
    FSkinButton: TSeSkinObject;
    FShowGlyph: boolean;
    FShowCaption: boolean;
    function GetVersion: TSeSkinVersion;
    procedure SetVersion(const Value: TSeSkinVersion);
    procedure SetSkinEngine(const Value: TSeSkinEngine);
    procedure SetSkinObject(const Value: string);
    procedure SetShowGlyph(const Value: boolean);
    procedure SetShowCaption(const Value: boolean);
  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 }
    procedure PaintFace; override;
    procedure PaintGlyph; override;
    procedure PaintText; override;
    { VCL protected  }
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    constructor Create(AOwner: TComponent); override;
    procedure Loaded; override;
    destructor Destroy; override;
  published
    property Align;
    property Blend;
    property BlendValue;
    property Caption;
    property Default;
    property Enabled;
    property Glyph;
    property Kind;
    { Determines whether to show caption }
    property ShowCaption: boolean read FShowCaption write SetShowCaption;
    { Determines whether to show glyph }
    property ShowGlyph: boolean read FShowGlyph write SetShowGlyph;
    { 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 Transparent;
    property Version: TSeSkinVersion read GetVersion write SetVersion
      stored false;
    property OnClick;
  end;

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

{ TSeSkinButton }

constructor TSeSkinButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FSkinObject := 'Button';
  FShowGlyph := true;
  FShowCaption := true;
end;

destructor TSeSkinButton.Destroy;
begin
  if FSkinButton <> nil then FSkinButton.Free;
  inherited Destroy;
end;

procedure TSeSkinButton.Loaded;
begin
  inherited;
  Invalidate;
end;

function TSeSkinButton.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
       (FSkinButton <> nil)
    then
      Result := true
    else
      Result := false;
end;

procedure TSeSkinButton.PaintFace;
var
  DrawState: TSeState;
  FFace: TSeSkinObject;
begin
  if UseSkin then
  begin
    if not Enabled then
      DrawState := ssDisabled
    else
      if State = kbsPressed then
        DrawState := ssPressed
      else
        if MouseInControl then
          DrawState := ssHot
        else
          if Focused or ActiveDefault then
            DrawState := ssFocused
          else
            DrawState := ssNormal;

    if FSkinButton.FindObjectByName('Face') <> nil then
      FFace := FSkinButton.FindObjectByName('Face')
    else
      FFace := FSkinButton;

    FFace.State := DrawState;
    FFace.BoundsRect := Rect(0, 0, FWidth, FHeight);
    FFace.Draw(Buffer);
  end
  else
    inherited ;
end;

procedure TSeSkinButton.PaintGlyph;
begin
  if not UseSkin or FShowGlyph then
    inherited PaintGlyph;
end;

procedure TSeSkinButton.PaintText;
var
  R: TRect;
  DrawState: TSeState;
  FText: TSeSkinObject;
begin
  if UseSkin and ShowCaption then
    with Buffer do
    begin
      if not Enabled then
        DrawState := ssDisabled
      else
        if State = kbsPressed then
          DrawState := ssPressed
        else
          if MouseInControl then
            DrawState := ssHot
          else
            if Focused or ActiveDefault then
              DrawState := ssFocused
            else
              DrawState := ssNormal;

      if FSkinButton.FindObjectByName('Text') <> nil then
      begin
        FText := FSkinButton.FindObjectByName('Text');
        FText.State := DrawState;
        Font := FText.Font;
      end
      else
        Font := FSkinButton.Font;

      R := Rect(0, 0, TextWidth(Caption), TextHeight(Caption));
      OffsetRect(R, (RectWidth(GetCaptionRect) - R.Right) div 2, (RectHeight(GetCaptionRect) - R.Bottom) div 2);
      OffsetRect(R, GetCaptionRect.Left, GetCaptionRect.Top);

      { Draw Text }
      DrawText(Caption, R, DrawTextBiDiModeFlags(DT_CENTER));
    end
  else
    inherited PaintText;
end;

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

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

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

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

  SkinEngine := FSkinEngine;
end;

{ VCL Protected }

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

{ Properties }

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

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

procedure TSeSkinButton.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 FSkinButton <> nil then FSkinButton.Free;
    FSkinButton := nil;

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

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

  Invalidate;
end;

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

procedure TSeSkinButton.SetShowGlyph(const Value: boolean);
begin
  FShowGlyph := Value;
end;

procedure TSeSkinButton.SetShowCaption(const Value: boolean);
begin
  FShowCaption := Value;
end;

end.
