wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data ) { #if !wxMAC_USE_CORE_GRAPHICS ::GetPort( & m_macPrintFormerPort ) ; #endif m_err = noErr ; wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->GetNativeData() ; PMRect rPage; m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); if ( m_err != noErr ) return; m_maxX = wxCoord(rPage.right - rPage.left) ; m_maxY = wxCoord(rPage.bottom - rPage.top); PMResolution res; #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 PMPrinter printer; PMSessionGetCurrentPrinter(native->m_macPrintSession, &printer); PMPrinterGetOutputResolution( printer, native->m_macPrintSettings, &res) ; #else m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res); #endif m_ppi = wxSize(int(res.hRes), int(res.vRes)); }
wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data ) { m_err = noErr ; wxOSXPrintData *native = (wxOSXPrintData*) data->GetNativeData() ; PMRect rPage; m_err = PMGetAdjustedPageRect(native->GetPageFormat(), &rPage); if ( m_err != noErr ) return; m_maxX = wxCoord(rPage.right - rPage.left) ; m_maxY = wxCoord(rPage.bottom - rPage.top); PMResolution res; PMPrinter printer; m_err = PMSessionGetCurrentPrinter(native->GetPrintSession(), &printer); if ( m_err == noErr ) { #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 if ( PMPrinterGetOutputResolution != NULL ) { { m_err = PMPrinterGetOutputResolution( printer, native->GetPrintSettings(), &res) ; if ( m_err == -9589 /* kPMKeyNotFound */ ) { m_err = noErr ; res.hRes = res.vRes = 300; } } } else #endif { #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 m_err = PMPrinterGetPrinterResolution(printer, kPMCurrentValue, &res); if ( m_err != noErr ) { m_err = PMGetResolution((PMPageFormat) (native->GetPageFormat()), &res); } #endif } } else { res.hRes = res.vRes = 300; } m_maxX = wxCoord((double)m_maxX * res.hRes / 72.0); m_maxY = wxCoord((double)m_maxY * res.vRes / 72.0); m_ppi = wxSize(int(res.hRes), int(res.vRes)); }
bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) ) { if ( m_err ) return false ; wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ; #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4 && wxMAC_USE_CORE_GRAPHICS { CFStringRef s[1] = { kPMGraphicsContextCoreGraphics }; CFArrayRef graphicsContextsArray = CFArrayCreate(NULL, (const void**)s, 1, &kCFTypeArrayCallBacks); PMSessionSetDocumentFormatGeneration(native->m_macPrintSession, kPMDocumentFormatPDF, graphicsContextsArray, NULL); CFRelease(graphicsContextsArray); } #endif #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4 && wxMAC_USE_CORE_GRAPHICS m_err = PMSessionBeginCGDocument(native->m_macPrintSession, native->m_macPrintSettings, native->m_macPageFormat); #else m_err = PMSessionBeginDocument(native->m_macPrintSession, native->m_macPrintSettings, native->m_macPageFormat); #endif if ( m_err != noErr ) return false; PMRect rPage; m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); if ( m_err != noErr ) return false ; m_maxX = wxCoord(rPage.right - rPage.left) ; m_maxY = wxCoord(rPage.bottom - rPage.top); PMResolution res; #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 PMPrinter printer; PMSessionGetCurrentPrinter(native->m_macPrintSession, &printer); PMPrinterGetOutputResolution( printer, native->m_macPrintSettings, &res) ; #else m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res); #endif m_ppi = wxSize(int(res.hRes), int(res.vRes)); return true ; }
bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) { sm_abortIt = FALSE; sm_abortWindow = NULL; if (!printout) return FALSE; printout->SetIsPreview(FALSE); if (m_printDialogData.GetMinPage() < 1) m_printDialogData.SetMinPage(1); if (m_printDialogData.GetMaxPage() < 1) m_printDialogData.SetMaxPage(9999); // Create a suitable device context wxDC *dc = NULL; if (prompt) { wxPrintDialog dialog(parent, & m_printDialogData); if (dialog.ShowModal() == wxID_OK) { dc = dialog.GetPrintDC(); m_printDialogData = dialog.GetPrintDialogData(); } } else { dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ; } // May have pressed cancel. if (!dc || !dc->Ok()) { if (dc) delete dc; return FALSE; } // on the mac we have always pixels as addressing mode with 72 dpi printout->SetPPIScreen(72, 72); PMResolution res; wxMacCarbonPrintData* nativeData = (wxMacCarbonPrintData*) (m_printDialogData.GetPrintData().GetNativeData()); PMGetResolution((PMPageFormat) (nativeData->m_macPageFormat), &res); printout->SetPPIPrinter(int(res.hRes), int(res.vRes)); // Set printout parameters printout->SetDC(dc); int w, h; wxCoord ww, hh; dc->GetSize(&w, &h); printout->SetPageSizePixels((int)w, (int)h); dc->GetSizeMM(&ww, &hh); printout->SetPageSizeMM((int)ww, (int)hh); // Create an abort window wxBeginBusyCursor(); printout->OnPreparePrinting(); // Get some parameters from the printout, if defined int fromPage, toPage; int minPage, maxPage; printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage); if (maxPage == 0) { wxEndBusyCursor(); return FALSE; } // Only set min and max, because from and to have been // set by the user m_printDialogData.SetMinPage(minPage); m_printDialogData.SetMaxPage(maxPage); wxWindow *win = CreateAbortWindow(parent, printout); wxSafeYield(win,true); if (!win) { wxEndBusyCursor(); wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK, parent); delete dc; return FALSE; } sm_abortWindow = win; sm_abortWindow->Show(TRUE); wxSafeYield(win,true); printout->OnBeginPrinting(); bool keepGoing = TRUE; int copyCount; for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++) { if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage())) { wxEndBusyCursor(); wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent); break; } if (sm_abortIt) break; int pn; for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn); pn++) { if (sm_abortIt) { keepGoing = FALSE; break; } else { #if TARGET_CARBON if ( UMAGetSystemVersion() >= 0x1000 ) #endif { GrafPtr thePort ; GetPort( &thePort ) ; wxSafeYield(win,true); SetPort( thePort ) ; } dc->StartPage(); keepGoing = printout->OnPrintPage(pn); dc->EndPage(); } } printout->OnEndDocument(); } printout->OnEndPrinting(); if (sm_abortWindow) { sm_abortWindow->Show(FALSE); delete sm_abortWindow; sm_abortWindow = NULL; } wxEndBusyCursor(); delete dc; return TRUE; }
// -------------------------------------------------------------------------------------------------------------- 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; }
bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) { sm_abortIt = false; sm_abortWindow = NULL; if (!printout) return false; printout->SetIsPreview(false); if (m_printDialogData.GetMinPage() < 1) m_printDialogData.SetMinPage(1); if (m_printDialogData.GetMaxPage() < 1) m_printDialogData.SetMaxPage(9999); // Create a suitable device context wxPrinterDC *dc = NULL; if (prompt) { wxMacPrintDialog dialog(parent, & m_printDialogData); if (dialog.ShowModal() == wxID_OK) { dc = wxDynamicCast(dialog.GetPrintDC(), wxPrinterDC); wxASSERT(dc); m_printDialogData = dialog.GetPrintDialogData(); } } else { dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ; } // May have pressed cancel. if (!dc || !dc->IsOk()) { delete dc; return false; } // on the mac we have always pixels as addressing mode with 72 dpi printout->SetPPIScreen(72, 72); #ifndef __LP64__ PMResolution res; wxOSXPrintData* nativeData = (wxOSXPrintData*) (m_printDialogData.GetPrintData().GetNativeData()); PMGetResolution( (nativeData->GetPageFormat()), &res); printout->SetPPIPrinter(int(res.hRes), int(res.vRes)); #endif // Set printout parameters printout->SetDC(dc); int w, h; dc->GetSize(&w, &h); printout->SetPageSizePixels((int)w, (int)h); printout->SetPaperRectPixels(dc->GetPaperRect()); wxCoord mw, mh; dc->GetSizeMM(&mw, &mh); printout->SetPageSizeMM((int)mw, (int)mh); // Create an abort window wxBeginBusyCursor(); printout->OnPreparePrinting(); // Get some parameters from the printout, if defined int fromPage, toPage; int minPage, maxPage; printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage); if (maxPage == 0) { wxEndBusyCursor(); return false; } // Only set min and max, because from and to have been // set by the user m_printDialogData.SetMinPage(minPage); m_printDialogData.SetMaxPage(maxPage); printout->OnBeginPrinting(); bool keepGoing = true; if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage())) { wxEndBusyCursor(); wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent); } int pn; for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn); pn++) { if (sm_abortIt) { keepGoing = false; break; } else { dc->StartPage(); keepGoing = printout->OnPrintPage(pn); dc->EndPage(); } } printout->OnEndDocument(); printout->OnEndPrinting(); if (sm_abortWindow) { sm_abortWindow->Show(false); delete sm_abortWindow; sm_abortWindow = NULL; } wxEndBusyCursor(); delete dc; return true; }
int Printer_print (void (*draw) (void *boss, Graphics g), void *boss) { #if defined (UNIX) structMelderFile tempFile = { 0 }; char tempPath_utf8 [] = "/tmp/picXXXXXX"; close (mkstemp (tempPath_utf8)); Melder_pathToFile (Melder_peekUtf8ToWcs (tempPath_utf8), & tempFile); thePrinter. graphics = Graphics_create_postscriptjob (& tempFile, thePrinter. resolution, thePrinter. spots, thePrinter. paperSize, thePrinter. orientation, thePrinter. magnification); if (! thePrinter. graphics) return Melder_error1 (L"Cannot create temporary PostScript file for printing."); draw (boss, thePrinter. graphics); forget (thePrinter. graphics); char command [500]; sprintf (command, Melder_peekWcsToUtf8 (Site_getPrintCommand ()), tempPath_utf8); system (command); MelderFile_delete (& tempFile); #elif defined (_WIN32) int postScriptCode = POSTSCRIPT_PASSTHROUGH; DOCINFO docInfo; DEVMODE *devMode; initPrinter (); if (! theWinPrint. hDevMode) { memset (& theWinPrint, 0, sizeof (PRINTDLG)); theWinPrint. lStructSize = sizeof (PRINTDLG); theWinPrint. Flags = PD_RETURNDEFAULT; if (! PrintDlg (& theWinPrint)) return Melder_error1 (L"Cannot initialize printer."); } if (Melder_backgrounding) { theWinPrint. Flags = PD_RETURNDEFAULT | PD_RETURNDC; if (! PrintDlg (& theWinPrint) || theWinPrint. hDC == NULL) { return Melder_error1 (L"Cannot print from a script on this computer."); } } else { theWinPrint. Flags &= ~ PD_RETURNDEFAULT; theWinPrint. Flags |= PD_RETURNDC; if (! PrintDlg (& theWinPrint)) return 1; } theWinDC = theWinPrint. hDC; thePrinter. postScript = thePrinter. allowDirectPostScript && Escape (theWinDC, QUERYESCSUPPORT, sizeof (int), (LPSTR) & postScriptCode, NULL); /* * The HP colour inkjet printer returns in dmFields: * 0, 1, 8, 9, 10, 11, 12, 13, 14, 15, 23, 24, 25, 26 = DM_ORIENTATION | * DM_PAPERSIZE | DM_COPIES | DM_DEFAULTSOURCE | DM_PRINTQUALITY | * DM_COLOR | DM_DUPLEX | DM_YRESOLUTION | DM_TTOPTION | DM_COLLATE | * DM_ICMMETHOD | DM_ICMINTENT | DM_MEDIATYPE | DM_DITHERTYPE */ devMode = * (DEVMODE **) theWinPrint. hDevMode; thePrinter. resolution = devMode -> dmFields & DM_YRESOLUTION ? devMode -> dmYResolution : devMode -> dmFields & DM_PRINTQUALITY ? ( devMode -> dmPrintQuality > 0 ? devMode -> dmPrintQuality : 300 ) : 300; if (devMode -> dmFields & DM_PAPERWIDTH) { thePrinter. paperWidth = devMode -> dmPaperWidth * thePrinter. resolution / 254; thePrinter. paperHeight = devMode -> dmPaperLength * thePrinter. resolution / 254; } else if (devMode -> dmFields & DM_PAPERSIZE) { static struct { float width, height; } sizes [] = { { 0, 0 }, { 8.5, 11 }, { 8.5, 11 }, { 11, 17 }, { 17, 11 }, { 8.5, 14 }, { 5.5, 8.5 }, { 7.25, 10.5 }, { 297/25.4, 420/25.4 }, { 210/25.4, 297/25.4 }, { 210/25.4, 297/25.4 }, { 148.5/25.4, 210/25.4 }, { 250/25.4, 354/25.4 }, { 182/25.4, 257/25.4 }, { 8.5, 13 }, { 215/25.4, 275/25.4 }, { 10, 14 }, { 11, 17 }, { 8.5, 11 }, { 3.875, 8.875 }, { 4.125, 9.5 }, { 4.5, 10.375 } }; int paperSize = devMode -> dmPaperSize; if (paperSize <= 0 || paperSize > 21) paperSize = 1; thePrinter. paperWidth = sizes [paperSize]. width * thePrinter. resolution; thePrinter. paperHeight = sizes [paperSize]. height * thePrinter. resolution; if (devMode -> dmOrientation == DMORIENT_LANDSCAPE) { long dummy = thePrinter. paperWidth; thePrinter. paperWidth = thePrinter. paperHeight; thePrinter. paperHeight = dummy; } } else { thePrinter. paperWidth = 1000; thePrinter. paperHeight = 1000; } EnableWindow ((HWND) XtWindow (theCurrentPraatApplication -> topShell), FALSE); SetAbortProc (theWinDC, AbortFunc); memset (& docInfo, 0, sizeof (DOCINFO)); docInfo. cbSize = sizeof (DOCINFO); docInfo. lpszDocName = L"Praatjes"; docInfo. lpszOutput = NULL; if (thePrinter. postScript) { StartDoc (theWinDC, & docInfo); StartPage (theWinDC); initPostScriptPage (); thePrinter. graphics = Graphics_create_postscriptprinter (); if (! thePrinter. graphics) return Melder_error1 (L"Cannot open printer."); draw (boss, thePrinter. graphics); forget (thePrinter. graphics); exitPostScriptPage (); EndPage (theWinDC); EndDoc (theWinDC); } else { StartDoc (theWinDC, & docInfo); StartPage (theWinDC); thePrinter. graphics = Graphics_create_screenPrinter (NULL, (unsigned long) theWinDC); if (! thePrinter. graphics) return Melder_error1 (L"Cannot open printer."); draw (boss, thePrinter. graphics); forget (thePrinter. graphics); if (EndPage (theWinDC) < 0) { Melder_error1 (L"Cannot print page."); } else { EndDoc (theWinDC); } } EnableWindow ((HWND) XtWindow (theCurrentPraatApplication -> topShell), TRUE); DeleteDC (theWinDC), theWinDC = NULL; #elif defined (macintosh) Boolean result; initPrinter (); if (Melder_backgrounding) { PMSessionValidatePageFormat (theMacPrintSession, theMacPageFormat, & result); PMSessionValidatePrintSettings (theMacPrintSession, theMacPrintSettings, & result); } else { Boolean accepted; PMSessionPrintDialog (theMacPrintSession, theMacPrintSettings, theMacPageFormat, & accepted); if (! accepted) return 1; /* Normal cancelled return. */ } PMSessionValidatePageFormat (theMacPrintSession, theMacPageFormat, & result); PMSessionValidatePrintSettings (theMacPrintSession, theMacPrintSettings, & result); PMResolution res; PMGetResolution (theMacPageFormat, & res); thePrinter. resolution = res. hRes; PMGetAdjustedPaperRect (theMacPageFormat, & paperSize); thePrinter. paperWidth = paperSize. right - paperSize. left; thePrinter. paperHeight = paperSize. bottom - paperSize. top; Boolean isPostScriptDriver = FALSE; //PMSessionIsDocumentFormatSupported (theMacPrintSession, // kPMDocumentFormatPICTPS, & isPostScriptDriver); CFArrayRef supportedFormats; PMSessionGetDocumentFormatGeneration (theMacPrintSession, & supportedFormats); CFIndex numberOfSupportedFormats = CFArrayGetCount (supportedFormats); if (Melder_debug == 21) { MelderInfo_open (); MelderInfo_writeLine1 (L"Supported document formats:"); } for (CFIndex i = 0; i < numberOfSupportedFormats; i ++) { CFStringRef supportedFormat = CFArrayGetValueAtIndex (supportedFormats, i); if (CFStringCompare (supportedFormat, kPMDocumentFormatPICTPS, 0) == 0) { isPostScriptDriver = TRUE; } if (Melder_debug == 21) { MelderInfo_writeLine3 (Melder_integer (i), L": ", Melder_peekUtf8ToWcs (CFStringGetCStringPtr (supportedFormat, kCFStringEncodingUTF8))); } } if (Melder_debug == 21) { MelderInfo_close (); } CFRelease (supportedFormats); isPostScriptDriver = FALSE; // OVERRIDE, because from 10.4 on we have something better: we'll be sending PDF thePrinter. postScript = thePrinter. allowDirectPostScript && isPostScriptDriver; if (thePrinter. postScript) { CFStringRef strings [1]; strings [0] = kPMGraphicsContextQuickdraw; CFArrayRef array = CFArrayCreate (kCFAllocatorDefault, (const void **) strings, 1, & kCFTypeArrayCallBacks); OSStatus err = PMSessionSetDocumentFormatGeneration (theMacPrintSession, kPMDocumentFormatPICTPS, array, NULL); CFRelease (array); if (err != 0) { return Melder_error2 (L"PMSessionSetDocumentFormatGeneration: error ", Melder_integer (err)); } } PMOrientation orientation; PMGetOrientation (theMacPageFormat, & orientation); thePrinter. orientation = orientation == kPMLandscape || orientation == kPMReverseLandscape ? kGraphicsPostscript_orientation_LANDSCAPE : kGraphicsPostscript_orientation_PORTRAIT; PMSessionBeginDocument (theMacPrintSession, theMacPrintSettings, theMacPageFormat); PMSessionBeginPage (theMacPrintSession, theMacPageFormat, NULL); PMSessionGetGraphicsContext (theMacPrintSession, kPMGraphicsContextQuickdraw, (void **) & theMacPort); /* * On PostScript, the point (0, 0) is the bottom left corner of the paper, which is fine. * On the screen, however, the point (0, 0) is the top left corner of the writable page. * Since we want paper-related margins, not writable-page-related margins, * we require that this point gets the coordinates (250, 258) or so, * so that the top left corner of the paper gets coordinates (0, 0). * The "left" and "top" attributes of rPaper are negative values (e.g. -250 and -258), * so multiply them by -1. * * Under Carbon, the port has to be set inside the page. */ SetPort (theMacPort); if (! thePrinter. postScript) SetOrigin (- paperSize. left, - paperSize. top); if (thePrinter. postScript) { if (! openPostScript ()) error1 (L"Cannot print PostScript.") thePrinter. graphics = Graphics_create_postscriptprinter (); if (! thePrinter. graphics) goto end; draw (boss, thePrinter. graphics); forget (thePrinter. graphics); closePostScript (); } else { thePrinter. graphics = Graphics_create_screenPrinter (NULL, (unsigned long) theMacPort); draw (boss, thePrinter. graphics); forget (thePrinter. graphics); } end: if (theMacPort) { PMSessionEndPage (theMacPrintSession); PMSessionEndDocument (theMacPrintSession); theMacPort = NULL; } #endif iferror return 0; return 1; }