//Autor: Wiesław Dudek
//


unit TextFrm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons;

type
  TTextForm = class(TForm)
    Memo1: TMemo;
    Label1: TLabel;
    Memo2: TMemo;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    BitBtn4: TBitBtn;
    procedure FormShow(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
  private
    { Private declarations }
    procedure RefreshAll ;
  public
    { Public declarations }
  end;

var
  TextForm: TTextForm;

implementation

{$R *.DFM}

procedure TTextForm.RefreshAll ;
var fin : TextFile ;
    aLinia : string ;
begin
  Memo1.Clear ;
  if FileExists(ExtractFilePath(Application.ExeName)+'Sample.txt') then
  begin
    AssignFile(fin,ExtractFilePath(Application.ExeName)+'Sample.txt') ;
    Reset(fin) ;
    while not eof(fin) do
    begin
      Readln(fin,aLinia) ;
      Memo1.Lines.Add(aLinia) ;
    end ;
    CloseFile(fin) ;
  end ;
end ;

procedure TTextForm.FormShow(Sender: TObject);
begin
  RefreshAll ;
end;

procedure TTextForm.BitBtn4Click(Sender: TObject);
begin
  Close ;
end;

procedure TTextForm.BitBtn1Click(Sender: TObject);
var fout : TextFile ;
    i : integer ;
begin
  AssignFile(fout,ExtractFilePath(Application.ExeName)+'Sample.txt') ;
  if FileExists(ExtractFilePath(Application.ExeName)+'Sample.txt') then Append(fout)
  else Rewrite(fout) ;
  for i:=1 to Memo2.Lines.Count do
    Writeln(fout,Memo2.Lines.Strings[i-1]) ;
  CloseFile(fout) ;
  RefreshAll ;
end;

procedure TTextForm.BitBtn2Click(Sender: TObject);
var fout : TextFile ;
    i : integer ;
begin
  AssignFile(fout,ExtractFilePath(Application.ExeName)+'Sample.txt') ;
  Rewrite(fout) ;
  for i:=1 to Memo2.Lines.Count do
    Writeln(fout,Memo2.Lines.Strings[i-1]) ;
  CloseFile(fout) ;
  RefreshAll ;
end;

procedure TTextForm.BitBtn3Click(Sender: TObject);
var i, aSrodek : integer ;
begin
  with TStringList.Create do
  try
    if FileExists(ExtractFilePath(Application.ExeName)+'Sample.txt')
      then LoadFromFile(ExtractFilePath(Application.ExeName)+'Sample.txt') ;
    aSrodek:=Count div 2 ;
    for i:=1 to Memo2.Lines.Count do
      Insert(aSrodek+i-1,Memo2.Lines.Strings[i-1]) ;
    SaveToFile(ExtractFilePath(Application.ExeName)+'Sample.txt') ;
  finally
    Free ;
  end ;
  RefreshAll ; 
end;

end.
