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

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

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

interface

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

type

{ TSeSkinToolBar class }

  TSeSkinToolBar = class(TKsCustomToolBar)
  private
    FSkinEngine: TSeSkinEngine;
    FSkinBar: 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;
    { overrides }
    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
    { 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 {===============================================================}

uses KsForms;

{ TSeSkinToolBar ===============================================================}

constructor TSeSkinToolBar.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FSkinObject := 'ToolBar';
end;

destructor TSeSkinToolBar.Destroy;
begin
  if FSkinBar <> nil then FSkinBar.Free;
  inherited Destroy;
end;

procedure TSeSkinToolBar.Loaded;
begin
  inherited;
end;

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

procedure TSeSkinToolBar.PaintBuffer;
var
  SkinObject: TSeSkinObject;
begin
  if not UseSkin then
    inherited
  else
  begin
    SkinObject := FSkinBar.FindObjectByName('Frame');
    if SkinObject = nil then
      SkinObject := FSkinBar;

    SkinObject.BoundsRect := Rect(0, 0, FWidth, FHeight);
    SkinObject.Draw(Buffer);
  end;
end;

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

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

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

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

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

  SkinEngine := FSkinEngine;
end;

{ Properties }

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

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

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

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

  Invalidate;
end;

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

end;

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

end.
