unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Buttons,
  SynEdit, SynHighlighterCpp, SynCompletion, SynPluginSyncroEdit,
  SynHighlighterPas, UTF8Process, Types, LCLType, ComCtrls, StdCtrls, Menus,
  LazUTF8, LConvEncoding, cp866, cp1251,INIFiles;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    ImageList1: TImageList;
    ImageList2: TImageList;
    Label1: TLabel;
    Label2: TLabel;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem10: TMenuItem;
    MenuItem11: TMenuItem;
    MenuItem12: TMenuItem;
    MenuItem13: TMenuItem;
    MenuItem14: TMenuItem;
    MenuItem15: TMenuItem;
    MenuItem16: TMenuItem;
    MenuItem17: TMenuItem;
    MenuItem18: TMenuItem;
    MenuItem19: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem20: TMenuItem;
    MenuItem21: TMenuItem;
    MenuItem22: TMenuItem;
    MenuItem23: TMenuItem;
    MenuItem24: TMenuItem;
    MenuItem25: TMenuItem;
    MenuItem26: TMenuItem;
    MenuItem27: TMenuItem;
    MenuItem28: TMenuItem;
    MenuItem29: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem30: TMenuItem;
    MenuItem31: TMenuItem;
    MenuItem32: TMenuItem;
    MenuItem33: TMenuItem;
    MenuItem34: TMenuItem;
    MenuItem35: TMenuItem;
    MenuItem36: TMenuItem;
    MenuItem37: TMenuItem;
    MenuItem38: TMenuItem;
    MenuItem39: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem40: TMenuItem;
    MenuItem41: TMenuItem;
    MenuItem42: TMenuItem;
    MenuItem43: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    OpenDialog1: TOpenDialog;
    Panel1: TPanel;
    Panel2: TPanel;
    PopupMenu1: TPopupMenu;
    ProcessUTF8_1: TProcessUTF8;
    ProcessUTF8_2: TProcessUTF8;
    ProcessUTF8_3: TProcessUTF8;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    SaveDialog1: TSaveDialog;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    Splitter1: TSplitter;
    StatusBar1: TStatusBar;
    SynAutoComplete1: TSynAutoComplete;
    SynCompletion1: TSynCompletion;
    SynEdit1: TSynEdit;
    SynFreePascalSyn1: TSynFreePascalSyn;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure CheckBox3Change(Sender: TObject);
    procedure FormActivate(Sender: TObject);


    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Label3Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure MenuItem38Click(Sender: TObject);
    procedure MenuItem3Click(Sender: TObject);
    procedure MenuItem41Click(Sender: TObject);
    procedure MenuItem42Click(Sender: TObject);
    procedure MenuItem43Click(Sender: TObject);
    procedure MenuItem4Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure MenuItem6Click(Sender: TObject);
    procedure MenuItem7Click(Sender: TObject);
    procedure MenuItem8Click(Sender: TObject);
    procedure Panel1Click(Sender: TObject);
    procedure RadioButton1ChangeBounds(Sender: TObject);
    procedure RadioButton1Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);

    procedure RadioButton3Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);
    procedure SpeedButton7Click(Sender: TObject);
    procedure SpeedButton8Click(Sender: TObject);
    procedure SpeedButton9Click(Sender: TObject);
    procedure OpenFile();
    procedure rbshow();

  private

  public

  end;

var
  Form1: TForm1;
  pr_path, pr_path_dir, fullFileName: String;
  pgui:String;

  MyUtf8: widestring;
  MyCP866: ansistring;
  MyCP1251: ansistring;
  st_p, os, vers :String;
  ListCP866: TStringList;
  ListCP1251: TStringList;
  ListUtf8: TStringList;
  list: TStringList;
  p_crt: integer;
  IniF:TINIFile;// Класс для работы с INI-файлами
  fpc_path: String;
  rb_cp866, rb_cp1251, rb_utf8: Integer;



implementation

uses  fun_1, Unit2, Unit5;


{$R *.lfm}

{ TForm1 }

procedure TForm1.OpenFile();
begin
     SynEdit1.Clear;
     ListCP866.LoadFromFile(fullFileName);
     MyCP866:=ListCP866.Text;
          MyUtf8:=fun_1.CP866_to_UTF8(MyCP866);
          SynEdit1.Text:=MyUtf8;

     Form1.Caption:= fullFileName;
     ChDir(ExtractFileDir(fullFileName));
end;

procedure TForm1.rbshow();
begin
     if (rb_cp866 = 1) then begin Form1.RadioButton1.Checked:=true; end;
     if (rb_cp866 = 0) then begin Form1.RadioButton1.Checked:=false;  end;

     if (rb_cp1251 = 1) then begin Form1.RadioButton2.Checked:=true;  end;
     if (rb_cp1251 = 0) then begin Form1.RadioButton2.Checked:=false; end;

     if (rb_utf8 = 1) then begin Form1.RadioButton3.Checked:=true;  end;
     if (rb_utf8 = 0) then begin Form1.RadioButton3.Checked:=false;  end;

     if Unit1.pgui = '' then Form1.CheckBox3.Checked:=false;
     if Unit1.pgui = '-WG' then Form1.CheckBox3.Checked:=true;
end;



procedure TForm1.FormCreate(Sender: TObject);
begin
  rb_cp866 := 1;
  rb_cp1251 := 0;
  rb_utf8 := 0;

  vers:='  [Версия 0.061]  ';
  Label2.Caption:=vers;
  os := 'win';
  p_crt:=0;
  pgui:='';
  if os = 'linux' then st_p   := '/';
  if os = 'win' then st_p   := '\';
  ListCP866 := TStringList.Create;
  ListCP1251 := TStringList.Create;
  ListUtf8 := TStringList.Create;
  list:= TStringList.Create;
    try
                  pr_path:= ParamStr(0);
                  pr_path_dir:= ExtractFilePath(pr_path);
                  fullFileName:=ParamStr(1);
                  SynEdit1.Clear;
                  OpenFile();
                  Form1.Caption:= fullFileName;
                  ChDir(pr_path_dir);
        except
            //ShowMessage('Ошибка: нет файла настроек');
        end;
         if FileExists('key_pas.ini') then SynCompletion1.ItemList.LoadFromFile('key_pas.ini');
         Form1.Caption:=pr_path_dir;

 if (FileExists(pr_path_dir + 'fpc.ini'))then
  begin
    Inif := TINIFile.Create(pr_path_dir + 'fpc.ini');
  try
    fpc_path:=Inif.ReadString('System', 'fpc_path', '');
    rb_cp866:=Inif.ReadInteger('System', 'rb_cp866', 1);
    rb_cp1251:=Inif.ReadInteger('System', 'rb_cp1251', 0);
    rb_utf8:=Inif.ReadInteger('System', 'rb_utf8', 0);
    pgui:=Inif.ReadString('System', 'pgui', '');
    rbshow();
 finally
    Inif.Free;
  end;
 end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ListCP866.Free;
  ListCP1251.Free;
  ListUtf8.Free;
  list.Free;
end;

procedure TForm1.Label3Click(Sender: TObject);
begin

end;

procedure TForm1.MenuItem1Click(Sender: TObject);
begin
  SynEdit1.CutToClipboard;
end;


procedure TForm1.MenuItem2Click(Sender: TObject);
begin
  SynEdit1.CopyToClipboard;
end;

procedure TForm1.MenuItem38Click(Sender: TObject);
begin
  Form5.ShowModal;
end;

procedure TForm1.MenuItem3Click(Sender: TObject);
begin
  SynEdit1.PasteFromClipboard;
end;

procedure TForm1.MenuItem41Click(Sender: TObject);
var  cur: Integer;
begin
  cur := SynEdit1.CaretY;
  SynEdit1.Lines.add('');
  SynEdit1.Lines.Insert(cur,'if a = b then');
  SynEdit1.Lines.Insert(cur+1,'   c := 1;');
end;

procedure TForm1.MenuItem42Click(Sender: TObject);
var  cur: Integer;
begin
  cur := SynEdit1.CaretY;
  SynEdit1.Lines.add('');
  SynEdit1.Lines.Insert(cur,'if a = b then');
  SynEdit1.Lines.Insert(cur+1,'   c := 1');
  SynEdit1.Lines.Insert(cur+2,' else c := 0;');
end;

procedure TForm1.MenuItem43Click(Sender: TObject);
var  cur: Integer;
begin
  cur := SynEdit1.CaretY;
  SynEdit1.Lines.add('');
  SynEdit1.Lines.Insert(cur,'for i := 1 to 10 do begin');
  SynEdit1.Lines.Insert(cur+1,'   i := 1;');
  SynEdit1.Lines.Insert(cur+2,'   write(i);');
  SynEdit1.Lines.Insert(cur+3,'end;');
end;

procedure TForm1.MenuItem4Click(Sender: TObject);
begin
  SynEdit1.ClearSelection;
end;

procedure TForm1.MenuItem5Click(Sender: TObject);
begin
  SynEdit1.SelectAll;
end;

procedure TForm1.MenuItem6Click(Sender: TObject);
begin
  SynEdit1.Undo;
end;

procedure TForm1.MenuItem7Click(Sender: TObject);
begin
  SynEdit1.Redo;
end;

procedure TForm1.MenuItem8Click(Sender: TObject);
var str: String;
    cur: Integer;
begin
  cur := SynEdit1.CaretY;
  str:='    //остановка закрытия окна';
  SynEdit1.Lines.Insert(cur,str);
  str:=' readln(); ';
  SynEdit1.Lines.Insert(cur+1,str);
end;

procedure TForm1.Panel1Click(Sender: TObject);
begin

end;

procedure TForm1.RadioButton1ChangeBounds(Sender: TObject);
begin

end;




procedure TForm1.RadioButton1Click(Sender: TObject);
begin
       if (Form1.RadioButton1.Checked = true) then begin rb_cp866 := 1; rb_cp1251:=0; rb_utf8 := 0; end;
     //Label3.Caption := '(' + inttostr(rb_cp866) + ')' +  '(' + inttostr(rb_cp1251) + ')' +  '(' + inttostr(rb_utf8) + ')';
end;




procedure TForm1.RadioButton2Click(Sender: TObject);
begin
     if (Form1.RadioButton2.Checked = true) then begin rb_cp866 := 0; rb_cp1251:=1; rb_utf8 := 0; end;
     //Label3.Caption := '(' + inttostr(rb_cp866) + ')' +  '(' + inttostr(rb_cp1251) + ')' +  '(' + inttostr(rb_utf8) + ')';
end;



procedure TForm1.RadioButton3Click(Sender: TObject);
begin
       if (Form1.RadioButton3.Checked = true) then begin rb_cp866 := 0; rb_cp1251:=0; rb_utf8 := 1; end;
     //Label3.Caption := '(' + inttostr(rb_cp866) + ')' +  '(' + inttostr(rb_cp1251) + ')' +  '(' + inttostr(rb_utf8) + ')';
end;




procedure TForm1.Button1Click(Sender: TObject);
begin
 SynEdit1.Clear;
 SynEdit1.Lines.LoadFromFile(fullFileName);
 MyCP1251 := SynEdit1.Text;
 MyUtf8:= ConvertEncoding(MyCP1251, EncodingCP1251, EncodingUTF8);
 SynEdit1.Text:= MyUtf8;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 SynEdit1.Clear;
 SynEdit1.Lines.LoadFromFile(fullFileName);
 MyCP866 := SynEdit1.Text;
 MyUtf8:= ConvertEncoding(MyCP866, EncodingCP866, EncodingUTF8);
 SynEdit1.Text:= MyUtf8;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
   SynEdit1.Clear;
 SynEdit1.Lines.LoadFromFile(fullFileName);
end;

procedure TForm1.CheckBox3Change(Sender: TObject);
begin
 if CheckBox3.Checked then Unit1.pgui:='-WG'
 else Unit1.pgui:=''
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  rbshow();
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
     //if Pos('crt', SynEdit1.Text) or Pos('CRT', SynEdit1.Text) <> 0 then RadioButton2.Checked:=true;
    if os = 'win' then fun_1.compiling_win();
end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
    if os = 'win' then fun_1.run_win();
end;

procedure TForm1.SpeedButton3Click(Sender: TObject);
begin
    if SaveDialog1.Execute then
  begin
     fullFileName:=SaveDialog1.FileName;
     fun_1.save_win();
  end;
end;

procedure TForm1.SpeedButton4Click(Sender: TObject);
begin
     if fullFileName='' then
  begin
   if SaveDialog1.Execute then fullFileName:=SaveDialog1.FileName;
  end;
 fun_1.save_win();

end;

procedure TForm1.SpeedButton5Click(Sender: TObject);
begin
SynEdit1.Lines.Clear;
SynEdit1.Lines.Add(' program HelloWorld; ');
SynEdit1.Lines.Add(' var // секция объявления переменных ');
SynEdit1.Lines.Add('   x :integer;  //объявим целочисленную переменную  ');
SynEdit1.Lines.Add(' begin ');
SynEdit1.Lines.Add('   writeln(' + #39 + 'Привет Мир!' + #39 + ');');
SynEdit1.Lines.Add('   read(x); // ожидаем ввода (который всегда заканчивается нажатием Enter)');
SynEdit1.Lines.Add(' end. ');

end;

procedure TForm1.SpeedButton6Click(Sender: TObject);
begin
if OpenDialog1.Execute then
  begin
   fullFileName := OpenDialog1.FileName;
   OpenFile();
  end;
end;

procedure TForm1.SpeedButton7Click(Sender: TObject);
begin
//if Pos('crt', SynEdit1.Text) or Pos('crt', SynEdit1.Text) <> 0 then RadioButton2.Checked:=true;
 if os = 'win' then fun_1.compiling_win();
 Form1.ProcessUTF8_1.WaitOnExit;
    if os = 'win' then fun_1.run_win();
end;

procedure TForm1.SpeedButton8Click(Sender: TObject);
begin
  ChDir(ExtractFileDir(fullFileName));
  //if Pos('crt', SynEdit1.Text) or Pos('crt', SynEdit1.Text) <> 0 then RadioButton2.Checked:=true;
  if os = 'win' then fun_1.console_win();

    Form1.ProcessUTF8_2.WaitOnExit;
  if os = 'linux' then
  begin
    st_p   := '/';
    ProcessUTF8_2.CommandLine:='./' + 'compile_run.sh';
  end;
  if os = 'win' then
  begin
    st_p   := '\';
    ProcessUTF8_2.CommandLine:='compile_run.bat';
  end;

    ProcessUTF8_2.Execute;
  end;

procedure TForm1.SpeedButton9Click(Sender: TObject);
begin
Form2.ShowModal;
end;



end.

