1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/cocoa/nsPrintSettingsX.mm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,199 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsPrintSettingsX.h" 1.10 +#include "nsObjCExceptions.h" 1.11 + 1.12 +#include "plbase64.h" 1.13 +#include "plstr.h" 1.14 + 1.15 +#include "nsCocoaUtils.h" 1.16 + 1.17 +#include "mozilla/Preferences.h" 1.18 + 1.19 +using namespace mozilla; 1.20 + 1.21 +#define MAC_OS_X_PAGE_SETUP_PREFNAME "print.macosx.pagesetup-2" 1.22 + 1.23 +NS_IMPL_ISUPPORTS_INHERITED(nsPrintSettingsX, nsPrintSettings, nsPrintSettingsX) 1.24 + 1.25 +nsPrintSettingsX::nsPrintSettingsX() 1.26 +{ 1.27 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK; 1.28 + 1.29 + mPrintInfo = [[NSPrintInfo sharedPrintInfo] copy]; 1.30 + 1.31 + NS_OBJC_END_TRY_ABORT_BLOCK; 1.32 +} 1.33 + 1.34 +nsPrintSettingsX::nsPrintSettingsX(const nsPrintSettingsX& src) 1.35 +{ 1.36 + *this = src; 1.37 +} 1.38 + 1.39 +nsPrintSettingsX::~nsPrintSettingsX() 1.40 +{ 1.41 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK; 1.42 + 1.43 + [mPrintInfo release]; 1.44 + 1.45 + NS_OBJC_END_TRY_ABORT_BLOCK; 1.46 +} 1.47 + 1.48 +nsPrintSettingsX& nsPrintSettingsX::operator=(const nsPrintSettingsX& rhs) 1.49 +{ 1.50 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN; 1.51 + 1.52 + if (this == &rhs) { 1.53 + return *this; 1.54 + } 1.55 + 1.56 + nsPrintSettings::operator=(rhs); 1.57 + 1.58 + [mPrintInfo release]; 1.59 + mPrintInfo = [rhs.mPrintInfo copy]; 1.60 + 1.61 + return *this; 1.62 + 1.63 + NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(*this); 1.64 +} 1.65 + 1.66 +nsresult nsPrintSettingsX::Init() 1.67 +{ 1.68 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; 1.69 + 1.70 + InitUnwriteableMargin(); 1.71 + 1.72 + return NS_OK; 1.73 + 1.74 + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; 1.75 +} 1.76 + 1.77 +// Should be called whenever the page format changes. 1.78 +NS_IMETHODIMP nsPrintSettingsX::InitUnwriteableMargin() 1.79 +{ 1.80 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; 1.81 + 1.82 + PMPaper paper; 1.83 + PMPaperMargins paperMargin; 1.84 + PMPageFormat pageFormat = GetPMPageFormat(); 1.85 + ::PMGetPageFormatPaper(pageFormat, &paper); 1.86 + ::PMPaperGetMargins(paper, &paperMargin); 1.87 + mUnwriteableMargin.top = NS_POINTS_TO_INT_TWIPS(paperMargin.top); 1.88 + mUnwriteableMargin.left = NS_POINTS_TO_INT_TWIPS(paperMargin.left); 1.89 + mUnwriteableMargin.bottom = NS_POINTS_TO_INT_TWIPS(paperMargin.bottom); 1.90 + mUnwriteableMargin.right = NS_POINTS_TO_INT_TWIPS(paperMargin.right); 1.91 + 1.92 + return NS_OK; 1.93 + 1.94 + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; 1.95 +} 1.96 + 1.97 +void 1.98 +nsPrintSettingsX::SetCocoaPrintInfo(NSPrintInfo* aPrintInfo) 1.99 +{ 1.100 + mPrintInfo = aPrintInfo; 1.101 +} 1.102 + 1.103 +NS_IMETHODIMP nsPrintSettingsX::ReadPageFormatFromPrefs() 1.104 +{ 1.105 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; 1.106 + 1.107 + nsAutoCString encodedData; 1.108 + nsresult rv = 1.109 + Preferences::GetCString(MAC_OS_X_PAGE_SETUP_PREFNAME, &encodedData); 1.110 + if (NS_FAILED(rv)) { 1.111 + return rv; 1.112 + } 1.113 + 1.114 + // decode the base64 1.115 + char* decodedData = PL_Base64Decode(encodedData.get(), encodedData.Length(), nullptr); 1.116 + NSData* data = [NSData dataWithBytes:decodedData length:strlen(decodedData)]; 1.117 + if (!data) 1.118 + return NS_ERROR_FAILURE; 1.119 + 1.120 + PMPageFormat newPageFormat; 1.121 + OSStatus status = ::PMPageFormatCreateWithDataRepresentation((CFDataRef)data, &newPageFormat); 1.122 + if (status == noErr) { 1.123 + SetPMPageFormat(newPageFormat); 1.124 + } 1.125 + InitUnwriteableMargin(); 1.126 + 1.127 + return NS_OK; 1.128 + 1.129 + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; 1.130 +} 1.131 + 1.132 +NS_IMETHODIMP nsPrintSettingsX::WritePageFormatToPrefs() 1.133 +{ 1.134 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; 1.135 + 1.136 + PMPageFormat pageFormat = GetPMPageFormat(); 1.137 + if (pageFormat == kPMNoPageFormat) 1.138 + return NS_ERROR_NOT_INITIALIZED; 1.139 + 1.140 + NSData* data = nil; 1.141 + OSStatus err = ::PMPageFormatCreateDataRepresentation(pageFormat, (CFDataRef*)&data, kPMDataFormatXMLDefault); 1.142 + if (err != noErr) 1.143 + return NS_ERROR_FAILURE; 1.144 + 1.145 + nsAutoCString encodedData; 1.146 + encodedData.Adopt(PL_Base64Encode((char*)[data bytes], [data length], nullptr)); 1.147 + if (!encodedData.get()) 1.148 + return NS_ERROR_OUT_OF_MEMORY; 1.149 + 1.150 + return Preferences::SetCString(MAC_OS_X_PAGE_SETUP_PREFNAME, encodedData); 1.151 + 1.152 + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; 1.153 +} 1.154 + 1.155 +nsresult nsPrintSettingsX::_Clone(nsIPrintSettings **_retval) 1.156 +{ 1.157 + NS_ENSURE_ARG_POINTER(_retval); 1.158 + *_retval = nullptr; 1.159 + 1.160 + nsPrintSettingsX *newSettings = new nsPrintSettingsX(*this); 1.161 + if (!newSettings) 1.162 + return NS_ERROR_FAILURE; 1.163 + *_retval = newSettings; 1.164 + NS_ADDREF(*_retval); 1.165 + return NS_OK; 1.166 +} 1.167 + 1.168 +NS_IMETHODIMP nsPrintSettingsX::_Assign(nsIPrintSettings *aPS) 1.169 +{ 1.170 + nsPrintSettingsX *printSettingsX = static_cast<nsPrintSettingsX*>(aPS); 1.171 + if (!printSettingsX) 1.172 + return NS_ERROR_UNEXPECTED; 1.173 + *this = *printSettingsX; 1.174 + return NS_OK; 1.175 +} 1.176 + 1.177 +PMPrintSettings 1.178 +nsPrintSettingsX::GetPMPrintSettings() 1.179 +{ 1.180 + return static_cast<PMPrintSettings>([mPrintInfo PMPrintSettings]); 1.181 +} 1.182 + 1.183 +PMPrintSession 1.184 +nsPrintSettingsX::GetPMPrintSession() 1.185 +{ 1.186 + return static_cast<PMPrintSession>([mPrintInfo PMPrintSession]); 1.187 +} 1.188 + 1.189 +PMPageFormat 1.190 +nsPrintSettingsX::GetPMPageFormat() 1.191 +{ 1.192 + return static_cast<PMPageFormat>([mPrintInfo PMPageFormat]); 1.193 +} 1.194 + 1.195 +void 1.196 +nsPrintSettingsX::SetPMPageFormat(PMPageFormat aPageFormat) 1.197 +{ 1.198 + PMPageFormat oldPageFormat = GetPMPageFormat(); 1.199 + ::PMCopyPageFormat(aPageFormat, oldPageFormat); 1.200 + [mPrintInfo updateFromPMPageFormat]; 1.201 +} 1.202 +