home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-08-28 | 9.8 KB | 422 lines | [TEXT/MPS ] |
- /*
- File: PrinterQD.cpp
-
- Contains: QuickDraw support for the OpenDoc printing utilities.
-
- Owned by: Jens Alfke
-
- Copyright: © 1995-96 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <2> 2/2/96 JA Filled in header comments
- <1> 2/2/96 JA first checked in
-
- To Do:
- */
-
-
- // -- OpenDoc --
-
- #ifndef _ALTPOINT_
- #include <AltPoint.h>
- #endif
-
- #ifndef SOM_ODCanvas_xh
- #include <Canvas.xh>
- #endif
-
- #ifndef SOM_ODClipboard_xh
- #include <Clipbd.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_Commands_defined
- #include <CmdDefs.xh>
- #endif
-
- #ifndef SOM_ODDispatcher_xh
- #include <Disptch.xh>
- #endif
-
- #ifndef SOM_ODFacet_xh
- #include <Facet.xh>
- #endif
-
- #ifndef SOM_ODFrame_xh
- #include <Frame.xh>
- #endif
-
- #ifndef SOM_ODMenuBar_xh
- #include <MenuBar.xh>
- #endif
-
- #ifndef SOM_ODSession_xh
- #include <ODSessn.xh>
- #endif
-
- #ifndef SOM_ODStorageSystem_xh
- #include <ODStor.xh>
- #endif
-
- #ifndef SOM_ODPart_xh
- #include <Part.xh>
- #endif
-
- #ifndef SOM_ODPlatformTypeList_xh
- #include <PfTypLs.xh>
- #endif
-
- #ifndef SOM_ODShape_xh
- #include <Shape.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdProps_defined
- #include <StdProps.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdTypes_defined
- #include <StdTypes.xh>
- #endif
-
- #ifndef SOM_ODTransform_xh
- #include <Trnsform.xh>
- #endif
-
- #ifndef SOM_ODWindowState_xh
- #include <WinStat.xh>
- #endif
-
- // -- OpenDoc Utilities --
-
- #ifndef _EXCEPT_
- #include <Except.h>
- #endif
-
- #ifndef _ITEXT_
- #include <IText.h>
- #endif
-
- #ifndef _DLOGUTIL_
- #include <DlogUtil.h>
- #endif
-
- #ifndef _ODDEBUG_
- #include <ODDebug.h>
- #endif
-
- #ifndef _ODNEW_
- #include <ODNew.h>
- #endif
-
- #ifndef _ODUTILS_
- #include <ODUtils.h>
- #endif
-
- #ifndef _STORUTIL_
- #include <StorUtil.h>
- #endif
-
- #ifndef _TEMPOBJ_
- #include <TempObj.h>
- #endif
-
- #ifndef _USERSRCM_
- #include <UseRsrcM.h>
- #endif
-
- // -- MacToolbox --
-
- #ifndef __TEXTUTILS__
- #include <TextUtils.h>
- #endif
-
- // -- Printer --
- #define _PRINTER_PRIVATE_
- #ifndef _PRINTER_
- #include "Printer.h"
- #endif
-
- #pragma segment Printer
-
-
- //---------------------------------------------------------------------------------
- // THROW_IF_PRINT_ERROR [static]
- //---------------------------------------------------------------------------------
-
- static void
- THROW_IF_PRINT_ERROR( )
- {
- OSErr err = PrError();
- if( err ) THROW(err);
- }
-
-
- //=================================================================================
- // CQDPrinter implementation
- //=================================================================================
-
- //---------------------------------------------------------------------------------
- // CQDPrinter::CQDPrinter
- //---------------------------------------------------------------------------------
-
- CQDPrinter::CQDPrinter()
- {
- fJob = kODNULL;
- fPort = kODNULL;
- fIOBuffer = kODNULL;
- fPrintingOpen = 0;
- fPageOpen = kODFalse;
- }
-
- //---------------------------------------------------------------------------------
- // CQDPrinter::~CQDPrinter
- //---------------------------------------------------------------------------------
-
- CQDPrinter::~CQDPrinter()
- {
- ODDisposeHandle((ODHandle)fJob);
- ODDisposePtr(fPort); // Should have been disposed earlier, but...
- ODDisposePtr(fIOBuffer);
- }
-
- //---------------------------------------------------------------------------------
- // CQDPrinter::OpenPrinting
- //---------------------------------------------------------------------------------
-
- void CQDPrinter::OpenPrinting()
- {
- PrOpen();
- THROW_IF_PRINT_ERROR();
- inherited::OpenPrinting();
- }
-
- //---------------------------------------------------------------------------------
- // CQDPrinter::ClosePrinting
- //---------------------------------------------------------------------------------
-
- void CQDPrinter::ClosePrinting()
- {
- inherited::ClosePrinting();
- PrClose();
- }
-
- //---------------------------------------------------------------------------------
- // CQDPrinter::ReadJobFromHandle
- //---------------------------------------------------------------------------------
-
- ODPlatformPrintJob CQDPrinter::ReadJobFromHandle(ODValueType valueType, ODHandle h)
- {
- ASSERT(valueType==kODTypeQuickDrawPageSetup,kODErrInvalidGraphicsSystem);
- WASSERT(fJob==kODNULL);
-
- fJob = (THPrint) h;
- return (ODPlatformPrintJob)fJob;
- }
-
- //---------------------------------------------------------------------------------
- // CQDPrinter::CreateNewJob
- //---------------------------------------------------------------------------------
-
- ODPlatformPrintJob CQDPrinter::CreateNewJob()
- {
- fJob = (THPrint) ODNewHandle(sizeof(TPrint));
- PrintDefault(fJob);
- if( PrError() ) {
- ODDisposeHandle((ODHandle)fJob);
- fJob = kODNULL;
- THROW(PrError());
- }
- return (ODPlatformPrintJob)fJob;
- }
-
- //---------------------------------------------------------------------------------
- // CQDPrinter::CopyJobToHandle
- //---------------------------------------------------------------------------------
-
- ODHandle CQDPrinter::CopyJobToHandle( )
- {
- ASSERT(fJob!=kODNULL,kODErrAssertionFailed);
- return ODCopyHandle((ODHandle)fJob);
- }
-
- //---------------------------------------------------------------------------------
- // CQDPrinter::CreatePrintingPlatformCanvas
- //---------------------------------------------------------------------------------
-
- ODPlatformCanvas CQDPrinter::CreatePrintingPlatformCanvas( Environment*, ODGraphicsSystem g,
- ODFrame*, ODShape* )
- {
- ASSERT(g==kODQuickDraw,kODErrInvalidGraphicsSystem);
- WASSERT(!fPort);
- WASSERT(!fIOBuffer);
-
- fPort = (TPrPort*) ODNewPtr(sizeof(TPrPort));
- fIOBuffer = (Ptr) ODNewPtr(522);
-
- GrafPtr curPort;
- GetPort(&curPort);
-
- PrOpenDoc(fJob, fPort, fIOBuffer);
-
- SetPort(curPort); // PrOpenDoc changes current port
-
- if( PrError() ) {
- ODDisposePtr(fPort);
- ODDisposePtr(fIOBuffer);
- fPort = kODNULL;
- fIOBuffer = kODNULL;
- THROW(PrError());
- }
- return (ODPlatformCanvas) fPort;
- }
-
- //---------------------------------------------------------------------------------
- // CQDPrinter::CleanupPrintingEnv
- //---------------------------------------------------------------------------------
-
- void CQDPrinter::CleanupPrintingEnv(Environment* ev, ODFrame* initiator)
- {
- // This routine should not throw exeptions and must call the inherited method.
-
- if( fPort ) {
- TRY{
- this->ClosePage(ev); // In case it was left open
- }CATCH_ALL{
- }ENDTRY
-
- PrCloseDoc(fPort);
-
- // Spool document if necessary for this printer:
- if( (**fJob).prJob.bJDocLoop == bSpoolLoop && PrError() == noErr ) {
- TPrStatus theStatus;
- PrPicFile(fJob, fPort, fIOBuffer, kODNULL, &theStatus);
- }
-
- ODDisposePtr(fIOBuffer);
- ODDisposePtr(fPort);
- fPort = kODNULL;
- fIOBuffer = kODNULL;
- }
-
- inherited::CleanupPrintingEnv(ev,initiator);
- }
-
- //---------------------------------------------------------------------------------
- // CQDPrinter::GetPageRect
- //---------------------------------------------------------------------------------
-
- ODRect CQDPrinter::GetPageRect( Environment *ev )
- {
- this->GetPlatformPrintJob(ev); // Force internalization of fJob
-
- return (ODRect) (**fJob).prInfo.rPage;
- }
-
- //---------------------------------------------------------------------------------
- // CQDPrinter::OpenPage
- //---------------------------------------------------------------------------------
-
- void CQDPrinter::OpenPage( Environment*, ODUShort page )
- {
- if( !fPageOpen ) {
- PrOpenPage(fPort, kODNULL);
- THROW_IF_PRINT_ERROR();
- fPageOpen = kODTrue;
- }
- }
-
- //---------------------------------------------------------------------------------
- // CQDPrinter::ClosePage
- //---------------------------------------------------------------------------------
-
- void CQDPrinter::ClosePage( Environment* )
- {
- if( fPageOpen ) {
- PrClosePage(fPort);
- fPageOpen = kODFalse;
- THROW_IF_PRINT_ERROR();
- }
- }
-
- //---------------------------------------------------------------------------------
- // CQDPrinter::RunPageSetupDialog
- //---------------------------------------------------------------------------------
-
- ODBoolean CQDPrinter::RunPageSetupDialog( Environment* )
- {
- PrValidate(fJob);
- THROW_IF_PRINT_ERROR();
-
- ODBoolean success = PrStlDialog(fJob);
- THROW_IF_PRINT_ERROR();
- return success;
- }
-
- //---------------------------------------------------------------------------------
- // CQDPrinter::RunPrintDialog
- //---------------------------------------------------------------------------------
-
- ODBoolean CQDPrinter::RunPrintDialog( Environment* )
- {
- ODBoolean success;
- TRY{
- ASSERT(fJob!=kODNULL,kODErrAssertionFailed);
-
- PrValidate(fJob);
- THROW_IF_PRINT_ERROR();
-
- success = PrJobDialog(fJob);
- THROW_IF_PRINT_ERROR();
-
- }CATCH_ALL{
- WARN("Error %d in page setup dialog",ErrorCode());
- success = kODFalse;
- // ignore exception
- }ENDTRY
- return success;
- }
-
- //---------------------------------------------------------------------------------
- // CQDPrinter::PrintDocument
- //---------------------------------------------------------------------------------
-
- void CQDPrinter::DoPrint(Environment* ev, ODShape* area)
- {
- // Here's what you've been waiting for: the print loop.
- // See Tech Note PR10 "A Printing Loop That Cares" for more details.
-
- // Extract page range from print job:
- short firstPage = (**fJob).prJob.iFstPage;
- short lastPage = (**fJob).prJob.iLstPage;
- ODULong pageCount = this->CountPages(ev, area);
- if ( lastPage > pageCount )
- lastPage = pageCount;
- (**fJob).prJob.iFstPage = 1;
- (**fJob).prJob.iLstPage = iPrPgMax;
-
- // Print the whole ting as many times as the user wants.
- short totalPages = 1;
- for( short copy = (**fJob).prJob.iCopies; copy>0 ; copy-- )
- {
- // Print the pages, one at a time.
- for (short page = firstPage; page <= lastPage; page++, totalPages++)
- {
- if (totalPages % iPFMaxPgs == 0)
- {
- // Every 128 pages we must close, spool, and re-open printing:
- PrCloseDoc(fPort);
- if( (**fJob).prJob.bJDocLoop == bSpoolLoop && PrError() == noErr ) {
- TPrStatus theStatus;
- PrPicFile(fJob, fPort, fIOBuffer, kODNULL, &theStatus);
- }
- PrOpenDoc(fJob, fPort, (Ptr)fIOBuffer);
- THROW_IF_PRINT_ERROR();
- }
-
- this->PrintPage(ev, page, area); // Shazam!
-
- THROW_IF_PRINT_ERROR();
- }
- }
- }
-