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>
|
|
|
|
|
2014-04-16 01:19:31 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2007-01-11 21:54:29 +00:00
|
|
|
#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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(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;
|
|
|
|
|
2014-03-25 16:32:58 +00:00
|
|
|
nsRefPtr<nsPrintSettingsX> settings(do_QueryObject(aPS));
|
2009-10-09 05:20:37 +00:00
|
|
|
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
|
|
|
|
2013-07-24 11:48:37 +00:00
|
|
|
NS_IMETHODIMP nsDeviceContextSpecX::BeginDocument(const nsAString& aTitle,
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t* aPrintToFileName,
|
2013-07-24 11:48:37 +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;
|
|
|
|
|
2013-07-24 11:48:37 +00:00
|
|
|
if (!aTitle.IsEmpty()) {
|
2013-10-10 16:59:40 +00:00
|
|
|
CFStringRef cfString =
|
|
|
|
::CFStringCreateWithCharacters(NULL, reinterpret_cast<const UniChar*>(aTitle.BeginReading()),
|
|
|
|
aTitle.Length());
|
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 {
|
2014-01-23 18:26:40 +00:00
|
|
|
newSurface = new gfxQuartzSurface(gfxSize((int32_t)width, (int32_t)height), gfxImageFormat::ARGB32, 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
|
|
|
}
|