unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Menus,
Buttons;
type
TItem = record
ID: Integer;
Unused: Boolean;
Box: TEdit;
end;
{ TForm1 }
TForm1 = class(TForm)
btnGenerate: TButton;
btnSelect: TButton;
btnRemove: TButton;
lblInfo: TLabel;
PopupMenu1: TPopupMenu;
procedure btnGenerateClick(Sender: TObject);
procedure btnSelectClick(Sender: TObject);
procedure btnRemoveClick(Sender: TObject);
procedure BoxClick(Sender: TObject);
procedure BoxEnter(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure MenuItemClick(Sender: TObject);
private
MenuResult: Integer;
Items: array of TItem;
procedure AddItem;
procedure SelectItem(ID: Integer);
procedure SelectItem(Box: TEdit);
procedure RemoveSelected;
end;
const
BoxSize = 30;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.btnGenerateClick(Sender: TObject);
var
MenuItem: TMenuItem;
i: Integer;
begin
if Length(Items) > 20 then
begin
ShowMessage('You have reached the maximum item count.');
Exit;
end;
PopupMenu1.Items.Clear;
for i := 0 to 2 do
begin
MenuItem := TMenuItem.Create(PopupMenu1);
case i of
0: MenuItem.Caption := '1 item';
1: MenuItem.Caption := '3 items';
2: MenuItem.Caption := 'Cancel';
end;
MenuItem.Tag := i;
MenuItem.OnClick := @MenuItemClick;
PopupMenu1.Items.Add(MenuItem);
end;
MenuResult := 0;
PopupMenu1.PopUp(Mouse.CursorPos.X, Mouse.CursorPos.Y);
case MenuResult of
0: AddItem;
1: for i := 0 to 2 do AddItem;
end;
if Length(Items) > 0 then
begin
Caption := 'Now select some items';
lblInfo.Visible := True;
btnSelect.Enabled := True;
end;
end;
procedure TForm1.btnSelectClick(Sender: TObject);
var
MenuItem: TMenuItem;
ID, i : Integer;
begin
if Length(Items) <= 0 then
begin
ShowMessage('Please click the Generate button.');
Exit;
end;
PopupMenu1.Items.Clear;
for i := 0 to Length(Items)-1 do
if not(Items[i].Unused) then
begin
ID := Items[i].ID;
MenuItem := TMenuItem.Create(PopupMenu1);
MenuItem.Caption := 'Item no. ' + ID.ToString;
MenuItem.Tag := ID;
MenuItem.OnClick := @MenuItemClick;
PopupMenu1.Items.Add(MenuItem);
end;
MenuResult := 0;
PopupMenu1.PopUp(Mouse.CursorPos.X, Mouse.CursorPos.Y);
SelectItem(MenuResult);
end;
procedure TForm1.btnRemoveClick(Sender: TObject);
begin
RemoveSelected;
if Length(Items) <= 0 then
begin
Caption := 'Please generate some boxes';
lblInfo.Visible := False;
btnSelect.Enabled := False;
end;
end;
procedure TForm1.BoxClick(Sender: TObject);
begin
if not(Sender is TEdit) then Exit;
SelectItem(Sender as TEdit);
end;
procedure TForm1.BoxEnter(Sender: TObject);
begin
SelectNext(Sender as TEdit, True, True); // Remove focus
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Caption := 'Please generate some boxes';
Constraints.MinWidth := 320;
Constraints.MinHeight := 240;
lblInfo.Caption := 'You can select an item by clicking on it.';
lblInfo.Visible := False;
btnSelect.Enabled := False;
btnRemove.Enabled := False;
end;
procedure TForm1.MenuItemClick(Sender: TObject);
begin
if not(Sender is TMenuItem) then Exit;
MenuResult := (Sender as TMenuItem).Tag;
end;
procedure TForm1.AddItem;
var
NewBox: TEdit;
X, Y, ID: Integer;
i: Integer;
Found: Boolean;
begin
// Prevent overlap
repeat
X := Random(Width - BoxSize);
Y := Random(Height - BoxSize -60);
Found := False;
for i := 0 to Length(Items)-1 do
if Abs(X-Items[i].Box.Left) < BoxSize then
if Abs(Y-Items[i].Box.Top) < BoxSize then
begin
Found := True;
Break;
end;
until not(Found);
// Get an ID
ID := 0;
repeat
Found := False;
for i := 0 to Length(Items)-1 do
if Items[i].ID = ID then
begin
Found := True;
Inc(ID);
Break;
end;
until not(Found);
NewBox := TEdit.Create(Self);
NewBox.OnClick := @BoxClick;
NewBox.OnEnter := @BoxEnter;
NewBox.ReadOnly := True;
NewBox.TabStop := False;
NewBox.Caption := ID.ToString;
NewBox.Color := Random($FFFF);
NewBox.Left := X;
NewBox.Top := Y;
NewBox.Width := BoxSize;
NewBox.Height := BoxSize;
NewBox.Parent := Self;
i := Length(Items);
SetLength(Items, i+1);
Items[i].ID := ID;
Items[i].Box := NewBox;
Items[i].Unused := False;
end;
procedure TForm1.SelectItem(ID: Integer);
var
i: Integer;
begin
for i := 0 to Length(Items)-1 do
if Items[i].ID = ID then
begin
Items[i].Box.Color := clWhite;
Items[i].Unused := True;
end;
btnRemove.Enabled := True;
end;
procedure TForm1.SelectItem(Box: TEdit);
var
i: Integer;
begin
for i := 0 to Length(Items)-1 do
if Items[i].Box = Box then
begin
Items[i].Box.Color := clWhite;
Items[i].Unused := True;
end;
btnRemove.Enabled := True;
end;
procedure TForm1.RemoveSelected;
var
RemovedCount: Integer;
i: Integer;
begin
RemovedCount := 0;
for i := 0 to Length(Items)-1 do
begin
if Items[i].Unused then
begin
Items[i].Box.Free;
Inc(RemovedCount);
Continue;
end;
if RemovedCount > 0 then
Items[i-RemovedCount] := Items[i];
end;
SetLength(Items, Length(Items)-RemovedCount);
btnRemove.Enabled := False;
end;
end.