2006-02-07 04:12:56 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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/. */
|
1998-11-14 05:05:13 +03:00
|
|
|
|
2017-01-06 14:29:11 +03:00
|
|
|
#include "nsDeviceContextSpecWin.h"
|
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2016-06-06 18:10:28 +03:00
|
|
|
#include "mozilla/gfx/PrintTargetPDF.h"
|
2016-06-14 21:26:35 +03:00
|
|
|
#include "mozilla/gfx/PrintTargetWindows.h"
|
2016-12-01 06:48:32 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2017-01-06 14:29:11 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2016-04-19 23:51:25 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2006-02-07 04:13:53 +03:00
|
|
|
#include <winspool.h>
|
2009-01-05 21:17:44 +03:00
|
|
|
|
2006-02-10 23:40:54 +03:00
|
|
|
#include "nsIWidget.h"
|
|
|
|
|
2009-01-18 23:14:14 +03:00
|
|
|
#include "nsTArray.h"
|
2006-02-07 04:13:53 +03:00
|
|
|
#include "nsIPrintSettingsWin.h"
|
2006-02-07 04:13:12 +03:00
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2007-05-06 15:29:41 +04:00
|
|
|
#include "nsStringEnumerator.h"
|
2007-09-03 02:52:38 +04:00
|
|
|
|
2006-02-10 23:40:54 +03:00
|
|
|
#include "gfxWindowsSurface.h"
|
2006-02-07 04:13:12 +03:00
|
|
|
|
2007-03-01 03:02:27 +03:00
|
|
|
#include "nsIFileStreams.h"
|
2006-02-07 04:14:52 +03:00
|
|
|
#include "nsIWindowWatcher.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
2010-05-14 13:24:41 +04:00
|
|
|
#include "mozilla/Services.h"
|
2016-05-05 15:06:53 +03:00
|
|
|
#include "nsWindowsHelpers.h"
|
2006-02-07 04:14:52 +03:00
|
|
|
|
2015-06-18 09:18:00 +03:00
|
|
|
#include "mozilla/gfx/Logging.h"
|
|
|
|
|
2017-06-20 06:34:43 +03:00
|
|
|
#ifdef MOZ_ENABLE_SKIA_PDF
|
|
|
|
#include "mozilla/gfx/PrintTargetSkPDF.h"
|
|
|
|
#include "nsIUUIDGenerator.h"
|
2017-06-08 13:55:42 +03:00
|
|
|
#include "mozilla/widget/PDFViaEMFPrintHelper.h"
|
2017-06-20 06:34:43 +03:00
|
|
|
#endif
|
|
|
|
|
2016-12-01 06:48:32 +03:00
|
|
|
static mozilla::LazyLogModule kWidgetPrintingLogMod("printing-widget");
|
2015-06-04 01:25:57 +03:00
|
|
|
#define PR_PL(_p1) MOZ_LOG(kWidgetPrintingLogMod, mozilla::LogLevel::Debug, _p1)
|
2006-02-07 04:14:03 +03:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
using namespace mozilla;
|
2016-06-03 13:27:31 +03:00
|
|
|
using namespace mozilla::gfx;
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2017-06-08 13:55:42 +03:00
|
|
|
#ifdef MOZ_ENABLE_SKIA_PDF
|
|
|
|
using namespace mozilla::widget;
|
|
|
|
#endif
|
|
|
|
|
2016-01-22 19:05:19 +03:00
|
|
|
static const wchar_t kDriverName[] = L"WINSPOOL";
|
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// The printer data is shared between the PrinterEnumerator and the nsDeviceContextSpecWin
|
|
|
|
// The PrinterEnumerator creates the printer info
|
|
|
|
// but the nsDeviceContextSpecWin cleans it up
|
|
|
|
// If it gets created (via the Page Setup Dialog) but the user never prints anything
|
|
|
|
// then it will never be delete, so this class takes care of that.
|
|
|
|
class GlobalPrinters {
|
|
|
|
public:
|
|
|
|
static GlobalPrinters* GetInstance() { return &mGlobalPrinters; }
|
|
|
|
~GlobalPrinters() { FreeGlobalPrinters(); }
|
|
|
|
|
|
|
|
void FreeGlobalPrinters();
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
bool PrintersAreAllocated() { return mPrinters != nullptr; }
|
2012-08-22 19:56:38 +04:00
|
|
|
LPWSTR GetItemFromList(int32_t aInx) { return mPrinters?mPrinters->ElementAt(aInx):nullptr; }
|
2006-02-07 04:13:21 +03:00
|
|
|
nsresult EnumeratePrinterList();
|
2009-11-13 01:18:32 +03:00
|
|
|
void GetDefaultPrinterName(nsString& aDefaultPrinterName);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t GetNumPrinters() { return mPrinters?mPrinters->Length():0; }
|
2006-02-07 04:13:12 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
GlobalPrinters() {}
|
2006-02-07 04:13:21 +03:00
|
|
|
nsresult EnumerateNativePrinters();
|
2006-02-07 04:13:12 +03:00
|
|
|
void ReallocatePrinters();
|
|
|
|
|
2009-02-23 04:05:24 +03:00
|
|
|
static GlobalPrinters mGlobalPrinters;
|
2009-11-13 01:18:32 +03:00
|
|
|
static nsTArray<LPWSTR>* mPrinters;
|
2006-02-07 04:13:12 +03:00
|
|
|
};
|
|
|
|
//---------------
|
|
|
|
// static members
|
2009-02-23 04:05:24 +03:00
|
|
|
GlobalPrinters GlobalPrinters::mGlobalPrinters;
|
2012-07-30 18:20:58 +04:00
|
|
|
nsTArray<LPWSTR>* GlobalPrinters::mPrinters = nullptr;
|
2006-02-07 04:13:12 +03:00
|
|
|
|
2016-01-22 19:05:19 +03:00
|
|
|
struct AutoFreeGlobalPrinters
|
|
|
|
{
|
|
|
|
~AutoFreeGlobalPrinters() {
|
|
|
|
GlobalPrinters::GetInstance()->FreeGlobalPrinters();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
2006-02-07 04:13:53 +03:00
|
|
|
nsDeviceContextSpecWin::nsDeviceContextSpecWin()
|
1998-11-14 05:05:13 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mDriverName = nullptr;
|
|
|
|
mDeviceName = nullptr;
|
2013-10-08 22:48:02 +04:00
|
|
|
mDevMode = nullptr;
|
2017-06-20 06:34:43 +03:00
|
|
|
#ifdef MOZ_ENABLE_SKIA_PDF
|
2017-06-08 13:55:42 +03:00
|
|
|
mPrintViaSkPDF = false;
|
|
|
|
mDC = NULL;
|
|
|
|
mPDFPageCount = 0;
|
|
|
|
mPDFCurrentPageNum = 0;
|
|
|
|
mPrintViaPDFInProgress = false;
|
2017-06-20 06:34:43 +03:00
|
|
|
#endif
|
1998-11-14 05:05:13 +03:00
|
|
|
}
|
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2007-09-03 02:52:38 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsDeviceContextSpecWin, nsIDeviceContextSpec)
|
2007-09-03 02:52:38 +04:00
|
|
|
|
2006-02-07 04:13:32 +03:00
|
|
|
nsDeviceContextSpecWin::~nsDeviceContextSpecWin()
|
1998-11-14 05:05:13 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
SetDeviceName(nullptr);
|
|
|
|
SetDriverName(nullptr);
|
2013-10-08 22:48:02 +04:00
|
|
|
SetDevMode(nullptr);
|
2006-02-07 04:13:12 +03:00
|
|
|
|
2006-02-07 04:13:54 +03:00
|
|
|
nsCOMPtr<nsIPrintSettingsWin> psWin(do_QueryInterface(mPrintSettings));
|
|
|
|
if (psWin) {
|
2012-07-30 18:20:58 +04:00
|
|
|
psWin->SetDeviceName(nullptr);
|
|
|
|
psWin->SetDriverName(nullptr);
|
2013-10-08 22:48:02 +04:00
|
|
|
psWin->SetDevMode(nullptr);
|
2006-02-07 04:13:54 +03:00
|
|
|
}
|
|
|
|
|
2017-06-08 13:55:42 +03:00
|
|
|
#ifdef MOZ_ENABLE_SKIA_PDF
|
|
|
|
if (mPrintViaSkPDF ) {
|
|
|
|
CleanupPrintViaPDF();
|
|
|
|
}
|
|
|
|
#endif
|
2006-02-07 04:13:12 +03:00
|
|
|
// Free them, we won't need them for a while
|
|
|
|
GlobalPrinters::GetInstance()->FreeGlobalPrinters();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-07 04:13:53 +03:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
// helper
|
2014-01-04 19:02:17 +04:00
|
|
|
static char16_t * GetDefaultPrinterNameFromGlobalPrinters()
|
2006-02-07 04:13:50 +03:00
|
|
|
{
|
2009-11-13 01:18:32 +03:00
|
|
|
nsAutoString printerName;
|
|
|
|
GlobalPrinters::GetInstance()->GetDefaultPrinterName(printerName);
|
|
|
|
return ToNewUnicode(printerName);
|
2006-02-07 04:13:53 +03:00
|
|
|
}
|
1998-11-14 05:05:13 +03:00
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
2013-03-09 06:45:14 +04:00
|
|
|
NS_IMETHODIMP nsDeviceContextSpecWin::Init(nsIWidget* aWidget,
|
2006-02-07 04:13:32 +03:00
|
|
|
nsIPrintSettings* aPrintSettings,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aIsPrintPreview)
|
2006-02-07 04:13:12 +03:00
|
|
|
{
|
|
|
|
mPrintSettings = aPrintSettings;
|
|
|
|
|
2013-03-09 06:45:14 +04:00
|
|
|
nsresult rv = NS_ERROR_GFX_PRINTER_NO_PRINTER_AVAILABLE;
|
2006-02-07 04:13:53 +03:00
|
|
|
if (aPrintSettings) {
|
2017-06-20 06:34:43 +03:00
|
|
|
#ifdef MOZ_ENABLE_SKIA_PDF
|
2017-07-31 07:23:50 +03:00
|
|
|
nsAutoString printViaPdf;
|
|
|
|
Preferences::GetString("print.print_via_pdf_encoder", printViaPdf);
|
|
|
|
if (printViaPdf.EqualsLiteral("skia-pdf")) {
|
2017-06-20 06:34:43 +03:00
|
|
|
mPrintViaSkPDF = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-06 14:29:11 +03:00
|
|
|
// If we're in the child and printing via the parent or we're printing to
|
|
|
|
// PDF we only need information from the print settings.
|
|
|
|
mPrintSettings->GetOutputFormat(&mOutputFormat);
|
|
|
|
if ((XRE_IsContentProcess() &&
|
|
|
|
Preferences::GetBool("print.print_via_parent")) ||
|
|
|
|
mOutputFormat == nsIPrintSettings::kOutputFormatPDF) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-02-07 04:13:53 +03:00
|
|
|
nsCOMPtr<nsIPrintSettingsWin> psWin(do_QueryInterface(aPrintSettings));
|
|
|
|
if (psWin) {
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* deviceName;
|
|
|
|
char16_t* driverName;
|
2006-02-07 04:13:53 +03:00
|
|
|
psWin->GetDeviceName(&deviceName); // creates new memory (makes a copy)
|
|
|
|
psWin->GetDriverName(&driverName); // creates new memory (makes a copy)
|
|
|
|
|
2008-10-22 04:54:26 +04:00
|
|
|
LPDEVMODEW devMode;
|
2006-02-07 04:13:53 +03:00
|
|
|
psWin->GetDevMode(&devMode); // creates new memory (makes a copy)
|
|
|
|
|
|
|
|
if (deviceName && driverName && devMode) {
|
|
|
|
// Scaling is special, it is one of the few
|
|
|
|
// devMode items that we control in layout
|
|
|
|
if (devMode->dmFields & DM_SCALE) {
|
|
|
|
double scale = double(devMode->dmScale) / 100.0f;
|
|
|
|
if (scale != 1.0) {
|
|
|
|
aPrintSettings->SetScaling(scale);
|
|
|
|
devMode->dmScale = 100;
|
|
|
|
}
|
|
|
|
}
|
2006-02-07 04:13:50 +03:00
|
|
|
|
2006-02-07 04:13:53 +03:00
|
|
|
SetDeviceName(deviceName);
|
|
|
|
SetDriverName(driverName);
|
|
|
|
SetDevMode(devMode);
|
|
|
|
|
|
|
|
// clean up
|
2013-08-22 14:31:48 +04:00
|
|
|
free(deviceName);
|
|
|
|
free(driverName);
|
2006-02-07 04:13:53 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
} else {
|
2006-02-07 04:14:03 +03:00
|
|
|
PR_PL(("***** nsDeviceContextSpecWin::Init - deviceName/driverName/devMode was NULL!\n"));
|
2013-08-22 14:31:48 +04:00
|
|
|
if (deviceName) free(deviceName);
|
|
|
|
if (driverName) free(driverName);
|
2006-02-07 04:14:13 +03:00
|
|
|
if (devMode) ::HeapFree(::GetProcessHeap(), 0, devMode);
|
2006-02-07 04:13:53 +03:00
|
|
|
}
|
2006-02-07 04:13:21 +03:00
|
|
|
}
|
2006-02-07 04:13:59 +03:00
|
|
|
} else {
|
2006-02-07 04:14:03 +03:00
|
|
|
PR_PL(("***** nsDeviceContextSpecWin::Init - aPrintSettingswas NULL!\n"));
|
2006-02-07 04:13:59 +03:00
|
|
|
}
|
2006-02-07 04:13:53 +03:00
|
|
|
|
2016-03-19 14:01:30 +03:00
|
|
|
// Get the Printer Name to be used and output format.
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t * printerName = nullptr;
|
2009-12-11 07:02:13 +03:00
|
|
|
if (mPrintSettings) {
|
|
|
|
mPrintSettings->GetPrinterName(&printerName);
|
|
|
|
}
|
2006-02-07 04:13:53 +03:00
|
|
|
|
|
|
|
// If there is no name then use the default printer
|
|
|
|
if (!printerName || (printerName && !*printerName)) {
|
|
|
|
printerName = GetDefaultPrinterNameFromGlobalPrinters();
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
|
|
|
|
2006-02-07 04:13:53 +03:00
|
|
|
NS_ASSERTION(printerName, "We have to have a printer name");
|
2007-12-04 18:56:17 +03:00
|
|
|
if (!printerName || !*printerName) return rv;
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2006-02-07 04:13:53 +03:00
|
|
|
return GetDataFromPrinter(printerName, mPrintSettings);
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------
|
|
|
|
// Helper Function - Free and reallocate the string
|
2013-12-03 19:12:57 +04:00
|
|
|
static void CleanAndCopyString(wchar_t*& aStr, const wchar_t* aNewStr)
|
2006-02-07 04:13:12 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
if (aStr != nullptr) {
|
|
|
|
if (aNewStr != nullptr && wcslen(aStr) > wcslen(aNewStr)) { // reuse it if we can
|
2008-10-22 04:54:26 +04:00
|
|
|
wcscpy(aStr, aNewStr);
|
2006-02-07 04:13:12 +03:00
|
|
|
return;
|
|
|
|
} else {
|
2017-07-01 05:05:41 +03:00
|
|
|
free(aStr);
|
2012-07-30 18:20:58 +04:00
|
|
|
aStr = nullptr;
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (nullptr != aNewStr) {
|
2017-07-01 05:05:41 +03:00
|
|
|
aStr = (wchar_t*) malloc(sizeof(wchar_t) * (wcslen(aNewStr) + 1));
|
2008-10-22 04:54:26 +04:00
|
|
|
wcscpy(aStr, aNewStr);
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 13:27:31 +03:00
|
|
|
already_AddRefed<PrintTarget> nsDeviceContextSpecWin::MakePrintTarget()
|
2006-02-10 23:40:54 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mDevMode, "DevMode can't be NULL here");
|
|
|
|
|
2017-06-20 06:34:43 +03:00
|
|
|
#ifdef MOZ_ENABLE_SKIA_PDF
|
|
|
|
if (mPrintViaSkPDF) {
|
|
|
|
double width, height;
|
|
|
|
mPrintSettings->GetEffectivePageSize(&width, &height);
|
|
|
|
if (width <= 0 || height <= 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert twips to points
|
|
|
|
width /= TWIPS_PER_POINT_FLOAT;
|
|
|
|
height /= TWIPS_PER_POINT_FLOAT;
|
|
|
|
IntSize size = IntSize::Truncate(width, height);
|
|
|
|
|
|
|
|
if (mOutputFormat == nsIPrintSettings::kOutputFormatPDF) {
|
|
|
|
nsXPIDLString filename;
|
|
|
|
mPrintSettings->GetToFileName(getter_Copies(filename));
|
|
|
|
|
|
|
|
nsAutoCString printFile(NS_ConvertUTF16toUTF8(filename).get());
|
|
|
|
auto skStream = MakeUnique<SkFILEWStream>(printFile.get());
|
|
|
|
return PrintTargetSkPDF::CreateOrNull(Move(skStream), size);
|
|
|
|
}
|
2017-06-08 13:55:42 +03:00
|
|
|
|
|
|
|
if (mDevMode) {
|
|
|
|
// When printing to a printer via Skia PDF we open a temporary file that
|
|
|
|
// we draw the print output into as PDF output, then once we reach
|
|
|
|
// EndDcoument we'll convert that PDF file to EMF page by page to print
|
|
|
|
// each page. Here we create the temporary file and wrap it in a
|
|
|
|
// PrintTargetSkPDF that we return.
|
|
|
|
nsresult rv =
|
|
|
|
NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(mPDFTempFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIUUIDGenerator> uuidGenerator =
|
|
|
|
do_GetService("@mozilla.org/uuid-generator;1", &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsID uuid;
|
|
|
|
rv = uuidGenerator->GenerateUUIDInPlace(&uuid);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
char uuidChars[NSID_LENGTH];
|
|
|
|
uuid.ToProvidedString(uuidChars);
|
|
|
|
|
|
|
|
nsAutoCString printFile("tmp-printing");
|
|
|
|
printFile.Append(nsPrintfCString("%s.pdf", uuidChars));
|
|
|
|
rv = mPDFTempFile->AppendNative(printFile);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
nsAutoCString filePath;
|
|
|
|
mPDFTempFile->GetNativePath(filePath);
|
|
|
|
auto skStream = MakeUnique<SkFILEWStream>(filePath.get());
|
|
|
|
return PrintTargetSkPDF::CreateOrNull(Move(skStream), size);
|
|
|
|
}
|
2017-06-20 06:34:43 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-03-19 14:01:30 +03:00
|
|
|
if (mOutputFormat == nsIPrintSettings::kOutputFormatPDF) {
|
2007-03-01 03:02:27 +03:00
|
|
|
nsXPIDLString filename;
|
|
|
|
mPrintSettings->GetToFileName(getter_Copies(filename));
|
|
|
|
|
2007-07-17 03:16:48 +04:00
|
|
|
double width, height;
|
|
|
|
mPrintSettings->GetEffectivePageSize(&width, &height);
|
2015-06-18 09:18:00 +03:00
|
|
|
if (width <= 0 || height <= 0) {
|
2016-06-03 13:27:31 +03:00
|
|
|
return nullptr;
|
2015-06-18 09:18:00 +03:00
|
|
|
}
|
|
|
|
|
2007-03-01 03:02:27 +03:00
|
|
|
// convert twips to points
|
2008-03-19 23:51:42 +03:00
|
|
|
width /= TWIPS_PER_POINT_FLOAT;
|
|
|
|
height /= TWIPS_PER_POINT_FLOAT;
|
2006-02-10 23:40:54 +03:00
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> file = do_CreateInstance("@mozilla.org/file/local;1");
|
2007-03-01 03:02:27 +03:00
|
|
|
nsresult rv = file->InitWithPath(filename);
|
2016-06-03 13:27:31 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2007-03-01 03:02:27 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFileOutputStream> stream = do_CreateInstance("@mozilla.org/network/file-output-stream;1");
|
|
|
|
rv = stream->Init(file, -1, -1, 0);
|
2016-06-03 13:27:31 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2007-03-01 03:02:27 +03:00
|
|
|
|
2016-07-26 17:48:30 +03:00
|
|
|
return PrintTargetPDF::CreateOrNull(stream, IntSize::Truncate(width, height));
|
2016-06-06 18:10:28 +03:00
|
|
|
}
|
2007-03-01 03:02:27 +03:00
|
|
|
|
2016-06-06 18:10:28 +03:00
|
|
|
if (mDevMode) {
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(mDriverName, "No driver!");
|
2016-06-06 18:10:28 +03:00
|
|
|
HDC dc = ::CreateDCW(mDriverName, mDeviceName, nullptr, mDevMode);
|
|
|
|
if (!dc) {
|
|
|
|
gfxCriticalError(gfxCriticalError::DefaultOptions(false))
|
|
|
|
<< "Failed to create device context in GetSurfaceForPrinter";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-14 21:26:35 +03:00
|
|
|
// The PrintTargetWindows takes over ownership of this DC
|
|
|
|
return PrintTargetWindows::CreateOrNull(dc);
|
2016-06-03 13:27:31 +03:00
|
|
|
}
|
|
|
|
|
2016-06-06 18:10:28 +03:00
|
|
|
return nullptr;
|
2016-01-05 13:08:57 +03:00
|
|
|
}
|
2015-12-21 23:33:13 +03:00
|
|
|
|
2016-03-19 14:01:30 +03:00
|
|
|
float
|
|
|
|
nsDeviceContextSpecWin::GetDPI()
|
|
|
|
{
|
|
|
|
// To match the previous printing code we need to return 72 when printing to
|
|
|
|
// PDF and 144 when printing to a Windows surface.
|
2017-06-08 13:55:42 +03:00
|
|
|
#ifdef MOZ_ENABLE_SKIA_PDF
|
|
|
|
if (mPrintViaSkPDF) {
|
|
|
|
return 72.0f;
|
|
|
|
}
|
|
|
|
#endif
|
2016-03-19 14:01:30 +03:00
|
|
|
return mOutputFormat == nsIPrintSettings::kOutputFormatPDF ? 72.0f : 144.0f;
|
|
|
|
}
|
|
|
|
|
2016-01-05 13:08:57 +03:00
|
|
|
float
|
|
|
|
nsDeviceContextSpecWin::GetPrintingScale()
|
|
|
|
{
|
2016-01-22 19:05:19 +03:00
|
|
|
MOZ_ASSERT(mPrintSettings);
|
2017-06-08 13:55:42 +03:00
|
|
|
#ifdef MOZ_ENABLE_SKIA_PDF
|
|
|
|
if (mPrintViaSkPDF) {
|
|
|
|
return 1.0f; // PDF is vector based, so we don't need a scale
|
|
|
|
}
|
|
|
|
#endif
|
2016-03-19 14:01:30 +03:00
|
|
|
// To match the previous printing code there is no scaling for PDF.
|
|
|
|
if (mOutputFormat == nsIPrintSettings::kOutputFormatPDF) {
|
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The print settings will have the resolution stored from the real device.
|
2016-01-22 19:05:19 +03:00
|
|
|
int32_t resolution;
|
|
|
|
mPrintSettings->GetResolution(&resolution);
|
|
|
|
return float(resolution) / GetDPI();
|
2006-02-10 23:40:54 +03:00
|
|
|
}
|
2006-02-11 03:03:51 +03:00
|
|
|
|
2017-06-08 13:55:42 +03:00
|
|
|
#ifdef MOZ_ENABLE_SKIA_PDF
|
|
|
|
void
|
|
|
|
nsDeviceContextSpecWin::CleanupPrintViaPDF()
|
|
|
|
{
|
|
|
|
if (mPDFPrintHelper) {
|
|
|
|
mPDFPrintHelper->CloseDocument();
|
|
|
|
mPDFPrintHelper = nullptr;
|
|
|
|
mPDFPageCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPDFTempFile) {
|
|
|
|
mPDFTempFile->Remove(/* aRecursive */ false);
|
|
|
|
mPDFTempFile = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDC != NULL) {
|
|
|
|
if (mPrintViaPDFInProgress) {
|
|
|
|
::EndDoc(mDC);
|
|
|
|
mPrintViaPDFInProgress = false;
|
|
|
|
}
|
|
|
|
::DeleteDC(mDC);
|
|
|
|
mDC = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDeviceContextSpecWin::FinishPrintViaPDF()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mDC != NULL);
|
|
|
|
MOZ_ASSERT(mPDFPrintHelper);
|
|
|
|
MOZ_ASSERT(mPDFTempFile);
|
|
|
|
MOZ_ASSERT(mPrintViaPDFInProgress);
|
|
|
|
|
|
|
|
bool isPrinted = false;
|
|
|
|
bool endPageSuccess = false;
|
|
|
|
if (::StartPage(mDC) > 0) {
|
|
|
|
isPrinted = mPDFPrintHelper->DrawPage(mDC, mPDFCurrentPageNum++,
|
|
|
|
::GetDeviceCaps(mDC, HORZRES),
|
|
|
|
::GetDeviceCaps(mDC, VERTRES));
|
|
|
|
if (::EndPage(mDC) > 0) {
|
|
|
|
endPageSuccess = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPDFCurrentPageNum < mPDFPageCount && isPrinted && endPageSuccess) {
|
|
|
|
nsresult rv = NS_DispatchToCurrentThread(NewRunnableMethod(
|
|
|
|
"nsDeviceContextSpecWin::PrintPDFOnThread",
|
|
|
|
this,
|
|
|
|
&nsDeviceContextSpecWin::FinishPrintViaPDF));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CleanupPrintViaPDF();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDeviceContextSpecWin::BeginDocument(const nsAString& aTitle,
|
|
|
|
const nsAString& aPrintToFileName,
|
|
|
|
int32_t aStartPage,
|
|
|
|
int32_t aEndPage)
|
|
|
|
{
|
|
|
|
#ifdef MOZ_ENABLE_SKIA_PDF
|
|
|
|
if (mPrintViaSkPDF && (mOutputFormat != nsIPrintSettings::kOutputFormatPDF)) {
|
|
|
|
// Here we create mDC which we'll draw each page from our temporary PDF file
|
|
|
|
// to once we reach EndDocument. The only reason we create it here rather
|
|
|
|
// than in EndDocument is so that we don't need to store aTitle and
|
|
|
|
// aPrintToFileName as member data.
|
|
|
|
NS_WARNING_ASSERTION(mDriverName, "No driver!");
|
|
|
|
mDC = ::CreateDCW(mDriverName, mDeviceName, nullptr, mDevMode);
|
|
|
|
if (mDC == NULL) {
|
|
|
|
gfxCriticalError(gfxCriticalError::DefaultOptions(false))
|
|
|
|
<< "Failed to create device context in GetSurfaceForPrinter";
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t DOC_TITLE_LENGTH = MAX_PATH - 1;
|
|
|
|
nsString title(aTitle);
|
|
|
|
nsString printToFileName(aPrintToFileName);
|
|
|
|
if (title.Length() > DOC_TITLE_LENGTH) {
|
|
|
|
title.SetLength(DOC_TITLE_LENGTH - 3);
|
|
|
|
title.AppendLiteral("...");
|
|
|
|
}
|
|
|
|
|
|
|
|
DOCINFOW di;
|
|
|
|
di.cbSize = sizeof(di);
|
|
|
|
di.lpszDocName = title.Length() > 0 ? title.get() : L"Mozilla Document";
|
|
|
|
di.lpszOutput = printToFileName.Length() > 0 ?
|
|
|
|
printToFileName.get() : nullptr;
|
|
|
|
di.lpszDatatype = nullptr;
|
|
|
|
di.fwType = 0;
|
|
|
|
|
|
|
|
if (::StartDocW(mDC, &di) <= 0) {
|
|
|
|
// Defer calling CleanupPrintViaPDF() in destructor because PDF temp file
|
|
|
|
// is not ready yet.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPrintViaPDFInProgress = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDeviceContextSpecWin::EndDocument()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
#ifdef MOZ_ENABLE_SKIA_PDF
|
|
|
|
if (mPrintViaSkPDF &&
|
|
|
|
mOutputFormat != nsIPrintSettings::kOutputFormatPDF &&
|
|
|
|
mPrintViaPDFInProgress) {
|
|
|
|
|
|
|
|
mPDFPrintHelper = MakeUnique<PDFViaEMFPrintHelper>();
|
|
|
|
rv = mPDFPrintHelper->OpenDocument(mPDFTempFile);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mPDFPageCount = mPDFPrintHelper->GetPageCount();
|
|
|
|
if (mPDFPageCount <= 0) {
|
|
|
|
CleanupPrintViaPDF();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mPDFCurrentPageNum = 0;
|
|
|
|
|
|
|
|
rv = NS_DispatchToCurrentThread(NewRunnableMethod(
|
|
|
|
"nsDeviceContextSpecWin::PrintPDFOnThread",
|
|
|
|
this,
|
|
|
|
&nsDeviceContextSpecWin::FinishPrintViaPDF));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
CleanupPrintViaPDF();
|
|
|
|
NS_WARNING("Failed to dispatch to the current thread!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
2013-12-03 19:12:57 +04:00
|
|
|
void nsDeviceContextSpecWin::SetDeviceName(char16ptr_t aDeviceName)
|
2006-02-07 04:13:12 +03:00
|
|
|
{
|
|
|
|
CleanAndCopyString(mDeviceName, aDeviceName);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2013-12-03 19:12:57 +04:00
|
|
|
void nsDeviceContextSpecWin::SetDriverName(char16ptr_t aDriverName)
|
2006-02-07 04:13:12 +03:00
|
|
|
{
|
|
|
|
CleanAndCopyString(mDriverName, aDriverName);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2008-10-22 04:54:26 +04:00
|
|
|
void nsDeviceContextSpecWin::SetDevMode(LPDEVMODEW aDevMode)
|
2006-02-07 04:13:12 +03:00
|
|
|
{
|
2006-02-07 04:14:13 +03:00
|
|
|
if (mDevMode) {
|
|
|
|
::HeapFree(::GetProcessHeap(), 0, mDevMode);
|
|
|
|
}
|
2006-02-07 04:13:29 +03:00
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
mDevMode = aDevMode;
|
|
|
|
}
|
|
|
|
|
2006-02-07 04:13:53 +03:00
|
|
|
//------------------------------------------------------------------
|
2016-07-22 11:56:13 +03:00
|
|
|
void
|
2008-10-22 04:54:26 +04:00
|
|
|
nsDeviceContextSpecWin::GetDevMode(LPDEVMODEW &aDevMode)
|
2006-02-07 04:13:51 +03:00
|
|
|
{
|
2006-02-07 04:14:13 +03:00
|
|
|
aDevMode = mDevMode;
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
|
|
|
|
2016-07-22 11:56:13 +03:00
|
|
|
#define DISPLAY_LAST_ERROR
|
2006-02-07 04:13:59 +03:00
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Setup the object's data member with the selected printer's data
|
2006-02-07 04:13:29 +03:00
|
|
|
nsresult
|
2013-12-03 19:12:57 +04:00
|
|
|
nsDeviceContextSpecWin::GetDataFromPrinter(char16ptr_t aName, nsIPrintSettings* aPS)
|
1998-11-14 05:05:13 +03:00
|
|
|
{
|
2006-02-07 04:13:29 +03:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
if (!GlobalPrinters::GetInstance()->PrintersAreAllocated()) {
|
2006-02-07 04:13:37 +03:00
|
|
|
rv = GlobalPrinters::GetInstance()->EnumeratePrinterList();
|
2006-02-07 04:13:59 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2006-02-07 04:14:03 +03:00
|
|
|
PR_PL(("***** nsDeviceContextSpecWin::GetDataFromPrinter - Couldn't enumerate printers!\n"));
|
2006-02-07 04:13:59 +03:00
|
|
|
DISPLAY_LAST_ERROR
|
|
|
|
}
|
2006-02-07 04:13:37 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
|
|
|
|
2016-08-22 08:45:49 +03:00
|
|
|
nsHPRINTER hPrinter = nullptr;
|
2013-12-03 19:12:57 +04:00
|
|
|
wchar_t *name = (wchar_t*)aName; // Windows APIs use non-const name argument
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2013-12-03 19:12:57 +04:00
|
|
|
BOOL status = ::OpenPrinterW(name, &hPrinter, nullptr);
|
2006-02-07 04:13:21 +03:00
|
|
|
if (status) {
|
2016-08-22 08:45:49 +03:00
|
|
|
nsAutoPrinter autoPrinter(hPrinter);
|
2006-02-07 04:13:21 +03:00
|
|
|
|
2008-10-22 04:54:26 +04:00
|
|
|
LPDEVMODEW pDevMode;
|
2006-02-07 04:13:21 +03:00
|
|
|
|
|
|
|
// Allocate a buffer of the correct size.
|
2016-08-22 08:45:49 +03:00
|
|
|
LONG needed = ::DocumentPropertiesW(nullptr, hPrinter, name, nullptr,
|
|
|
|
nullptr, 0);
|
|
|
|
if (needed < 0) {
|
|
|
|
PR_PL(("**** nsDeviceContextSpecWin::GetDataFromPrinter - Couldn't get "
|
|
|
|
"size of DEVMODE using DocumentPropertiesW(pDeviceName = \"%s\"). "
|
|
|
|
"GetLastEror() = %08x\n",
|
|
|
|
aName ? NS_ConvertUTF16toUTF8(aName).get() : "", GetLastError()));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2006-02-07 04:13:12 +03:00
|
|
|
|
2016-08-22 08:45:49 +03:00
|
|
|
pDevMode = (LPDEVMODEW)::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY,
|
|
|
|
needed);
|
2006-02-07 04:14:13 +03:00
|
|
|
if (!pDevMode) return NS_ERROR_FAILURE;
|
2006-02-07 04:13:12 +03:00
|
|
|
|
2006-02-07 04:13:21 +03:00
|
|
|
// Get the default DevMode for the printer and modify it for our needs.
|
2016-08-22 08:45:49 +03:00
|
|
|
LONG ret = ::DocumentPropertiesW(nullptr, hPrinter, name,
|
|
|
|
pDevMode, nullptr, DM_OUT_BUFFER);
|
2006-02-07 04:13:21 +03:00
|
|
|
|
2016-08-22 08:45:49 +03:00
|
|
|
if (ret == IDOK && aPS) {
|
2016-01-12 20:40:07 +03:00
|
|
|
nsCOMPtr<nsIPrintSettingsWin> psWin = do_QueryInterface(aPS);
|
|
|
|
MOZ_ASSERT(psWin);
|
|
|
|
psWin->CopyToNative(pDevMode);
|
2006-02-07 04:14:18 +03:00
|
|
|
// Sets back the changes we made to the DevMode into the Printer Driver
|
2016-08-22 08:45:49 +03:00
|
|
|
ret = ::DocumentPropertiesW(nullptr, hPrinter, name,
|
|
|
|
pDevMode, pDevMode,
|
|
|
|
DM_IN_BUFFER | DM_OUT_BUFFER);
|
2016-05-05 15:06:53 +03:00
|
|
|
|
|
|
|
// We need to copy the final DEVMODE settings back to our print settings,
|
|
|
|
// because they may have been set from invalid prefs.
|
2016-08-22 08:45:49 +03:00
|
|
|
if (ret == IDOK) {
|
2016-05-05 15:06:53 +03:00
|
|
|
// We need to get information from the device as well.
|
|
|
|
nsAutoHDC printerDC(::CreateICW(kDriverName, aName, nullptr, pDevMode));
|
|
|
|
if (NS_WARN_IF(!printerDC)) {
|
|
|
|
::HeapFree(::GetProcessHeap(), 0, pDevMode);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
psWin->CopyFromNative(printerDC, pDevMode);
|
|
|
|
}
|
2006-02-07 04:13:29 +03:00
|
|
|
}
|
|
|
|
|
2016-08-22 08:45:49 +03:00
|
|
|
if (ret != IDOK) {
|
2006-02-07 04:14:18 +03:00
|
|
|
::HeapFree(::GetProcessHeap(), 0, pDevMode);
|
2016-08-22 08:45:49 +03:00
|
|
|
PR_PL(("***** nsDeviceContextSpecWin::GetDataFromPrinter - DocumentProperties call failed code: %d/0x%x\n", ret, ret));
|
2006-02-07 04:14:18 +03:00
|
|
|
DISPLAY_LAST_ERROR
|
|
|
|
return NS_ERROR_FAILURE;
|
2006-02-07 04:13:21 +03:00
|
|
|
}
|
|
|
|
|
2006-02-07 04:14:18 +03:00
|
|
|
SetDevMode(pDevMode); // cache the pointer and takes responsibility for the memory
|
2006-02-07 04:13:21 +03:00
|
|
|
|
2008-10-22 04:54:26 +04:00
|
|
|
SetDeviceName(aName);
|
2006-04-05 00:57:13 +04:00
|
|
|
|
2016-01-22 19:05:19 +03:00
|
|
|
SetDriverName(kDriverName);
|
2006-04-05 00:57:13 +04:00
|
|
|
|
2006-02-07 04:13:29 +03:00
|
|
|
rv = NS_OK;
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_GFX_PRINTER_NAME_NOT_FOUND;
|
2008-10-22 04:54:26 +04:00
|
|
|
PR_PL(("***** nsDeviceContextSpecWin::GetDataFromPrinter - Couldn't open printer: [%s]\n", NS_ConvertUTF16toUTF8(aName).get()));
|
2006-02-07 04:13:59 +03:00
|
|
|
DISPLAY_LAST_ERROR
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
2006-02-07 04:13:29 +03:00
|
|
|
return rv;
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//***********************************************************
|
|
|
|
// Printer Enumerator
|
|
|
|
//***********************************************************
|
|
|
|
nsPrinterEnumeratorWin::nsPrinterEnumeratorWin()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPrinterEnumeratorWin::~nsPrinterEnumeratorWin()
|
|
|
|
{
|
|
|
|
// Do not free printers here
|
|
|
|
// GlobalPrinters::GetInstance()->FreeGlobalPrinters();
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsPrinterEnumeratorWin, nsIPrinterEnumerator)
|
2006-02-07 04:13:12 +03:00
|
|
|
|
2007-05-06 15:29:41 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
2006-02-07 04:13:32 +03:00
|
|
|
// Return the Default Printer name
|
2016-07-22 11:56:13 +03:00
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
nsPrinterEnumeratorWin::GetDefaultPrinterName(char16_t * *aDefaultPrinterName)
|
2006-02-07 04:13:32 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aDefaultPrinterName);
|
|
|
|
|
|
|
|
*aDefaultPrinterName = GetDefaultPrinterNameFromGlobalPrinters(); // helper
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-07-22 11:56:13 +03:00
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
nsPrinterEnumeratorWin::InitPrintSettingsFromPrinter(const char16_t *aPrinterName, nsIPrintSettings *aPrintSettings)
|
2006-02-07 04:13:32 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aPrinterName);
|
|
|
|
NS_ENSURE_ARG_POINTER(aPrintSettings);
|
|
|
|
|
|
|
|
if (!*aPrinterName) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-01-06 14:29:11 +03:00
|
|
|
// When printing to PDF on Windows there is no associated printer driver.
|
|
|
|
int16_t outputFormat;
|
|
|
|
aPrintSettings->GetOutputFormat(&outputFormat);
|
|
|
|
if (outputFormat == nsIPrintSettings::kOutputFormatPDF) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsDeviceContextSpecWin> devSpecWin = new nsDeviceContextSpecWin();
|
2006-02-07 04:13:32 +03:00
|
|
|
if (!devSpecWin) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
if (NS_FAILED(GlobalPrinters::GetInstance()->EnumeratePrinterList())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-01-22 19:05:19 +03:00
|
|
|
AutoFreeGlobalPrinters autoFreeGlobalPrinters;
|
|
|
|
|
2016-06-10 11:44:45 +03:00
|
|
|
// If the settings have already been initialized from prefs then pass these to
|
|
|
|
// GetDataFromPrinter, so that they are saved to the printer.
|
|
|
|
bool initializedFromPrefs;
|
|
|
|
nsresult rv =
|
|
|
|
aPrintSettings->GetIsInitializedFromPrefs(&initializedFromPrefs);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (initializedFromPrefs) {
|
|
|
|
// If we pass in print settings to GetDataFromPrinter it already copies
|
|
|
|
// things back to the settings, so we can return here.
|
|
|
|
return devSpecWin->GetDataFromPrinter(aPrinterName, aPrintSettings);
|
|
|
|
}
|
|
|
|
|
2006-02-07 04:13:32 +03:00
|
|
|
devSpecWin->GetDataFromPrinter(aPrinterName);
|
|
|
|
|
2008-10-22 04:54:26 +04:00
|
|
|
LPDEVMODEW devmode;
|
2006-02-07 04:13:32 +03:00
|
|
|
devSpecWin->GetDevMode(devmode);
|
2016-01-22 19:05:19 +03:00
|
|
|
if (NS_WARN_IF(!devmode)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2006-02-07 04:13:32 +03:00
|
|
|
}
|
|
|
|
|
2016-01-22 19:05:19 +03:00
|
|
|
aPrintSettings->SetPrinterName(aPrinterName);
|
|
|
|
|
|
|
|
// We need to get information from the device as well.
|
2016-01-26 12:31:11 +03:00
|
|
|
char16ptr_t printerName = aPrinterName;
|
|
|
|
HDC dc = ::CreateICW(kDriverName, printerName, nullptr, devmode);
|
2016-01-22 19:05:19 +03:00
|
|
|
if (NS_WARN_IF(!dc)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrintSettingsWin> psWin = do_QueryInterface(aPrintSettings);
|
|
|
|
MOZ_ASSERT(psWin);
|
2016-01-12 20:40:07 +03:00
|
|
|
psWin->CopyFromNative(dc, devmode);
|
2016-01-22 19:05:19 +03:00
|
|
|
::DeleteDC(dc);
|
|
|
|
|
2006-02-07 04:13:32 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Enumerate all the Printers from the global array and pass their
|
|
|
|
// names back (usually to script)
|
2016-07-22 11:56:13 +03:00
|
|
|
NS_IMETHODIMP
|
2007-05-06 15:29:41 +04:00
|
|
|
nsPrinterEnumeratorWin::GetPrinterNameList(nsIStringEnumerator **aPrinterNameList)
|
2006-02-07 04:13:12 +03:00
|
|
|
{
|
2007-05-06 15:29:41 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aPrinterNameList);
|
2012-07-30 18:20:58 +04:00
|
|
|
*aPrinterNameList = nullptr;
|
2006-02-07 04:13:12 +03:00
|
|
|
|
2006-02-07 04:14:56 +03:00
|
|
|
nsresult rv = GlobalPrinters::GetInstance()->EnumeratePrinterList();
|
|
|
|
if (NS_FAILED(rv)) {
|
2007-05-06 15:29:41 +04:00
|
|
|
PR_PL(("***** nsDeviceContextSpecWin::GetPrinterNameList - Couldn't enumerate printers!\n"));
|
2006-02-07 04:14:56 +03:00
|
|
|
return rv;
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numPrinters = GlobalPrinters::GetInstance()->GetNumPrinters();
|
2009-01-18 23:14:14 +03:00
|
|
|
nsTArray<nsString> *printers = new nsTArray<nsString>(numPrinters);
|
2007-05-06 15:29:41 +04:00
|
|
|
if (!printers)
|
2006-02-07 04:13:12 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-05-06 15:29:41 +04:00
|
|
|
|
2015-11-18 18:25:19 +03:00
|
|
|
nsString* names = printers->AppendElements(numPrinters);
|
2015-12-02 22:28:38 +03:00
|
|
|
for (uint32_t printerInx = 0; printerInx < numPrinters; ++printerInx) {
|
|
|
|
LPWSTR name = GlobalPrinters::GetInstance()->GetItemFromList(printerInx);
|
2015-11-18 18:25:19 +03:00
|
|
|
names[printerInx].Assign(name);
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
|
|
|
|
2007-05-06 15:44:33 +04:00
|
|
|
return NS_NewAdoptingStringEnumerator(aPrinterNameList, printers);
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
//-- Global Printers
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2006-02-07 04:13:21 +03:00
|
|
|
// THe array hold the name and port for each printer
|
2016-07-22 11:56:13 +03:00
|
|
|
void
|
2006-02-07 04:13:12 +03:00
|
|
|
GlobalPrinters::ReallocatePrinters()
|
|
|
|
{
|
|
|
|
if (PrintersAreAllocated()) {
|
|
|
|
FreeGlobalPrinters();
|
|
|
|
}
|
2009-11-13 01:18:32 +03:00
|
|
|
mPrinters = new nsTArray<LPWSTR>();
|
2006-02-07 04:13:12 +03:00
|
|
|
NS_ASSERTION(mPrinters, "Printers Array is NULL!");
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2016-07-22 11:56:13 +03:00
|
|
|
void
|
2006-02-07 04:13:12 +03:00
|
|
|
GlobalPrinters::FreeGlobalPrinters()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
if (mPrinters != nullptr) {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i=0;i<mPrinters->Length();i++) {
|
2009-02-23 04:05:24 +03:00
|
|
|
free(mPrinters->ElementAt(i));
|
2006-02-07 04:13:21 +03:00
|
|
|
}
|
2006-02-07 04:13:12 +03:00
|
|
|
delete mPrinters;
|
2012-07-30 18:20:58 +04:00
|
|
|
mPrinters = nullptr;
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2016-07-22 11:56:13 +03:00
|
|
|
nsresult
|
2006-02-07 04:13:21 +03:00
|
|
|
GlobalPrinters::EnumerateNativePrinters()
|
1998-11-14 05:05:13 +03:00
|
|
|
{
|
2006-02-07 04:14:56 +03:00
|
|
|
nsresult rv = NS_ERROR_GFX_PRINTER_NO_PRINTER_AVAILABLE;
|
2006-02-07 04:14:03 +03:00
|
|
|
PR_PL(("-----------------------\n"));
|
|
|
|
PR_PL(("EnumerateNativePrinters\n"));
|
|
|
|
|
2016-07-22 11:56:13 +03:00
|
|
|
WCHAR szDefaultPrinterName[1024];
|
2009-11-13 01:18:32 +03:00
|
|
|
DWORD status = GetProfileStringW(L"devices", 0, L",",
|
|
|
|
szDefaultPrinterName,
|
2011-10-11 09:50:08 +04:00
|
|
|
ArrayLength(szDefaultPrinterName));
|
2006-02-07 04:13:21 +03:00
|
|
|
if (status > 0) {
|
|
|
|
DWORD count = 0;
|
2009-11-13 01:18:32 +03:00
|
|
|
LPWSTR sPtr = szDefaultPrinterName;
|
|
|
|
LPWSTR ePtr = szDefaultPrinterName + status;
|
|
|
|
LPWSTR prvPtr = sPtr;
|
2006-02-07 04:13:21 +03:00
|
|
|
while (sPtr < ePtr) {
|
2010-02-24 22:50:11 +03:00
|
|
|
if (*sPtr == 0) {
|
2009-11-13 01:18:32 +03:00
|
|
|
LPWSTR name = wcsdup(prvPtr);
|
2006-02-07 04:13:21 +03:00
|
|
|
mPrinters->AppendElement(name);
|
2006-02-07 04:14:03 +03:00
|
|
|
PR_PL(("Printer Name: %s\n", prvPtr));
|
2006-02-07 04:13:21 +03:00
|
|
|
prvPtr = sPtr+1;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
sPtr++;
|
|
|
|
}
|
|
|
|
rv = NS_OK;
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
2006-02-07 04:14:03 +03:00
|
|
|
PR_PL(("-----------------------\n"));
|
2006-02-07 04:13:21 +03:00
|
|
|
return rv;
|
1998-11-14 05:05:13 +03:00
|
|
|
}
|
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
//------------------------------------------------------------------
|
2006-02-07 04:13:17 +03:00
|
|
|
// Uses the GetProfileString to get the default printer from the registry
|
2016-07-22 11:56:13 +03:00
|
|
|
void
|
2009-11-13 01:18:32 +03:00
|
|
|
GlobalPrinters::GetDefaultPrinterName(nsString& aDefaultPrinterName)
|
2006-02-07 04:13:12 +03:00
|
|
|
{
|
2009-11-13 01:18:32 +03:00
|
|
|
aDefaultPrinterName.Truncate();
|
2016-07-22 11:56:13 +03:00
|
|
|
WCHAR szDefaultPrinterName[1024];
|
2009-11-13 01:18:32 +03:00
|
|
|
DWORD status = GetProfileStringW(L"windows", L"device", 0,
|
|
|
|
szDefaultPrinterName,
|
2011-10-11 09:50:08 +04:00
|
|
|
ArrayLength(szDefaultPrinterName));
|
2006-02-07 04:13:17 +03:00
|
|
|
if (status > 0) {
|
2009-11-13 01:18:32 +03:00
|
|
|
WCHAR comma = ',';
|
|
|
|
LPWSTR sPtr = szDefaultPrinterName;
|
2016-07-22 11:56:13 +03:00
|
|
|
while (*sPtr != comma && *sPtr != 0)
|
2006-02-07 04:13:17 +03:00
|
|
|
sPtr++;
|
|
|
|
if (*sPtr == comma) {
|
2010-08-20 22:19:38 +04:00
|
|
|
*sPtr = 0;
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
2009-11-13 01:18:32 +03:00
|
|
|
aDefaultPrinterName = szDefaultPrinterName;
|
2006-02-07 04:13:17 +03:00
|
|
|
} else {
|
2010-05-18 11:50:14 +04:00
|
|
|
aDefaultPrinterName = EmptyString();
|
2006-02-07 04:13:17 +03:00
|
|
|
}
|
|
|
|
|
2010-02-24 22:50:11 +03:00
|
|
|
PR_PL(("DEFAULT PRINTER [%s]\n", aDefaultPrinterName.get()));
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2006-02-07 04:14:30 +03:00
|
|
|
// This goes and gets the list of available printers and puts
|
2006-02-07 04:13:12 +03:00
|
|
|
// the default printer at the beginning of the list
|
2016-07-22 11:56:13 +03:00
|
|
|
nsresult
|
2006-02-07 04:13:12 +03:00
|
|
|
GlobalPrinters::EnumeratePrinterList()
|
1998-11-14 05:05:13 +03:00
|
|
|
{
|
2006-02-07 04:13:12 +03:00
|
|
|
// reallocate and get a new list each time it is asked for
|
|
|
|
// this deletes the list and re-allocates them
|
|
|
|
ReallocatePrinters();
|
|
|
|
|
|
|
|
// any of these could only fail with an OUT_MEMORY_ERROR
|
|
|
|
// PRINTER_ENUM_LOCAL should get the network printers on Win95
|
2006-02-07 04:13:21 +03:00
|
|
|
nsresult rv = EnumerateNativePrinters();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2006-02-07 04:13:14 +03:00
|
|
|
|
2006-02-07 04:13:15 +03:00
|
|
|
// get the name of the default printer
|
2009-11-13 01:18:32 +03:00
|
|
|
nsAutoString defPrinterName;
|
2006-02-07 04:13:15 +03:00
|
|
|
GetDefaultPrinterName(defPrinterName);
|
|
|
|
|
|
|
|
// put the default printer at the beginning of list
|
2009-11-13 01:18:32 +03:00
|
|
|
if (!defPrinterName.IsEmpty()) {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i=0;i<mPrinters->Length();i++) {
|
2009-11-13 01:18:32 +03:00
|
|
|
LPWSTR name = mPrinters->ElementAt(i);
|
|
|
|
if (defPrinterName.Equals(name)) {
|
2006-02-07 04:13:15 +03:00
|
|
|
if (i > 0) {
|
2009-11-13 01:18:32 +03:00
|
|
|
LPWSTR ptr = mPrinters->ElementAt(0);
|
2009-02-23 04:05:24 +03:00
|
|
|
mPrinters->ElementAt(0) = name;
|
|
|
|
mPrinters->ElementAt(i) = ptr;
|
2006-02-07 04:13:15 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we at least tried to get the printers
|
|
|
|
if (!PrintersAreAllocated()) {
|
2006-02-07 04:14:03 +03:00
|
|
|
PR_PL(("***** nsDeviceContextSpecWin::EnumeratePrinterList - Printers aren`t allocated\n"));
|
2006-02-07 04:13:12 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1998-11-14 05:05:13 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-02-07 04:13:12 +03:00
|
|
|
|