home *** CD-ROM | disk | FTP | other *** search
/ The Education Master 1994 (4th Edition) / EDUCATIONS_MASTER_4TH_EDITION.bin / files / windties / paprexps / textstrm.pas < prev    next >
Pascal/Delphi Source File  |  1991-11-26  |  5KB  |  216 lines

  1. {TextStrm - Extensions to ObjectWindows Copyright (C) Doug Overmyer 7/1/91}
  2. unit TextStrm;
  3. {$R TextStrm.res}
  4. {**********************  Interface  *************************}
  5. interface
  6. uses WinTypes, WinProcs, WinDos, Strings, WObjects,StdDlgs,WFPlus;
  7. type
  8. PTextStream = ^TTextStream ;
  9. TTextStream = object(TBufStream)
  10.    CharsToRead : LongInt;
  11.    CharsRead : LongInt;
  12.    ARecord :PChar;
  13.    constructor Init(FileName:PChar;Mode,Size:Word);
  14.    destructor Done;virtual;
  15.    function GetNext:PChar;virtual;
  16.    function WriteNext(szARecord:PChar):integer;virtual;
  17.    function WriteEOF:integer;virtual;
  18.    function IsEOF:Boolean;virtual;
  19.    function GetPctDone:Integer;
  20. end;
  21.  
  22. type
  23. PMeterWindow = ^TMeterWindow;
  24. TMeterWindow = object(TWindow)
  25.   TheRedBrush:HBrush;
  26.   TheGrayBrush:Hbrush;
  27.   ThePen:HPen;
  28.   X,Y,dX,dY,mX :Integer;
  29.   PctDone :Integer;
  30.   Icon:HIcon;
  31.   constructor Init(AParent:PWindowsObject;ATitle:PChar);
  32.   procedure   SetupWindow;virtual;
  33.   destructor  Done; virtual;
  34.   procedure   Draw(NewPctDone:Integer);virtual;
  35.   procedure    Paint(PaintDC:HDC;var PaintInfo:TPaintStruct);virtual;
  36. end;
  37. {************************  Implementation        ********************}
  38. implementation
  39. {************************  TTextStream         **********************}
  40. constructor TTextStream.Init(FileName:PChar; Mode,Size:Word);
  41. begin
  42.     TBufStream.Init(FileName,Mode,Size);
  43.   CharsRead := 0;
  44.   CharsToRead := TBufStream.GetSize;
  45.   ARecord := MemAlloc(32000);
  46. end;
  47.  
  48. destructor TTextStream.Done;
  49. begin
  50.     TBufStream.Done;
  51.   FreeMem(ARecord,32000);
  52. end;
  53.  
  54. {replace unwanted control chars with spaces 10/5/91}
  55. function TTextStream.GetNext:PChar;
  56. var
  57.     Blksize:Integer;
  58.   AChar:Char;
  59.   Indx : Integer;
  60.   IsEOR : Boolean;
  61. begin
  62.     Indx := 0;
  63.   IsEOR := False;
  64.   ARecord[0] := #0;
  65.   while (CharsRead < CharsToRead) and (IsEOR = False) do
  66.       begin
  67.       TBufStream.Read(AChar,1);
  68.     Inc(CharsRead);
  69.     case AChar of
  70.       #13:
  71.           begin
  72.         ARecord[Indx] := #0;
  73.         IsEOR := True;
  74.         end;
  75.         #26:
  76.           begin
  77.         if Indx > 0 then
  78.             begin
  79.           ARecord[Indx] := #0;
  80.           IsEOR := True;
  81.           end;
  82.         end;
  83.       #10:
  84.           begin
  85.         end;
  86.       #9:
  87.           begin
  88.         ARecord[Indx] := AChar;
  89.         Inc(Indx);
  90.         end;
  91.       #0..#31:
  92.           begin
  93.         ARecord[Indx] := ' ';
  94.         Inc(Indx);
  95.         end;
  96.       else
  97.           begin
  98.         ARecord[Indx] := AChar;
  99.         inc(Indx);
  100.         end;
  101.     end;
  102.   end;
  103.   ARecord[Indx] := #0;
  104.   GetNext := ARecord;
  105. end;
  106.  
  107. {This method not actually used due to performance loss - instead
  108.    TStream.Write is called directly}
  109. function TTextStream.WriteNext(szARecord:PChar):Integer;
  110. const
  111.   CRLF : Array[0..2] of Char = #13#10#0;
  112. begin
  113.     TBufStream.Write(szARecord,
  114.     StrLen(szARecord));
  115.     TBufStream.Write(CRLF,2);
  116.     WriteNext := StrLen(szARecord);
  117. end;
  118.  
  119. function TTextStream.WriteEOF:Integer;
  120. const
  121.       EOF : Array[0..1] of Char  = #26;
  122. begin
  123.     TBufStream.Write(EOF,1);
  124.    WriteEOF := 1;
  125. end;
  126.  
  127. function TTextStream.IsEOF:Boolean;
  128. begin
  129.     IsEOF := False;
  130.     if CharsRead >= CharsToRead then
  131.        IsEOF := True;
  132. end;
  133.  
  134. function TTextStream.GetPctDone:Integer;
  135. begin
  136.     GetPctDone := CharsRead*100 div CharsToRead;
  137. end;
  138.  
  139.  
  140. {***************************  TMeterWindow  ***********************}
  141. constructor TMeterWindow.Init(Aparent:PWindowsObject;ATitle:PChar);
  142. begin
  143.     TWindow.Init(AParent,ATitle);
  144.   DisableAutoCreate;
  145.      ThePen := CreatePen(ps_Solid,0,$00000000);
  146.   TheGrayBrush := CreateSolidBrush($00C0C0C0);
  147.   TheRedBrush  := CreateSolidBrush(RGB(255,0,0));
  148.   with Attr do
  149.        begin
  150.       X := 100;Y :=100 ;W := 350;H := 95;
  151.       Style := ws_Popup or ws_Visible or ws_Border or ws_Caption;
  152.        end;
  153.   X := 50;
  154.   Y := 10;
  155.   dX := 275;
  156.   dY := 30;
  157.   mX := 50;   {midpoint between X & X+dX}
  158.   PctDone := 0;
  159. end;
  160.  
  161. procedure TMeterWindow.SetupWindow;
  162. begin
  163.     TWindow.SetupWindow;
  164.     Icon :=LoadIcon(HInstance,'TS_Icon1');
  165. end;
  166.  
  167. destructor TMeterWindow.Done;
  168. begin
  169.      DeleteObject(TheGrayBrush);
  170.   DeleteObject(TheRedBrush);
  171.   DeleteObject(ThePen);
  172.   TWindow.Done;
  173. end;
  174.  
  175. procedure TMeterWindow.Draw(NewPctDone:Integer);
  176. var
  177. Rgn:TRect;
  178. begin
  179.     PctDone := NewPctDone;
  180.     If PctDone > 0 then
  181.        mX :=  X + ((dX * PctDone) div 100)
  182.    else
  183.        mX := X;
  184.    Rgn.Left := X;
  185.    Rgn.Top := Y;
  186.    Rgn.Right := Max(210,mx);
  187.    Rgn.Bottom := Y+dY+20;
  188.    InvalidateRect(HWindow,@Rgn,false);
  189.    UpdateWindow(HWindow); 
  190. end;
  191.  
  192. procedure TMeterWindow.Paint(PaintDC:HDC;var PaintInfo:TPaintStruct);
  193. var
  194.     OldBrush : HBrush;
  195.   OldPen :HPen;
  196.   OldColor : LongInt;
  197.   OldBkMode : Integer;
  198.   Buf  : Array[0..6] of Char;
  199. begin
  200.     DrawIcon(PaintDC,10,10,Icon);
  201.   OldPen := SelectObject(PaintDC,ThePen);
  202.   OldBrush := SelectObject(PaintDC,TheGrayBrush);
  203.   Rectangle(PaintDC,X,Y,mX,Y+dY);
  204.   Str(PctDone:2, Buf);
  205.   StrCat(Buf,'%');
  206.   SetTextAlign(PaintDC,ta_left);
  207.   OldColor := SetTextColor(PaintDC,RGB(255,0,0));  {Red}
  208.   {OldBkMode := SetBkMode(PaintDC,Transparent);}
  209.   TextOut(PaintDC,180,42,Buf,StrLen(Buf));
  210.   SelectObject(PaintDC,OldBrush);
  211.   SelectObject(PaintDC,OldPen);
  212.   SetTextColor(PaintDC,Oldcolor);
  213.   {SetBkMode(PaintDC,OldBkMode);}
  214. end;
  215. end.
  216.