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

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

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

interface

uses
  Windows, Classes, Messages, Graphics, Controls, Forms, StdCtrls, ExtCtrls,
  KsBitmap, KsUtils, KsControls, KsEdits, KsMenus, KsItems, KsSkinVersion,
  KsSkinObjects, KsSkinSource, KsSkinEngine;

type

{ TSeSkinEdit class }

  TSeSkinEdit = class(TKsCustomEdit)
  private
    FSkinEngine: TSeSkinEngine;
    FSkinEdit: 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 }
    function CreatePopupMenu(AOwner: TComponent): TKsCustomPopupMenu; override;
    function CreatePopupMenuItem(AOwner: TComponent): TKsCustomItem; override;

    function GetEditRect: TRect; override;
    procedure PaintBorder(Buffer: TKsBitmap); override;
    procedure PaintBackground(Rect: TRect; Buffer: TKsBitmap); 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 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;
    { Specifies the appearance and behavior by using specified theme's from TSeSkinEngine .
      See Also:
        TSeSkinEngine
    }
    property SkinEngine: TSeSkinEngine read FSkinEngine write SetSkinEngine;
    property Version: TSeSkinVersion read GetVersion write SetVersion
      stored false;
  end;

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

uses KsSkinMenus, KsSkinItems;

{ TSeSkinEdit }

constructor TSeSkinEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FSkinObject := 'Edit';
end;

destructor TSeSkinEdit.Destroy;
begin
  if FSkinEdit <> nil then FSkinEdit.Free;
  inherited Destroy;
end;

procedure TSeSkinEdit.Loaded;
begin
  inherited Loaded;
end;

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

{ Edits }

function TSeSkinEdit.CreatePopupMenu(AOwner: TComponent): TKsCustomPopupMenu;
begin
  Result := TSeSkinPopupMenu.Create(AOwner);
  (Result as TSeSkinPopupMenu).SkinEngine := FSkinEngine;
end;

function TSeSkinEdit.CreatePopupMenuItem(AOwner: TComponent): TKsCustomItem;
begin
  Result := TSeSkinItem.Create(AOwner);
  (Result as TSeSkinItem).SkinEngine := FSkinEngine;
end;

procedure TSeSkinEdit.PaintBackground(Rect: TRect; Buffer: TKsBitmap);
var
  SkinObject: TSeSkinObject;
  DrawState: TSeState;
begin
  if UseSkin then
  begin
    if not Enabled then
      DrawState := ssDisabled
    else
      if MouseInControl then
        DrawState := ssHot
      else
        if Focused then
          DrawState := ssFocused
        else
          DrawState := ssNormal;

    SkinObject := FSkinEdit.FindObjectByName('Frame');
    if SkinObject = nil then SkinObject := FSkinEdit;

    if SkinObject <> nil then
    begin
      SkinObject.State := DrawState;
      with GetEditRect do
        SkinObject.BoundsRect := Classes.Rect(-Left, -Top, FWidth, FHeight);
      SkinObject.Draw(Buffer);
    end;
  end
  else
    inherited ;
end;

procedure TSeSkinEdit.PaintBorder(Buffer: TKsBitmap);
var
  SkinObject: TSeSkinObject;
  DrawState: TSeState;
begin
  if UseSkin then
  begin
    if not Enabled then
      DrawState := ssDisabled
    else
      if MouseInControl then
        DrawState := ssHot
      else
        if Focused then
          DrawState := ssFocused
        else
          DrawState := ssNormal;

    SkinObject := FSkinEdit.FindObjectByName('Frame');

    if SkinObject <> nil then
    begin
      SkinObject.State := DrawState;
      SkinObject.BoundsRect := Rect(0, 0, FWidth, FHeight);
      SkinObject.Draw(Buffer);
    end;
  end
  else
    inherited ;
end;

function TSeSkinEdit.GetEditRect: TRect;
begin
  if UseSkin then
  begin
    Result.Left := 4;
    Result.Top := 4;
    Result.Right := Width - 4;
    Buffer.Font.Assign(Font);
    Result.Bottom := Result.Top + Buffer.TextHeight('Pq');
  end
  else
    Result := inherited GetEditRect;
end;

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

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

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

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

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

  SkinEngine := FSkinEngine;
end;

{ Properties }

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

procedure TSeSkinEdit.SetSkinEngine(const Value: TSeSkinEngine);
var
  SkinObject: TSeSkinOBject;
begin
  FSkinEngine := Value;

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

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

    if FSkinEdit <> nil then
    begin
      FSkinEdit.ParentControl := Self;

      if FSkinEdit.FindObjectByName('Text') <> nil then
      begin
        SkinObject := FSkinEdit.FindObjectByName('Text');
        Self.Font := SkinObject.Font;
      end
      else
        Self.Font := FSkinEdit.Font; 
    end;
  end
  else
  begin
    if FSkinEdit <> nil then FSkinEdit.Free;
    FSkinEdit := nil;
  end;

  if (PopupMenu <> nil) then
    (PopupMenu as TSeSkinPopupMenu).SkinEngine := Value;

  Invalidate;
end;

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

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

end.
