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

  1. {                          DEMO2.PAS
  2.              Copyright (c) 1985, 87 by Borland International, Inc.            }
  3.  
  4. program Demo2;
  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..6] of Char = (#2, ^K, ^Q, #2, #0, #68, #0);
  14.   MakeBackup = True;
  15.  
  16.   {**************************************************************}
  17.   {****************** demonstration follows *********************}
  18.   {*********                                         ************}
  19.   {*** This demonstration shows the use of two editor windows ***}
  20.   {************* opening on two separate files. *****************}
  21.   {**************************************************************}
  22.  
  23. const
  24.   {Number of windows}
  25.   nwindows = 3;
  26. type
  27.   BorderElements = (topleft, topright, botleft, botright, horiz, vert);
  28.   BorderChars = array[BorderElements] of Char;
  29.   FileArray = array[1..nwindows] of String;
  30.   EdWindowArray = array[1..nwindows] of EdCB;
  31. const
  32.   {Coordinates of the editor windows}
  33.   windpos : array[1..nwindows, 1..4] of Byte =
  34.   ((2, 3, 39, 13), (42, 3, 79, 13), (2, 16, 79, 23));
  35.   {Borders around the windows}
  36.   Border : BorderChars = '┌┐└┘─│';
  37.  
  38. var
  39.   EdWindows : EdWindowArray;  {Array of editor control blocks}
  40.   ExitCode : Word;            {Status code set by binary editor functions}
  41.   ExitCommand : Integer;      {Code for command used to leave editor}
  42.   Fname : FileArray;          {Files being edited}
  43.   CurWindow : Word;           {Current editor window}
  44.  
  45.   {Procedures and functions used as part of the demo}
  46.  
  47.   {$I demo.inc}
  48.  
  49.   procedure GetFileNames(var Fname : FileArray);
  50.     {-Return a file name either from the command line or a prompt}
  51.   var
  52.     i : Word;
  53.  
  54.   begin                       {GetFileName}
  55.     for i := 1 to nwindows do
  56.       if i <= ParamCount then
  57.         Fname[i] := ParamStr(i)
  58.       else
  59.         repeat
  60.           Write('Enter file name #', i, ': ');
  61.           ReadLn(Fname[i]);
  62.         until (Fname[i] <> '');
  63.   end;                        {GetFileName}
  64.  
  65.   procedure InitWindow(var EdData : EdCB);
  66.     {-Draw a nice screen frame around the editor window}
  67.   var
  68.     MsgPos : Byte;
  69.     DemoMsg : String;
  70.  
  71.   begin                       {InitWindow}
  72.     with EdData do begin
  73.       {Draw a frame around the editor window}
  74.       DrawBox(Border, x1, y1, x2, y2);
  75.  
  76.       {Demo line}
  77.       DemoMsg := ' '+FilenameBinaryEditor(eddata)+' ';
  78.       MsgPos := Succ((X2+X1-Length(DemoMsg)) shr 1);
  79.       CRTputFast(MsgPos, Y2+2, CAerr+DemoMsg);
  80.     end;
  81.   end;                        {InitWindow}
  82.  
  83.   procedure InitStatusLine;
  84.     {-Draw a status/prompt line for the editor demo}
  85.  
  86.   begin                       {InitStatusLine}
  87.     WriteStatus(' ^K^D saves file, ^K^Q quits editor, <F10> switches windows');
  88.     {Initialize for the on-screen clock}
  89.     TickMax := 2500;
  90.     TickCount := TickMax;
  91.     LastTime := '';
  92.   end;                        {InitStatusLine}
  93.  
  94.   function ExitBinaryEditor(var EdWindows : EdWindowArray;
  95.                             ExitCommand : Integer;
  96.                             var CurWindow : Word) : Boolean;
  97.     {-Handle an editor exit - save, abandon file, switch windows}
  98.   var
  99.     ExitCode : Word;
  100.     msg : String;
  101.     ch : Char;
  102.  
  103.     function AnyModified : Boolean;
  104.       {-Return true if any window is unsaved}
  105.     var
  106.       modified : Boolean;
  107.       i : Word;
  108.  
  109.     begin                     {AnyModified}
  110.       modified := False;
  111.       for i := 1 to nwindows do
  112.         modified := modified or ModifiedFileBinaryEditor(EdWindows[i]);
  113.       AnyModified := modified;
  114.     end;                      {AnyModified}
  115.  
  116.     procedure SwitchWindows(var CurWindow : Word);
  117.       {-Move to the next window}
  118.  
  119.     begin                     {SwitchWindows}
  120.       CurWindow := Succ(CurWindow);
  121.       if CurWindow > nwindows then
  122.         CurWindow := 1;
  123.     end;                      {SwitchWindows}
  124.  
  125.   begin                       {ExitBinaryEditor}
  126.     case ExitCommand of
  127.       -1 :                    {^K^D}
  128.         begin
  129.           ExitCode := SaveFileBinaryEditor(EdWindows[CurWindow], MakeBackup);
  130.           CheckSaveFile(ExitCode, FilenameBinaryEditor(edwindows[curwindow]));
  131.           if AnyModified then begin
  132.             SwitchWindows(CurWindow);
  133.             ExitBinaryEditor := False;
  134.           end else begin
  135.             ExitBinaryEditor := True;
  136.             GoToXY(1, 25);
  137.           end;
  138.         end;
  139.  
  140.       0 :                     {^K^Q}
  141.         if AnyModified then begin
  142.           WriteStatus(' Changes made. Abandon all windows? (Y/N) ');
  143.           repeat
  144.             ch:=upcase(readkey);
  145.           until ch in ['Y','N'];
  146.           if ch='Y' then begin
  147.             ExitBinaryEditor := True;
  148.             GoToXY(1, 25);
  149.           end else begin
  150.             while KeyPressed do
  151.               ch:=Readkey;
  152.             ExitBinaryEditor := False;
  153.             InitStatusLine;
  154.           end;
  155.         end else begin
  156.           ExitBinaryEditor := True;
  157.           GoToXY(1, 25);
  158.         end;
  159.  
  160.       1 :                     {F10}
  161.         begin
  162.           {Switch to another window}
  163.           SwitchWindows(CurWindow);
  164.           ExitBinaryEditor := False;
  165.         end;
  166.  
  167.     end;
  168.   end;                        {ExitBinaryEditor}
  169.  
  170. begin                         {Demo2}
  171.  
  172.   {Get file names}
  173.   GetFileNames(Fname);
  174.  
  175.   ClrScr;
  176.  
  177.   {Initialize a window for each file}
  178.   for CurWindow := 1 to nwindows do begin
  179.  
  180.     ExitCode :=
  181.     InitBinaryEditor(
  182.     EdWindows[CurWindow],     {Editor control block, initialized by InitBinaryEditor}
  183.     MaxFileSize,              {Size of data area to reserve for binary editor text buffer, $FFE0 max}
  184.     windpos[CurWindow, 1],    {Coordinates of editor window, upper left 1..80}
  185.     windpos[CurWindow, 2],    {Coordinates of editor window, upper left 1..25}
  186.     windpos[CurWindow, 3],    {Coordinates of editor window, lower right}
  187.     windpos[CurWindow, 4],    {Coordinates of editor window, lower right}
  188.     True,                     {True to wait for retrace on color cards}
  189.     EdOptInsert+EdOptIndent,  {Initial editor toggles}
  190.     '.PAS',                   {Default extension for file names}
  191.     ExitCommands,             {Commands to exit editor}
  192.     Addr(UserEventCheck));    {Address of user event handler}
  193.  
  194.     CheckInitBinary(ExitCode);
  195.  
  196.     {Read each file}
  197.     ExitCode := ReadFileBinaryEditor(EdWindows[CurWindow], Fname[CurWindow]);
  198.     CheckReadFile(ExitCode, FilenameBinaryEditor(edwindows[curwindow]));
  199.  
  200.     {Reset the editor for the new file}
  201.     ResetBinaryEditor(EdWindows[CurWindow]);
  202.  
  203.     {Set up the window border and title}
  204.     InitWindow(EdWindows[CurWindow]);
  205.  
  206.     {Enter and exit the editor to draw the screen}
  207.     ExitCommand := UseBinaryEditor(EdWindows[CurWindow], ^K^Q);
  208.  
  209.   end;
  210.  
  211.   {Write a status and prompt line}
  212.   InitStatusLine;
  213.  
  214.   CurWindow := 1;
  215.  
  216.   repeat
  217.  
  218.     {Edit the file}
  219.     ExitCommand := UseBinaryEditor(EdWindows[CurWindow], '');
  220.  
  221.     {Handle the exit by saving the file or switching windows}
  222.   until ExitBinaryEditor(EdWindows, ExitCommand, CurWindow);
  223.  
  224.   ClrScr;
  225.  
  226. end.                          {Demo2}
  227.