Bug 459674: widget printing wide char fixes r=stuart sr=roc

This commit is contained in:
Brad Lassey 2008-10-21 20:54:26 -04:00
parent 51ac46cda7
commit d3f20c5653
6 changed files with 74 additions and 81 deletions

View File

@ -291,7 +291,7 @@ SetupDevModeFromSettings(LPDEVMODE aDevMode, nsIPrintSettings* aPrintSettings)
// Helper Function - Free and reallocate the string
static nsresult
SetPrintSettingsFromDevMode(nsIPrintSettings* aPrintSettings,
LPDEVMODE aDevMode)
LPDEVMODEW aDevMode)
{
if (aPrintSettings == nsnull) {
return NS_ERROR_FAILURE;
@ -1015,8 +1015,8 @@ ShowNativePrintDialog(HWND aHWnd,
}
// Setup local Data members
psWin->SetDeviceName(device);
psWin->SetDriverName(driver);
psWin->SetDeviceName(NS_ConvertUTF8toUTF16(device).get());
psWin->SetDriverName(NS_ConvertUTF8toUTF16(driver).get());
#if defined(DEBUG_rods) || defined(DEBUG_dcone)
printf("printer: driver %s, device %s flags: %d\n", driver, device, prntdlg.Flags);
@ -1067,7 +1067,7 @@ ShowNativePrintDialog(HWND aHWnd,
::GlobalUnlock(prntdlg.hDevNames);
// Transfer the settings from the native data to the PrintSettings
LPDEVMODE devMode = (LPDEVMODE)::GlobalLock(prntdlg.hDevMode);
LPDEVMODEW devMode = (LPDEVMODEW)::GlobalLock(prntdlg.hDevMode);
if (devMode == NULL) {
::GlobalFree(hGlobalDevMode);
return NS_ERROR_FAILURE;

View File

@ -45,14 +45,14 @@
/**
* Native types
*/
[ptr] native nsDevMode(DEVMODE);
[ptr] native nsDevMode(DEVMODEW);
/**
* Simplified PrintSettings for Windows interface
*
* @status UNDER_REVIEW
*/
[scriptable, uuid(FF328FE4-41D5-4b78-82AB-6B1FBC7930AF)]
[scriptable, uuid(f13b225d-473e-4372-b11f-b6dff9fe0c5b)]
interface nsIPrintSettingsWin : nsISupports
{
@ -69,8 +69,8 @@ interface nsIPrintSettingsWin : nsISupports
* via the "m_pd" data member of the CPrintDialog
* in MFC.
*/
[noscript] attribute charPtr deviceName;
[noscript] attribute charPtr driverName;
[noscript] attribute wstring deviceName;
[noscript] attribute wstring driverName;
[noscript] attribute nsDevMode devMode;

View File

@ -216,14 +216,14 @@ static PRUnichar * GetDefaultPrinterNameFromGlobalPrinters()
//----------------------------------------------------------------
static nsresult
EnumerateNativePrinters(DWORD aWhichPrinters, LPTSTR aPrinterName, PRBool& aIsFound, PRBool& aIsFile)
EnumerateNativePrinters(DWORD aWhichPrinters, LPWSTR aPrinterName, PRBool& aIsFound, PRBool& aIsFile)
{
#ifdef WINCE
aIsFound = PR_FALSE;
#else
DWORD dwSizeNeeded = 0;
DWORD dwNumItems = 0;
LPPRINTER_INFO_2 lpInfo = NULL;
LPPRINTER_INFO_2W lpInfo = NULL;
// Get buffer size
if (::EnumPrinters ( aWhichPrinters, NULL, 2, NULL, 0, &dwSizeNeeded, &dwNumItems )) {
@ -231,7 +231,7 @@ EnumerateNativePrinters(DWORD aWhichPrinters, LPTSTR aPrinterName, PRBool& aIsFo
}
// allocate memory
lpInfo = (LPPRINTER_INFO_2)HeapAlloc ( GetProcessHeap (), HEAP_ZERO_MEMORY, dwSizeNeeded );
lpInfo = (LPPRINTER_INFO_2W)HeapAlloc ( GetProcessHeap (), HEAP_ZERO_MEMORY, dwSizeNeeded );
if ( lpInfo == NULL ) {
return NS_ERROR_OUT_OF_MEMORY;
}
@ -243,9 +243,9 @@ EnumerateNativePrinters(DWORD aWhichPrinters, LPTSTR aPrinterName, PRBool& aIsFo
for (DWORD i = 0; i < dwNumItems; i++ ) {
if (_tcscmp(lpInfo[i].pPrinterName, aPrinterName) == 0) {
if (wcscmp(lpInfo[i].pPrinterName, aPrinterName) == 0) {
aIsFound = PR_TRUE;
aIsFile = _tcscmp(lpInfo[i].pPortName, _T("FILE:")) == 0;
aIsFile = wcscmp(lpInfo[i].pPortName, L"FILE:") == 0;
break;
}
}
@ -257,7 +257,7 @@ EnumerateNativePrinters(DWORD aWhichPrinters, LPTSTR aPrinterName, PRBool& aIsFo
//----------------------------------------------------------------
static void
CheckForPrintToFileWithName(LPTSTR aPrinterName, PRBool& aIsFile)
CheckForPrintToFileWithName(LPWSTR aPrinterName, PRBool& aIsFile)
{
PRBool isFound = PR_FALSE;
aIsFile = PR_FALSE;
@ -370,24 +370,14 @@ GetFileNameForPrintSettings(nsIPrintSettings* aPS)
//----------------------------------------------------------------------------------
static nsresult
CheckForPrintToFile(nsIPrintSettings* aPS, LPTSTR aPrinterName, PRUnichar* aUPrinterName)
CheckForPrintToFile(nsIPrintSettings* aPS, LPWSTR aPrinterName, PRUnichar* aUPrinterName)
{
nsresult rv = NS_OK;
if (!aPrinterName && !aUPrinterName) return rv;
PRBool toFile;
#ifdef UNICODE
CheckForPrintToFileWithName(aPrinterName?aPrinterName:aUPrinterName, toFile);
#else
if (aPrinterName) {
CheckForPrintToFileWithName(aPrinterName, toFile);
} else {
nsCAutoString nativeName;
NS_CopyUnicodeToNative(nsDependentString(aUPrinterName), nativeName);
CheckForPrintToFileWithName(const_cast<char*>(nativeName.get()), toFile);
}
#endif
// Since the driver wasn't a "Print To File" Driver, check to see
// if the name of the file has been set to the special "FILE:"
if (!toFile) {
@ -422,12 +412,12 @@ NS_IMETHODIMP nsDeviceContextSpecWin::Init(nsIWidget* aWidget,
if (aPrintSettings) {
nsCOMPtr<nsIPrintSettingsWin> psWin(do_QueryInterface(aPrintSettings));
if (psWin) {
char* deviceName;
char* driverName;
PRUnichar* deviceName;
PRUnichar* driverName;
psWin->GetDeviceName(&deviceName); // creates new memory (makes a copy)
psWin->GetDriverName(&driverName); // creates new memory (makes a copy)
LPDEVMODE devMode;
LPDEVMODEW devMode;
psWin->GetDevMode(&devMode); // creates new memory (makes a copy)
if (deviceName && driverName && devMode) {
@ -470,7 +460,7 @@ NS_IMETHODIMP nsDeviceContextSpecWin::Init(nsIWidget* aWidget,
PR_PL(("***** nsDeviceContextSpecWin::Init - aPrintSettingswas NULL!\n"));
}
LPDEVMODE pDevMode = NULL;
LPDEVMODEW pDevMode = NULL;
HGLOBAL hDevNames = NULL;
// Get the Print Name to be used
@ -494,11 +484,11 @@ NS_IMETHODIMP nsDeviceContextSpecWin::Init(nsIWidget* aWidget,
//----------------------------------------------------------
// Helper Function - Free and reallocate the string
static void CleanAndCopyString(char*& aStr, char* aNewStr)
static void CleanAndCopyString(PRUnichar*& aStr, const PRUnichar* aNewStr)
{
if (aStr != nsnull) {
if (aNewStr != nsnull && strlen(aStr) > strlen(aNewStr)) { // reuse it if we can
PL_strcpy(aStr, aNewStr);
if (aNewStr != nsnull && wcslen(aStr) > wcslen(aNewStr)) { // reuse it if we can
wcscpy(aStr, aNewStr);
return;
} else {
PR_Free(aStr);
@ -507,8 +497,8 @@ static void CleanAndCopyString(char*& aStr, char* aNewStr)
}
if (nsnull != aNewStr) {
aStr = (char *)PR_Malloc(PL_strlen(aNewStr) + 1);
PL_strcpy(aStr, aNewStr);
aStr = (PRUnichar *)PR_Malloc(sizeof(PRUnichar)*(wcslen(aNewStr) + 1));
wcscpy(aStr, aNewStr);
}
}
@ -544,7 +534,7 @@ NS_IMETHODIMP nsDeviceContextSpecWin::GetSurfaceForPrinter(gfxASurface **surface
newSurface = new gfxPDFSurface(stream, gfxSize(width, height));
} else {
if (mDevMode) {
HDC dc = ::CreateDC(mDriverName, mDeviceName, NULL, mDevMode);
HDC dc = ::CreateDCW(mDriverName, mDeviceName, NULL, mDevMode);
// have this surface take over ownership of this DC
newSurface = new gfxWindowsSurface(dc, gfxWindowsSurface::FLAG_TAKE_DC | gfxWindowsSurface::FLAG_FOR_PRINTING);
@ -562,19 +552,19 @@ NS_IMETHODIMP nsDeviceContextSpecWin::GetSurfaceForPrinter(gfxASurface **surface
}
//----------------------------------------------------------------------------------
void nsDeviceContextSpecWin::SetDeviceName(char* aDeviceName)
void nsDeviceContextSpecWin::SetDeviceName(const PRUnichar* aDeviceName)
{
CleanAndCopyString(mDeviceName, aDeviceName);
}
//----------------------------------------------------------------------------------
void nsDeviceContextSpecWin::SetDriverName(char* aDriverName)
void nsDeviceContextSpecWin::SetDriverName(const PRUnichar* aDriverName)
{
CleanAndCopyString(mDriverName, aDriverName);
}
//----------------------------------------------------------------------------------
void nsDeviceContextSpecWin::SetDevMode(LPDEVMODE aDevMode)
void nsDeviceContextSpecWin::SetDevMode(LPDEVMODEW aDevMode)
{
if (mDevMode) {
::HeapFree(::GetProcessHeap(), 0, mDevMode);
@ -585,7 +575,7 @@ void nsDeviceContextSpecWin::SetDevMode(LPDEVMODE aDevMode)
//------------------------------------------------------------------
void
nsDeviceContextSpecWin::GetDevMode(LPDEVMODE &aDevMode)
nsDeviceContextSpecWin::GetDevMode(LPDEVMODEW &aDevMode)
{
aDevMode = mDevMode;
}
@ -593,7 +583,7 @@ nsDeviceContextSpecWin::GetDevMode(LPDEVMODE &aDevMode)
//----------------------------------------------------------------------------------
// Map an incoming size to a Windows Native enum in the DevMode
static void
MapPaperSizeToNativeEnum(LPDEVMODE aDevMode,
MapPaperSizeToNativeEnum(LPDEVMODEW aDevMode,
PRInt16 aType,
double aW,
double aH)
@ -644,7 +634,7 @@ MapPaperSizeToNativeEnum(LPDEVMODE aDevMode,
// Setup Paper Size & Orientation options into the DevMode
//
static void
SetupDevModeFromSettings(LPDEVMODE aDevMode, nsIPrintSettings* aPrintSettings)
SetupDevModeFromSettings(LPDEVMODEW aDevMode, nsIPrintSettings* aPrintSettings)
{
// Setup paper size
if (aPrintSettings) {
@ -725,33 +715,32 @@ nsDeviceContextSpecWin::GetDataFromPrinter(const PRUnichar * aName, nsIPrintSett
}
HANDLE hPrinter = NULL;
nsCAutoString nativeName;
NS_CopyUnicodeToNative(nsDependentString(aName), nativeName);
BOOL status = ::OpenPrinter(const_cast<char*>(nativeName.get()),
BOOL status = ::OpenPrinterW((LPWSTR)(aName),
&hPrinter, NULL);
if (status) {
LPDEVMODE pDevMode;
LPDEVMODEW pDevMode;
DWORD dwNeeded, dwRet;
// Allocate a buffer of the correct size.
dwNeeded = ::DocumentProperties(NULL, hPrinter,
const_cast<char*>(nativeName.get()),
dwNeeded = ::DocumentPropertiesW(NULL, hPrinter,
const_cast<wchar_t*>(aName),
NULL, NULL, 0);
pDevMode = (LPDEVMODE)::HeapAlloc (::GetProcessHeap(), HEAP_ZERO_MEMORY, dwNeeded);
pDevMode = (LPDEVMODEW)::HeapAlloc (::GetProcessHeap(), HEAP_ZERO_MEMORY, dwNeeded);
if (!pDevMode) return NS_ERROR_FAILURE;
// Get the default DevMode for the printer and modify it for our needs.
dwRet = DocumentProperties(NULL, hPrinter,
const_cast<char*>(nativeName.get()),
dwRet = DocumentPropertiesW(NULL, hPrinter,
const_cast<wchar_t*>(aName),
pDevMode, NULL, DM_OUT_BUFFER);
if (dwRet == IDOK && aPS) {
SetupDevModeFromSettings(pDevMode, aPS);
// Sets back the changes we made to the DevMode into the Printer Driver
dwRet = ::DocumentProperties(NULL, hPrinter,
const_cast<char*>(nativeName.get()),
dwRet = ::DocumentPropertiesW(NULL, hPrinter,
const_cast<wchar_t*>(aName),
pDevMode, pDevMode,
DM_IN_BUFFER | DM_OUT_BUFFER);
}
@ -766,15 +755,15 @@ nsDeviceContextSpecWin::GetDataFromPrinter(const PRUnichar * aName, nsIPrintSett
SetDevMode(pDevMode); // cache the pointer and takes responsibility for the memory
SetDeviceName(const_cast<char*>(nativeName.get()));
SetDeviceName(aName);
SetDriverName("WINSPOOL");
SetDriverName(L"WINSPOOL");
::ClosePrinter(hPrinter);
rv = NS_OK;
} else {
rv = NS_ERROR_GFX_PRINTER_NAME_NOT_FOUND;
PR_PL(("***** nsDeviceContextSpecWin::GetDataFromPrinter - Couldn't open printer: [%s]\n", nativeName.get()));
PR_PL(("***** nsDeviceContextSpecWin::GetDataFromPrinter - Couldn't open printer: [%s]\n", NS_ConvertUTF16toUTF8(aName).get()));
DISPLAY_LAST_ERROR
}
return rv;
@ -790,7 +779,7 @@ nsDeviceContextSpecWin::GetDataFromPrinter(const PRUnichar * aName, nsIPrintSett
void
nsDeviceContextSpecWin::SetupPaperInfoFromSettings()
{
LPDEVMODE devMode;
LPDEVMODEW devMode;
GetDevMode(devMode);
NS_ASSERTION(devMode, "DevMode can't be NULL here");
@ -803,7 +792,7 @@ nsDeviceContextSpecWin::SetupPaperInfoFromSettings()
// Helper Function - Free and reallocate the string
nsresult
nsDeviceContextSpecWin::SetPrintSettingsFromDevMode(nsIPrintSettings* aPrintSettings,
LPDEVMODE aDevMode)
LPDEVMODEW aDevMode)
{
if (aPrintSettings == nsnull) {
return NS_ERROR_FAILURE;
@ -916,7 +905,7 @@ nsPrinterEnumeratorWin::InitPrintSettingsFromPrinter(const PRUnichar *aPrinterNa
devSpecWin->GetDataFromPrinter(aPrinterName);
LPDEVMODE devmode;
LPDEVMODEW devmode;
devSpecWin->GetDevMode(devmode);
NS_ASSERTION(devmode, "DevMode can't be NULL here");
if (devmode) {
@ -953,8 +942,12 @@ nsPrinterEnumeratorWin::GetPrinterNameList(nsIStringEnumerator **aPrinterNameLis
PRInt32 printerInx = 0;
while( printerInx < numPrinters ) {
LPTSTR name = GlobalPrinters::GetInstance()->GetItemFromList(printerInx++);
#ifdef UNICODE
nsDependentString newName(name);
#else
nsAutoString newName;
NS_CopyNativeToUnicode(nsDependentCString(name), newName);
#endif
printers->AppendString(newName);
}
@ -1055,7 +1048,7 @@ GlobalPrinters::GetDefaultPrinterName(LPTSTR& aDefaultPrinterName)
PR_PL(("DEFAULT PRINTER [%s]\n", aDefaultPrinterName));
#else
aDefaultPrinterName = "UNKNOWN";
aDefaultPrinterName = TEXT("UNKNOWN");
#endif
}

View File

@ -64,34 +64,34 @@ public:
NS_IMETHOD Init(nsIWidget* aWidget, nsIPrintSettings* aPS, PRBool aIsPrintPreview);
void GetDriverName(char *&aDriverName) const { aDriverName = mDriverName; }
void GetDeviceName(char *&aDeviceName) const { aDeviceName = mDeviceName; }
void GetDriverName(PRUnichar *&aDriverName) const { aDriverName = mDriverName; }
void GetDeviceName(PRUnichar *&aDeviceName) const { aDeviceName = mDeviceName; }
// The GetDevMode will return a pointer to a DevMode
// whether it is from the Global memory handle or just the DevMode
// To get the DevMode from the Global memory Handle it must lock it
// So this call must be paired with a call to UnlockGlobalHandle
void GetDevMode(LPDEVMODE &aDevMode);
void GetDevMode(LPDEVMODEW &aDevMode);
// helper functions
nsresult GetDataFromPrinter(const PRUnichar * aName, nsIPrintSettings* aPS = nsnull);
static nsresult SetPrintSettingsFromDevMode(nsIPrintSettings* aPrintSettings,
LPDEVMODE aDevMode);
LPDEVMODEW aDevMode);
protected:
void SetDeviceName(char* aDeviceName);
void SetDriverName(char* aDriverName);
void SetDevMode(LPDEVMODE aDevMode);
void SetDeviceName(const PRUnichar* aDeviceName);
void SetDriverName(const PRUnichar* aDriverName);
void SetDevMode(LPDEVMODEW aDevMode);
void SetupPaperInfoFromSettings();
virtual ~nsDeviceContextSpecWin();
char* mDriverName;
char* mDeviceName;
LPDEVMODE mDevMode;
PRUnichar* mDriverName;
PRUnichar* mDeviceName;
LPDEVMODEW mDevMode;
nsCOMPtr<nsIPrintSettings> mPrintSettings;
};

View File

@ -78,7 +78,7 @@ nsPrintSettingsWin::~nsPrintSettingsWin()
}
/* [noscript] attribute charPtr deviceName; */
NS_IMETHODIMP nsPrintSettingsWin::SetDeviceName(char * aDeviceName)
NS_IMETHODIMP nsPrintSettingsWin::SetDeviceName(const PRUnichar * aDeviceName)
{
if (mDeviceName) {
nsMemory::Free(mDeviceName);
@ -86,7 +86,7 @@ NS_IMETHODIMP nsPrintSettingsWin::SetDeviceName(char * aDeviceName)
mDeviceName = aDeviceName?nsCRT::strdup(aDeviceName):nsnull;
return NS_OK;
}
NS_IMETHODIMP nsPrintSettingsWin::GetDeviceName(char * *aDeviceName)
NS_IMETHODIMP nsPrintSettingsWin::GetDeviceName(PRUnichar **aDeviceName)
{
NS_ENSURE_ARG_POINTER(aDeviceName);
*aDeviceName = mDeviceName?nsCRT::strdup(mDeviceName):nsnull;
@ -94,7 +94,7 @@ NS_IMETHODIMP nsPrintSettingsWin::GetDeviceName(char * *aDeviceName)
}
/* [noscript] attribute charPtr driverName; */
NS_IMETHODIMP nsPrintSettingsWin::SetDriverName(char * aDriverName)
NS_IMETHODIMP nsPrintSettingsWin::SetDriverName(const PRUnichar * aDriverName)
{
if (mDriverName) {
nsMemory::Free(mDriverName);
@ -102,18 +102,18 @@ NS_IMETHODIMP nsPrintSettingsWin::SetDriverName(char * aDriverName)
mDriverName = aDriverName?nsCRT::strdup(aDriverName):nsnull;
return NS_OK;
}
NS_IMETHODIMP nsPrintSettingsWin::GetDriverName(char * *aDriverName)
NS_IMETHODIMP nsPrintSettingsWin::GetDriverName(PRUnichar **aDriverName)
{
NS_ENSURE_ARG_POINTER(aDriverName);
*aDriverName = mDriverName?nsCRT::strdup(mDriverName):nsnull;
return NS_OK;
}
void nsPrintSettingsWin::CopyDevMode(DEVMODE* aInDevMode, DEVMODE *& aOutDevMode)
void nsPrintSettingsWin::CopyDevMode(DEVMODEW* aInDevMode, DEVMODEW *& aOutDevMode)
{
aOutDevMode = nsnull;
size_t size = aInDevMode->dmSize + aInDevMode->dmDriverExtra;
aOutDevMode = (LPDEVMODE)::HeapAlloc (::GetProcessHeap(), HEAP_ZERO_MEMORY, size);
aOutDevMode = (LPDEVMODEW)::HeapAlloc (::GetProcessHeap(), HEAP_ZERO_MEMORY, size);
if (aOutDevMode) {
memcpy(aOutDevMode, aInDevMode, size);
}
@ -121,7 +121,7 @@ void nsPrintSettingsWin::CopyDevMode(DEVMODE* aInDevMode, DEVMODE *& aOutDevMode
}
/* [noscript] attribute nsDevMode devMode; */
NS_IMETHODIMP nsPrintSettingsWin::GetDevMode(DEVMODE * *aDevMode)
NS_IMETHODIMP nsPrintSettingsWin::GetDevMode(DEVMODEW * *aDevMode)
{
NS_ENSURE_ARG_POINTER(aDevMode);
@ -133,7 +133,7 @@ NS_IMETHODIMP nsPrintSettingsWin::GetDevMode(DEVMODE * *aDevMode)
return NS_OK;
}
NS_IMETHODIMP nsPrintSettingsWin::SetDevMode(DEVMODE * aDevMode)
NS_IMETHODIMP nsPrintSettingsWin::SetDevMode(DEVMODEW * aDevMode)
{
if (mDevMode) {
::HeapFree(::GetProcessHeap(), 0, mDevMode);

View File

@ -74,11 +74,11 @@ public:
nsPrintSettingsWin& operator=(const nsPrintSettingsWin& rhs);
protected:
void CopyDevMode(DEVMODE* aInDevMode, DEVMODE *& aOutDevMode);
void CopyDevMode(DEVMODEW* aInDevMode, DEVMODEW *& aOutDevMode);
char* mDeviceName;
char* mDriverName;
LPDEVMODE mDevMode;
PRUnichar* mDeviceName;
PRUnichar* mDriverName;
LPDEVMODEW mDevMode;
};