home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / PNL Libraries / MyPrinting.p < prev    next >
Encoding:
Text File  |  1995-10-27  |  6.6 KB  |  240 lines  |  [TEXT/CWIE]

  1. unit MyPrinting;
  2.  
  3. interface
  4.  
  5.     uses
  6.         Printing;
  7.  
  8.     type
  9.         PObject = object
  10.                 procedure Create;
  11.                 procedure Destroy;
  12.                 function CountPages (r: rect): integer;
  13.                 function DrawPage (r: rect; gp: GrafPtr; pg: integer; first, last: boolean): OSErr;
  14.                 procedure OpenPrintingStatusDialog;
  15.                 procedure DoIdle;
  16.                 procedure ClosePrintingStatusDialog;
  17.             end;
  18.  
  19.     var
  20.         thePrintingRecordHandle: THPrint;
  21.  
  22.     procedure StartupPrinting;
  23.     function PrintStuff (pob: PObject; thePrRecHdl: THPrint): OSErr; { may return userCanceledErr }
  24.     function DoPageSetup (pob: PObject; thePrRecHdl: THPrint): OSErr;
  25.  
  26. implementation
  27.  
  28.     uses
  29.         Quickdraw, ToolUtils, Resources, 
  30.         MyCursors, MyStartup;
  31.  
  32.     procedure PObject.Create;
  33.     begin
  34.     end;
  35.  
  36.     procedure PObject.Destroy;
  37.     begin
  38.         dispose(self);
  39.     end;
  40.  
  41.     function PObject.CountPages (r: rect): integer;
  42.     begin
  43.         CountPages := 1;
  44.     end;
  45.  
  46.     procedure PObject.DoIdle;
  47.     begin
  48.     end;
  49.  
  50.     function PObject.DrawPage (r: rect; gp: GrafPtr; pg: integer; first, last: boolean): OSErr;
  51.     begin
  52.         SetPort(gp);
  53.         with r do
  54.             MoveTo((left + right) div 2 - 20, (top + bottom) div 2);
  55.         DrawString('Not Yet Implemented');
  56.         DrawPage := noErr;
  57.     end;
  58.  
  59.     procedure PObject.OpenPrintingStatusDialog;
  60.     begin
  61.         CursorSetWatch;
  62.     end;
  63.  
  64.     procedure PObject.ClosePrintingStatusDialog;
  65.     begin
  66.         CursorSetArrow;
  67.     end;
  68.  
  69.     var
  70.         gpob: PObject;
  71.  
  72.     procedure DoIdle;
  73.     begin
  74.         gpob.DoIdle;
  75.     end;
  76.  
  77.     function DoPageSetup (pob: PObject; thePrRecHdl: THPrint): OSErr;
  78.         var
  79.             dummy: boolean;
  80.     begin
  81.         PrOpen;
  82.         if PrError = noErr then begin
  83.             dummy := PrStlDialog(thePrRecHdl);
  84.             DoPageSetup := noErr;
  85.         end
  86.         else begin
  87.             DoPageSetup := PrError;
  88.         end;
  89.         PrClose;
  90.     end;
  91.  
  92. {*------ PrintStuff ---------------------------------------------------------*}
  93. {** **   PrintStuff will call all of the necessary Print Manager calls to print }
  94. {**   a document.  It checks PrError() after each Print Manager call.  If an }
  95. { **   error is found, all of the Print Manager open calls (i.e., PrOpen, }
  96. { **   PrOpenDoc...) will have a corresponding close call before the error }
  97. { **   is posted to the user.  You want to use this approach to make sure the }
  98. { **   Print Manager closes properly and all temporary memory is released. }
  99.     function PrintStuff (pob: PObject; thePrRecHdl: THPrint): OSErr;
  100.  
  101.         var
  102.             copies, firstPage, lastPage, numberOfCopies, pageNumber, printmgrsResFile, realNumberOfPagesInDoc: Integer;
  103.             oldPort: GrafPtr;
  104.             thePrPort: TPPrPort;
  105.             theStatus: TPrStatus;
  106.             err: OSErr;
  107.     begin
  108.         GetPort(oldPort);
  109.         gpob := pob;
  110.  
  111.         PrOpen;
  112.         if PrError = noErr then begin
  113.              { Save the current resource file (i.e. the printer driver's) so the driver will not lose its }
  114.             { resources upon return from the pIdleProc.}
  115.             printmgrsResFile := CurResFile;
  116.  
  117.             realNumberOfPagesInDoc := pob.CountPages(thePrRecHdl^^.prInfo.rPage);
  118.  
  119.             if PrJobDialog(thePrRecHdl) then begin
  120.  {                          Get the number of copies of the document that}
  121.  {                          the user wants printed from iCopies of the TPrJob}
  122.   {                         record (IM II-151).}
  123.  
  124.                 numberOfCopies := thePrRecHdl^^.prJob.iCopies;
  125.  
  126.   {                           Get the first and last pages of the document that}
  127.   {                          were requested to be printed by the user from}
  128.    {                         iFstPage and iLastPage from the TPrJob record}
  129.    {                         (IM II-151).}
  130.  
  131.                 firstPage := thePrRecHdl^^.prJob.iFstPage;
  132.                 lastPage := thePrRecHdl^^.prJob.iLstPage;
  133.  
  134. {                             Print "all" pages in the print loop}
  135.  
  136.                 thePrRecHdl^^.prJob.iFstPage := 1;
  137.                 thePrRecHdl^^.prJob.iLstPage := 9999;
  138.                 if (lastPage > realNumberOfPagesInDoc) then begin
  139.                     lastPage := realNumberOfPagesInDoc;
  140.                 end;
  141.  
  142.   {                           Print the number of copies of the document}
  143.  {                           requested by the user from the Print Job Dialog.}
  144.                 pob.OpenPrintingStatusDialog;
  145.  
  146.                 for copies := 1 to numberOfCopies do begin
  147.  {                               Install and call your "Print Status Dialog".}
  148.                     thePrRecHdl^^.prJob.pIdleProc := @DoIdle;
  149.  
  150.                     UseResFile(printmgrsResFile);
  151.  
  152.                     thePrPort := PrOpenDoc(thePrRecHdl, nil, nil);
  153.  
  154.                     if (PrError = noErr) then begin
  155.                             {  Print the range of pages of the document requested by the user from the Print Job Dialog.}
  156.  
  157.                         pageNumber := firstPage;
  158.                         while ((pageNumber <= lastPage) and (PrError = noErr)) do begin
  159.  
  160.                             PrOpenPage(thePrPort, nil);
  161.  
  162.                             if (PrError = noErr) then begin
  163.                                 { rPage (IM II-150) is the printable area for the currently selected printer. By passing the current}
  164.                               { enables your app to use the same routine to draw to the screen and the printer's GrafPort.}
  165.  
  166.                                 err := pob.DrawPage(thePrRecHdl^^.prInfo.rPage, GrafPtr(thePrPort), pageNumber, firstPage = pageNumber, lastPage = pageNumber);
  167.                                 if err <> noErr then begin
  168.                                     PrSetError(err);
  169.                                 end;
  170.                             end;
  171.                             PrClosePage(thePrPort);
  172.                             pageNumber := pageNumber + 1;
  173.                         end;  {**  End pagenumber loop  **}
  174.                     end;
  175.                     PrCloseDoc(thePrPort);
  176.                 end;  {**  End copies loop  **}
  177.  
  178.                 pob.ClosePrintingStatusDialog;
  179.   {                            The printing job is being canceled by the request}
  180.   {                            of the user from the Print Style Dialog or the}
  181.   {                            Print Job Dialog PrError will be set to iPrAbort}
  182.    {                           to tell the Print Manager to abort the current}
  183.    {                           printing job.}
  184.             end
  185.             else begin
  186.                 PrSetError(iPrAbort); {** Cancel from the job dialog **}
  187.             end;
  188.         end;
  189.         if (thePrRecHdl^^.prJob.bJDocLoop = bSpoolLoop) and (PrError = noErr) then begin
  190.             PrPicFile(thePrRecHdl, nil, nil, nil, theStatus);
  191.         end;
  192.  
  193.   {        Grab the printing error before you close}
  194.  {        the Print Manager and the error disappears.}
  195.  
  196.         if PrError = iPrAbort then begin
  197.             PrintStuff := userCanceledErr;
  198.         end
  199.         else begin
  200.             PrintStuff := PrError;
  201.         end;
  202.  
  203.         PrClose;
  204.  
  205.         SetPort(oldPort);
  206.     end;  {**  PrintStuff  **}
  207.  
  208.     function InitPrinting(var msg: integer): OSStatus;
  209.     begin
  210.         msg := msg; { Unused }
  211.         thePrintingRecordHandle := THPrint(NewHandle(SIZEOF(TPrint)));
  212.         PrOpen;
  213.         if PrError = noErr then begin
  214.             PrintDefault(thePrintingRecordHandle);
  215.             PrClose;
  216.         end;
  217.         InitPrinting := noErr;
  218.     end;
  219.  
  220.     procedure FinishPrinting;
  221.     begin
  222.         DisposeHandle(handle(thePrintingRecordHandle));
  223.     end;
  224.  
  225.     procedure StartupPrinting;
  226.     begin
  227.         StartupCursors;
  228.         SetStartup(InitPrinting, nil, 0, FinishPrinting);
  229.     end;
  230.     
  231. end.
  232. procedure PObject.PostPrintingErrors (oe: OSErr);
  233.     var
  234.         s: str255;
  235.         a: integer;
  236. begin
  237.     NumToString(oe, s);
  238.     ParamText('Print Error = ', s, '', '');
  239.     a := Alert(fail_alert_id, nil);
  240. end;