home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Utilities / PrinterQD.cpp < prev    next >
Encoding:
Text File  |  1996-08-28  |  9.8 KB  |  422 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        PrinterQD.cpp
  3.  
  4.     Contains:    QuickDraw support for the OpenDoc printing utilities.
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1995-96 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>      2/2/96    JA        Filled in header comments
  13.          <1>      2/2/96    JA        first checked in
  14.  
  15.     To Do:
  16. */
  17.  
  18.  
  19. // -- OpenDoc --
  20.  
  21. #ifndef _ALTPOINT_
  22. #include <AltPoint.h>
  23. #endif
  24.  
  25. #ifndef SOM_ODCanvas_xh
  26. #include <Canvas.xh>
  27. #endif
  28.  
  29. #ifndef SOM_ODClipboard_xh
  30. #include <Clipbd.xh>
  31. #endif
  32.  
  33. #ifndef SOM_Module_OpenDoc_Commands_defined
  34. #include <CmdDefs.xh>
  35. #endif
  36.  
  37. #ifndef SOM_ODDispatcher_xh
  38. #include <Disptch.xh>
  39. #endif
  40.  
  41. #ifndef SOM_ODFacet_xh
  42. #include <Facet.xh>
  43. #endif
  44.  
  45. #ifndef SOM_ODFrame_xh
  46. #include <Frame.xh>
  47. #endif
  48.  
  49. #ifndef SOM_ODMenuBar_xh
  50. #include <MenuBar.xh>
  51. #endif
  52.  
  53. #ifndef SOM_ODSession_xh
  54. #include <ODSessn.xh>
  55. #endif
  56.  
  57. #ifndef SOM_ODStorageSystem_xh
  58. #include <ODStor.xh>
  59. #endif
  60.  
  61. #ifndef SOM_ODPart_xh
  62. #include <Part.xh>
  63. #endif
  64.  
  65. #ifndef SOM_ODPlatformTypeList_xh
  66. #include <PfTypLs.xh>
  67. #endif
  68.  
  69. #ifndef SOM_ODShape_xh
  70. #include <Shape.xh>
  71. #endif
  72.  
  73. #ifndef SOM_Module_OpenDoc_StdProps_defined
  74. #include <StdProps.xh>
  75. #endif
  76.  
  77. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  78. #include <StdTypes.xh>
  79. #endif
  80.  
  81. #ifndef SOM_ODTransform_xh
  82. #include <Trnsform.xh>
  83. #endif
  84.  
  85. #ifndef SOM_ODWindowState_xh
  86. #include <WinStat.xh>
  87. #endif
  88.  
  89. // -- OpenDoc Utilities --
  90.  
  91. #ifndef _EXCEPT_
  92. #include <Except.h>
  93. #endif
  94.  
  95. #ifndef _ITEXT_
  96. #include <IText.h>
  97. #endif
  98.  
  99. #ifndef _DLOGUTIL_
  100. #include <DlogUtil.h>
  101. #endif
  102.  
  103. #ifndef _ODDEBUG_
  104. #include <ODDebug.h>
  105. #endif
  106.  
  107. #ifndef _ODNEW_
  108. #include <ODNew.h>
  109. #endif
  110.  
  111. #ifndef _ODUTILS_
  112. #include <ODUtils.h>
  113. #endif
  114.  
  115. #ifndef _STORUTIL_
  116. #include <StorUtil.h>
  117. #endif
  118.  
  119. #ifndef _TEMPOBJ_
  120. #include <TempObj.h>
  121. #endif
  122.  
  123. #ifndef _USERSRCM_
  124. #include <UseRsrcM.h>
  125. #endif
  126.  
  127. // -- MacToolbox --
  128.  
  129. #ifndef __TEXTUTILS__
  130. #include <TextUtils.h>
  131. #endif
  132.  
  133. // -- Printer --
  134. #define _PRINTER_PRIVATE_
  135. #ifndef _PRINTER_
  136. #include "Printer.h"
  137. #endif
  138.  
  139. #pragma segment Printer
  140.  
  141.  
  142. //---------------------------------------------------------------------------------
  143. // THROW_IF_PRINT_ERROR  [static]
  144. //---------------------------------------------------------------------------------
  145.  
  146. static void
  147. THROW_IF_PRINT_ERROR( )
  148. {
  149.     OSErr err = PrError();
  150.     if( err ) THROW(err);
  151. }
  152.  
  153.  
  154. //=================================================================================
  155. // CQDPrinter implementation
  156. //=================================================================================
  157.  
  158. //---------------------------------------------------------------------------------
  159. // CQDPrinter::CQDPrinter
  160. //---------------------------------------------------------------------------------
  161.  
  162. CQDPrinter::CQDPrinter()
  163. {
  164.     fJob = kODNULL;
  165.     fPort = kODNULL;
  166.     fIOBuffer = kODNULL;
  167.     fPrintingOpen = 0;
  168.     fPageOpen = kODFalse;
  169. }
  170.  
  171. //---------------------------------------------------------------------------------
  172. // CQDPrinter::~CQDPrinter
  173. //---------------------------------------------------------------------------------
  174.  
  175. CQDPrinter::~CQDPrinter()
  176. {
  177.     ODDisposeHandle((ODHandle)fJob);
  178.     ODDisposePtr(fPort);                    // Should have been disposed earlier, but...
  179.     ODDisposePtr(fIOBuffer);
  180. }
  181.  
  182. //---------------------------------------------------------------------------------
  183. // CQDPrinter::OpenPrinting
  184. //---------------------------------------------------------------------------------
  185.  
  186. void CQDPrinter::OpenPrinting()
  187. {
  188.     PrOpen();
  189.     THROW_IF_PRINT_ERROR();
  190.     inherited::OpenPrinting();
  191. }
  192.  
  193. //---------------------------------------------------------------------------------
  194. // CQDPrinter::ClosePrinting
  195. //---------------------------------------------------------------------------------
  196.  
  197. void CQDPrinter::ClosePrinting()
  198. {
  199.     inherited::ClosePrinting();
  200.     PrClose();
  201. }
  202.  
  203. //---------------------------------------------------------------------------------
  204. // CQDPrinter::ReadJobFromHandle
  205. //---------------------------------------------------------------------------------
  206.  
  207. ODPlatformPrintJob CQDPrinter::ReadJobFromHandle(ODValueType valueType, ODHandle h)
  208. {
  209.     ASSERT(valueType==kODTypeQuickDrawPageSetup,kODErrInvalidGraphicsSystem);
  210.     WASSERT(fJob==kODNULL);
  211.     
  212.     fJob = (THPrint) h;
  213.     return (ODPlatformPrintJob)fJob;
  214. }
  215.  
  216. //---------------------------------------------------------------------------------
  217. // CQDPrinter::CreateNewJob
  218. //---------------------------------------------------------------------------------
  219.  
  220. ODPlatformPrintJob CQDPrinter::CreateNewJob()
  221. {
  222.     fJob = (THPrint) ODNewHandle(sizeof(TPrint));
  223.     PrintDefault(fJob);
  224.     if( PrError() ) {
  225.         ODDisposeHandle((ODHandle)fJob);
  226.         fJob = kODNULL;
  227.         THROW(PrError());
  228.     }
  229.     return (ODPlatformPrintJob)fJob;
  230. }
  231.  
  232. //---------------------------------------------------------------------------------
  233. // CQDPrinter::CopyJobToHandle
  234. //---------------------------------------------------------------------------------
  235.  
  236. ODHandle CQDPrinter::CopyJobToHandle( )
  237. {
  238.     ASSERT(fJob!=kODNULL,kODErrAssertionFailed);
  239.     return ODCopyHandle((ODHandle)fJob);
  240. }
  241.  
  242. //---------------------------------------------------------------------------------
  243. // CQDPrinter::CreatePrintingPlatformCanvas
  244. //---------------------------------------------------------------------------------
  245.  
  246. ODPlatformCanvas CQDPrinter::CreatePrintingPlatformCanvas( Environment*, ODGraphicsSystem g,
  247.                                                             ODFrame*, ODShape* )
  248. {
  249.     ASSERT(g==kODQuickDraw,kODErrInvalidGraphicsSystem);
  250.     WASSERT(!fPort);
  251.     WASSERT(!fIOBuffer);
  252.     
  253.     fPort = (TPrPort*) ODNewPtr(sizeof(TPrPort));
  254.     fIOBuffer = (Ptr) ODNewPtr(522);
  255.     
  256.     GrafPtr curPort;
  257.     GetPort(&curPort);
  258.     
  259.     PrOpenDoc(fJob, fPort, fIOBuffer);
  260.     
  261.     SetPort(curPort);                        // PrOpenDoc changes current port
  262.     
  263.     if( PrError() ) {
  264.         ODDisposePtr(fPort);
  265.         ODDisposePtr(fIOBuffer);
  266.         fPort = kODNULL;
  267.         fIOBuffer = kODNULL;
  268.         THROW(PrError());
  269.     }
  270.     return (ODPlatformCanvas) fPort;
  271. }
  272.  
  273. //---------------------------------------------------------------------------------
  274. // CQDPrinter::CleanupPrintingEnv
  275. //---------------------------------------------------------------------------------
  276.  
  277. void CQDPrinter::CleanupPrintingEnv(Environment* ev, ODFrame* initiator)
  278. {
  279.     // This routine should not throw exeptions and must call the inherited method.
  280.     
  281.     if( fPort ) {
  282.         TRY{
  283.             this->ClosePage(ev);                // In case it was left open
  284.         }CATCH_ALL{
  285.         }ENDTRY
  286.         
  287.         PrCloseDoc(fPort);
  288.         
  289.         // Spool document if necessary for this printer:
  290.         if( (**fJob).prJob.bJDocLoop == bSpoolLoop && PrError() == noErr ) {
  291.             TPrStatus theStatus;
  292.             PrPicFile(fJob, fPort, fIOBuffer, kODNULL, &theStatus);
  293.         }
  294.         
  295.         ODDisposePtr(fIOBuffer);
  296.         ODDisposePtr(fPort);
  297.         fPort = kODNULL;
  298.         fIOBuffer = kODNULL;
  299.     }
  300.     
  301.     inherited::CleanupPrintingEnv(ev,initiator);
  302. }
  303.  
  304. //---------------------------------------------------------------------------------
  305. // CQDPrinter::GetPageRect
  306. //---------------------------------------------------------------------------------
  307.  
  308. ODRect CQDPrinter::GetPageRect( Environment *ev )
  309. {
  310.     this->GetPlatformPrintJob(ev);        // Force internalization of fJob
  311.     
  312.     return (ODRect) (**fJob).prInfo.rPage;
  313. }
  314.  
  315. //---------------------------------------------------------------------------------
  316. // CQDPrinter::OpenPage
  317. //---------------------------------------------------------------------------------
  318.  
  319. void CQDPrinter::OpenPage( Environment*, ODUShort page )
  320. {
  321.     if( !fPageOpen ) {
  322.         PrOpenPage(fPort, kODNULL);
  323.         THROW_IF_PRINT_ERROR();
  324.         fPageOpen = kODTrue;
  325.     }
  326. }
  327.  
  328. //---------------------------------------------------------------------------------
  329. // CQDPrinter::ClosePage
  330. //---------------------------------------------------------------------------------
  331.  
  332. void CQDPrinter::ClosePage( Environment* )
  333. {
  334.     if( fPageOpen ) {
  335.         PrClosePage(fPort);
  336.         fPageOpen = kODFalse;
  337.         THROW_IF_PRINT_ERROR();
  338.     }
  339. }
  340.  
  341. //---------------------------------------------------------------------------------
  342. // CQDPrinter::RunPageSetupDialog
  343. //---------------------------------------------------------------------------------
  344.  
  345. ODBoolean CQDPrinter::RunPageSetupDialog( Environment* )
  346. {
  347.     PrValidate(fJob);
  348.     THROW_IF_PRINT_ERROR();
  349.     
  350.     ODBoolean success = PrStlDialog(fJob);
  351.     THROW_IF_PRINT_ERROR();
  352.     return success;
  353. }
  354.  
  355. //---------------------------------------------------------------------------------
  356. // CQDPrinter::RunPrintDialog
  357. //---------------------------------------------------------------------------------
  358.  
  359. ODBoolean CQDPrinter::RunPrintDialog( Environment* )
  360. {
  361.     ODBoolean success;
  362.     TRY{
  363.         ASSERT(fJob!=kODNULL,kODErrAssertionFailed);
  364.         
  365.         PrValidate(fJob);
  366.         THROW_IF_PRINT_ERROR();
  367.  
  368.         success = PrJobDialog(fJob);
  369.         THROW_IF_PRINT_ERROR();
  370.         
  371.     }CATCH_ALL{
  372.         WARN("Error %d in page setup dialog",ErrorCode());
  373.         success = kODFalse;
  374.         // ignore exception
  375.     }ENDTRY
  376.     return success;
  377. }
  378.  
  379. //---------------------------------------------------------------------------------
  380. // CQDPrinter::PrintDocument
  381. //---------------------------------------------------------------------------------
  382.  
  383. void CQDPrinter::DoPrint(Environment* ev, ODShape* area)
  384. {
  385.     // Here's what you've been waiting for: the print loop.
  386.     // See Tech Note PR10 "A Printing Loop That Cares" for more details.
  387.     
  388.     // Extract page range from print job:
  389.     short firstPage = (**fJob).prJob.iFstPage;
  390.     short lastPage = (**fJob).prJob.iLstPage;
  391.     ODULong pageCount = this->CountPages(ev, area);
  392.     if ( lastPage > pageCount ) 
  393.         lastPage = pageCount;
  394.     (**fJob).prJob.iFstPage = 1;
  395.     (**fJob).prJob.iLstPage = iPrPgMax;
  396.  
  397.     // Print the whole ting as many times as the user wants.
  398.     short totalPages = 1;
  399.     for( short copy = (**fJob).prJob.iCopies; copy>0 ; copy-- )
  400.     {
  401.         // Print the pages, one at a time.
  402.         for (short page = firstPage; page <= lastPage; page++, totalPages++)
  403.         {
  404.             if (totalPages % iPFMaxPgs == 0)
  405.             {
  406.                 // Every 128 pages we must close, spool, and re-open printing:
  407.                 PrCloseDoc(fPort);
  408.                 if( (**fJob).prJob.bJDocLoop == bSpoolLoop && PrError() == noErr ) {
  409.                     TPrStatus theStatus;
  410.                     PrPicFile(fJob, fPort, fIOBuffer, kODNULL, &theStatus);
  411.                 }
  412.                 PrOpenDoc(fJob, fPort, (Ptr)fIOBuffer);
  413.                 THROW_IF_PRINT_ERROR();
  414.             }
  415.             
  416.             this->PrintPage(ev, page, area);                // Shazam!
  417.         
  418.             THROW_IF_PRINT_ERROR();
  419.         }
  420.     }
  421. }
  422.