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

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

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

interface

uses
  Windows, Classes, Messages, Graphics, Controls, Forms, StdCtrls, ExtCtrls,
  KsBitmap, KsUtils, KsControls, KsEdits, KsComboBoxs, KsListBoxs,
  KsSkinListBoxs, KsSkinVersion, KsSkinObjects, KsSkinSource, KsSkinEngine;

type

{ TSeSkinComboBox class }

  TSeSkinComboBox = class(TKsCustomComboBox)
  private
    FSkinEngine: TSeSkinEngine;
    FSkinObject: string;
    FSkinComboBox: TSeSkinObject;
    FButtonWidth: integer;
    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;
    { Edit }
    procedure PaintBorder(Buffer: TKsBitmap); override;
    procedure PaintBackground(Rect: TRect; Buffer: TKsBitmap); override;
    { Combo }
    procedure BeforeDropDown; override;
    function CreateListBox(AOwner: TComponent): TKsCustomListBox; override;
    function GetButtonHeight: integer; 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 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 {===============================================================}

{ TSeSkinComboBox }

constructor TSeSkinComboBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FSkinObject := 'ComboBox';
end;

destructor TSeSkinComboBox.Destroy;
begin
  if FSkinComboBox <> nil then FSkinComboBox.Free;
  inherited Destroy;
end;

procedure TSeSkinComboBox.Loaded;
begin
  inherited;
end;

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

{ Combo }

procedure TSeSkinComboBox.BeforeDropDown;
begin
  inherited;
  if ListBox <> nil then
    (ListBox as TSeSkinListBox).SkinEngine := SkinEngine;
end;

function TSeSkinComboBox.CreateListBox(AOwner: TComponent): TKsCustomListBox;
begin
  Result := TSeSkinListBox.Create(AOwner);
end;

function TSeSkinComboBox.GetButtonHeight: integer;
begin
  Result := inherited GetButtonHeight;

  if FButtonWidth <> 0 then
    Result := FButtonWidth
  else
    Result := inherited GetButtonHeight;
end;

{ Drawing }

procedure TSeSkinComboBox.PaintBackground(Rect: TRect; Buffer: TKsBitmap);
var
  SkinObject: TSeSkinObject;
  DrawState: TSeState;
begin
  if Buffer = nil then Exit;

  if not UseSkin then
  begin
    inherited ;
    Exit;
  end;

  if not Enabled then
    DrawState := ssDisabled
  else
    if MouseInControl then
      DrawState := ssHot
    else
      if Focused then
        DrawState := ssFocused
      else
        DrawState := ssNormal;

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

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

procedure TSeSkinComboBox.PaintBorder(Buffer: TKsBitmap);
var
  R: TRect;
  SkinObject: TSeSkinObject;
  DrawState: TSeState;
begin
  if Buffer = nil then Exit;

  if not UseSkin then
  begin
    inherited ;
    Exit;
  end;

  if not Enabled then
    DrawState := ssDisabled
  else
    if MouseInControl then
      DrawState := ssHot
    else
      if Focused then
        DrawState := ssFocused
      else
        DrawState := ssNormal;

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

  { Draw Button }
  R := GetButtonRect;

  if not Enabled then
    DrawState := ssDisabled
  else
    if MouseOnButton then
      DrawState := ssHot
    else
      if Focused then
        DrawState := ssFocused
      else
        DrawState := ssNormal;

  SkinObject := FSkinComboBox.FindObjectByName('Button');
  if SkinObject <> nil then
  begin
    SkinObject.State := DrawState;
    SkinObject.BoundsRect := R;
    SkinObject.Draw(Buffer);
  end;

  R := GetEditRect;
  InflateRect(R, 1, 2);
  PaintListItem(R, Buffer);
end;

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

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

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

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

  SkinEngine := FSkinEngine;
end;

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

{ Properties }

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

procedure TSeSkinComboBox.SetSkinEngine(const Value: TSeSkinEngine);
var
  SkinObject: TSeSkinObject;
begin
  FSkinEngine := Value;

  if ListBox <> nil then
    (ListBox as TSeSkinListBox).SkinEngine := SkinEngine;

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

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

    if FSkinComboBox <> nil then
    begin
      FSkinComboBox.ParentControl := Self;
      { Set Font }
      if FSkinComboBox.FindObjectByName('Text') <> nil then
      begin
        SkinObject := FSkinComboBox.FindObjectByName('Text');
        Self.Font := SkinObject.Font;
      end
      else
        Self.Font := FSkinComboBox.Font;

      if FSkinComboBox.FindObjectByName('Button') <> nil then
        FButtonWidth := FSkinComboBox.FindObjectByName('Button').Width;
    end;
  end
  else
  begin
    if FSkinComboBox <> nil then FSkinComboBox.Free;
    FSkinComboBox := nil;
  end;

  Invalidate;
end;

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

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

end.
