home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / bpos2_v3.zip / crt.pas < prev   
Pascal/Delphi Source File  |  1995-07-12  |  8KB  |  362 lines

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