unit TDSampleWin;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, TDMB, Vcl.StdCtrls;

type
  TMainForm = class(TForm)
    btnSimple: TButton;
    btnMoreText: TButton;
    btnEvenMoreText: TButton;
    btnCaption: TButton;
    btnOKCan: TButton;
    btnYesNo: TButton;
    btnYesNoCancel: TButton;
    btnWarning: TButton;
    btnError: TButton;
    btnInfo: TButton;
    btnCustomIcon: TButton;
    btnShield: TButton;
    btnCustomButtons: TButton;
    btnTaskButtons: TButton;
    btnButtonSample1: TButton;
    btnButtonSample2: TButton;
    btnFooter: TButton;
    btnDetails: TButton;
    btnRadios: TButton;
    btnButtonSample3: TButton;
    btnHtml: TButton;
    btnHtml2: TButton;
    btnVerification: TButton;
    procedure btnSimpleClick(Sender: TObject);
    procedure btnMoreTextClick(Sender: TObject);
    procedure btnEvenMoreTextClick(Sender: TObject);
    procedure btnCaptionClick(Sender: TObject);
    procedure btnOKCanClick(Sender: TObject);
    procedure btnYesNoClick(Sender: TObject);
    procedure btnYesNoCancelClick(Sender: TObject);
    procedure btnWarningClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnErrorClick(Sender: TObject);
    procedure btnInfoClick(Sender: TObject);
    procedure btnCustomIconClick(Sender: TObject);
    procedure btnShieldClick(Sender: TObject);
    procedure btnCustomButtonsClick(Sender: TObject);
    procedure btnTaskButtonsClick(Sender: TObject);
    procedure btnButtonSample1Click(Sender: TObject);
    procedure btnButtonSample2Click(Sender: TObject);
    procedure btnFooterClick(Sender: TObject);
    procedure btnDetailsClick(Sender: TObject);
    procedure btnRadiosClick(Sender: TObject);
    procedure btnButtonSample3Click(Sender: TObject);
    procedure btnHtmlClick(Sender: TObject);
    procedure btnHtml2Click(Sender: TObject);
    procedure btnVerificationClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

uses
  ShellAPI;

{$R *.dfm}

procedure TMainForm.btnButtonSample1Click(Sender: TObject);
begin
  TD
    .Text('Do you want to save the changes made to "New file.txt"?')
    .AddButton('Save', mrYes)
    .AddButton('Don''t save', mrNo)
    .AddButton('Cancel', mrCancel)
    .Execute(Self);
end;

procedure TMainForm.btnButtonSample2Click(Sender: TObject);
begin
  if TD
    .Text('Do you want to close all files without saving them?')
    .Text('This operation cannot be undone.')
    .Warning
    .YesNo
    .DefButton(tcbNo)
    .Execute(Self) = mrYes
  then
    TD('All lost.').Execute(Self);
end;

procedure TMainForm.btnButtonSample3Click(Sender: TObject);
begin
  if TD
    .Text('Do you want to close all files without saving them?')
    .Text('This operation cannot be undone.')
    .Warning
    .AddButton('Yes, close without saving', mrYes)
    .AddButton('No, cancel', mrCancel, True)
    .Execute(Self) = mrYes
  then
    TD('All lost.').Execute(Self);
end;

procedure TMainForm.btnCaptionClick(Sender: TObject);
begin
  TD
    .WindowCaption('Frog Simulator')
    .Text('This is a frog simulator.')
    .Text('I just wanted to tell you that.')
    .Execute(Self);
end;

procedure TMainForm.btnCustomButtonsClick(Sender: TObject);
begin
  TD('Where do you want to go today?')
    .Text('We offer many interesting locations.')
    .AddButton('Redmond', 1)
    .AddButton('London', 2)
    .AddButton('New York City', 3)
    .Execute(Self);
end;

procedure TMainForm.btnCustomIconClick(Sender: TObject);
begin
  TD('This is using the app icon.').CustomIcon(Application.Icon).Execute(Self);
end;

procedure TMainForm.btnDetailsClick(Sender: TObject);
begin
  TD('Where do you want to go today?')
    .Text('We offer many interesting locations.')
    .AddButton('Redmond', 1)
    .AddButton('London', 2)
    .AddButton('New York City', 3)
    .AddFlag(tfUseCommandLinksNoIcon)
    .Details('You have to pay for yourself.', dpFooter, 'Terms and Conditions')
    .Execute(Self);
end;

procedure TMainForm.btnErrorClick(Sender: TObject);
begin
  TD('Couldn''t write file to disk.').Error.Execute(Self);
end;

procedure TMainForm.btnEvenMoreTextClick(Sender: TObject);
begin
  TD('This is a very informative dialog.')
    .Text('You can write a lot of stuff here, if you want to.')
    .Text('But you don''t have to. It''s optional.')
    .Execute(Self);
end;

procedure TMainForm.btnFooterClick(Sender: TObject);
begin
  TD('Where do you want to go today?')
    .Text('We offer many interesting locations.')
    .AddButton('Redmond', 1)
    .AddButton('London', 2)
    .AddButton('New York City', 3)
    .AddFlag(tfUseCommandLinksNoIcon)
    .Footer('You have to pay for yourself.', tdiWarning)
    .Execute(Self);
end;

procedure TMainForm.btnHtml2Click(Sender: TObject);
begin
  TD('Where do you want to go today?')
    .Text('We offer many interesting locations.')
    .AddButton('Redmond', 1)
    .AddButton('London', 2)
    .AddButton('New York City', 3)
    .AddFlag(tfUseCommandLinksNoIcon)
    .Footer('You have to pay for yourself. Please read our <a href="http://contoso.com">terms and conditions</a>.', tdiInformation)
    .Hypertext(
      procedure(const AURL: string)
      begin
        TD('There are our terms and conditions:').Text('Blah, blah, blah.').Execute{todo: add handle}
      end
    )
    .Execute(Self);
end;

procedure TMainForm.btnHtmlClick(Sender: TObject);
begin
  TD('Where do you want to go today?')
    .Text('We offer many interesting locations.')
    .AddButton('Redmond', 1)
    .AddButton('London', 2)
    .AddButton('New York City', 3)
    .AddFlag(tfUseCommandLinksNoIcon)
    .Footer('You have to pay for yourself. Please read our <a href="http://contoso.com">terms and conditions</a>.', tdiInformation)
    .Hypertext
    .Execute(Self);
end;

procedure TMainForm.btnInfoClick(Sender: TObject);
begin
  TD('This is a very interesting piece of information.').Info.Execute(Self);
end;

procedure TMainForm.btnMoreTextClick(Sender: TObject);
begin
  TD('This is a very informative dialog.')
    .Text('You can write a lot of stuff here, if you want to.')
    .Execute(Self);
end;

procedure TMainForm.btnOKCanClick(Sender: TObject);
begin
  var res := TD('All data on the C: drive will be lost.')
    .Warning
    .OKCancel
    .Execute(Self) = mrOk;
  if res then
    TD('Disk formatted.').Execute(Self)
end;

procedure TMainForm.btnRadiosClick(Sender: TObject);
var
  R: Integer;
begin
  if
    TD('Where do you want to go today?')
    .Text('We offer many interesting locations.')
    .AddRadioButton('Redmond')        //
    .AddRadioButton('London')         //
    .AddRadioButton('New York City')  // or .AddRadioButtons
    .DefRadioButton(2)
    .Execute(R, Self) = mrOk
  then
    TD.TextFmt('Radio: %d', [R]).Execute;
end;

procedure TMainForm.btnShieldClick(Sender: TObject);
begin
  TD('This action requires elevated privileges.')
    .Text('Only an administrator can perform this operation.')
    .Shield
    .OKCancel
    .Execute;
end;

procedure TMainForm.btnSimpleClick(Sender: TObject);
begin
  TD('Hello, World?').Execute;
end;

procedure TMainForm.btnTaskButtonsClick(Sender: TObject);
begin
  TD('Where do you want to go today?')
    .Text('We offer many interesting locations.')
    .AddButton('Redmond', 1)
    .AddButton('London', 2)
    .AddButton('New York City', 3)
    .AddFlag(tfUseCommandLinks)
    .Execute(Self);
end;

procedure TMainForm.btnVerificationClick(Sender: TObject);
begin
  var b := False;
  if TD('Which OS do you want to use?')
    .AddRadioButtons(['&Windows', '&macOS', '&Linux'])
    .Verification('&Don''t ask again', @b)
    .OKCancel
    .Execute = mrOK
  then
    if b then
      // Save to registry
end;

procedure TMainForm.btnWarningClick(Sender: TObject);
begin
  TD('System battery critically low. Save all your work now.')
    .Warning
    .Execute(Self)
end;

procedure TMainForm.btnYesNoCancelClick(Sender: TObject);
begin
  var res := TD('Do you want to save the changes made to the file?')
    .YesNoCancel
    .Execute(Self);
  case res of
    mrYes:
      { Save };
    mrNo:
      { Do nothing };
    mrCancel:
      { Abort app exit };
  end;
end;

procedure TMainForm.btnYesNoClick(Sender: TObject);
begin
  var res := TD('Do you want to delete all data on the C: drive?')
    .Warning
    .YesNo
    .Execute(Self) = mrYes;
  if res then
    TD('Disk formatted.').Execute(Self)
end;

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  var b := False;
  CanClose := TD
    .Text('Are you sure you want to close this amazing demo app?')
    .Text('Before you close it, you should make sure you have tried all the buttons.')
    .Text('You should try each button at least once.')
    .CustomIcon(Application.Icon)
    .Footer('Copyright © 2021 <a href="https://rejbrand.se/">Andreas Rejbrand</a>')
    .AddButton('&Yes, close it', mrYes)
    .AddButton('&No, keep it open', mrNo, True)
    .Verification('&Go to rejbrand.se when this app is closed.', @b)
    .Hypertext
    .Execute(Self) = mrYes;
  if CanClose and b then
    ShellExecute(Handle, 'open', 'https://rejbrand.se/', nil, nil, SW_SHOWNORMAL);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  Application.Title := Caption;
end;

end.
