NBPrg40. W_TEXT.PAS. MiniOS. OpenSource.

NBPrg40. W_TEXT.PAS. MiniOS. OpenSource.
***************************************

{ Nikita Beloshenko (GNU) w_text = text(WORK_SYS) }

Unit w_text;

(***********************************************
 *                                             *
 *   W_TEXT.PAS                                *
 *                                             *
 *   w_text.                                   *
 *   Graphical text editor.                    *
 *                                             *
 ***********************************************)

Interface

Uses crt,dos,graph,interf_w,work_sys;

Const
  maxi_ed=1500;
  maxj_ed=40;

Type
     wedit=Object(work_sys_object)
           End;

     ed_coord=Record
                x,y,x1,y1:integer;
              End;

     edit_editor=Object(wedit)
                   stx,sty:integer;
                   i_pos,j_pos:word; pagepos:word;
                   d_:Array[1..maxi_ed,1..maxj_ed] Of char;
                   Procedure insert_chr(Var ch:char);
                   Procedure printpage;
                   Procedure init_d;
                 End;

     edit_palete=Object(wedit)
                   titlestring:String;
                   titlestringcolor:integer;
                   editbodycolor:integer;
                   editframecolor:integer;
                   titlecolor:integer;
                   framecolor:integer;
                   bodycolor:integer;
                   Constructor init;
                   Procedure restruct;
                   Destructor done;
                 End;

      edit_interf=Object(wedit)
                    editor:edit_editor;
                    pal:edit_palete;
                    titlecoord:ed_coord;
                    editcoord:ed_coord;
                    coord:ed_coord;
                    Constructor init;
                    Procedure buildbody;
                    Procedure run;
                    Procedure printhelp;
                    Procedure savefile;
                    Procedure openfile;
                    Destructor done;
                  End;


Procedure text_run;

Var pedit_interf:^edit_interf;

Implementation

Var maxx,maxy:integer;

Procedure text_run;
Begin
  maxx:=getmaxx; maxy:=getmaxy;
  new(pedit_interf,init);
  With pedit_interf^ Do Begin
       editor.init_d;
       With editor Do Begin i_pos:=1; j_pos:=1; pagepos:=1; End;
       With editor Do With editcoord Do Begin stx:=x+10; sty:=y+10; End;
  End;
  pedit_interf^.openfile;
  pedit_interf^.run;
  pedit_interf^.savefile;
  dispose(pedit_interf,done);
End;

(*---------------------------------------*)
(*          ЏЂ‹€’ђЂ ђ…„ЂЉ’ЋђЂ            *)
(*---------------------------------------*)
Constructor edit_palete.init;
Begin
  titlestring:='Text edit system.';
  titlestringcolor:=yellow;
  editbodycolor:=white;
  editframecolor:=darkgray;
  titlecolor:=blue;
  framecolor:=red;
  bodycolor:=lightgray;
End;

Procedure edit_palete.restruct;
Begin
  titlestring:='Text edit system.';
  titlestringcolor:=black;
  editbodycolor:=black;
  editframecolor:=black;
  titlecolor:=black;
  framecolor:=black;
  bodycolor:=black;
End;

Destructor edit_palete.done;
Begin
  titlestring:='';
  editbodycolor:=0;
  editframecolor:=0;
  titlecolor:=0;
  framecolor:=0;
  bodycolor:=0;
End;

(*---------------------------------------*)
(*          ЊЋ„“‹њ €Ќ’…ђ”…‰‘Ђ            *)
(*---------------------------------------*)
Constructor edit_interf.init;
Var e_ch:char;
Begin
  pal.init;
  With coord Do Begin
       x:=maxx Div 5;    y:=maxy Div 5;
       x1:=maxx-x-20;    y1:=maxy-y;
  End;
  With titlecoord Do Begin
       x:=maxx Div 5;    y:=maxy Div 5;
       x1:=maxx-x-20;    y1:=y+15;
  End;
  With editcoord Do Begin
       x:=maxx Div 5;    y:=maxy Div 5;
       x1:=maxx-x-20;    y1:=maxy-y;
       inc(x,5); dec(x1,5); inc(y,40);
       dec(y1,20);
  End;
  buildbody; e_ch:=' ';
End;

Procedure edit_interf.buildbody;
Begin
  setcolor(pal.framecolor);
  setfillstyle(solidfill,pal.bodycolor);
  With coord Do Begin
       rectangle(x,y,x1,y1);
       line(x+2,y-2,x1+2,y-2);
       line(x1+2,y-2,x1+2,y1-2);
       bar(x+1,y+1,x1-1,y1-1);
  End;
  setfillstyle(solidfill,pal.titlecolor);
  With titlecoord Do bar(x+2,y+2,x1-2,y1-2);
  setfillstyle(solidfill,pal.editbodycolor);
  setcolor(pal.editframecolor);
  With editcoord Do Begin
       bar(x+1,y+1,x1-1,y1-1);
       rectangle(x,y,x1,y1);
  End;
  With coord Do Begin
       setcolor(pal.titlestringcolor);
       settextstyle(smallfont,horizdir,4);
       outtextxy(x+5,y,pal.titlestring);
  End;
End;

Procedure edit_interf.run;
Var user_key:char; i:word;
    si_pos,j:byte; s:String;
Begin user_key:=' ';
  While ord(user_key)<>27 Do Begin
        printhelp;
        user_key:=readkey;
        If ord(user_key)=4 Then savefile;
        If ord(user_key)=15 Then Begin savefile; openfile; End;
        With editor Do Begin
             si_pos:=i_pos;
             setfillstyle(solidfill,white);
             With editcoord Do bar(x+1,y+1,x1-1,y1-1);
             If (ord(user_key)=13) Then Begin
                s:=''; inc(i_pos); j_pos:=1;
                For j:=1 To maxj_ed Do s:=s+' ';
                If d_[maxi_ed]=s Then Begin
                   For i:=maxi_ed Downto i_pos+1 Do
                       d_[i]:=d_[i-1];
                   For j:=1 To maxj_ed Do d_[i_pos,j]:=s[j];
                End;
             End;
             If (ord(user_key)=6) Then Begin
                s:=''; For j:=1 To maxj_ed Do s:=s+' ';
                For i:=i_pos To maxi_ed-1 Do d_[i]:=d_[i+1];
                For j:=1 To maxj_ed Do d_[maxi_ed,j]:=s[j];
                j_pos:=1;
             End;
             If (ord(user_key)=8) Then Begin
                For j:=j_pos-1 To maxj_ed-1 Do d_[i_pos,j]:=d_[i_pos,j+1];
                If j_pos>1 Then dec(j_pos);
             End;
             If (ord(user_key)=22) Then dec(i_pos);
             If (ord(user_key)=16) Then inc(i_pos);
             If (ord(user_key)=19) Then inc(j_pos);
             If (ord(user_key)=23) Then dec(j_pos);
             If (j_pos<=0) Then Begin j_pos:=1; dec(i_pos); End;
             If (j_pos>=maxj_ed) Then Begin j_pos:=1; inc(i_pos); End;
             If (i_pos<=0) Then i_pos:=1;
             If (i_pos>=maxi_ed) Then i_pos:=1;
             If (ord(user_key)>=32) And (ord(user_key)<=126) Then
                insert_chr(user_key);
             printpage;
        End;
  End;
End;

Procedure edit_interf.printhelp;
Var x,y,x1,y1:integer;
Begin
  setcolor(pal.framecolor);
  setfillstyle(solidfill,pal.bodycolor);
  settextstyle(smallfont,horizdir,4);
  x:=maxx Div 20; x1:=x+85;
  y:=maxy Div 5;  y1:=maxy-y;
  rectangle(x,y,x1,y1);
  bar(x+1,y+1,x1-1,y1-1);
  inc(x,10);
  inc(y,10); outtextxy(x,y,'help:');
  inc(y,10); outtextxy(x,y,'alt+u=up');
  inc(y,10); outtextxy(x,y,'alt+q=down');
  inc(y,10); outtextxy(x,y,'alt+r=right');
  inc(y,10); outtextxy(x,y,'alt+i=left');
  inc(y,10); outtextxy(x,y,'ctrl+del=');
  inc(y,10); outtextxy(x,y,'=del_string');
  inc(y,10); outtextxy(x,y,'<=back_space');
  inc(y,10); outtextxy(x,y,'ctrl+d=save');
  inc(y,10); outtextxy(x,y,'ctrl+o=open');
  inc(y,10); outtextxy(x,y,'esc-exit');
  inc(y,15); line(x-10,y,x1,y);
  dec(x,9); inc(y); dec(x1); dec(y1);
  bar(x,y,x1,y1);
End;

Procedure edit_interf.savefile;
Var x,y,x1,y1:integer; x_,y_:integer;
    ch:char; s:String;
    t:text;  i,j:word;
Begin
  setcolor(pal.framecolor);
  setfillstyle(solidfill,pal.bodycolor);
  settextstyle(smallfont,horizdir,4);
  x:=maxx Div 20; x1:=x+85;
  y:=maxy Div 5;  y1:=maxy-y;
  rectangle(x,y,x1,y1);
  bar(x+1,y+1,x1-1,y1-1); inc(x,10); inc(y,125);
  dec(x,9); inc(y); dec(x1); dec(y1);
  bar(x,y,x1,y1); inc(x,9); inc(y,10);
  outtextxy(x,y,'Do you want'); inc(y,10);
  outtextxy(x,y,'to save');     inc(y,10);
  outtextxy(x,y,'file?');       inc(y,10);
  outtextxy(x,y,'Y-yes.');      inc(y,10);
  outtextxy(x,y,'N-not.');      inc(y,10); ch:=' ';
  While (ch<>'Y') And (ch<>'N') Do ch:=readkey;
  If ch='Y' Then Begin
     s:=''; outtextxy(x,y,'Input name.');  inc(y,10);
     x_:=x; y_:=y;
     While (ord(ch)<>13) And (ord(ch)<>27) Do Begin
           ch:=readkey;
           If (ord(ch)>=32) And (ord(ch)<=126) Then s:=s+ch;
           inc(x_,7); outtextxy(x_,y_,ch);
           If x_-x>70 Then Begin inc(y,10); x:=x_; End;
     End;
     If pos('.',s)=0 Then s:=s+'.w_t';
     assign(t,s); rewrite(t);
     For i:=1 To maxi_ed Do Begin
         For j:=1 To maxj_ed Do write(t,editor.d_[i,j]);
         writeln(t);
     End;
     close(t);
  End;
End;

Procedure edit_interf.openfile;
Var x,y,x1,y1:integer; x_,y_:integer;
    ch:char; s:String;
    t:text;  i,j:word;
Begin
  setcolor(pal.framecolor);
  setfillstyle(solidfill,pal.bodycolor);
  settextstyle(smallfont,horizdir,4);
  x:=maxx Div 20; x1:=x+85;
  y:=maxy Div 5;  y1:=maxy-y;
  rectangle(x,y,x1,y1);
  bar(x+1,y+1,x1-1,y1-1); inc(x,10); inc(y,125);
  dec(x,9); inc(y); dec(x1); dec(y1);
  bar(x,y,x1,y1); inc(x,9); inc(y,10);
  outtextxy(x,y,'Do you want'); inc(y,10);
  outtextxy(x,y,'to open');     inc(y,10);
  outtextxy(x,y,'file?');       inc(y,10);
  outtextxy(x,y,'Y-yes.');      inc(y,10);
  outtextxy(x,y,'N-not.');      inc(y,10); ch:=' ';
  While (ch<>'Y') And (ch<>'N') Do ch:=readkey;
  If ch='Y' Then Begin
     s:=''; outtextxy(x,y,'Input name.');  inc(y,10);
     x_:=x; y_:=y;
     While (ord(ch)<>13) And (ord(ch)<>27) Do Begin
           ch:=readkey;
           If (ord(ch)>=32) And (ord(ch)<=126) Then s:=s+ch;
           inc(x_,7); outtextxy(x_,y_,ch);
           If x_-x>70 Then Begin inc(y,10); x:=x_; End;
     End;
     If pos('.',s)=0 Then s:=s+'.w_t';
     assign(t,s); reset(t);
     For i:=1 To maxi_ed Do For j:=1 To maxj_ed Do editor.d_[i,j]:=' ';
     i:=0;
     While Not eof(t) Do Begin
           inc(i); readln(t,s);
           For j:=1 To maxj_ed Do editor.d_[i,j]:=s[j];
     End;
     close(t);
  End;
End;

Destructor edit_interf.done;
Begin
  pal.restruct;
  printhelp;
  buildbody;
  pal.done;
End;

(*------------------------------------*)
(*            ‘ЂЊ ђ…„ЂЉ’Ћђ            *)
(*------------------------------------*)
Procedure edit_editor.insert_chr(Var ch:char);
Var j:byte;
Begin
  If d_[i_pos,maxj_ed]<>' ' Then Begin
     d_[i_pos,j_pos]:=ch; inc(j_pos);
     If j_pos>maxj_ed Then Begin j_pos:=1; inc(i_pos); End;
  End Else Begin
     For j:=maxj_ed Downto j_pos Do d_[i_pos,j]:=d_[i_pos,j-1];
     d_[i_pos,j_pos]:=ch; inc(j_pos);
  End;
End;

Procedure edit_editor.printpage;
Var i,j:word;
    x,y:integer;
Begin
  x:=stx; y:=sty;
  setcolor(red);
  settextstyle(smallfont,horizdir,4);
  If i_pos>=20 Then
  For i:=i_pos-19 To i_pos Do Begin
      y:=y+10; x:=stx;
      For j:=1 To maxj_ed Do Begin
          x:=x+7;
          outtextxy(x,y,d_[i,j]);
          If (i=i_pos) And (j=j_pos) Then Begin
             setcolor(red); line(x-3,y+11,x+3,y+11);
          End;
      End;
  End;
  If i_pos<20 Then
  For i:=1 To 19 Do Begin
      y:=y+10; x:=stx;
      For j:=1 To maxj_ed Do Begin
          x:=x+7;
          outtextxy(x,y,d_[i,j]);
          If (i=i_pos) And (j=j_pos) Then Begin
             setcolor(red); line(x-3,y+11,x+3,y+11);
          End;
      End;
  End;
End;

Procedure edit_editor.init_d;
Var i,j:word;
Begin
  For i:=1 To maxi_ed Do For j:=1 To maxj_ed Do d_[i,j]:=' ';
  i_pos:=1; j_pos:=1;
End;

End.