mPrt = new PrintData();
mPrt = new PrintData(PrintData::eIsPrintPreview);
DocumentViewerImpl::PrintPreview(nsIPrintSettings* aPrintSettings){  if (!mPresShell) {    // A frame that's not displayed can't be printed!    return NS_OK;  }  if (mIsDoingPrinting) {    return NS_ERROR_FAILURE;  }  nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(mContainer));  NS_ASSERTION(docShell, "This has to be a docshell");  // Temporary code for Bug 136185  nsCOMPtr<nsIXULDocument> xulDoc(do_QueryInterface(mDocument));  if (xulDoc) {    ShowPrintErrorDialog(NS_ERROR_GFX_PRINTER_NO_XUL, PR_FALSE);    return NS_ERROR_FAILURE;  }  // Get the webshell for this documentviewer  nsCOMPtr<nsIWebShell> webContainer(do_QueryInterface(mContainer));  // Get the DocShell and see if it is busy  // We can't Print or Print Preview this document if it is still busy  PRUint32 busyFlags = nsIDocShell::BUSY_FLAGS_NONE;  // Preview this document if it is still busy  if (NS_FAILED(docShell->GetBusyFlags(&busyFlags)) ||      busyFlags != nsIDocShell::BUSY_FLAGS_NONE) {    ShowPrintErrorDialog(NS_ERROR_GFX_PRINTER_DOC_IS_BUSY_PP, PR_FALSE);    return NS_ERROR_FAILURE;  }  nsresult rv = NS_OK;#if defined(XP_PC) && defined(DEBUG_rods) && defined(DEBUG_PRINTING)  if (!mIsDoingPrintPreview) {    RemoveFilesInDir(".\\");  }#endif#ifdef NS_PRINT_PREVIEW  if (mIsDoingPrintPreview) {    mOldPrtPreview = mPrtPreview;    mPrtPreview = nsnull;  }  mPrt = new PrintData();  if (!mPrt) {    mIsCreatingPrintPreview = PR_FALSE;    return NS_ERROR_OUT_OF_MEMORY;  }  // Check to see if we need to transfer any of our old values  // over to the new PrintData object  if (mOldPrtPreview) {    mPrt->mOrigZoom     = mOldPrtPreview->mOrigZoom;    mPrt->mOrigTextZoom = mOldPrtPreview->mOrigTextZoom;    mPrt->mOrigDCScale  = mOldPrtPreview->mOrigDCScale;    // maintain the the original presentation if it is there    // by transfering it over to the new PrintData object    if (mOldPrtPreview->HasCachedPres()) {      mPrt->mIsCachingPresentation = mOldPrtPreview->mIsCachingPresentation;      mPrt->mCachedPresObj         = mOldPrtPreview->mCachedPresObj;      // don't want it to get deleted when the mOldPrtPreview is deleted       mOldPrtPreview->mIsCachingPresentation = PR_FALSE;      mOldPrtPreview->mCachedPresObj         = nsnull;    }  } else {    // Get the Original PixelScale in case we need to start changing it    mDeviceContext->GetCanonicalPixelScale(mPrt->mOrigDCScale);  }  // You have to have both a PrintOptions and a PrintSetting to call  // CheckForPrinters.  // The user can pass in a null PrintSettings, but you can only  // create one if you have a PrintOptions.  So we we might as check  // to if we have a PrintOptions first, because we can't do anything  // below without it then inside we check to se if the printSettings  // is null to know if we need to create on.  mPrt->mPrintSettings = aPrintSettings;  mPrt->mPrintOptions  = do_GetService(kPrintOptionsCID, &rv);  if (NS_SUCCEEDED(rv) && mPrt->mPrintOptions) {    // if they don't pass in a PrintSettings, then make one    if (mPrt->mPrintSettings == nsnull) {      mPrt->mPrintOptions->CreatePrintSettings(getter_AddRefs(mPrt->mPrintSettings));    }    NS_ASSERTION(mPrt->mPrintSettings, "You can't PrintPreview without a PrintSettings!");    // Get the default printer name and set it into the PrintSettings    if (NS_FAILED(CheckForPrinters(mPrt->mPrintOptions, mPrt->mPrintSettings, NS_ERROR_GFX_PRINTER_PRINTPREVIEW, PR_FALSE))) {      delete mPrt;      mPrt = nsnull;      return NS_ERROR_FAILURE;    }  }  // Let's print ...  mIsCreatingPrintPreview = PR_TRUE;  mIsDoingPrintPreview    = PR_TRUE;  aPrintSettings->SetIsPrintPreview(mIsDoingPrintPreview);  // Very important! Turn Off scripting  TurnScriptingOn(PR_FALSE);  // Get the currently focused window and cache it  // because the Print Dialog will "steal" focus and later when you try  // to get the currently focused windows it will be NULL  mPrt->mCurrentFocusWin = getter_AddRefs(FindFocusedDOMWindowInternal());  // Check to see if there is a "regular" selection  PRBool isSelection = IsThereARangeSelection(mPrt->mCurrentFocusWin);  // Create a list for storing the WebShells that need to be printed  if (!mPrt->mPrintDocList) {    mPrt->mPrintDocList = new nsVoidArray();    if (!mPrt->mPrintDocList) {      mIsCreatingPrintPreview = PR_FALSE;      mIsDoingPrintPreview    = PR_FALSE;      aPrintSettings->SetIsPrintPreview(mIsDoingPrintPreview);      TurnScriptingOn(PR_TRUE);      return NS_ERROR_OUT_OF_MEMORY;    }  } else {    mPrt->mPrintDocList->Clear();  }  // Add Root Doc to Tree and List  mPrt->mPrintObject             = new PrintObject;  mPrt->mPrintObject->mWebShell  = webContainer;  mPrt->mPrintDocList->AppendElement(mPrt->mPrintObject);  mPrt->mIsParentAFrameSet = IsParentAFrameSet(webContainer);  mPrt->mPrintObject->mFrameType = mPrt->mIsParentAFrameSet ? eFrameSet : eDoc;  // Build the "tree" of PrintObjects  nsCOMPtr<nsIDocShellTreeNode>  parentAsNode(do_QueryInterface(webContainer));  BuildDocTree(parentAsNode, mPrt->mPrintDocList, mPrt->mPrintObject);  // Create the linkage from the suv-docs back to the content element  // in the parent document  MapContentToWebShells(mPrt->mPrintObject, mPrt->mPrintObject);  // Get whether the doc contains a frameset  // Also, check to see if the currently focus webshell  // is a child of this webshell  mPrt->mIsIFrameSelected = IsThereAnIFrameSelected(webContainer, mPrt->mCurrentFocusWin, mPrt->mIsParentAFrameSet);  CheckForHiddenFrameSetFrames();  DUMP_DOC_LIST("\nAfter Mapping------------------------------------------");  // Setup print options for UI  rv = NS_ERROR_FAILURE;  if (mPrt->mPrintSettings != nsnull) {    mPrt->mPrintSettings->GetShrinkToFit(&mPrt->mShrinkToFit);    if (mPrt->mIsParentAFrameSet) {      if (mPrt->mCurrentFocusWin) {        mPrt->mPrintSettings->SetHowToEnableFrameUI(nsIPrintSettings::kFrameEnableAll);      } else {        mPrt->mPrintSettings->SetHowToEnableFrameUI(nsIPrintSettings::kFrameEnableAsIsAndEach);      }    } else {      mPrt->mPrintSettings->SetHowToEnableFrameUI(nsIPrintSettings::kFrameEnableNone);    }    // Now determine how to set up the Frame print UI    mPrt->mPrintSettings->SetPrintOptions(nsIPrintSettings::kEnableSelectionRB, isSelection || mPrt->mIsIFrameSelected);  }#ifdef DEBUG_PRINTING  if (mPrt->mPrintSettings) {    PRInt16 printHowEnable = nsIPrintSettings::kFrameEnableNone;    mPrt->mPrintSettings->GetHowToEnableFrameUI(&printHowEnable);    PRBool val;    mPrt->mPrintSettings->GetPrintOptions(nsIPrintSettings::kEnableSelectionRB, &val);    PRINT_DEBUG_MSG1("********* DocumentViewerImpl::Print *********\n");    PRINT_DEBUG_MSG2("IsParentAFrameSet:   %s \n", PRT_YESNO(mPrt->mIsParentAFrameSet));    PRINT_DEBUG_MSG2("IsIFrameSelected:    %s \n", PRT_YESNO(mPrt->mIsIFrameSelected));    PRINT_DEBUG_MSG2("Main Doc Frame Type: %s \n", gFrameTypesStr[mPrt->mPrintObject->mFrameType]);    PRINT_DEBUG_MSG2("HowToEnableFrameUI:  %s \n", gFrameHowToEnableStr[printHowEnable]);    PRINT_DEBUG_MSG2("EnableSelectionRB:   %s \n", PRT_YESNO(val));    PRINT_DEBUG_MSG1("*********************************************\n");    PRINT_DEBUG_FLUSH  }#endif  PRBool doSilent = PR_TRUE;  nscoord width  = NS_INCHES_TO_TWIPS(8.5);  nscoord height = NS_INCHES_TO_TWIPS(11.0);  nsCOMPtr<nsIDeviceContext> ppDC;  nsCOMPtr<nsIDeviceContextSpecFactory> factory = do_CreateInstance(kDeviceContextSpecFactoryCID);  if (factory) {    nsCOMPtr<nsIDeviceContextSpec> devspec;    nsCOMPtr<nsIDeviceContext> dx;    nsresult rv = factory->CreateDeviceContextSpec(mWindow, aPrintSettings, *getter_AddRefs(devspec), doSilent);    if (NS_SUCCEEDED(rv)) {      rv = mDeviceContext->GetDeviceContextFor(devspec, *getter_AddRefs(ppDC));      if (NS_SUCCEEDED(rv)) {        mDeviceContext->SetAltDevice(ppDC);        if (mPrt->mPrintSettings != nsnull) {          // Shrink to Fit over rides and scaling values          if (!mPrt->mShrinkToFit) {            double scaling;            mPrt->mPrintSettings->GetScaling(&scaling);            mDeviceContext->SetCanonicalPixelScale(float(scaling)*mPrt->mOrigDCScale);          }        }        ppDC->GetDeviceSurfaceDimensions(width, height);      }    }  }  if (doSilent) {    mPrt->mPrintSettings->SetPrintFrameType(nsIPrintSettings::kFramesAsIs);  }  // override any UI that wants to PrintPreview any selection  PRInt16 printRangeType = nsIPrintSettings::kRangeAllPages;  mPrt->mPrintSettings->GetPrintRange(&printRangeType);  if (printRangeType == nsIPrintSettings::kRangeSelection) {    mPrt->mPrintSettings->SetPrintRange(nsIPrintSettings::kRangeAllPages);  }  mPrt->mPrintDC = mDeviceContext;  // Cache original Zoom value and then set it to 1.0  mPrt->mPrintDC->GetTextZoom(mPrt->mOrigTextZoom);  mPrt->mPrintDC->GetZoom(mPrt->mOrigZoom);  mPrt->mPrintDC->SetTextZoom(1.0f);  mPrt->mPrintDC->SetZoom(1.0f);  if (mDeviceContext) {    mDeviceContext->SetUseAltDC(kUseAltDCFor_FONTMETRICS, PR_TRUE);    mDeviceContext->SetUseAltDC(kUseAltDCFor_CREATERC_REFLOW, PR_TRUE);    mDeviceContext->SetUseAltDC(kUseAltDCFor_SURFACE_DIM, PR_TRUE);  }  PRBool cacheOldPres = CheckDocumentForPPCaching();  // If we are caching the Presentation then  // end observing the document BEFORE we do any new reflows  if (cacheOldPres && !mPrt->HasCachedPres()) {    mPrt->SetCacheOldPres(PR_TRUE);    mPresShell->EndObservingDocument();  }  rv = DocumentReadyForPrinting();  mIsCreatingPrintPreview = PR_FALSE;  /* cleaup on failure + notify user */  if (NS_FAILED(rv)) {    if (mPrt) {      delete mPrt;      mPrt = nsnull;    }    /* cleanup done, let's fire-up an error dialog to notify the user     * what went wrong...     */    ShowPrintErrorDialog(rv, PR_FALSE);    TurnScriptingOn(PR_TRUE);    mIsCreatingPrintPreview = PR_FALSE;    mIsDoingPrintPreview    = PR_FALSE;    return rv;  }  // At this point we are done preparing everything  // before it is to be created  // Noew create the new Presentation and display it  InstallNewPresentation();  // PrintPreview was built using the mPrt (code reuse)  // then we assign it over  mPrtPreview = mPrt;  mPrt        = nsnull;  // Turning off the scaling of twips so any of the UI scrollbars  // will not get scaled  nsCOMPtr<nsIPrintPreviewContext> printPreviewContext(do_QueryInterface(mPresContext));  if (printPreviewContext) {    printPreviewContext->SetScalingOfTwips(PR_FALSE);    mDeviceContext->SetCanonicalPixelScale(mPrtPreview->mOrigDCScale);  }#endif // NS_PRINT_PREVIEW  return NS_OK;}