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

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

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

interface

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

type

{ Use TSeSkinSpeedButton to put a advanced speed button on a form. }
  TSeSkinSpeedButton = class(TKsCustomSpeedButton)
  private
    FSkinEngine: TSeSkinEngine;
    FSkinButton: TSeSkinObject;
    FSkinObject: string;
    function GetVersion: TSeSkinVersion;
    procedure SetSkinEngine(const Value: TSeSkinEngine);
    procedure SetVersion(const Value: TSeSkinVersion);
    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;

    procedure PaintBuffer; override;
    { VCL protected  }
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Loaded; override;
  published
    { Specifies the appearance and behavior by using specified theme's from TSeSkinEngine .
      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 {===============================================================}

uses KsSkinSpinButtons;

{ TSeSkinSpeedButton }

constructor TSeSkinSpeedButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FSkinObject := 'SpeedButton';
end;

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

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

function TSeSkinSpeedButton.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 TSeSkinSpeedButton.PaintBuffer;
var
  DrawState: TSeState;
  ButtonOffset, ChevronOffset: TPoint;
  SkinObject: TSeSkinObject;
begin
  if Buffer = nil then Exit;

  if not UseSkin then
  begin
    inherited ;
    Exit;
  end;
  SkinObject := nil;

  DrawState := ssNormal;

  if Flat then
  begin
    { Flat }
      
    if State = KsSpeedButtons.bsDown then
      DrawState := ssPressed
    else
      if MouseInControl then
        DrawState := ssHot;

    if (State = bsExclusive) and not MouseInControl then
      DrawState := ssFocused;

    if FSkinButton.FindObjectByName('FlatFace') <> nil then
      SkinObject := FSkinButton.FindObjectByName('FlatFace')
  end
  else
  begin
    { Normal }

    if State = KsSpeedButtons.bsDown then
      DrawState := ssPressed;

    if State = bsExclusive then
      DrawState := ssFocused;

    if FSkinButton.FindObjectByName('Face') <> nil then
      SkinObject := FSkinButton.FindObjectByName('Face')
  end;

  if not Enabled then
    DrawState := ssDisabled;

  if SkinObject = nil then
    SkinObject := FSkinButton;

  if Parent is TSeSkinSpinButton then
  begin
    { Spin button }
    if FSkinButton.FindObjectByName('Face') <> nil then
      SkinObject := FSkinButton.FindObjectByName('Face')
    else
      SkinObject := FSkinButton;

    SkinObject.State := DrawState;
    SkinObject.BoundsRect := GetButtonRect;
    SkinObject.Draw(Buffer);
  end
  else
  begin
    { Standard }
    SkinObject.State := DrawState;
    SkinObject.BoundsRect := GetButtonRect;
    SkinObject.Draw(Buffer);

    if ShowChevron then
    begin
      if Flat then
      begin
        if FSkinButton.FindObjectByName('FlatChevron') <> nil then
          SkinObject := FSkinButton.FindObjectByName('FlatChevron');
      end
      else
      begin
        if FSkinButton.FindObjectByName('Chevron') <> nil then
          SkinObject := FSkinButton.FindObjectByName('Chevron');
      end;

      SkinObject.State := DrawState;
      SkinObject.BoundsRect := GetChevronRect;
      SkinObject.Draw(Buffer);
    end;
  end;

  { Draw Glyph }
  ButtonOffset := Point(0, 0);
  ChevronOffset := Point(0, 0);

  if FState in [KsSpeedButtons.bsDown, bsExclusive] then
    ButtonOffset := Point(1, 1);

  if (FState in [bsChevronDown, bsExclusive]) or Down then
    ChevronOffset := Point(1, 1);

  if ShowGlyph then
    DrawGlyph(ButtonOffset);

  { Draw Text }

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

  if ShowCaption then
    DrawCaption(ButtonOffset);
end;

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

procedure TSeSkinSpeedButton.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 TSeSkinSpeedButton.WMSkinChange(var Msg: TMessage);
begin
  if Pointer(Msg.LParam) = nil then Exit;
  if TSeSkinEngine(Msg.LParam) <> FSkinEngine then Exit;

  SkinEngine := FSkinEngine;
end;

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

{ Properties }

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

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

procedure TSeSkinSpeedButton.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);
  end
  else
  begin
    if FSkinButton <> nil then FSkinButton.Free;
    FSkinButton := nil;
  end;

  Invalidate;
end;

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

end.

