Bug 675709 - Export the constants for paper orientation. r=haik
authorHiroshi Hatake <cosmo0920.oucc@gmail.com>
Mon, 06 Feb 2017 11:02:50 +0900
changeset 479813 dd49b1d1bf8f3576be156c727ffa6acf74a4565c
parent 479788 0ca553b86af3e03ce6eb75dd4e2f42c965f44ba6
child 479814 17414d851efb55e58879157ef16f525a2e73ab0b
push id44368
push userbbouvier@mozilla.com
push dateTue, 07 Feb 2017 10:10:28 +0000
reviewershaik
bugs675709
milestone54.0a1
Bug 675709 - Export the constants for paper orientation. r=haik
widget/cocoa/nsDeviceContextSpecX.mm
widget/cocoa/nsPrintOptionsX.mm
widget/cocoa/nsPrintSettingsX.h
widget/cocoa/nsPrintSettingsX.mm
--- a/widget/cocoa/nsDeviceContextSpecX.mm
+++ b/widget/cocoa/nsDeviceContextSpecX.mm
@@ -59,16 +59,29 @@ NS_IMETHODIMP nsDeviceContextSpecX::Init
                                          bool aIsPrintPreview)
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
 
   RefPtr<nsPrintSettingsX> settings(do_QueryObject(aPS));
   if (!settings)
     return NS_ERROR_NO_INTERFACE;
 
+  bool toFile;
+  settings->GetPrintToFile(&toFile);
+
+  bool toPrinter = !toFile && !aIsPrintPreview;
+  if (!toPrinter) {
+    double width, height;
+    settings->GetEffectivePageSize(&width, &height);
+    width /= TWIPS_PER_POINT_FLOAT;
+    height /= TWIPS_PER_POINT_FLOAT;
+
+    settings->SetCocoaPaperSize(width, height);
+  }
+
   mPrintSession = settings->GetPMPrintSession();
   ::PMRetain(mPrintSession);
   mPageFormat = settings->GetPMPageFormat();
   mPrintSettings = settings->GetPMPrintSettings();
 
 #ifdef MOZ_ENABLE_SKIA_PDF
   const nsAdoptingString& printViaPdf =
     mozilla::Preferences::GetString("print.print_via_pdf_encoder");
--- a/widget/cocoa/nsPrintOptionsX.mm
+++ b/widget/cocoa/nsPrintOptionsX.mm
@@ -4,29 +4,16 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsCOMPtr.h"
 #include "nsQueryObject.h"
 #include "nsIServiceManager.h"
 #include "nsPrintOptionsX.h"
 #include "nsPrintSettingsX.h"
 
-// The constants for paper orientation were renamed in 10.9. __MAC_10_9 is
-// defined on OS X 10.9 and later. Although 10.8 and earlier are not supported
-// at this time, this allows for building on those older OS versions. The
-// values are consistent across OS versions so the rename does not affect
-// runtime, just compilation.
-#ifdef __MAC_10_9
-#define NS_PAPER_ORIENTATION_PORTRAIT   (NSPaperOrientationPortrait)
-#define NS_PAPER_ORIENTATION_LANDSCAPE  (NSPaperOrientationLandscape)
-#else
-#define NS_PAPER_ORIENTATION_PORTRAIT   (NSPortraitOrientation)
-#define NS_PAPER_ORIENTATION_LANDSCAPE  (NSLandscapeOrientation)
-#endif
-
 using namespace mozilla::embedding;
 
 nsPrintOptionsX::nsPrintOptionsX()
 {
 }
 
 nsPrintOptionsX::~nsPrintOptionsX()
 {
--- a/widget/cocoa/nsPrintSettingsX.h
+++ b/widget/cocoa/nsPrintSettingsX.h
@@ -4,16 +4,29 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef nsPrintSettingsX_h_
 #define nsPrintSettingsX_h_
 
 #include "nsPrintSettingsImpl.h"  
 #import <Cocoa/Cocoa.h>
 
+// The constants for paper orientation were renamed in 10.9. __MAC_10_9 is
+// defined on OS X 10.9 and later. Although 10.8 and earlier are not supported
+// at this time, this allows for building on those older OS versions. The
+// values are consistent across OS versions so the rename does not affect
+// runtime, just compilation.
+#ifdef __MAC_10_9
+#define NS_PAPER_ORIENTATION_PORTRAIT   (NSPaperOrientationPortrait)
+#define NS_PAPER_ORIENTATION_LANDSCAPE  (NSPaperOrientationLandscape)
+#else
+#define NS_PAPER_ORIENTATION_PORTRAIT   (NSPortraitOrientation)
+#define NS_PAPER_ORIENTATION_LANDSCAPE  (NSLandscapeOrientation)
+#endif
+
 #define NS_PRINTSETTINGSX_IID \
 { 0x0DF2FDBD, 0x906D, 0x4726, \
   { 0x9E, 0x4D, 0xCF, 0xE0, 0x87, 0x8D, 0x70, 0x7C } }
 
 class nsPrintSettingsX : public nsPrintSettings
 {
 public:
   NS_DECLARE_STATIC_IID_ACCESSOR(NS_PRINTSETTINGSX_IID)
@@ -46,28 +59,44 @@ public:
 
   // Re-initialize mAdjustedPaper{Width,Height} with values from mPageFormat.
   // Should be called whenever mPageFormat is initialized or overwritten.
   nsresult InitAdjustedPaperSize();
 
   void SetInchesScale(float aWidthScale, float aHeightScale);
   void GetInchesScale(float *aWidthScale, float *aHeightScale);
 
+  NS_IMETHOD SetPaperSizeUnit(int16_t aPaperSizeUnit) override;
+
+  NS_IMETHOD SetScaling(double aScaling) override;
+  NS_IMETHOD SetToFileName(const char16_t * aToFileName) override;
+
+  NS_IMETHOD GetOrientation(int32_t *aOrientation) override;
+  NS_IMETHOD SetOrientation(int32_t aOrientation) override;
+
+  NS_IMETHOD SetUnwriteableMarginTop(double aUnwriteableMarginTop) override;
+  NS_IMETHOD SetUnwriteableMarginLeft(double aUnwriteableMarginLeft) override;
+  NS_IMETHOD SetUnwriteableMarginBottom(double aUnwriteableMarginBottom) override;
+  NS_IMETHOD SetUnwriteableMarginRight(double aUnwriteableMarginRight) override;
+
   void SetAdjustedPaperSize(double aWidth, double aHeight);
   void GetAdjustedPaperSize(double *aWidth, double *aHeight);
+  nsresult SetCocoaPaperSize(double aWidth, double aHeight);
 
 protected:
   virtual ~nsPrintSettingsX();
 
   nsPrintSettingsX(const nsPrintSettingsX& src);
   nsPrintSettingsX& operator=(const nsPrintSettingsX& rhs);
 
   nsresult _Clone(nsIPrintSettings **_retval) override;
   nsresult _Assign(nsIPrintSettings *aPS) override;
 
+  int GetCocoaUnit(int16_t aGeckoUnit);
+
   // The out param has a ref count of 1 on return so caller needs to PMRelase() when done.
   OSStatus CreateDefaultPageFormat(PMPrintSession aSession, PMPageFormat& outFormat);
   OSStatus CreateDefaultPrintSettings(PMPrintSession aSession, PMPrintSettings& outSettings);
 
   NSPrintInfo* mPrintInfo;
 
   // Scaling factors used to convert the NSPrintInfo
   // paper size units to inches
--- a/widget/cocoa/nsPrintSettingsX.mm
+++ b/widget/cocoa/nsPrintSettingsX.mm
@@ -249,24 +249,247 @@ NS_IMETHODIMP nsPrintSettingsX::SetPaper
   mPaperHeight = aPaperHeight;
   mAdjustedPaperHeight = aPaperHeight * mHeightScale;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsPrintSettingsX::GetEffectivePageSize(double *aWidth, double *aHeight)
 {
-  *aWidth  = NS_INCHES_TO_TWIPS(mAdjustedPaperWidth / mWidthScale);
-  *aHeight = NS_INCHES_TO_TWIPS(mAdjustedPaperHeight / mHeightScale);
+  if (kPaperSizeInches == GetCocoaUnit(mPaperSizeUnit)) {
+    *aWidth  = NS_INCHES_TO_TWIPS(mAdjustedPaperWidth / mWidthScale);
+    *aHeight = NS_INCHES_TO_TWIPS(mAdjustedPaperHeight / mHeightScale);
+  } else {
+    *aWidth  = NS_MILLIMETERS_TO_TWIPS(mAdjustedPaperWidth / mWidthScale);
+    *aHeight = NS_MILLIMETERS_TO_TWIPS(mAdjustedPaperHeight / mHeightScale);
+  }
   return NS_OK;
 }
 
 void nsPrintSettingsX::SetAdjustedPaperSize(double aWidth, double aHeight)
 {
   mAdjustedPaperWidth = aWidth;
   mAdjustedPaperHeight = aHeight;
 }
 
 void nsPrintSettingsX::GetAdjustedPaperSize(double *aWidth, double *aHeight)
 {
   *aWidth = mAdjustedPaperWidth;
   *aHeight = mAdjustedPaperHeight;
 }
+
+NS_IMETHODIMP
+nsPrintSettingsX::SetPaperSizeUnit(int16_t aPaperSizeUnit)
+{
+  mPaperSizeUnit = aPaperSizeUnit;
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsPrintSettingsX::SetScaling(double aScaling)
+{
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+
+  NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
+  [printInfoDict setObject: [NSNumber numberWithFloat: aScaling]
+   forKey: NSPrintScalingFactor];
+  NSPrintInfo* newPrintInfo =
+      [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
+  if (NS_WARN_IF(!newPrintInfo)) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
+  SetCocoaPrintInfo(newPrintInfo);
+  [newPrintInfo release];
+  return NS_OK;
+
+  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+}
+
+NS_IMETHODIMP
+nsPrintSettingsX::SetToFileName(const char16_t *aToFileName)
+{
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+
+  NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
+  nsString filename = nsDependentString(aToFileName);
+
+  NSURL* jobSavingURL =
+      [NSURL fileURLWithPath: nsCocoaUtils::ToNSString(filename)];
+  if (jobSavingURL) {
+    [printInfoDict setObject: NSPrintSaveJob forKey: NSPrintJobDisposition];
+    [printInfoDict setObject: jobSavingURL forKey: NSPrintJobSavingURL];
+  }
+  NSPrintInfo* newPrintInfo =
+      [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
+  if (NS_WARN_IF(!newPrintInfo)) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
+  SetCocoaPrintInfo(newPrintInfo);
+  [newPrintInfo release];
+  return NS_OK;
+
+  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+}
+
+NS_IMETHODIMP
+nsPrintSettingsX::GetOrientation(int32_t *aOrientation)
+{
+  if ([mPrintInfo orientation] == NS_PAPER_ORIENTATION_PORTRAIT) {
+    *aOrientation = nsIPrintSettings::kPortraitOrientation;
+  } else {
+    *aOrientation = nsIPrintSettings::kLandscapeOrientation;
+  }
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsPrintSettingsX::SetOrientation(int32_t aOrientation)
+{
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+
+  NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
+  if (aOrientation == nsIPrintSettings::kPortraitOrientation) {
+    [printInfoDict setObject: [NSNumber numberWithInt: NS_PAPER_ORIENTATION_PORTRAIT]
+     forKey: NSPrintOrientation];
+  } else {
+    [printInfoDict setObject: [NSNumber numberWithInt: NS_PAPER_ORIENTATION_LANDSCAPE]
+     forKey: NSPrintOrientation];
+  }
+  NSPrintInfo* newPrintInfo =
+      [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
+  if (NS_WARN_IF(!newPrintInfo)) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
+  SetCocoaPrintInfo(newPrintInfo);
+  [newPrintInfo release];
+  return NS_OK;
+
+  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+}
+
+NS_IMETHODIMP
+nsPrintSettingsX::SetUnwriteableMarginTop(double aUnwriteableMarginTop)
+{
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+
+  nsPrintSettings::SetUnwriteableMarginTop(aUnwriteableMarginTop);
+  NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
+  [printInfoDict setObject : [NSNumber numberWithDouble: aUnwriteableMarginTop]
+   forKey : NSPrintTopMargin];
+  NSPrintInfo* newPrintInfo =
+      [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
+  if (NS_WARN_IF(!newPrintInfo)) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
+  SetCocoaPrintInfo(newPrintInfo);
+  [newPrintInfo release];
+  return NS_OK;
+
+  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+}
+
+NS_IMETHODIMP
+nsPrintSettingsX::SetUnwriteableMarginLeft(double aUnwriteableMarginLeft)
+{
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+
+  nsPrintSettings::SetUnwriteableMarginLeft(aUnwriteableMarginLeft);
+  NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
+  [printInfoDict setObject : [NSNumber numberWithDouble: aUnwriteableMarginLeft]
+   forKey : NSPrintLeftMargin];
+  NSPrintInfo* newPrintInfo =
+      [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
+  if (NS_WARN_IF(!newPrintInfo)) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
+  SetCocoaPrintInfo(newPrintInfo);
+  [newPrintInfo release];
+  return NS_OK;
+
+  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+}
+
+NS_IMETHODIMP
+nsPrintSettingsX::SetUnwriteableMarginBottom(double aUnwriteableMarginBottom)
+{
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+
+  nsPrintSettings::SetUnwriteableMarginBottom(aUnwriteableMarginBottom);
+  NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
+  [printInfoDict setObject : [NSNumber numberWithDouble: aUnwriteableMarginBottom]
+   forKey : NSPrintBottomMargin];
+  NSPrintInfo* newPrintInfo =
+      [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
+  if (NS_WARN_IF(!newPrintInfo)) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
+  SetCocoaPrintInfo(newPrintInfo);
+  [newPrintInfo release];
+  return NS_OK;
+
+  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+}
+
+NS_IMETHODIMP
+nsPrintSettingsX::SetUnwriteableMarginRight(double aUnwriteableMarginRight)
+{
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+
+  nsPrintSettings::SetUnwriteableMarginRight(aUnwriteableMarginRight);
+  NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
+  [printInfoDict setObject : [NSNumber numberWithDouble: aUnwriteableMarginRight]
+   forKey : NSPrintRightMargin];
+  NSPrintInfo* newPrintInfo =
+      [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
+  if (NS_WARN_IF(!newPrintInfo)) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
+  SetCocoaPrintInfo(newPrintInfo);
+  [newPrintInfo release];
+  return NS_OK;
+
+  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+}
+
+int
+nsPrintSettingsX::GetCocoaUnit(int16_t aGeckoUnit)
+{
+  if (aGeckoUnit == kPaperSizeMillimeters)
+    return kPaperSizeMillimeters;
+  else
+    return kPaperSizeInches;
+}
+
+nsresult nsPrintSettingsX::SetCocoaPaperSize(double aWidth, double aHeight)
+{
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+
+  NSSize paperSize;
+  NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
+  if ([mPrintInfo orientation] == NS_PAPER_ORIENTATION_PORTRAIT) {
+    // switch widths and heights
+    paperSize = NSMakeSize(aWidth, aHeight);
+    [printInfoDict setObject: [NSValue valueWithSize: paperSize]
+     forKey: NSPrintPaperSize];
+  } else {
+    paperSize = NSMakeSize(aHeight, aWidth);
+    [printInfoDict setObject: [NSValue valueWithSize: paperSize]
+     forKey: NSPrintPaperSize];
+  }
+  NSPrintInfo* newPrintInfo =
+      [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
+  if (NS_WARN_IF(!newPrintInfo)) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
+  SetCocoaPrintInfo(newPrintInfo);
+  [newPrintInfo release];
+  return NS_OK;
+
+  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+}