1) On windows and OS/2 it implements IDL attr of "defaultPrinterName" (like Bug 118152)

2) Stubs this out on al other platforms
3) On Windows implmenets new method "InitPrintSettingsFromPrinter" which sets certain key values from the native devmode into the print settings
4) Stubs this out on all other platforms
5) It also cleans up the nsDeviceContextSpecWin interface a little bit:
   It now has a single GetDevMode method that must be paired with a UnlockDevMode  method.
6) It moved GetDataFromPrinter into the public interface
7) Created a simple helper function for getting the default printer name.
Bug 123554 r=dcone sr=attinasi
This commit is contained in:
rods%netscape.com 2006-02-07 01:13:32 +00:00
Родитель 209100bebe
Коммит 141af8f1bc
4 изменённых файлов: 143 добавлений и 44 удалений

Просмотреть файл

@ -266,6 +266,23 @@ interface nsIPrintOptions : nsISupports
[scriptable, uuid(a6cf9128-15b3-11d2-932e-00805f8add32)] [scriptable, uuid(a6cf9128-15b3-11d2-932e-00805f8add32)]
interface nsIPrinterEnumerator : nsISupports interface nsIPrinterEnumerator : nsISupports
{ {
/**
* The name of the default printer
* This name must be in the list of printer names returned by
* "availablePrinters"
*
*/
readonly attribute wstring defaultPrinterName;
/**
* Initializes certain settings from the native printer into the PrintSettings
* These settings include, but are not limited to:
* Page Orientation
* Page Size
* Number of Copies
*/
void initPrintSettingsFromPrinter(in wstring aPrinterName, in nsIPrintSettings aPrintSettings);
/** /**
* Returns an array of the names of all installed printers. * Returns an array of the names of all installed printers.
* *

Просмотреть файл

@ -482,7 +482,13 @@ NS_IMETHODIMP nsPrinterEnumeratorGTK::EnumeratePrinters(PRUint32* aCount, PRUnic
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP nsPrinterEnumeratorGTK::DisplayPropertiesDlg(const PRUnichar *aPrinter, nsIPrintSettings *aPrintSettings) /* void initPrintSettingsFromPrinter (in wstring aPrinterName, in nsIPrintSettings aPrintSettings); */
NS_IMETHODIMP nsPrinterEnumeratorGTK::InitPrintSettingsFromPrinter(const PRUnichar *aPrinterName, nsIPrintSettings *aPrintSettings)
{
return NS_OK;
}
NS_IMETHODIMP nsPrinterEnumeratorGTK::DisplayPropertiesDlg(const PRUnichar *aPrinter, nsIPrintSettings* aPrintSettings)
{ {
/* fixme: We simply ignore the |aPrinter| argument here /* fixme: We simply ignore the |aPrinter| argument here
* We should get the supported printer attributes from the printer and * We should get the supported printer attributes from the printer and

Просмотреть файл

@ -200,7 +200,7 @@ const NativePaperSizes kPaperSizes[] = {
const PRInt32 kNumPaperSizes = 41; const PRInt32 kNumPaperSizes = 41;
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
nsDeviceContextSpecWin :: nsDeviceContextSpecWin() : nsDeviceContextSpecWin::nsDeviceContextSpecWin() :
mUseExtendedPrintDlg(NULL) mUseExtendedPrintDlg(NULL)
{ {
NS_INIT_REFCNT(); NS_INIT_REFCNT();
@ -223,7 +223,7 @@ nsDeviceContextSpecWin :: nsDeviceContextSpecWin() :
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
NS_IMPL_ISUPPORTS1(nsDeviceContextSpecWin, nsIDeviceContextSpec) NS_IMPL_ISUPPORTS1(nsDeviceContextSpecWin, nsIDeviceContextSpec)
nsDeviceContextSpecWin :: ~nsDeviceContextSpecWin() nsDeviceContextSpecWin::~nsDeviceContextSpecWin()
{ {
SetDeviceName(nsnull); SetDeviceName(nsnull);
SetDriverName(nsnull); SetDriverName(nsnull);
@ -328,7 +328,9 @@ SetupDevModeFromSettings(LPDEVMODE aDevMode, nsIPrintSettings* aPrintSettings)
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Helper Function - Free and reallocate the string // Helper Function - Free and reallocate the string
static nsresult SetPrintSettingsFromDevMode(nsIPrintSettings* aPrintSettings, LPDEVMODE aDevMode) nsresult
nsDeviceContextSpecWin::SetPrintSettingsFromDevMode(nsIPrintSettings* aPrintSettings,
LPDEVMODE aDevMode)
{ {
if (aPrintSettings == nsnull) { if (aPrintSettings == nsnull) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
@ -376,9 +378,9 @@ static nsresult SetPrintSettingsFromDevMode(nsIPrintSettings* aPrintSettings, LP
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
NS_IMETHODIMP nsDeviceContextSpecWin :: Init(nsIWidget* aWidget, NS_IMETHODIMP nsDeviceContextSpecWin::Init(nsIWidget* aWidget,
nsIPrintSettings* aPrintSettings, nsIPrintSettings* aPrintSettings,
PRBool aQuiet) PRBool aQuiet)
{ {
mPrintSettings = aPrintSettings; mPrintSettings = aPrintSettings;
@ -429,19 +431,19 @@ static void CleanAndCopyString(char*& aStr, char* aNewStr)
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
void nsDeviceContextSpecWin :: SetDeviceName(char* aDeviceName) void nsDeviceContextSpecWin::SetDeviceName(char* aDeviceName)
{ {
CleanAndCopyString(mDeviceName, aDeviceName); CleanAndCopyString(mDeviceName, aDeviceName);
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
void nsDeviceContextSpecWin :: SetDriverName(char* aDriverName) void nsDeviceContextSpecWin::SetDriverName(char* aDriverName)
{ {
CleanAndCopyString(mDriverName, aDriverName); CleanAndCopyString(mDriverName, aDriverName);
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
void nsDeviceContextSpecWin :: SetGlobalDevMode(HGLOBAL aHGlobal) void nsDeviceContextSpecWin::SetGlobalDevMode(HGLOBAL aHGlobal)
{ {
if (mGlobalDevMode) { if (mGlobalDevMode) {
::GlobalFree(mGlobalDevMode); ::GlobalFree(mGlobalDevMode);
@ -452,7 +454,7 @@ void nsDeviceContextSpecWin :: SetGlobalDevMode(HGLOBAL aHGlobal)
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
void nsDeviceContextSpecWin :: SetDevMode(LPDEVMODE aDevMode) void nsDeviceContextSpecWin::SetDevMode(LPDEVMODE aDevMode)
{ {
if (mDevMode) free(mDevMode); if (mDevMode) free(mDevMode);
@ -939,11 +941,44 @@ static HGLOBAL CreateGlobalDevModeAndInit(LPTSTR aPrintName, nsIPrintSettings* a
return hGlobalDevMode; return hGlobalDevMode;
} }
//------------------------------------------------------------------
// helper
static PRUnichar * GetDefaultPrinterNameFromGlobalPrinters()
{
PRUnichar * printerName;
LPTSTR lpPrtName;
GlobalPrinters::GetInstance()->GetDefaultPrinterName(lpPrtName);
nsString str;
#ifdef UNICODE
str.AppendWithConversion((PRUnichar *)lpPrtName);
#else
str.AssignWithConversion((char*)lpPrtName);
#endif
printerName = ToNewUnicode(str);
free(lpPrtName);
return printerName;
}
//------------------------------------------------------------------
void
nsDeviceContextSpecWin::GetDevMode(LPDEVMODE &aDevMode)
{
if (mIsDEVMODEGlobalHandle) {
if (mGlobalDevMode) {
aDevMode = (DEVMODE *)::GlobalLock(mGlobalDevMode);
} else {
aDevMode = NULL;
}
} else {
aDevMode = mDevMode;
}
}
//------------------------------------------------------------------ //------------------------------------------------------------------
// Displays the native Print Dialog // Displays the native Print Dialog
nsresult nsresult
nsDeviceContextSpecWin :: ShowNativePrintDialog(nsIWidget *aWidget, PRBool aQuiet) nsDeviceContextSpecWin::ShowNativePrintDialog(nsIWidget *aWidget, PRBool aQuiet)
{ {
NS_ENSURE_ARG_POINTER(aWidget); NS_ENSURE_ARG_POINTER(aWidget);
nsresult rv = NS_ERROR_FAILURE; nsresult rv = NS_ERROR_FAILURE;
@ -958,16 +993,7 @@ nsDeviceContextSpecWin :: ShowNativePrintDialog(nsIWidget *aWidget, PRBool aQuie
// If there is no name then use the default printer // If there is no name then use the default printer
if (!printerName || (printerName && !*printerName)) { if (!printerName || (printerName && !*printerName)) {
LPTSTR lpPrtName; printerName = GetDefaultPrinterNameFromGlobalPrinters();
GlobalPrinters::GetInstance()->GetDefaultPrinterName(lpPrtName);
nsString str;
#ifdef UNICODE
str.AppendWithConversion((PRUnichar *)lpPrtName);
#else
str.AssignWithConversion((char*)lpPrtName);
#endif
printerName = ToNewUnicode(str);
free(lpPrtName);
} }
NS_ASSERTION(printerName, "We have to have a printer name"); NS_ASSERTION(printerName, "We have to have a printer name");
@ -1286,7 +1312,7 @@ static HPROPSHEETPAGE ExtendPrintDialog(HWND aHWnd, char* aTitle)
//------------------------------------------------------------------ //------------------------------------------------------------------
// Displays the native Print Dialog // Displays the native Print Dialog
nsresult nsresult
nsDeviceContextSpecWin :: ShowNativePrintDialogEx(nsIWidget *aWidget, PRBool aQuiet) nsDeviceContextSpecWin::ShowNativePrintDialogEx(nsIWidget *aWidget, PRBool aQuiet)
{ {
NS_ENSURE_ARG_POINTER(aWidget); NS_ENSURE_ARG_POINTER(aWidget);
@ -1497,7 +1523,7 @@ nsDeviceContextSpecWin :: ShowNativePrintDialogEx(nsIWidget *aWidget, PRBool aQu
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Setup the object's data member with the selected printer's data // Setup the object's data member with the selected printer's data
nsresult nsresult
nsDeviceContextSpecWin :: GetDataFromPrinter(PRUnichar * aName, nsIPrintSettings* aPS) nsDeviceContextSpecWin::GetDataFromPrinter(const PRUnichar * aName, nsIPrintSettings* aPS)
{ {
nsresult rv = NS_ERROR_FAILURE; nsresult rv = NS_ERROR_FAILURE;
@ -1560,27 +1586,21 @@ nsDeviceContextSpecWin :: GetDataFromPrinter(PRUnichar * aName, nsIPrintSettings
// if it is a HGLOBAL then we need to "lock" it to get the LPDEVMODE // if it is a HGLOBAL then we need to "lock" it to get the LPDEVMODE
// and unlock it when we are done. // and unlock it when we are done.
void void
nsDeviceContextSpecWin :: SetupPaperInfoFromSettings() nsDeviceContextSpecWin::SetupPaperInfoFromSettings()
{ {
LPDEVMODE devMode; LPDEVMODE devMode;
if (mDevMode == NULL && mGlobalDevMode == NULL) { GetDevMode(devMode);
return; NS_ASSERTION(devMode, "DevMode can't be NULL here");
if (devMode) {
SetupDevModeFromSettings(devMode, mPrintSettings);
} }
if (mGlobalDevMode != nsnull) { UnlockDevMode();
devMode = (DEVMODE *)::GlobalLock(mGlobalDevMode);
} else {
devMode = mDevMode;
}
SetupDevModeFromSettings(devMode, mPrintSettings);
if (mGlobalDevMode != nsnull) ::GlobalUnlock(mGlobalDevMode);
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
nsresult nsresult
nsDeviceContextSpecWin :: ShowXPPrintDialog(PRBool aQuiet) nsDeviceContextSpecWin::ShowXPPrintDialog(PRBool aQuiet)
{ {
nsresult rv = NS_ERROR_FAILURE; nsresult rv = NS_ERROR_FAILURE;
@ -1694,6 +1714,53 @@ static void CleanupArray(PRUnichar**& aArray, PRInt32& aCount)
aCount = 0; aCount = 0;
} }
//----------------------------------------------------------------------------------
// Return the Default Printer name
/* readonly attribute wstring defaultPrinterName; */
NS_IMETHODIMP
nsPrinterEnumeratorWin::GetDefaultPrinterName(PRUnichar * *aDefaultPrinterName)
{
NS_ENSURE_ARG_POINTER(aDefaultPrinterName);
*aDefaultPrinterName = GetDefaultPrinterNameFromGlobalPrinters(); // helper
return NS_OK;
}
/* void initPrintSettingsFromPrinter (in wstring aPrinterName, in nsIPrintSettings aPrintSettings); */
NS_IMETHODIMP
nsPrinterEnumeratorWin::InitPrintSettingsFromPrinter(const PRUnichar *aPrinterName, nsIPrintSettings *aPrintSettings)
{
NS_ENSURE_ARG_POINTER(aPrinterName);
NS_ENSURE_ARG_POINTER(aPrintSettings);
if (!*aPrinterName) {
return NS_OK;
}
nsCOMPtr<nsDeviceContextSpecWin> devSpecWin = new nsDeviceContextSpecWin();
if (!devSpecWin) return NS_ERROR_OUT_OF_MEMORY;
if (NS_FAILED(GlobalPrinters::GetInstance()->EnumeratePrinterList())) {
return NS_ERROR_FAILURE;
}
devSpecWin->GetDataFromPrinter(aPrinterName);
LPDEVMODE devmode;
devSpecWin->GetDevMode(devmode);
NS_ASSERTION(devmode, "DevMode can't be NULL here");
if (devmode) {
nsDeviceContextSpecWin::SetPrintSettingsFromDevMode(aPrintSettings, devmode);
}
devSpecWin->UnlockDevMode();
// Free them, we won't need them for a while
GlobalPrinters::GetInstance()->FreeGlobalPrinters();
return NS_OK;
}
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Enumerate all the Printers from the global array and pass their // Enumerate all the Printers from the global array and pass their
// names back (usually to script) // names back (usually to script)
@ -1796,7 +1863,7 @@ NS_IMETHODIMP nsPrinterEnumeratorWin::DisplayPropertiesDlg(const PRUnichar *aPri
#endif #endif
if (stat == IDOK) { if (stat == IDOK) {
// Now set the print options from the native Page Setup // Now set the print options from the native Page Setup
SetPrintSettingsFromDevMode(aPrintSettings, pDevMode); nsDeviceContextSpecWin::SetPrintSettingsFromDevMode(aPrintSettings, pDevMode);
} }
free(pDevMode); free(pDevMode);
free(pNewDevMode); free(pNewDevMode);

Просмотреть файл

@ -53,11 +53,21 @@ public:
NS_IMETHOD Init(nsIWidget* aWidget, nsIPrintSettings* aPS, PRBool aQuiet); NS_IMETHOD Init(nsIWidget* aWidget, nsIPrintSettings* aPS, PRBool aQuiet);
void GetDriverName(char *&aDriverName) const { aDriverName = mDriverName; } void GetDriverName(char *&aDriverName) const { aDriverName = mDriverName; }
void GetDeviceName(char *&aDeviceName) const { aDeviceName = mDeviceName; } void GetDeviceName(char *&aDeviceName) const { aDeviceName = mDeviceName; }
void GetGlobalDevMode(HGLOBAL &aHGlobal) const { aHGlobal = mGlobalDevMode; }
void GetDevMode(LPDEVMODE &aDevMode) const { aDevMode = mDevMode; } // The GetDevMode will return a pointer to a DevMode
PRBool IsDEVMODEGlobalHandle() const { return mIsDEVMODEGlobalHandle; } // 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 UnlockDevMode() { if (mIsDEVMODEGlobalHandle && mGlobalDevMode) ::GlobalUnlock(mGlobalDevMode); }
// helper functions
nsresult GetDataFromPrinter(const PRUnichar * aName, nsIPrintSettings* aPS = nsnull);
static nsresult SetPrintSettingsFromDevMode(nsIPrintSettings* aPrintSettings,
LPDEVMODE aDevMode);
protected: protected:
nsresult ShowXPPrintDialog(PRBool aQuiet); nsresult ShowXPPrintDialog(PRBool aQuiet);
@ -72,7 +82,6 @@ protected:
void SetGlobalDevMode(HGLOBAL aHGlobal); void SetGlobalDevMode(HGLOBAL aHGlobal);
void SetDevMode(LPDEVMODE aDevMode); void SetDevMode(LPDEVMODE aDevMode);
nsresult GetDataFromPrinter(PRUnichar * aName, nsIPrintSettings* aPS = nsnull);
void SetupPaperInfoFromSettings(); void SetupPaperInfoFromSettings();
virtual ~nsDeviceContextSpecWin(); virtual ~nsDeviceContextSpecWin();