unit MainForm;
interface
uses
  SysUtils, Types, UITypes, Classes, Variants, FMX_Types, FMX_Controls, FMX_Forms,
  FMX_Dialogs, FMX_Edit, FMX_Layouts, FMX_ListBox, FMX_Memo, FMX_Ani,
  FMX_Filter_Effects, FMX_Effects, FMX_ExtCtrls;

type
  TfrmMain = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    Edit1: TEdit;
    LogBox: TListBox;
    Button2: TButton;
    Lang1: TLang;
    FloatAnimation1: TFloatAnimation;
    Button3: TButton;
    StatusBar1: TStatusBar;
    ToolBar1: TToolBar;
    ColorAnimation1: TColorAnimation;
    GlowEffect1: TGlowEffect;
    BoxBlurEffect1: TBoxBlurEffect;
    RippleEffect1: TRippleEffect;
    btnLogin: TCornerButton;
    btnAbout: TCornerButton;
    Button4: TButton;
    StyleBook1: TStyleBook;
    procedure Button1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure FormDeactivate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure MyMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Single);
    procedure MyMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Single);
    procedure MyClick(Sender: TObject);
    procedure MyKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char;
      Shift: TShiftState);
    procedure MyKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char;
      Shift: TShiftState);
    procedure MyEnter(Sender: TObject);
    procedure MyExit(Sender: TObject);
    procedure MyDoubleClick(Sender: TObject);
    procedure MyCanFocus(Sender: TObject; var ACanFocus: Boolean);
    procedure MyPaint(Sender: TObject; Canvas: TCanvas; const ARect: TRectF);
    procedure MyPainting(Sender: TObject; Canvas: TCanvas; const ARect: TRectF);
    procedure MyResize(Sender: TObject);
    procedure MyChange(Sender: TObject);
    procedure MyTyping(Sender: TObject);
    procedure MyChangeTracking(Sender: TObject);
    procedure MyChangeCheck(Sender: TObject);
    procedure MyCompare(Item1, Item2: TListBoxItem; var Result: Integer);
    procedure Timer1Timer(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure btnAboutClick(Sender: TObject);
    procedure btnLoginClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
    Path: String;
    LogFile: Text;
    const
      LogFileName = 'logfile.txt';

    procedure Log(const Trace: String; Clear: Boolean = False);
  public
    { Public declarations }
    SecondForm: TForm;
  end;

var
  frmMain: TfrmMain;

implementation
uses
  ChildForm, GridsForm, ColorsForm, AdditionalForm, RealWorldForm, ShapesForm,
  DemoFrame, iOSForm, SQLiteForm, StrUtils,
{$IFDEF Win32}
  CodeSiteLogging;
{$ELSE}
  FMX_Platform_iOS;
{$ENDIF}

{$R *.lfm}

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  GlobalDisableFocusEffect := False;
  Lang1.Lang := 'NL';
  Path := ExtractFilePath(ParamStr(0));
  Delete(Path,Length(Path),1); // remove trailing slash
  Path := ExtractFilePath(Path) + 'Documents';
  {$IFDEF WIN32}
  ForceDirectories(Path);
  CodeSite.Send('ForceDirectories: ' + Path);
  {$ENDIF}
  Path := Path + PathDelim;
  Log('OnCreate', True); // create new logfile
  Log(Path);
end;

procedure TfrmMain.Log(const Trace: String; Clear: Boolean = False);
begin
  if Clear then
    Clear := not DeleteFile(Path + LogFileName); // still Clear if failed...

  AssignFile(LogFile, Path + LogFileName);
  if FileExists(Path + LogFileName) and not Clear then Append(LogFile)
  else
    Rewrite(LogFile);

  writeln(LogFile, DateTimeToStr(Now) + ': ' + Trace);
  {$IFDEF WIN32}
  CodeSite.Send(Trace);
  {$ENDIF}
  if not (csDestroying in ComponentState) then // otherwise, keep on deactivating...
    LogBox.Items.Add(Trace);
  CloseFile(LogFile);
end;

procedure TfrmMain.btnLoginClick(Sender: TObject);
begin
  SecondForm := TfrmRealWorld.Create(Owner);
  try
    SecondForm.ShowModal
  finally
    FreeAndNil(SecondForm)
  end;
end;

procedure TfrmMain.Button1Click(Sender: TObject);
begin
  Log(Sender.ClassName + '.OnClick');
  ListBox1.Items.Add(Edit1.Text);
end;

procedure TfrmMain.Button2Click(Sender: TObject);
begin
  Log(Sender.ClassName + '.OnClick');
  LogBox.Items.LoadFromFile(Path + LogFileName);
end;

procedure TfrmMain.Button3Click(Sender: TObject);
begin
  SecondForm := TFrmSQLite.Create(Owner);
  try
    SecondForm.ShowModal
  finally
    SecondForm.Free;
    SecondForm := nil
  end;
end;

procedure TfrmMain.Button4Click(Sender: TObject);
begin
  Edit1.Text := ReverseString(Edit1.Text);
  {$IFDEF WIN32}
  Application.ProcessMessages;
  {$ENDIF}
  Sleep(2000);
  Edit1.Text := ReverseString(Edit1.Text);
end;

procedure TfrmMain.btnAboutClick(Sender: TObject);
begin
  ShowMessage('About iOSdemo v0.1');
end;

procedure TfrmMain.FormActivate(Sender: TObject);
begin
  Log('OnActivate');
end;

procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Log('OnClose');
end;

procedure TfrmMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  Log('OnCloseQuery');
end;

procedure TfrmMain.FormDeactivate(Sender: TObject);
begin
  Log('OnDeactivate');
end;

procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  Log('OnDestroy');
end;

procedure TfrmMain.FormResize(Sender: TObject);
begin
  Log(Format('OnResize Left=%d Top=%d Width=%d Height=%d',[Left,Top,ClientWidth,ClientHeight]));
  if Assigned(SecondForm) then
  begin
    SecondForm.Width := Width;
    SecondForm.Height := Height;
  end
end;

// Shared Log Events

procedure TfrmMain.MyCanFocus(Sender: TObject; var ACanFocus: Boolean);
begin
  Log(Sender.ClassName + '.OnCanFocus');
end;

procedure TfrmMain.MyChange(Sender: TObject);
begin
  Log(Sender.ClassName + '.OnChange');
end;

procedure TfrmMain.MyChangeCheck(Sender: TObject);
begin
  Log(Sender.ClassName + '.OnChangeCheck');
end;

procedure TfrmMain.MyChangeTracking(Sender: TObject);
begin
  Log(Sender.ClassName + '.OnChangeTracking');
end;

procedure TfrmMain.MyClick(Sender: TObject);
begin
  Log(Sender.ClassName + '.OnClick');
end;

procedure TfrmMain.MyCompare(Item1, Item2: TListBoxItem; var Result: Integer);
begin
  if Item1.Text > Item2.Text then Result := 1
  else
    if Item1.Text < Item2.Text then Result := -1
    else
      Result := 0;
  Log('OnMyCompare ' + IntToStr(Result));
end;

procedure TfrmMain.MyDoubleClick(Sender: TObject);
begin
  Log(Sender.ClassName + '.OnDoubleClick');
end;

procedure TfrmMain.MyEnter(Sender: TObject);
begin
  Log(Sender.ClassName + '.OnEnter');
end;

procedure TfrmMain.MyExit(Sender: TObject);
begin
  Log(Sender.ClassName + '.OnExit');
end;

procedure TfrmMain.MyKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char;
  Shift: TShiftState);
begin
  Log(Sender.ClassName + '.OnKeyDown ' + IntToStr(Key) + #32 + KeyChar);
end;

procedure TfrmMain.MyKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char;
  Shift: TShiftState);
begin
  Log(Sender.ClassName + '.OnKeyUp ' + IntToStr(Key) + #32 + KeyChar);
end;

procedure TfrmMain.MyMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Single);
begin
  Log(Sender.ClassName + '.OnMouseDown');
end;

procedure TfrmMain.MyMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Single);
begin
  Log(Sender.ClassName + '.OnMouseUp');
end;

procedure TfrmMain.MyPaint(Sender: TObject; Canvas: TCanvas;
  const ARect: TRectF);
begin
//Log(Sender.ClassName + '.OnPaint');
end;

procedure TfrmMain.MyPainting(Sender: TObject; Canvas: TCanvas;
  const ARect: TRectF);
begin
//Log(Sender.ClassName + '.OnPainting');
end;

procedure TfrmMain.MyResize(Sender: TObject);
begin
  Log(Sender.ClassName + '.OnResize');
end;

procedure TfrmMain.MyTyping(Sender: TObject);
begin
  Log(Sender.ClassName + '.OnTyping');
end;

procedure TfrmMain.Timer1Timer(Sender: TObject);
begin
  LogBox.Items.Add(DateTimeToStr(Now))
end;

initialization
{$IFDEF WIN32}
  CodeSite.Clear
{$ENDIF}
end.
