//----------------------------------------------------------------------------------------------------------------------- // (Borrowed from /Developer/Examples/Printing/App/) static OSStatus DoPrintDialog(PMPrintSession printSession, PMPageFormat pageFormat, PMPrintSettings* printSettings) { OSStatus status = noErr; Boolean accepted; UInt32 realNumberOfPagesinDoc; // In this sample code the caller provides a valid PageFormat reference but in // your application you may want to load and unflatten the PageFormat object // that was saved at PageSetup time. See LoadAndUnflattenPageFormat below. // Set up a valid PrintSettings object. if (*printSettings == kPMNoPrintSettings) { status = PMCreatePrintSettings(printSettings); check(status == noErr); // Note that PMPrintSettings is not session-specific, but calling // PMSessionDefaultPrintSettings assigns values specific to the printer // associated with the current printing session. if ((status == noErr) && (*printSettings != kPMNoPrintSettings)) status = PMSessionDefaultPrintSettings(printSession, *printSettings); check(status == noErr); } else { status = PMSessionValidatePrintSettings(printSession, *printSettings, kPMDontWantBoolean); check(status == noErr); } // Before displaying the Print dialog, we calculate the number of pages in the // document. On Mac OS X this is useful because we can prime the Print dialog // with the actual page range of the document and prevent the user from entering // out-of-range numbers. This is not possible on Mac OS 8 and 9 because the driver, // not the printing manager, controls the page range fields in the Print dialog. // Calculate the number of pages required to print the entire document. if (status == noErr) status = DetermineNumberOfPagesInDoc(pageFormat, &realNumberOfPagesinDoc); // Set a valid page range before displaying the Print dialog if (status == noErr) status = PMSetPageRange(*printSettings, 1, realNumberOfPagesinDoc); check(status == noErr); // Display the Print dialog. if (status == noErr) { status = PMSessionPrintDialog(printSession, *printSettings, pageFormat, &accepted); check(status == noErr); if (status == noErr && !accepted) status = kPMCancel; // user clicked Cancel button } return status; } // DoPrintDialog
/*------------------------------------------------------------------------------ Function: DoPrintDialog Parameters: Description: If the caller passes an empty PrintSettings object, DoPrintDialog creates a new one, otherwise it validates the one provided by the caller. It then invokes the Print dialog and checks for Cancel. Note that the PrintSettings object is modified by this function. ------------------------------------------------------------------------------*/ OSStatus DoPrintDialog(PrintingLogicPtr printJob) { OSStatus status = noErr; Boolean accepted; UInt32 realNumberOfPagesinDoc; // In this sample code the caller provides a valid PageFormat reference but in // your application you may want to load and unflatten the PageFormat object // that was saved at PageSetup time. See LoadAndUnflattenPageFormat below. // Set up a valid PrintSettings object. if (printJob->printSettings == kPMNoPrintSettings) { status = PMCreatePrintSettings(&printJob->printSettings); // Note that PMPrintSettings is not session-specific, but calling // PMSessionDefaultPrintSettings assigns values specific to the printer // associated with the current printing session. if ((status == noErr) && (printJob->printSettings != kPMNoPrintSettings)) status = PMSessionDefaultPrintSettings(printJob->printSession, printJob->printSettings); } else status = PMSessionValidatePrintSettings(printJob->printSession, printJob->printSettings, kPMDontWantBoolean); // Before displaying the Print dialog, we calculate the number of pages in the // document. On Mac OS X this is useful because we can prime the Print dialog // with the actual page range of the document and prevent the user from entering // out-of-range numbers. This is not possible on Mac OS 8 and 9 because the driver, // not the printing manager, controls the page range fields in the Print dialog. // Calculate the number of pages required to print the entire document. if (status == noErr) status = DetermineNumberOfPagesInDoc(&realNumberOfPagesinDoc,printJob); // Set a valid page range before displaying the Print dialog if (status == noErr) status = PMSetPageRange(printJob->printSettings, 1, realNumberOfPagesinDoc); // Display the Print dialog. if (status == noErr) { void * giLocker; giLocker = interpreterProxy->ioLoadFunctionFrom("getUIToLock", ""); if (giLocker != 0) { long *foo; foo = malloc(sizeof(long)*7); foo[0] = 4; foo[1] = (long)PMSessionPrintDialog; foo[2] = (long)printJob->printSession; foo[3] = (long)printJob->printSettings; foo[4] = (long)printJob->pageFormat; foo[5] = (long)&accepted; foo[6] = 0; ((int (*) (void *)) giLocker)(foo); status = foo[6]; free(foo); } else status = PMSessionPrintDialog(printJob->printSession, printJob->printSettings, printJob->pageFormat, &accepted); if (status == noErr && !accepted) status = kPMCancel; // user clicked Cancel button } return status; } // DoPrintDialog
//----------------------------------------------------------------------------------------------------------------------- static OSStatus DoPrintLoop(DocStoragePtr docStP, PMPrintSession printSession, PMPageFormat pageFormat, PMPrintSettings printSettings) { OSStatus status = noErr, tempErr; CGContextRef printingCtx; UInt32 realNumberOfPagesinDoc, pageNumber, firstPage, lastPage; CFStringRef jobName; // use window title status = CopyWindowTitleAsCFString(docStP->ownerWindow, &jobName); status = PMPrintSettingsSetJobName(printSettings, jobName); CFRelease (jobName); // Get the user's Print dialog selection for first and last pages to print. if (status == noErr) { status = PMGetFirstPage(printSettings, &firstPage); if (status == noErr) status = PMGetLastPage(printSettings, &lastPage); } // Check that the selected page range does not exceed the actual number of pages in the document. if (status == noErr) { status = DetermineNumberOfPagesInDoc(pageFormat, &realNumberOfPagesinDoc); if (realNumberOfPagesinDoc < lastPage) lastPage = realNumberOfPagesinDoc; } // Before executing the print loop, tell the Carbon Printing Manager which pages // will be spooled so that the progress dialog can reflect an accurate page count. if (status == noErr) status = PMSetFirstPage(printSettings, firstPage, false); if (status == noErr) status = PMSetLastPage(printSettings, lastPage, false); // Note, we don't have to worry about the number of copies. The printing // manager handles this. So we just iterate through the document from the // first page to be printed, to the last. if (status == noErr) { // Now, tell the printing system that we promise never to use any Quickdraw calls: status = PMSessionBeginCGDocument(printSession, printSettings, pageFormat); check(status == noErr); if (status == noErr) { pageNumber = firstPage; // Note that we check PMSessionError immediately before beginning a new page. // This handles user cancelling appropriately. Also, if we got an error on // any previous iteration of the print loop, we break out of the loop. while ( (pageNumber <= lastPage) && (status == noErr) && (PMSessionError(printSession) == noErr) ) { status = PMSessionBeginPage(printSession, pageFormat, NULL); check(status == noErr); if (status == noErr) { status = PMSessionGetCGGraphicsContext(printSession, &printingCtx); check(status == noErr); if (status == noErr) { DrawThePage(printingCtx, docStP); } tempErr = PMSessionEndPage(printSession); if(status == noErr) status = tempErr; } pageNumber++; } // end while loop // Close the print job. This dismisses the progress dialog on Mac OS X. tempErr = PMSessionEndDocument(printSession); if (status == noErr) status = tempErr; } } // Only report a printing error once we have completed the print loop. This ensures // that every PMBeginXXX call that returns no error is followed by a matching PMEndXXX // call, so the Printing Manager can release all temporary memory and close properly. tempErr = PMSessionError(printSession); if(status == noErr) status = tempErr; /* if ((status != noErr) && (status != kPMCancel)) PostPrintingErrors(status); */ return status; } // DoPrintLoop
int ioPrintPreProcessing(PrintingLogicPtr printJob,int numberOfPages) { OSStatus status = noErr; UInt32 realNumberOfPagesinDoc; // Display the Print dialog. if (printJob->printSession == NULL) return -1; printJob->numberOfPages = numberOfPages; printJob->allowPostscript = false; status = DoPrintDialog(printJob); if (status == noErr) { //issues with os 9 don't do CFStringRef jobName = CFSTR("Squeak"); //issues with os 9 don't do status = PMSetJobNameCFString(printJob->printSettings, jobName); // Get the user's Print dialog selection for first and last pages to print. if (status == noErr) { status = PMGetFirstPage(printJob->printSettings, &printJob->firstPage); if (status == noErr) status = PMGetLastPage(printJob->printSettings, &printJob->lastPage); } // Check that the selected page range does not exceed the actual number of // pages in the document. if (status == noErr) { status = DetermineNumberOfPagesInDoc(&realNumberOfPagesinDoc,printJob); if (realNumberOfPagesinDoc < printJob->lastPage) printJob->lastPage = realNumberOfPagesinDoc; } // Before executing the print loop, tell the Carbon Printing Manager which pages // will be spooled so that the progress dialog can reflect an accurate page count. // This is recommended on Mac OS X. On Mac OS 8 and 9, we have no control over // what the printer driver displays. if (status == noErr) status = PMSetFirstPage(printJob->printSettings, printJob->firstPage, false); if (status == noErr) status = PMSetLastPage(printJob->printSettings, printJob->lastPage, false); if (status == noErr) status = PMSetPageRange(printJob->printSettings, 1, printJob->lastPage-printJob->firstPage+1); // Check if we can add PostScript to the spool file if (status == noErr) { CFStringRef strings[1]; CFArrayRef ourGraphicsContextsArray; OSErr err = noErr; printJob->allowPostscript = true; //IncludePostScriptInSpoolFile(printJob); // // at this point you've already created a print session // strings[0] = kPMGraphicsContextCoreGraphics; // This is important! ourGraphicsContextsArray = CFArrayCreate (kCFAllocatorDefault, (const void **)strings, 1, &kCFTypeArrayCallBacks); if (ourGraphicsContextsArray != NULL) { err = PMSessionSetDocumentFormatGeneration (printJob->printSession, kPMDocumentFormatPDF, ourGraphicsContextsArray, NULL); CFRelease (ourGraphicsContextsArray); } } // Begin a new print job. status = PMSessionBeginDocument(printJob->printSession, printJob->printSettings, printJob->pageFormat); } return status; }