{*************************************************************************}
{ TMS Replace Dialog component                                            }
{ for Delphi & C++Builder                                                 }
{                                                                         }
{ written by TMS Software                                                 }
{           copyright © 2013                                              }
{           Email : info@tmssoftware.com                                  }
{           Web : http://www.tmssoftware.com                              }
{                                                                         }
{ The source code is given as is. The author is not responsible           }
{ for any possible damage done due to the use of this code.               }
{ The component can be freely used in any application. The complete       }
{ source code remains property of the author and may not be distributed,  }
{ published, given or sold in any form as such. No parts of the source    }
{ code can be included in any other component or application without      }
{ written authorization of the author.                                    }
{*************************************************************************}

unit FMX.TMSReplaceDialog;

interface

{$I TMSDEFS.INC}

uses
  Classes, FMX.Forms, FMX.Memo, FMX.Menus, FMX.Edit, FMX.Controls, FMX.Types,
  FMX.Layouts, FMX.ListBox, UITypes, FMX.TMSBaseControl, FMX.StdCtrls;

type
  TTMSFMXReplaceDialogOption = (fdoCurrentFile, fdoSelection, fdoAllOpenFiles, fdoCaseSensitive, fdoExpression,
    fdoWholeWordOnly, fdoWrapAtEndOfFile, fdoMoreEnabled,
    fdoFindEnabled, fdoReplaceAllEnabled, fdoReplaceEnabled,
    fdoCloseEnabled, fdoFindList, fdoMoreExpanded, fdoReplaceList, fdoDown);

  TAdvFindDialogVisibleOption = (fdovCaseSensitive, fdovExpression,
    fdovWholeWordOnly, fdovWrapAtEndOfFile, fdovMore, fdovReplace, fdovReplaceAll, fdovFind,
    fdovClose, fdovFindCombo, fdovFindMemo, fdovReplaceCombo, fdovReplaceMemo, fdovReplaceAllRange, fdovCurrentFile, fdovSelection,
    fdovAllOpenFiles, fdovDirection);


  TTMSFMXReplaceDialogOptions = set of TTMSFMXReplaceDialogOption;
  TTMSFMXReplaceDialogVisibleOptions = set of TAdvFindDialogVisibleOption;

const
  DefaultReplaceOptions = [fdoMoreEnabled, fdoReplaceAllEnabled, fdoFindEnabled, fdoReplaceEnabled, fdoCloseEnabled, fdoDown];
  DefaultVisibleReplaceOptions = [fdovCaseSensitive, fdovExpression,
    fdovWholeWordOnly, fdovWrapAtEndOfFile, fdovMore, fdovReplace, fdovReplaceAll, fdovFind,
    fdovClose, fdovFindCombo, fdovFindMemo, fdovReplaceCombo, fdovReplaceMemo, fdovReplaceAllRange, fdovCurrentFile, fdovSelection,
    fdovDirection];

type

  TTMSFMXReplaceDialog = class;

  TEditChangeEvent = procedure(Sender: TObject; var AText: string) of object;


  TTMSFMXReplaceDialogForm = class(TForm)
    RangeGroup: TGroupBox;
    DirGroup: TGroupBox;
    Label1: TLabel;
    ComboBox1: TComboEdit;
    SpeedButton1: TSpeedButton;
    PopupMenu1: TPopupMenu;
    abCharacter1: TMenuItem;
    NewLine1: TMenuItem;
    AnyCharacter1: TMenuItem;
    CharacterinRange1: TMenuItem;
    CharacternotinRange1: TMenuItem;
    BeginningofLine1: TMenuItem;
    EndofLine1: TMenuItem;
    aggedExpression1: TMenuItem;
    Or1: TMenuItem;
    N0or1matches1: TMenuItem;
    N1orMoreMatches1: TMenuItem;
    N0or1Matches2: TMenuItem;
    N1: TMenuItem;
    FindBtn: TButton;
    ReplaceBtn: TButton;
    ReplaceAllBtn: TButton;
    CloseBtn: TButton;
    MoreBtn: TButton;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    Label2: TLabel;
    Label3: TLabel;
    Memo1: TMemo;
    Memo2: TMemo;
    ComboBox2: TComboEdit;
    Label4: TLabel;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    CurrentFile: TRadioButton;
    AllOpen: TRadioButton;
    Selection: TRadioButton;
    procedure abCharacter1Click(Sender: TObject);
    procedure NewLine1Click(Sender: TObject);
    procedure AnyCharacter1Click(Sender: TObject);
    procedure CharacterinRange1Click(Sender: TObject);
    procedure CharacternotinRange1Click(Sender: TObject);
    procedure BeginningofLine1Click(Sender: TObject);
    procedure EndofLine1Click(Sender: TObject);
    procedure aggedExpression1Click(Sender: TObject);
    procedure Or1Click(Sender: TObject);
    procedure N0or1matches1Click(Sender: TObject);
    procedure N1orMoreMatches1Click(Sender: TObject);
    procedure N0or1Matches2Click(Sender: TObject);
    procedure CloseBtnClick(Sender: TObject);
    procedure FindBtnClick(Sender: TObject);
    procedure ReplaceBtnClick(Sender: TObject);
    procedure ReplaceAllBtnClick(Sender: TObject);
    procedure MoreBtnClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure CheckBox4Click(Sender: TObject);
    procedure CheckBox5Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure RangeGroupClick(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure ComboBox2Change(Sender: TObject);
    procedure Memo1Change(Sender: TObject);
    procedure DirGroupClick(Sender: TObject);
  private
    { Private declarations }
    FOnReplace: TNotifyEvent;
    FOnFind: TNotifyEvent;
    FOnReplaceAll: TNotifyEvent;
    FOnClose: TNotifyEvent;
    FDialog: TTMSFMXReplaceDialog;
    FOnFindEditChange: TEditChangeEvent;
    FOnReplaceEditChange: TEditChangeEvent;
    function GetReplaceText: String;
    procedure SetReplaceText(const Value: String);
    function GetFindText: String;
    procedure SetFindText(const Value: String);
  protected
    { Protected declarations }
    procedure DoFindEditChange(var AText: string); virtual;
    procedure DoReplaceEditChange(var AText: string); virtual;
    procedure EnableButtons; virtual;
  public
    { Public declarations }
    property Dialog: TTMSFMXReplaceDialog read FDialog write FDialog;
    property OnReplace: TNotifyEvent read FOnReplace write FOnReplace;
    property OnClose: TNotifyEvent read FOnClose write FOnClose;
    property OnReplaceAll: TNotifyEvent read FOnReplaceAll write FOnReplaceAll;
    property OnFind: TNotifyEvent read FOnFind write FOnFind;
    property OnFindEditChange: TEditChangeEvent read FOnFindEditChange write FOnFindEditChange;
    property OnReplaceEditChange: TEditChangeEvent read FOnReplaceEditChange write FOnReplaceEditChange;
    property ReplaceText: String read GetReplaceText write SetReplaceText;
    property FindText: String read GetFindText write SetFindText;
  end;

  TReplaceUILanguage = class(TPersistent)
  private
    FFindWhat: String;
    FCaseSensitive: String;
    FWrapAtEndOfFile: String;
    FExpression: String;
    FMore: String;
    FWholeWordOnly: String;
    FClose: String;
    FFind: String;
    FCurrentFile: String;
    FReplace: String;
    FSelection: String;
    FReplaceAll: String;
    FAllOpenFiles: String;
    FReplaceWith: String;
    FLess: String;
    FReplaceAllRange: String;
    FDirection: String;
    FDown: String;
    FUp: String;
    FReplaceCaption: string;
  public
    constructor Create(AOwner: TComponent);
    procedure Assign(Source: TPersistent); override;
    procedure Init;
  published
    property ReplaceCaption: string read FReplaceCaption write FReplaceCaption;
    property FindWhat: String read FFindWhat write FFindWhat;
    property ReplaceWith: String read FReplaceWith write FReplaceWith;
    property CaseSensitive: String read FCaseSensitive write FCaseSensitive;
    property Expression: String read FExpression write FExpression;
    property WholeWordOnly: String read FWholeWordOnly write FWholeWordOnly;
    property WrapAtEndOfFile: String read FWrapAtEndOfFile write FWrapAtEndOfFile;
    property Find: String read FFind write FFind;
    property Replace: String read FReplace write FReplace;
    property ReplaceAll: String read FReplaceAll write FReplaceAll;
    property ReplaceAllRange: String read FReplaceAllRange write FReplaceAllRange;
    property Close: String read FClose write FClose;
    property More: String read FMore write FMore;
    property Less: String read FLess write FLess;
    property CurrentFile: String read FCurrentFile write FCurrentFile;
    property Selection: String read FSelection write FSelection;
    property AllOpenFiles: String read FAllOpenFiles write FAllOpenFiles;
    property Direction: String read FDirection write FDirection;
    property Up: String read FUp write FUp;
    property Down: String read FDown write FDown;
  end;

  [ComponentPlatformsAttribute(TMSPlatforms)]
  TTMSFMXReplaceDialog = class(TComponent)
  private
    FReplaceText: String;
    FOnReplace: TNotifyEvent;
    FOnReplacePrevious: TNotifyEvent;
    FOnClose: TNotifyEvent;
    FOptions: TTMSFMXReplaceDialogOptions;
    FReplaceList: TStringList;
    FOnFind: TNotifyEvent;
    FFindText: String;
    FFindList: TStringList;
    FUILanguage: TReplaceUILanguage;
    FVisibleOptions: TTMSFMXReplaceDialogVisibleOptions;
    FOnFindEditChange: TEditChangeEvent;
    FOnReplaceEditChange: TEditChangeEvent;
    FAutoHistory: boolean;
    Frm: TTMSFMXReplaceDialogForm;
    procedure SetUILanguage(const Value: TReplaceUILanguage);
    procedure SetOptions(const Value: TTMSFMXReplaceDialogOptions);
  protected
    procedure DoClose(Sender: TObject);
    procedure DoReplace(Sender: TObject);
    procedure DoReplaceAll(Sender: TObject);
    procedure DoFind(Sender: TObject);
    procedure DoFindEditChange(Sender: TObject; var AText: string);
    procedure DoReplaceEditChange(Sender: TObject; var AText: string);
    function CreateDialogForm(AOwner: TComponent): TTMSFMXReplaceDialogForm; virtual;
    function InitDialog: TCustomForm; virtual;
  public
    procedure Execute; virtual;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Form: TTMSFMXReplaceDialogForm read Frm;
    procedure Close;
  published
    property AutoHistory: boolean read FAutoHistory write FAutoHistory default true;
    property ReplaceText: String read FReplaceText write FReplaceText;
    property FindText: String read FFindText write FFindText;
    property ReplaceList: TStringList read FReplaceList write FReplaceList;
    property FindList: TStringList read FFindList write FFindList;
    property OnFind: TNotifyEvent read FOnFind write FOnFind;
    property OnFindEditChange: TEditChangeEvent read FOnFindEditChange write FOnFindEditChange;
    property OnReplace: TNotifyEvent read FOnReplace write FOnReplace;
    property OnReplaceEditChange: TEditChangeEvent read FOnReplaceEditChange write FOnReplaceEditChange;
    property OnClose: TNotifyEvent read FOnClose write FOnClose;
    property OnReplaceAll: TNotifyEvent read FOnReplacePrevious write FOnReplacePrevious;
    property Options: TTMSFMXReplaceDialogOptions read FOptions write SetOptions default DefaultReplaceOptions;
    property VisibleOptions: TTMSFMXReplaceDialogVisibleOptions read FVisibleOptions write FVisibleOptions default DefaultVisibleReplaceOptions;
    property UILanguage: TReplaceUILanguage read FUILanguage write SetUILanguage;
  end;

var
  TMSFMXReplaceDialogForm: TTMSFMXReplaceDialogForm;
  More: Boolean;
  FPrevCap: String;

{$R *.fmx}

implementation

{ TTMSFMXReplaceDialog }

procedure TTMSFMXReplaceDialog.Close;
begin
  Form.Close;
end;

constructor TTMSFMXReplaceDialog.Create(AOwner: TComponent);
begin
  inherited;
  FUILanguage := TReplaceUILanguage.Create(Self);
  FOptions := DefaultReplaceOptions;
  FVisibleOptions := DefaultVisibleReplaceOptions;
  FReplaceList := TStringList.Create;
  FFindList := TStringList.Create;
  FAutoHistory := true;
end;

destructor TTMSFMXReplaceDialog.Destroy;
begin
  FUILanguage.Free;
  FReplaceList.Free;
  FFindList.Free;
  inherited;
end;

procedure TTMSFMXReplaceDialog.DoClose(Sender: TObject);
begin
  if FAutoHistory then
  begin
    if FFindList.IndexOf(FindText) = -1 then
      FFindList.Add(FindText);

    if FReplaceList.IndexOf(ReplaceText) = -1 then
      FReplaceList.Add(ReplaceText);
  end;

  if Assigned(OnClose) then
    OnClose(Self);
end;

procedure TTMSFMXReplaceDialog.DoReplace(Sender: TObject);
begin
  if Assigned(OnReplace) then
    OnReplace(Self);
end;

procedure TTMSFMXReplaceDialog.DoReplaceAll(Sender: TObject);
begin
  if Assigned(OnReplaceAll) then
    OnReplaceAll(Self);
end;

procedure TTMSFMXReplaceDialog.DoReplaceEditChange(Sender: TObject;
  var AText: string);
begin
  if Assigned(FOnReplaceEditChange) then
    FOnReplaceEditChange(Self, AText);
end;

procedure TTMSFMXReplaceDialog.DoFind(Sender: TObject);
begin
  if Assigned(OnFind) then
    OnFind(Self);
end;

procedure TTMSFMXReplaceDialog.DoFindEditChange(Sender: TObject;
  var AText: string);
begin
  if Assigned(FOnFindEditChange) then
    FOnFindEditChange(Self, AText);
end;

function TTMSFMXReplaceDialog.CreateDialogForm(
  AOwner: TComponent): TTMSFMXReplaceDialogForm;
begin
  Result := TTMSFMXReplaceDialogForm.Create(AOwner);
end;

function TTMSFMXReplaceDialog.InitDialog: TCustomForm;
const
  DirHeight = 50;
begin
  frm := CreateDialogForm(Self);
  frm.Dialog := Self;
  frm.ReplaceText := ReplaceText;
  frm.FindText := FindText;
  frm.OnClose := DoClose;
  frm.OnReplace := DoReplace;
  frm.OnReplaceAll := DoReplaceAll;
  frm.OnFind := DoFind;
  frm.OnFindEditChange := DoFindEditChange;
  frm.OnReplaceEditChange := DoReplaceEditChange;

  frm.CheckBox1.IsChecked := fdoCaseSensitive in Options;
  frm.CheckBox2.IsChecked := fdoExpression in Options;
  frm.CheckBox4.IsChecked := fdoWholeWordOnly in Options;
  frm.CheckBox5.IsChecked := fdoWrapAtEndOfFile in Options;
  frm.FindBtn.Enabled := fdoFindEnabled in Options;
  frm.ReplaceBtn.Enabled := fdoReplaceEnabled in Options;
  frm.ReplaceAllBtn.Enabled := fdoReplaceAllEnabled in Options;
  frm.CloseBtn.Enabled := fdoCloseEnabled in Options;
  frm.MoreBtn.Enabled := fdoMoreEnabled in Options;

  frm.CheckBox1.Visible := fdovCaseSensitive in VisibleOptions;
  frm.CheckBox2.Visible := fdovExpression in VisibleOptions;
  frm.CheckBox4.Visible := fdovWholeWordOnly in VisibleOptions;
  frm.CheckBox5.Visible := fdovWrapAtEndOfFile in VisibleOptions;
  frm.FindBtn.Visible := fdovFind in VisibleOptions;
  frm.ReplaceBtn.Visible := fdovReplace in VisibleOptions;
  frm.ReplaceAllBtn.Visible := fdovReplaceAll in VisibleOptions;
  frm.CloseBtn.Visible := fdovClose in VisibleOptions;
  frm.MoreBtn.Visible := fdovMore in VisibleOptions;

  frm.CheckBox1.Text := UILanguage.CaseSensitive;
  frm.CheckBox2.Text := UILanguage.Expression;
  frm.CheckBox4.Text := UILanguage.WholeWordOnly;
  frm.CheckBox5.Text := UILanguage.WrapAtEndOfFile;
  frm.FindBtn.Text := UILanguage.Find;
  frm.ReplaceBtn.Text := UILanguage.Replace;
  frm.ReplaceAllBtn.Text := UILanguage.ReplaceAll;
  frm.CloseBtn.Text := UILanguage.Close;
  frm.MoreBtn.Text := UILanguage.More;

  (frm.RangeGroup.Children[0] as TRadioButton).OnChange := frm.RangeGroupClick;
  (frm.RangeGroup.Children[1] as TRadioButton).OnChange := frm.RangeGroupClick;
  (frm.RangeGroup.Children[2] as TRadioButton).OnChange := frm.RangeGroupClick;

  frm.AllOpen.Visible := (fdovAllOpenFiles in VisibleOptions);
  frm.Selection.Visible := (fdovSelection in VisibleOptions);
  frm.CurrentFile.Visible := (fdovCurrentFile in VisibleOptions);

  frm.RangeGroup.Visible := (fdovReplaceAllRange in VisibleOptions) and (frm.RangeGroup.ChildrenCount > 0);

  frm.RangeGroup.Text := UILanguage.ReplaceAllRange;

  frm.Selection.Text := UILanguage.Selection;
  frm.AllOpen.Text := UILanguage.AllOpenFiles;
  frm.CurrentFile.Text := UILanguage.CurrentFile;

  frm.AllOpen.Text := UIlanguage.AllOpenFiles;
  frm.CurrentFile.Text := UILanguage.CurrentFile;
  frm.Selection.Text := UILanguage.Selection;

  frm.RangeGroup.Text := UILanguage.ReplaceAllRange;


  if fdoCurrentFile in Options then
    frm.CurrentFile.IsChecked := True;

  if fdoSelection in Options then
    frm.Selection.IsChecked := True;

  if fdoAllOpenFiles in Options then
    frm.AllOpen.IsChecked := True;

  if (fdoDown in Options) then
    frm.RadioButton2.IsChecked := True
  else
    frm.RadioButton3.IsChecked := True;

  frm.Label1.Text := UILanguage.FindWhat;
  frm.Label2.Text := UILanguage.FindWhat;

  frm.Label3.Text := UILanguage.ReplaceWith;
  frm.Label4.Text := UILanguage.ReplaceWith;

  if fdoFindList in Options then
    frm.ComboBox1.Typing := False;

  if fdoReplaceList in Options then
    frm.ComboBox2.Typing := False;

  frm.ComboBox1.Items.Assign(FindList);
  frm.ComboBox2.Items.Assign(ReplaceList);

  frm.ComboBox1.Visible := fdovFindCombo in VisibleOptions;
  frm.ComboBox2.Visible := fdovReplaceCombo in VisibleOptions;
  frm.Memo1.Visible := fdovFindMemo in VisibleOptions;
  frm.Memo2.Visible := fdovReplaceMemo in VisibleOptions;

  frm.DirGroup.Visible := fdovDirection in VisibleOptions;

  (frm.DirGroup.Children[0] as TRadioButton).OnChange := frm.DirGroupClick;
  (frm.DirGroup.Children[1] as TRadioButton).OnChange := frm.DirGroupClick;

  frm.RadioButton2.Text := UILanguage.Down;
  frm.RadioButton3.Text := UILanguage.Up;

  frm.DirGroup.Text := UILanguage.Direction;

  if frm.DirGroup.Visible then
    frm.Height := 235
  else
    frm.Height := 185;

  if not frm.DirGroup.Visible then
  begin
    frm.MoreBtn.Position.Y := frm.MoreBtn.Position.Y - DirHeight;
    frm.CloseBtn.Position.Y := frm.CloseBtn.Position.Y - DirHeight;
    frm.RangeGroup.Position.Y := frm.RangeGroup.Position.Y - DirHeight;
    frm.Memo1.Position.Y := frm.Memo1.Position.Y - DirHeight;
    frm.Memo2.Position.Y := frm.Memo2.Position.Y - DirHeight;
    frm.label2.Position.Y := frm.Label2.Position.Y - DirHeight;
    frm.label4.Position.Y := frm.Label4.Position.Y - DirHeight;
  end;

  frm.Caption := UILanguage.ReplaceCaption;


  if (fdoMoreExpanded in Options) then
  begin
    if not More then
      frm.MoreBtnClick(Self);
  end
  else
  begin
    if More then
      frm.MoreBtnClick(Self);
  end;

  Result := frm;
end;

procedure TTMSFMXReplaceDialog.Execute;
var
  frm: TTMSFMXReplaceDialogForm;
begin
  frm := InitDialog as TTMSFMXReplaceDialogForm;
  frm.EnableButtons;
  frm.ShowModal;
end;

procedure TTMSFMXReplaceDialog.SetOptions(const Value: TTMSFMXReplaceDialogOptions);
var
  iCount: Integer;
  FOldValues: TTMSFMXReplaceDialogOptions;
begin
  FOldValues := FOptions;
  FOptions := Value;
  iCount := 0;
  if (fdoCurrentFile in Options) then
    Inc(iCount);

  if (fdoSelection in Options) then
    Inc(iCount);

  if (fdoAllOpenFiles in Options) then
    Inc(iCount);

  if iCount > 1 then
    FOptions := FOldValues;
end;

procedure TTMSFMXReplaceDialog.SetUILanguage(const Value: TReplaceUILanguage);
begin
  if FUILanguage <> Value then
    FUILanguage.Assign(Value);
end;

{ TReplaceUILanguage }

procedure TReplaceUILanguage.Assign(Source: TPersistent);
begin
  if (Source is TReplaceUILanguage) then
  begin
    FFindWhat :=  (Source as TReplaceUILanguage).FindWhat;
    FReplace  := (Source as TReplaceUILanguage).Replace;
    FReplaceAll := (Source as TReplaceUILanguage).ReplaceAll;
    FReplaceWith  := (Source as TReplaceUILanguage).ReplaceWith;
    FReplaceAllRange := (Source as TReplaceUILanguage).ReplaceAllRange;
    FCaseSensitive := (Source as TReplaceUILanguage).CaseSensitive;
    FExpression := (Source as TReplaceUILanguage).Expression;
    FWholeWordOnly := (Source as TReplaceUILanguage).WholeWordOnly;
    FWrapAtEndOfFile := (Source as TReplaceUILanguage).WrapAtEndOfFile;
    FFind := (Source as TReplaceUILanguage).Find;
    FClose := (Source as TReplaceUILanguage).Close;
    FMore := (Source as TReplaceUILanguage).More;
    FLess := (Source as TReplaceUILanguage).Less;
    FSelection := (Source as TReplaceUILanguage).Selection;
    FCurrentFile := (Source as TReplaceUILanguage).CurrentFile;
    FAllOpenFiles := (Source as TReplaceUILanguage).AllOpenFiles;
    FDirection := (Source as TReplaceUILanguage).Direction;
    FUp := (Source as TReplaceUILanguage).Up;
    FDown := (Source as TReplaceUILanguage).Down;
  end;
end;

constructor TReplaceUILanguage.Create(AOwner: TComponent);
begin
  inherited Create;
  if(csDesigning in AOwner.ComponentState) and not
    ((csReading in AOwner.ComponentState) or (csLoading in AOwner.ComponentState)) then
  begin
    Init;
  end;
end;

procedure TReplaceUILanguage.Init;
begin
  FReplaceCaption := 'Replace';
  FCaseSensitive := 'Case sensitive';
  FExpression := 'Expression';
  FWholeWordOnly := 'Whole word Only';
  FWrapAtEndOfFile := 'Wrap at the end of file';
  FFind := 'Find';
  FFindWhat := 'Find what:';
  FReplaceWith := 'Replace with:';
  FReplaceAll := 'Replace &All';
  FReplace := 'Replace';
  FClose := 'Close';
  FMore := 'More';
  FLess := 'Less';
  FSelection := 'Selection';
  FCurrentFile := 'Current File';
  FAllOpenFiles := 'All open files';
  FReplaceAllRange := 'Replace all range';
  FDirection := 'Direction';
  FUp := 'Up';
  FDown := 'Down';
end;

procedure TTMSFMXReplaceDialogForm.abCharacter1Click(Sender: TObject);
begin
  ComboBox1.Text := ComboBox1.Text + '\t';
end;

procedure TTMSFMXReplaceDialogForm.aggedExpression1Click(Sender: TObject);
begin
  ComboBox1.Text := ComboBox1.Text + '()';
end;

procedure TTMSFMXReplaceDialogForm.AnyCharacter1Click(Sender: TObject);
begin
  ComboBox1.Text := ComboBox1.Text + '.';
end;

procedure TTMSFMXReplaceDialogForm.BeginningofLine1Click(Sender: TObject);
begin
  ComboBox1.Text := ComboBox1.Text + '^';
end;

procedure TTMSFMXReplaceDialogForm.FindBtnClick(Sender: TObject);
begin
  if Assigned(OnFind) then
    OnFind(Self);
end;

procedure TTMSFMXReplaceDialogForm.ReplaceBtnClick(Sender: TObject);
begin
  if Assigned(OnReplace) then
    OnReplace(Self);
end;

procedure TTMSFMXReplaceDialogForm.ReplaceAllBtnClick(Sender: TObject);
begin
  if Assigned(OnReplaceAll) then
    OnReplaceAll(Self);
end;

procedure TTMSFMXReplaceDialogForm.CloseBtnClick(Sender: TObject);
begin
  Close;
end;

procedure TTMSFMXReplaceDialogForm.MoreBtnClick(Sender: TObject);
var
  heightopen, heightclosed: integer;
begin
  if DirGroup.Visible then
  begin
    heightopen := 446;
    heightclosed := 235;
  end
  else
  begin
    heightopen := 446 - 50;
    heightclosed := 235 - 50;
  end;

  if not More then
  begin
    Height := heightopen;
    MoreBtn.Text := Dialog.UILanguage.Less;
    Dialog.Options := Dialog.Options + [fdoMoreExpanded];
  end
  else
  begin
    MoreBtn.Text := Dialog.UILanguage.More;
    Height := heightclosed;
    Dialog.Options := Dialog.Options - [fdoMoreExpanded];
  end;
  Label1.Enabled := More;
  ComboBox1.Enabled := More;
  Label3.Enabled := More;
  ComboBox2.Enabled := More;
  More := not More;
end;

procedure TTMSFMXReplaceDialogForm.CharacterinRange1Click(Sender: TObject);
begin
  ComboBox1.Text := ComboBox1.Text + '[]';
end;

procedure TTMSFMXReplaceDialogForm.CharacternotinRange1Click(Sender: TObject);
begin
  ComboBox1.Text := ComboBox1.Text + '[^]';
end;

procedure TTMSFMXReplaceDialogForm.CheckBox1Click(Sender: TObject);
begin
  if CheckBox1.IsChecked then
    Dialog.Options := Dialog.Options + [fdoCaseSensitive]
  else
    Dialog.Options := Dialog.Options - [fdoCaseSensitive];
end;

procedure TTMSFMXReplaceDialogForm.CheckBox2Click(Sender: TObject);
begin
  if CheckBox2.IsChecked then
    Dialog.Options := Dialog.Options + [fdoExpression]
  else
    Dialog.Options := Dialog.Options - [fdoExpression];
end;

procedure TTMSFMXReplaceDialogForm.CheckBox4Click(Sender: TObject);
begin
  if CheckBox4.IsChecked then
    Dialog.Options := Dialog.Options + [fdoWholeWordOnly]
  else
    Dialog.Options := Dialog.Options - [fdoWholeWordOnly];
end;

procedure TTMSFMXReplaceDialogForm.CheckBox5Click(Sender: TObject);
begin
  if CheckBox5.IsChecked then
    Dialog.Options := Dialog.Options + [fdoWrapAtEndOfFile]
  else
    Dialog.Options := Dialog.Options - [fdoWrapAtEndOfFile];
end;

procedure TTMSFMXReplaceDialogForm.ComboBox1Change(Sender: TObject);
var
  s: string;
begin
  s := ComboBox1.Text;
  DoFindEditChange(s);
  Dialog.FindText := s;
  EnableButtons;
end;

procedure TTMSFMXReplaceDialogForm.ComboBox2Change(Sender: TObject);
var
  s: string;
begin
  s := ComboBox2.Text;
  DoReplaceEditChange(s);
  Dialog.ReplaceText := s;
end;

procedure TTMSFMXReplaceDialogForm.DirGroupClick(Sender: TObject);
begin
  if RadioButton2.IsChecked then
    Dialog.Options := Dialog.Options + [fdoDown]
  else
    Dialog.Options := Dialog.Options - [fdoDown];
end;

procedure TTMSFMXReplaceDialogForm.DoFindEditChange(var AText: string);
begin
  if Assigned(FOnFindEditChange) then
    FOnFindEditChange(Self, AText);
end;

procedure TTMSFMXReplaceDialogForm.DoReplaceEditChange(var AText: string);
begin
  if Assigned(FOnReplaceEditChange) then
    FOnReplaceEditChange(Self, AText);
end;

procedure TTMSFMXReplaceDialogForm.EnableButtons;
begin
  FindBtn.Enabled := (Dialog.FindText <> '') and (fdoFindEnabled in Dialog.Options);
  ReplaceBtn.Enabled := (Dialog.FindText <> '') and (fdoReplaceEnabled in Dialog.Options);
  ReplaceAllBtn.Enabled := (Dialog.FindText <> '') and (fdoReplaceAllEnabled in Dialog.Options);
end;

procedure TTMSFMXReplaceDialogForm.EndofLine1Click(Sender: TObject);
begin
  ComboBox1.Text := ComboBox1.Text + '$';
end;

procedure TTMSFMXReplaceDialogForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if Assigned(OnClose) then
    OnClose(Self);
end;

procedure TTMSFMXReplaceDialogForm.FormCreate(Sender: TObject);
begin
  More := False;
  Height := 210;

  ComboBox1.TabOrder := 0;
  FindBtn.TabOrder := 1;
  ComboBox2.TabOrder := 2;
  ReplaceBtn.TabOrder := 3;
  CheckBox1.TabOrder := 4;
  CheckBox4.TabOrder := 5;
  CheckBox2.TabOrder := 6;
  CheckBox5.TabOrder := 7;
  RadioButton2.TabOrder := 8;
  RadioButton3.TabOrder := 9;
  CurrentFile.TabOrder := 10;
  Selection.TabOrder := 11;
  AllOpen.TabOrder := 12;
  MoreBtn.TabOrder := 13;
  CloseBtn.TabOrder := 14;
  Memo1.TabOrder := 15;
  Memo2.TabOrder := 16;
end;


function TTMSFMXReplaceDialogForm.GetFindText: String;
begin
  if not More then
    Result := ComboBox1.Text
  else
    Result := Memo1.Text;
end;

function TTMSFMXReplaceDialogForm.GetReplaceText: String;
begin
  if not More then
    Result := ComboBox2.Text
  else
    Result := Memo2.Text;
end;

procedure TTMSFMXReplaceDialogForm.Memo1Change(Sender: TObject);
var
  s: string;
begin
  s := Memo1.Lines.Text;
  DoFindEditChange(s);
  Dialog.FindText := s;
  EnableButtons;
end;

procedure TTMSFMXReplaceDialogForm.N0or1matches1Click(Sender: TObject);
begin
  ComboBox1.Text := ComboBox1.Text + '*';
end;

procedure TTMSFMXReplaceDialogForm.N0or1Matches2Click(Sender: TObject);
begin
  ComboBox1.Text := ComboBox1.Text + '?';
end;

procedure TTMSFMXReplaceDialogForm.N1orMoreMatches1Click(Sender: TObject);
begin
  ComboBox1.Text := ComboBox1.Text + '+';
end;

procedure TTMSFMXReplaceDialogForm.NewLine1Click(Sender: TObject);
begin
  ComboBox1.Text := ComboBox1.Text + '\n';
end;

procedure TTMSFMXReplaceDialogForm.Or1Click(Sender: TObject);
begin
  ComboBox1.Text := ComboBox1.Text + '|';
end;

procedure TTMSFMXReplaceDialogForm.RangeGroupClick(Sender: TObject);
begin
  Dialog.Options := Dialog.Options - [fdoCurrentFile, fdoSelection, fdoAllOpenFiles];
  if Sender = CurrentFile then
    Dialog.Options := Dialog.Options + [fdoCurrentFile]
  else if Sender = Selection then
    Dialog.Options := Dialog.Options + [fdoSelection]
  else if Sender = AllOpen then
    Dialog.Options := Dialog.Options + [fdoAllOpenFiles];
end;

procedure TTMSFMXReplaceDialogForm.SetFindText(const Value: String);
begin
  if not More then
    ComboBox1.Text := Value
  else
    Memo1.Text := Value;
end;

procedure TTMSFMXReplaceDialogForm.SetReplaceText(const Value: String);
begin
  if not More then
    ComboBox2.Text := Value
  else
    Memo2.Text := Value;
end;

end.
