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