2007-01-27 04:06:59 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-01-11 21:54:29 +00:00
|
|
|
|
|
|
|
#include "nsDeviceContextSpecX.h"
|
|
|
|
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIPrintOptions.h"
|
2009-10-09 05:20:37 +00:00
|
|
|
#include "nsPrintSettingsX.h"
|
2007-01-11 21:54:29 +00:00
|
|
|
|
|
|
|
#include "gfxQuartzSurface.h"
|
2007-01-27 04:06:59 +00:00
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
|
2013-01-15 12:22:03 +00:00
|
|
|
// This must be the last include:
|
|
|
|
#include "nsObjCExceptions.h"
|
|
|
|
|
2007-01-11 21:54:29 +00:00
|
|
|
nsDeviceContextSpecX::nsDeviceContextSpecX()
|
|
|
|
: mPrintSession(NULL)
|
|
|
|
, mPageFormat(kPMNoPageFormat)
|
|
|
|
, mPrintSettings(kPMNoPrintSettings)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDeviceContextSpecX::~nsDeviceContextSpecX()
|
|
|
|
{
|
2008-02-19 20:58:01 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2007-01-11 21:54:29 +00:00
|
|
|
if (mPrintSession)
|
|
|
|
::PMRelease(mPrintSession);
|
2008-02-19 20:58:01 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-01-11 21:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsDeviceContextSpecX, nsIDeviceContextSpec)
|
2007-07-18 00:07:36 +00:00
|
|
|
|
2007-01-11 21:54:29 +00:00
|
|
|
NS_IMETHODIMP nsDeviceContextSpecX::Init(nsIWidget *aWidget,
|
|
|
|
nsIPrintSettings* aPS,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aIsPrintPreview)
|
2007-01-11 21:54:29 +00:00
|
|
|
{
|
2008-02-19 20:58:01 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-10-09 05:20:37 +00:00
|
|
|
nsCOMPtr<nsPrintSettingsX> settings(do_QueryInterface(aPS));
|
|
|
|
if (!settings)
|
2007-09-19 22:26:45 +00:00
|
|
|
return NS_ERROR_NO_INTERFACE;
|
2007-01-27 04:06:59 +00:00
|
|
|
|
2009-10-09 05:20:37 +00:00
|
|
|
mPrintSession = settings->GetPMPrintSession();
|
2007-09-19 22:26:45 +00:00
|
|
|
::PMRetain(mPrintSession);
|
2009-10-09 05:20:37 +00:00
|
|
|
mPageFormat = settings->GetPMPageFormat();
|
|
|
|
mPrintSettings = settings->GetPMPrintSettings();
|
2007-09-19 22:26:45 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-19 20:58:01 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-09-19 22:26:45 +00:00
|
|
|
}
|
2007-01-11 21:54:29 +00:00
|
|
|
|
2007-01-27 04:06:59 +00:00
|
|
|
NS_IMETHODIMP nsDeviceContextSpecX::BeginDocument(PRUnichar* aTitle,
|
|
|
|
PRUnichar* aPrintToFileName,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aStartPage,
|
|
|
|
int32_t aEndPage)
|
2007-01-27 04:06:59 +00:00
|
|
|
{
|
2008-02-19 20:58:01 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-01-27 04:06:59 +00:00
|
|
|
if (aTitle) {
|
2012-05-30 17:43:39 +00:00
|
|
|
CFStringRef cfString = ::CFStringCreateWithCharacters(NULL, aTitle, NS_strlen(aTitle));
|
2007-01-27 04:06:59 +00:00
|
|
|
if (cfString) {
|
2008-02-11 18:28:18 +00:00
|
|
|
::PMPrintSettingsSetJobName(mPrintSettings, cfString);
|
2007-01-27 04:06:59 +00:00
|
|
|
::CFRelease(cfString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OSStatus status;
|
|
|
|
status = ::PMSetFirstPage(mPrintSettings, aStartPage, false);
|
|
|
|
NS_ASSERTION(status == noErr, "PMSetFirstPage failed");
|
|
|
|
status = ::PMSetLastPage(mPrintSettings, aEndPage, false);
|
|
|
|
NS_ASSERTION(status == noErr, "PMSetLastPage failed");
|
|
|
|
|
2007-07-18 00:07:36 +00:00
|
|
|
status = ::PMSessionBeginCGDocumentNoDialog(mPrintSession, mPrintSettings, mPageFormat);
|
|
|
|
if (status != noErr)
|
|
|
|
return NS_ERROR_ABORT;
|
2007-01-27 04:06:59 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-19 20:58:01 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-01-27 04:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsDeviceContextSpecX::EndDocument()
|
|
|
|
{
|
2008-02-19 20:58:01 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-01-27 04:06:59 +00:00
|
|
|
::PMSessionEndDocumentNoDialog(mPrintSession);
|
|
|
|
return NS_OK;
|
2008-02-19 20:58:01 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-01-27 04:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsDeviceContextSpecX::BeginPage()
|
|
|
|
{
|
2008-02-19 20:58:01 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-01-27 04:06:59 +00:00
|
|
|
PMSessionError(mPrintSession);
|
|
|
|
OSStatus status = ::PMSessionBeginPageNoDialog(mPrintSession, mPageFormat, NULL);
|
|
|
|
if (status != noErr) return NS_ERROR_ABORT;
|
|
|
|
return NS_OK;
|
2008-02-19 20:58:01 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-01-27 04:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsDeviceContextSpecX::EndPage()
|
|
|
|
{
|
2008-02-19 20:58:01 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-01-27 04:06:59 +00:00
|
|
|
OSStatus status = ::PMSessionEndPageNoDialog(mPrintSession);
|
|
|
|
if (status != noErr) return NS_ERROR_ABORT;
|
|
|
|
return NS_OK;
|
2008-02-19 20:58:01 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-01-27 04:06:59 +00:00
|
|
|
}
|
|
|
|
|
2008-03-19 20:51:42 +00:00
|
|
|
void nsDeviceContextSpecX::GetPaperRect(double* aTop, double* aLeft, double* aBottom, double* aRight)
|
2007-01-11 21:54:29 +00:00
|
|
|
{
|
2008-02-19 20:58:01 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2008-03-19 20:51:42 +00:00
|
|
|
PMRect paperRect;
|
|
|
|
::PMGetAdjustedPaperRect(mPageFormat, &paperRect);
|
2008-02-19 20:58:01 +00:00
|
|
|
|
2008-03-19 20:51:42 +00:00
|
|
|
*aTop = paperRect.top, *aLeft = paperRect.left;
|
|
|
|
*aBottom = paperRect.bottom, *aRight = paperRect.right;
|
2008-02-19 20:58:01 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2008-02-11 18:28:18 +00:00
|
|
|
}
|
|
|
|
|
2007-01-11 21:54:29 +00:00
|
|
|
NS_IMETHODIMP nsDeviceContextSpecX::GetSurfaceForPrinter(gfxASurface **surface)
|
|
|
|
{
|
2008-02-19 20:58:01 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-01-11 21:54:29 +00:00
|
|
|
double top, left, bottom, right;
|
2008-03-19 20:51:42 +00:00
|
|
|
GetPaperRect(&top, &left, &bottom, &right);
|
2007-01-11 21:54:29 +00:00
|
|
|
const double width = right - left;
|
|
|
|
const double height = bottom - top;
|
|
|
|
|
2007-01-27 04:06:59 +00:00
|
|
|
CGContextRef context;
|
2007-07-18 00:07:36 +00:00
|
|
|
::PMSessionGetCGGraphicsContext(mPrintSession, &context);
|
|
|
|
|
2007-01-27 04:06:59 +00:00
|
|
|
nsRefPtr<gfxASurface> newSurface;
|
|
|
|
|
2007-04-04 01:09:15 +00:00
|
|
|
if (context) {
|
2008-02-11 18:28:18 +00:00
|
|
|
// Initially, origin is at bottom-left corner of the paper.
|
2008-03-19 20:51:42 +00:00
|
|
|
// Here, we translate it to top-left corner of the paper.
|
|
|
|
CGContextTranslateCTM(context, 0, height);
|
2007-04-04 01:09:15 +00:00
|
|
|
CGContextScaleCTM(context, 1.0, -1.0);
|
2011-10-01 00:20:33 +00:00
|
|
|
newSurface = new gfxQuartzSurface(context, gfxSize(width, height), true);
|
2007-04-04 01:09:15 +00:00
|
|
|
} else {
|
2012-08-22 15:56:38 +00:00
|
|
|
newSurface = new gfxQuartzSurface(gfxSize((int32_t)width, (int32_t)height), gfxASurface::ImageFormatARGB32, true);
|
2007-04-04 01:09:15 +00:00
|
|
|
}
|
2007-01-11 21:54:29 +00:00
|
|
|
|
|
|
|
if (!newSurface)
|
2007-01-27 04:06:59 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2007-01-11 21:54:29 +00:00
|
|
|
|
|
|
|
*surface = newSurface;
|
|
|
|
NS_ADDREF(*surface);
|
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-19 20:58:01 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-01-11 21:54:29 +00:00
|
|
|
}
|