]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/core/printmac.cpp
Use the data scheme to load resources in the WebKitGTK+ implementation, rather than...
[wxWidgets.git] / src / osx / core / printmac.cpp
index 3ed74451c3a1e440393ee30d663ab841c3c91a22..9b1eded8fe430ecab38cc8ea4d28a8afb7af4d0e 100644 (file)
@@ -46,9 +46,9 @@ static int ResolutionSorter(const void *e1, const void *e2)
 {
     const PMResolution *res1 = (const PMResolution *)e1;
     const PMResolution *res2 = (const PMResolution *)e2;
-    int area1 = res1->hRes * res1->vRes;
-    int area2 = res2->hRes * res2->vRes;
-    
+    const double area1 = res1->hRes * res1->vRes;
+    const double area2 = res2->hRes * res2->vRes;
+
     if (area1 < area2)
         return -1;
     else if (area1 > area2)
@@ -91,7 +91,7 @@ static PMResolution *GetSupportedResolutions(PMPrinter printer, UInt32 *count)
                 resolutions[realCount++] = res;
         }
         qsort(resolutions, realCount, sizeof(PMResolution), ResolutionSorter);
-        
+
         *count = realCount;
     }
     if ((*count == 0) && (resolutions))
@@ -131,12 +131,12 @@ void wxOSXPrintData::UpdateToPMState()
 {
 }
 
-bool wxOSXPrintData::TransferFrom( const wxPrintData &data )
+void wxOSXPrintData::TransferPrinterNameFrom( const wxPrintData &data )
 {
     CFArrayRef printerList;
     CFIndex index, count;
     CFStringRef name;
-    
+
     if (PMServerCreatePrinterList(kPMServerLocal, &printerList) == noErr)
     {
         PMPrinter printer = NULL;
@@ -158,7 +158,10 @@ bool wxOSXPrintData::TransferFrom( const wxPrintData &data )
             PMSessionSetCurrentPMPrinter(m_macPrintSession, printer);
         CFRelease(printerList);
     }
-    
+}
+
+void wxOSXPrintData::TransferPaperInfoFrom( const wxPrintData &data )
+{
     PMPrinter printer;
     PMSessionGetCurrentPrinter(m_macPrintSession, &printer);
 
@@ -220,14 +223,14 @@ bool wxOSXPrintData::TransferFrom( const wxPrintData &data )
                     if ( PMPaperCreateCustom != NULL)
                     {
                         PMPaperCreateCustom(printer, wxCFStringRef( id, wxFont::GetDefaultEncoding() ), wxCFStringRef( name, wxFont::GetDefaultEncoding() ),
-                            papersize.x, papersize.y, &margins, &paper);
+                                            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);
+                                      papersize.x, papersize.y, &margins, &paper);
                     }
 #endif
                 }
@@ -248,10 +251,10 @@ bool wxOSXPrintData::TransferFrom( const wxPrintData &data )
     PMSetCollate(m_macPrintSettings, data.GetCollate());
     if ( data.IsOrientationReversed() )
         PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
-            kPMReverseLandscape : kPMReversePortrait , false ) ;
+                         kPMReverseLandscape : kPMReversePortrait , false ) ;
     else
         PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
-            kPMLandscape : kPMPortrait , false ) ;
+                         kPMLandscape : kPMPortrait , false ) ;
 
     PMDuplexMode mode = 0 ;
     switch( data.GetDuplex() )
@@ -272,10 +275,16 @@ bool wxOSXPrintData::TransferFrom( const wxPrintData &data )
 
     if ( data.IsOrientationReversed() )
         PMSetOrientation(  m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
-            kPMReverseLandscape : kPMReversePortrait , false ) ;
+                         kPMReverseLandscape : kPMReversePortrait , false ) ;
     else
         PMSetOrientation(  m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
-            kPMLandscape : kPMPortrait , false ) ;
+                         kPMLandscape : kPMPortrait , false ) ;
+}
+
+void wxOSXPrintData::TransferResolutionFrom( const wxPrintData &data )
+{
+    PMPrinter printer;
+    PMSessionGetCurrentPrinter(m_macPrintSession, &printer);
 
     UInt32 resCount;
     PMResolution *resolutions = GetSupportedResolutions(printer, &resCount);
@@ -284,7 +293,7 @@ bool wxOSXPrintData::TransferFrom( const wxPrintData &data )
         wxPrintQuality quality = data.GetQuality();
         if (quality >= 0)
             quality = wxPRINT_QUALITY_HIGH;
-        
+
         PMResolution res = resolutions[((quality - wxPRINT_QUALITY_DRAFT) * (resCount - 1)) / 3];
 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
         if ( PMPrinterSetOutputResolution != NULL )
@@ -299,13 +308,22 @@ bool wxOSXPrintData::TransferFrom( const wxPrintData &data )
 
         free(resolutions);
     }
+}
+
+bool wxOSXPrintData::TransferFrom( const wxPrintData &data )
+{
+    TransferPrinterNameFrom(data);
+    TransferPaperInfoFrom(data);
+    TransferResolutionFrom(data);
+
     // 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);
+                                m_macPageFormat, kPMDontWantBoolean);
     PMSessionValidatePrintSettings(m_macPrintSession,
-        m_macPrintSettings, kPMDontWantBoolean);
+                                   m_macPrintSettings, kPMDontWantBoolean);
+
 #if wxOSX_USE_COCOA
     UpdateFromPMState();
 #endif
@@ -313,12 +331,26 @@ bool wxOSXPrintData::TransferFrom( const wxPrintData &data )
     return true ;
 }
 
-bool wxOSXPrintData::TransferTo( wxPrintData &data )
+void wxOSXPrintData::TransferPrinterNameTo( wxPrintData &data )
+{
+    CFStringRef name;
+    PMPrinter printer ;
+    PMSessionGetCurrentPrinter( m_macPrintSession, &printer );
+    if (PMPrinterIsDefault(printer))
+        data.SetPrinterName(wxEmptyString);
+    else
+    {
+        name = PMPrinterGetName(printer);
+        CFRetain(name);
+        data.SetPrinterName(wxCFStringRef(name).AsString());
+    }
+}
+
+void wxOSXPrintData::TransferPaperInfoTo( wxPrintData &data )
 {
+    PMPrinter printer ;
+    PMSessionGetCurrentPrinter( m_macPrintSession, &printer );
     OSStatus err = noErr ;
-#if wxOSX_USE_COCOA
-    UpdateToPMState();
-#endif
     UInt32 copies ;
     err = PMGetCopies( m_macPrintSettings , &copies ) ;
     if ( err == noErr )
@@ -344,17 +376,6 @@ bool wxOSXPrintData::TransferTo( wxPrintData &data )
     if (PMGetCollate(m_macPrintSettings, &collate) == noErr)
         data.SetCollate(collate);
 
-    CFStringRef name;
-    PMPrinter printer ;
-    PMSessionGetCurrentPrinter( m_macPrintSession, &printer );
-    if (PMPrinterIsDefault(printer))
-        data.SetPrinterName(wxEmptyString);
-    else
-    {
-        name = PMPrinterGetName(printer);
-        CFRetain(name);
-        data.SetPrinterName(wxCFStringRef(name).AsString());
-    }
 
     PMDuplexMode mode = 0 ;
     PMGetDuplex(  m_macPrintSettings, &mode ) ;
@@ -372,9 +393,28 @@ bool wxOSXPrintData::TransferTo( wxPrintData &data )
             break ;
     }
 
+    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)
+    {
+        data.SetPaperId(id);
+    }
+}
+
+void wxOSXPrintData::TransferResolutionTo( wxPrintData &data )
+{
+    PMPrinter printer ;
+    PMSessionGetCurrentPrinter( m_macPrintSession, &printer );
+
     /* assume high quality, will change below if we are able to */
     data.SetQuality(wxPRINT_QUALITY_HIGH);
-    
+
     PMResolution *resolutions;
     UInt32 resCount;
     resolutions = GetSupportedResolutions(printer, &resCount);
@@ -405,20 +445,18 @@ bool wxOSXPrintData::TransferTo( wxPrintData &data )
                 data.SetQuality((((i + 1) * 3) / resCount) + wxPRINT_QUALITY_DRAFT);
         }
         free(resolutions);
-    }    
-    
-    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)
-    {
-        data.SetPaperId(id);
     }
+}
+
+bool wxOSXPrintData::TransferTo( wxPrintData &data )
+{
+#if wxOSX_USE_COCOA
+    UpdateToPMState();
+#endif
+
+    TransferPrinterNameTo(data);
+    TransferPaperInfoTo(data);
+    TransferResolutionTo(data);
     return true ;
 }
 
@@ -473,7 +511,7 @@ void wxOSXPrintData::TransferTo( wxPageSetupData* data )
 }
 
 void wxOSXPrintData::TransferTo( wxPrintDialogData* data )
-{ 
+{
 #if wxOSX_USE_COCOA
     UpdateToPMState();
 #endif
@@ -525,11 +563,11 @@ wxPrintNativeDataBase* wxOSXCreatePrintData()
 {
 #if wxOSX_USE_COCOA
     return new wxOSXCocoaPrintData();
-#endif
-#if wxOSX_USE_CARBON
+#elif wxOSX_USE_CARBON
     return new wxOSXCarbonPrintData();
-#endif
+#else
     return NULL;
+#endif
 }
 
 /*
@@ -553,21 +591,10 @@ bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
     sm_abortWindow = NULL;
 
     if (!printout)
+    {
+        sm_lastError = wxPRINTER_ERROR;
         return false;
-
-    printout->SetIsPreview(false);
-
-    // Get some parameters from the printout, if defined
-    int fromPage, toPage;
-    int minPage, maxPage;
-    printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
-
-    if (maxPage == 0) return false;
-
-    // Only set min and max, because from and to will be
-    // set by the user
-    m_printDialogData.SetMinPage(minPage);
-    m_printDialogData.SetMaxPage(maxPage);
+    }
 
     if (m_printDialogData.GetMinPage() < 1)
         m_printDialogData.SetMinPage(1);
@@ -608,7 +635,7 @@ bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
 
     if (PMSessionGetCurrentPrinter(nativeData->GetPrintSession(), &printer) == noErr)
     {
-#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
         if ( PMPrinterGetOutputResolution != NULL )
         {
             if (PMPrinterGetOutputResolution( printer, nativeData->GetPrintSettings(), &res) == -9589 /* kPMKeyNotFound */ )
@@ -619,11 +646,16 @@ bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
         else
 #endif
         {
-#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 
+#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
             PMPrinterGetPrinterResolution(printer, kPMCurrentValue, &res);
 #endif
         }
     }
+    else
+    {
+        // fallback
+        res.hRes = res.vRes = 300;
+    }
     printout->SetPPIPrinter(int(res.hRes), int(res.vRes));
 
     // Set printout parameters
@@ -642,6 +674,22 @@ bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
 
     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)
+    {
+        sm_lastError = wxPRINTER_ERROR;
+        return false;
+    }
+
+    // Only set min and max, because from and to will be
+    // set by the user
+    m_printDialogData.SetMinPage(minPage);
+    m_printDialogData.SetMaxPage(maxPage);
+
     printout->OnBeginPrinting();
 
     bool keepGoing = true;
@@ -659,7 +707,6 @@ bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
     {
         if (sm_abortIt)
         {
-                keepGoing = false;
                 break;
         }
         else
@@ -676,8 +723,7 @@ bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
     if (sm_abortWindow)
     {
         sm_abortWindow->Show(false);
-        delete sm_abortWindow;
-        sm_abortWindow = NULL;
+        wxDELETE(sm_abortWindow);
     }
 
     wxEndBusyCursor();
@@ -717,7 +763,7 @@ bool wxMacPrinter::Setup(wxWindow *WXUNUSED(parent))
     return (ret == wxID_OK);
 #endif
 
-    return wxID_CANCEL;
+    return false;
 }
 
 /*