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
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2016-04-19 23:51:25 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
1998-11-14 05:05:13 +03:00
|
|
|
#include "nsDeviceContextSpecWin.h"
|
|
|
|
#include "prmem.h"
|
2009-01-05 21:17:44 +03:00
|
|
|
|
2006-02-07 04:13:53 +03:00
|
|
|
#include <winspool.h>
|
2009-01-05 21:17:44 +03:00
|
|
|
|
2006-02-07 04:14:31 +03:00
|
|
|
#include <tchar.h>
|
2006-02-07 04:13:21 +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"
|
2006-02-07 04:14:01 +03:00
|
|
|
#include "nsCRT.h"
|
2006-02-07 04:13:12 +03:00
|
|
|
#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
|
|
|
|
2007-03-01 03:02:27 +03:00
|
|
|
#include "gfxPDFSurface.h"
|
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
|
|
|
|
2006-02-07 04:14:55 +03:00
|
|
|
// For NS_CopyNativeToUnicode
|
|
|
|
#include "nsNativeCharsetUtils.h"
|
|
|
|
|
2006-02-07 04:13:53 +03:00
|
|
|
// File Picker
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIFilePicker.h"
|
2006-02-07 04:13:12 +03:00
|
|
|
#include "nsIStringBundle.h"
|
2006-02-07 04:14:02 +03:00
|
|
|
#define NS_ERROR_GFX_PRINTER_BUNDLE_URL "chrome://global/locale/printing.properties"
|
2006-02-07 04:13:21 +03:00
|
|
|
|
2015-06-18 09:18:00 +03:00
|
|
|
#include "mozilla/gfx/Logging.h"
|
|
|
|
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2006-02-15 18:45:43 +03:00
|
|
|
PRLogModuleInfo * kWidgetPrintingLogMod = PR_NewLogModule("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-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;
|
2006-02-07 04:13:21 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
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);
|
2016-03-19 14:01:30 +03:00
|
|
|
mPrintSettings->GetOutputFormat(&mOutputFormat);
|
2009-12-11 07:02:13 +03:00
|
|
|
}
|
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;
|
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 {
|
|
|
|
PR_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) {
|
2013-12-03 19:12:57 +04:00
|
|
|
aStr = (wchar_t *)PR_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-10 23:40:54 +03:00
|
|
|
NS_IMETHODIMP nsDeviceContextSpecWin::GetSurfaceForPrinter(gfxASurface **surface)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mDevMode, "DevMode can't be NULL here");
|
|
|
|
|
2016-01-05 13:08:57 +03:00
|
|
|
*surface = nullptr;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxASurface> newSurface;
|
2007-03-01 03:02:27 +03:00
|
|
|
|
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) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFileOutputStream> stream = do_CreateInstance("@mozilla.org/network/file-output-stream;1");
|
|
|
|
rv = stream->Init(file, -1, -1, 0);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2007-07-17 03:16:48 +04:00
|
|
|
newSurface = new gfxPDFSurface(stream, gfxSize(width, height));
|
2007-03-01 03:02:27 +03:00
|
|
|
} else {
|
|
|
|
if (mDevMode) {
|
2009-12-11 07:02:13 +03:00
|
|
|
NS_WARN_IF_FALSE(mDriverName, "No driver!");
|
2013-10-08 22:48:02 +04:00
|
|
|
HDC dc = ::CreateDCW(mDriverName, mDeviceName, nullptr, mDevMode);
|
2015-06-18 09:18:00 +03:00
|
|
|
if (!dc) {
|
|
|
|
gfxCriticalError(gfxCriticalError::DefaultOptions(false)) << "Failed to create device context in GetSurfaceForPrinter";
|
|
|
|
return NS_ERROR_GFX_PRINTER_NAME_NOT_FOUND;
|
|
|
|
}
|
2007-03-01 03:02:27 +03:00
|
|
|
|
|
|
|
// have this surface take over ownership of this DC
|
2007-09-25 02:50:44 +04:00
|
|
|
newSurface = new gfxWindowsSurface(dc, gfxWindowsSurface::FLAG_TAKE_DC | gfxWindowsSurface::FLAG_FOR_PRINTING);
|
2015-06-18 09:18:00 +03:00
|
|
|
if (newSurface->GetType() == (gfxSurfaceType)-1) {
|
|
|
|
gfxCriticalError() << "Invalid windows surface from " << gfx::hexa(dc);
|
2016-01-05 13:08:57 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-06-18 09:18:00 +03:00
|
|
|
}
|
2007-03-01 03:02:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 13:08:57 +03:00
|
|
|
newSurface.forget(surface);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
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.
|
|
|
|
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);
|
2016-01-05 13:08:57 +03:00
|
|
|
|
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
|
|
|
|
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
|
|
|
//------------------------------------------------------------------
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-02-07 04:13:59 +03:00
|
|
|
#define DISPLAY_LAST_ERROR
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-10-08 22:48:02 +04:00
|
|
|
HANDLE hPrinter = nullptr;
|
2013-12-03 19:12:57 +04:00
|
|
|
wchar_t *name = (wchar_t*)aName; // Windows APIs use non-const name argument
|
2008-10-22 04:54:26 +04: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) {
|
|
|
|
|
2008-10-22 04:54:26 +04:00
|
|
|
LPDEVMODEW pDevMode;
|
2006-02-07 04:13:21 +03:00
|
|
|
DWORD dwNeeded, dwRet;
|
|
|
|
|
|
|
|
// Allocate a buffer of the correct size.
|
2013-12-03 19:12:57 +04:00
|
|
|
dwNeeded = ::DocumentPropertiesW(nullptr, hPrinter, name, nullptr, nullptr, 0);
|
2006-02-07 04:13:12 +03:00
|
|
|
|
2008-10-22 04:54:26 +04:00
|
|
|
pDevMode = (LPDEVMODEW)::HeapAlloc (::GetProcessHeap(), HEAP_ZERO_MEMORY, dwNeeded);
|
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.
|
2013-12-03 19:12:57 +04:00
|
|
|
dwRet = DocumentPropertiesW(nullptr, hPrinter, name,
|
2013-10-08 22:48:02 +04:00
|
|
|
pDevMode, nullptr, DM_OUT_BUFFER);
|
2006-02-07 04:13:21 +03:00
|
|
|
|
2006-02-07 04:14:18 +03:00
|
|
|
if (dwRet == 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
|
2013-12-03 19:12:57 +04:00
|
|
|
dwRet = ::DocumentPropertiesW(nullptr, hPrinter, name,
|
2006-02-07 04:15:01 +03:00
|
|
|
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.
|
|
|
|
if (dwRet == IDOK) {
|
|
|
|
// 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);
|
|
|
|
::ClosePrinter(hPrinter);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
psWin->CopyFromNative(printerDC, pDevMode);
|
|
|
|
}
|
2006-02-07 04:13:29 +03:00
|
|
|
}
|
|
|
|
|
2006-02-07 04:14:18 +03:00
|
|
|
if (dwRet != IDOK) {
|
|
|
|
::HeapFree(::GetProcessHeap(), 0, pDevMode);
|
|
|
|
::ClosePrinter(hPrinter);
|
|
|
|
PR_PL(("***** nsDeviceContextSpecWin::GetDataFromPrinter - DocumentProperties call failed code: %d/0x%x\n", dwRet, dwRet));
|
|
|
|
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:21 +03:00
|
|
|
::ClosePrinter(hPrinter);
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
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)
|
2006-02-07 04:13:21 +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
|
2006-02-07 04:13:12 +03:00
|
|
|
void
|
|
|
|
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!");
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
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"));
|
|
|
|
|
2009-11-13 01:18:32 +03:00
|
|
|
WCHAR szDefaultPrinterName[1024];
|
|
|
|
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
|
2006-02-07 04:13:12 +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();
|
|
|
|
WCHAR szDefaultPrinterName[1024];
|
|
|
|
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;
|
2010-02-24 22:50:11 +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
|
|
|
|
nsresult
|
|
|
|
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
|
|
|
|