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

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

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

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, ExtCtrls,
  KsBitmap, KsControls, KsForms, KsItems, KsMenuForm, KsSkinObjects,
  KsSkinSource, KsSkinEngine;

type

{ TSeSkinPopupForm }

  TSeSkinPopupForm = class(TKsPopupForm)
  private
    function UseSkin: boolean;
  protected
    function GetClientRect: TRect; override;
    procedure PaintNCArea(Buffer: TKsBitmap); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

{ TSeSkinPopupWindow class }

  TSeSkinPopupWindow = class(TKsPopupWindow)
  private
    FSkinEngine: TSeSkinEngine;
  protected
    class function GetFormClass: TKsPopupFormClass; override;
  public
    constructor CreatePopupWindow(AOwner: TComponent; AItems: TKsCustomItem;
      AParentView: TKsItemView); override;
    destructor Destroy; override;

    property SkinEngine: TSeSkinEngine read FSkinEngine write FSkinEngine;
  end;

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

uses KsMenus, KsSkinItems;

{ TSeSkinPopupForm ================================================================}

constructor TSeSkinPopupForm.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
end;

destructor TSeSkinPopupForm.Destroy;
begin
  inherited;
end;

function TSeSkinPopupForm.UseSkin: boolean;
begin
  Result := false;
  if PopupWindow = nil then Exit;

  with TSeSkinPopupWindow(PopupWindow) do
    if (FSkinEngine <> nil) and (FSkinEngine.SkinSource <> nil) and
       (not FSkinEngine.SkinSource.IsChanging) and
       (FSkinEngine.SkinSource.Count > 0) and
       (FSkinEngine.SkinSource.GetObjectByName('PopupMenu') <> nil)
    then
      Result := true;
end;

function TSeSkinPopupForm.GetClientRect: TRect;
var
  SkinObject: TSeSkinObject;
begin
  if UseSkin then
  begin
    SkinObject := TSeSkinPopupWindow(PopupWindow).SkinEngine.SkinSource.GetObjectByName('PopupMenu');
    SkinObject := SkinObject.FindObjectByName('Frame');
    if SkinObject <> nil then
      with SkinObject do
        Result := Rect(MarginLeft, MarginTop, FWidth - MarginRight, FHeight - MarginBottom)
    else
      Result := Rect(3, 3, FWidth - 3, FHeight - 3)
  end
  else
    Result := inherited GetClientRect;
end;

procedure TSeSkinPopupForm.PaintNCArea(Buffer: TKsBitmap);
var
  SkinObject: TSeSkinObject;
begin
  if UseSkin then
  begin
    SkinObject := TSeSkinPopupWindow(PopupWindow).SkinEngine.SkinSource.GetObjectByName('PopupMenu');
    SkinObject := SkinObject.FindObjectByName('Frame');

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

{ TSeSkinPopupWindow ===============================================================}

constructor TSeSkinPopupWindow.CreatePopupWindow(AOwner: TComponent;
  AItems: TKsCustomItem; AParentView: TKsItemView);
begin
  inherited CreatePopupWindow(AOwner, AItems, AParentView);
  FSkinEngine := TSeSkinItem(AItems).SkinEngine;
end;

destructor TSeSkinPopupWindow.Destroy;
begin
  inherited Destroy;
end;

class function TSeSkinPopupWindow.GetFormClass: TKsPopupFormClass;
begin
  Result := TSeSkinPopupForm;
end;

end.
