Beispiel #1
0
int ioPrintCleanup(PrintingLogicPtr *token) {                                                                	PrintingLogicPtr printJob;
    //	Release the PageFormat and PrintSettings objects.  PMRelease decrements the
    //	ref count of the allocated objects.  We let the Printing Manager decide when
    //	to release the allocated memory.
    
    printJob = *token;
    if (printJob == nil) 
        return 0;
        
    if (printJob->pageFormat != kPMNoPageFormat)
        (void)PMRelease(printJob->pageFormat);
        
    if (printJob->printSettings != kPMNoPrintSettings)
        (void)PMRelease(printJob->printSettings);
    
    //	Terminate the current printing session. 
    if (printJob->printSession != NULL)
    	(void)PMRelease(printJob->printSession);
    
    printJob->pageFormat = kPMNoPageFormat;
    printJob->printSettings = kPMNoPrintSettings;
    printJob->printSession = NULL;
    free(printJob);
    *token = nil;
    
    return 0;
}
// ---------------------------------------------------------------------------
//
// ------------
bool bXMapPrintSetup::process(int msg, void* prm){
_bTrace_("bXMapPrintSetup::process",true);
PMPrintSession	ps=NULL;
PMPageFormat	pf=kPMNoPageFormat;
OSStatus		status=PMCreateSession(&ps);
    
    _gapp->printMgr()->get_page_format(&pf,kMapPageFormatName);
    for(;;){
        if(status!=noErr){
            break;
        }
        status=DoPageSetupDialog(ps,&pf);
        if(status!=noErr){
            break;
        }
        break;
    }
    if(pf!=kPMNoPageFormat){
        _gapp->printMgr()->set_page_format(pf,kMapPageFormatName);
        (void)PMRelease(pf);
    }
    if(ps){
        (void)PMRelease(ps);
    }
    return(true);
}
Beispiel #3
0
static boolean
carbonKillPrintVars(void)
{
	
#if TARGET_API_MAC_CARBON == 1
	
	// empty out carbon print vars
	// setting to nil is important since it is checked for
	if (nil != shellprintinfo.printsettings)
	{
		PMRelease(shellprintinfo.printsettings);
		shellprintinfo.printsettings = nil;
	}
	
	if (nil != shellprintinfo.pageformat)
	{
		PMRelease(shellprintinfo.pageformat);
		shellprintinfo.pageformat = nil;
	}
	
	if (nil != shellprintinfo.printhandle)
	{
		PMRelease(shellprintinfo.printhandle);
		shellprintinfo.printhandle = nil;
	}
	
#endif
	
	return(true);
}	
Beispiel #4
0
void wxMacCarbonPrintData::CopyFrom( wxNativePrintData* d )
{
    wxMacCarbonPrintData *data = (wxMacCarbonPrintData*) d ;
    if ( data->m_macPrintSession != kPMNoReference )
        PMRetain( data->m_macPrintSession ) ;
    if ( m_macPrintSession != kPMNoReference )
    {
        PMRelease( m_macPrintSession ) ;
        m_macPrintSession = kPMNoReference ;
    }
    if ( data->m_macPrintSession != kPMNoReference )
        m_macPrintSession = data->m_macPrintSession ;

    if ( data->m_macPrintSettings != kPMNoPrintSettings )
        PMRetain( data->m_macPrintSettings ) ;
    if ( m_macPrintSettings != kPMNoPrintSettings )
    {
        PMRelease( m_macPrintSettings ) ;
        m_macPrintSettings = kPMNoPrintSettings ;
    }
    if ( data->m_macPrintSettings != kPMNoPrintSettings )
        m_macPrintSettings = data->m_macPrintSettings ;

    if ( data->m_macPageFormat != kPMNoPageFormat )
        PMRetain( data->m_macPageFormat ) ;
    if ( m_macPageFormat != kPMNoPageFormat )
    {
        PMRelease( m_macPageFormat ) ;
        m_macPageFormat = kPMNoPageFormat ;
    }
    if ( data->m_macPageFormat != kPMNoPageFormat )
        m_macPageFormat = data->m_macPageFormat ;
}
// --------------------------------------------------------------------------------------------------------------
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 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;
}
void ClosePrinter(void)
{
#if TARGET_API_MAC_CARBON
	//if(gPageFormat) {PMRelease(gPageFormat); gPageFormat = 0;} 
	// we want to save the pageFormat so the user can get the settings they set in Page Setup 
	
	if(gPrintSettings) {PMRelease(gPrintSettings); gPrintSettings = 0;} 
	if(gPrintSession) {PMRelease(gPrintSession); gPrintSession = 0;} 
	// Apple says... "By not saving print settings between calls to the Print dialog, you ensure that the dialog displays with the appropriate default settings, which is the recommended behavior."
#else 
	PrClose();
#endif
}
// --------------------------------------------------------------------------------------------------------------
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
//-----------------------------------------------------------------------------------------------------------------------
void ProcessPrintCommand(DocStoragePtr docStP, UInt32 commandID)
{
    OSStatus err = noErr;
    PMPrintSession  printSession = NULL;
    
    if ( PMCreateSession(&printSession) == noErr )
    {
	if (commandID == kHICommandPageSetup)
	{
	    DoPageSetupDialog(printSession, &docStP->pageFormat, &docStP->flattenedPageFormat);
	}
	else if (commandID == kHICommandPrint)
	{
	    if (docStP->pageFormat == NULL)
	    {
		err = MyCreatePageFormat(printSession, &docStP->pageFormat);
	    }

	    if (DoPrintDialog(printSession, docStP->pageFormat, &docStP->printSettings) == noErr)
	    {
		DoPrintLoop(docStP, printSession, docStP->pageFormat, docStP->printSettings);
	    }
	}
	PMRelease(printSession);
    }
    else
    {
	fprintf(stderr, "PMCreateSession FAILED\n");
    }
}   // ProcessPrintCommand
Beispiel #10
0
// ---------------------------------------------------------------------------------------------
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);
}
Beispiel #11
0
wxMacCarbonPrintData::~wxMacCarbonPrintData()
{
    if (m_macPageFormat != kPMNoPageFormat)
    {
        (void)PMRelease(m_macPageFormat);
        m_macPageFormat = kPMNoPageFormat;
    }

    if (m_macPrintSettings != kPMNoPrintSettings)
    {
        (void)PMRelease(m_macPrintSettings);
        m_macPrintSettings = kPMNoPrintSettings;
    }

    if ( m_macPrintSession != kPMNoReference )
    {
        (void)PMRelease(m_macPrintSession);
        m_macPrintSession = kPMNoReference;
    }
}
// --------------------------------------------------------------------------------------------------------------
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;
}
// --------------------------------------------------------------------------------------------------------------
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;
}
Beispiel #14
0
// -----------------------------------------------------------------------
OSStatus CreateDefaultPageFormatForDocument(void *docDataP)
{
    OSStatus err = noErr, tempErr;
    PMPrintSession printSession;
    err = PMCreateSession(&printSession);
    if(!err){
	PMPageFormat pageFormat;
	err = PMCreatePageFormat(&pageFormat);	// we own a reference to this page format
	if(err == noErr)
	{
	    err = PMSessionDefaultPageFormat(printSession, pageFormat);
	    if (err == noErr){	
		// this routine will keep its own reference to the pageFormat
		err = SetPageFormatOnPrivateData(docDataP, pageFormat);	
	    }
	    tempErr = PMRelease(pageFormat);// release our reference obtained from PMCreatePageFormat
	    if(!err)err = tempErr;
	}
	tempErr = PMRelease(printSession);
	if(!err)err = tempErr;
    }
    return err;
}
Beispiel #15
0
// -----------------------------------------------------------------
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
Beispiel #16
0
/*------------------------------------------------------------------------------
    Allow the user define the page setup for printing to current printer

    Parameters:
        printSession    -   current printing session
        pageFormat      -   a PageFormat object addr

    Description:
        If the caller passes in an empty PageFormat object, create a new one,
        otherwise validate the one provided by the caller.
        Invokes the Page Setup dialog and checks for Cancel.
        Flattens the PageFormat object so it can be saved with the document.
        Note that the PageFormat object is modified by this function.

------------------------------------------------------------------------------*/
static void _doPageSetupDialog(PMPageFormat* pageFormat)
{
	OSStatus    		status;
	Boolean     		accepted;
	PMPrintSession	printSession;

	status = PMCreateSession(&printSession);
	if ( noErr != status )
	{
		PostPrintingErrors(status);
		return;
	}

	//  Set up a valid PageFormat object.
	if (*pageFormat == kPMNoPageFormat)
	{
	  status = PMCreatePageFormat(pageFormat);

	  //  Note that PMPageFormat is not session-specific, but calling
	  //  PMSessionDefaultPageFormat assigns values specific to the printer
	  //  associated with the current printing session.
		if ((status == noErr) && (*pageFormat != kPMNoPageFormat))
			status = PMSessionDefaultPageFormat(printSession, *pageFormat);
	}
	else
	{
		status = PMSessionValidatePageFormat(printSession, *pageFormat, kPMDontWantBoolean);
	}

	//  Display the Page Setup dialog.
	if (status == noErr)
	{
		status = PMSessionPageSetupDialog(printSession, *pageFormat, &accepted);
		if (!accepted)
		{
			status = kPMCancel; // user clicked Cancel button
		}
	}

	status = PMRelease(printSession);
	return;
}
Beispiel #17
0
bool QMacPrintEngine::end()
{
    Q_D(QMacPrintEngine);
    if (d->state == QPrinter::Aborted)
        return true;  // I was just here a function call ago :)
    if(d->paintEngine->type() == QPaintEngine::CoreGraphics)
        static_cast<QCoreGraphicsPaintEngine*>(d->paintEngine)->d_func()->hd = 0;
    d->paintEngine->end();
    if (d->state != QPrinter::Idle) {
        if (d->shouldSuppressStatus()) {
            PMSessionEndPageNoDialog(d->session);
            PMSessionEndDocumentNoDialog(d->session);
        } else {
            PMSessionEndPage(d->session);
            PMSessionEndDocument(d->session);
        }
        PMRelease(d->session);
        d->session = 0;
    }
    d->state  = QPrinter::Idle;
    return true;
}
Beispiel #18
0
void wxOSXPrintData::TransferPaperInfoFrom( const wxPrintData &data )
{
    PMPrinter printer;
    PMSessionGetCurrentPrinter(m_macPrintSession, &printer);

    wxSize papersize = wxDefaultSize;
    const wxPaperSize paperId = data.GetPaperId();
    if ( paperId != wxPAPER_NONE && wxThePrintPaperDatabase )
    {
        papersize = wxThePrintPaperDatabase->GetSize(paperId);
        if ( papersize != wxDefaultSize )
        {
            papersize.x /= 10;
            papersize.y /= 10;
        }
    }
    else
    {
        papersize = data.GetPaperSize();
    }

    if ( papersize != wxDefaultSize )
    {
        papersize.x = (wxInt32) (papersize.x * mm2pt);
        papersize.y = (wxInt32) (papersize.y * mm2pt);

        double height, width;
        PMPaperGetHeight(m_macPaper, &height);
        PMPaperGetWidth(m_macPaper, &width);

        if ( fabs( width - papersize.x ) >= 5 ||
            fabs( height - papersize.y ) >= 5 )
        {
            // we have to change the current paper
            CFArrayRef paperlist = 0 ;
            if ( PMPrinterGetPaperList( printer, &paperlist ) == noErr )
            {
                PMPaper bestPaper = kPMNoData ;
                CFIndex top = CFArrayGetCount(paperlist);
                for ( CFIndex i = 0 ; i < top ; ++ i )
                {
                    PMPaper paper = (PMPaper) CFArrayGetValueAtIndex( paperlist, i );
                    PMPaperGetHeight(paper, &height);
                    PMPaperGetWidth(paper, &width);
                    if ( fabs( width - papersize.x ) < 5 &&
                        fabs( height - papersize.y ) < 5 )
                    {
                        // TODO test for duplicate hits and use additional
                        // criteria for best match
                        bestPaper = paper;
                    }
                }
                PMPaper paper = kPMNoData;
                if ( bestPaper == kPMNoData )
                {
                    const PMPaperMargins margins = { 0.0, 0.0, 0.0, 0.0 };
                    wxString id, name(wxT("Custom paper"));
                    id.Printf(wxT("wxPaperCustom%dx%d"), papersize.x, papersize.y);

                    if ( PMPaperCreateCustom
                         (
                            printer,
                            wxCFStringRef(id, wxFont::GetDefaultEncoding()),
                            wxCFStringRef(name, wxFont::GetDefaultEncoding()),
                            papersize.x, papersize.y,
                            &margins,
                            &paper
                         ) )
                    {
                        bestPaper = paper;
                    }
                }
                if ( bestPaper != kPMNoData )
                {
                    PMPageFormat pageFormat;
                    PMCreatePageFormatWithPMPaper(&pageFormat, bestPaper);
                    PMCopyPageFormat( pageFormat, m_macPageFormat );
                    PMRelease(pageFormat);
                    PMGetPageFormatPaper(m_macPageFormat, &m_macPaper);
                }
                PMRelease(paper);
            }
        }
    }

    PMSetCopies( m_macPrintSettings , data.GetNoCopies() , false ) ;
    PMSetCollate(m_macPrintSettings, data.GetCollate());
    if ( data.IsOrientationReversed() )
        PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
                         kPMReverseLandscape : kPMReversePortrait , false ) ;
    else
        PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
                         kPMLandscape : kPMPortrait , false ) ;

    PMDuplexMode mode = 0 ;
    switch( data.GetDuplex() )
    {
        case wxDUPLEX_HORIZONTAL :
            mode = kPMDuplexNoTumble ;
            break ;
        case wxDUPLEX_VERTICAL :
            mode = kPMDuplexTumble ;
            break ;
        case wxDUPLEX_SIMPLEX :
        default :
            mode = kPMDuplexNone ;
            break ;
    }
    PMSetDuplex(  m_macPrintSettings, mode ) ;


    if ( data.IsOrientationReversed() )
        PMSetOrientation(  m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
                         kPMReverseLandscape : kPMReversePortrait , false ) ;
    else
        PMSetOrientation(  m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
                         kPMLandscape : kPMPortrait , false ) ;
}
Beispiel #19
0
wxOSXCarbonPrintData::~wxOSXCarbonPrintData()
{
    (void)PMRelease(m_macPageFormat);
    (void)PMRelease(m_macPrintSettings);
    (void)PMRelease(m_macPrintSession);
}
Beispiel #20
0
bool wxOSXPrintData::TransferFrom( const wxPrintData &data )
{
    PMPrinter printer;
    PMSessionGetCurrentPrinter(m_macPrintSession, &printer);

    wxSize papersize = wxDefaultSize;
    const wxPaperSize paperId = data.GetPaperId();
    if ( paperId != wxPAPER_NONE && wxThePrintPaperDatabase )
    {
        papersize = wxThePrintPaperDatabase->GetSize(paperId);
        if ( papersize != wxDefaultSize )
        {
            papersize.x /= 10;
            papersize.y /= 10;
        }
    }
    else
    {
        papersize = data.GetPaperSize();
    }
    
    if ( papersize != wxDefaultSize )
    {
        papersize.x = (wxInt32) (papersize.x * mm2pt);
        papersize.y = (wxInt32) (papersize.y * mm2pt);
        
        double height, width;
        PMPaperGetHeight(m_macPaper, &height);
        PMPaperGetWidth(m_macPaper, &width);
        
        if ( fabs( width - papersize.x ) >= 5 || 
            fabs( height - papersize.y ) >= 5 )
        {
            // we have to change the current paper
            CFArrayRef paperlist = 0 ;
            if ( PMPrinterGetPaperList( printer, &paperlist ) == noErr )
            {
                PMPaper bestPaper = kPMNoData ;
                CFIndex top = CFArrayGetCount(paperlist);
                for ( CFIndex i = 0 ; i < top ; ++ i )
                {
                    PMPaper paper = (PMPaper) CFArrayGetValueAtIndex( paperlist, i );
                    PMPaperGetHeight(paper, &height);
                    PMPaperGetWidth(paper, &width);
                    if ( fabs( width - papersize.x ) < 5 && 
                        fabs( height - papersize.y ) < 5 )
                    {
                        // TODO test for duplicate hits and use additional
                        // criteria for best match
                        bestPaper = paper;
                    }
                }
                PMPaper paper = kPMNoData;
                if ( bestPaper == kPMNoData )
                {
                    const PMPaperMargins margins = { 0.0, 0.0, 0.0, 0.0 };
                    wxString id, name(_T("Custom paper"));                    
                    id.Printf(_T("wxPaperCustom%dx%d"), papersize.x, papersize.y);

#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
                    if ( PMPaperCreateCustom != NULL)
                    {
                        PMPaperCreateCustom(printer, wxCFStringRef( id, wxFont::GetDefaultEncoding() ), wxCFStringRef( name, wxFont::GetDefaultEncoding() ),
                            papersize.x, papersize.y, &margins, &paper);
                    }
#endif
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 
                    if ( paper == kPMNoData )
                    {
                        PMPaperCreate(printer, wxCFStringRef( id, wxFont::GetDefaultEncoding() ), wxCFStringRef( name, wxFont::GetDefaultEncoding() ), 
                            papersize.x, papersize.y, &margins, &paper);
                    }
#endif
                }
                if ( bestPaper != kPMNoData )
                {
                    PMPageFormat pageFormat;
                    PMCreatePageFormatWithPMPaper(&pageFormat, bestPaper);
                    PMCopyPageFormat( pageFormat, m_macPageFormat );
                    PMRelease(pageFormat);
                    PMGetPageFormatPaper(m_macPageFormat, &m_macPaper);
                }
                PMRelease(paper);
            }
        }
    }

    CFArrayRef printerList;
    CFIndex index, count;
    CFStringRef name;
    
    if (PMServerCreatePrinterList(kPMServerLocal, &printerList) == noErr)
    {
        count = CFArrayGetCount(printerList);
        for (index = 0; index < count; index++)
        {
            printer = (PMPrinter)CFArrayGetValueAtIndex(printerList, index);
            if ((data.GetPrinterName().empty()) && (PMPrinterIsDefault(printer)))
                break;
            else
            {
                name = PMPrinterGetName(printer);
                CFRetain(name);
                if (data.GetPrinterName() == wxCFStringRef(name).AsString())
                    break;
            }
        }
        if (index < count)
            PMSessionSetCurrentPMPrinter(m_macPrintSession, printer);
        CFRelease(printerList);
    }
    
    PMSetCopies( m_macPrintSettings , data.GetNoCopies() , false ) ;
    PMSetCollate(m_macPrintSettings, data.GetCollate());
    if ( data.IsOrientationReversed() )
        PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
            kPMReverseLandscape : kPMReversePortrait , false ) ;
    else
        PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
            kPMLandscape : kPMPortrait , false ) ;
    
    PMDuplexMode mode = 0 ;
    switch( data.GetDuplex() )
    {
        case wxDUPLEX_HORIZONTAL :
            mode = kPMDuplexNoTumble ;
            break ;
        case wxDUPLEX_VERTICAL :
            mode = kPMDuplexTumble ;
            break ;
        case wxDUPLEX_SIMPLEX :
        default :
            mode = kPMDuplexNone ;
            break ;
    }
    PMSetDuplex(  m_macPrintSettings, mode ) ;

    // PMQualityMode not yet accessible via API
    
    
    if ( data.IsOrientationReversed() )
        PMSetOrientation(  m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
            kPMReverseLandscape : kPMReversePortrait , false ) ;
    else
        PMSetOrientation(  m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
            kPMLandscape : kPMPortrait , false ) ;
    
#ifndef __LP64__
    // PMQualityMode not accessible via API
    // TODO: use our quality property to determine optimal resolution
    PMResolution res;
    PMTag tag = kPMMaxSquareResolution;
    PMPrinterGetPrinterResolution(printer, tag, &res);
    PMSetResolution( m_macPageFormat, &res);
#endif

    // after setting the new resolution the format has to be updated, otherwise the page rect remains 
    // at the 'old' scaling
    
    PMSessionValidatePageFormat(m_macPrintSession,
        m_macPageFormat, kPMDontWantBoolean);
    PMSessionValidatePrintSettings(m_macPrintSession,
        m_macPrintSettings, kPMDontWantBoolean);
#if wxOSX_USE_COCOA
    UpdateFromPMState();
#endif

    return true ;
}
Beispiel #21
0
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;
}
Beispiel #22
0
// -------------------------------------------------------------------------------
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;
}