unit UEditor67;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, StdActns, ActnList, ImgList, ComCtrls, StdCtrls, ToolWin,
  AppEvnts, ExtActns;

type
  TForm1 = class(TForm)
    ToolBar1: TToolBar;
    TBCreate: TToolButton;
    TBSave: TToolButton;
    ToolButton4: TToolButton;
    ToolButton6: TToolButton;
    TBUndo: TToolButton;
    TBCut: TToolButton;
    TBCopy: TToolButton;
    TBPaste: TToolButton;
    ToolButton11: TToolButton;
    ToolButton14: TToolButton;
    ToolButton16: TToolButton;
    TBLeft: TToolButton;
    TBCenter: TToolButton;
    TBRight: TToolButton;
    TBBullet: TToolButton;
    RichEdit1: TRichEdit;
    StatusBar1: TStatusBar;
    MainMenu1: TMainMenu;
    MFile1: TMenuItem;
    MNew1: TMenuItem;
    MOpen1: TMenuItem;
    MClose1: TMenuItem;
    N1: TMenuItem;
    MSave1: TMenuItem;
    MSaveAs1: TMenuItem;
    N2: TMenuItem;
    MPrint1: TMenuItem;
    MPrintSetup1: TMenuItem;
    N3: TMenuItem;
    MExit1: TMenuItem;
    MEdit1: TMenuItem;
    MUndo1: TMenuItem;
    N5: TMenuItem;
    MCut1: TMenuItem;
    MCopy1: TMenuItem;
    MPaste1: TMenuItem;
    N6: TMenuItem;
    MFind1: TMenuItem;
    MReplace1: TMenuItem;
    MFormat1: TMenuItem;
    MFont1: TMenuItem;
    MParagr1: TMenuItem;
    MBullet: TMenuItem;
    MWindow1: TMenuItem;
    MNewWind1: TMenuItem;
    N7: TMenuItem;
    MCascad1: TMenuItem;
    MArrange1: TMenuItem;
    MHorizont1: TMenuItem;
    MVert1: TMenuItem;
    MArrIcon1: TMenuItem;
    MHelp1: TMenuItem;
    MCallHelp1: TMenuItem;
    N8: TMenuItem;
    MAbout1: TMenuItem;
    Image1: TImageList;
    ActionList1: TActionList;
    ACut: TEditCut;
    ACopy: TEditCopy;
    APaste: TEditPaste;
    AArrangeIcons: TWindowArrange;
    ACascade: TWindowCascade;
    ATileHorizontally: TWindowTileHorizontal;
    ATileVertically: TWindowTileVertical;
    ACreate: TAction;
    ASave: TAction;
    AExit: TAction;
    PopupMenu1: TPopupMenu;
    N9: TMenuItem;
    N10: TMenuItem;
    N11: TMenuItem;
    N12: TMenuItem;
    N13: TMenuItem;
    N14: TMenuItem;
    N15: TMenuItem;
    N16: TMenuItem;
    N17: TMenuItem;
    AUndo: TEditUndo;
    ApplicationEvents1: TApplicationEvents;
    N18: TMenuItem;
    AParagraph: TAction;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton5: TToolButton;
    ABold: TRichEditBold;
    AItalic: TRichEditItalic;
    AUnderline: TRichEditUnderline;
    AStrike: TRichEditStrikeOut;
    ABullet: TRichEditBullets;
    ALeft: TRichEditAlignLeft;
    ARight: TRichEditAlignRight;
    ACenter: TRichEditAlignCenter;
    FileOpen1: TFileOpen;
    FileSaveAs1: TFileSaveAs;
    FilePrintSetup1: TFilePrintSetup;
    FilePageSetup1: TFilePageSetup;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    N4: TMenuItem;
    FontEdit1: TFontEdit;
    ToolButton9: TToolButton;
    PrintDlg1: TPrintDlg;
    ToolButton10: TToolButton;
    SearchFind1: TSearchFind;
    SearchFindNext1: TSearchFindNext;
    SearchReplace1: TSearchReplace;
    ToolButton12: TToolButton;
    ToolButton15: TToolButton;
    ToolButton17: TToolButton;
    procedure ACreateExecute(Sender: TObject);
    procedure ASaveExecute(Sender: TObject);
    procedure StatusExecute(Sender: TObject);
    procedure RichEdit1KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure RichEdit1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure AExitExecute(Sender: TObject);
    procedure ApplicationEvents1Hint(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormDestroy(Sender: TObject);
    procedure AParagraphExecute(Sender: TObject);
    procedure RichEdit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FileOpen1Accept(Sender: TObject);
    procedure FileSaveAs1BeforeExecute(Sender: TObject);
    procedure FileSaveAs1Accept(Sender: TObject);
    procedure FontEdit1Accept(Sender: TObject);
    procedure FontEdit1FontDialogApply(Sender: TObject; Wnd: HWND);
    procedure PrintDlg1Accept(Sender: TObject);
    procedure SearchFind1BeforeExecute(Sender: TObject);
    procedure SearchFind1FindDialogClose(Sender: TObject);
    procedure SearchReplace1BeforeExecute(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses UCustom67, Consts;

{$R *.dfm}

type
  TFileOld = class(TMenuItem)
   Procedure Click(); override;
  end;

var SFile: AnsiString = 'Неизвестный';
    NFileMen: word;
    NFiles: word = 10;
    TmpItem: TFileOld;
    Ins: boolean = true;
    HideSel: boolean;

procedure TFileOld.Click();
begin
 Form1.RichEdit1.Lines.LoadFromFile(Caption);
 SFile := ExtractFileName(Caption);
 Form1.RichEdit1.Modified:=false;
end;

procedure FileToMenu(Dialog:TOpenDialog);
begin
 with Form1 do begin
  // Сохранение имени в SFile
  SFile := Dialog.FileName;
  // Удаление раздела, соответствующего тому же файлу
  if (MainMenu1.Items[0].Find(SFile) <> nil)
   then MainMenu1.Items[0].Find(SFile).Free;
  // Создание нового раздела
  TmpItem := TFileOld.Create(MainMenu1);
  TmpItem.Caption := SFile;
  // Вставка в меню нового раздела
  MainMenu1.Items[0].Insert(NFileMen,TmpItem);
  // Если список переполнен, удаление последнего раздела
  if(MainMenu1.Items[0].Count > NFileMen + NFiles + 2)
    then MainMenu1.Items[0].Items[NFileMen+NFiles].Free;
 end;
end;

procedure TForm1.StatusExecute(Sender: TObject);
begin
 StatusBar1.Panels.Items[0].Text:=
                    IntToStr(RichEdit1.CaretPos.y+1) +
                    ': '+IntToStr(RichEdit1.CaretPos.x+1);
 if (RichEdit1.Modified)
  then StatusBar1.Panels.Items[1].Text:='модиф.'
  else StatusBar1.Panels.Items[1].Text:='';
 if Ins
  then StatusBar1.Panels.Items[2].Text:='вставка'
  else StatusBar1.Panels.Items[2].Text:='замена';
end;

procedure TForm1.ACreateExecute(Sender: TObject);
var res:integer;
begin
 if (RichEdit1.Modified)
 then begin
  res:=Application.MessageBox('Текст в окне был изменен. Сохранить его?',
                              'Подтвердите сохранение файла',
                               MB_YESNOCANCEL);
  case (res) of
   IDYES:    ASaveExecute(Sender);
   IDCANCEL: Exit;
  end;
  RichEdit1.Clear;
  RichEdit1.Modified := false;
  SFile :='Неизвестный';
 end
end;

procedure TForm1.ASaveExecute(Sender: TObject);
begin
 if (SFile = 'Неизвестный')
 then FileSaveAs1.Execute
 else RichEdit1.Lines.SaveToFile(SFile);
 RichEdit1.Modified:=false;
end;

procedure TForm1.RichEdit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 if (Key = VK_INSERT) and (Shift = [])
   then Ins := not Ins;
 StatusExecute(Sender);
end;

procedure TForm1.RichEdit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 StatusExecute(Sender);
end;

procedure TForm1.RichEdit1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  StatusExecute(Sender);
end;

procedure TForm1.FormCreate(Sender: TObject);
var List: TStringList;
    i: word;
begin
 RichEdit1.Paragraph.FirstIndent:=0;
 RichEdit1.Paragraph.LeftIndent:=- RichEdit1.Paragraph.FirstIndent;
 RichEdit1.Modified:=false;
 NFileMen := MainMenu1.Items[0].Count - 2;
 if FileExists('Menu.cfg')
  then begin
   List := TStringList.Create;
   List.LoadFromFile('Menu.cfg');
   for i := 0 to List.Count-1 do
   begin
    TmpItem := TFileOld.Create(MainMenu1);
    TmpItem.Caption := List.Strings[i];
    MainMenu1.Items[0].Insert(NFileMen,TmpItem);
   end;
   List.Free;
  end;
 StatusExecute(Sender);
end;

procedure TForm1.AExitExecute(Sender: TObject);
begin
 Close;
end;
procedure TForm1.ApplicationEvents1Hint(Sender: TObject);
begin
 StatusBar1.Panels.Items[3].Text := Application.Hint;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
 if RichEdit1.Modified then
  CanClose := (Application.MessageBox(
               'Текст документа не сохранен.'+
               #13'Действительно хотите закончить работу ?',
               'Подтвердите завершение работы',
               MB_ICONQUESTION+MB_YESNO) = IDYES);
end;

procedure TForm1.FormDestroy(Sender: TObject);
var List: TStringList;
    i: word;
begin
 List := TStringList.Create;
 for i := MainMenu1.Items[0].Count-3 downto NFileMen  do
   List.Add(MainMenu1.Items[0].Items[i].Caption);
 List.SaveToFile(ExtractFileDir(ParamStr(0))+'\Menu.cfg');
   List.Free;
end;

procedure TForm1.AParagraphExecute(Sender: TObject);
begin
 FCustom.ShowModal;
end;

procedure TForm1.FileOpen1Accept(Sender: TObject);
begin
   SFile := FileOpen1.Dialog.FileName;
   (ActiveControl as TRichEdit).Lines.LoadFromFile(SFile);
   (ActiveControl as TRichEdit).Modified:=false;
   FileToMenu(FileOpen1.Dialog);
end;

procedure TForm1.FileSaveAs1BeforeExecute(Sender: TObject);
begin
 FileSaveAs1.Dialog.FileName := SFile;
end;

procedure TForm1.FileSaveAs1Accept(Sender: TObject);
begin
 if (SFile <> FileSaveAs1.Dialog.FileName)
    then FileToMenu(FileSaveAs1.Dialog);
 SFile := FileSaveAs1.Dialog.FileName;
 (ActiveControl as TRichEdit).Lines.SaveToFile(SFile);
 (ActiveControl as TRichEdit).Modified:=false;
end;

procedure TForm1.FontEdit1Accept(Sender: TObject);
begin
 RichEdit1.SelAttributes.Assign(FontEdit1.Dialog.Font);
end;

procedure TForm1.FontEdit1FontDialogApply(Sender: TObject; Wnd: HWND);
begin
 RichEdit1.SelAttributes.Assign(FontEdit1.Dialog.Font);
end;

procedure TForm1.PrintDlg1Accept(Sender: TObject);
var i:integer;
begin
 for i:=1 to PrintDlg1.Dialog.Copies do
   RichEdit1.Print('Печать RichEdit1');
end;

procedure TForm1.SearchFind1BeforeExecute(Sender: TObject);
begin
 with (ActiveControl as TRichEdit) do
 begin
  HideSel := HideSelection;
  HideSelection := false;
  SearchFind1.Dialog.FindText := SelText;
  SearchFind1.Dialog.Position := Form1.ClientToScreen(Point(Left, Top + Height));
 end;
end;

procedure TForm1.SearchReplace1BeforeExecute(Sender: TObject);
begin
 with (ActiveControl as TRichEdit) do
 begin
  HideSel := HideSelection;
  HideSelection := false;
  SearchReplace1.Dialog.FindText := SelText;
  SearchReplace1.Dialog.Position := Form1.ClientToScreen(Point(Left, Top + Height));
 end;
end;

procedure TForm1.SearchFind1FindDialogClose(Sender: TObject);
begin
 (ActiveControl as TRichEdit).HideSelection := HideSel;
end;

end.
