DocumentViewerImpl::PrintPreview()
DocumentViewerImpl::PrintPreview(nsIPrintSettings* aPrintSettings)
DocumentViewerImpl::PrintPreview(){  nsresult rv = NS_OK;#ifdef NS_PRINT_PREVIEW  // if we are printing another URL, then exit  // the reason we check here is because this method can be called while   // another is still in here (the printing dialog is a good example).  // the only time we can print more than one job at a time is the regression tests  if (mIsDoingPrintPreview) {#if 0    // Let the user know we are not ready to print.    rv = NS_ERROR_NOT_AVAILABLE;    ShowPrintErrorDialog(rv);    return rv;#else    ReturnToGalleyPresentation();    return NS_OK;#endif  }    // Let's print ...  mIsCreatingPrintPreview = PR_TRUE;  mIsDoingPrintPreview    = PR_TRUE;  mPrt = new PrintData();  if (mPrt == nsnull) {    mIsCreatingPrintPreview = PR_FALSE;    return NS_ERROR_OUT_OF_MEMORY;  }  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 == nsnull) {    mPrt->mPrintDocList = new nsVoidArray();    if (mPrt->mPrintDocList == nsnull) {      mIsCreatingPrintPreview = PR_FALSE;      mIsDoingPrintPreview    = PR_FALSE;      TurnScriptingOn(PR_TRUE);      return NS_ERROR_FAILURE;    }  } else {    mPrt->mPrintDocList->Clear();  }  // Get the webshell for this documentviewer  nsCOMPtr<nsIWebShell> webContainer(do_QueryInterface(mContainer));  // 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);  DUMP_DOC_LIST("\nAfter Mapping------------------------------------------");  // Setup print options for UI  rv = NS_ERROR_FAILURE;  nsCOMPtr<nsIPrintOptions> printService =            do_GetService(kPrintOptionsCID, &rv);  if (NS_SUCCEEDED(rv) && printService) {    if (mPrt->mIsParentAFrameSet) {      if (mPrt->mCurrentFocusWin) {        printService->SetHowToEnableFrameUI(nsIPrintOptions::kFrameEnableAll);      } else {        printService->SetHowToEnableFrameUI(nsIPrintOptions::kFrameEnableAsIsAndEach);      }    } else {      printService->SetHowToEnableFrameUI(nsIPrintOptions::kFrameEnableNone);    }    // Now determine how to set up the Frame print UI    printService->SetPrintOptions(nsIPrintOptions::kPrintOptionsEnableSelectionRB, isSelection || mPrt->mIsIFrameSelected);  }#ifdef DEBUG_PRINTING  if (printService) {    PRInt16 printHowEnable = nsIPrintOptions::kFrameEnableNone;     printService->GetHowToEnableFrameUI(&printHowEnable);    PRBool val;    printService->GetPrintOptions(nsIPrintOptions::kPrintOptionsEnableSelectionRB, &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) {    nsIDeviceContextSpec *devspec = nsnull;    nsCOMPtr<nsIDeviceContext> dx;    factory->CreateDeviceContextSpec(mWindow, devspec, doSilent);    if (nsnull != devspec) {      nsresult rv = mDeviceContext->GetDeviceContextFor(devspec, *getter_AddRefs(ppDC));       if (NS_SUCCEEDED(rv)) {        mDeviceContext->SetAltDevice(ppDC);        if (printService) {          double scaling;          printService->GetScaling(&scaling);          if (scaling < 1.0) {            mDeviceContext->SetCanonicalPixelScale(float(scaling));          }        }        ppDC->GetDeviceSurfaceDimensions(width, height);        NS_RELEASE(devspec);      }    }  }  if (printService && doSilent) {    //printService->SetPrintRange(nsIPrintOptions::kFramesAsIs);    printService->SetPrintFrameType(nsIPrintOptions::kFramesAsIs);  }  mPrt->mPrintDC     = mDeviceContext; // XXX why?  mPrt->mFilePointer = nsnull;  if (mDeviceContext) {    mDeviceContext->SetUseAltDC(kUseAltDCFor_FONTMETRICS, PR_TRUE);    mDeviceContext->SetUseAltDC(kUseAltDCFor_CREATE_RC, PR_TRUE);    mDeviceContext->SetUseAltDC(kUseAltDCFor_SURFACE_DIM, PR_TRUE);  }  rv = DocumentReadyForPrinting();  mIsCreatingPrintPreview = PR_FALSE;  /* cleaup on failure + notify user */  if (NS_FAILED(rv)) {    if (mPrt) {      mPrt->OnEndPrinting(rv);      delete mPrt;      mPrt = nsnull;    }    /* cleanup done, let's fire-up an error dialog to notify the user     * what went wrong...      */       ShowPrintErrorDialog(rv);    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;#endif // NS_PRINT_PREVIEW  return NS_OK;}