PRInt16 printRangeType = nsIPrintOptions::kRangeAllPages; nsCOMPtr<nsIPrintOptions> printService = do_GetService(kPrintOptionsCID, &rv); if (NS_SUCCEEDED(rv)) { printService->GetPrintRange(&printRangeType); }   if (printRangeType == nsIPrintOptions::kRangeSelection && IsThereARangeSelection(domWinIntl)) {
PRInt16 printRangeType = nsIPrintSettings::kRangeAllPages; if (mPrt->mPrintSettings != nsnull) { mPrt->mPrintSettings->GetPrintRange(&printRangeType); }   if (printRangeType == nsIPrintSettings::kRangeSelection && IsThereARangeSelection(domWinIntl)) {
DocumentViewerImpl::ReflowPrintObject(PrintObject * aPO){  NS_ASSERTION(aPO, "Pointer is null!");  // Now locate the nsIDocument for the WebShell  nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(aPO->mWebShell));  NS_ASSERTION(docShell, "The DocShell can't be NULL!");  nsCOMPtr<nsIPresShell>  wsPresShell;  docShell->GetPresShell(getter_AddRefs(wsPresShell));  NS_ASSERTION(wsPresShell, "The PresShell can't be NULL!");  nsCOMPtr<nsIDocument> document;  wsPresShell->GetDocument(getter_AddRefs(document));  // create the PresContext  PRBool containerIsSet = PR_FALSE;  nsresult rv;  if (mIsCreatingPrintPreview) {    aPO->mPresContext = do_CreateInstance(kPrintPreviewContextCID,&rv);    if (NS_FAILED(rv)) {      return rv;    }  } else {    nsCOMPtr<nsIPrintContext> printcon(do_CreateInstance(kPrintContextCID, &rv));    if (NS_FAILED(rv)) {      return rv;    } else {      aPO->mPresContext = do_QueryInterface(printcon);      if (NS_FAILED(rv)) {        return rv;      }    }  }  // init it with the DC  (aPO->mPresContext)->Init(mPrt->mPrintDocDC);  CreateStyleSet(document, getter_AddRefs(aPO->mStyleSet));  aPO->mPresShell = do_CreateInstance(kPresShellCID, &rv);  if (NS_FAILED(rv)) {    return rv;  }  aPO->mViewManager = do_CreateInstance(kViewManagerCID, &rv);  if (NS_FAILED(rv)) {    return rv;  }  rv = aPO->mViewManager->Init(mPrt->mPrintDocDC);  if (NS_FAILED(rv)) {    return rv;  }  PRInt32 pageWidth, pageHeight;  mPrt->mPrintDocDC->GetDeviceSurfaceDimensions(pageWidth, pageHeight);  PRInt32 width, height;  if (aPO->mContent == nsnull || !aPO->mPrintAsIs ||      (aPO->mPrintAsIs && aPO->mParent != nsnull && !aPO->mParent->mPrintAsIs) ||      (aPO->mFrameType == eIFrame && aPO == mPrt->mSelectedPO)) {    width  = pageWidth;    height = pageHeight;  } else {    width  = aPO->mRect.width;    height = aPO->mRect.height;  }  PRINT_DEBUG_MSG5("In DV::ReflowPrintObject PO: %p (%9s) Setting w,h to %d,%d\n", aPO, gFrameTypesStr[aPO->mFrameType], width, height);  nsCOMPtr<nsIDOMWindowInternal> domWinIntl(do_QueryInterface(mPrt->mPrintDocDW));  // XXX - Hack Alert  // OK,  so ther eis a selection, we will print the entire selection   // on one page and then crop the page.  // This means you can never print any selection that is longer than one page  // put it keeps it from page breaking in the middle of your print of the selection  // (see also nsSimplePageSequence.cpp)  PRInt16 printRangeType = nsIPrintOptions::kRangeAllPages;  nsCOMPtr<nsIPrintOptions> printService =            do_GetService(kPrintOptionsCID, &rv);  if (NS_SUCCEEDED(rv)) {    printService->GetPrintRange(&printRangeType);  }  if (printRangeType == nsIPrintOptions::kRangeSelection && IsThereARangeSelection(domWinIntl)) {    height = 0x0FFFFFFF;  }  nsRect tbounds = nsRect(0, 0, width, height);  // Create a child window of the parent that is our "root view/window"  rv = CallCreateInstance(kViewCID, &aPO->mRootView);  if (NS_FAILED(rv)) {    return rv;  }  rv = (aPO->mRootView)->Init(aPO->mViewManager, tbounds, nsnull);  if (NS_FAILED(rv)) {    return rv;  }#ifdef NS_PRINT_PREVIEW  // Here we decide whether we need scrollbars and   // what the parent will be of the widget  if (mIsCreatingPrintPreview) {    PRBool canCreateScrollbars = PR_FALSE;    nsCOMPtr<nsIWidget> widget = mParentWidget;    // the top PrintObject's widget will always have scrollbars    if (aPO->mParent != nsnull && aPO->mContent) {      nsCOMPtr<nsIFrameManager> frameMan;      aPO->mParent->mPresShell->GetFrameManager(getter_AddRefs(frameMan));      NS_ASSERTION(frameMan, "No Frame manager!");      nsIFrame* frame;      frameMan->GetPrimaryFrameFor(aPO->mContent, &frame);      if (frame != nsnull && (aPO->mFrameType == eIFrame || aPO->mFrameType == eFrame)) {        frame->FirstChild(aPO->mParent->mPresContext, nsnull, &frame);      }      if (frame != nsnull) {        nsIView* view = nsnull;        frame->GetView(aPO->mParent->mPresContext, &view);        if (view != nsnull) {          view->GetWidget(*getter_AddRefs(widget));          canCreateScrollbars = PR_FALSE;        }      }    } else {      canCreateScrollbars = PR_TRUE;    }    rv = aPO->mRootView->CreateWidget(kWidgetCID, nsnull, widget->GetNativeData(NS_NATIVE_WIDGET));    aPO->mRootView->GetWidget(*getter_AddRefs(aPO->mWindow));    aPO->mPresContext->SetPaginatedScrolling(canCreateScrollbars);  }#endif // NS_PRINT_PREVIEW  // Setup hierarchical relationship in view manager  aPO->mViewManager->SetRootView(aPO->mRootView);  aPO->mPresShell->Init(document, aPO->mPresContext,                        aPO->mViewManager, aPO->mStyleSet);  nsCompatibility mode;  mPresContext->GetCompatibilityMode(&mode);  aPO->mPresContext->SetCompatibilityMode(mode);  if (!containerIsSet) {    nsCOMPtr<nsISupports> supps(do_QueryInterface(aPO->mWebShell));    aPO->mPresContext->SetContainer(supps);  }  // get the old history  nsCOMPtr<nsIPresShell> presShell;  nsCOMPtr<nsILayoutHistoryState>  layoutState;  NS_ENSURE_SUCCESS(GetPresShell(*(getter_AddRefs(presShell))), NS_ERROR_FAILURE);  presShell->CaptureHistoryState(getter_AddRefs(layoutState),PR_TRUE);  // set it on the new pres shell  aPO->mPresShell->SetHistoryState(layoutState);  // turn off animated GIFs  if (aPO->mPresContext) {    aPO->mPresContext->GetImageAnimationMode(&aPO->mImgAnimationMode);    aPO->mPresContext->SetImageAnimationMode(imgIContainer::kDontAnimMode);  }  aPO->mPresShell->BeginObservingDocument();  nsMargin margin(0,0,0,0);  if (printService) {    printService->GetMarginInTwips(margin);  }  // initialize it with the default/generic case  nsRect adjRect(aPO->mRect.x != 0?margin.left:0, aPO->mRect.y != 0?margin.top:0, width, height);  // XXX This is an arbitray height,   // but reflow somethimes gets upset when using max int  // basically, we want to reflow a single page that is large   // enough to fit any atomic object like an IFrame  const PRInt32 kFivePagesHigh = 5;  // now, change the value for special cases  if (mPrt->mPrintFrameType == nsIPrintOptions::kEachFrameSep) {    if (aPO->mFrameType == eFrame) {      adjRect.SetRect(0, 0, width, height);    } else if (aPO->mFrameType == eIFrame) {      height = pageHeight*kFivePagesHigh;      adjRect.SetRect(aPO->mRect.x != 0?margin.left:0, aPO->mRect.y != 0?margin.top:0, width, height);    }  } else if (mPrt->mPrintFrameType == nsIPrintOptions::kSelectedFrame) {    if (aPO->mFrameType == eFrame) {      adjRect.SetRect(0, 0, width, height);    } else if (aPO->mFrameType == eIFrame) {      if (aPO == mPrt->mSelectedPO) {        adjRect.x = 0;        adjRect.y = 0;      } else {        height = pageHeight*kFivePagesHigh;        adjRect.SetRect(aPO->mRect.x != 0?margin.left:0, aPO->mRect.y != 0?margin.top:0, width, height);      }    }  }  aPO->mPresContext->SetPageDim(&adjRect);  rv = aPO->mPresShell->InitialReflow(width, height);  if (NS_SUCCEEDED(rv)) {    // Transfer Selection Ranges to the new Print PresShell    nsCOMPtr<nsISelection> selection;    nsCOMPtr<nsISelection> selectionPS;    nsresult rvv = GetDocumentSelection(getter_AddRefs(selection), wsPresShell);    if (NS_SUCCEEDED(rvv) && selection) {      rvv = GetDocumentSelection(getter_AddRefs(selectionPS), aPO->mPresShell);      if (NS_SUCCEEDED(rvv) && selectionPS) {        PRInt32 cnt;        selection->GetRangeCount(&cnt);        PRInt32 inx;        for (inx=0;inx<cnt;inx++) {          nsCOMPtr<nsIDOMRange> range;          if (NS_SUCCEEDED(selection->GetRangeAt(inx, getter_AddRefs(range)))) {            selectionPS->AddRange(range);          }        }      }    }        #ifdef DEBUG_rods    {      char * docStr;      char * urlStr;      GetDocTitleAndURL(aPO, docStr, urlStr);      char filename[256];      sprintf(filename, "dump_%d.txt", gDumpFileNameCnt++);      // Dump all the frames and view to a a file      FILE * fd = fopen(filename, "w");      if (fd) {        nsIFrame  *theRootFrame;        aPO->mPresShell->GetRootFrame(&theRootFrame);        fprintf(fd, "Title: %s\n", docStr?docStr:"");        fprintf(fd, "URL:   %s\n", urlStr?urlStr:"");        fprintf(fd, "--------------- Frames ----------------\n");        nsCOMPtr<nsIRenderingContext> renderingContext;        mPrt->mPrintDocDC->CreateRenderingContext(*getter_AddRefs(renderingContext));        DumpFrames(fd, aPO->mPresContext, renderingContext, theRootFrame, 0);        fprintf(fd, "---------------------------------------\n\n");        fprintf(fd, "--------------- Views From Root Frame----------------\n");        nsIView * v;        theRootFrame->GetView(aPO->mPresContext, &v);        if (v) {          v->List(fd);        } else {          printf("View is null!\n");        }        nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(aPO->mWebShell));        if (docShell) {          fprintf(fd, "--------------- All Views ----------------\n");          DumpViews(docShell, fd);          fprintf(fd, "---------------------------------------\n\n");        }        fclose(fd);      }      if (docStr) nsMemory::Free(docStr);      if (urlStr) nsMemory::Free(urlStr);    }#endif  }  aPO->mPresShell->EndObservingDocument();  return rv;}