diff --git a/include/wx/mac/carbon/private/print.h b/include/wx/mac/carbon/private/print.h index 6e7e3ead45..c1e031be4f 100644 --- a/include/wx/mac/carbon/private/print.h +++ b/include/wx/mac/carbon/private/print.h @@ -46,8 +46,10 @@ public : virtual void TransferTo( wxPrintDialogData * ) ; private : virtual void ValidateOrCreate() ; + virtual void EnsureValidSession() ; public : PMPrintSession m_macPrintSession ; + PMPaper m_macPaper ; PMPageFormat m_macPageFormat ; PMPrintSettings m_macPrintSettings ; private: diff --git a/src/mac/carbon/printmac.cpp b/src/mac/carbon/printmac.cpp index 58b8f0c8d1..e967ee0107 100644 --- a/src/mac/carbon/printmac.cpp +++ b/src/mac/carbon/printmac.cpp @@ -2,10 +2,10 @@ // Name: src/mac/carbon/printwin.cpp // Purpose: wxMacPrinter framework // Author: Julian Smart -// Modified by: +// Modified by: Stefan Csomor // Created: 04/01/98 // RCS-ID: $Id$ -// Copyright: (c) Julian Smart +// Copyright: (c) Julian Smart, Stefan Csomor // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -51,6 +51,7 @@ wxMacCarbonPrintData::wxMacCarbonPrintData() m_macPageFormat = kPMNoPageFormat; m_macPrintSettings = kPMNoPrintSettings; m_macPrintSession = kPMNoReference ; + m_macPaper = kPMNoData; ValidateOrCreate() ; } @@ -73,41 +74,79 @@ wxMacCarbonPrintData::~wxMacCarbonPrintData() (void)PMRelease(m_macPrintSession); m_macPrintSession = kPMNoReference; } + + if ( m_macPaper != kPMNoData ) + { + (void)PMRelease(m_macPaper); + m_macPaper = kPMNoData; + } } +void wxMacCarbonPrintData::EnsureValidSession() +{ + if ( m_macPrintSession == kPMNoReference ) + { + PMCreateSession( &m_macPrintSession ) ; + } + wxASSERT( m_macPrintSession != kPMNoReference ); +} + + void wxMacCarbonPrintData::ValidateOrCreate() { - OSStatus err = noErr ; - if ( m_macPrintSession == kPMNoReference ) - { - err = PMCreateSession( (PMPrintSession *) &m_macPrintSession ) ; - } + EnsureValidSession(); + + OSStatus err = noErr; // Set up a valid PageFormat object. if ( m_macPageFormat == kPMNoPageFormat) { - err = PMCreatePageFormat((PMPageFormat *) &m_macPageFormat); - - // Note that PMPageFormat is not session-specific, but calling - // PMSessionDefaultPageFormat assigns values specific to the printer - // associated with the current printing session. - if ((err == noErr) && - ( m_macPageFormat != kPMNoPageFormat)) + if ( m_macPaper != kPMNoData ) { - err = PMSessionDefaultPageFormat((PMPrintSession) m_macPrintSession, - (PMPageFormat) m_macPageFormat); + err = PMCreatePageFormatWithPMPaper(&m_macPageFormat, m_macPaper); + if ( (err == noErr) && ( m_macPageFormat != kPMNoPageFormat)) + { + err = PMSessionValidatePageFormat(m_macPrintSession, + m_macPageFormat, + kPMDontWantBoolean); + } } + else + { + err = PMCreatePageFormat(&m_macPageFormat); + // Note that PMPageFormat is not session-specific, but calling + // PMSessionDefaultPageFormat assigns values specific to the printer + // associated with the current printing session. + if ( (err == noErr) && ( m_macPageFormat != kPMNoPageFormat)) + { + err = PMSessionDefaultPageFormat(m_macPrintSession, + m_macPageFormat); + } + } + } else { - err = PMSessionValidatePageFormat((PMPrintSession) m_macPrintSession, - (PMPageFormat) m_macPageFormat, + err = PMSessionValidatePageFormat(m_macPrintSession, + m_macPageFormat, kPMDontWantBoolean); } - + // make sure our m_macPaper is corresponding to what is used + if ( err == noErr ) + { + PMPaper currentPaper = kPMNoData; + PMGetPageFormatPaper(m_macPageFormat, ¤tPaper); + if( currentPaper != m_macPaper ) + { + PMRelease(m_macPaper); + m_macPaper = currentPaper; + PMRetain(m_macPaper); + } + } + // Set up a valid PrintSettings object. if ( m_macPrintSettings == kPMNoPrintSettings) { - err = PMCreatePrintSettings((PMPrintSettings *) &m_macPrintSettings); + err = PMCreatePrintSettings(&m_macPrintSettings); // Note that PMPrintSettings is not session-specific, but calling // PMSessionDefaultPrintSettings assigns values specific to the printer @@ -115,43 +154,96 @@ void wxMacCarbonPrintData::ValidateOrCreate() if ((err == noErr) && ( m_macPrintSettings != kPMNoPrintSettings)) { - err = PMSessionDefaultPrintSettings((PMPrintSession) m_macPrintSession, - (PMPrintSettings) m_macPrintSettings); + err = PMSessionDefaultPrintSettings(m_macPrintSession, + m_macPrintSettings); } } else { - err = PMSessionValidatePrintSettings((PMPrintSession) m_macPrintSession, - (PMPrintSettings) m_macPrintSettings, + err = PMSessionValidatePrintSettings(m_macPrintSession, + m_macPrintSettings, kPMDontWantBoolean); } } bool wxMacCarbonPrintData::TransferFrom( const wxPrintData &data ) { + EnsureValidSession(); + + 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 *= mm2pt; + 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; + } + } + if ( bestPaper != kPMNoData ) + { + PMRelease(m_macPaper); + m_macPaper = bestPaper; + PMRetain(m_macPaper); + PMRelease(m_macPageFormat); + m_macPageFormat = kPMNoPageFormat; + } + } + } + } + ValidateOrCreate() ; - PMSetCopies( (PMPrintSettings) m_macPrintSettings , data.GetNoCopies() , false ) ; + PMSetCopies( m_macPrintSettings , data.GetNoCopies() , false ) ; if ( data.IsOrientationReversed() ) - PMSetOrientation( (PMPageFormat) m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ? + PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ? kPMReverseLandscape : kPMReversePortrait , false ) ; else - PMSetOrientation( (PMPageFormat) m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ? + PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ? kPMLandscape : kPMPortrait , false ) ; // collate cannot be set #if 0 // not yet tested if ( !m_printerName.empty() ) - PMSessionSetCurrentPrinter( (PMPrintSession) m_macPrintSession , wxMacCFStringHolder( m_printerName , wxFont::GetDefaultEncoding() ) ) ; -#endif -#ifndef __LP64__ - PMColorMode color ; - PMGetColorMode( (PMPrintSettings) m_macPrintSettings, &color ) ; - if ( data.GetColour() ) - { - if ( color == kPMBlackAndWhite ) - PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMColor ) ; - } - else - PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMBlackAndWhite ) ; + PMSessionSetCurrentPrinter( m_macPrintSession , wxMacCFStringHolder( m_printerName , wxFont::GetDefaultEncoding() ) ) ; #endif #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 @@ -171,23 +263,20 @@ bool wxMacCarbonPrintData::TransferFrom( const wxPrintData &data ) mode = kPMDuplexNone ; break ; } - PMSetDuplex( (PMPrintSettings) m_macPrintSettings, mode ) ; + PMSetDuplex( m_macPrintSettings, mode ) ; } -#endif - // PMQualityMode not yet accessible via API - // todo paperSize - +#endif + // PMQualityMode not accessible via API + // TODO: use our quality property to determine optimal resolution PMResolution res; - PMPrinter printer; - PMSessionGetCurrentPrinter(m_macPrintSession, &printer); PMTag tag = kPMMaxSquareResolution; PMPrinterGetPrinterResolution(printer, tag, &res); - PMSetResolution((PMPageFormat) m_macPageFormat, &res); + PMSetResolution(m_macPageFormat, &res); // after setting the new resolution the format has to be updated, otherwise the page rect remains // at the 'old' scaling - PMSessionValidatePageFormat((PMPrintSession) m_macPrintSession, - (PMPageFormat) m_macPageFormat, + PMSessionValidatePageFormat(m_macPrintSession, + m_macPageFormat, kPMDontWantBoolean) ; return true ; @@ -203,7 +292,7 @@ bool wxMacCarbonPrintData::TransferTo( wxPrintData &data ) data.SetNoCopies( copies ) ; PMOrientation orientation ; - err = PMGetOrientation( m_macPageFormat , &orientation ) ; + err = PMGetOrientation( m_macPageFormat , &orientation ) ; if ( err == noErr ) { if ( orientation == kPMPortrait || orientation == kPMReversePortrait ) @@ -229,17 +318,11 @@ bool wxMacCarbonPrintData::TransferTo( wxPrintData &data ) } #endif -#ifndef __LP64__ - PMColorMode color ; - err = PMGetColorMode( m_macPrintSettings, &color ) ; - if ( err == noErr ) - data.SetColour( !(color == kPMBlackAndWhite) ) ; -#endif #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 if ( &PMGetDuplex!=NULL ) { PMDuplexMode mode = 0 ; - PMGetDuplex( (PMPrintSettings) m_macPrintSettings, &mode ) ; + PMGetDuplex( m_macPrintSettings, &mode ) ; switch( mode ) { case kPMDuplexNoTumble : @@ -257,21 +340,17 @@ bool wxMacCarbonPrintData::TransferTo( wxPrintData &data ) #endif // PMQualityMode not yet accessible via API - PMPaper paper ; - PMGetPageFormatPaper( m_macPageFormat, &paper ); - - PMRect rPaper; - err = PMGetUnadjustedPaperRect( m_macPageFormat, &rPaper); - if ( err == noErr ) + double height, width; + PMPaperGetHeight(m_macPaper, &height); + PMPaperGetWidth(m_macPaper, &width); + + wxSize sz((int)(width * pt2mm + 0.5 ) , + (int)(height * pt2mm + 0.5 )); + data.SetPaperSize(sz); + wxPaperSize id = wxThePrintPaperDatabase->GetSize(wxSize(sz.x* 10, sz.y * 10)); + if (id != wxPAPER_NONE) { - wxSize sz((int)(( rPaper.right - rPaper.left ) * pt2mm + 0.5 ) , - (int)(( rPaper.bottom - rPaper.top ) * pt2mm + 0.5 )); - data.SetPaperSize(sz); - wxPaperSize id = wxThePrintPaperDatabase->GetSize(wxSize(sz.x* 10, sz.y * 10)); - if (id != wxPAPER_NONE) - { - data.SetPaperId(id); - } + data.SetPaperId(id); } return true ; } @@ -419,7 +498,7 @@ bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) PMResolution res; wxMacCarbonPrintData* nativeData = (wxMacCarbonPrintData*) (m_printDialogData.GetPrintData().GetNativeData()); - PMGetResolution((PMPageFormat) (nativeData->m_macPageFormat), &res); + PMGetResolution((nativeData->m_macPageFormat), &res); printout->SetPPIPrinter(int(res.hRes), int(res.vRes)); // Set printout parameters