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

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

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

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, KsScrollBars, KsBitmap, KsUtils, KsListBoxs, KsSkinVersion,
  KsSkinObjects, KsSkinSource, KsSkinEngine;

type

  TSeSkinListBox = class(TKsCustomListBox)
  private
    FSkinEngine: TSeSkinEngine;
    FSkinListBox: TSeSkinObject;
    FSkinObject: string;
    function GetVersion: TSeSkinVersion;
    procedure SetVersion(const Value: TSeSkinVersion);
    procedure SetSkinEngine(const Value: TSeSkinEngine);
    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: TKsCustomScrollBar; override;
    function GetBorderWidth: integer; override;
    function GetChckBxFieldWidth: integer; override;
    function GetCheckBoxSize: TPoint; override;
    function GetItemHeight(Index: integer): integer; override;

    procedure DrawBorder; override;
    procedure DrawBackground; override;
    procedure DrawItem(Buffer: TKsBitmap; Index: integer; ARect: TRect); override;
    procedure DrawCheckBox(Index: integer; ARect: TRect); override;
    { VCL protected  }
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    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 {===============================================================}

uses KsSkinScrollBars;

{ TSeSkinListBox }

constructor TSeSkinListBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FSkinObject := 'ListBox';
end;

destructor TSeSkinListBox.Destroy;
begin
  if FSkinListBox<> nil then FSkinListBox.Free;
  inherited;
end;

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

function TSeSkinListBox.CreateScrollBar: TKsCustomScrollBar;
begin
  Result := TSeSkinScrollBar.Create(Self);
  (Result as TSeSkinScrollBar).SkinEngine := FSkinEngine;
end;

function TSeSkinListBox.GetChckBxFieldWidth: integer;
begin
  Result := inherited GetChckBxFieldWidth;
  if UseSkin then
    Result := Result + 6;
end;

function TSeSkinListBox.GetBorderWidth: integer;
var
  SkinObject: TSeSkinObject;
begin
  if UseSkin then
  begin
    SkinObject := FSkinListBox.FindObjectByName('Frame');
    if SkinObject = nil then
      SkinObject := FSkinListBox;

    if SkinObject <> nil then
    begin
      Result := SkinObject.MarginLeft;
      if SkinObject.MarginTop > Result then
        Result := SkinObject.MarginTop;
      if SkinObject.MarginRight > Result then
        Result := SkinObject.MarginRight;
      if SkinObject.MarginBottom > Result then
        Result := SkinObject.MarginBottom;
    end;
  end
  else
    Result := inherited GetBorderWidth;
end;

function TSeSkinListBox.GetCheckBoxSize: TPoint;
begin
  Result := inherited GetCheckBoxSize;

  if UseSkin then
    Result := Result;
end;

function TSeSkinListBox.GetItemHeight(Index: integer): integer;
begin
  Result := inherited GetItemHeight(Index);

  if UseSkin and ShowCheckBoxes then
    Result := Result + 4;
end;

{ Drawing }

procedure TSeSkinListBox.DrawCheckBox(Index: integer; ARect: TRect);
var
  DrawState: TSeState;
  SkinObject: TSeSkinObject;
begin
  if not UseSkin then
  begin
    inherited;
    Exit;
  end;

  if not Enabled then
    DrawState := ssDisabled
  else
    if MouseOnItemIndex = Index then
      DrawState := ssHot
    else
      DrawState := ssNormal;

  case State[Index] of
    cbChecked: SkinObject := FSkinListBox.FindObjectByName('Checked');
    cbGrayed: SkinObject := FSkinListBox.FindObjectByName('Mixed');
  else
    SkinObject := FSkinListBox.FindObjectByName('Unchecked');
  end;

  if SkinObject <> nil then
  begin
    SkinObject.State := DrawState;
    SkinObject.BoundsRect := ARect;
    SkinObject.Draw(Buffer);
  end
  else
    inherited;
end;

procedure TSeSkinListBox.DrawItem(Buffer: TKsBitmap; Index: integer; ARect: TRect);
var
  DrawState: TSeState;
  SkinObject: TSeSkinObject;
  TmpS: string;
  R, TmpRect: TRect;
  TmpState: TOwnerDrawState;
  Color: Longint;
begin
  if UseSkin then
  begin
    DrawState := ssNormal;

    if not Enabled then
      DrawState := ssDisabled
    else
      if Selected[Index] then
        if Focused then
          DrawState := ssFocused
        else
          DrawState := ssPressed;

    { Set Font }
    if FSkinListBox.FindObjectByName('Item') <> nil then
    begin
      SkinObject := FSkinListBox.FindObjectByName('Item');
      SkinObject.State := DrawState;
      SkinObject.BoundsRect := ARect;
      Buffer.Font := SkinObject.Font;
    end
    else
    begin
      inherited ;
      Exit;
    end;


    TmpRect := ARect;
    Inc(TmpRect.Left, 2);

    if ListStyle in [lbFonts, lbFontSamples, lbColors, lbColorNames] then
    begin
      TmpS := Items[Index];
      { Font and Color }
      case ListStyle of
        lbFonts:
          begin
            { Font Listbox }
            SkinObject.Draw(Buffer);

            if (TrueTypeBmp <> nil) and (Integer(Items.Objects[Index]) and TMPF_TRUETYPE <> 0) then
            begin
              R := Rect(0, 0, TrueTypeBmp.Width, TrueTypeBmp.Height);
              RectCenter(R, Rect(TmpRect.Left, TmpRect.Top, TmpRect.Left + DefaultFontColorHeight, TmpRect.Bottom));
              TrueTypeBmp.Draw(Buffer, R.Left, R.Top);
            end;

            Inc(TmpRect.Left, DefaultFontColorHeight + 4);
            Buffer.DrawText(TmpS, TmpRect, DrawTextBiDiModeFlags(DT_LEFT or DT_VCENTER or DT_SINGLELINE));
          end;
        lbFontSamples:
          begin
            { Font Listbox }
            Buffer.Font.Name := Items[Index];

            SkinObject.Draw(Buffer);

            if (TrueTypeBmp <> nil) and (Integer(Items.Objects[Index]) and TMPF_TRUETYPE <> 0) then
            begin
              R := Rect(0, 0, TrueTypeBmp.Width, TrueTypeBmp.Height);
              RectCenter(R, Rect(TmpRect.Left, TmpRect.Top, TmpRect.Left + DefaultFontColorHeight, TmpRect.Bottom));
              TrueTypeBmp.Draw(Buffer, R.Left, R.Top);
            end;

            Inc(TmpRect.Left, DefaultFontColorHeight + 4);
            Buffer.DrawText(TmpS, TmpRect, DrawTextBiDiModeFlags(DT_LEFT or DT_VCENTER or DT_SINGLELINE));
          end;
        lbColors:
          begin
            { Font Listbox }
            SkinObject.Draw(Buffer);

            { Draw Color Rect }
            InflateRect(TmpRect, -4, -4);
            Color := StringToColor(TmpS);
            Buffer.FillRect(TmpRect, KColor(Color));
            Buffer.DrawRect(TmpRect, ckBlack);
          end;
        lbColorNames:
          begin
            { Font Listbox }
            SkinObject.Draw(Buffer);

            { Draw Color Rect }
            Color := StringToColor(TmpS);
            R := Rect(0, 0, ColorRectWidth, DefaultFontColorHeight - 4);
            RectCenter(R, Rect(TmpRect.Left, TmpRect.Top, TmpRect.Left + ColorRectWidth, TmpRect.Bottom));
            Buffer.FillRect(R, KColor(Color));
            Buffer.DrawRect(R, ckBlack);
            { Draw Text}
            if Pos('cl', TmpS) > 0 then
              Delete(TmpS, 1, 2);
            Inc(TmpRect.Left, ColorRectWidth + 4);
            Buffer.DrawText(TmpS, TmpRect, DrawTextBiDiModeFlags(DT_LEFT or DT_VCENTER or DT_SINGLELINE));
          end;
      end;

      Exit;
    end;

    if (ListStyle = lbStandard) or (not Assigned(OnDrawItem)) then
    begin
      TmpS := Items[Index];

      SkinObject.Draw(Buffer);

      if WordWrap then
        Buffer.DrawText(TmpS, TmpRect, DrawTextBiDiModeFlags(DT_LEFT or DT_WORDBREAK))
      else
        Buffer.DrawText(TmpS, TmpRect, DrawTextBiDiModeFlags(DT_LEFT));
    end
    else
     inherited ;
  end
  else
    inherited ;
end;

procedure TSeSkinListBox.DrawBackground;
var
  DrawState: TSeState;
  SkinObject: TSeSkinObject;
begin
  if UseSkin then
  begin
    if not Enabled then
      DrawState := ssDisabled
    else
      if MouseInControl then
        DrawState := ssHot
      else
        DrawState := ssNormal;

    SkinObject := FSkinListBox.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;

procedure TSeSkinListBox.DrawBorder;
begin
  if not UseSkin then
  begin
    inherited;
    Exit;
  end;
end;

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

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

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

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

  SkinEngine := FSkinEngine;
end;

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

{ Properties }

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

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

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

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

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

  if FScrlBar <> nil then
    (FScrlBar as TSeSkinScrollBar).SkinEngine := FSkinEngine;

  Invalidate;
end;

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

end.

