home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / tptools.zip / BINED.ZIP / DEMO1.PAS < prev    next >
Pascal/Delphi Source File  |  1987-12-21  |  10KB  |  312 lines

  1. {                          DEMO1.PAS
  2.              Copyright (c) 1985, 87 by Borland International, Inc.            }
  3.  
  4. program Demo1;
  5.  
  6. uses
  7.   bined,
  8.   dos,
  9.   crt;
  10.  
  11. const
  12.   {Commands other than ^K^D to exit editor}
  13.   ExitCommands : array[0..9] of Char = (#2, ^K, ^Q, #2, #0, #68, #2, ^K, ^S, #0);
  14.   MakeBackup = True;
  15.  
  16.   {***************************************************************}
  17.   {****************** demonstration follows **********************}
  18.   {***************************************************************}
  19.   {* This demonstration shows the use of one editor window which *}
  20.   {****** can be sized and shifted during an edit session. *******}
  21.   {***************************************************************}
  22.  
  23. const
  24.   {Initial Coordinates of the editor window}
  25.   Windx1 = 11;
  26.   Windy1 = 6;
  27.   Windx2 = 58;
  28.   Windy2 = 20;
  29.  
  30. var
  31.   EdData : EdCB;              {Editor control block}
  32.   ExitCode : Word;            {Status code set by binary editor functions}
  33.   ExitCommand : Word;         {Code for command used to leave editor}
  34.   Fname : String;             {Input name of file being edited}
  35.  
  36. type
  37.   BorderElements = (topleft, topright, botleft, botright, horiz, vert);
  38.   BorderChars = array[BorderElements] of Char;
  39. const
  40.   Border :   BorderChars = '┌┐└┘─│';
  41.   NoBorder : BorderChars = '      ';
  42.  
  43.   {Procedures and functions used as part of the demo}
  44.  
  45.   {$I demo.inc}
  46.  
  47.   function GetFileName : String;
  48.     {-Return a file name either from the command line or a prompt}
  49.   var
  50.     Fname : String;
  51.  
  52.   begin                       {GetFileName}
  53.     if ParamCount > 0 then
  54.       Fname := ParamStr(1)
  55.     else begin
  56.       Write('Enter file name to edit: ');
  57.       ReadLn(Fname);
  58.     end;
  59.     if Fname = '' then
  60.       Halt;
  61.     GetFileName := Fname;
  62.   end;                        {GetFileName}
  63.  
  64.   procedure InitWindow(var EdData : EdCB);
  65.     {-Draw a nice screen frame around the editor window}
  66.   var
  67.     MsgPos : Byte;
  68.     DemoMsg : String;
  69.  
  70.   begin                       {InitWindow}
  71.     {Draw a frame around the editor window}
  72.     with EdData do begin
  73.       DrawBox(Border, x1, y1, x2, y2);
  74.       {Demo line}
  75.       DemoMsg := ' Borland Binary Editor ';
  76.       MsgPos := 2+((X2+X1-Length(DemoMsg)) shr 1);
  77.       CRTputFast(MsgPos, Y2+2, CAerr+DemoMsg);
  78.     end;
  79.   end;                        {InitWindow}
  80.  
  81.   procedure InitStatusLine;
  82.     {-Draw a status/prompt line for the editor demo}
  83.  
  84.   begin                       {InitStatusLine}
  85.     WriteStatus(' ^K^D saves file, ^K^Q abandons, <F10> resizes window');
  86.     {Initialize for the on-screen clock}
  87.     TickMax := 2500; {Empirical: keep clock ticking without excessive overhead}
  88.     TickCount := TickMax;
  89.     LastTime := '';
  90.   end;                        {InitStatusLine}
  91.  
  92.   function ExitBinaryEditor(var EdData : EdCB; ExitCommand : Integer)
  93.     : Boolean;
  94.     {-Handle an editor exit - save or abandon file}
  95.   var
  96.     ExitCode : Word;
  97.  
  98.     procedure ModifyWindow(var EdData : EdCB);
  99.       {-Move or resize editor window interactively}
  100.     var
  101.       ch : Char;
  102.       redraw, done, scroll : Boolean;
  103.       kbflag : Word absolute $0040 : $0017;
  104.       lastkbflag : Word;
  105.  
  106.       procedure UpdateScreen(var EdData : EdCb);
  107.         {-Update the screen after the window is resized}
  108.       var
  109.         junk : Word;
  110.  
  111.       begin                   {UpdateScreen}
  112.         {Redraw the window box}
  113.         InitWindow(EdData);
  114.         if not(keypressed) then begin
  115.           {Force the editor to update the screen and return}
  116.           junk := UseBinaryEditor(EdData, ^K^S);
  117.           redraw := false;
  118.         end;
  119.       end;                    {UpdateScreen}
  120.  
  121.     begin                     {ModifyWindow}
  122.  
  123.       with eddata do begin
  124.  
  125.         {Show a prompt for resizing}
  126.         WriteStatus(^X^Y^Z^[' to move/resize window. <ScrollLock> moves. <Enter> accepts.');
  127.  
  128.         lastkbflag := not(kbflag);
  129.         done := False;
  130.         redraw := False;
  131.  
  132.         repeat
  133.  
  134.           {Update the screen}
  135.           if redraw then
  136.             UpdateScreen(EdData);
  137.  
  138.           {Keep keyboard toggles up to date}
  139.           repeat
  140.             scroll := (kbflag and $10) <> 0;
  141.             if kbflag <> lastkbflag then begin
  142.               WriteKeyboardToggles(kbflag);
  143.               lastkbflag := kbflag;
  144.             end;
  145.           until KeyPressed;
  146.  
  147.           ch := Readkey;
  148.  
  149.           if (ch = #0) and KeyPressed then begin
  150.  
  151.             ch:=Readkey;
  152.  
  153.             case ch of
  154.  
  155.               #75 :           {left arrow}
  156.                 if scroll then begin
  157.                   if X1 > 1 then begin
  158.                     DrawBox(NoBorder, x1, y1, x2, y2);
  159.                     X1 := Pred(X1);
  160.                     X2 := Pred(X2);
  161.                     redraw := true;
  162.                   end;
  163.                 end else if X2 > X1+30 then begin
  164.                   DrawBox(NoBorder, x1, y1, x2, y2);
  165.                   X2 := Pred(X2);
  166.                   redraw := true;
  167.                 end;
  168.  
  169.               #77 :           {right arrow}
  170.                 if scroll then begin
  171.                   if X2 < 78 then begin
  172.                     DrawBox(NoBorder, x1, y1, x2, y2);
  173.                     X1 := Succ(X1);
  174.                     X2 := Succ(X2);
  175.                     redraw := true;
  176.                   end;
  177.                 end else if X2 < 78 then begin
  178.                   DrawBox(NoBorder, x1, y1, x2, y2);
  179.                   X2 := Succ(X2);
  180.                   redraw := true;
  181.                 end;
  182.  
  183.               #72 :           {up arrow}
  184.                 if scroll then begin
  185.                   if Y1 > 1 then begin
  186.                     DrawBox(NoBorder, x1, y1, x2, y2);
  187.                     Y1 := Pred(Y1);
  188.                     Y2 := Pred(Y2);
  189.                     redraw := true;
  190.                   end;
  191.                 end else if Y2 > Y1+3 then begin
  192.                   DrawBox(NoBorder, x1, y1, x2, y2);
  193.                   Y2 := Pred(Y2);
  194.                   redraw := true;
  195.                 end;
  196.  
  197.               #80 :           {down arrow}
  198.                 if scroll then begin
  199.                   if Y2 < 22 then begin
  200.                     DrawBox(NoBorder, x1, y1, x2, y2);
  201.                     Y1 := Succ(Y1);
  202.                     Y2 := Succ(Y2);
  203.                     redraw := true;
  204.                   end;
  205.                 end else if Y2 < 22 then begin
  206.                   DrawBox(NoBorder, x1, y1, x2, y2);
  207.                   Y2 := Succ(Y2);
  208.                   redraw := true;
  209.                 end;
  210.  
  211.             end;
  212.           end else if ch = #13 then
  213.             done := True;
  214.  
  215.         until done;
  216.  
  217.         InitStatusLine;
  218.  
  219.       end;
  220.     end;                      {ModifyWindow}
  221.  
  222.     function YesAnswer(prompt : String) : Boolean;
  223.       {-Return true for a yes answer to the prompt}
  224.     var
  225.       ch : Char;
  226.  
  227.     begin                     {YesAnswer}
  228.       WriteStatus(prompt);
  229.       repeat
  230.         ch:=UpCase(Readkey);
  231.       until ch in ['Y', 'N'];
  232.       YesAnswer := (ch = 'Y');
  233.     end;                      {YesAnswer}
  234.  
  235.   begin                       {ExitBinaryEditor}
  236.     case ExitCommand of
  237.       -1 :                    {^K^D}
  238.         begin
  239.           ExitCode := SaveFileBinaryEditor(EdData, MakeBackup);
  240.           CheckSaveFile(ExitCode, FilenameBinaryEditor(eddata));
  241.           ExitBinaryEditor := True;
  242.           GoToXY(1, 25);
  243.         end;
  244.  
  245.       0 :                     {^K^Q}
  246.         begin
  247.           if ModifiedFileBinaryEditor(EdData) then
  248.             if YesAnswer('File modified. Save it? (Y/N) ') then begin
  249.               ExitCode := SaveFileBinaryEditor(EdData, MakeBackup);
  250.               CheckSaveFile(ExitCode, FilenameBinaryEditor(eddata));
  251.             end;
  252.           ExitBinaryEditor := True;
  253.           GoToXY(1, 25);
  254.         end;
  255.  
  256.       1, 2 :                  {F10 or ^K^S}
  257.         begin
  258.           {Allow moving or resizing the window}
  259.           ModifyWindow(EdData);
  260.           ExitBinaryEditor := False;
  261.         end;
  262.  
  263.     end;
  264.   end;                        {ExitBinaryEditor}
  265.  
  266. begin                         {Demo1}
  267.  
  268.   {Get a file name}
  269.   Fname := GetFileName;
  270.  
  271.   ClrScr;
  272.  
  273.   {Initialize a window for the file}
  274.   ExitCode :=
  275.   InitBinaryEditor(
  276.   EdData,                     {Editor control block, initialized by InitBinaryEditor}
  277.   MaxFileSize,                {Size of data area to reserve for binary editor text buffer, $FFE0 max}
  278.   Windx1,                     {Coordinates of editor window, upper left 1..80}
  279.   Windy1,                     {Coordinates of editor window, upper left 1..25}
  280.   Windx2,                     {Coordinates of editor window, lower right}
  281.   Windy2,                     {Coordinates of editor window, lower right}
  282.   True,                       {True to wait for retrace on color cards}
  283.   EdOptInsert+EdOptIndent,    {Initial editor toggles}
  284.   '.PAS',                     {Default extension for file names}
  285.   ExitCommands,               {Commands to exit editor}
  286.   Addr(UserEventCheck));      {Address of user event handler}
  287.   CheckInitBinary(ExitCode);
  288.  
  289.   {Read the file}
  290.   ExitCode := ReadFileBinaryEditor(EdData, Fname);
  291.   CheckReadFile(ExitCode, FilenameBinaryEditor(eddata));
  292.  
  293.   {Reset the editor for the new file}
  294.   ResetBinaryEditor(EdData);
  295.  
  296.   {Write a status and prompt line}
  297.   InitStatusLine;
  298.  
  299.   repeat
  300.     {Set up the window border and title}
  301.     InitWindow(EdData);
  302.  
  303.     {Edit the file}
  304.     ExitCommand := UseBinaryEditor(EdData, '');
  305.  
  306.     {Handle the exit by saving the file or whatever}
  307.   until ExitBinaryEditor(EdData, ExitCommand);
  308.  
  309.   ClrScr;
  310.  
  311. end.                          {Demo1}
  312.