nsCOMPtr<nsIPrintSettings> printSettings; #ifdef NS_DEBUG nsresult rv = NS_ERROR_FAILURE;  mDebugFile = aDebugFile;   printSettings = aPrintSettings; nsCOMPtr<nsIPrintOptions> printOptions = do_GetService(kPrintOptionsCID, &rv); if (NS_SUCCEEDED(rv)) {  if (printSettings == nsnull) { printOptions->CreatePrintSettings(getter_AddRefs(printSettings)); } NS_ASSERTION(printSettings, "You can't PrintPreview without a PrintSettings!"); } if (printSettings) printSettings->SetPrintSilent(aSilent);
DocumentViewerImpl::Print(nsIPrintSettings*       aPrintSettings,                          nsIWebProgressListener* aWebProgressListener){#ifdef DEBUG_PRINTING  // need for capturing result on each doc and sub-doc that is printed  gDumpFileNameCnt   = 0;  gDumpLOFileNameCnt = 0;#endif  nsresult rv = NS_ERROR_FAILURE;  if (mIsDoingPrintPreview) {    PRBool okToPrint = PR_FALSE;    nsCOMPtr<nsIPref> prefs(do_GetService(NS_PREF_CONTRACTID));    if (prefs) {      prefs->GetBoolPref("print.whileInPrintPreview", &okToPrint);    }    if (!okToPrint) {      ShowPrintErrorDialog(NS_ERROR_GFX_PRINTER_PRINT_WHILE_PREVIEW, PR_FALSE);      return NS_OK;    }  }  // 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 (mIsDoingPrinting) {    // Let the user know we are not ready to print.    rv = NS_ERROR_NOT_AVAILABLE;    ShowPrintErrorDialog(rv);    return rv;  }    mPrt = new PrintData();  if (mPrt == nsnull) {    return NS_ERROR_OUT_OF_MEMORY;  }  // if they don't pass in a PrintSettings, then make one  // it will have all the default values  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_NO_PRINTER_AVAILABLE, PR_TRUE))) {      delete mPrt;      mPrt = nsnull;      return NS_ERROR_FAILURE;    }  }  mPrt->mPrintSettings->SetIsCancelled(PR_FALSE);  mPrt->mPrintSettings->GetShrinkToFit(&mPrt->mShrinkToFit);  // Let's print ...  mIsDoingPrinting = PR_TRUE;  if (aWebProgressListener != nsnull) {    mPrt->mPrintProgressListeners.AppendElement((void*)aWebProgressListener);    NS_ADDREF(aWebProgressListener);  }    // 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) {      mIsDoingPrinting = PR_FALSE;      delete mPrt;      mPrt = nsnull;      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------------------------------------------");  rv = NS_ERROR_FAILURE;  // Setup print options for UI  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  /* create factory (incl. create print dialog) */  nsCOMPtr<nsIDeviceContextSpecFactory> factory =          do_CreateInstance(kDeviceContextSpecFactoryCID, &rv);            if (NS_SUCCEEDED(rv)) {#ifdef DEBUG_dcone    printf("PRINT JOB STARTING\n");#endif    nsCOMPtr<nsIDeviceContextSpec> devspec;    nsCOMPtr<nsIDeviceContext> dx;    mPrt->mPrintDC = nsnull; // XXX why?#ifdef NS_DEBUG    mPrt->mDebugFilePtr = mDebugFile;#endif    PRBool printSilently;    mPrt->mPrintSettings->GetPrintSilent(&printSilently);    rv = factory->CreateDeviceContextSpec(mWindow, mPrt->mPrintSettings, *getter_AddRefs(devspec), printSilently);    if (NS_SUCCEEDED(rv)) {      rv = mPresContext->GetDeviceContext(getter_AddRefs(dx));      if (NS_SUCCEEDED(rv)) {        rv = dx->GetDeviceContextFor(devspec, *getter_AddRefs(mPrt->mPrintDC));        if (NS_SUCCEEDED(rv)) {          // Get the Original PixelScale incase we need to start changing it          mPrt->mPrintDC->GetCanonicalPixelScale(mPrt->mOrigDCScale);          // Shrink to Fit over rides and scaling values          if (!mPrt->mShrinkToFit) {            double scaling;            mPrt->mPrintSettings->GetScaling(&scaling);            if (scaling <= 1.0) {              mPrt->mPrintDC->SetCanonicalPixelScale(float(scaling)*mPrt->mOrigDCScale);            }          }          if(webContainer) {#ifdef DEBUG_dcone            float   a1,a2;            PRInt32 i1,i2;            printf("CRITICAL PRINTING INFORMATION\n");            // DEVICE CONTEXT INFORMATION from PresContext            printf("DeviceContext of Presentation Context(%x)\n",dx);            dx->GetDevUnitsToTwips(a1);            dx->GetTwipsToDevUnits(a2);            printf("    DevToTwips = %f TwipToDev = %f\n",a1,a2);            dx->GetAppUnitsToDevUnits(a1);            dx->GetDevUnitsToAppUnits(a2);            printf("    AppUnitsToDev = %f DevUnitsToApp = %f\n",a1,a2);            dx->GetCanonicalPixelScale(a1);            printf("    GetCanonicalPixelScale = %f\n",a1);            dx->GetScrollBarDimensions(a1, a2);            printf("    ScrollBar x = %f y = %f\n",a1,a2);            dx->GetZoom(a1);            printf("    Zoom = %f\n",a1);            dx->GetDepth((PRUint32&)i1);            printf("    Depth = %d\n",i1);            dx->GetDeviceSurfaceDimensions(i1,i2);            printf("    DeviceDimension w = %d h = %d\n",i1,i2);            // DEVICE CONTEXT INFORMATION            printf("DeviceContext created for print(%x)\n",mPrt->mPrintDC);            mPrt->mPrintDC->GetDevUnitsToTwips(a1);            mPrt->mPrintDC->GetTwipsToDevUnits(a2);            printf("    DevToTwips = %f TwipToDev = %f\n",a1,a2);            mPrt->mPrintDC->GetAppUnitsToDevUnits(a1);            mPrt->mPrintDC->GetDevUnitsToAppUnits(a2);            printf("    AppUnitsToDev = %f DevUnitsToApp = %f\n",a1,a2);            mPrt->mPrintDC->GetCanonicalPixelScale(a1);            printf("    GetCanonicalPixelScale = %f\n",a1);            mPrt->mPrintDC->GetScrollBarDimensions(a1, a2);            printf("    ScrollBar x = %f y = %f\n",a1,a2);            mPrt->mPrintDC->GetZoom(a1);            printf("    Zoom = %f\n",a1);            mPrt->mPrintDC->GetDepth((PRUint32&)i1);            printf("    Depth = %d\n",i1);            mPrt->mPrintDC->GetDeviceSurfaceDimensions(i1,i2);            printf("    DeviceDimension w = %d h = %d\n",i1,i2);#endif /* DEBUG_dcone */            // Always check and set the print settings first and then fall back            // onto the PrintService if there isn't a PrintSettings            //            // Posiible Usage values:            //   nsIPrintSettings::kUseInternalDefault            //   nsIPrintSettings::kUseSettingWhenPossible            //            // NOTE: The consts are the same for PrintSettings and PrintSettings            PRInt16 printFrameTypeUsage = nsIPrintSettings::kUseSettingWhenPossible;            mPrt->mPrintSettings->GetPrintFrameTypeUsage(&printFrameTypeUsage);            // Ok, see if we are going to use our value and override the default            if (printFrameTypeUsage == nsIPrintSettings::kUseSettingWhenPossible) {              // Get the Print Options/Settings PrintFrameType to see what is preferred              PRInt16 printFrameType = nsIPrintSettings::kEachFrameSep;              mPrt->mPrintSettings->GetPrintFrameType(&printFrameType);              // Don't let anybody do something stupid like try to set it to              // kNoFrames when we are printing a FrameSet              if (printFrameType == nsIPrintSettings::kNoFrames) {                mPrt->mPrintFrameType = nsIPrintSettings::kEachFrameSep;                mPrt->mPrintSettings->SetPrintFrameType(mPrt->mPrintFrameType);              } else {                // First find out from the PrinService what options are available                 // to us for Printing FrameSets                PRInt16 howToEnableFrameUI;                mPrt->mPrintSettings->GetHowToEnableFrameUI(&howToEnableFrameUI);                if (howToEnableFrameUI != nsIPrintSettings::kFrameEnableNone) {                  switch (howToEnableFrameUI) {                    case nsIPrintSettings::kFrameEnableAll:                      mPrt->mPrintFrameType = printFrameType;                      break;                    case nsIPrintSettings::kFrameEnableAsIsAndEach:                      if (printFrameType != nsIPrintSettings::kSelectedFrame) {                        mPrt->mPrintFrameType = printFrameType;                      } else { // revert back to a good value                        mPrt->mPrintFrameType = nsIPrintSettings::kEachFrameSep;                      }                      break;                  } // switch                  mPrt->mPrintSettings->SetPrintFrameType(mPrt->mPrintFrameType);                }              }            }            if (mPrt->mPrintOptions) {              // check to see if we are printing to a file              PRBool isPrintToFile = PR_FALSE;              mPrt->mPrintSettings->GetPrintToFile(&isPrintToFile);              if (isPrintToFile) {              // On some platforms The BeginDocument needs to know the name of the file              // and it uses the PrintService to get it, so we need to set it into the PrintService here                PRUnichar* fileName;                mPrt->mPrintSettings->GetToFileName(&fileName);                if (fileName != nsnull) {                  mPrt->mPrintOptions->SetPrintToFile(PR_TRUE);                  mPrt->mPrintOptions->SetToFileName(fileName);                  nsMemory::Free(fileName);                }              } else {                mPrt->mPrintOptions->SetPrintToFile(PR_FALSE);                mPrt->mPrintOptions->SetToFileName(nsnull);              }            }            PRUnichar * docTitleStr;            PRUnichar * docURLStr;            GetDisplayTitleAndURL(mPrt->mPrintObject, mPrt->mPrintSettings, &docTitleStr, &docURLStr, eDocTitleDefURLDoc);             // BeginDocument may pass back a FAILURE code            // i.e. On Windows, if you are printing to a file and hit "Cancel"             //      to the "File Name" dialog, this comes back as an error            // Don't start printing when regression test are executed              rv = mPrt->mDebugFilePtr ? NS_OK: mPrt->mPrintDC->BeginDocument(docTitleStr);            PRINT_DEBUG_MSG1("****************** Begin Document ************************\n");            if (docTitleStr) nsMemory::Free(docTitleStr);            if (docURLStr) nsMemory::Free(docURLStr);            if (NS_SUCCEEDED(rv)) {              DoPrintProgress(PR_TRUE);              // Print listener setup...              if (mPrt != nsnull) {                mPrt->OnStartPrinting();                  }              //              // The mIsPrinting flag is set when the ImageGroup observer is              // notified that images must be loaded as a result of the               // InitialReflow...              //              if(!mIsPrinting  || mPrt->mDebugFilePtr) {                rv = DocumentReadyForPrinting();                PRINT_DEBUG_MSG1("PRINT JOB ENDING, OBSERVER WAS NOT CALLED\n");              } else {                // use the observer mechanism to finish the printing                PRINT_DEBUG_MSG1("PRINTING OBSERVER STARTED\n");              }            }          }        }      }        } else {      mPrt->mPrintSettings->SetIsCancelled(PR_TRUE);      mPrt->mPrintOptions->SetIsCancelled(PR_TRUE);    }  }   /* cleaup on failure + notify user */  if (NS_FAILED(rv)) {    /* cleanup... */    if (mPagePrintTimer) {      mPagePrintTimer->Stop();      NS_RELEASE(mPagePrintTimer);    }        if (mPrt) {      mPrt->OnEndPrinting(rv);      delete mPrt;      mPrt = nsnull;    }    mIsDoingPrinting = PR_FALSE;    /* cleanup done, let's fire-up an error dialog to notify the user     * what went wrong...      *      * When rv == NS_ERROR_ABORT, it means we want out of the      * print job without displaying any error messages     */    if (rv != NS_ERROR_ABORT) {      ShowPrintErrorDialog(rv);    }  }        return rv;}