unit EDIC;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Menus;

type
  TDicBox = class(TForm)
    btSave: TButton;
    btESC: TButton;
    btSort: TButton;
    ProgressBar1: TProgressBar;
    Dict: TRichEdit;
    EDPopupMenu: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    N8: TMenuItem;
    N9: TMenuItem;
    N10: TMenuItem;
    EDFindDialog: TFindDialog;
    N11: TMenuItem;
    N12: TMenuItem;
    procedure SortButtonClick(Sender: TObject);
    procedure SaveButtonClick(Sender: TObject);
    procedure ESCButtonClick(Sender: TObject);
    procedure AEDFindExecute(Sender: TObject);
    procedure EDFindDialogFind(Sender: TObject);
    procedure EDFormDeactivate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  DicBox: TDicBox;

implementation

uses Unit1;

{$R *.dfm}

procedure TDicBox.SortButtonClick(Sender: TObject);
var
   Tmp: TStringList;
   i, n: integer;
begin
try
  Tmp := TStringList.Create;
  Tmp.Sorted := True;
  Tmp.CaseSensitive := True;  {!}
  n := Dict.Lines.Count;
  ProgressBar1.Min := 0;
  ProgressBar1.Max := n;
  for i := 0 to n-1 do begin
    Tmp.Append(Dict.Lines[i]);
    ProgressBar1.Position := i;
  end;
  ProgressBar1.Position := 0;
  Dict.Text := 'Немного подождите...';
  Application.ProcessMessages;
  Dict.Lines.BeginUpdate;
  Dict.Clear;
  n := Tmp.Count;
  for i := 0 to n-1 do begin
    Dict.Lines.Append(Tmp[i]);
    ProgressBar1.Position := i;
  end;
  Dict.Lines.EndUpdate;
  ProgressBar1.Position := 0;
finally
  FreeAndNil(Tmp);
end;
end;


procedure TDicBox.SaveButtonClick(Sender: TObject);
begin
  Dict.Lines.SaveToFile(Unit1.Form1.PubDicName);
  DicBox.Close;
end;


procedure TDicBox.ESCButtonClick(Sender: TObject);
begin
  DicBox.Close;
end;


procedure TDicBox.AEDFindExecute(Sender: TObject);
begin
  with EDFindDialog do begin
    FindText := Dict.SelText;
    Options:=Options + [frHideUpDown,frHideWholeWord];
    Execute;
  end;
end;


procedure TDicBox.EDFindDialogFind(Sender: TObject);
var SPos: integer;
begin
  SPos := Dict.SelStart;
  if Dict.SelLength > 0 then SPos := SPos + Dict.SelLength;
  with EDFindDialog do begin
  if frMatchCase in Options  {поиск с учетом или без учета регистра}
    then Dict.SelStart := Dict.FindText(FindText, SPos, Length(Dict.Text)-SPos,[stMatchCase])
    else Dict.SelStart := Dict.FindText(FindText, SPos, Length(Dict.Text)-SPos, []);
  if Dict.SelStart >= Spos
    then begin
    Dict.SelLength := Length(FindText);
    SPos := Dict.SelStart+Dict.SelLength+1;
  end
    else
    if MessageDlg('Текст "'+FindText+'" далее не найден. Продолжать?',
                   mtConfirmation,mbYesNoCancel,0) <> mrYes
    then begin
       CloseDialog;
       exit;
    end;
  end;
  Dict.Perform(EM_SCROLLCARET,0,0);
  Dict.SetFocus;
end;


procedure TDicBox.EDFormDeactivate(Sender: TObject);
begin
  if EDFindDialog.Execute then EDFindDialog.CloseDialog;
  DicBox.SetFocus;
end;


end.
