home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / bpos2api.zip / CRT.PAS < prev    next >
Pascal/Delphi Source File  |  1993-11-17  |  7KB  |  344 lines

  1. Unit Crt;  {$R-,I-,S-,Q-,T-}
  2.  
  3. Interface
  4.  
  5. Uses
  6.   OS2Def, OS2Subs;
  7.  
  8. Const
  9.   Black         =  0;
  10.   Blue          =  1;
  11.   Green         =  2;
  12.   Cyan          =  3;
  13.   Red           =  4;
  14.   Magenta       =  5;
  15.   Brown         =  6;
  16.   LightGray     =  7;
  17.   DarkGray      =  8;
  18.   LightBlue     =  9;
  19.   LightGreen    = 10;
  20.   LightCyan     = 11;
  21.   LightRed      = 12;
  22.   LightMagenta  = 13;
  23.   Yellow        = 14;
  24.   White         = 15;
  25.   Blink         = 128;
  26.   LastMode      = $07;
  27.  
  28.   BW40     =   0;
  29.   CO40     =   1;
  30.   BW80     =   2;
  31.   CO80     =   3;
  32.   Mono     =   7;
  33.  
  34.   CheckBreak : boolean = true;
  35.  
  36. Var
  37.   TextAttr : Byte;
  38.   WindMin, WindMax : word;
  39.  
  40.   Function KeyPressed : Boolean;
  41.   Function ReadKey : Char;
  42.  
  43.   Procedure ClrScr;
  44.   Procedure GotoXY(x,y : Byte);
  45.   Function WhereX : Byte;
  46.   Function WhereY : Byte;
  47.   Procedure TextMode(Mode : Integer);
  48.   Procedure TextColor(Color : Byte);
  49.   Procedure TextBackground(Color : Byte);
  50.   Procedure LowVideo;
  51.   Procedure NormVideo;
  52.   Procedure HighVideo;
  53.   Procedure Window(X1, Y1, X2, Y2: Byte);
  54.   Procedure ClrEol;
  55.   Procedure DelLine;
  56.  
  57.  
  58.   Procedure Delay(ms : Word);
  59.   Procedure Sound(Hz: Word);
  60.   Procedure NoSound;
  61.  
  62.   Procedure AssignCrt(Var f : Text);
  63.  
  64. Implementation
  65.  
  66. Uses
  67.   Dos, DosProcs;
  68. Const
  69.   ExtKeyChar : Char = #0;
  70.  
  71.   Function KeyPressed : Boolean;
  72.   Var
  73.     KeyInfo : TKbdKeyInfo;
  74.   Begin
  75.     KbdPeek(KeyInfo,0);
  76.     KeyPressed:= (ExtKeyChar <> #0) or ((KeyInfo.fbStatus And $40) <> 0);
  77.   End;
  78.  
  79.   Function ReadKey : Char;
  80.   Var
  81.     KeyInfo : TKbdKeyInfo;
  82.   Begin
  83.     If ExtKeyChar <> #0 then
  84.       Begin
  85.         ReadKey:= ExtKeyChar;
  86.         ExtKeyChar:= #0
  87.       End
  88.     else
  89.       Begin
  90.         KbdCharIn(KeyInfo,0,0);
  91.         If KeyInfo.chChar = #0 then
  92.           ExtKeyChar:= KeyInfo.chScan;
  93.         ReadKey:= KeyInfo.chChar;
  94.       End;
  95.   End;
  96.  
  97.   Procedure ClrScr;
  98.   Var
  99.     Cell : Record
  100.              c,a : Byte;
  101.            End;
  102.   Begin
  103.     Cell.c:= $20;
  104.     Cell.a:= TextAttr;
  105.     VioScrollUp(hi(WindMin),lo(WindMin),hi(WindMax),lo(WindMax),
  106.       hi(WindMax)-hi(WindMin)+1,Cell,0);
  107.     GotoXY(1,1);
  108.   End;
  109.  
  110.   Procedure GotoXY(x,y : Byte);
  111.   Begin
  112.     VioSetCurPos(y - 1 + hi(WindMin), x - 1 + lo(WindMin),0);
  113.   End;
  114.  
  115.   Function WhereX : Byte;
  116.   Var
  117.     x,y : Word;
  118.   Begin
  119.     VioGetCurPos(y,x,0);
  120.     WhereX:= x + 1 - lo(WindMin);
  121.   End;
  122.  
  123.   Function WhereY : Byte;
  124.   Var
  125.     x,y : Word;
  126.   Begin
  127.     VioGetCurPos(y,x,0);
  128.     WhereY:= y + 1 - hi(WindMin);
  129.   End;
  130.  
  131.   Procedure Window(X1, Y1, X2, Y2: Byte);
  132.   type
  133.     WordRec = record lo,hi : byte end;
  134.   begin
  135.     WordRec(WindMin).lo := X1-1;
  136.     WordRec(WindMin).hi := Y1-1;
  137.     WordRec(WindMax).lo := X2-1;
  138.     WordRec(WindMax).hi := Y2-1;
  139.   end;
  140.  
  141.   procedure ClrEol;
  142.   var
  143.     Cell : Record
  144.              c,a : Byte;
  145.            End;
  146.     x,y : word;
  147.   begin
  148.     Cell.c := ord(' ');
  149.     Cell.a := TextAttr;
  150.     VioGetCurPos(y,x,0);
  151.     VioWrtNCell(Cell,lo(WindMax)-x+1,y,x,0);
  152.   end;
  153.  
  154.   Procedure DelLine;
  155.   begin
  156.  
  157.   end;
  158.  
  159.   procedure Sound(Hz: Word);
  160.   begin
  161.   end;
  162.  
  163.   procedure NoSound;
  164.   begin
  165.   end;
  166.  
  167.   Procedure TextMode(Mode : Integer);
  168.   Begin
  169.     TextAttr:= $07;
  170.   End;
  171.  
  172.   Procedure TextColor(Color : Byte);
  173.   Begin
  174.     TextAttr:= (TextAttr And $70) or (Color and $0F) + Ord(Color > $0F) * $80;
  175.   End;
  176.  
  177.   Procedure TextBackground(Color : Byte);
  178.   Begin
  179.     TextAttr:= (TextAttr And $8F) or ((Color And $07) Shl 4);
  180.   End;
  181.  
  182.   Procedure LowVideo;
  183.   Begin
  184.     TextAttr:= TextAttr And $F7;
  185.   End;
  186.  
  187.   Procedure NormVideo;
  188.   Begin
  189.     TextAttr:= $07;
  190.   End;
  191.  
  192.   Procedure HighVideo;
  193.   Begin
  194.     TextAttr:= TextAttr Or $08;
  195.   End;
  196.  
  197.   Procedure Delay(ms : Word);
  198.   Begin
  199.     DosSleep(ms);
  200.   End;
  201.  
  202.   Procedure WritePChar(s : PChar;Len : Word);
  203.   Var
  204.     x,y  : Word;
  205.     c    : Char;
  206.     i    : Integer;
  207.     Cell : Word;
  208.   Begin
  209.     VioGetCurPos(y,x,0);
  210.     Cell := $20 + TextAttr Shl 8;
  211.     i := 0;
  212.     while (i < Len) do begin
  213.       case s[i] of
  214.         #8 : begin
  215.                if x > lo(WindMin) then dec(x)
  216.                else x := WindMax;
  217.              end;
  218.         ^G : ;
  219.         ^M : x := lo(WindMin);
  220.         ^J : inc(y);
  221.       else
  222.         VioWrtCharStrAtt(@s[i],1,y,x,TextAttr,0);
  223.         inc(x);
  224.       end;
  225.       If x > lo(WindMax) then
  226.         Begin
  227.           x := 0; Inc(y);
  228.         End;
  229.       If y > hi(WindMax) then
  230.         Begin
  231.           VioScrollUp(hi(WindMin),lo(WindMin),hi(WindMax),lo(WindMax),
  232.             1,Cell,0);
  233.           y := hi(WindMax);
  234.         End;
  235.       inc(i);
  236.     end;
  237.     VioSetCurPos(y,x,0);
  238.   End;
  239.  
  240.   Function CrtRead(Var f : Text) : Word; Far;
  241.   Var
  242.     Max    : Integer;
  243.     CurPos : Integer;
  244.     c      : Char;
  245.     i      : Integer;
  246.     c1     : Array[0..2] of Char;
  247.   Begin
  248.     With TextRec(f) do
  249.       Begin
  250.         Max:= BufSize - 2;
  251.         CurPos:= 0;
  252.         Repeat
  253.           c:= ReadKey;
  254.           Case c of
  255.          #8 : Begin
  256.                 If CurPos > 0 then
  257.                   Begin
  258.                     c1:= #8' '#8; WritePChar(@c1,3);
  259.                     Dec(CurPos);
  260.                   End;
  261.               End;
  262.          ^M : Begin
  263.                 BufPtr^[CurPos]:= #$0D; Inc(CurPos);
  264.                 BufPtr^[CurPos]:= #$0A; Inc(CurPos);
  265.                 BufPos:= 0;
  266.                 BufEnd:= CurPos;
  267.                 Break;
  268.               End;
  269.   #32..#255 : If CurPos < Max then
  270.                 Begin
  271.                   BufPtr^[CurPos]:= c; Inc(CurPos);
  272.                   WritePChar(@c,1);
  273.                 End;
  274.           End;
  275.         Until False;
  276.       End;
  277.     CrtRead:= 0;
  278.   End;
  279.  
  280.   Function CrtWrite(Var f : Text) : Word; Far;
  281.   Begin
  282.     With TextRec(f) do
  283.       Begin
  284.         WritePChar(PChar(BufPtr),BufPos);
  285.         BufPos:= 0;
  286.       End;
  287.     CrtWrite:= 0;
  288.   End;
  289.  
  290.   Function CrtReturn(Var f : Text) : Word; Far;
  291.   Begin
  292.     CrtReturn:= 0;
  293.   End;
  294.  
  295.   Function CrtOpen(Var f : Text) : Word; Far;
  296.   Var
  297.     InOut,
  298.     Flush,
  299.     Close : Pointer;
  300.   Begin
  301.     With TextRec(f) do
  302.       Begin
  303.         If Mode = fmInput then
  304.           Begin
  305.             InOut:= @CrtRead;
  306.             Flush:= @CrtReturn;
  307.             Close:= @CrtReturn;
  308.           End
  309.         else
  310.           Begin
  311.             Mode:= fmOutput;
  312.             InOut:= @CrtWrite;
  313.             Flush:= @CrtWrite;
  314.             Close:= @CrtReturn;
  315.           End;
  316.  
  317.         InOutFunc:= InOut;
  318.         FlushFunc:= Flush;
  319.         CloseFunc:= Close;
  320.       End;
  321.     CrtOpen:= 0;
  322.   End;
  323.  
  324.   Procedure AssignCrt(Var f : Text);
  325.   Begin
  326.     With TextRec(f) do
  327.       Begin
  328.         Mode:= fmClosed;
  329.         BufSize:= 128;
  330.         BufPtr:= @Buffer;
  331.         OpenFunc:= @CrtOpen;
  332.       End;
  333.   End;
  334.  
  335. Begin
  336.   TextAttr:= LightGray;
  337.   WindMin := 0;
  338.   WindMax := 79+24*256;
  339.   AssignCrt(Input);
  340.   Reset(Input);
  341.   AssignCrt(Output);
  342.   Rewrite(Output);
  343. End.
  344.