// --------------------------------------------------------------------------------------------- static pascal void MyPrintDialogDoneProc(PMPrintSession printSession, WindowRef documentWindow, Boolean accepted) { OSStatus err = noErr, tempErr; void *ourDataP = GetOurWindowProperty(documentWindow); if(ourDataP) { PMPrintSettings printSettings = GetPrintSettingsFromPrivateData(ourDataP); // only run the print loop if the user accepted the print dialog. if(accepted){ err = MyDoPrintLoop(printSession, GetPageFormatFromPrivateData(ourDataP), printSettings, ourDataP, &gMyPrintingProcsWithStatusDialog); } // We're done with the print settings on our private data so // set them to NULL which causes the print settings to be released tempErr = SetPrintSettingsOnPrivateData(ourDataP, NULL); if(err == noErr) err = tempErr; }// else Assert(...); // now we release the session we created to do the Print dialog tempErr = PMRelease(printSession); if(err == noErr) err = tempErr; DoErrorAlert(err, kMyPrintErrorFormatStrKey); }
// -------------------------------------------------------------------------------------------------------------- static pascal void MyPrintDialogDoneProc(PMPrintSession printSession, WindowRef documentWindow, Boolean accepted) { OSStatus status = noErr, tempErr; void *ourDataP = GetOurWindowProperty(documentWindow); if(ourDataP) { PMPrintSettings printSettings = GetPrintSettingsFromPrivateData(ourDataP); // only run the print loop if the user accepted the print dialog. if(accepted) status = MyDoPrintLoop(printSession, GetPageFormatFromPrivateData(ourDataP), printSettings, ourDataP); SetPrintSettingsOnPrivateData(ourDataP, NULL); tempErr = PMRelease(printSettings); if(status == noErr) status = tempErr; } // now we release the session we created to do the Print dialog tempErr = PMRelease(printSession); if(status == noErr) status = tempErr; DoErrorAlert(status, kMyPrintErrorFormatStrKey); }
// -------------------------------------------------------------------------------------------------------------- OSStatus DoPageSetup(WindowRef window, void *docDataP) { OSStatus err = noErr; if(docDataP){ PMPrintSession printSession; PMPageFormat pageFormat = NULL; err = PMCreateSession(&printSession); if(!err){ Boolean accepted; err = MySetupPageFormatForPrinting(printSession, docDataP, &pageFormat); if(!err){ Cursor arrow; SetCursor(GetQDGlobalsArrow(&arrow)); err = PMSessionUseSheets(printSession, window, gMyPageSetupDoneProc); if(!err) err = PMSessionPageSetupDialog(printSession, 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(err){ // only if there is an error do we release the session // otherwise we'll do that in our sheet done proc (void)PMRelease(printSession); } } } DoErrorAlert(err, kMyPrintErrorFormatStrKey); return err; } // DoPageSetup
// -------------------------------------------------------------------------------------------------------------- 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; }
// -------------------------------------------------------------------------------------------------------------- static pascal void MyPageSetupDoneProc(PMPrintSession printSession, WindowRef documentWindow, Boolean accepted) { // this sample doesn't do anything with the page format after page setup is done // now we release the session we created to do the page setup dialog OSStatus err = PMRelease(printSession); if(err) DoErrorAlert(err, kMyPrintErrorFormatStrKey); return; }
// ----------------------------------------------------------------- OSStatus DoPageSetup(WindowRef window, void *docDataP) { OSStatus err = noErr; if(docDataP){ PMPrintSession printSession; err = PMCreateSession(&printSession); if(!err){ Boolean accepted; static PMSheetDoneUPP myPageSetupDoneProc = NULL; if(!myPageSetupDoneProc){ myPageSetupDoneProc = NewPMSheetDoneUPP(MyPageSetupDoneProc); if(!myPageSetupDoneProc){ err = memFullErr; } } if(!err) // validate the page format we're going to pass to the dialog code err = PMSessionValidatePageFormat(printSession, GetPageFormatFromPrivateData(docDataP), kPMDontWantBoolean); if(!err){ Boolean sheetsAreAvailable = true; err = PMSessionUseSheets(printSession, window, myPageSetupDoneProc); if(err == kPMNotImplemented){ // sheets are not available (aka, Mac OS 8/9) err = noErr; sheetsAreAvailable = false; } if(!err){ err = PMSessionPageSetupDialog(printSession, GetPageFormatFromPrivateData(docDataP), &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) MyPageSetupDoneProc(printSession, window, accepted); } } if(err){ // only if there is an error do we release the session, // otherwise we'll do that in our sheet done proc (void)PMRelease(printSession); } } } DoErrorAlert(err, kMyPrintErrorFormatStrKey); return err; } // DoPageSetup
// -------------------------------------------------------------------------------------------------------------- static OSStatus MySetupPageFormatForPrinting(PMPrintSession printSession, void *docDataP, PMPageFormat *pageFormatP) { OSStatus status = noErr; PMPageFormat pageFormat = GetPageFormatFromPrivateData(docDataP); if (!pageFormat) { status = PMCreatePageFormat(&pageFormat); if(status == noErr) { status = PMSessionDefaultPageFormat(printSession, pageFormat); if(status == noErr){ PMResolution appDrawingResolution; // for illustrative purposes, this sample will draw at 300,300 dpi resolution appDrawingResolution.hRes = appDrawingResolution.vRes = 300.; status = PMSetResolution(pageFormat, &appDrawingResolution); } if (status == noErr) SetPageFormatOnPrivateData(docDataP, pageFormat); else{ (void)PMRelease(pageFormat); pageFormat = NULL; } } }else{ // we already have a page format so we'll validate it status = PMSessionValidatePageFormat(printSession, pageFormat, kPMDontWantBoolean); if(status){ // if validate failed! SetPageFormatOnPrivateData(docDataP, NULL); (void)PMRelease(pageFormat); pageFormat = NULL; } } DoErrorAlert(status, kMyPrintErrorFormatStrKey); *pageFormatP = pageFormat; return status; }
static OSStatus DoFSRefSave(const OurSaveDialogData *dialogDataP, NavReplyRecord* reply, AEDesc *actualDescP) { OSStatus err = noErr; FSRef fileRefParent; if ((err = AEGetDescData( actualDescP, &fileRefParent, sizeof( FSRef ) )) == noErr ) { // get the name data and its length: HFSUniStr255 nameBuffer; UniCharCount sourceLength = 0; sourceLength = (UniCharCount)CFStringGetLength( reply->saveFileName ); CFStringGetCharacters( reply->saveFileName, CFRangeMake( 0, sourceLength ), (UniChar*)&nameBuffer.unicode ); if ( sourceLength > 0 ) { if ( reply->replacing ) { // delete the file we are replacing: FSRef fileToDelete; if ((err = FSMakeFSRefUnicode( &fileRefParent, sourceLength, nameBuffer.unicode, kTextEncodingUnicodeDefault, &fileToDelete )) == noErr ) { err = FSDeleteObject( &fileToDelete ); if ( err == fBsyErr ){ DoErrorAlert(fBsyErr, kMyDeleteErrorFormatStrKey); } } } if ( err == noErr ) { // create the file based on Unicode, but we can write the file's data with an FSSpec: FSSpec newFileSpec; // get the FSSpec back so we can write the file's data if ((err = FSCreateFileUnicode( &fileRefParent, sourceLength, nameBuffer.unicode, kFSCatInfoNone, NULL, NULL, &newFileSpec)) == noErr) { FInfo fileInfo; fileInfo.fdType = kFileTypePDF; fileInfo.fdCreator = kFileCreator; err = FSpSetFInfo( &newFileSpec, &fileInfo ); // now that we have the FSSpec, we can proceed with the save operation: if(!err){ FSRef fsRef; err = FSpMakeFSRef(&newFileSpec, &fsRef); // make an FSRef if(!err){ CFURLRef saveURL = CFURLCreateFromFSRef(NULL, &fsRef); if(saveURL){ // delete the file we just made for making the FSRef err = FSpDelete(&newFileSpec); if(!err) err = MakePDFDocument(dialogDataP->parentWindow, dialogDataP->documentDataP, saveURL); if(!err) err = NavCompleteSave( reply, kNavTranslateInPlace ); if(err){ // an error ocurred saving the file, so delete the copy // left over: (void)FSpDelete( &newFileSpec ); DoErrorAlert(err, kMyWriteErrorFormatStrKey); } CFRelease(saveURL); }else{ // delete the file we just made for making the FSRe (void)FSpDelete(&newFileSpec); err = kCantCreateSaveURL; DoErrorAlert(err, kMyCreateURLErrorFormatStrKey); } } } } } } } return err; }
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; }