unit RichForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, ToolWin, ExtCtrls, StdCtrls, RichEdit2, Menus,PCTypesUtils,
  {$ifndef DELPHI3}
  ImgList,
  {$endif}
  Spin, siComp, siLngLnk;

type
  TfrmRichEdit = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    Panel1: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    ToolButton9: TToolButton;
    ToolButton10: TToolButton;
    ToolButton11: TToolButton;
    ToolButton12: TToolButton;
    ComboBox1: TComboBox;
    ToolButton13: TToolButton;
    ImageList1: TImageList;
    ColorDialog1: TColorDialog;
    PopupMenu1: TPopupMenu;
    LoadFromFile1: TMenuItem;
    SaveToFile1: TMenuItem;
    N1: TMenuItem;
    InsertObject1: TMenuItem;
    N2: TMenuItem;
    ToolButton6: TToolButton;
    ToolButton14: TToolButton;
    ToolButton15: TToolButton;
    ToolButton16: TToolButton;
    ToolButton17: TToolButton;
    ToolButton18: TToolButton;
    ToolButton19: TToolButton;
    ToolButton20: TToolButton;
    SetLanguage1: TMenuItem;
    SaveDialog1: TSaveDialog;
    SpinEdit1: TSpinEdit;
    N3: TMenuItem;
    Numbering1: TMenuItem;
    None1: TMenuItem;
    Bullets1: TMenuItem;
    Bumbers1: TMenuItem;
    LowerCase1: TMenuItem;
    UpperCase1: TMenuItem;
    Sequance1: TMenuItem;
    SetStartNumber1: TMenuItem;
    OpenDialog1: TOpenDialog;
    Panel2: TPanel;
    bOK: TButton;
    bCancel: TButton;
    lng_Forms: TsiLangLinked;
    procedure bOKClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure RichEdit1SelectionChange(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ToolButton16Click(Sender: TObject);
    procedure ToolButton15Click(Sender: TObject);
    procedure ToolButton14Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure ToolButton18Click(Sender: TObject);
    procedure ToolButton19Click(Sender: TObject);
    procedure RichEdit1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure LoadFromFile1Click(Sender: TObject);
    procedure SaveToFile1Click(Sender: TObject);
    procedure InsertObject1Click(Sender: TObject);
    procedure SpinEdit1Change(Sender: TObject);
    procedure None1Click(Sender: TObject);
    procedure Bullets1Click(Sender: TObject);
    procedure Bumbers1Click(Sender: TObject);
    procedure LowerCase1Click(Sender: TObject);
    procedure UpperCase1Click(Sender: TObject);
    procedure SetStartNumber1Click(Sender: TObject);
    procedure bCancelClick(Sender: TObject);

  private
    { Private declarations }
  public
    fStream: TMemorystream;
    RichEdit1: TRichEdit98;
    TextChangesCount : integer; // Tolik 10/03/2020 --
    procedure LngClick(Sender: TObject);
    procedure OnFormKeyPress(Sender: TObject; var Key: Char);
    procedure OnFormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure OnFormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    Procedure onTextChange(Sender: TObject);
  end;

var
  frmRichEdit: TfrmRichEdit;
  langcodes : array[1..69] of Integer = (1078,1052,1025,1069,2067,2060,1046,
  1026,1059,1027,1050,1029,1030,1043,3081,4105,5129,7177,2057,1033,1061,1080,
  1065,1035,2077,1036,3084,1031,1032,1037,1038,1040,1039,1057,1041,1042,1062,
  1063,1071,1086,2058,1044,2068,1045,2070,1048,1049,3098,2074,1072,2052,1051,
  1060,1034,3082,1053,4108,2055,2064,1054,1028,1073,1074,1055,1058,1075,1066,
  1076,1077);
  innerChange: Boolean;




implementation

{$R *.DFM}

procedure TfrmRichEdit.ToolButton1Click(Sender: TObject);
begin
  if fsBold in richedit1.SelAttributes.Style then
    richedit1.SelAttributes.Style := richedit1.SelAttributes.Style - [fsBold]
  else
    richedit1.SelAttributes.Style := richedit1.SelAttributes.Style + [fsBold];
end;

procedure TfrmRichEdit.ToolButton2Click(Sender: TObject);
begin
  if fsItalic in richedit1.SelAttributes.Style then
    richedit1.SelAttributes.Style := richedit1.SelAttributes.Style - [fsItalic]
  else
    richedit1.SelAttributes.Style := richedit1.SelAttributes.Style + [fsItalic];

end;

procedure TfrmRichEdit.ToolButton3Click(Sender: TObject);
begin
  if fsUnderLine in richedit1.SelAttributes.Style then
    richedit1.SelAttributes.Style := richedit1.SelAttributes.Style - [fsUnderLine]
  else
    richedit1.SelAttributes.Style := richedit1.SelAttributes.Style + [fsUnderLine];
end;

procedure TfrmRichEdit.ToolButton5Click(Sender: TObject);
begin
if colordialog1.Execute then begin
  richedit1.SelAttributes.Color := colordialog1.color;
end;
end;

procedure TfrmRichEdit.FormCreate(Sender: TObject);
var i,a:integer;
    xParent,subItem: TMenuItem;
begin
  TextChangesCount := 0; // Tolik 10/03/2020

  richEdit1 := TRichEdit98.Create(self);
  richEdit1.parent := self;
  richedit1.visible := true;
  richedit1.align := alClient;
  richedit1.HideSelection := false;
  RichEdit1.OnMouseDown := RichEdit1MouseDown;
  RichEdit1.OnSelectionChange := RichEdit1SelectionChange;
  RichEdit1.OnKeyPress := OnFormKeyPress;
  RichEdit1.OnKeyDown := OnFormKeyDown;
  RichEdit1.OnKeyUp := OnFormKeyUp;

  for i := 0 to Screen.Fonts.Count -1 do
  begin
    Combobox1.Items.Add(Screen.Fonts[i]);
  end;
  Combobox1.ItemIndex := 0;

  xParent := popUpMenu1.Items[popUpMenu1.Items.Count-1];

  for a := 1 to 69 do
  begin
    subItem := TMenuItem.Create(self);
    subItem.Caption := LanguageName(TLanguage(langcodes[a]));
    subItem.Tag := langcodes[a];
    subItem.OnClick := lngClick;
    xParent.Add(subItem);
  end;
  innerchange := false;
end;

procedure TfrmRichEdit.ComboBox1Change(Sender: TObject);
begin
  if innerchange then exit;
  richedit1.SelAttributes.Name  := Combobox1.Text;
end;

procedure TfrmRichEdit.RichEdit1SelectionChange(Sender: TObject);
begin
  innerchange := true;
  Combobox1.ItemIndex := Combobox1.Items.IndexOf(richedit1.SelAttributes.Name);
  SpinEdit1.Value := richedit1.SelAttributes.Size;
  innerchange := false;
end;

procedure TfrmRichEdit.ToolButton6Click(Sender: TObject);
begin
  richedit1.Paragraph.Alignment := taJustify;
end;

procedure TfrmRichEdit.FormShow(Sender: TObject);
var
  SelChange: TNotifyEvent; // Tolik 05/10/2021 --
begin
  richedit1.SetFocus;
  RichEdit1SelectionChange(nil);

  SelChange := RichEdit1.OnSelectionChange;
  RichEdit1.OnSelectionChange := nil;
  RichEdit1.OnChange := nil;

  RichEdit1.Lines.LoadFromStream(fStream, nil );

  RichEdit1.OnSelectionChange := SelChange; // Tolik 05/10/2021 --
  RichEdit1.OnChange := onTextChange; // Tolik 10/03/2020 --

  RichEdit1SelectionChange(nil);
end;

procedure TfrmRichEdit.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
//  ModalResult := mrCancel;
end;

procedure TfrmRichEdit.ToolButton16Click(Sender: TObject);
begin
   richedit1.SelAttributes.IndexKind := ikSuperscript;
end;

procedure TfrmRichEdit.ToolButton15Click(Sender: TObject);
begin
   richedit1.SelAttributes.IndexKind := ikSubscript;
end;

procedure TfrmRichEdit.ToolButton14Click(Sender: TObject);
begin
   richedit1.SelAttributes.IndexKind := ikNone;
end;

procedure TfrmRichEdit.ToolButton7Click(Sender: TObject);
begin
   richedit1.Paragraph.Alignment := taLeft;
end;

procedure TfrmRichEdit.ToolButton8Click(Sender: TObject);
begin
  richedit1.Paragraph.Alignment := taCenter;
end;

procedure TfrmRichEdit.ToolButton9Click(Sender: TObject);
begin
  richedit1.Paragraph.Alignment := taRight;
end;

procedure TfrmRichEdit.ToolButton12Click(Sender: TObject);
begin
  richedit1.Paragraph.Numbering := nsBullet;
end;

procedure TfrmRichEdit.ToolButton18Click(Sender: TObject);
begin
  richedit1.Paragraph.Numbering := nsNumber;
end;

procedure TfrmRichEdit.ToolButton19Click(Sender: TObject);
begin
  richedit1.Paragraph.Numbering := nsNone;
end;

procedure TfrmRichEdit.RichEdit1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var pnt: TPoint;
  begin

  if button = mbRight then
  begin
    pnt := RichEdit1.ClientToScreen(Point(x,y));
    pOpUpMenu1.Popup(pnt.x,pnt.y);
  end;
end;

procedure TfrmRichEdit.LngClick(Sender: TObject);
begin
  RichEdit1.Language := TLanguage(TMEnuItem(sender).tag);
end;

procedure TfrmRichEdit.LoadFromFile1Click(Sender: TObject);
begin
 if opendialog1.Execute then
  richedit1.lines.LoadFromFile(opendialog1.filename);
end;

procedure TfrmRichEdit.SaveToFile1Click(Sender: TObject);
begin
 if savedialog1.Execute then
  richedit1.lines.SaveToFile(savedialog1.filename, nil );
end;

procedure TfrmRichEdit.InsertObject1Click(Sender: TObject);
begin
  richedit1.InsertObjectDialog;
end;

procedure TfrmRichEdit.SpinEdit1Change(Sender: TObject);
begin
  if innerchange then exit;
  if Spinedit1.Text = '' then exit;
if  SpinEdit1.Value > 0 then
  richedit1.SelAttributes.Size := SpinEdit1.Value;
end;

procedure TfrmRichEdit.None1Click(Sender: TObject);
begin
  richedit1.Paragraph.Numbering := nsNone;
end;

procedure TfrmRichEdit.Bullets1Click(Sender: TObject);
begin
  richedit1.Paragraph.Numbering := nsBullet;
end;

procedure TfrmRichEdit.Bumbers1Click(Sender: TObject);
begin
  richedit1.Paragraph.Numbering := nsNumber;
end;

procedure TfrmRichEdit.LowerCase1Click(Sender: TObject);
begin
  richedit1.Paragraph.Numbering := nsLowercase;
end;

procedure TfrmRichEdit.UpperCase1Click(Sender: TObject);
begin
  richedit1.Paragraph.Numbering := nsUpperCase;
end;

procedure TfrmRichEdit.SetStartNumber1Click(Sender: TObject);
var strx: string;
begin
  strx := inputbox('Numbering Start','Enter a Number','');
  if strx <> '' then begin
    richedit1.Paragraph.NumberingStart := strtointdef(strx,0);
  end;
end;

procedure TfrmRichEdit.bOKClick(Sender: TObject);
begin
//  ModalResult := mrOk;
end;

procedure TfrmRichEdit.bCancelClick(Sender: TObject);
begin
//  ModalResult := mrCancel;
end;

procedure TfrmRichEdit.OnFormKeyPress(Sender: TObject; var Key: Char);
begin
  // Tolik 13/02/2020 --
  richedit1.SelAttributes.Name := Combobox1.Text;
  //richedit1.Font.Name := Combobox1.Text;
  if Combobox1.Text = 'GOST'  then
  begin
    if Key = '№' then
    begin
      Key := chr(35);
      richedit1.Refresh;
      //Font.Name := 'GOST'
    end;
  end;

  // по Esc - Отмена!
  if Key = #27 then
  begin
    bCancel.Click;
  end;
end;

procedure TfrmRichEdit.OnFormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  //if richedit1.SelText <> '' then
  //if sender <> RichEdit1 then
  begin
    richedit1.SelAttributes.Name := Combobox1.Text;

    //richedit1.Font.Name := Combobox1.Text;
  end;
 { if richedit1.SelAttributes.Name = 'GOST' then
    richedit1.Font.Charset := 204;  }
  if (ssCtrl in Shift) and (Key = 13) then
  begin
    Key := 0;
    bOK.Click;
  end;
end;

procedure TfrmRichEdit.OnFormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin

end;

// Tolik 10/03/2020 --
Procedure TfrmRichEdit.onTextChange(Sender: TObject);
begin
  inc(TextChangesCount);
end;

end.
