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

  Custom Menus      
  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 EpIIMenus;

{$I EpIIVer.inc}
{$T-,W-,X+,P+}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, ExtCtrls,
  Menus, KsItems, ImgList, KsBitmap, KsUtils, KsControls, KsMenus, 
  EpIIItems, EpIIVersion, EpIIStyles;

type

{ TEpMenuBar class }

  TEpMenuBar = class(TKsCustomMenuBar)
  private
    FBackground: TEpBackground;
    FBorder: TepBorder;
    FPopupBackground: TEpBackground;
    FPopupBorder: TepBorder;
    FItemBackground: TEpBackground;
    FItemBorder: TepBorder;
    FItemFont: TFont;
    function GetVersion: TEpVersion;
    procedure SetVersion(const Value: TEpVersion);
    procedure SetBackground(const Value: TEpBackground);
    procedure SetBorder(const Value: TepBorder);
    procedure SetPopupBackground(const Value: TEpBackground);
    procedure SetPopupBorder(const Value: TepBorder);
    procedure SetItemBackground(const Value: TEpBackground);
    procedure SetItemBorder(const Value: TepBorder);
    procedure SetItemFont(const Value: TFont);
  protected
    procedure DoFontChange(Sender: TObject);
    procedure DoBackgroundChange(Sender: TObject);
    procedure DoBorderChange(Sender: TObject);
    { need for }
    class function GetItemClassProc: TKsCustomItemClass; override;
    { }
    function GetViewRect: TRect; override;
    procedure PaintBackground; 
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Loaded; override;
  published
    property Align;
    property Anchors;
    { It's possible to draw the background for control. Based on TEpBackground class.
      See Also:
        TEpBackground
    }
    property Background: TEpBackground read FBackground write SetBackground;
    property Blend;
    property BlendValue;
    { It's possible to draw the border for control. Based on TEpBorder class.
      See Also:
        TEpBorder
    }
    property Border: TepBorder read FBorder write SetBorder;
    property Images;
    { Link menu item Background property
      See Also:
        TEpBackground
    }
    property ItemBackground: TEpBackground read FItemBackground write SetItemBackground;
    { Link menu item Border property
      See Also:
        TEpBorder
    }
    property ItemBorder: TepBorder read FItemBorder write SetItemBorder;
    property ItemFont: TFont read FItemFont write SetItemFont;
    property Items;
    { Link popup menu Background property
      See Also:
        TEpPopupMenu,
        TEpBackground
    }
    property PopupBackground: TEpBackground read FPopupBackground write SetPopupBackground;
    property PopupBlend;
    property PopupBlendValue;
    { Link popup menu Border property
      See Also:
        TEpPopupMenu,
        TEpBorder
    }
    property PopupBorder: TepBorder read FPopupBorder write SetPopupBorder;
    property Version: TEpVersion read GetVersion write SetVersion stored False;
  end;

{ TEpPopupMenu class }

  TEpPopupMenu = class(TKsCustomPopupMenu)
  private
    FItemBackground: TEpBackground;
    FPopupBackground: TEpBackground;
    FItemBorder: TepBorder;
    FPopupBorder: TepBorder;
    FItemFont: TFont;
    function GetVersion: TEpVersion;
    procedure SetVersion(const Value: TEpVersion);
    procedure SetItemBackground(const Value: TEpBackground);
    procedure SetItemBorder(const Value: TepBorder);
    procedure SetItemFont(const Value: TFont);
    procedure SetPopupBackground(const Value: TEpBackground);
    procedure SetPopupBorder(const Value: TepBorder);
  protected
    { need for }
    class function GetItemClassProc: TKsCustomItemClass; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    procedure Popup(X, Y: integer); override;
  published
    { Link menu item Background property
      See Also:
        TEpBackground
    }
    property ItemBackground: TEpBackground read FItemBackground write SetItemBackground;
    { Link menu item Border property
      See Also:
        TEpBorder
    }
    property ItemBorder: TepBorder read FItemBorder write SetItemBorder;
    property ItemFont: TFont read FItemFont write SetItemFont;
    property Items;
    { Link popup menu Background property
      See Also:
        TEpPopupMenu,
        TEpBackground
    }
    property PopupBackground: TEpBackground read FPopupBackground write SetPopupBackground;
    property PopupBlend;
    property PopupBlendValue;
    { Link popup menu Border property
      See Also:
        TEpPopupMenu,
        TEpBorder
    }
    property PopupBorder: TepBorder read FPopupBorder write SetPopupBorder;
    property Version: TEpVersion read GetVersion write SetVersion stored False;
  end;

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

{ TEpMenuBar ===============================================================}

constructor TEpMenuBar.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FBackground := TEpBackground.Create;
  FBackground.OnChange := DoBackgroundChange;
  FBorder := TEpBorder.Create;
  FBorder.OnChange := DoBorderChange;

  FPopupBackground := TEpBackground.Create;
  FPopupBorder := TEpBorder.Create;

  FItemBackground := TEpBackground.Create;
  FItemBackground.OnChange := DoBackgroundChange;
  FItemBorder := TEpBorder.Create;
  FItemBorder.OnChange := DoBorderChange;
  FItemFont := TFont.Create;
  FItemFont.OnChange := DoFontChange;

  Align := alNone;
end;

destructor TEpMenuBar.Destroy;
begin
  FItemFont.Free;
  FItemBorder.Free;
  FItemBackground.Free;
  FPopupBorder.Free;
  FPopupBackground.Free;
  FBorder.Free;
  FBackground.Free;
  inherited Destroy;
end;

procedure TEpMenuBar.Loaded;
begin
  ItemBackground := FItemBackground;
  ItemBorder := FItemBorder;
  ItemFont := FItemFont;
  PopupBackground := FPopupBackground;
  PopupBorder := FPopupBorder;

  inherited Loaded;
end;

class function TEpMenuBar.GetItemClassProc: TKsCustomItemClass;
begin
  Result := TEpItem;
end;

function TEpMenuBar.GetViewRect: TRect;
begin
  if FBorder <> nil then
    with FBorder do
    begin
      case Style of
        ebsSingleBorder: Result := Rect(Width, Width, FWidth - Width, FHeight - Width);
        ebsBitmapBorder: Result := Rect(BitmapMarginX, BitmapMarginY,
          FWidth - BitmapMarginX, FHeight - BitmapMarginY);
      else
        Result := inherited GetViewRect;
      end;
    end
  else
    Result := inherited GetViewRect;
end;

procedure TEpMenuBar.PaintBackground;
begin
  if (Buffer <> nil) then
  begin
    if FBackground <> nil then
      FBackground.Draw(Buffer, Rect(0, 0, FWidth, FHeight));
    if FBorder <> nil then
      FBorder.Draw(Buffer, Rect(0, 0, FWidth, FHeight), 0);
  end;
end;

procedure TEpMenuBar.DoFontChange(Sender: TObject);
begin
  TEpItem(Items).Font := FItemFont;
  if View <> nil then
    View.CalcSize;
  Invalidate;
end;

procedure TEpMenuBar.DoBackgroundChange(Sender: TObject);
begin
  if View <> nil then
    View.CalcSize;
  Invalidate;
end;

procedure TEpMenuBar.DoBorderChange(Sender: TObject);
begin
  if View <> nil then
    View.CalcSize;
  Invalidate;
end;

{ Propepties }

function TEpMenuBar.GetVersion: TEpVersion;
begin
  Result := EpVersion;
end;

procedure TEpMenuBar.SetBackground(const Value: TEpBackground);
begin
  FBackground.Assign(Value);
  Invalidate;
end;

procedure TEpMenuBar.SetBorder(const Value: TepBorder);
begin
  FBorder.Assign(Value);
  Invalidate;
end;

procedure TEpMenuBar.SetVersion(const Value: TEpVersion);
begin
end;

procedure TEpMenuBar.SetPopupBackground(const Value: TEpBackground);
begin
  FPopupBackground.Assign(Value);
  TEpItem(Items).PopupBackground := FPopupBackground;
end;

procedure TEpMenuBar.SetPopupBorder(const Value: TepBorder);
begin
  FPopupBorder.Assign(Value);
  TEpItem(Items).PopupBorder := FPopupBorder;
end;

procedure TEpMenuBar.SetItemBackground(const Value: TEpBackground);
begin
  FItemBackground.Assign(Value);
  TEpItem(Items).Background := FItemBackground;
  Invalidate;
end;

procedure TEpMenuBar.SetItemBorder(const Value: TepBorder);
begin
  FItemBorder.Assign(Value);
  TEpItem(Items).Border := FItemBorder;
  if View <> nil then
    View.CalcSize;
end;

procedure TEpMenuBar.SetItemFont(const Value: TFont);
begin
  FItemFont.Assign(Value);
  TEpItem(Items).Font := FItemFont;
end;

{ TEpPopupMenu ==========================================================}

constructor TEpPopupMenu.Create(AOwner: TComponent);
begin
  inherited;
  FPopupBackground := TEpBackground.Create;
  FPopupBorder := TEpBorder.Create;
  FItemBackground := TEpBackground.Create;
  FItemBorder := TEpBorder.Create;
  FItemFont := TFont.Create;
end;

destructor TEpPopupMenu.Destroy;
begin
  FItemFont.Free;
  FItemBorder.Free;
  FItemBackground.Free;
  FPopupBorder.Free;
  FPopupBackground.Free;
  inherited;
end;

class function TEpPopupMenu.GetItemClassProc: TKsCustomItemClass;
begin
  Result := TEpItem;
end;

function TEpPopupMenu.GetVersion: TEpVersion;
begin
  Result := EpVersion;
end;

procedure TEpPopupMenu.Popup(X, Y: integer);
begin
  ItemBackground := FItemBackground;
  ItemBorder := FItemBorder;
  ItemFont := FItemFont;
  PopupBackground := FPopupBackground;
  PopupBorder := FPopupBorder;

  inherited Popup(X, Y);
end;

procedure TEpPopupMenu.SetItemBackground(const Value: TEpBackground);
begin
  FItemBackground.Assign(Value);
  TEpItem(Items).Background := FItemBackground;
end;

procedure TEpPopupMenu.SetItemBorder(const Value: TepBorder);
begin
  FItemBorder.Assign(Value);
  TEpItem(Items).Border := FItemBorder;
end;

procedure TEpPopupMenu.SetItemFont(const Value: TFont);
begin
  FItemFont.Assign(Value);
  TEpItem(Items).Font := FItemFont;
end;

procedure TEpPopupMenu.SetPopupBackground(const Value: TEpBackground);
begin
  FPopupBackground.Assign(Value);
  TEpItem(Items).PopupBackground := FPopupBackground;
end;

procedure TEpPopupMenu.SetPopupBorder(const Value: TepBorder);
begin
  FPopupBorder.Assign(Value);
  TEpItem(Items).PopupBorder := FPopupBorder;
end;

procedure TEpPopupMenu.SetVersion(const Value: TEpVersion);
begin
end;

end.


