unit uNewNameDialog;
{$INCLUDE 'Plugin.inc'}

interface

uses
  Windows, SysUtils, Classes,
  {$INCLUDE 'DelphiFarUnits.inc'} ,
  uPluginConsts, uOptions, uMessages;

type
  TNewNameDialog = class(TOkCancelDialog)
  private
    fOptions: TOptions;
    fSavePatternSettings: boolean;
  private
    fLabelFileNamePattern: TFarDialog.TLabel;
    fEditFileNamePattern: TFarDialog.TEdit;
    fLabelStartNumber: TFarDialog.TLabel;
    fEditStartNumber: TFarDialog.TEdit;
    fSeparator: TFarDialog.TSeparator;
    fCheckSavePatternSettings: TFarDialog.TCheckbox;
    fButtonDeleteSavedPattern: TFarDialog.TButton;
  protected
    procedure CreateUserControls; override;
    procedure CreateButtonControls; override;
    procedure ConsoleSizeToDialogSize(var Width, Height: Integer); override;
    procedure SetSize(Width, Height: integer); override;
    procedure ApplySavedPattern;
    procedure UpdateButtonOK;
    procedure EditFileNamePatternChanged(Sender: TFarDialog.TCustomEdit; var AcceptChanges: Boolean);
    procedure EditStartNumberChanged(Sender: TFarDialog.TCustomEdit; var AcceptChanges: Boolean);
    procedure ButtonDeleteSavedPatternClicked(Sender: TFarDialog.TButton; var StopProcessing: boolean);
    property LabelFileNamePattern: TFarDialog.TLabel read fLabelFileNamePattern;
    property EditFileNamePattern: TFarDialog.TEdit read fEditFileNamePattern;
    property LabelStartNumber: TFarDialog.TLabel read fLabelStartNumber;
    property EditStartNumber: TFarDialog.TEdit read fEditStartNumber;
    property Separator: TFarDialog.TSeparator read fSeparator;
    property CheckSavePatternSettings: TFarDialog.TCheckbox read fCheckSavePatternSettings;
    property ButtonDeleteSavedPattern: TFarDialog.TButton read fButtonDeleteSavedPattern;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Clear; override;
    function Execute: boolean;
    property Options: TOptions read fOptions write fOptions;
    property SavePatternSettings: boolean read fSavePatternSettings write fSavePatternSettings;
  end;

implementation

{ TNewNameDialog }

procedure TNewNameDialog.ApplySavedPattern;
var
  Info: TOptions.TSavedPatternInfo;
begin
  if ButtonDeleteSavedPattern.Enabled then
  begin
    CheckSavePatternSettings.Checked := SavePatternSettings;
    ButtonDeleteSavedPattern.Enabled := False;
  end;
  if Options.SavedPatterns.Find(EditFileNamePattern.Text, Info) then
  begin
    if Info.StartingNumberAsString <> '' then
    begin
      EditStartNumber.Text := Info.StartingNumberAsString;
      EditStartNumber.Unchanged := True;
      CheckSavePatternSettings.Checked := True;
      ButtonDeleteSavedPattern.Enabled := True;
    end;
  end;
end;

procedure TNewNameDialog.ButtonDeleteSavedPatternClicked(Sender: TFarDialog.TButton; var StopProcessing: boolean);
begin
  Options.SavedPatterns.Delete(EditFileNamePattern.Text);
  EditStartNumber.Focused := True;
  CheckSavePatternSettings.Checked := False;
  ButtonDeleteSavedPattern.Enabled := False;
  StopProcessing := True;
end;

procedure TNewNameDialog.Clear;
begin
  inherited;
  fLabelFileNamePattern := nil;
  fEditFileNamePattern := nil;
  fLabelStartNumber := nil;
  fEditStartNumber := nil;
  fSeparator := nil;
  fCheckSavePatternSettings := nil;
  fButtonDeleteSavedPattern := nil;
end;

procedure TNewNameDialog.ConsoleSizeToDialogSize(var Width, Height: Integer);
begin
  inherited;
  //Width := 60;
  Height := 11;
end;

constructor TNewNameDialog.Create;
begin
  inherited Create(DIALOG_MAIN_GUID);
end;

procedure TNewNameDialog.CreateButtonControls;
begin
  inherited;
  fButtonDeleteSavedPattern := AddButton(0, 0, GetMsg(MDeleteSavedPattern));
  fButtonDeleteSavedPattern.NoClose := True;
  fButtonDeleteSavedPattern.OnClick := ButtonDeleteSavedPatternClicked;
end;

procedure TNewNameDialog.CreateUserControls;
begin
  inherited;
  fLabelFileNamePattern := AddLabel(0, 0, 0, GetMsg(MEnterBeginningOfFileName));
  fEditFileNamePattern := AddEdit(0, 0, 0, '');
  fEditFileNamePattern.History := 'FileNamePattern';
  fEditFileNamePattern.OnChange := EditFileNamePatternChanged;
  fLabelStartNumber := AddLabel(0, 0, 0, GetMsg(MStartFromThisNumber));
  fEditStartNumber := AddEdit(0, 0, 0, '');
  fEditStartNumber.OnChange := EditStartNumberChanged;
  fSeparator := AddSeparator(0);
  fCheckSavePatternSettings := AddCheckbox(0, 0, GetMsg(MSavePatternSettings), False);
end;

destructor TNewNameDialog.Destroy;
begin
  inherited;
end;

procedure TNewNameDialog.EditFileNamePatternChanged(Sender: TFarDialog.TCustomEdit; var AcceptChanges: Boolean);
begin
  ApplySavedPattern;
  UpdateButtonOK;
end;

procedure TNewNameDialog.EditStartNumberChanged(Sender: TFarDialog.TCustomEdit; var AcceptChanges: Boolean);
begin
  UpdateButtonOK;
end;

function TNewNameDialog.Execute: boolean;
begin
  Result := False;
  EditFileNamePattern.Text := Options.FileNamePattern;
  EditStartNumber.Text := Options.StartingNumberAsString;
  CheckSavePatternSettings.Checked := SavePatternSettings;
  ApplySavedPattern;
  UpdateButtonOK;
  if inherited Execute then
  begin
    Options.FileNamePattern := EditFileNamePattern.Text;
    Options.StartingNumberAsString := EditStartNumber.Text;
    SavePatternSettings := CheckSavePatternSettings.Checked;
    Result := True;
  end;
end;

procedure TNewNameDialog.SetSize(Width, Height: integer);
begin
  inherited;
  AutoPos(LabelFileNamePattern);
  AutoPos(EditFileNamePattern);
  AutoPos(EditStartNumber);
  EditStartNumber.Width := 10;
  AutoPosRight(LabelStartNumber, EditStartNumber);
  AutoPos(Separator);
  AutoPos(CheckSavePatternSettings);
  ButtonDeleteSavedPattern.Top := ButtonOK.Top;
end;

procedure TNewNameDialog.UpdateButtonOK;
var
  n: integer;
begin
  ButtonOK.Enabled := True
    and (EditFileNamePattern.Text <> '')
    and TryStrToInt(EditStartNumber.Text, n)
    and (n >= 0);
    ;
end;

end.
