Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
nsDeviceContextSpecWin.cpp File Reference
#include "nsDeviceContextSpecWin.h"
#include "prmem.h"
#include <winspool.h>
#include <tchar.h>
#include "nsVoidArray.h"
#include "nsIPrintSettingsWin.h"
#include "nsString.h"
#include "nsCRT.h"
#include "nsIServiceManager.h"
#include "nsReadableUtils.h"
#include "nsGfxCIID.h"
#include "nsIWindowWatcher.h"
#include "nsIDOMWindow.h"
#include "nsNativeCharsetUtils.h"
#include "nsILocalFile.h"
#include "nsIFile.h"
#include "nsIFilePicker.h"
#include "nsIStringBundle.h"
#include "prlog.h"

Go to the source code of this file.

Classes

class  GlobalPrinters
struct  NativePaperSizes

Defines

#define NS_ERROR_GFX_PRINTER_BUNDLE_URL   "chrome://global/locale/printing.properties"
#define PR_PL(_p1)
#define DISPLAY_LAST_ERROR

Functions

static PRUnicharGetDefaultPrinterNameFromGlobalPrinters ()
static nsresult EnumerateNativePrinters (DWORD aWhichPrinters, LPTSTR aPrinterName, PRBool &aIsFound, PRBool &aIsFile)
static void CheckForPrintToFileWithName (LPTSTR aPrinterName, PRBool &aIsFile)
static nsresult GetFileNameForPrintSettings (nsIPrintSettings *aPS)
static nsresult CheckForPrintToFile (nsIPrintSettings *aPS, LPTSTR aPrinterName, PRUnichar *aUPrinterName)
static void CleanAndCopyString (char *&aStr, char *aNewStr)
static void MapPaperSizeToNativeEnum (LPDEVMODE aDevMode, PRInt16 aType, double aW, double aH)
static void SetupDevModeFromSettings (LPDEVMODE aDevMode, nsIPrintSettings *aPrintSettings)
static void CleanupArray (PRUnichar **&aArray, PRInt32 &aCount)

Variables

static HWND gParentWnd = NULL
const NativePaperSizes kPaperSizes []
const PRInt32 kNumPaperSizes = 41

Class Documentation

struct NativePaperSizes

Definition at line 131 of file nsPrintDialogUtil.cpp.

Class Members
double mHeight
PRBool mIsInches
short mPaperSize
double mWidth

Define Documentation

Definition at line 654 of file nsDeviceContextSpecWin.cpp.

#define NS_ERROR_GFX_PRINTER_BUNDLE_URL   "chrome://global/locale/printing.properties"

Definition at line 63 of file nsDeviceContextSpecWin.cpp.

#define PR_PL (   _p1)

Definition at line 70 of file nsDeviceContextSpecWin.cpp.


Function Documentation

static nsresult CheckForPrintToFile ( nsIPrintSettings aPS,
LPTSTR  aPrinterName,
PRUnichar aUPrinterName 
) [static]

Definition at line 370 of file nsDeviceContextSpecWin.cpp.

{
  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(NS_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) {
    nsXPIDLString toFileName;
    aPS->GetToFileName(getter_Copies(toFileName));
    if (toFileName) {
      if (*toFileName) {
        if (toFileName.EqualsLiteral("FILE:")) {
          // this skips the setting of the "print to file" info below
          // which we don't want to do.
          return NS_OK; 
        }
      }
    }
  }
  aPS->SetPrintToFile(toFile);
  if (toFile) {
    rv = GetFileNameForPrintSettings(aPS);
  }
  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void CheckForPrintToFileWithName ( LPTSTR  aPrinterName,
PRBool aIsFile 
) [static]

Definition at line 257 of file nsDeviceContextSpecWin.cpp.

{
  PRBool isFound = PR_FALSE;
  aIsFile = PR_FALSE;
#ifndef WINCE
  nsresult rv = EnumerateNativePrinters(PRINTER_ENUM_LOCAL, aPrinterName, isFound, aIsFile);
  if (isFound) return;

  rv = EnumerateNativePrinters(PRINTER_ENUM_NETWORK, aPrinterName, isFound, aIsFile);
  if (isFound) return;

  rv = EnumerateNativePrinters(PRINTER_ENUM_SHARED, aPrinterName, isFound, aIsFile);
  if (isFound) return;

  rv = EnumerateNativePrinters(PRINTER_ENUM_REMOTE, aPrinterName, isFound, aIsFile);
  if (isFound) return;
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void CleanAndCopyString ( char *&  aStr,
char *  aNewStr 
) [static]

Definition at line 495 of file nsDeviceContextSpecWin.cpp.

{
  if (aStr != nsnull) {
    if (aNewStr != nsnull && strlen(aStr) > strlen(aNewStr)) { // reuse it if we can
      PL_strcpy(aStr, aNewStr);
      return;
    } else {
      PR_Free(aStr);
      aStr = nsnull;
    }
  }

  if (nsnull != aNewStr) {
    aStr = (char *)PR_Malloc(PL_strlen(aNewStr) + 1);
    PL_strcpy(aStr, aNewStr);
  }
}

Here is the caller graph for this function:

static void CleanupArray ( PRUnichar **&  aArray,
PRInt32 aCount 
) [static]

Definition at line 843 of file nsDeviceContextSpecWin.cpp.

{
  for (PRInt32 i = aCount - 1; i >= 0; i--) {
    nsMemory::Free(aArray[i]);
  }
  nsMemory::Free(aArray);
  aArray = NULL;
  aCount = 0;
}
static nsresult EnumerateNativePrinters ( DWORD  aWhichPrinters,
LPTSTR  aPrinterName,
PRBool aIsFound,
PRBool aIsFile 
) [static]

Definition at line 216 of file nsDeviceContextSpecWin.cpp.

{
#ifdef WINCE
  aIsFound = PR_FALSE;
#else
  DWORD             dwSizeNeeded = 0;
  DWORD             dwNumItems   = 0;
  LPPRINTER_INFO_2  lpInfo        = NULL;

  // Get buffer size
  if (::EnumPrinters ( aWhichPrinters, NULL, 2, NULL, 0, &dwSizeNeeded, &dwNumItems )) {
    return NS_ERROR_FAILURE;
  }

  // allocate memory
  lpInfo = (LPPRINTER_INFO_2)HeapAlloc ( GetProcessHeap (), HEAP_ZERO_MEMORY, dwSizeNeeded );
  if ( lpInfo == NULL ) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  if (::EnumPrinters ( PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)lpInfo, dwSizeNeeded, &dwSizeNeeded, &dwNumItems) == 0 ) {
    ::HeapFree(GetProcessHeap (), 0, lpInfo);
    return NS_OK;
  }


  for (DWORD i = 0; i < dwNumItems; i++ ) {
    if (_tcscmp(lpInfo[i].pPrinterName, aPrinterName) == 0) {
      aIsFound = PR_TRUE;
      aIsFile  = _tcscmp(lpInfo[i].pPortName, _T("FILE:")) == 0;
      break;
    }
  }

  ::HeapFree(GetProcessHeap (), 0, lpInfo);
#endif
  return NS_OK;
}

Here is the caller graph for this function:

Definition at line 202 of file nsDeviceContextSpecWin.cpp.

{
  PRUnichar * printerName;
  LPTSTR lpPrtName;
  GlobalPrinters::GetInstance()->GetDefaultPrinterName(lpPrtName);
  nsAutoString str;
  NS_CopyNativeToUnicode(nsDependentCString((char *)lpPrtName), str);
  printerName = ToNewUnicode(str);
  free(lpPrtName);
  return printerName;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 277 of file nsDeviceContextSpecWin.cpp.

{
  // for testing
#ifdef DEBUG_rods
  return NS_OK;
#endif

  nsresult rv;

  nsCOMPtr<nsIFilePicker> filePicker = do_CreateInstance("@mozilla.org/filepicker;1", &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIStringBundleService> bundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
  NS_ENSURE_SUCCESS(rv, rv);
  nsCOMPtr<nsIStringBundle> bundle;
  rv = bundleService->CreateBundle(NS_ERROR_GFX_PRINTER_BUNDLE_URL, getter_AddRefs(bundle));
  NS_ENSURE_SUCCESS(rv, rv);

  nsXPIDLString title;
  rv = bundle->GetStringFromName(NS_LITERAL_STRING("PrintToFile").get(), getter_Copies(title));
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIWindowWatcher> wwatch =
    (do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv));
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIDOMWindow> window;
  wwatch->GetActiveWindow(getter_AddRefs(window));

  rv = filePicker->Init(window, title, nsIFilePicker::modeSave);
  NS_ENSURE_SUCCESS(rv, rv);
 
  rv = filePicker->AppendFilters(nsIFilePicker::filterAll);
  NS_ENSURE_SUCCESS(rv, rv);

  PRUnichar* fileName;
  aPS->GetToFileName(&fileName);

  if (fileName) {
    if (*fileName) {
      nsAutoString leafName;
      nsCOMPtr<nsILocalFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
      if (file) {
        rv = file->InitWithPath(nsDependentString(fileName));
        if (NS_SUCCEEDED(rv)) {
          file->GetLeafName(leafName);
          filePicker->SetDisplayDirectory(file);
        }
      }
      if (!leafName.IsEmpty()) {
        rv = filePicker->SetDefaultString(leafName);
      }
      NS_ENSURE_SUCCESS(rv, rv);
    }
    nsMemory::Free(fileName);
  }

  PRInt16 dialogResult;
  filePicker->Show(&dialogResult);

  if (dialogResult == nsIFilePicker::returnCancel) {
    return NS_ERROR_ABORT;
  }

  nsCOMPtr<nsILocalFile> localFile;
  rv = filePicker->GetFile(getter_AddRefs(localFile));
  NS_ENSURE_SUCCESS(rv, rv);
  
  if (dialogResult == nsIFilePicker::returnReplace) {
    // be extra safe and only delete when the file is really a file
    PRBool isFile;
    rv = localFile->IsFile(&isFile);
    if (NS_SUCCEEDED(rv) && isFile) {
      rv = localFile->Remove(PR_FALSE /* recursive delete */);
      NS_ENSURE_SUCCESS(rv, rv);
    }
  }

  nsAutoString unicodePath;
  rv = localFile->GetPath(unicodePath);
  NS_ENSURE_SUCCESS(rv,rv);

  if (unicodePath.IsEmpty()) {
    rv = NS_ERROR_ABORT;
  }

  if (NS_SUCCEEDED(rv)) aPS->SetToFileName(unicodePath.get());

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void MapPaperSizeToNativeEnum ( LPDEVMODE  aDevMode,
PRInt16  aType,
double  aW,
double  aH 
) [static]

Definition at line 545 of file nsDeviceContextSpecWin.cpp.

{

#ifdef DEBUG_rods
  BOOL doingOrientation = aDevMode->dmFields & DM_ORIENTATION;
  BOOL doingPaperSize   = aDevMode->dmFields & DM_PAPERSIZE;
  BOOL doingPaperLength = aDevMode->dmFields & DM_PAPERLENGTH;
  BOOL doingPaperWidth  = aDevMode->dmFields & DM_PAPERWIDTH;
#endif

  PRBool foundEnum = PR_FALSE;
  for (PRInt32 i=0;i<kNumPaperSizes;i++) {
    if (kPaperSizes[i].mWidth == aW && kPaperSizes[i].mHeight == aH) {
      aDevMode->dmPaperSize = kPaperSizes[i].mPaperSize;
      aDevMode->dmFields &= ~DM_PAPERLENGTH;
      aDevMode->dmFields &= ~DM_PAPERWIDTH;
      aDevMode->dmFields |= DM_PAPERSIZE;
      return;
    }
  }

  short width  = 0;
  short height = 0;
  if (aType == nsIPrintSettings::kPaperSizeInches) {
    width  = short(NS_TWIPS_TO_MILLIMETERS(NS_INCHES_TO_TWIPS(float(aW))) / 10);
    height = short(NS_TWIPS_TO_MILLIMETERS(NS_INCHES_TO_TWIPS(float(aH))) / 10);

  } else if (aType == nsIPrintSettings::kPaperSizeMillimeters) {
    width  = short(aW / 10.0);
    height = short(aH / 10.0);
  } else {
    return; // don't set anything
  }

  // width and height is in 
  aDevMode->dmPaperSize   = 0;
  aDevMode->dmPaperWidth  = width;
  aDevMode->dmPaperLength = height;

  aDevMode->dmFields |= DM_PAPERSIZE;
  aDevMode->dmFields |= DM_PAPERLENGTH;
  aDevMode->dmFields |= DM_PAPERWIDTH;
}

Here is the caller graph for this function:

static void SetupDevModeFromSettings ( LPDEVMODE  aDevMode,
nsIPrintSettings aPrintSettings 
) [static]

Definition at line 596 of file nsDeviceContextSpecWin.cpp.

{
  // Setup paper size
  if (aPrintSettings) {
    PRInt16 type;
    aPrintSettings->GetPaperSizeType(&type);
    if (type == nsIPrintSettings::kPaperSizeNativeData) {
      PRInt16 paperEnum;
      aPrintSettings->GetPaperData(&paperEnum);
      aDevMode->dmPaperSize = paperEnum;
      aDevMode->dmFields &= ~DM_PAPERLENGTH;
      aDevMode->dmFields &= ~DM_PAPERWIDTH;
      aDevMode->dmFields |= DM_PAPERSIZE;
    } else {
      PRInt16 unit;
      double width, height;
      aPrintSettings->GetPaperSizeUnit(&unit);
      aPrintSettings->GetPaperWidth(&width);
      aPrintSettings->GetPaperHeight(&height);
      MapPaperSizeToNativeEnum(aDevMode, unit, width, height);
    }

    // Setup Orientation
    PRInt32 orientation;
    aPrintSettings->GetOrientation(&orientation);
    aDevMode->dmOrientation = orientation == nsIPrintSettings::kPortraitOrientation?DMORIENT_PORTRAIT:DMORIENT_LANDSCAPE;
    aDevMode->dmFields |= DM_ORIENTATION;

    // Setup Number of Copies
    PRInt32 copies;
    aPrintSettings->GetNumCopies(&copies);
    aDevMode->dmCopies = copies;
    aDevMode->dmFields |= DM_COPIES;
  }

}

Here is the call graph for this function:


Variable Documentation

HWND gParentWnd = NULL [static]

Definition at line 76 of file nsDeviceContextSpecWin.cpp.

Definition at line 167 of file nsDeviceContextSpecWin.cpp.

Definition at line 122 of file nsDeviceContextSpecWin.cpp.