]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/core/printmac.cpp
Fix signatures of various image handlers methods.
[wxWidgets.git] / src / osx / core / printmac.cpp
index d7aae24967aec36bcd4f267938c51854a12d1844..3e14c848dcee8fad2a04b41a605e0849019599d0 100644 (file)
@@ -27,7 +27,7 @@
     #include "wx/math.h"
 #endif
 
-#include "wx/osx/uma.h"
+#include "wx/osx/private.h"
 
 #include "wx/osx/printmac.h"
 #include "wx/osx/private/print.h"
 
 #include <stdlib.h>
 
-IMPLEMENT_DYNAMIC_CLASS(wxMacCarbonPrintData, wxPrintNativeDataBase)
-IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase)
-IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase)
+//
+// move to print_osx.cpp
+//
 
-bool wxMacCarbonPrintData::IsOk() const
+IMPLEMENT_DYNAMIC_CLASS(wxOSXPrintData, wxPrintNativeDataBase)
+
+bool wxOSXPrintData::IsOk() const
 {
     return (m_macPageFormat != kPMNoPageFormat) && (m_macPrintSettings != kPMNoPrintSettings) && (m_macPrintSession != kPMNoReference);
 }
-wxMacCarbonPrintData::wxMacCarbonPrintData()
+
+wxOSXPrintData::wxOSXPrintData()
 {
     m_macPageFormat = kPMNoPageFormat;
     m_macPrintSettings = kPMNoPrintSettings;
     m_macPrintSession = kPMNoReference ;
-    ValidateOrCreate() ;
+    m_macPaper = kPMNoData;
 }
 
-wxMacCarbonPrintData::~wxMacCarbonPrintData()
+wxOSXPrintData::~wxOSXPrintData()
 {
-    if (m_macPageFormat != kPMNoPageFormat)
-    {
-        (void)PMRelease(m_macPageFormat);
-        m_macPageFormat = kPMNoPageFormat;
-    }
+}
 
-    if (m_macPrintSettings != kPMNoPrintSettings)
-    {
-        (void)PMRelease(m_macPrintSettings);
-        m_macPrintSettings = kPMNoPrintSettings;
-    }
+void wxOSXPrintData::UpdateFromPMState()
+{
+}
 
-    if ( m_macPrintSession != kPMNoReference )
-    {
-        (void)PMRelease(m_macPrintSession);
-        m_macPrintSession = kPMNoReference;
-    }
+void wxOSXPrintData::UpdateToPMState()
+{
 }
 
-void wxMacCarbonPrintData::ValidateOrCreate()
+bool wxOSXPrintData::TransferFrom( const wxPrintData &data )
 {
-    OSStatus err = noErr ;
-    if ( m_macPrintSession == kPMNoReference )
-    {
-        err = PMCreateSession( &m_macPrintSession ) ;
-    }
-    //  Set up a valid PageFormat object.
-    if ( m_macPageFormat == kPMNoPageFormat)
-    {
-        err = PMCreatePageFormat(&m_macPageFormat);
+    PMPrinter printer;
+    PMSessionGetCurrentPrinter(m_macPrintSession, &printer);
 
-        //  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))
+    wxSize papersize = wxDefaultSize;
+    const wxPaperSize paperId = data.GetPaperId();
+    if ( paperId != wxPAPER_NONE && wxThePrintPaperDatabase )
+    {
+        papersize = wxThePrintPaperDatabase->GetSize(paperId);
+        if ( papersize != wxDefaultSize )
         {
-            err = PMSessionDefaultPageFormat(m_macPrintSession,
-                m_macPageFormat);
+            papersize.x /= 10;
+            papersize.y /= 10;
         }
     }
     else
     {
-        err = PMSessionValidatePageFormat(m_macPrintSession,
-            m_macPageFormat,
-            kPMDontWantBoolean);
+        papersize = data.GetPaperSize();
     }
-
-    //  Set up a valid PrintSettings object.
-    if ( m_macPrintSettings == kPMNoPrintSettings)
+    
+    if ( papersize != wxDefaultSize )
     {
-        err = PMCreatePrintSettings( &m_macPrintSettings);
-
-        //  Note that PMPrintSettings is not session-specific, but calling
-        //  PMSessionDefaultPrintSettings assigns values specific to the printer
-        //  associated with the current printing session.
-        if ((err == noErr) &&
-            ( m_macPrintSettings != kPMNoPrintSettings))
+        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 )
         {
-            err = PMSessionDefaultPrintSettings(m_macPrintSession,
-               m_macPrintSettings);
+            // 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 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);
+            }
         }
     }
-    else
+
+    CFArrayRef printerList;
+    CFIndex index, count;
+    CFStringRef name;
+    
+    if (PMServerCreatePrinterList(kPMServerLocal, &printerList) == noErr)
     {
-        err = PMSessionValidatePrintSettings( m_macPrintSession,
-            m_macPrintSettings,
-            kPMDontWantBoolean);
+        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);
     }
-}
-
-bool wxMacCarbonPrintData::TransferFrom( const wxPrintData &data )
-{
-    ValidateOrCreate() ;
-    PMSetCopies( (PMPrintSettings) m_macPrintSettings , data.GetNoCopies() , false ) ;
+    
+    PMSetCopies( m_macPrintSettings , data.GetNoCopies() , false ) ;
+    PMSetCollate(m_macPrintSettings, data.GetCollate());
     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 , wxCFStringRef( m_printerName , wxFont::GetDefaultEncoding() ) ) ;
-#endif
-#if wxOSX_USE_CARBON
-    PMColorMode color ;
-    PMGetColorMode(  (PMPrintSettings) m_macPrintSettings, &color ) ;
-    if ( data.GetColour() )
-    {
-        if ( color == kPMBlackAndWhite )
-            PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMColor ) ;
-    }
-    else
-        PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMBlackAndWhite ) ;
-#endif
-
+    
     PMDuplexMode mode = 0 ;
     switch( data.GetDuplex() )
     {
@@ -168,38 +204,47 @@ bool wxMacCarbonPrintData::TransferFrom( const wxPrintData &data )
             mode = kPMDuplexNone ;
             break ;
     }
-    PMSetDuplex( (PMPrintSettings) m_macPrintSettings, mode ) ;
+    PMSetDuplex(  m_macPrintSettings, mode ) ;
 
     // PMQualityMode not yet accessible via API
-    // todo paperSize
-
+    
+    
+    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;
-    PMPrinter printer;
-    PMSessionGetCurrentPrinter(m_macPrintSession, &printer);
-
-#if wxOSX_USE_CARBON
     PMTag tag = kPMMaxSquareResolution;
     PMPrinterGetPrinterResolution(printer, tag, &res);
-    PMSetResolution((PMPageFormat) m_macPageFormat, &res);
-#else
-    PMPrinterGetOutputResolution( printer,  
-        (PMPrintSettings) m_macPrintSettings,  &res) ;
-    // TODO transfer ? into page format ?
-    // may fail !
+    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((PMPrintSession) m_macPrintSession,
-            (PMPageFormat) m_macPageFormat,
-            kPMDontWantBoolean) ;
+    
+    PMSessionValidatePageFormat(m_macPrintSession,
+        m_macPageFormat, kPMDontWantBoolean);
+    PMSessionValidatePrintSettings(m_macPrintSession,
+        m_macPrintSettings, kPMDontWantBoolean);
+#if wxOSX_USE_COCOA
+    UpdateFromPMState();
+#endif
 
     return true ;
 }
 
-bool wxMacCarbonPrintData::TransferTo( wxPrintData &data )
+bool wxOSXPrintData::TransferTo( wxPrintData &data )
 {
     OSStatus err = noErr ;
-
+#if wxOSX_USE_COCOA
+    UpdateToPMState();
+#endif
     UInt32 copies ;
     err = PMGetCopies( m_macPrintSettings , &copies ) ;
     if ( err == noErr )
@@ -221,25 +266,24 @@ bool wxMacCarbonPrintData::TransferTo( wxPrintData &data )
         }
     }
 
-    // collate cannot be set
-#if 0
+    Boolean collate;
+    if (PMGetCollate(m_macPrintSettings, &collate) == noErr)
+        data.SetCollate(collate);
+    
+    CFStringRef name;
+    PMPrinter printer ;
+    PMSessionGetCurrentPrinter( m_macPrintSession, &printer );
+    if (PMPrinterIsDefault(printer))
+        data.SetPrinterName(wxEmptyString);
+    else
     {
-        wxCFStringRef name ;
-        PMPrinter printer ;
-        PMSessionGetCurrentPrinter( m_macPrintSession ,
-            &printer ) ;
-        m_printerName = name.AsString() ;
+        name = PMPrinterGetName(printer);
+        CFRetain(name);
+        data.SetPrinterName(wxCFStringRef(name).AsString());
     }
-#endif
-
-#ifndef __LP64__
-    PMColorMode color ;
-    err = PMGetColorMode( m_macPrintSettings, &color ) ;
-    if ( err == noErr )
-        data.SetColour( !(color == kPMBlackAndWhite) ) ;
-#endif
+    
     PMDuplexMode mode = 0 ;
-    PMGetDuplex( (PMPrintSettings) m_macPrintSettings, &mode ) ;
+    PMGetDuplex(  m_macPrintSettings, &mode ) ;
     switch( mode )
     {
         case kPMDuplexNoTumble :
@@ -255,34 +299,33 @@ bool wxMacCarbonPrintData::TransferTo( wxPrintData &data )
     }
     // 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 ;
 }
 
-void wxMacCarbonPrintData::TransferFrom( wxPageSetupData *WXUNUSED(data) )
+void wxOSXPrintData::TransferFrom( wxPageSetupData *WXUNUSED(data) )
 {
     // should we setup the page rect here ?
     // since MacOS sometimes has two same paper rects with different
     // page rects we could make it roundtrip safe perhaps
 }
 
-void wxMacCarbonPrintData::TransferTo( wxPageSetupData* data )
+void wxOSXPrintData::TransferTo( wxPageSetupData* data )
 {
+#if wxOSX_USE_COCOA
+    UpdateToPMState();
+#endif
     PMRect rPaper;
     OSStatus err = PMGetUnadjustedPaperRect(m_macPageFormat, &rPaper);
     if ( err == noErr )
@@ -321,8 +364,11 @@ void wxMacCarbonPrintData::TransferTo( wxPageSetupData* data )
     }
 }
 
-void wxMacCarbonPrintData::TransferTo( wxPrintDialogData* data )
+void wxOSXPrintData::TransferTo( wxPrintDialogData* data )
 {
+#if wxOSX_USE_COCOA
+    UpdateToPMState();
+#endif
     UInt32 minPage , maxPage ;
     PMGetPageRange( m_macPrintSettings , &minPage , &maxPage ) ;
     data->SetMinPage( minPage ) ;
@@ -338,7 +384,7 @@ void wxMacCarbonPrintData::TransferTo( wxPrintDialogData* data )
         data->SetAllPages( true ) ;
         // This means all pages, more or less
         data->SetFromPage(1);
-        data->SetToPage(32000);
+        data->SetToPage(9999);
     }
     else
     {
@@ -348,7 +394,7 @@ void wxMacCarbonPrintData::TransferTo( wxPrintDialogData* data )
     }
 }
 
-void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData* data )
+void wxOSXPrintData::TransferFrom( wxPrintDialogData* data )
 {
     // Respect the value of m_printAllPages
     if ( data->GetAllPages() )
@@ -362,12 +408,28 @@ void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData* data )
         PMSetLastPage( m_macPrintSettings , (UInt32) kPMPrintAllPages, true ) ;
     else
         PMSetLastPage( m_macPrintSettings , (UInt32) data->GetToPage() , false ) ;
+#if wxOSX_USE_COCOA
+    UpdateFromPMState();
+#endif
+}
+
+wxPrintNativeDataBase* wxOSXCreatePrintData()
+{
+#if wxOSX_USE_COCOA
+    return new wxOSXCocoaPrintData();
+#endif
+#if wxOSX_USE_CARBON
+    return new wxOSXCarbonPrintData();
+#endif
+    return NULL;
 }
 
 /*
 * Printer
 */
 
+IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase)
+
 wxMacPrinter::wxMacPrinter(wxPrintDialogData *data):
 wxPrinterBase(data)
 {
@@ -411,25 +473,19 @@ bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
     // May have pressed cancel.
     if (!dc || !dc->IsOk())
     {
-        if (dc)
-            delete dc;
+        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;
-    wxMacCarbonPrintData* nativeData = (wxMacCarbonPrintData*)
+    wxOSXPrintData* nativeData = (wxOSXPrintData*)
           (m_printDialogData.GetPrintData().GetNativeData());
-#if wxOSX_USE_CARBON
-    PMGetResolution((PMPageFormat) (nativeData->m_macPageFormat), &res);
-#else 
-    PMPrinter printer;
-    PMSessionGetCurrentPrinter(nativeData->m_macPrintSession, &printer);
-    PMPrinterGetOutputResolution( printer, nativeData->m_macPrintSettings, &res) ;
-#endif
+    PMGetResolution( (nativeData->GetPageFormat()), &res);
     printout->SetPPIPrinter(int(res.hRes), int(res.vRes));
-
+#endif
     // Set printout parameters
     printout->SetDC(dc);
 
@@ -509,7 +565,7 @@ bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
 
 wxDC* wxMacPrinter::PrintDialog(wxWindow *parent)
 {
-    wxDC* dc = (wxDC*) NULL;
+    wxDC* dc = NULL;
 
     wxPrintDialog dialog(parent, & m_printDialogData);
     int ret = dialog.ShowModal();
@@ -544,6 +600,8 @@ bool wxMacPrinter::Setup(wxWindow *WXUNUSED(parent))
 * Print preview
 */
 
+IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase)
+
 wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout,
                                      wxPrintout *printoutForPrinting,
                                      wxPrintDialogData *data)
@@ -618,4 +676,39 @@ void wxMacPrintPreview::DetermineScaling(void)
     m_previewScaleY = float(ppiScreen.y) / ppiPrinter.y;
 }
 
+//
+// end of print_osx.cpp
+//
+
+#if wxOSX_USE_CARBON
+
+IMPLEMENT_DYNAMIC_CLASS(wxOSXCarbonPrintData, wxOSXPrintData)
+
+wxOSXCarbonPrintData::wxOSXCarbonPrintData()
+{
+    if ( PMCreateSession( &m_macPrintSession ) == noErr )
+    {
+        if ( PMCreatePageFormat(&m_macPageFormat) == noErr )
+        {
+            PMSessionDefaultPageFormat(m_macPrintSession,
+                    m_macPageFormat);
+            PMGetPageFormatPaper(m_macPageFormat, &m_macPaper);
+        }
+        
+        if ( PMCreatePrintSettings(&m_macPrintSettings) == noErr )
+        {
+            PMSessionDefaultPrintSettings(m_macPrintSession,
+                m_macPrintSettings);
+        }
+    }
+}
+
+wxOSXCarbonPrintData::~wxOSXCarbonPrintData()
+{
+    (void)PMRelease(m_macPageFormat);
+    (void)PMRelease(m_macPrintSettings);
+    (void)PMRelease(m_macPrintSession);
+}
+#endif
+
 #endif