// -------------------------------------------------------------------------------------------------------------- OSStatus DoPrint(WindowRef parentWindow, void *ourDataP) { OSStatus status = noErr; PMPrintSettings printSettings = NULL; PMPageFormat pageFormat = NULL; UInt32 minPage = 1, maxPage; PMPrintSession printSession; status = PMCreateSession(&printSession); if(status == noErr){ status = MySetupPageFormatForPrinting(printSession, ourDataP, &pageFormat); if (status == noErr) { status = PMCreatePrintSettings(&printSettings); if(status == noErr) { status = PMSessionDefaultPrintSettings(printSession, printSettings); if(status == noErr){ CFStringRef windowTitleRef; status = CopyWindowTitleAsCFString(parentWindow, &windowTitleRef); if(status == noErr) { // set the job name before displaying the print dialog status = PMSetJobNameCFString (printSettings, windowTitleRef); CFRelease (windowTitleRef); } } } if (status == noErr) { maxPage = MyGetDocumentNumPagesInDoc(ourDataP); status = PMSetPageRange(printSettings, minPage, maxPage); } if (status == noErr) { Boolean accepted; SetPrintSettingsOnPrivateData(ourDataP, printSettings); status = PMSessionUseSheets(printSession, parentWindow, gMyPrintDialogDoneProc); if(status == noErr) status = PMSessionPrintDialog(printSession, printSettings, pageFormat, &accepted); // when using sheets, the value of 'accepted' returned here is irrelevant // since it is our sheet done proc that is called when the dialog is dismissed } } if(status != noErr){ // if we got an error our dialog done proc will not be called so we need to clean up if(printSettings){ SetPrintSettingsOnPrivateData(ourDataP, NULL); (void)PMRelease(printSettings); } (void)PMRelease(printSession); } } DoErrorAlert(status, kMyPrintErrorFormatStrKey); return status; }
OSStatus OpenPrinterAndValidate(void) { OSStatus err = noErr; #if TARGET_API_MAC_CARBON ///////////////////////////////////////////////// err = PMCreateSession(&gPrintSession); if(err) return err; // get and validate the page formatting if (gPageFormat == kPMNoPageFormat) { // Set up a valid PageFormat object err = PMCreatePageFormat(&gPageFormat); // Note that PMPageFormat is not session-specific, but calling // PMSessionDefaultPageFormat assigns values specific to the printer // associated with the current printing session. if ((err == noErr) && (gPageFormat != kPMNoPageFormat)) err = PMSessionDefaultPageFormat(gPrintSession, gPageFormat); } else{ err = PMSessionValidatePageFormat(gPrintSession, gPageFormat, kPMDontWantBoolean); } if(err) return err; ///////////////////////////////////////////////// // let's also get and validate the print settings if (gPrintSettings == kPMNoPrintSettings) { err = PMCreatePrintSettings(&gPrintSettings); // Note that PMPrintSettings is not session-specific, but calling // PMSessionDefaultPrintSettings assigns values specific to the printer // associated with the current printing session. if ((err == noErr) && (gPrintSettings != kPMNoPrintSettings)) err = PMSessionDefaultPrintSettings(gPrintSession, gPrintSettings); } else { err = PMSessionValidatePrintSettings(gPrintSession, gPrintSettings, kPMDontWantBoolean); } return err; #else //// MACB4CARBON //////////////////////////////////////// ///////////////////////////////////////////////// PrOpen(); err = PrError(); if(err) return err; // always use the global if(gPrRecHdl == nil){ gPrRecHdl = (THPrint)_NewHandleClear(sizeof(TPrint)); if(!gPrRecHdl)return memFullErr; PrintDefault(gPrRecHdl); } PrValidate(gPrRecHdl); // We ignore the returned value return(noErr); #endif }
//----------------------------------------------------------------------------------------------------------------------- // (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
void wxMacCarbonPrintData::ValidateOrCreate() { OSStatus err = noErr ; if ( m_macPrintSession == kPMNoReference ) { err = PMCreateSession( (PMPrintSession *) &m_macPrintSession ) ; } // Set up a valid PageFormat object. if ( m_macPageFormat == kPMNoPageFormat) { err = PMCreatePageFormat((PMPageFormat *) &m_macPageFormat); // Note that PMPageFormat is not session-specific, but calling // PMSessionDefaultPageFormat assigns values specific to the printer // associated with the current printing session. if ((err == noErr) && ( m_macPageFormat != kPMNoPageFormat)) { err = PMSessionDefaultPageFormat((PMPrintSession) m_macPrintSession, (PMPageFormat) m_macPageFormat); } } else { err = PMSessionValidatePageFormat((PMPrintSession) m_macPrintSession, (PMPageFormat) m_macPageFormat, kPMDontWantBoolean); } // Set up a valid PrintSettings object. if ( m_macPrintSettings == kPMNoPrintSettings) { err = PMCreatePrintSettings((PMPrintSettings *) &m_macPrintSettings); // Note that PMPrintSettings is not session-specific, but calling // PMSessionDefaultPrintSettings assigns values specific to the printer // associated with the current printing session. if ((err == noErr) && ( m_macPrintSettings != kPMNoPrintSettings)) { err = PMSessionDefaultPrintSettings((PMPrintSession) m_macPrintSession, (PMPrintSettings) m_macPrintSettings); } } else { err = PMSessionValidatePrintSettings((PMPrintSession) m_macPrintSession, (PMPrintSettings) m_macPrintSettings, kPMDontWantBoolean); } }
wxOSXCarbonPrintData::wxOSXCarbonPrintData() { if ( PMCreateSession( &m_macPrintSession ) == noErr ) { if ( PMCreatePageFormat(&m_macPageFormat) == noErr ) { PMSessionDefaultPageFormat(m_macPrintSession, m_macPageFormat); PMGetPageFormatPaper(m_macPageFormat, &m_macPaper); } if ( PMCreatePrintSettings(&m_macPrintSettings) == noErr ) { PMSessionDefaultPrintSettings(m_macPrintSession, m_macPrintSettings); } } }
static void initPrinter () { Boolean result; PMResolution res300 = { 300, 300 }, res600 = { 600, 600 }; if (! theMacPrintSettings) { // once PMCreateSession (& theMacPrintSession); // initialize the Printing Manager PMCreatePageFormat (& theMacPageFormat); PMCreatePrintSettings (& theMacPrintSettings); PMSessionDefaultPageFormat (theMacPrintSession, theMacPageFormat); PMSessionDefaultPrintSettings (theMacPrintSession, theMacPrintSettings); } PMSessionValidatePageFormat (theMacPrintSession, theMacPageFormat, & result); PMSessionValidatePrintSettings (theMacPrintSession, theMacPrintSettings, & result); /* * BUG. * If we now ask for the available printer resolutions, * we may get the answer that there's only 300 dpi (perhaps PostScript drivers say so?). * So we don't rely on that and have a buggy assumption instead. */ PMSetResolution (theMacPageFormat, & res300); // perhaps all printers have this... PMSetResolution (theMacPageFormat, & res600); // ... but this is preferred }
bool QPrinter::prepare(PMPrintSettings *s) { if (!psession && PMCreateSession(&psession) != noErr) return FALSE; if (*s == kPMNoPrintSettings) { if (PMCreatePrintSettings(s) != noErr) return FALSE; if (PMSessionDefaultPrintSettings(psession, *s) != noErr) return FALSE; } else { if (PMSessionValidatePrintSettings(psession, *s, kPMDontWantBoolean) != noErr) return FALSE; } if (minPage() != 0 || maxPage() != 0) PMSetPageRange(*s, minPage(), maxPage()); if (d->printRange != AllPages) { PMSetFirstPage(*s, fromPage(), TRUE); PMSetLastPage(*s, toPage(), TRUE); } PMSetColorMode(*s, colorMode() == GrayScale ? kPMGray : kPMColor); PMSetCopies(*s, numCopies(), TRUE); if (outputToFile()) { CFStringRef cfstring = qstring2cfstring(outputFileName()); CFURLRef outFile = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, cfstring, kCFURLPOSIXPathStyle, false); PMSessionSetDestination(psession, *s, kPMDestinationFile, kPMDocumentFormatPDF, outFile); CFRelease(outFile); CFRelease(cfstring); } QString printName = printerName(); if (!printName.isEmpty()) { CFStringRef pname = qstring2cfstring(printName); PMSessionSetCurrentPrinter(psession, pname); CFRelease(pname); } return TRUE; }
static boolean carbonCreateFormatAndSetting(void) { #if TARGET_API_MAC_CARBON == 1 OSStatus theErr; theErr = PMCreatePageFormat(&shellprintinfo.pageformat); if(theErr != noErr || (shellprintinfo.pageformat == kPMNoPageFormat)) goto error; theErr = PMCreatePrintSettings(&shellprintinfo.printsettings); if(theErr != noErr) goto error; return (true); error: carbonKillPrintVars(); #endif return false; }
/*------------------------------------------------------------------------------ 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
OSStatus MakePDFDocument(WindowRef parentWindow, void *documentDataP, CFURLRef saveURL) { #pragma unused (parentWindow) OSStatus err = noErr, tempErr; PMPrintSettings printSettings = NULL; PMPrintSession printSession; err = PMCreateSession(&printSession); if(err == noErr){ // validate the page format we're going to use err = PMSessionValidatePageFormat(printSession, GetPageFormatFromPrivateData(documentDataP), kPMDontWantBoolean); if (err == noErr) { err = PMCreatePrintSettings(&printSettings); if(err == noErr) { err = PMSessionDefaultPrintSettings(printSession, printSettings); if(err == noErr){ CFStringRef myDocumentNameRef; err = CopyDocumentName(documentDataP, &myDocumentNameRef); if(err == noErr) { // set the job name err = PMSetJobNameCFString (printSettings, myDocumentNameRef); CFRelease(myDocumentNameRef); // release our reference to the document name } } if (err == noErr) err = PMSetPageRange(printSettings, 1, MyGetDocumentNumPagesInDoc(documentDataP)); // set our destination to be our target URL and the format to be PDF if(err == noErr){ // this API exists ONLY in MacOS X 10.1 and later only if(!err) err = Call_PMSessionSetDestination(printSession, printSettings, kPMDestinationFile, kPMDocumentFormatPDF, saveURL); } #if !NO_SAVE_STATUS_DIALOG /* It looks a bit odd to call PMSessionUseSheets when we aren't even using the print dialog BUT the reason we are doing so is so that the printing status narration will use sheets and not a modal dialog. NOTE: this is only called when this code is built with !NO_SAVE_STATUS_DIALOG is true, i.e. we want status narration of the save to PDF process. */ if(err == noErr){ err = PMSessionUseSheets(printSession, parentWindow, NULL); } #endif } if (err == noErr) { err = MyDoPrintLoop(printSession, GetPageFormatFromPrivateData(documentDataP), printSettings, documentDataP, #if NO_SAVE_STATUS_DIALOG &gMyPrintingProcsNoStatusDialog #else &gMyPrintingProcsWithStatusDialog #endif ); } } if(printSettings){ tempErr = PMRelease(printSettings); if(err == noErr) err = tempErr; } tempErr = PMRelease(printSession); if(err == noErr) err = tempErr; } DoErrorAlert(err, kMySaveAsPDFErrorFormatStrKey); return err; }
// ------------------------------------------------------------------------------- OSStatus DoPrint(WindowRef parentWindow, void *documentDataP, Boolean printOne) { OSStatus err = noErr; PMPrintSettings printSettings = NULL; UInt32 minPage = 1, maxPage; PMPrintSession printSession; err = PMCreateSession(&printSession); if(err == noErr){ // validate the page format we're going to use err = PMSessionValidatePageFormat(printSession, GetPageFormatFromPrivateData(documentDataP), kPMDontWantBoolean); if (err == noErr) { err = PMCreatePrintSettings(&printSettings); if(err == noErr) { err = PMSessionDefaultPrintSettings(printSession, printSettings); if(err == noErr){ CFStringRef myDocumentNameRef; err = CopyDocumentName(documentDataP, &myDocumentNameRef); if(err == noErr) { // set the job name before displaying the print dialog err = PMSetJobNameCFString (printSettings, myDocumentNameRef); CFRelease(myDocumentNameRef); // release our reference to the document name } } } if (err == noErr) { /* On Mac OS X, calling PMSetPageRange has the following benefits: (a) sets the From/To settings in the print dialog to the range of pages in the document AND (b) the print dialog code enforces this so that users can't enter values outside this range. */ maxPage = MyGetDocumentNumPagesInDoc(documentDataP); err = PMSetPageRange(printSettings, minPage, maxPage); } if (err == noErr) { Boolean accepted; static PMSheetDoneUPP myPrintDialogDoneProc = NULL; if(!myPrintDialogDoneProc){ myPrintDialogDoneProc = NewPMSheetDoneUPP(MyPrintDialogDoneProc); if(!myPrintDialogDoneProc) err = memFullErr; } if(!err){ Boolean sheetsAreAvailable = true; err = SetPrintSettingsOnPrivateData(documentDataP, printSettings); if(!err){ err = PMSessionUseSheets(printSession, parentWindow, myPrintDialogDoneProc); if(err == kPMNotImplemented){ // we get here if sheets are not available, i.e. Mac OS 8/9 err = noErr; sheetsAreAvailable = false; } if(err == noErr && !printOne){ err = PMSessionPrintDialog(printSession, printSettings, GetPageFormatFromPrivateData(documentDataP), &accepted); /* when using sheets, the value of 'accepted' returned here is irrelevant since it is our sheet done proc that is called when the dialog is dismissed. Our dialog done proc will be called when the sheet is dismissed. If sheets are NOT implemented then WE call our DialogDone proc here to complete the dialog. */ if(err == noErr && !sheetsAreAvailable) MyPrintDialogDoneProc(printSession, parentWindow, accepted); }else{ // if we are doing print one we have no dialog, therefore // we have to call our dialog done proc since there is no // dialog to do so for us if(err == noErr) MyPrintDialogDoneProc(printSession, parentWindow, true); } } } } } // need to release the print settings this function created. if(printSettings){ OSStatus tempErr = PMRelease(printSettings); if(err == noErr) err = tempErr; printSettings = NULL; } if(err != noErr){ /* normally the printSettings set in the Private Data and printSession would be released by our dialog done proc but if we got an error and therefore got to this point in our code, the dialog done proc was NOT called and therefore we need to release the printSession here */ if(printSettings){ // this releases any print settings already stored on our private data (void)SetPrintSettingsOnPrivateData(documentDataP, NULL); } (void)PMRelease(printSession); // ignoring error since we already have one } } DoErrorAlert(err, kMyPrintErrorFormatStrKey); return err; }
void QMacPrintEnginePrivate::initialize() { Q_ASSERT(!session); Q_Q(QMacPrintEngine); if (!paintEngine) paintEngine = new QCoreGraphicsPaintEngine(); q->gccaps = paintEngine->gccaps; fullPage = false; if (PMCreateSession(&session) != noErr) session = 0; PMPrinter printer; if (session && PMSessionGetCurrentPrinter(session, &printer) == noErr) { QList<QVariant> resolutions = supportedResolutions(); if (!resolutions.isEmpty() && mode != QPrinter::ScreenResolution) { if (resolutions.count() > 1 && mode == QPrinter::HighResolution) { int max = 0; for (int i = 0; i < resolutions.count(); ++i) { int value = resolutions.at(i).toInt(); if (value > max) max = value; } resolution.hRes = resolution.vRes = max; } else { resolution.hRes = resolution.vRes = resolutions.at(0).toInt(); } if(resolution.hRes == 0) resolution.hRes = resolution.vRes = 600; } else { resolution.hRes = resolution.vRes = qt_defaultDpi(); } } bool settingsInitialized = (settings != 0); bool settingsOK = !settingsInitialized ? PMCreatePrintSettings(&settings) == noErr : true; if (settingsOK && !settingsInitialized) settingsOK = PMSessionDefaultPrintSettings(session, settings) == noErr; bool formatInitialized = (format != 0); bool formatOK = !formatInitialized ? PMCreatePageFormat(&format) == noErr : true; if (formatOK) { if (!formatInitialized) { formatOK = PMSessionDefaultPageFormat(session, format) == noErr; } formatOK = PMSessionValidatePageFormat(session, format, kPMDontWantBoolean) == noErr; } #ifndef Q_OS_MAC64 # if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4) if (QSysInfo::MacintoshVersion < QSysInfo::MV_10_4) # endif { if(paintEngine->type() == QPaintEngine::CoreGraphics) { CFStringRef strings[1] = { kPMGraphicsContextCoreGraphics }; QCFType<CFArrayRef> contextArray = CFArrayCreate(kCFAllocatorDefault, reinterpret_cast<const void **>(strings), 1, &kCFTypeArrayCallBacks); OSStatus err = PMSessionSetDocumentFormatGeneration(session, kPMDocumentFormatPDF, contextArray, 0); if(err != noErr) { qWarning("QMacPrintEngine::initialize: Cannot set format generation to PDF: %ld", err); state = QPrinter::Error; } } } #endif if (!settingsOK || !formatOK) { qWarning("QMacPrintEngine::initialize: Unable to initialize QPainter"); state = QPrinter::Error; } QHash<QMacPrintEngine::PrintEnginePropertyKey, QVariant>::const_iterator propC; for (propC = valueCache.constBegin(); propC != valueCache.constEnd(); propC++) { q->setProperty(propC.key(), propC.value()); } }