unit uOptionsUI;
{$INCLUDE 'FarCtags.inc'}

interface

uses
  SysUtils,
  {$INCLUDE 'DelphiFarUnits.inc'} ,
  uOptions;

type
  TOptionsDialog = class(TOkCancelDialog)
  private
    fOptions: TOptions;
    fLabelCtagsFileName: TFarDialog.TLabel;
    fEditCtagsFileName: TFarDialog.TEdit;
    fCheckFunctionSearchDelay: TFarDialog.TCheckbox;
    fEditFunctionSearchDelay: TFarDialog.TEdit;
    fLabelFunctionSearchDelay: TFarDialog.TLabel;
  protected
    procedure Clear; override;
    procedure CreateUserControls; override;
    procedure SetSize(Width, Height: integer); override;
    procedure ConsoleSizeToDialogSize(var Width, Height: Integer); override;
    procedure CheckFunctionSearchDelayClick(Sender: TFarDialog.TCheckbox; FinalState: TFarDialog.TCheckbox.TCheckState; var AllowChange: boolean);
    procedure EditFunctionSearchDelayChange(Sender: TFarDialog.TCustomEdit; var AcceptChanges: Boolean);
    property LabelCtagsFileName: TFarDialog.TLabel read fLabelCtagsFileName;
    property EditCtagsFileName: TFarDialog.TEdit read fEditCtagsFileName;
    property CheckFunctionSearchDelay: TFarDialog.TCheckbox read fCheckFunctionSearchDelay;
    property EditFunctionSearchDelay: TFarDialog.TEdit read fEditFunctionSearchDelay;
    property LabelFunctionSearchDelay: TFarDialog.TLabel read fLabelFunctionSearchDelay;
  public
    constructor Create;
    destructor Destroy; override;
    function Execute: boolean;
    property Options: TOptions read fOptions write fOptions;
  end;

implementation

uses
  ShellApi, Windows,
  uMessages, uFarCtagsConsts;

{ TOptionsDialog }

procedure TOptionsDialog.CheckFunctionSearchDelayClick(Sender: TFarDialog.TCheckbox; FinalState: TFarDialog.TCheckbox.TCheckState; var AllowChange: boolean);
begin
  EditFunctionSearchDelay.Enabled := FinalState = csChecked;
end;

procedure TOptionsDialog.Clear;
begin
  inherited;
  fLabelCtagsFileName := nil;
  fEditCtagsFileName := nil;
  fCheckFunctionSearchDelay := nil;
  fEditFunctionSearchDelay := nil;
  fLabelFunctionSearchDelay := nil;
end;

constructor TOptionsDialog.Create;
begin
  inherited Create(DIALOG_OPTIONS_GUID);
end;

procedure TOptionsDialog.CreateUserControls;
begin
  inherited;
  fLabelCtagsFileName := AddLabel(0, 0, 0, GetMsg(MCtagsFileName));
  fEditCtagsFileName := AddEdit(0, 0, 0, '');
  fEditCtagsFileName.History := 'CtagsFileName';
  fCheckFunctionSearchDelay := AddCheckbox(0, 0, GetMsg(MFunctionSearchDelayEnabled), False);
  fCheckFunctionSearchDelay.OnClick := CheckFunctionSearchDelayClick;
  fEditFunctionSearchDelay := AddEdit(0, 0, 0, '');
  EditFunctionSearchDelay.OnChange := EditFunctionSearchDelayChange;
  fLabelFunctionSearchDelay := AddLabel(0, 0, 0, GetMsg(MFunctionSearchDelayMilliseconds))
end;

destructor TOptionsDialog.Destroy;
begin
  inherited;
end;

procedure TOptionsDialog.EditFunctionSearchDelayChange(Sender: TFarDialog.TCustomEdit; var AcceptChanges: Boolean);
begin
  ButtonOK.Enabled := StrToIntDef(EditFunctionSearchDelay.Text, -1) >= 0;
end;

function TOptionsDialog.Execute: boolean;
begin
  Result := False;
  EditCtagsFileName.Text := Options.CtagsFileName;
  CheckFunctionSearchDelay.Checked := Options.FunctionSearchDelayEnabled;
  EditFunctionSearchDelay.Text := IntToStr(Options.FunctionSearchDelay);
  if inherited Execute then
  begin
    Options.CtagsFileName := EditCtagsFileName.Text;
    Options.FunctionSearchDelayEnabled := CheckFunctionSearchDelay.Checked;
    Options.FunctionSearchDelay := StrToIntDef(EditFunctionSearchDelay.Text, 0);
    Result := True;
  end;
end;

procedure TOptionsDialog.SetSize(Width, Height: integer);
begin
  inherited;
  LabelCtagsFileName.AlignTo(Frame, 2, 1, -4, 0);
  EditCtagsFileName.AlignTo(LabelCtagsFileName, 0, 1, 0, 0);
  CheckFunctionSearchDelay.AlignTo(EditCtagsFileName, 0, 1, 0, 0);
  EditFunctionSearchDelay.AlignTo(CheckFunctionSearchDelay, 4, 1, 0, 0);
  EditFunctionSearchDelay.Width := 5;
  LabelFunctionSearchDelay.AlignTo(EditFunctionSearchDelay, EditFunctionSearchDelay.Width + 2, 0, 0, 0);
  LabelFunctionSearchDelay.Width := CheckFunctionSearchDelay.Width - LabelFunctionSearchDelay.Left;
end;

procedure TOptionsDialog.ConsoleSizeToDialogSize(var Width, Height: Integer);
begin
  inherited;
  Height := 10;
end;

end.
