1
0
lazarus-tutorials/set_demo/setdemo_main.pas

123 lines
2.3 KiB
ObjectPascal
Raw Permalink Normal View History

2021-10-13 15:50:09 -04:00
unit setdemo_main;
{$mode objfpc}{$H+}
interface
uses
Buttons, Forms, Controls, Graphics, Dialogs, ExtCtrls,
dualdigitsset, SysUtils, Classes, strutils;
type
TButtonArr = array[TDigits] of TSpeedButton;
{ TMainForm }
TMainForm = class(TForm)
editSetA: TLabeledEdit;
editSetZ: TLabeledEdit;
editUnion: TLabeledEdit;
editIntersection: TLabeledEdit;
editSymmetricDiff: TLabeledEdit;
editDiffAZ: TLabeledEdit;
editDiffZA: TLabeledEdit;
pnlA: TPanel;
pnlResultSets: TPanel;
pnlZ: TPanel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
ds: TDualAZSet;
function CreateButtons(setID: Char): TButtonArr;
procedure ButtonClick(Sender: TObject);
procedure UpdateSetExpressions;
public
end;
var
MainForm: TMainForm;
implementation
{$R *.lfm}
{ TMainForm }
procedure TMainForm.FormCreate(Sender: TObject);
begin
ds := TDualAZSet.Create;
CreateButtons('A');
CreateButtons('Z');
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
ds.Free;
end;
function TMainForm.CreateButtons(setID: Char): TButtonArr;
const
spacing = 10;
aLeft = 40;
var
i: integer;
b: TSpeedButton;
begin
for i := Low(TDigits) to High(TDigits) do
begin
b := TSpeedButton.Create(Self);
b.Top := spacing;
b.Left := aLeft + i * (b.Width + spacing);
b.Caption := IntToStr(i);
b.Tag := i;
b.Name := Format('%s%d', [setID, i]);
case setId of
'A': b.Parent := pnlA;
'Z': b.Parent := pnlZ;
end;
b.OnClick := @ButtonClick;
Result[i] := b;
end;
end;
procedure TMainForm.ButtonClick(Sender: TObject);
var b: TSpeedButton;
begin
// We only care about SpeedButtons. Just return for others.
if not (Sender is TSpeedButton) then Exit;
b := TSpeedButton(Sender);
case b.Name[1] of
'A': begin
if (b.Tag in ds.SetA)
then ds.SetA := ds.SetA - [b.Tag]
else ds.SetA := ds.SetA + [b.Tag];
editSetA.Caption := ds.SetAasText;
end;
'Z': begin
if (b.Tag in ds.SetZ)
then ds.SetZ := ds.SetZ - [b.Tag]
else ds.SetZ := ds.SetZ + [b.Tag];
editSetZ.Caption := ds.SetZasText;
end;
end;
UpdateSetExpressions;
end;
procedure TMainForm.UpdateSetExpressions;
begin
editUnion.Caption := ds.UnionAsText;
editSymmetricDiff.Caption := ds.SymDiffAsText;
editIntersection.Caption := ds.IntersectionAsText;
editDiffAZ.Caption := ds.DiffAZasText;
editDiffZA.Caption := ds.DiffZAasText;
end;
end.