unit menu_class;
interface
uses
Classes, Defines, Windows, SysUtils, Controls, Graphics, JvPanel;
type
TButtonStyle = (etButton, etSeperator, etText);
type
TMenuEntry =
class(TCollectionItem)
private
FPanel: TJvPanel;
FButtonStyle: TButtonStyle;
public
destructor Destroy();
override;
property Button: TJvPanel
read FPanel
write FPanel;
property ButtonStyle: TButtonStyle
read FButtonStyle
write FButtonStyle;
end;
type
TMenuEntries =
class(TCollection)
private
function GetEntry(
Index: Integer): TMenuEntry;
public
constructor Create;
destructor Destroy;
override;
function Add(AOwner: TWinControl): TMenuEntry;
procedure Del(
Index: Integer);
property MenuEntry[
Index: Integer]: TMenuEntry
read GetEntry;
end;
type
TMenuManager =
class(TObject)
private
FOwner: TWinControl;
FXPos: Integer;
FYPos: Integer;
FTempYPos: Integer;
FMenuEntries: TMenuEntries;
FActiveButton: Integer;
FItemClick: TNotifyEvent;
function GetAcctiveButton: Integer;
procedure SetActiveButton(
const Value: Integer);
procedure OnClick(Sender: TObject);
public
constructor Create(AOwner: TWinControl; XPos, YPos: Integer);
destructor Destroy;
override;
procedure AddButton(Caption:
String; ButtonStyle: TButtonStyle = etButton);
procedure RemoveButton(
Index: Integer);
procedure ClearButtons;
function Count: Integer;
property ButtonActive: Integer
read GetAcctiveButton
write SetActiveButton;
property OnItemClick: TNotifyEvent
read FItemClick
write FItemClick;
end;
implementation
{ TMenuManager }
procedure TMenuManager.AddButton(Caption:
String;
ButtonStyle: TButtonStyle);
var
AMenuEntry: TMenuEntry;
begin
Self.FTempYPos := Self.FMenuEntries.Count * 40 + Self.FYPos;
AMenuEntry := Self.FMenuEntries.Add(Self.FOwner);
AMenuEntry.FPanel.DoubleBuffered := True;
AMenuEntry.FPanel.Left := Self.FXPos;
AMenuEntry.FPanel.Top := Self.FTempYPos;
AMenuEntry.FPanel.Height := 33;
AMenuEntry.FPanel.Width := 233;
AMenuEntry.FPanel.Transparent := True;
AMenuEntry.FPanel.BevelInner := bvNone;
AMenuEntry.FPanel.BevelOuter := bvNone;
AMenuEntry.FPanel.Color := clGray;
AMenuEntry.FPanel.Alignment := taLeftJustify;
AMenuEntry.FPanel.Caption := Caption;
AMenuEntry.FPanel.Font.Color := clBlack;
AMenuEntry.FPanel.Font.Size := 24;
AMenuEntry.FPanel.Font.Style := [fsBold];
if ButtonStyle = etSeperator
then
AMenuEntry.FPanel.BorderWidth := 0
else AMenuEntry.FPanel.BorderWidth := 1;
AMenuEntry.FPanel.Parent := FOwner;
AMenuEntry.FPanel.OnClick := Self.OnClick;
AMenuEntry.FPanel.Tag := AMenuEntry.
Index;
AMenuEntry.FButtonStyle := ButtonStyle;
end;
procedure TMenuManager.ClearButtons;
begin
while Self.FMenuEntries.Count > 0
do begin
Self.FMenuEntries.Del(Self.FMenuEntries.Count -1);
end;
end;
function TMenuManager.Count: Integer;
begin
Result := Self.FMenuEntries.Count;
end;
constructor TMenuManager.Create(AOwner: TWinControl; XPos, YPos: Integer);
begin
inherited Create;
Self.FOwner := AOwner;
Self.FXPos := XPos;
Self.FYPos := YPos;
Self.FTempYPos := Self.FYPos;
Self.FActiveButton := -1;
Self.FMenuEntries := TMenuEntries.Create;
end;
destructor TMenuManager.Destroy;
begin
Self.ClearButtons;
Self.FMenuEntries.Destroy;
inherited Destroy;
end;
function TMenuManager.GetAcctiveButton: Integer;
begin
Result := Self.FActiveButton;
end;
procedure TMenuManager.OnClick(Sender: TObject);
begin
if Assigned(FItemClick)
then begin
if Sender
is TJvPanel
then begin
Self.SetActiveButton(TJvPanel(Sender).Tag);
//Self.FActiveButton := TJvPanel(Sender).Tag;
OnItemClick(Self);
// <-- panel existiert nicht mehr
end;
end;
// hier error!!!!!!!!!!!!!!!!!
end;
procedure TMenuManager.RemoveButton(
Index: Integer);
begin
Self.FMenuEntries.Del(
Index);
end;
procedure TMenuManager.SetActiveButton(
const Value: Integer);
var
AMenuEntry: TMenuEntry;
begin
if Value = -1
then
Exit;
AMenuEntry := Self.FMenuEntries.MenuEntry[Value];
if (AMenuEntry <>
nil)
and (Value <> Self.FActiveButton)
then begin
if AMenuEntry.ButtonStyle = etButton
then begin
AMenuEntry.FPanel.Color := clYellow;
if Self.FActiveButton > -1
then
Self.FMenuEntries.MenuEntry[Self.FActiveButton].FPanel.Color := clGray;
Self.FActiveButton := Value;
end;
end;
end;
{ TMenuEntries }
function TMenuEntries.Add(AOwner: TWinControl): TMenuEntry;
begin
Result :=
inherited Add
as TMenuEntry;
Result.FPanel := TJvPanel.Create(AOwner);
Result.FPanel.Parent := AOwner;
end;
constructor TMenuEntries.Create;
begin
inherited Create(TMenuEntry);
end;
procedure TMenuEntries.Del(
Index: Integer);
begin
Self.GetEntry(
Index).Free;
end;
destructor TMenuEntries.Destroy;
begin
inherited Destroy;
end;
function TMenuEntries.GetEntry(
Index: Integer): TMenuEntry;
begin
Result :=
inherited Items[
Index]
as TMenuEntry;
end;
{ TMenuEntry }
destructor TMenuEntry.Destroy;
begin
Self.FPanel.SetFocus;
Self.FPanel.Free;
inherited Destroy;
end;
end.