home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 11.ddi / WDOCDEMO.ZIP / PRNTEST.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  5.3 KB  |  191 lines

  1. {************************************************}
  2. {                                                }
  3. {   ObjectWindows Demo                           }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. program prntest;
  9.  
  10. uses WinDos, Strings, WinTypes, WinProcs, Objects, OWindows, OStdDlgs, OPrinter, BWCC;
  11.  
  12. {$R PRNTEST.RES}
  13.  
  14. {$I PRNTEST.INC}
  15.  
  16. type
  17.   PPCharCollection = ^TPCharCollection;
  18.   TPCharCollection = object(TCollection)
  19.     procedure FreeItem(Item: Pointer); virtual;
  20.   end;
  21.  
  22.   PTextWindow = ^TTextWindow;
  23.   TTextWindow = object(TWindow)
  24.     FileName: array[0..fsPathName] of Char;
  25.     FileLines: PPCharCollection;
  26.     Printer: PPrinter;
  27.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  28.     destructor Done; virtual;
  29.     procedure CMFileOpen(var Msg: TMessage); virtual cm_First + cm_FileOpen;
  30.     procedure CMFilePrint(var Msg: TMessage); virtual cm_First + cm_FilePrint;
  31.     procedure LoadFile;
  32.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  33.   end;
  34.  
  35.   TTextApp = object(TApplication)
  36.     procedure InitMainWindow; virtual;
  37.   end;
  38.  
  39.   PTextPrint = ^TTextPrint;
  40.   TTextPrint = object(TPrintout)
  41.     TextHeight, LinesPerPage, FirstOnPage, LastOnPage: Integer;
  42.     TheLines: PCollection;
  43.     constructor Init(ATitle: PChar; TheText: PPCharCollection);
  44.     function GetDialogInfo(var Pages: Integer): Boolean; virtual;
  45.     function HasNextPage(Page: Word): Boolean; virtual;
  46.     procedure SetPrintParams(ADC: HDC; ASize: TPoint); virtual;
  47.     procedure PrintPage(Page: Word; var Rect: TRect; Flags: Word); virtual;
  48.   end;
  49.  
  50.  
  51. procedure TPCharCollection.FreeItem(Item: Pointer);
  52. begin
  53.   StrDispose(PChar(Item));
  54. end;
  55.  
  56. constructor TTextWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  57. begin
  58.   inherited Init(AParent, ATitle);
  59.   Attr.Menu := LoadMenu(HInstance, 'MAINMENU');
  60.   Attr.Style := Attr.Style or ws_VScroll or ws_HScroll;
  61.   StrCopy(FileName, '*.*');
  62.   FileLines := New(PPCharCollection, Init(10, 10));
  63.   Scroller := New(PScroller, Init(@Self, 10, 17, 80, 10));
  64.   Printer := New(PPrinter, Init);
  65. end;
  66.  
  67. destructor TTextWindow.Done;
  68. begin
  69.   if Assigned(FileLines) then Dispose(FileLines, Done);
  70.   inherited Done;
  71. end;
  72.  
  73. procedure TTextWindow.CMFileOpen(var Msg: TMessage);
  74. begin
  75.   if Application^.ExecDialog(New(PFileDialog, Init(@Self,
  76.     PChar(sd_FileOpen), FileName))) = id_OK then
  77.     LoadFile;
  78. end;
  79.  
  80. procedure TTextWindow.CMFilePrint(var Msg: TMessage);
  81. var
  82.   Printout: PPrintout;
  83. begin
  84.   Printout := New(PTextPrint, Init(FileName, FileLines));
  85.   Printer^.Print(@Self, Printout);
  86.   Dispose(Printout, Done);
  87. end;
  88.  
  89. procedure TTextWindow.LoadFile;
  90. var
  91.   TextLine: array[0..255] of Char;
  92.   TextFile: Text;
  93. begin
  94.   if Assigned(FileLines) then Dispose(FileLines, Done);
  95.   FileLines := New(PPCharCollection, Init(100, 10));
  96.   Assign(TextFile, FileName);
  97.   Reset(TextFile);
  98.   while not eof(TextFile) do
  99.   begin
  100.     Readln(TextFile, TextLine);
  101.     FileLines^.Insert(StrNew(TextLine));
  102.   end;
  103.   Close(TextFile);
  104.   Scroller^.SetRange(10, FileLines^.Count);
  105.   InvalidateRect(HWindow, nil, True);
  106. end;
  107.  
  108. procedure TTextWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  109. var
  110.   Line: Integer;
  111.   TextMetrics: TTextMetric;
  112.   TheText: PChar;
  113.  
  114.   function TextVisible(ALine: Integer): Boolean;
  115.   begin
  116.     with Scroller^ do
  117.       TextVisible := IsVisibleRect(0, (ALine div YUnit) + YPos, 1, Attr.W div YUnit);
  118.   end;
  119.  
  120. begin
  121.   GetTextMetrics(PaintDC, TextMetrics);
  122.   Scroller^.SetUnits(TextMetrics.tmAveCharWidth, TextMetrics.tmHeight);
  123.   Line := 0;
  124.   while (Line < FileLines^.Count) and TextVisible(Line) do
  125.   begin
  126.     TheText := PChar(FileLines^.At(Line));
  127.     if TheText <> nil then
  128.       TextOut(PaintDC, 0, Line * Scroller^.YUnit, TheText, StrLen(TheText));
  129.     Inc(Line);
  130.   end;
  131. end;
  132.  
  133. procedure TTextApp.InitMainWindow;
  134. begin
  135.   MainWindow := New(PTextWindow, Init(nil, 'Text Viewer'));
  136. end;
  137.  
  138. constructor TTextPrint.Init(ATitle: PChar; TheText: PPCharCollection);
  139. begin
  140.   inherited Init(ATitle);
  141.   TheLines := TheText;
  142.   FirstOnPage := 0;
  143.   LastOnPage := 0;
  144. end;
  145.  
  146. function TTextPrint.GetDialogInfo(var Pages: Integer): Boolean;
  147. begin
  148.   Pages := TheLines^.Count div LinesPerPage + 1;
  149.   GetDialogInfo := True;
  150. end;
  151.  
  152. function TTextPrint.HasNextPage(Page: Word): Boolean;
  153. begin
  154.   HasNextPage := LastOnPage < TheLines^.Count - 1;
  155. end;
  156.  
  157. procedure TTextPrint.SetPrintParams(ADC: HDC; ASize: TPoint);
  158. var
  159.   TextMetrics: TTextMetric;
  160. begin
  161.   inherited SetPrintParams(ADC, ASize);
  162.   GetTextMetrics(DC, TextMetrics);
  163.   TextHeight := TextMetrics.tmHeight;
  164.   LinesPerPage := Size.Y div TextHeight;
  165. end;
  166.  
  167. procedure TTextPrint.PrintPage(Page: Word; var Rect: TRect; Flags: Word);
  168. var
  169.   Line: Integer;
  170.   TheText: PChar;
  171. begin
  172.   FirstOnPage := (Page - 1) * LinesPerPage;
  173.   LastOnPage := (Page * LinesPerPage) - 1;
  174.   if LastOnPage >= TheLines^.Count then LastOnPage := TheLines^.Count - 1;
  175.   for Line := FirstOnPage to LastOnPage do
  176.   begin
  177.     TheText := TheLines^.At(Line);
  178.     if TheText <> nil then
  179.       TextOut(DC, 0, (Line - FirstOnPage) * TextHeight, TheText, StrLen(TheText));
  180.   end;
  181. end;
  182.  
  183. var
  184.   TextApp: TTextApp;
  185.  
  186. begin
  187.   TextApp.Init('TextPrn');
  188.   TextApp.Run;
  189.   TextApp.Done;
  190. end.
  191.