Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
nsDeviceContextSpecWin Class Reference

#include <nsDeviceContextSpecWin.h>

Inheritance diagram for nsDeviceContextSpecWin:
Inheritance graph
[legend]
Collaboration diagram for nsDeviceContextSpecWin:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsDeviceContextSpecWin ()
NS_DECL_ISUPPORTS NS_IMETHOD Init (nsIWidget *aWidget, nsIPrintSettings *aPS, PRBool aIsPrintPreview)
void GetDriverName (char *&aDriverName) const
void GetDeviceName (char *&aDeviceName) const
void GetDevMode (LPDEVMODE &aDevMode)
nsresult GetDataFromPrinter (const PRUnichar *aName, nsIPrintSettings *aPS=nsnull)

Static Public Member Functions

static nsresult SetPrintSettingsFromDevMode (nsIPrintSettings *aPrintSettings, LPDEVMODE aDevMode)

Protected Member Functions

void SetDeviceName (char *aDeviceName)
void SetDriverName (char *aDriverName)
void SetDevMode (LPDEVMODE aDevMode)
void SetupPaperInfoFromSettings ()
virtual ~nsDeviceContextSpecWin ()

Protected Attributes

char * mDriverName
char * mDeviceName
LPDEVMODE mDevMode
nsCOMPtr< nsIPrintSettingsmPrintSettings

Detailed Description

Definition at line 47 of file nsDeviceContextSpecWin.h.


Constructor & Destructor Documentation

Definition at line 170 of file nsDeviceContextSpecWin.cpp.

Definition at line 182 of file nsDeviceContextSpecWin.cpp.

{
  SetDeviceName(nsnull);
  SetDriverName(nsnull);
  SetDevMode(NULL);

  nsCOMPtr<nsIPrintSettingsWin> psWin(do_QueryInterface(mPrintSettings));
  if (psWin) {
    psWin->SetDeviceName(nsnull);
    psWin->SetDriverName(nsnull);
    psWin->SetDevMode(NULL);
  }

  // Free them, we won't need them for a while
  GlobalPrinters::GetInstance()->FreeGlobalPrinters();
}

Here is the call graph for this function:


Member Function Documentation

Definition at line 660 of file nsDeviceContextSpecWin.cpp.

{
#ifdef WINCE 
  return NS_ERROR_NOT_IMPLEMENTED;
#else
  nsresult rv = NS_ERROR_FAILURE;

  if (!GlobalPrinters::GetInstance()->PrintersAreAllocated()) {
    rv = GlobalPrinters::GetInstance()->EnumeratePrinterList();
    if (NS_FAILED(rv)) {
      PR_PL(("***** nsDeviceContextSpecWin::GetDataFromPrinter - Couldn't enumerate printers!\n"));
      DISPLAY_LAST_ERROR
    }
    NS_ENSURE_SUCCESS(rv, rv);
  }

  HANDLE hPrinter = NULL;
  nsCAutoString nativeName;
  NS_CopyUnicodeToNative(nsDependentString(aName), nativeName);
  BOOL status = ::OpenPrinter(NS_CONST_CAST(char*, nativeName.get()),
                              &hPrinter, NULL);
  if (status) {

    LPDEVMODE   pDevMode;
    DWORD       dwNeeded, dwRet;

    // Allocate a buffer of the correct size.
    dwNeeded = ::DocumentProperties(gParentWnd, hPrinter,
                                    NS_CONST_CAST(char*, nativeName.get()),
                                    NULL, NULL, 0);

    pDevMode = (LPDEVMODE)::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(gParentWnd, hPrinter, 
                               NS_CONST_CAST(char*, nativeName.get()),
                               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(gParentWnd, hPrinter,
                                   NS_CONST_CAST(char*, nativeName.get()),
                                   pDevMode, pDevMode,
                                   DM_IN_BUFFER | DM_OUT_BUFFER);
    }

    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;
    }

    SetDevMode(pDevMode); // cache the pointer and takes responsibility for the memory

    SetDeviceName(NS_CONST_CAST(char*, nativeName.get()));
  
    // The driver should be NULL for Win95/Win98
    OSVERSIONINFO os;
    os.dwOSVersionInfoSize = sizeof(os);
    ::GetVersionEx(&os);
    if (VER_PLATFORM_WIN32_NT == os.dwPlatformId) {
      SetDriverName("WINSPOOL");
    } else {
      SetDriverName(NULL);
    }
    ::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()));
    DISPLAY_LAST_ERROR
  }
  return rv;
#endif // WINCE
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsDeviceContextSpecWin::GetDeviceName ( char *&  aDeviceName) const [inline]

Definition at line 57 of file nsDeviceContextSpecWin.h.

{ aDeviceName = mDeviceName;     }

Here is the caller graph for this function:

void nsDeviceContextSpecWin::GetDevMode ( LPDEVMODE &  aDevMode)

Definition at line 537 of file nsDeviceContextSpecWin.cpp.

{
  aDevMode = mDevMode;
}

Here is the caller graph for this function:

void nsDeviceContextSpecWin::GetDriverName ( char *&  aDriverName) const [inline]

Definition at line 56 of file nsDeviceContextSpecWin.h.

{ aDriverName = mDriverName;     }

Here is the caller graph for this function:

NS_IMETHODIMP nsDeviceContextSpecWin::Init ( nsIWidget aWidget,
nsIPrintSettings aPS,
PRBool  aIsPrintPreview 
)

Definition at line 411 of file nsDeviceContextSpecWin.cpp.

{
  mPrintSettings = aPrintSettings;

  gParentWnd = (HWND)aWidget->GetNativeData(NS_NATIVE_WINDOW);

  nsresult rv = NS_ERROR_FAILURE;
  if (aPrintSettings) {
    nsCOMPtr<nsIPrintSettingsWin> psWin(do_QueryInterface(aPrintSettings));
    if (psWin) {
      char* deviceName;
      char* driverName;
      psWin->GetDeviceName(&deviceName); // creates new memory (makes a copy)
      psWin->GetDriverName(&driverName); // creates new memory (makes a copy)

      LPDEVMODE devMode;
      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;
          }
        }

        SetDeviceName(deviceName);
        SetDriverName(driverName);
        SetDevMode(devMode);

        if (!aIsPrintPreview) {
          rv = CheckForPrintToFile(mPrintSettings, deviceName, nsnull);
          if (NS_FAILED(rv)) {
            nsCRT::free(deviceName);
            nsCRT::free(driverName);
            return NS_ERROR_FAILURE;
          }
        }

        // clean up
        nsCRT::free(deviceName);
        nsCRT::free(driverName);

        return NS_OK;
      } else {
        PR_PL(("***** nsDeviceContextSpecWin::Init - deviceName/driverName/devMode was NULL!\n"));
        if (deviceName) nsCRT::free(deviceName);
        if (driverName) nsCRT::free(driverName);
        if (devMode) ::HeapFree(::GetProcessHeap(), 0, devMode);
      }
    }
  } else {
    PR_PL(("***** nsDeviceContextSpecWin::Init - aPrintSettingswas NULL!\n"));
  }

  LPDEVMODE pDevMode  = NULL;
  HGLOBAL   hDevNames = NULL;

  // Get the Print Name to be used
  PRUnichar * printerName;
  mPrintSettings->GetPrinterName(&printerName);

  // If there is no name then use the default printer
  if (!printerName || (printerName && !*printerName)) {
    printerName = GetDefaultPrinterNameFromGlobalPrinters();
  }

  NS_ASSERTION(printerName, "We have to have a printer name");
  if (!printerName || !*printerName) return NS_ERROR_FAILURE;

  if (!aIsPrintPreview) {
    CheckForPrintToFile(mPrintSettings, nsnull, printerName);
  }
 
  return GetDataFromPrinter(printerName, mPrintSettings);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsDeviceContextSpecWin::SetDeviceName ( char *  aDeviceName) [protected]

Definition at line 514 of file nsDeviceContextSpecWin.cpp.

{
  CleanAndCopyString(mDeviceName, aDeviceName);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsDeviceContextSpecWin::SetDevMode ( LPDEVMODE  aDevMode) [protected]

Definition at line 526 of file nsDeviceContextSpecWin.cpp.

{
  if (mDevMode) {
    ::HeapFree(::GetProcessHeap(), 0, mDevMode);
  }

  mDevMode = aDevMode;
}

Here is the caller graph for this function:

void nsDeviceContextSpecWin::SetDriverName ( char *  aDriverName) [protected]

Definition at line 520 of file nsDeviceContextSpecWin.cpp.

{
  CleanAndCopyString(mDriverName, aDriverName);
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsDeviceContextSpecWin::SetPrintSettingsFromDevMode ( nsIPrintSettings aPrintSettings,
LPDEVMODE  aDevMode 
) [static]

Definition at line 761 of file nsDeviceContextSpecWin.cpp.

{
  if (aPrintSettings == nsnull) {
    return NS_ERROR_FAILURE;
  }
  aPrintSettings->SetIsInitializedFromPrinter(PR_TRUE);

  BOOL doingNumCopies   = aDevMode->dmFields & DM_COPIES;
  BOOL doingOrientation = aDevMode->dmFields & DM_ORIENTATION;
  BOOL doingPaperSize   = aDevMode->dmFields & DM_PAPERSIZE;
  BOOL doingPaperLength = aDevMode->dmFields & DM_PAPERLENGTH;
  BOOL doingPaperWidth  = aDevMode->dmFields & DM_PAPERWIDTH;

  if (doingOrientation) {
    PRInt32 orientation  = aDevMode->dmOrientation == DMORIENT_PORTRAIT?
      nsIPrintSettings::kPortraitOrientation:nsIPrintSettings::kLandscapeOrientation;
    aPrintSettings->SetOrientation(orientation);
  }

  // Setup Number of Copies
  if (doingNumCopies) {
    aPrintSettings->SetNumCopies(PRInt32(aDevMode->dmCopies));
  }

  if (aDevMode->dmFields & DM_SCALE) {
    double scale = double(aDevMode->dmScale) / 100.0f;
    if (scale != 1.0) {
      aPrintSettings->SetScaling(scale);
      aDevMode->dmScale = 100;
      // To turn this on you must change where the mPrt->mShrinkToFit is being set in the DocumentViewer
      //aPrintSettings->SetShrinkToFit(PR_FALSE);
    }
  }

  if (doingPaperSize) {
    aPrintSettings->SetPaperSizeType(nsIPrintSettings::kPaperSizeNativeData);
    aPrintSettings->SetPaperData(aDevMode->dmPaperSize);
    for (PRInt32 i=0;i<kNumPaperSizes;i++) {
      if (kPaperSizes[i].mPaperSize == aDevMode->dmPaperSize) {
        aPrintSettings->SetPaperSizeUnit(kPaperSizes[i].mIsInches?nsIPrintSettings::kPaperSizeInches:nsIPrintSettings::kPaperSizeMillimeters);
        break;
      }
    }

  } else if (doingPaperLength && doingPaperWidth) {
    PRBool found = PR_FALSE;
    for (PRInt32 i=0;i<kNumPaperSizes;i++) {
      if (kPaperSizes[i].mPaperSize == aDevMode->dmPaperSize) {
        aPrintSettings->SetPaperSizeType(nsIPrintSettings::kPaperSizeDefined);
        aPrintSettings->SetPaperWidth(kPaperSizes[i].mWidth);
        aPrintSettings->SetPaperHeight(kPaperSizes[i].mHeight);
        aPrintSettings->SetPaperSizeUnit(kPaperSizes[i].mIsInches?nsIPrintSettings::kPaperSizeInches:nsIPrintSettings::kPaperSizeMillimeters);
        found = PR_TRUE;
        break;
      }
    }
    if (!found) {
      return NS_ERROR_FAILURE;
    }
  } else {
    return NS_ERROR_FAILURE;
  }
  return NS_OK;
}

Definition at line 747 of file nsDeviceContextSpecWin.cpp.

{
  LPDEVMODE devMode;

  GetDevMode(devMode);
  NS_ASSERTION(devMode, "DevMode can't be NULL here");
  if (devMode) {
    SetupDevModeFromSettings(devMode, mPrintSettings);
  }
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 82 of file nsDeviceContextSpecWin.h.

LPDEVMODE nsDeviceContextSpecWin::mDevMode [protected]

Definition at line 83 of file nsDeviceContextSpecWin.h.

Definition at line 81 of file nsDeviceContextSpecWin.h.

Definition at line 85 of file nsDeviceContextSpecWin.h.


The documentation for this class was generated from the following files: