home *** CD-ROM | disk | FTP | other *** search
-
- #include <Gestalt.h>
- #include <CodeFragments.h>
- #include <QDOffscreen.h>
- #include <Errors.h>
- #include <Resources.h>
- #include <GXGraphics.h>
- #include <GXEnvironment.h>
- #include <GXPrinting.h>
- #include <GXMath.h>
-
- #include "appMain.h" // neded for DoEvent
-
- #include "gxGlobals.h"
- #include "gxUtils.h"
- #include "gxPrintUtils.h"
- #include "qdPrintUtils.h"
-
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE FUNCTION PROTOTYPES
- |**| ==============================================================================
- \**/
- OSErr PrintingEventProc ( EventRecord *anEvent, Boolean filterEvent ) ;
- OSErr MyGXPrintAShape ( gxShape currentShape, void *refCon ) ;
-
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE GLOBALS
- |**| ==============================================================================
- \**/
- OSErr gGXPrinting_present_err = 1;
- OSErr gGXPrinting_available_err = 1;
- OSErr gGXPrinting_initialized_err = 1;
-
-
- /**\
- |**| ==============================================================================
- |**| PUBLIC FUNCTIONS
- |**| ==============================================================================
- \**/
-
-
- /*------------------------------------------------------------------------------*\
- GXPrinting_present
- *------------------------------------------------------------------------------*
- This function returns noErr if QuickDraw GX graphics and typography are present
- (i.e passes gestalt) and returns an error otherwise.
- It also sets the global gGXPrinting_present_err based on the result.
- \*------------------------------------------------------------------------------*/
- OSErr GXPrinting_present ( void )
- {
- OSErr err;
- long response;
-
- if ( !gGXPrinting_present_err )
- return noErr ;
-
- err = Gestalt(gestaltGXPrintingMgrVersion, &response) ;
-
- #ifdef powerc
- // This is a sanity check to see if the PowerPC QuickDrawGXLib is installed
- // just in case we are "weak" linked against QuickDrawGXLib
- if (!err)
- if ( kUnresolvedCFragSymbolAddress == (long)GXNewGraphicsClient)
- err = 1; // QuickDraw GX is not available in Power Mac
- #endif
-
- gGXPrinting_present_err = err;
- return err;
- }
-
-
- /*------------------------------------------------------------------------------*\
- GXPrinting_available
- *------------------------------------------------------------------------------*
- This function returns noErr if QuickDraw GX printing is available
- (i.e present and initialized) to the application and returns an error otherwise.
- It also sets the global gGX_available_err based on the return value.
- \*------------------------------------------------------------------------------*/
- OSErr GXPrinting_available ( void )
- {
- OSErr err ;
-
- err = GXPrinting_present();
- if ( !err )
- err = GXPrinting_initialize();
-
- if ( false ) // there isn't a dtp
- return 1 ; // should return something else
-
- gGXPrinting_available_err = err;
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- GXPrinting_initialize
- *------------------------------------------------------------------------------*
- This function initializes all the GX printing stuff.
- It should be called early in the application.
- \*------------------------------------------------------------------------------*/
- OSErr GXPrinting_initialize ( void )
- {
- OSErr err ;
-
- if ( !gGXPrinting_initialized_err )
- return noErr;
-
- err = GXPrinting_present();
- if ( !err )
- {
- err = GXInitPrinting() ;
- }
-
- if ( !err )
- { // Initialize our printing event override UPP
- gQDGXPrintingEventUPP = NewGXPrintingEventProc(PrintingEventProc) ;
- }
-
- gGXPrinting_initialized_err = err;
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- DisposeGXPrinting
- *------------------------------------------------------------------------------*
- This function disposes all the GX printing stuff.
- It should be called before the application exits.
- \*------------------------------------------------------------------------------*/
- void DisposeGXPrinting ( void )
- {
- if( !gGXPrinting_initialized_err )
- {
- DisposeRoutineDescriptor(gQDGXPrintingEventUPP) ;
- GXExitPrinting() ;
- gGXPrinting_initialized_err = 1;
- }
- }
-
-
- /*------------------------------------------------------------------------------*\
- GetDefaultGXJob
- *------------------------------------------------------------------------------*
- This routine sets up the default GX Job object.
- \*------------------------------------------------------------------------------*/
- OSErr GetDefaultGXJob ( gxJob *theJob )
- {
- OSErr err ;
-
- err = GXNewJob( theJob ) ;
- if (err)
- *theJob = nil ;
- else
- GXInstallApplicationOverride( *theJob, gxPrintingEventMsg, gQDGXPrintingEventUPP) ;
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- LoadResourceGXJob
- *------------------------------------------------------------------------------*
- This routine loads a print handle resource with the resType r_printJobType
- If there is no print record resource in the indicated resource file,
- the function returns resNotFound.
- Upon entry, theJob is expected to be a valid job or nil.
- \*------------------------------------------------------------------------------*/
- OSErr LoadResourceGXJob ( gxJob *theJob, FSSpec *spec )
- {
- short savedRefNum;
- short resRefNum;
- Handle resHandle;
- OSErr err = noErr ;
-
- resRefNum = FSpOpenResFile( spec, fsRdPerm) ;
- if (resRefNum == -1)
- return fnfErr ;
-
- savedRefNum = CurResFile() ; // Save the current resource file refNum
- UseResFile( resRefNum ) ; // set the current res file
-
- // Get our resource, if there is one
- resHandle = Get1IndResource(r_printJobType,1) ;
- if ( resHandle==nil )
- { // check for and old THPrint rec and convert it if found
- resHandle = Get1IndResource(r_printHdlType,1) ;
- if ( resHandle != nil )
- {
- if (*theJob==nil) GXNewJob(theJob) ;
- GXConvertPrintRecord( *theJob, (THPrint)resHandle ) ;
- err = GXGetJobError( *theJob ) ;
- }
- else
- err = ResError() ;
- }
- else
- {
- if (*theJob==nil) GXNewJob(theJob) ;
- GXUnflattenJobFromHdl( *theJob, resHandle ) ;
- err = GXGetJobError( *theJob ) ;
- }
-
- if ( resHandle) ReleaseResource( resHandle ) ;
- CloseResFile( resRefNum ) ;
- UseResFile( savedRefNum ) ; // switch back to the original resource file.
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- SaveResourceGXJob
- *------------------------------------------------------------------------------*
- This routine saves the passed print job in resource fork of the
- indicated file. If there are already a print record resources, they are
- deleted and replaced.
- \*------------------------------------------------------------------------------*/
- OSErr SaveResourceGXJob ( gxJob theJob, FSSpec *spec )
- {
- short savedRefNum, uniqID;
- OSErr err = noErr ;
- Handle oldHdl, resHandle;
- short resRefNum;
-
- resRefNum = FSpOpenResFile( spec, fsWrPerm ) ;
- if (resRefNum == -1)
- return fnfErr ;
-
- // flatten the job to a handle.
- resHandle = NewHandle(0) ;
- GXFlattenJobToHdl( theJob, resHandle ) ;
- err = GXGetJobError( theJob ) ;
-
- if (!err)
- {
- savedRefNum = CurResFile() ; // Save the current resource file refNum
- UseResFile(resRefNum) ; // set the current res file
-
- // remove any existing resources
- while ( (oldHdl=Get1IndResource(r_printHdlType,1)) != nil )
- {
- RemoveResource(oldHdl) ; // remove the resource from the file
- DisposeHandle(oldHdl) ; // free the handle that is left behind
- }
- while ( (oldHdl=Get1IndResource(r_printJobType,1)) != nil )
- {
- RemoveResource(oldHdl) ; // remove the resource from the file
- DisposeHandle(oldHdl) ; // free the handle that is left behind
- }
-
- // Save the new resource with a unique ID.
- uniqID = Unique1ID( r_printHdlType ) ;
- AddResource( resHandle, r_printJobType, uniqID, "\p") ;
- err = ResError() ;
-
- UseResFile(savedRefNum) ; // switch back to the original resource file.
- }
-
- CloseResFile( resRefNum ) ;
- FlushVol( nil , spec->vRefNum ) ;
- return noErr ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- GXGetPageSize
- *------------------------------------------------------------------------------*
- \*------------------------------------------------------------------------------*/
- OSErr GXGetPageSize ( gxJob theJob, Rect *pageRect )
- {
- gxFormat pageFormat;
- gxRectangle gxpage ;
-
- pageFormat = GXGetJobFormat( theJob, 1) ;
- GXGetFormatDimensions( pageFormat, &gxpage, nil ) ; // get the pageSize
- pageRect->top = FixedToInt( gxpage.top ) ; // convert it to a regular rect
- pageRect->left = FixedToInt( gxpage.left ) ;
- pageRect->bottom = FixedToInt( gxpage.bottom ) ;
- pageRect->right = FixedToInt( gxpage.right ) ;
-
- return noErr ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- GXStyleDlog
- *------------------------------------------------------------------------------*
- This routine displays the GX Page Setup dialog (a.k.a. Format dialog)
- \*------------------------------------------------------------------------------*/
- OSErr GXStyleDlog ( gxJob theJob )
- {
- OSErr err ;
- gxDialogResult result;
-
- result = GXJobDefaultFormatDialog(theJob, &gQDGXEditMenuRec) ;
- if ( result == gxCancelSelected )
- return userCanceledErr ;
-
- // just check we don't have an error
- err = GXGetJobError(theJob) ;
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- GXStyleDlog
- *------------------------------------------------------------------------------*
- This routine displays the GX Print dialog (a.k.a. Job dialog)
- \*------------------------------------------------------------------------------*/
- OSErr GXJobDlog ( gxJob theJob )
- {
- OSErr err ;
- gxDialogResult result;
-
- result = GXJobPrintDialog( theJob, &gQDGXEditMenuRec ) ;
- if ( result == gxCancelSelected )
- return userCanceledErr ;
-
- // just check we don't have an error
- err = GXGetJobError(theJob) ;
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- GXPrintLoopBegin
- *------------------------------------------------------------------------------*
- \*------------------------------------------------------------------------------*/
- OSErr GXPrintLoopBegin ( GXPrintLoopParamsPtr params )
- {
- OSErr err ;
- long jobFirst, jobLast ;
- Str255 title ;
-
- err = GXGetJobError( params->theJob ) ;
- if (err) goto StartJobFailed ;
-
- // Determine which pages the user selected to print.
- // If the user specifies a range that is greater than the actual number
- // of pages, then only print those pages that are in the document.
- GXGetJobPageRange( params->theJob, &jobFirst, &jobLast) ;
- if (jobFirst > params->firstPage) params->firstPage = jobFirst;
- if (jobLast < params->lastPage ) params->lastPage = jobLast;
-
- // begin printing if there are no errors.
- GetWTitle ( params->window, title ) ;
- GXStartJob( params->theJob, title, params->lastPage - params->firstPage + 1 ) ;
- err = GXGetJobError( params->theJob ) ;
- if (err) goto StartJobFailed ;
-
- // Create a new viewport for printing.
- params->printViewPort = GXNewViewPort( gxScreenViewDevices ) ;
- params->printingPort = (GrafPtr)(params->window) ;
-
- StartJobFailed:
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- GXPrintLoopPageBefore
- *------------------------------------------------------------------------------*
- \*------------------------------------------------------------------------------*/
- OSErr GXPrintLoopPageBefore ( GXPrintLoopParamsPtr params )
- {
- gxFormat pageFormat;
- Point patStretch = {1,1};
- Rect everywhereRect = {0,0,32767,32767};
- OSErr err ;
-
- pageFormat = GXGetJobFormat( params->theJob, 1) ;
- GXStartPage( params->theJob,
- params->currentPage - params->firstPage + 1,
- pageFormat,
- 1, &(params->printViewPort) ) ;
-
- // Setup the translator.
- GXInstallQDTranslator( params->printingPort, gxDefaultOptionsTranslation,
- &everywhereRect, &everywhereRect,
- patStretch, NewgxShapeSpoolProc(MyGXPrintAShape),
- (void*)params->printViewPort) ;
-
- err = GXGetJobError( params->theJob ) ;
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- GXPrintLoopPageAfter
- *------------------------------------------------------------------------------*
- \*------------------------------------------------------------------------------*/
- OSErr GXPrintLoopPageAfter ( GXPrintLoopParamsPtr params )
- {
- OSErr err ;
-
- GXRemoveQDTranslator( params->printingPort, nil ) ;
- // should dispose of MyPrintShape UPP
- GXFinishPage( params->theJob ) ;
- err = GXGetJobError( params->theJob ) ;
- return noErr ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- GXPrintLoopEnd
- *------------------------------------------------------------------------------*
- \*------------------------------------------------------------------------------*/
- OSErr GXPrintLoopEnd ( GXPrintLoopParamsPtr params )
- {
- OSErr err ;
-
- GXFinishJob( params->theJob ) ;
- err = GXGetJobError( params->theJob ) ;
- GXDisposeViewPort( params->printViewPort ) ;
- return err ;
- }
-
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE FUNCTIONS
- |**| ==============================================================================
- \**/
-
-
- /*------------------------------------------------------------------------------*\
- PrintingEventProc
- *------------------------------------------------------------------------------*
- This routine handles update and menu events while a GX movable-modal
- printing dialog box is displayed. It is installed by InitGXPrinting()
- and remover by RemoveGXPrinting()
- \*------------------------------------------------------------------------------*/
- static OSErr PrintingEventProc ( EventRecord *anEvent, Boolean filterEvent )
- {
- if (!filterEvent)
- {
- switch (anEvent->what)
- {
- case mouseDown:
- case keyDown:
- case autoKey:
- break;
- default:
- DoEvent(anEvent) ;
- }
- }
- return(noErr) ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- MyGXPrintAShape
- *------------------------------------------------------------------------------*
- This is a spool proc for the QD-to-QDGX translator routines. In this
- routine, we simply draw each shape we're passed (printing it).
- You could also capture each shape into one gxPicture shape, and then do
- one draw of that picture afterwards.
- \*------------------------------------------------------------------------------*/
- static OSErr MyGXPrintAShape ( gxShape currentShape, void* refCon )
- {
- GXSetShapeViewPorts(currentShape, 1, (gxViewPort*) refCon) ;
- GXDrawShape(currentShape) ;
- return ( GXGetGraphicsError(nil) ) ;
- }
-