2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07: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-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsDeviceContextSpecX.h"
|
|
|
|
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIPrintOptions.h"
|
2009-10-08 22:20:37 -07:00
|
|
|
#include "nsPrintSettingsX.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "gfxQuartzSurface.h"
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
|
2013-01-15 04:22:03 -08:00
|
|
|
// This must be the last include:
|
|
|
|
#include "nsObjCExceptions.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsDeviceContextSpecX::nsDeviceContextSpecX()
|
|
|
|
: mPrintSession(NULL)
|
|
|
|
, mPageFormat(kPMNoPageFormat)
|
|
|
|
, mPrintSettings(kPMNoPrintSettings)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDeviceContextSpecX::~nsDeviceContextSpecX()
|
|
|
|
{
|
2008-02-19 12:58:01 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mPrintSession)
|
|
|
|
::PMRelease(mPrintSession);
|
2008-02-19 12:58:01 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsDeviceContextSpecX, nsIDeviceContextSpec)
|
2007-07-17 17:07:36 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP nsDeviceContextSpecX::Init(nsIWidget *aWidget,
|
|
|
|
nsIPrintSettings* aPS,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aIsPrintPreview)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-19 12:58:01 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
nsCOMPtr<nsPrintSettingsX> settings(do_QueryInterface(aPS));
|
|
|
|
if (!settings)
|
2007-09-19 15:26:45 -07:00
|
|
|
return NS_ERROR_NO_INTERFACE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
mPrintSession = settings->GetPMPrintSession();
|
2007-09-19 15:26:45 -07:00
|
|
|
::PMRetain(mPrintSession);
|
2009-10-08 22:20:37 -07:00
|
|
|
mPageFormat = settings->GetPMPageFormat();
|
|
|
|
mPrintSettings = settings->GetPMPrintSettings();
|
2007-09-19 15:26:45 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-19 12:58:01 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-09-19 15:26:45 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-24 04:48:37 -07:00
|
|
|
NS_IMETHODIMP nsDeviceContextSpecX::BeginDocument(const nsAString& aTitle,
|
|
|
|
PRUnichar* aPrintToFileName,
|
|
|
|
int32_t aStartPage,
|
|
|
|
int32_t aEndPage)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-19 12:58:01 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2013-07-24 04:48:37 -07:00
|
|
|
if (!aTitle.IsEmpty()) {
|
2013-07-24 05:40:45 -07:00
|
|
|
CFStringRef cfString = ::CFStringCreateWithCharacters(NULL, aTitle.BeginReading(), aTitle.Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
if (cfString) {
|
2008-02-11 10:28:18 -08:00
|
|
|
::PMPrintSettingsSetJobName(mPrintSettings, cfString);
|
2007-03-22 10:30:00 -07: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-17 17:07:36 -07:00
|
|
|
status = ::PMSessionBeginCGDocumentNoDialog(mPrintSession, mPrintSettings, mPageFormat);
|
|
|
|
if (status != noErr)
|
|
|
|
return NS_ERROR_ABORT;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-19 12:58:01 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsDeviceContextSpecX::EndDocument()
|
|
|
|
{
|
2008-02-19 12:58:01 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
::PMSessionEndDocumentNoDialog(mPrintSession);
|
|
|
|
return NS_OK;
|
2008-02-19 12:58:01 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsDeviceContextSpecX::BeginPage()
|
|
|
|
{
|
2008-02-19 12:58:01 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
PMSessionError(mPrintSession);
|
|
|
|
OSStatus status = ::PMSessionBeginPageNoDialog(mPrintSession, mPageFormat, NULL);
|
|
|
|
if (status != noErr) return NS_ERROR_ABORT;
|
|
|
|
return NS_OK;
|
2008-02-19 12:58:01 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsDeviceContextSpecX::EndPage()
|
|
|
|
{
|
2008-02-19 12:58:01 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
OSStatus status = ::PMSessionEndPageNoDialog(mPrintSession);
|
|
|
|
if (status != noErr) return NS_ERROR_ABORT;
|
|
|
|
return NS_OK;
|
2008-02-19 12:58:01 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-03-19 13:51:42 -07:00
|
|
|
void nsDeviceContextSpecX::GetPaperRect(double* aTop, double* aLeft, double* aBottom, double* aRight)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-19 12:58:01 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2008-03-19 13:51:42 -07:00
|
|
|
PMRect paperRect;
|
|
|
|
::PMGetAdjustedPaperRect(mPageFormat, &paperRect);
|
2008-02-19 12:58:01 -08:00
|
|
|
|
2008-03-19 13:51:42 -07:00
|
|
|
*aTop = paperRect.top, *aLeft = paperRect.left;
|
|
|
|
*aBottom = paperRect.bottom, *aRight = paperRect.right;
|
2008-02-19 12:58:01 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2008-02-11 10:28:18 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP nsDeviceContextSpecX::GetSurfaceForPrinter(gfxASurface **surface)
|
|
|
|
{
|
2008-02-19 12:58:01 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
double top, left, bottom, right;
|
2008-03-19 13:51:42 -07:00
|
|
|
GetPaperRect(&top, &left, &bottom, &right);
|
2007-03-22 10:30:00 -07:00
|
|
|
const double width = right - left;
|
|
|
|
const double height = bottom - top;
|
|
|
|
|
|
|
|
CGContextRef context;
|
2007-07-17 17:07:36 -07:00
|
|
|
::PMSessionGetCGGraphicsContext(mPrintSession, &context);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsRefPtr<gfxASurface> newSurface;
|
|
|
|
|
2007-04-03 18:09:15 -07:00
|
|
|
if (context) {
|
2008-02-11 10:28:18 -08:00
|
|
|
// Initially, origin is at bottom-left corner of the paper.
|
2008-03-19 13:51:42 -07:00
|
|
|
// Here, we translate it to top-left corner of the paper.
|
|
|
|
CGContextTranslateCTM(context, 0, height);
|
2007-04-03 18:09:15 -07:00
|
|
|
CGContextScaleCTM(context, 1.0, -1.0);
|
2011-09-30 17:20:33 -07:00
|
|
|
newSurface = new gfxQuartzSurface(context, gfxSize(width, height), true);
|
2007-04-03 18:09:15 -07:00
|
|
|
} else {
|
2013-09-24 13:45:13 -07:00
|
|
|
newSurface = new gfxQuartzSurface(gfxSize((int32_t)width, (int32_t)height), gfxImageFormatARGB32, true);
|
2007-04-03 18:09:15 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!newSurface)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
*surface = newSurface;
|
|
|
|
NS_ADDREF(*surface);
|
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-19 12:58:01 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|