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

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

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

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, ExtCtrls,
  Menus, KsItems, ImgList, KsBitmap, KsUtils, KsControls, KsMenus,
  KsSkinItems, KsSkinVersion, KsSkinObjects, KsSkinSource, KsSkinEngine;

type

{ TSeSkinMenuBar class }

{ TSeSkinMenuBar is a menu bar and its accompanying drop-down menus for a form. }
  TSeSkinMenuBar = class(TKsCustomMenuBar)
  private
    FSkinEngine: TSeSkinEngine;
    FSkinMenuBar: TSeSkinObject;
    FSkinObject: string;
    function GetVersion: TSeSkinVersion;
    procedure SetVersion(const Value: TSeSkinVersion);
    procedure SetSkinEngine(const Value: TSeSkinEngine);

    procedure WMInvalidateSkinObject(var Msg: TMessage); message WM_INVALIDATESKINOBJECT;
    procedure WMBeforeChange(var Msg: TMessage); message WM_BEFORECHANGE;
    procedure WMSkinChange(var Msg: TMessage); message WM_SKINCHANGE;
    procedure SetSkinObject(const Value: string);
  protected
    function UseSkin: boolean;
    { need for }
    class function GetItemClassProc: TKsCustomItemClass; override;
    { Protected }
    procedure ItemsChanged; override;
    function GetViewRect: TRect; override;
    procedure PaintBuffer; override;
    { VCL protected  }
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Loaded; override;
  published
    property Align;
    property Anchors;
    property Images;
    property Items;
    property PopupBlend;
    property PopupBlendValue;
    { 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;

{ TSeSkinPopupMenu class }

{ TSeSkinPopupMenu encapsulates the properties, methods, and events of a advanced pop-up menu. }
  TSeSkinPopupMenu = class(TKsCustomPopupMenu)
  private
    FSkinEngine: TSeSkinEngine;
    function GetVersion: TSeSkinVersion;
    procedure SetVersion(const Value: TSeSkinVersion);
    procedure SetSkinEngine(const Value: TSeSkinEngine);
  protected
    { need for }
    class function GetItemClassProc: TKsCustomItemClass; override;
    { VCL protected  }
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Loaded; override;

    procedure Popup(X, Y: integer); override;
  published
    property Items;
    property Images;
    { 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;
    property Version: TSeSkinVersion read GetVersion write SetVersion stored False;
  end;

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

uses KsControlBars;

{ TSeSkinMenuBar ===============================================================}

constructor TSeSkinMenuBar.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FSkinObject := 'MenuBar';
end;

destructor TSeSkinMenuBar.Destroy;
begin
  inherited Destroy;
end;

procedure TSeSkinMenuBar.Loaded;
begin
  inherited Loaded;
  SkinEngine := FSkinEngine;
end;

class function TSeSkinMenuBar.GetItemClassProc: TKsCustomItemClass;
begin
  Result := TSeSkinItem;
end;

procedure TSeSkinMenuBar.ItemsChanged;
begin
  (Items as TSeSkinItem).SkinEngine := FSkinEngine;
  inherited;
end;

{ Internal }

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

procedure TSeSkinMenuBar.PaintBuffer;
var
  SkinObject: TSeSkinObject;
begin
  if UseSkin then
  begin
    if Parent is TKsCustomControlBar then
      SkinObject := FSkinMenuBar.FindObjectByName('ToolbarFrame')
    else
      SkinObject := FSkinMenuBar.FindObjectByName('Frame');

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

    if (View <> nil) and (Buffer <> nil) then
    begin
      with GetViewRect do
      begin
        View.Left := Left;
        View.Top := Top;
      end;
      View.Paint(Buffer);
    end;
  end
  else
    inherited ;
end;

function TSeSkinMenuBar.GetViewRect: TRect;
begin
  if UseSkin and (FSkinMenuBar.FindObjectByName('Frame') <> nil) then
    with FSkinMenuBar.FindObjectByName('Frame') do
    begin
      Result := Rect(MarginLeft, MarginTop, Width - MarginRight, Height - MarginBottom);
    end
  else
    Result := inherited GetViewRect;
end;

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

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

  if FSkinMenuBar <> nil then FSkinMenuBar.Free;
  FSkinMenuBar := nil;
  TSeSkinItem(Items).SkinEngine := FSkinEngine;
end;

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

  SkinEngine := FSkinEngine;
end;

{ VCL protected }

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

{ Properties }

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

procedure TSeSkinMenuBar.SetSkinEngine(const Value: TSeSkinEngine);
begin
  FSkinEngine := Value;
  TSeSkinItem(Items).SkinEngine := Value;

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

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

    if FSkinMenuBar <> nil then
    begin
      FSkinMenuBar.ParentControl := Self;
      if (FSkinMenuBar.FindObjectByName('Frame') <> nil) then
        Height := FSkinMenuBar.FindObjectByName('Frame').Height;
    end;
  end
  else
  begin
    if FSkinMenuBar <> nil then FSkinMenuBar.Free;
    FSkinMenuBar := nil;
  end;

  Invalidate;
end;

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

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

{ TSeSkinPopupMenu ==========================================================}

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

destructor TSeSkinPopupMenu.Destroy;
begin
  inherited;
end;

class function TSeSkinPopupMenu.GetItemClassProc: TKsCustomItemClass;
begin
  Result := TSeSkinItem;
end;

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

procedure TSeSkinPopupMenu.Loaded;
begin
  inherited;
  SkinEngine := FSkinEngine;
end;

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

procedure TSeSkinPopupMenu.SetSkinEngine(const Value: TSeSkinEngine);
begin
  FSkinEngine := Value;
  TSeSkinItem(Items).SkinEngine := SkinEngine;
end;

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

procedure TSeSkinPopupMenu.Popup(X, Y: integer);
begin
  SkinEngine := FSkinEngine;
  
  inherited Popup(X, Y);
end;

end.
