// -------------------------------------------------------------------------------------------------------------- OSStatus CreateSheetDoneProcs(void) { OSStatus err = noErr; gMyPageSetupDoneProc = NewPMSheetDoneUPP(MyPageSetupDoneProc); if(!gMyPageSetupDoneProc) err = memFullErr; if(!err){ gMyPrintDialogDoneProc = NewPMSheetDoneUPP(MyPrintDialogDoneProc); if(!gMyPrintDialogDoneProc) err = memFullErr; } return err; }
// ----------------------------------------------------------------- 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
// ------------------------------------------------------------------------------- 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; }