unit OptionsForm;

interface

procedure ShowOptionsForm;

implementation

uses
  Windows, SysUtils, Types, Classes, UITypes, Controls, Graphics, StdCtrls,
  Messages, Forms, MainForm, UxForm, UxPanel;

function _scale(X: Integer): Integer;
begin
  Result := MulDiv(X, Screen.PixelsPerInch, 96);
end;

type
  TOptionsForm = class(TUxForm)
    cbCaretAfterEOL: TCheckBox;
    cbLineHighlight: TCheckBox;
    cbShowHidden: TCheckBox;
    cbMathInput: TCheckBox;
    btnClose: TUxButton;
    procedure SaveSettings(Sender: TObject);
  end;

procedure ShowOptionsForm;
resourcestring
  SOptions = 'Options';
var
  frm: TOptionsForm;
  y: Integer;

  procedure AddOption(var ACheckBox: TCheckBox; const AText, AHint: string;
    AChecked: Boolean);
  begin
    ACheckBox := TCheckBox.Create(frm);
    ACheckBox.Parent := frm.ParentWindow;
    ACheckBox.Left := _scale(24);
    ACheckBox.Top := y;
    Inc(y, 2 * ACheckBox.Height);
    ACheckBox.ScaleBy(Screen.PixelsPerInch, 96);
    ACheckBox.Width := frm.ParentWindow.ClientWidth - _scale(24) - _scale(8);
    ACheckBox.Anchors := [akLeft, akTop, akRight];
    ACheckBox.Caption := AText;
    ACheckBox.Hint := AHint;
    ACheckBox.Checked := AChecked;
    ACheckBox.OnClick := frm.SaveSettings;
  end;

begin

  frm := TOptionsForm.CreateNew(AlgosimMainForm);
  try

    frm.Caption := SOptions;

    y := _scale(24);
    AddOption(
      frm.cbCaretAfterEOL,
      '&Caret beyond EOL',
      'If checked, the caret can be positioned beyond the end of each line.',
      AlgosimMainForm.teConsole.CaretAfterEOL
    );
    AddOption(
      frm.cbLineHighlight,
      '&Line highlight',
      'If checked, the line with the caret will be highlighted.',
      AlgosimMainForm.teConsole.LineHighlight
    );
    AddOption(
      frm.cbShowHidden,
      'S&how hidden characters',
      'If checked, hidden characters will be displayed.',
      AlgosimMainForm.teConsole.ShowHiddenCharacters
    );
    AddOption(
      frm.cbMathInput,
      '&Math input mode',
      'If checked, the keyboard will insert mathematical symbols automatically.',
      AlgosimMainForm.MathInputMode
    );

    frm.btnClose := TUxButton.Create(frm);
    frm.btnClose.Parent := frm.ParentWindow;
    frm.btnClose.Height := _scale(27);
    frm.btnClose.Width := _scale(75);
    frm.btnClose.Caption := 'Close';
    frm.btnClose.Default := True;
    frm.btnClose.Cancel := True;
    frm.btnClose.ModalResult := mrClose;
    frm.btnClose.Top := frm.ParentWindow.ClientHeight - frm.btnClose.Height - _scale(8);
    frm.btnClose.Left := frm.ParentWindow.ClientWidth - frm.btnClose.Width - _scale(8);
    frm.btnClose.Anchors := [akRight, akBottom];
    Inc(y, frm.btnClose.Height);

    frm.SetClientSize(TSize.Create(_scale(300), y + _scale(24)));
    frm.Constraints.MinHeight := frm.Height;

    frm.ShowModal;

  finally
    frm.Free;
  end;

end;

{ TOptionsForm }

procedure TOptionsForm.SaveSettings(Sender: TObject);
begin
  AlgosimMainForm.teConsole.CaretAfterEOL := cbCaretAfterEOL.Checked;
  AlgosimMainForm.teConsole.LineHighlight := cbLineHighlight.Checked;
  AlgosimMainForm.teConsole.ShowHiddenCharacters := cbShowHidden.Checked;
  AlgosimMainForm.MathInputMode := cbMathInput.Checked;
  AlgosimMainForm.SaveSettings([csConsoleHiddenChars, csConsoleLineHighlight,
    csConsoleCaretBeyondEOL, csMathIM]);
end;

end.
