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

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

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

interface

uses Messages, Windows, SysUtils, Classes, Graphics, Menus, Controls, Forms,
  StdCtrls, Mask, KsBitmap, KsUtils, KsControls, KsGrids, KsSkinObjects,
  KsSkinSource, KsSkinEngine, KsSkinVersion;

type

  { TSeSkinDrawGrid }

  TSeSkinDrawGrid = class(TKsCustomDrawGrid)
  private
    FSkinGrid: TSeSkinObject;
    FSkinEngine: TSeSkinEngine;
    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;
    function CreateScrollBar: TControl; override;
    { Drawing }
    procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; 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;

  { TSeSkinStringGrid }

  TSeSkinStringGrid = class(TKsCustomStringGrid)
  private
    FSkinGrid: TSeSkinObject;
    FSkinEngine: TSeSkinEngine;
    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;
    function CreateScrollBar: TControl; override;
    { Drawing }
    procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; 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 KsSkinScrollBars;

{ TSeSkinDrawGrid }

constructor TSeSkinDrawGrid.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  SkinObject := 'Grid';
  DefaultDrawing := false;
end;

destructor TSeSkinDrawGrid.Destroy;
begin
  if FSkinGrid <> nil then FSkinGrid.Free;
  inherited Destroy;
end;

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

function TSeSkinDrawGrid.CreateScrollBar: TControl;
begin
  Result := TSeSkinScrollBar.Create(Self);
  (Result as TSeSkinScrollBar).SkinEngine := SkinEngine;
end;

procedure TSeSkinDrawGrid.DrawCell(ACol, ARow: Integer; ARect: TRect;
  AState: TGridDrawState);
var
  TempRect: TRect;
  DrawState: TSeState;
  SkinObject: TSeSkinObject;
begin
  if UseSkin then
  begin
    {  }
    DrawState := ssNormal;

    if not Enabled then
      DrawState := ssDisabled
    else
      if gdFocused in AState then
        DrawState := ssFocused
      else
        if gdSelected in AState then
          DrawState := ssHot;

    if gdFixed in AState then
      SkinObject := FSkinGrid.FindObjectByName('Fixed')
    else
      SkinObject := FSkinGrid.FindObjectByName('Cell');
      
    if SkinObject = nil then
      SkinObject := FSkinGrid;

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

    { Focus Rect }
    if not (csDesigning in ComponentState) and
      (gdFocused in AState) and
      ([goEditing, goAlwaysShowEditor] * Options <>
      [goEditing, goAlwaysShowEditor])
      and not (goRowSelect in Options) then
    begin
      if not UseRightToLeftAlignment then
        Windows.DrawFocusRect(Buffer.DC, ARect)
      else
      begin
        TempRect := ARect;
        TempRect.Left := ARect.Right;
        TempRect.Right := ARect.Left;
        Windows.DrawFocusRect(Buffer.DC, TempRect);
      end;
    end;
  end
  else
  begin
    { Standard }
    DefaultDrawCell(ACol, ARow, ARect, AState)
  end;
  inherited ;
end;

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

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

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

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

  SkinEngine := FSkinEngine;
end;

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

{ properties }

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

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

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

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

  if HScrollBar <> nil then
    TSeSkinScrollBar(HScrollBar).SkinEngine := Value;
  if VScrollBar <> nil then
    TSeSkinScrollBar(VScrollBar).SkinEngine := Value;

  Invalidate;
end;

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

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

{ TSeSkinStringGrid =============================================================}

constructor TSeSkinStringGrid.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  SkinObject := 'Grid';
  DefaultDrawing := false;
end;

destructor TSeSkinStringGrid.Destroy;
begin
  if FSkinGrid <> nil then FSkinGrid.Free;
  inherited Destroy;
end;

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

function TSeSkinStringGrid.CreateScrollBar: TControl;
begin
  Result := TSeSkinScrollBar.Create(Self);
  (Result as TSeSkinScrollBar).SkinEngine := SkinEngine;
end;

procedure TSeSkinStringGrid.DrawCell(ACol, ARow: Integer; ARect: TRect;
  AState: TGridDrawState);
var
  TempRect: TRect;
  DrawState: TSeState;
  SkinObject: TSeSkinObject;
begin
  if UseSkin then
  begin
    {  }
    DrawState := ssNormal;

    if not Enabled then
      DrawState := ssDisabled
    else
      if gdFixed in AState then
        DrawState := ssPressed
      else
        if gdFocused in AState then
          DrawState := ssFocused
        else
          if gdSelected in AState then
            DrawState := ssHot;

    SkinObject := FSkinGrid.FindObjectByName('Cell');
    if SkinObject = nil then
      SkinObject := FSkinGrid;

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

    { Focus Rect }
    if not (csDesigning in ComponentState) and
      (gdFocused in AState) and
      ([goEditing, goAlwaysShowEditor] * Options <>
      [goEditing, goAlwaysShowEditor])
      and not (goRowSelect in Options) then
    begin
      if not UseRightToLeftAlignment then
        Windows.DrawFocusRect(Buffer.DC, ARect)
      else
      begin
        TempRect := ARect;
        TempRect.Left := ARect.Right;
        TempRect.Right := ARect.Left;
        Windows.DrawFocusRect(Buffer.DC, TempRect);
      end;
    end;
    { Draw text }
    Inc(ARect.Left, 2);
    Inc(ARect.Top, 2);
    Buffer.DrawText(Cells[ACol, ARow], ARect, 0);
  end
  else
  begin
    { Standard }
    DefaultDrawCell(ACol, ARow, ARect, AState);
    { Draw text }
    Inc(ARect.Left, 2);
    Inc(ARect.Top, 2);
    Buffer.DrawText(Cells[ACol, ARow], ARect, 0);
  end;
  inherited ;
end;

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

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

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

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

  SkinEngine := FSkinEngine;
end;

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

{ properties }

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

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

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

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

  if HScrollBar <> nil then
    TSeSkinScrollBar(HScrollBar).SkinEngine := Value;
  if VScrollBar <> nil then
    TSeSkinScrollBar(VScrollBar).SkinEngine := Value;

  Invalidate;
end;

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

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

end.
