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

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

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

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, KsBitmap, KsUtils, KsForms, KsEffects, KsHints,
  KsSkinVersion, KsSkinObjects, KsSkinSource, KsSkinEngine;

type

{ TSeSkinHint }

  TSeSkinHint = class(TKsCustomHint)
  private
    FSkinEngine: TSeSkinEngine;
    FSkinHint: TSeSkinObject;
    FSkinObject: string;
    function GetVersion: TSeSkinVersion;
    procedure SetVersion(const Value: TSeSkinVersion);
    procedure SetSkinEngine(const Value: TSeSkinEngine);
    procedure SetSkinObject(const Value: string);
  protected
    function UseSkin: boolean;

    function GetHintBounds(Canvas: TCanvas; const AHint: string): TRect; override;
    procedure PaintHint(Buffer: TKsBitmap; R: TRect; const AHint: string); override;
    { VCL protected  }
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; 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 {===============================================================}

{ TSeSkinHint ===============================================================}

constructor TSeSkinHint.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FSkinObject := 'Hint';
end;

destructor TSeSkinHint.Destroy;
begin
  if FSkinHint <> nil then FSkinHint.Free;
  inherited Destroy;
end;

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

{ Overrides }

function TSeSkinHint.GetHintBounds(Canvas: TCanvas; const AHint: string): TRect;
begin
  Result := Rect(0, 0, 1000, 1000);
  if FSkinHint <> nil then
    Canvas.Font.Assign(FSkinHint.Font);

  DrawText(Canvas.Handle, PChar(AHint), -1, Result, DT_CALCRECT or DT_LEFT or
    DT_WORDBREAK or DT_NOPREFIX);

  if FSkinHint <> nil then
  begin
    Inc(Result.Right, FSkinHint.MarginLeft + FSkinHint.MarginRight);
    Inc(Result.Bottom, FSkinHint.MarginTop + FSkinHint.MarginBottom);
  end
  else
  begin
    Inc(Result.Right, 6);
    Inc(Result.Bottom, 6);
  end;

  if DropShadow then
  begin
    Inc(Result.Right, ShadowWidth);
    Inc(Result.Bottom, ShadowWidth);
  end;
end;

procedure TSeSkinHint.PaintHint(Buffer: TKsBitmap; R: TRect; const AHint: string);
begin
  if UseSkin then
  begin
    FSkinHint.BoundsRect := R;
    FSkinHint.Text := AHint;
    FSkinHint.Draw(Buffer);
  end
  else
    inherited ;
end;

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

{ Properties }

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

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

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

    if FSkinEngine.SkinSource.GetObjectByName(FSkinObject) <> nil then
      FSkinHint := FSkinEngine.SkinSource.GetObjectByName(FSkinObject).CreateCopy(nil);
  end
  else
  begin
    if FSkinHint <> nil then FSkinHint.Free;
    FSkinHint := nil;
  end;
end;

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

end.
