void wxOSXPrintData::TransferTo( wxPrintDialogData* data ) { #if wxOSX_USE_COCOA UpdateToPMState(); #endif UInt32 minPage , maxPage ; PMGetPageRange( m_macPrintSettings , &minPage , &maxPage ) ; data->SetMinPage( minPage ) ; data->SetMaxPage( maxPage ) ; UInt32 copies ; PMGetCopies( m_macPrintSettings , &copies ) ; data->SetNoCopies( copies ) ; UInt32 from , to ; PMGetFirstPage( m_macPrintSettings , &from ) ; PMGetLastPage( m_macPrintSettings , &to ) ; if ( to >= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages { data->SetAllPages( true ) ; // This means all pages, more or less data->SetFromPage(1); data->SetToPage(9999); } else { data->SetFromPage( from ) ; data->SetToPage( to ) ; data->SetAllPages( false ); } }
void wxMacCarbonPrintData::TransferTo( wxPrintDialogData* data ) { UInt32 minPage , maxPage ; PMGetPageRange( m_macPrintSettings , &minPage , &maxPage ) ; data->SetMinPage( minPage ) ; data->SetMaxPage( maxPage ) ; UInt32 copies ; PMGetCopies( m_macPrintSettings , &copies ) ; data->SetNoCopies( copies ) ; UInt32 from , to ; PMGetFirstPage( m_macPrintSettings , &from ) ; PMGetLastPage( m_macPrintSettings , &to ) ; data->SetFromPage( from ) ; data->SetToPage( to ) ; }
void QPrinter::interpret(PMPrintSettings *s) { //get values UInt32 from, to; if (PMGetFirstPage(*s, &from) == noErr && PMGetLastPage(*s, &to) == noErr) { if (to == INT_MAX) //this means all pages! from = to = 0; setFromTo(from, to); } UInt32 copies; if (PMGetCopies(*s, &copies) == noErr) setNumCopies(copies); UInt32 max, min; if (PMGetPageRange(*s, &min, &max) == noErr) { // The problem here is that Apple's print dialog will always show a one even when you pass // it a zero. So take care of that. The other problem is that we can also call this because // we need to interpret the default settings, in that case, the min and max will both be // zero. In that case, leave them "as is" as some other functions will reset the max and // make everything OK when the dialog is actually called. int newMin = min; int newMax = max; if (newMin < 1 && newMax != 0) newMin = 1; setMinMax(newMin, newMax); } PMColorMode cm; if (PMGetColorMode(*s, &cm) == noErr) setColorMode(cm == kPMGray ? GrayScale : Color); // Get the current Printer Name CFIndex currPrinterIndex; PMPrinter currPrinter; CFArrayRef printerArray; OSStatus err = PMSessionCreatePrinterList(psession, &printerArray, &currPrinterIndex, &currPrinter); if (err != noErr) qWarning("Qt: QPrinter::interpret problem creating printer list %ld", err); if (printerArray) { setPrinterName(cfstring2qstring((CFStringRef)CFArrayGetValueAtIndex(printerArray, currPrinterIndex))); CFRelease(printerArray); } }
//----------------------------------------------------------------------------------------------------------------------- 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
boolean shellprint (WindowPtr w, boolean fldialog) { /* 9/5/90 dmb: open and close print resources each time we're called. also, make sure we close each page opened, even when errors occur 9/28/91 dmb: for exit of edit mode before printing 4/24/92 dmb: make sure bJDocLoop is bSpoolLoop before calling PrPicFile, as per IM II-155. otherwise, we can get bogus PrErrors */ OSStatus theErr; boolean accepted; //remember that this is a grafport, TPPrPort isn't //but this should work because all the code that //messes with printing uses a grafport. // CGrafPtr printport; UInt32 minPage = 1, maxPage = 9999; SInt32 firstPage = 1; SInt32 lastPage = 9999; short i; boolean fl = false; if (w == nil) /*defensive driving*/ return (false); if (!carbonValidSession()) { carbonStdSetup(); } fl = false; /*until sucessfull print, this is return value*/ /*fills in fields of printinfo record*/ (*shellglobals.setprintinfoproutine) (); //this only counts the number of pages. maxPage = shellprintinfo.ctpages; //set the page range theErr = PMSetPageRange(shellprintinfo.printsettings, minPage, maxPage); if(theErr != noErr){ goto exit; } //finally display the dialog if (fldialog) { theErr = PMSessionPrintDialog( shellprintinfo.printhandle, shellprintinfo.printsettings, shellprintinfo.pageformat, &accepted); //either the user canceled or their was some other error. //don't print if (!accepted || (theErr != noErr)){ goto exit; } } setcursortype (cursoriswatch); shellupdateallnow (); /*update all windows that were dirtied by the print dialog*/ shellpushglobals (w); (*shellglobals.settextmoderoutine) (false); /*make sure editing changes are accepted*/ pushport (nil); /*save current port on stack*/ //Code change by Timothy Paustian Friday, June 16, 2000 4:19:04 PM //Changed to Opaque call for Carbon //I will have to watch out for this, //to save the port I need to use.. //PMSessionGetGraphicsContext ( //This does not return the printport, but TPrPort gPort field //I think I can get away with this because the only function that uses //it is pgInitDevice and that assumes you are passing a port. //It is OK in the old code because the first item in the struct is a port //we have to call this first before we can get the graphics context theErr = PMSessionBeginDocument( shellprintinfo.printhandle, shellprintinfo.printsettings, shellprintinfo.pageformat); if(theErr != noErr) goto exit; /*prepares for printing*/ (*shellglobals.beginprintroutine) (); /*fills in fields of printinfo record*/ // (*shellglobals.setprintinfoproutine) (); //this only counts the number of pages. //limit this to the number of pages the user asked for. theErr = PMGetFirstPage(shellprintinfo.printsettings, (UInt32 *) (&firstPage)); if (theErr == noErr) { theErr = PMGetLastPage(shellprintinfo.printsettings, (UInt32 *) (&lastPage)); } //sanity checks if(theErr != noErr) goto exit; if(lastPage > shellprintinfo.ctpages) lastPage = shellprintinfo.ctpages; for (i = 1; i <= lastPage; i++) { /*print one page*/ theErr = PMSessionBeginPage( shellprintinfo.printhandle, shellprintinfo.pageformat, &shellprintinfo.pagerect); if (theErr != noErr) break; //now set the graphics context. //in carbon we have to set up the port each time //to make sure it is in the printer port. Since other things can be going on //during the printing, the port may have changed. { GrafPtr thePort, oldPort; GetPort(&oldPort); //the second parameter is currently ignored. theErr = PMSessionGetGraphicsContext( shellprintinfo.printhandle, kPMGraphicsContextQuickdraw, (void **) &thePort); shellprintinfo.printport = thePort; SetPort(thePort); // Draw the page SetFractEnable (true); fl = (*shellglobals.printroutine) (i); //reset back to the old port. SetPort(oldPort); } theErr = PMSessionEndPage(shellprintinfo.printhandle); if (!fl) break; if (keyboardescape ()) { theErr = PMSessionSetError(shellprintinfo.printhandle, iPrAbort); } } /*for*/ theErr = PMSessionEndDocument(shellprintinfo.printhandle); if(theErr != noErr) goto exit; //everything worked and printing is done. so set fl to true. fl = true; //the PrPicFile is not supported in carbon. popport (); (*shellglobals.endprintroutine) (); shellpopglobals (); exit: carbonKillPrintVars(); // PMRelease(shellprintinfo.printhandle); // PMRelease(pageformat); // PMRelease(printsettings); currentprintport = NULL; return (fl); } /*shellprint*/
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; }
// -------------------------------------------------------------------------------------- static OSStatus MyDoPrintLoop(PMPrintSession printSession, PMPageFormat pageFormat, PMPrintSettings printSettings, const void *ourDataP, const PrintingProcs *printingProcsP) { OSStatus err = noErr; OSStatus tempErr = noErr; UInt32 firstPage, lastPage, totalDocPages = MyGetDocumentNumPagesInDoc(ourDataP); if(!err) err = PMGetFirstPage(printSettings, &firstPage); if (!err) err = PMGetLastPage(printSettings, &lastPage); if(!err && lastPage > totalDocPages){ // don't draw more than the number of pages in our document lastPage = totalDocPages; } if (!err) // tell the printing system the number of pages we are going to print err = 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 (!err) { PageDrawProc *drawProc = GetMyDrawPageProc(ourDataP); err = printingProcsP->BeginDocumentProc(printSession, printSettings, pageFormat); if (!err){ UInt32 pageNumber = firstPage; // need to check errors from our print loop and errors from the session for each // time around our print loop before calling our BeginPageProc while(pageNumber <= lastPage && err == noErr && PMSessionError(printSession) == noErr) { err = printingProcsP->BeginPageProc(printSession, pageFormat, NULL); if (!err){ GrafPtr oldPort = NULL; void *printingContext = NULL; GetPort(&oldPort); // preserve the existing port err = PMSessionGetGraphicsContext(printSession, kPMGraphicsContextQuickdraw, (void **)&printingContext); if(!err){ Rect pageRect; SetPort((CGrafPtr)printingContext); GetPortBounds(printingContext, &pageRect); err = drawProc(ourDataP, &pageRect, pageNumber); // image the correct page SetPort(oldPort); // restore the prior port } // we must call EndPage if BeginPage returned noErr tempErr = printingProcsP->EndPageProc(printSession); if(!err)err = tempErr; } pageNumber++; } // end while loop // we must call EndDocument if BeginDocument returned noErr tempErr = printingProcsP->EndDocumentProc(printSession); if(!err)err = tempErr; if(!err) err = PMSessionError(printSession); } } return err; }
// -------------------------------------------------------------------------------------------------------------- static OSStatus MyDoPrintLoop(PMPrintSession printSession, PMPageFormat pageFormat, PMPrintSettings printSettings, const void *ourDataP) { OSStatus err = noErr; OSStatus tempErr = noErr; UInt32 firstPage, lastPage, totalDocPages = MyGetDocumentNumPagesInDoc(ourDataP); PMResolution res; float ourAppScaling = 1.; err = PMGetResolution(pageFormat, &res); if(!err){ ourAppScaling = res.hRes/72.; // the scale factor we are applying } if(!err) err = PMGetFirstPage(printSettings, &firstPage); if (!err) err = PMGetLastPage(printSettings, &lastPage); if(!err && lastPage > totalDocPages){ // don't draw more than the number of pages in our document lastPage = totalDocPages; } if (!err) // tell the printing system the number of pages we are going to print err = PMSetLastPage(printSettings, lastPage, false); if (!err) { PageDrawProc *drawProc = GetMyDrawPageProc(ourDataP); err = PMSessionBeginDocument(printSession, printSettings, pageFormat); if (!err){ UInt32 pageNumber; for(pageNumber = firstPage; err == noErr && (err = PMSessionError(printSession)) == noErr && pageNumber <= lastPage; pageNumber++ ){ err = PMSessionBeginPage(printSession, pageFormat, NULL); if (!err){ GrafPtr oldPort = NULL; void *printingContext = NULL; GetPort(&oldPort); // preserve the existing port err = PMSessionGetGraphicsContext(printSession, kPMGraphicsContextQuickdraw, (void **)&printingContext); if(!err){ SetPort((CGrafPtr)printingContext); err = drawProc(ourDataP, ourAppScaling); // supply app drawing resolution SetPort(oldPort); // restore the prior port } tempErr = PMSessionEndPage(printSession); if(!err)err = tempErr; } } // end for loop tempErr = PMSessionEndDocument(printSession); if(!err)err = tempErr; } } return err; }
int QPrintDialog::exec() { Q_D(QPrintDialog); QMacBlockingFunction func; Boolean result; // If someone is reusing a QPrinter object, the end released all our old // information. In this case, we must reinitialize. if (d->ep->session == 0) d->ep->initialize(); // Carbon's documentation lies. // It seems the only way that Carbon lets you use all is if the minimum // for the page range is 1. This _kind of_ makes sense if you think about // it. However, calling _q_setFirstPage or _q_setLastPage always enforces the range. PMSetPageRange(d->ep->settings, minPage(), maxPage()); if (printRange() == PageRange) { PMSetFirstPage(d->ep->settings, fromPage(), false); PMSetLastPage(d->ep->settings, toPage(), false); } { //simulate modality QWidget modal_widg(0, Qt::Window); modal_widg.setObjectName(QLatin1String(__FILE__ "__modal_dlg")); QApplicationPrivate::enterModal(&modal_widg); PMSessionPrintDialog(d->ep->session, d->ep->settings, d->ep->format, &result); QApplicationPrivate::leaveModal(&modal_widg); } if (result) { UInt32 frompage, topage; PMGetFirstPage(d->ep->settings, &frompage); PMGetLastPage(d->ep->settings, &topage); topage = qMin(UInt32(INT_MAX), topage); setFromTo(frompage, topage); // OK, I need to map these values back let's see // If from is 1 and to is INT_MAX, then print it all // (Apologies to the folks with more than INT_MAX pages) // ...that's a joke. if (fromPage() == 1 && toPage() == INT_MAX) { setPrintRange(AllPages); setFromTo(0,0); } else { setPrintRange(PageRange); // In a way a lie, but it shouldn't hurt. // Carbon hands us back a very large number here even for ALL, set it to max // in that case to follow the behavior of the other print dialogs. if (maxPage() < toPage()) setFromTo(fromPage(), maxPage()); } // Keep us in sync with file output PMDestinationType dest; PMSessionGetDestinationType(d->ep->session, d->ep->settings, &dest); if (dest == kPMDestinationFile) { QCFType<CFURLRef> file; PMSessionCopyDestinationLocation(d->ep->session, d->ep->settings, &file); UInt8 localFile[2048]; // Assuming there's a POSIX file system here. CFURLGetFileSystemRepresentation(file, true, localFile, sizeof(localFile)); d->ep->outputFilename = QString::fromUtf8(reinterpret_cast<const char *>(localFile)); } } return result; }