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

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

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

interface

uses
  Windows, Messages, Classes, Graphics, Forms, KsBitmap, KsUtils, KsControls,
  KsProgress, KsSkinVersion, KsSkinObjects, KsSkinSource, KsSkinEngine;

type

{ TSeSkinProgressBar class }

{ TSeSkinProgressBar is a advanced Windows progress bar. }
  TSeSkinProgressBar = class(TKsCustomProgressBar)
  private
    FSkinEngine: TSeSkinEngine;
    FSkinObject: string;
    FSkinProgress: 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 GetBarRect: TRect; override;
    procedure PaintBar; override;
    procedure PaintFrame; override;
    { VCL protected  }
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Loaded; override;
  published
    property Blend;
    property BlendValue;
    property Max;
    property Min;
    property Position;
    property Orientation;
    property Smooth;
    { 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 {===============================================================}

{ TSeSkinProgressBar }

constructor TSeSkinProgressBar.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FSkinObject := 'ProgressBar';
  BorderWidth := 0;
end;

destructor TSeSkinProgressBar.Destroy;
begin
  if FSkinProgress <> nil then FSkinProgress.Free;
  inherited Destroy;
end;

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

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

function TSeSkinProgressBar.GetBarRect: TRect;
begin
  if UseSkin and (FSkinProgress.FindObjectByName('Frame') <> nil) then
  begin
    Result := Rect(0, 0, FWidth, FHeight);

    with FSkinProgress.FindObjectByName('Frame') do
      Result := Rect(MarginLeft, MarginTop, FWidth - MarginRight, FHeight - MarginBottom)
  end
  else
    Result := inherited GetBarRect;
end;

{ Drawing }

procedure TSeSkinProgressBar.PaintBar;
var
  FillR, R: TRect;
  W, Pos: integer;
  SkinObject: TSeSkinObject;
begin
  if UseSkin then
    with Buffer do
    begin
      R := GetBarRect;

      if Orientation = kboHorizontal then
        W := RectWidth(R)
      else
        W := RectHeight(R);
      { Calc Pos }
      Pos := Round(W * GetPercent(Position));
      { Set FillR }
      FillR := R;

      if Orientation = kboHorizontal then
        FillR.Right := FillR.Left + Pos
      else
        FillR.Top := FillR.Bottom - Pos;

      if Orientation = kboHorizontal then
        SkinObject := FSkinProgress.FindObjectByName('BarHorz')
      else
        SkinObject := FSkinProgress.FindObjectByName('BarVert');

      if SkinObject = nil then
        SkinObject := FSkinProgress.FindObjectByName('Bar');

      { Draw Bar }
      if SkinObject <> nil then
      begin
        SkinObject.BoundsRect := FillR;
        SkinObject.Draw(Buffer);
      end;
    end
  else
    inherited;
end;

procedure TSeSkinProgressBar.PaintFrame;
var
  R: TRect;
  SkinObject: TSeSkinObject;
begin
  if UseSkin then
  begin
    R := Rect(0, 0, FWidth, FHeight);

    { Draw Frame }
    SkinObject := FSkinProgress.FindObjectByName('Frame');

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

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

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

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

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

  SkinEngine := FSkinEngine;
end;

{ VCL }

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

{ Properties }

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

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

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

  Invalidate;
end;

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

procedure TSeSkinProgressBar.SetVersion(const Value: TSeSkinVersion);
begin

end;

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

end.
