Fix for bug 36796 - implement page setup on Mac and Mac OS X. r=sdagley, sr=alecf.

git-svn-id: svn://10.0.0.236/trunk@189646 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
sfraser%netscape.com
2006-02-10 22:49:09 +00:00
parent d82b657f6e
commit fdc93b4de6
4 changed files with 399 additions and 48 deletions

View File

@@ -21,6 +21,7 @@
*
* Contributor(s):
* Patrick C. Beard <beard@netscape.com>
* Simon Fraser <sfraser@netscape.com>
*
*
* Alternatively, the contents of this file may be used under the terms of
@@ -38,16 +39,23 @@
* ***** END LICENSE BLOCK ***** */
#include "nsDeviceContextSpecX.h"
#include "prmem.h"
#include "plstr.h"
#include "nsIServiceManager.h"
#include "nsIPrintOptions.h"
/** -------------------------------------------------------
* Construct the nsDeviceContextSpecX
* @update dc 12/02/98
*/
nsDeviceContextSpecX::nsDeviceContextSpecX()
: mPrintSession(0), mPageFormat(kPMNoPageFormat), mPrintSettings(kPMNoPrintSettings),
mSavedPort(0)
: mPrintingContext(0)
, mPageFormat(kPMNoPageFormat)
, mPrintSettings(kPMNoPrintSettings)
, mSavedPort(0)
, mBeganPrinting(PR_FALSE)
{
NS_INIT_REFCNT();
}
@@ -69,35 +77,59 @@ NS_IMPL_ISUPPORTS2(nsDeviceContextSpecX, nsIDeviceContextSpec, nsIPrintingContex
*/
NS_IMETHODIMP nsDeviceContextSpecX::Init(PRBool aQuiet)
{
// create a print session. then a default print settings.
OSStatus status = ::PMCreateSession(&mPrintSession);
if (status != noErr) return NS_ERROR_FAILURE;
status = ::PMCreatePageFormat(&mPageFormat);
if (status != noErr) return NS_ERROR_FAILURE;
status = ::PMSessionDefaultPageFormat(mPrintSession, mPageFormat);
if (status != noErr) return NS_ERROR_FAILURE;
status = ::PMCreatePrintSettings(&mPrintSettings);
if (status != noErr) return NS_ERROR_FAILURE;
status = ::PMSessionDefaultPrintSettings(mPrintSession, mPrintSettings);
if (status != noErr) return NS_ERROR_FAILURE;
nsresult rv;
nsCOMPtr<nsIPrintOptions> printOptionsService = do_GetService("@mozilla.org/gfx/printoptions;1", &rv);
if (NS_FAILED(rv)) return rv;
if (! aQuiet) {
Boolean accepted = false;
status = ::PMSessionPrintDialog(mPrintSession, mPrintSettings, mPageFormat, &accepted);
if (! accepted)
return NS_ERROR_ABORT;
}
// Because page setup can get called at any time, we can't use the session APIs here.
OSStatus status = ::PMBegin();
if (status != noErr) return NS_ERROR_FAILURE;
return NS_OK;
mBeganPrinting = PR_TRUE;
PMPageFormat optionsPageFormat = kPMNoPageFormat;
rv = printOptionsService->GetNativeData(nsIPrintOptions::kNativeDataPrintRecord, (void **)&optionsPageFormat);
if (NS_FAILED(rv)) return rv;
status = ::PMNewPageFormat(&mPageFormat);
if (status != noErr) return NS_ERROR_FAILURE;
if (optionsPageFormat != kPMNoPageFormat)
{
status = ::PMCopyPageFormat(optionsPageFormat, mPageFormat);
::PMDisposePageFormat(optionsPageFormat);
}
else
status = ::PMDefaultPageFormat(mPageFormat);
if (status != noErr) return NS_ERROR_FAILURE;
Boolean validated;
::PMValidatePageFormat(mPageFormat, &validated);
status = ::PMNewPrintSettings(&mPrintSettings);
if (status != noErr) return NS_ERROR_FAILURE;
status = ::PMDefaultPrintSettings(mPrintSettings);
if (status != noErr) return NS_ERROR_FAILURE;
if (! aQuiet)
{
Boolean accepted = false;
status = ::PMPrintDialog(mPrintSettings, mPageFormat, &accepted);
if (! accepted)
return NS_ERROR_ABORT;
if (status != noErr)
return NS_ERROR_FAILURE;
}
return NS_OK;
}
NS_IMETHODIMP nsDeviceContextSpecX::PrintManagerOpen(PRBool* aIsOpen)
{
*aIsOpen = (mPrintSession != 0);
*aIsOpen = mBeganPrinting;
return NS_OK;
}
@@ -108,17 +140,20 @@ NS_IMETHODIMP nsDeviceContextSpecX::PrintManagerOpen(PRBool* aIsOpen)
NS_IMETHODIMP nsDeviceContextSpecX::ClosePrintManager()
{
if (mPrintSettings != kPMNoPrintSettings)
::PMRelease(mPrintSettings);
::PMDisposePrintSettings(mPrintSettings);
if (mPageFormat != kPMNoPageFormat)
::PMRelease(mPageFormat);
if (mPrintSession)
::PMRelease(mPrintSession);
::PMDisposePageFormat(mPageFormat);
if (mBeganPrinting)
::PMEnd();
return NS_OK;
}
NS_IMETHODIMP nsDeviceContextSpecX::BeginDocument()
{
OSStatus status = ::PMSessionBeginDocument(mPrintSession, mPrintSettings, mPageFormat);
OSStatus status = ::PMBeginDocument(mPrintSettings, mPageFormat, &mPrintingContext);
if (status != noErr) return NS_ERROR_FAILURE;
return NS_OK;
@@ -126,20 +161,20 @@ NS_IMETHODIMP nsDeviceContextSpecX::BeginDocument()
NS_IMETHODIMP nsDeviceContextSpecX::EndDocument()
{
PMSessionEndDocument(mPrintSession);
::PMEndDocument(mPrintingContext);
mPrintingContext = 0;
return NS_OK;
}
NS_IMETHODIMP nsDeviceContextSpecX::BeginPage()
{
// see http://devworld.apple.com/techpubs/carbon/graphics/CarbonPrintingManager/Carbon_Printing_Manager/Functions/PMSessionBeginPage.html
OSStatus status = ::PMSessionBeginPage(mPrintSession, mPageFormat, NULL);
OSStatus status = ::PMBeginPage(mPrintingContext, NULL);
if (status != noErr) return NS_ERROR_FAILURE;
::GetPort(&mSavedPort);
GrafPtr printingPort;
status = ::PMSessionGetGraphicsContext(mPrintSession, kPMGraphicsContextQuickdraw,
&(void*)printingPort);
status = ::PMGetGrafPtr(mPrintingContext, &printingPort);
if (status != noErr) return NS_ERROR_FAILURE;
::SetPort(printingPort);
return NS_OK;
@@ -147,8 +182,9 @@ NS_IMETHODIMP nsDeviceContextSpecX::BeginPage()
NS_IMETHODIMP nsDeviceContextSpecX::EndPage()
{
OSStatus status = ::PMSessionEndPage(mPrintSession);
if (mSavedPort) {
OSStatus status = ::PMEndPage(mPrintingContext);
if (mSavedPort)
{
::SetPort(mSavedPort);
mSavedPort = 0;
}
@@ -158,15 +194,10 @@ NS_IMETHODIMP nsDeviceContextSpecX::EndPage()
NS_IMETHODIMP nsDeviceContextSpecX::GetPrinterResolution(double* aResolution)
{
PMPrinter currentPrinter;
OSStatus status = ::PMSessionGetCurrentPrinter(mPrintSession, &currentPrinter);
if (status != noErr) return NS_ERROR_FAILURE;
PMResolution defaultResolution;
status = ::PMPrinterGetPrinterResolution(currentPrinter, kPMDefaultResolution, &defaultResolution);
OSStatus status = ::PMGetPrinterResolution(kPMDefaultResolution, &defaultResolution);
if (status == noErr)
*aResolution = defaultResolution.hRes;
::PMRelease(currentPrinter);
return (status == noErr ? NS_OK : NS_ERROR_FAILURE);
}
@@ -174,7 +205,7 @@ NS_IMETHODIMP nsDeviceContextSpecX::GetPrinterResolution(double* aResolution)
NS_IMETHODIMP nsDeviceContextSpecX::GetPageRect(double* aTop, double* aLeft, double* aBottom, double* aRight)
{
PMRect pageRect;
PMGetAdjustedPageRect(mPageFormat, &pageRect);
::PMGetAdjustedPageRect(mPageFormat, &pageRect);
*aTop = pageRect.top, *aLeft = pageRect.left;
*aBottom = pageRect.bottom, *aRight = pageRect.right;
return NS_OK;

View File

@@ -43,9 +43,11 @@
#include "nsIDeviceContextSpec.h"
#include "nsIPrintingContext.h"
#include "nsDeviceContextMac.h"
#include <PMApplication.h>
class nsDeviceContextSpecX : public nsIDeviceContextSpec, public nsIPrintingContext {
class nsDeviceContextSpecX : public nsIDeviceContextSpec, public nsIPrintingContext
{
public:
/**
* Construct a nsDeviceContextSpecMac, which is an object which contains and manages a mac printrecord
@@ -101,10 +103,12 @@ protected:
virtual ~nsDeviceContextSpecX();
protected:
PMPrintSession mPrintSession; // printing session.
PMPageFormat mPageFormat; // page format.
PMPrintSettings mPrintSettings; // print settings.
CGrafPtr mSavedPort; // saved graphics port.
PMPrintContext mPrintingContext; // printing context (non-session APIs)
PMPageFormat mPageFormat; // page format.
PMPrintSettings mPrintSettings; // print settings.
CGrafPtr mSavedPort; // saved graphics port.
PRBool mBeganPrinting;
};
#endif

View File

@@ -0,0 +1,239 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Simon Fraser <sfraser@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include <PMApplication.h>
#include "nsCOMPtr.h"
#include "nsIServiceManager.h"
#include "nsWatchTask.h"
#include "nsPrintOptionsX.h"
#include "nsIPref.h"
#include "nsGfxUtils.h"
#include "plbase64.h"
#include "prmem.h"
#define MAC_OS_X_PAGE_SETUP_PREFNAME "print.macosx.pagesetup"
/** ---------------------------------------------------
*/
nsPrintOptionsX::nsPrintOptionsX()
: mPageFormat(kPMNoPageFormat)
{
OSStatus status = ::PMNewPageFormat(&mPageFormat);
NS_ASSERTION(status == noErr, "Error creating print settings");
status = ::PMBegin();
NS_ASSERTION(status == noErr, "Error from PMBegin()");
nsresult rv = ReadPageSetupFromPrefs();
if (NS_FAILED(rv))
::PMDefaultPageFormat(mPageFormat);
else
{
Boolean valid;
::PMValidatePageFormat(mPageFormat, &valid);
}
::PMEnd();
}
/** ---------------------------------------------------
*/
nsPrintOptionsX::~nsPrintOptionsX()
{
if (mPageFormat)
::PMDisposePageFormat(mPageFormat);
}
/** ---------------------------------------------------
*/
NS_IMETHODIMP
nsPrintOptionsX::ShowNativeDialog(void)
{
NS_ASSERTION(mPageFormat != kPMNoPageFormat, "No page format");
if (mPageFormat == kPMNoPageFormat)
return NS_ERROR_NOT_INITIALIZED;
OSStatus status = ::PMBegin();
if (status != noErr) return NS_ERROR_FAILURE;
Boolean validated;
::PMValidatePageFormat(mPageFormat, &validated);
Boolean accepted = false;
status = ::PMPageSetupDialog(mPageFormat, &accepted);
::PMEnd();
if (status != noErr)
return NS_ERROR_FAILURE;
if (!accepted)
return NS_ERROR_ABORT;
return NS_OK;
}
NS_IMETHODIMP
nsPrintOptionsX::ReadPrefs()
{
// it doesn't really matter if this fails
nsresult rv = ReadPageSetupFromPrefs();
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to write page setup to prefs");
return nsPrintOptions::ReadPrefs();
}
NS_IMETHODIMP
nsPrintOptionsX::WritePrefs()
{
// it doesn't really matter if this fails
nsresult rv = WritePageSetupToPrefs();
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to save page setup to prefs");
return nsPrintOptions::WritePrefs();
}
/* [noscript] voidPtr GetNativeData (in short aDataType); */
NS_IMETHODIMP
nsPrintOptionsX::GetNativeData(PRInt16 aDataType, void * *_retval)
{
nsresult rv = NS_OK;
NS_ENSURE_ARG_POINTER(_retval);
*_retval = nsnull;
switch (aDataType)
{
case kNativeDataPrintRecord:
// we need to clone and pass out
PMPageFormat pageFormat = kPMNoPageFormat;
OSStatus status = ::PMNewPageFormat(&pageFormat);
if (status != noErr) return NS_ERROR_FAILURE;
status = ::PMCopyPageFormat(mPageFormat, pageFormat);
if (status != noErr) {
::PMDisposePageFormat(pageFormat);
return NS_ERROR_FAILURE;
}
*_retval = pageFormat;
break;
default:
rv = NS_ERROR_FAILURE;
break;
}
return rv;
}
#pragma mark -
nsresult
nsPrintOptionsX::ReadPageSetupFromPrefs()
{
nsresult rv;
nsCOMPtr<nsIPref> prefs = do_GetService(NS_PREF_CONTRACTID, &rv);
if (NS_FAILED(rv))
return rv;
nsXPIDLCString encodedData;
rv = prefs->GetCharPref(MAC_OS_X_PAGE_SETUP_PREFNAME, getter_Copies(encodedData));
if (NS_FAILED(rv))
return rv;
// decode the base64
PRInt32 encodedDataLen = nsCRT::strlen(encodedData.get());
char* decodedData = ::PL_Base64Decode(encodedData.get(), encodedDataLen, nsnull);
if (!decodedData)
return NS_ERROR_FAILURE;
Handle decodedDataHandle = nsnull;
OSErr err = ::PtrToHand(decodedData, &decodedDataHandle, (encodedDataLen * 3) / 4);
PR_Free(decodedData);
if (err != noErr)
return NS_ERROR_OUT_OF_MEMORY;
StHandleOwner handleOwner(decodedDataHandle);
PMPageFormat newPageFormat = kPMNoPageFormat;
OSStatus status = ::PMUnflattenPageFormat(decodedDataHandle, &newPageFormat);
if (status != noErr)
return NS_ERROR_FAILURE;
status = ::PMCopyPageFormat(newPageFormat, mPageFormat);
::PMDisposePageFormat(newPageFormat);
newPageFormat = kPMNoPageFormat;
return (status == noErr) ? NS_OK : NS_ERROR_FAILURE;
}
nsresult
nsPrintOptionsX::WritePageSetupToPrefs()
{
if (mPageFormat == kPMNoPageFormat)
return NS_ERROR_NOT_INITIALIZED;
nsresult rv;
nsCOMPtr<nsIPref> prefs = do_GetService(NS_PREF_CONTRACTID, &rv);
if (NS_FAILED(rv))
return rv;
Handle pageFormatHandle = nsnull;
OSStatus err = ::PMFlattenPageFormat(mPageFormat, &pageFormatHandle);
if (err != noErr)
return NS_ERROR_FAILURE;
StHandleOwner handleOwner(pageFormatHandle);
StHandleLocker handleLocker(pageFormatHandle);
nsXPIDLCString encodedData;
encodedData.Adopt(::PL_Base64Encode(*pageFormatHandle, ::GetHandleSize(pageFormatHandle), nsnull));
if (!encodedData.get())
return NS_ERROR_OUT_OF_MEMORY;
return prefs->SetCharPref(MAC_OS_X_PAGE_SETUP_PREFNAME, encodedData);
}

View File

@@ -0,0 +1,77 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Simon Fraser <sfraser@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsPrintOptionsX_h__
#define nsPrintOptionsX_h__
#include <PMDefinitions.h>
#include "nsPrintOptionsImpl.h"
//*****************************************************************************
//*** nsPrintOptions
//*****************************************************************************
class nsPrintOptionsX : public nsPrintOptions
{
public:
nsPrintOptionsX();
virtual ~nsPrintOptionsX();
NS_IMETHOD ShowNativeDialog(void);
NS_IMETHOD ReadPrefs(void);
NS_IMETHOD WritePrefs(void);
NS_IMETHOD GetNativeData(PRInt16 aDataType, void * *_retval);
protected:
nsresult ReadPageSetupFromPrefs();
nsresult WritePageSetupToPrefs();
protected:
PMPageFormat mPageFormat; // persist this between runs
};
#endif /* nsPrintOptionsX_h__ */