home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TPEDIT.ZIP / EDITOR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-06-10  |  20.8 KB  |  607 lines

  1. Unit Editor;
  2.  
  3. { Copyright (c) 1987, 1988 - COMPUsystems N.W. }
  4.  
  5. Interface
  6.  
  7. Uses
  8.   CRT;
  9.  
  10. Type
  11.   InpDate    = String[8];
  12.   InpString  = String[80];
  13.   InpInteger = String[10];
  14.   InpReal    = String[28];
  15.   Str80      = String[80];
  16.   Str1       = String[1];
  17.  
  18. Var
  19.   FieldNo    : LongInt;
  20.   LastField  : LongInt;
  21.   Escape     : Boolean;
  22.  
  23. Procedure Color(Foreground,Background : Byte);
  24. Procedure Beep;
  25. Procedure EditDate(Var DS            : InpDate;
  26.                    X,Y,FG,BG         : Byte);
  27. Procedure EditString(Var S           : InpString;
  28.                      L,X,Y,FG,BG     : Byte;
  29.                      Picture         : Str80);
  30. Procedure EditInt(Var I              : LongInt;
  31.                   L,X,Y,FG,BG        : Byte);
  32. Procedure EditReal(Var R             : Real;
  33.                    L,X,Y,DecPl,FG,BG : Byte);
  34. Procedure EditChoice(Var C           : Str1;
  35.                      X,Y             : Byte;
  36.                      Choice1,Choice2 : Char;
  37.                      FG,BG           : Byte);
  38.  
  39. Implementation
  40.  
  41. Procedure Color(Foreground,Background : Byte);
  42. Begin
  43.   TextColor(Foreground);
  44.   TextBackground(Background);
  45. End;
  46.  
  47. Procedure InverseColor(Foreground,Background : Byte);
  48. Begin
  49.   If Foreground < 8 then Foreground := Foreground + 8;
  50.   If Background > 8 then Background := Background - 8;
  51.   TextColor(Background);
  52.   TextBackground(Foreground);
  53. End;
  54.  
  55. Procedure Beep;
  56. Begin
  57.   Sound(400); Delay(100);
  58.   Sound(200); Delay(100);
  59.   NoSound;
  60. End;
  61.  
  62. Procedure EditDate(Var DS    : InpDate;
  63.                    X,Y,FG,BG : Byte);
  64. Var
  65.   Mo,Da,Yr  : String[2];
  66.   Done      : Boolean;
  67.   Ch        : Char;
  68.   Postn       : Byte;
  69. Begin
  70.   gotoXY(X,Y);
  71.   InverseColor(FG,BG);
  72.   Escape := False;
  73.   If DS = '' then
  74.   Begin
  75.     DS := '  /  /  ';
  76.     Write(DS);
  77.   End else Write(DS);
  78.   gotoXY(X,Y); Postn := X;
  79.   Done := False;
  80.   Repeat
  81.     Ch := ReadKey;
  82.     Case Ch of
  83.       '0'..'9' : Begin
  84.                    Write(Ch);
  85.                    DS[(Postn+1)-X] := Ch;
  86.                    Postn := Postn + 1;
  87.                    If (Postn = X+2) or (Postn = X+5) then Postn := Postn + 1;
  88.                    gotoXY(Postn,Y);
  89.                    If Postn >= X + 8 then
  90.                    Begin
  91.                      Done := True;
  92.                      Inc(FieldNo);
  93.                    End;
  94.                  End;
  95.            #13 : Begin
  96.                    Done := True;
  97.                    Inc(FieldNo);
  98.                  End;
  99.            #27 : Begin
  100.                    Escape := True;
  101.                    FieldNo := LastField + 1;
  102.                  End;
  103.             #0 : Begin
  104.                    Ch := ReadKey;
  105.                    Case Ch of
  106.                          #71 : Begin               { Home }
  107.                                  Postn := X;
  108.                                  gotoXY(Postn,Y);
  109.                                End;
  110.                          #72 : Begin               { Up Arrow }
  111.                                  Done := True;
  112.                                  Dec(FieldNo);
  113.                                  If FieldNo < 1 then FieldNo := 1;
  114.                                End;
  115.                          #80 : Begin               { Down Arrow }
  116.                                  Done := True;
  117.                                  Inc(FieldNo);
  118.                                  If FieldNo > LastField then
  119.                                    FieldNo := LastField;
  120.                                End;
  121.                          #73 : Begin               { PgUp }
  122.                                  Done := True;
  123.                                  FieldNo := 1;
  124.                                End;
  125.                          #81 : Begin               { PgUp }
  126.                                  Done := True;
  127.                                  FieldNo := LastField;
  128.                                End;
  129.                          #75 : Begin               { Left arrow }
  130.                                  Postn := Postn - 1;
  131.                                  If (Postn = X+2) or (Postn = X+5) then
  132.                                    Postn := Postn - 1;
  133.                                  If Postn < X  then Postn := X;
  134.                                  gotoXY(Postn,Y);
  135.                                End;
  136.                          #77 : Begin               { Right arrow }
  137.                                  Postn := Postn + 1;
  138.                                  If (Postn = X+2) or (Postn = X+5) then
  139.                                    Postn := Postn + 1;
  140.                                  If Postn > X + 7  then Postn := X + 7;
  141.                                  gotoXY(Postn,Y);
  142.                                End;
  143.                    End; { Case }
  144.                  End; { Ch = #0 }
  145.     End; { Case }
  146.   Until (Done) or (Escape);
  147.   Color(FG,BG);
  148.   gotoXY(X,Y); Write(DS);
  149. End;
  150.  
  151. Procedure EditString(Var S       : InpString;
  152.                      L,X,Y,FG,BG : Byte;
  153.                      Picture     : Str80);
  154. Var
  155.   InsFlag,
  156.   Done    : Boolean;
  157.   Postn     : Byte;
  158.   Ch      : Char;
  159. Begin
  160.   Done := False;
  161.   InsFlag := False;
  162.   Escape := False;
  163.   gotoXY(X,Y);
  164.   InverseColor(FG,BG);
  165.   While Length(S) < L do S := S + ' ';
  166.   gotoXY(X,Y);
  167.   Write(S);
  168.   gotoXY(X,Y);
  169.   Postn := X;
  170.   Repeat
  171.     Ch := ReadKey;
  172.     Case Ch of
  173.       #32..#126 : Begin
  174.                     If InsFlag then
  175.                     Begin
  176.                       If Picture[Postn+1-X] = 'U' then Ch := Upcase(Ch);
  177.                       Write(Ch);
  178.                       Insert(Ch,S,(Postn+1)-X);
  179.                       S[0] := Chr(L);
  180.                       gotoXY(X,Y); Write(S);
  181.                     End else
  182.                     Begin
  183.                       If Picture[Postn+1-X] = 'U' then Ch := Upcase(Ch);
  184.                       Write(Ch);
  185.                       S[(Postn+1)-X] := Ch;
  186.                     End;
  187.                     Inc(Postn);
  188.                     If (Picture[Postn-X] = '#') and not (Ch in['0'..'9']) then
  189.                     Begin
  190.                       Dec(Postn);
  191.                       S[Postn+1-X] := ' ';
  192.                       gotoXY(X,Y); Write(S);
  193.                       Beep;
  194.                     End;
  195.                     If Picture[Postn+1-X] = '*' then Inc(Postn);
  196.                     gotoXY(Postn,Y);
  197.                     If Postn >= X + L then
  198.                     Begin
  199.                       Done := True;
  200.                       Inc(FieldNo);
  201.                     End;
  202.                   End;
  203.             #13 : Begin
  204.                     Done := True;
  205.                     Inc(FieldNo);
  206.                   End;
  207.             #27 : Begin
  208.                     Escape := True;
  209.                     FieldNo := LastField + 1;
  210.                   End;
  211.              #8 : Begin                        { Destructive Backspace }
  212.                     If Pos('-',Picture) = 0 then
  213.                     Begin
  214.                       Dec(Postn);
  215.                       If Postn < X then Postn := X;
  216.                       Delete(S,(Postn+1)-X,1);
  217.                       S := S + ' ';
  218.                       gotoXY(X,Y); Write(S);
  219.                       gotoXY(Postn,Y);
  220.                     End;
  221.                   End;
  222.              #0 : Begin
  223.                     Ch := ReadKey;
  224.                     Case Ch of
  225.                       #71 : Begin          { Home }
  226.                               Postn := X;
  227.                               gotoXY(Postn,Y);
  228.                             End;
  229.                       #72 : Begin               { Up Arrow }
  230.                               Done := True;
  231.                               Dec(FieldNo);
  232.                               If FieldNo < 1 then FieldNo := 1;
  233.                             End;
  234.                       #80 : Begin               { Down Arrow }
  235.                               Done := True;
  236.                               Inc(FieldNo);
  237.                               If FieldNo > LastField then
  238.                                 FieldNo := LastField;
  239.                             End;
  240.                       #73 : Begin               { PgUp }
  241.                               Done := True;
  242.                               FieldNo := 1;
  243.                             End;
  244.                       #81 : Begin               { PgUp }
  245.                               Done := True;
  246.                               FieldNo := LastField;
  247.                             End;
  248.                       #75 : Begin          { Left arrow }
  249.                               Dec(Postn);
  250.                               If Picture[Postn+1-X] = '*' then Dec(Postn);
  251.                               If Postn < X  then Postn := X;
  252.                               gotoXY(Postn,Y);
  253.                             End;
  254.                       #77 : Begin          { Right arrow }
  255.                               Inc(Postn);
  256.                               If Picture[Postn+1-X] = '*' then Inc(Postn);
  257.                               If Postn >= X + L-1  then Postn := X + L-1;
  258.                               gotoXY(Postn,Y);
  259.                             End;
  260.                       #82 : Begin          { Toggle Insert }
  261.                               If Pos('*',Picture) = 0 then
  262.                               If not InsFlag then InsFlag := True
  263.                                 else InsFlag := False;
  264.                             End;
  265.                       #83 : Begin          { Del }
  266.                               If Pos('*',Picture) = 0 then
  267.                               Begin
  268.                                 Delete(S,(Postn+1)-X,1);
  269.                                 S := S + ' ';
  270.                                 gotoXY(X,Y); Write(S);
  271.                                 gotoXY(Postn,Y);
  272.                               End;
  273.                             End;
  274.                     End; { Case }
  275.                   End; { Ch = #0 }
  276.     End; { Case }
  277.   Until (Done) or (Escape);
  278.   Color(FG,BG);
  279.   gotoXY(X,Y); Write(S);
  280.   While S[Length(S)] = ' ' do Delete(S,Length(S),1)
  281. End;
  282.  
  283. Function IntToStr(I : LongInt; Len : Byte) : InpInteger;
  284. Var
  285.   IntString : InpInteger;
  286. Begin
  287.   Str(I:Len,IntString);
  288.   IntToStr := IntString;
  289. End;
  290.  
  291. Function StrToInt(IStr : InpInteger) : LongInt;
  292. Var
  293.   Code      : Integer;
  294.   StringInt : LongInt;
  295. Begin
  296.   While IStr[1] = ' ' do Delete(IStr,1,1);
  297.   Val(IStr,StringInt,Code);
  298.   StrToInt := StringInt;
  299. End;
  300.  
  301. Procedure EditInt(Var I       : LongInt;
  302.                   L,X,Y,FG,BG : Byte);
  303. Var
  304.   Done    : Boolean;
  305.   Postn     : Byte;
  306.   Ch      : Char;
  307.   IInt    : InpInteger;
  308. Begin
  309.   Done := False;
  310.   Escape := False;
  311.   gotoXY(X,Y);
  312.   InverseColor(FG,BG);
  313.   IInt := IntToStr(I,L);
  314.   Write(IInt);
  315.   gotoXY(X,Y);
  316.   Postn := X + L;
  317.   gotoXY(Postn-1,Y);
  318.   Repeat
  319.     Ch := ReadKey;
  320.     Case Ch of
  321.      '-','0'..'9' : Begin
  322.                       IInt := IInt + Ch;
  323.                       While (IInt[1] = ' ') or (IInt[1] = '0')
  324.                         do Delete(IInt,1,1);
  325.                       If Length(IInt) = L then
  326.                       Begin
  327.                         Done := True;
  328.                         Inc(FieldNo);
  329.                       End;
  330.                       While Length(IInt) < L do IInt := ' ' + IInt;
  331.                       gotoXY(X,Y); Write(IInt);
  332.                       gotoXY(Postn-1,Y);
  333.                     End;
  334.               #13 : Begin
  335.                       Done := True;
  336.                       Inc(FieldNo);
  337.                     End;
  338.               #27 : Begin
  339.                       Escape := True;
  340.                       FieldNo := LastField + 1;
  341.                     End;
  342.                #8 : Begin
  343.                       Delete(IInt,Length(IInt),1);
  344.                       While Length(IInt) < L do IInt := ' ' + IInt;
  345.                       gotoXY(X,Y); Write(IInt);
  346.                       gotoXY(Postn-1,Y);
  347.                     End;
  348.                #0 : Begin
  349.                       Ch := ReadKey;
  350.                       Case Ch of
  351.                         #83 : Begin
  352.                                 Delete(IInt,Length(IInt),1);
  353.                                 While Length(IInt) < L do IInt := ' ' + IInt;
  354.                                 gotoXY(X,Y); Write(IInt);
  355.                                 gotoXY(Postn-1,Y);
  356.                               End;
  357.                         #72 : Begin               { Up Arrow }
  358.                                 Done := True;
  359.                                 Dec(FieldNo);
  360.                                 If FieldNo < 1 then FieldNo := 1;
  361.                               End;
  362.                         #80 : Begin               { Down Arrow }
  363.                                 Done := True;
  364.                                 Inc(FieldNo);
  365.                                 If FieldNo > LastField then
  366.                                   FieldNo := LastField;
  367.                               End;
  368.                         #73 : Begin               { PgUp }
  369.                                 Done := True;
  370.                                 FieldNo := 1;
  371.                               End;
  372.                         #81 : Begin               { PgUp }
  373.                                 Done := True;
  374.                                 FieldNo := LastField;
  375.                               End;
  376.                       End; { Case }
  377.                     End;
  378.     End; { Case }
  379.   Until (Done) or (Escape);
  380.   Color(FG,BG);
  381.   If IInt[Length(IInt)] = ' ' then IInt[Length(IInt)] := '0';
  382.   gotoXY(X,Y); Write(IInt);
  383.   I := StrToInt(IInt);
  384. End;
  385.  
  386. Function RealToStr(I : Real; L,DecPl : Byte) : InpReal;
  387. Var
  388.   StringReal : InpReal;
  389. Begin
  390.   Str(I:L:DecPl,StringReal);
  391.   RealToStr := StringReal;
  392. End;
  393.  
  394. Function StrToReal(RealStr : InpReal) : Real;
  395. Var
  396.   Code : Integer;
  397.   RealString : Real;
  398. Begin
  399.   While RealStr[1] = ' ' do Delete(RealStr,1,1);
  400.   Val(RealStr,RealString,Code);
  401.   StrToReal := RealString;
  402. End;
  403.  
  404. Procedure EditReal(Var R             : Real;
  405.                    L,X,Y,DecPl,FG,BG : Byte);
  406. Var
  407.   DecFlag,
  408.   Done      : Boolean;
  409.   Postn,Loc : Byte;
  410.   Ch        : Char;
  411.   IntPart   : InpInteger;
  412.   DecPart   : InpReal;
  413.   IReal     : InpReal;
  414. Begin
  415.   Done := False;
  416.   DecFlag := False;
  417.   Escape := False;
  418.   IReal := RealToStr(R,L,DecPl);
  419.   IntPart := Copy(IReal,1,L-(DecPl+1));
  420.   DecPart := Copy(IReal,L-DecPl+1,DecPl);
  421.   InverseColor(FG,BG);
  422.   gotoXY(X,Y); Write(IReal);
  423.   Postn := (X+L) - (DecPl+2);
  424.   gotoXY(Postn,Y);
  425.   Repeat
  426.     Ch := ReadKey;
  427.     Case Ch of
  428.               #46 : Begin
  429.                       If DecFlag then
  430.                       Begin
  431.                         DecFlag := False;
  432.                         Postn := (X+L) - (DecPl+2);
  433.                         gotoXY(Postn,Y);
  434.                       End;
  435.                       If not DecFlag then
  436.                       Begin
  437.                         DecFlag := True;
  438.                         Loc := 1;
  439.                         Postn := (X+L) - (DecPl);
  440.                         gotoXY(Postn,Y);
  441.                       End;
  442.                     End;
  443.      '-','0'..'9' : Begin
  444.                       If not DecFlag then
  445.                       Begin
  446.                         IntPart := IntPart + Ch;
  447.                         While (IntPart[1] = ' ') or (IntPart[1] = '0')
  448.                           do Delete(IntPart,1,1);
  449.                         If Length(IntPart) = L - (DecPl+1) then
  450.                         Begin
  451.                           DecFlag := True;
  452.                           Loc := 1;
  453.                           Postn := (X+L) - (DecPl);
  454.                           gotoXY(Postn,Y);
  455.                         End;
  456.                         While Length(IntPart) < L - (DecPl+1)
  457.                           do IntPart := ' ' + IntPart;
  458.                         gotoXY(X,Y); Write(IntPart);
  459.                         gotoXY(Postn,Y);
  460.                       End else
  461.                       Begin
  462.                         DecPart[Loc] := Ch;
  463.                         gotoXY(Postn,Y); Write(Copy(DecPart,1,DecPl));
  464.                         gotoXY(Postn,Y);
  465.                         Inc(Loc);
  466.                         If DecPart[DecPl] > '0' then
  467.                         Begin
  468.                           Done := True;
  469.                           Inc(FieldNo);
  470.                         End;
  471.                       End;
  472.                     End;
  473.               #13 : Begin
  474.                       Done := True;
  475.                       Inc(FieldNo);
  476.                     End;
  477.               #27 : Begin
  478.                       Escape := True;
  479.                       FieldNo := LastField + 1;
  480.                     End;
  481.                #8 : Begin
  482.                       If not DecFlag then
  483.                       Begin
  484.                         Delete(IntPart,Length(IntPart),1);
  485.                         While Length(IntPart) < L-DecPl-1
  486.                           do IntPart := ' ' + IntPart;
  487.                         gotoXY(X,Y); Write(IntPart);
  488.                         gotoXY(Postn,Y);
  489.                       End else
  490.                       Begin
  491.                         Delete(DecPart,1,1);
  492.                         DecPart := DecPart + '0';
  493.                         gotoXY(X+L-DecPl,Y);
  494.                         Write(DecPart);
  495.                         gotoXY(Postn,Y);
  496.                       End;
  497.                     End;
  498.                #0 : Begin
  499.                       Ch := ReadKey;
  500.                       Case Ch of
  501.                         #83 : Begin
  502.                                 If not DecFlag then
  503.                                 Begin
  504.                                   Delete(IntPart,Length(IntPart),1);
  505.                                   While Length(IntPart) < L-DecPl-1
  506.                                     do IntPart := ' ' + IntPart;
  507.                                   gotoXY(X,Y); Write(IntPart);
  508.                                   gotoXY(Postn,Y);
  509.                                 End else
  510.                                 Begin
  511.                                   Delete(DecPart,1,1);
  512.                                   DecPart := DecPart + '0';
  513.                                   gotoXY(X+L-DecPl,Y);
  514.                                   Write(DecPart);
  515.                                   gotoXY(Postn,Y);
  516.                                 End;
  517.                               End;
  518.                         #72 : Begin               { Up Arrow }
  519.                                 Done := True;
  520.                                 Dec(FieldNo);
  521.                                 If FieldNo < 1 then FieldNo := 1;
  522.                               End;
  523.                         #80 : Begin               { Down Arrow }
  524.                                 Done := True;
  525.                                 Inc(FieldNo);
  526.                                 If FieldNo > LastField then
  527.                                   FieldNo := LastField;
  528.                               End;
  529.                         #73 : Begin               { PgUp }
  530.                                 Done := True;
  531.                                 FieldNo := 1;
  532.                               End;
  533.                         #81 : Begin               { PgUp }
  534.                                 Done := True;
  535.                                 FieldNo := LastField;
  536.                               End;
  537.                       End; { Case }
  538.                     End;
  539.     End; { Case }
  540.   Until (Done) or (Escape);
  541.   Color(FG,BG);
  542.   If IntPart[Length(IntPart)] = ' ' then IntPart[Length(IntPart)] := '0';
  543.   IReal := IntPart + '.' + DecPart;
  544.   gotoXY(X,Y); Write(IReal);
  545.   R := StrToReal(IReal);
  546. End;
  547.  
  548. Procedure EditChoice(Var C            : Str1;
  549.                      X,Y              : Byte;
  550.                      Choice1,Choice2  : Char;
  551.                      FG,BG            : Byte);
  552. Var
  553.   Done : Boolean;
  554.   Ch   : Char;
  555. Begin
  556.   Done := False;
  557.   Escape := False;
  558.   gotoXY(X,Y);
  559.   InverseColor(FG,BG);
  560.   Repeat
  561.     Ch := Upcase(Readkey);
  562.     If Ch in[Choice1,Choice2] then
  563.     Begin
  564.       Done := True;
  565.       Inc(FieldNo);
  566.     End else
  567.     Begin
  568.       Case Ch of
  569.        #27 : Begin
  570.                Escape := True;
  571.                FieldNo := LastField + 1;
  572.              End;
  573.         #0 : Begin
  574.                Ch := ReadKey;
  575.                Case Ch of
  576.                  #72 : Begin               { Up Arrow }
  577.                          Done := True;
  578.                          Dec(FieldNo);
  579.                          If FieldNo < 1 then FieldNo := 1;
  580.                        End;
  581.                  #80 : Begin               { Down Arrow }
  582.                          Done := True;
  583.                          Inc(FieldNo);
  584.                          If FieldNo > LastField then
  585.                            FieldNo := LastField;
  586.                        End;
  587.                  #73 : Begin               { PgUp }
  588.                          Done := True;
  589.                          FieldNo := 1;
  590.                        End;
  591.                  #81 : Begin               { PgUp }
  592.                          Done := True;
  593.                          FieldNo := LastField;
  594.                        End;
  595.                End; { Case }
  596.              End; { Ch = #0 }
  597.       End; { Case }
  598.     End;
  599.     If not Done then Beep;
  600.   Until (Done) or (Escape);
  601.   C := Ch;
  602.   Color(FG,BG);
  603.   gotoXY(X,Y); Write(C);
  604. End;
  605.  
  606. End.
  607.