if (GetIsPrinting()) { nsPrintEngine::CloseProgressDialog(aWebProgressListener);
DocumentViewerImpl::PrintPreview(nsIPrintSettings* aPrintSettings,                                  nsIDOMWindow *aChildDOMWin,                                  nsIWebProgressListener* aWebProgressListener){  nsresult rv = NS_OK;#ifdef NS_PRINT_PREVIEW  if (!mPresShell) {    // A frame that's not displayed can't be printed!    return NS_OK;  }  if (mIsDoingPrinting) {    CloseProgressDialog(aWebProgressListener);    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) {    CloseProgressDialog(aWebProgressListener);    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) {    CloseProgressDialog(aWebProgressListener);    ShowPrintErrorDialog(NS_ERROR_GFX_PRINTER_DOC_IS_BUSY_PP, PR_FALSE);    return NS_ERROR_FAILURE;  }#if defined(XP_PC) && defined(EXTENDED_DEBUG_PRINTING)  if (!mIsDoingPrintPreview) {    if (kPrintingLogMod && kPrintingLogMod->level == DUMP_LAYOUT_LEVEL) {      RemoveFilesInDir(".\\");    }  }#endif  if (mIsDoingPrintPreview) {    mOldPrtPreview = mPrtPreview;    mPrtPreview = nsnull;  }  mPrt = new PrintData(PrintData::eIsPrintPreview);  if (!mPrt) {    CloseProgressDialog(aWebProgressListener);    mIsCreatingPrintPreview = PR_FALSE;    return NS_ERROR_OUT_OF_MEMORY;  }  // The WebProgressListener can be QI'ed to nsIPrintingPromptService  // then that means the progress dialog is already being shown.  nsCOMPtr<nsIPrintingPromptService> pps(do_QueryInterface(aWebProgressListener));  mPrt->mProgressDialogIsShown = pps != nsnull;  // 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.  // if they don't pass in a PrintSettings, then get the Global PS  mPrt->mPrintSettings = aPrintSettings;  if (!mPrt->mPrintSettings) {    GetGlobalPrintSettings(getter_AddRefs(mPrt->mPrintSettings));  }  mPrt->mPrintOptions = do_GetService(sPrintOptionsContractID, &rv);  if (NS_SUCCEEDED(rv) && mPrt->mPrintOptions && mPrt->mPrintSettings) {    // Get the default printer name and set it into the PrintSettings    rv = CheckForPrinters(mPrt->mPrintOptions, mPrt->mPrintSettings, NS_ERROR_GFX_PRINTER_NO_PRINTER_AVAILABLE, PR_TRUE);  } else {    NS_ASSERTION(mPrt->mPrintSettings, "You can't Print without a PrintSettings!");    rv = NS_ERROR_FAILURE;  }  if (NS_FAILED(rv)) {    CloseProgressDialog(aWebProgressListener);    delete mPrt;    mPrt = nsnull;    return NS_ERROR_FAILURE;  }  // Let's print ...  mIsCreatingPrintPreview = PR_TRUE;  mIsDoingPrintPreview    = PR_TRUE;  // 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;      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 PR_LOGGING  if (mPrt->mPrintSettings) {    PRInt16 printHowEnable = nsIPrintSettings::kFrameEnableNone;    mPrt->mPrintSettings->GetHowToEnableFrameUI(&printHowEnable);    PRBool val;    mPrt->mPrintSettings->GetPrintOptions(nsIPrintSettings::kEnableSelectionRB, &val);    PR_PL(("********* DocumentViewerImpl::Print *********\n"));    PR_PL(("IsParentAFrameSet:   %s \n", PRT_YESNO(mPrt->mIsParentAFrameSet)));    PR_PL(("IsIFrameSelected:    %s \n", PRT_YESNO(mPrt->mIsIFrameSelected)));    PR_PL(("Main Doc Frame Type: %s \n", gFrameTypesStr[mPrt->mPrintObject->mFrameType]));    PR_PL(("HowToEnableFrameUI:  %s \n", gFrameHowToEnableStr[printHowEnable]));    PR_PL(("EnableSelectionRB:   %s \n", PRT_YESNO(val)));    PR_PL(("*********************************************\n"));  }#endif  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, mPrt->mPrintSettings, *getter_AddRefs(devspec), PR_TRUE);    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);      }    }  }  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();  }  if (aWebProgressListener != nsnull) {    mPrt->mPrintProgressListeners.AppendElement((void*)aWebProgressListener);    NS_ADDREF(aWebProgressListener);  }  PRBool notifyOnInit = PR_FALSE;  ShowPrintProgress(PR_FALSE, notifyOnInit);  if (!notifyOnInit) {    rv = FinishPrintPreview();  } else {    rv = NS_OK;  }#endif // NS_PRINT_PREVIEW  return rv;}