printService->SetPageRange(startPageNum, endPageNum);
printService->SetStartPageRange(startPageNum); printService->SetEndPageRange(endPageNum);
DocumentViewerImpl::PrintContent(nsIWebShell *      aParent,                                 nsIDeviceContext * aDContext){  NS_ENSURE_ARG_POINTER(aParent);  NS_ENSURE_ARG_POINTER(aDContext);  nsCOMPtr<nsIStyleSet>       ss;  nsCOMPtr<nsIViewManager>    vm;  PRInt32                     width, height;  nsresult                    rv;  PRInt32                     count,i;  nsCOMPtr<nsIContentViewer>  viewer;  nsCOMPtr<nsIDocShellTreeNode> parentAsNode(do_QueryInterface(aParent));  // See if if the incoming doc is the root document  nsCOMPtr<nsIDocShellTreeItem> parentAsItem(do_QueryInterface(aParent));  nsCOMPtr<nsIDocShellTreeItem> parentsParentDoc;  parentAsItem->GetParent(getter_AddRefs(parentsParentDoc));  // When it is the top level document we need to check   // to see if it contains a frameset. If it does, then   // we only want to print the doc's children and not the document itself  // For anything else we always print all the children and the document  // for example, if the doc contains an IFRAME we eant to print the child  // document (the IFRAME) and then the rest of the document.  //  // XXX we really need to search the frame tree, and not the content  // but there is no way to distinguish between IFRAMEs and FRAMEs  // with the GetFrameType call.  // Bug 53459 has been files so we can eventually distinguish   // between IFRAME frames and FRAME frames  PRBool doesContainFrameSet = PR_FALSE;  // only check to see if there is a frameset if there is   // NO parent doc for this doc. meaning this parent is the root doc  if (!parentsParentDoc) {    nsCOMPtr<nsIPresShell> shell;    mPresContext->GetShell(getter_AddRefs(shell));    if (shell) {      nsCOMPtr<nsIDocument> doc;      shell->GetDocument(getter_AddRefs(doc));      if (doc) {        if (mSelectionDoc) {          doc = mSelectionDoc;        }        nsCOMPtr<nsIContent> rootContent = getter_AddRefs(doc->GetRootContent());        if (rootContent) {          if (NS_SUCCEEDED(FindFrameSetWithIID(rootContent, NS_GET_IID(nsIDOMHTMLFrameSetElement)))) {            doesContainFrameSet = PR_TRUE;          }        }      }    }  }  // print any child documents  // like frameset frames or iframes  parentAsNode->GetChildCount(&count);  if(count> 0) {          for(i=0;i<count;i++) {      nsCOMPtr<nsIDocShellTreeItem> child;      parentAsNode->GetChildAt(i, getter_AddRefs(child));      nsCOMPtr<nsIDocShell> childAsShell(do_QueryInterface(child));      childAsShell->GetContentViewer(getter_AddRefs(viewer));      nsCOMPtr<nsIContentViewerFile> viewerFile(do_QueryInterface(viewer));      if (viewerFile) {        nsCOMPtr<nsIWebShell> childWebShell(do_QueryInterface(child));        NS_ENSURE_SUCCESS(viewerFile->PrintContent(childWebShell,aDContext), NS_ERROR_FAILURE);      }    }  } if (!doesContainFrameSet) {    NS_ENSURE_SUCCESS( aDContext->BeginDocument(), NS_ERROR_FAILURE );    aDContext->GetDeviceSurfaceDimensions(width, height);    // 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)    if (IsThereASelection()) {      height = 0x0FFFFFFF;    }    nsCOMPtr<nsIPresContext> cx;    nsCOMPtr<nsIPrintContext> printcon;    rv = NS_NewPrintContext(getter_AddRefs(printcon));    if (NS_FAILED(rv)) {      return rv;    } else {      rv = printcon->QueryInterface(NS_GET_IID(nsIPresContext), getter_AddRefs(cx));      if (NS_FAILED(rv)) {        return rv;      }    }    cx->Init(aDContext);    CreateStyleSet(mDocument, getter_AddRefs(ss));    nsCOMPtr<nsIPresShell> ps;    rv = NS_NewPresShell(getter_AddRefs(ps));    if (NS_FAILED(rv)) {      return rv;    }    rv = nsComponentManager::CreateInstance(kViewManagerCID,                                            nsnull,                                            NS_GET_IID(nsIViewManager),                                            (void **)getter_AddRefs(vm));    if (NS_FAILED(rv)) {      return rv;    }    rv = vm->Init(aDContext);    if (NS_FAILED(rv)) {      return rv;    }    nsRect tbounds = nsRect(0, 0, width, height);    // Create a child window of the parent that is our "root view/window"    nsIView* rootView;    rv = nsComponentManager::CreateInstance(kViewCID, nsnull, NS_GET_IID(nsIView), (void **)&rootView);    if (NS_FAILED(rv)) {      return rv;    }    rv = rootView->Init(vm, tbounds, nsnull);    if (NS_FAILED(rv)) {      return rv;    }    // Setup hierarchical relationship in view manager    vm->SetRootView(rootView);    ps->Init(mDocument, cx, vm, ss);    nsCompatibility mode;    mPresContext->GetCompatibilityMode(&mode);    cx->SetCompatibilityMode(mode);    cx->SetContainer(aParent);    // 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);    ps->BeginObservingDocument();    //lay it out...    ps->InitialReflow(width, height);    // Transfer Selection Ranges to the new Print PresShell    nsCOMPtr<nsISelection> selection;    nsCOMPtr<nsISelection> selectionPS;    GetDocumentSelection(getter_AddRefs(selection));    if (selection) {      GetDocumentSelection(ps, getter_AddRefs(selectionPS));      if (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);          }        }      }    }              // update the history from the old presentation shell    nsCOMPtr<nsIFrameManager> fm;    rv = ps->GetFrameManager(getter_AddRefs(fm));    if(NS_SUCCEEDED(rv) && fm) {      nsIFrame* root;      ps->GetRootFrame(&root);      fm->RestoreFrameState(cx, root, layoutState);    }    ps->EndObservingDocument();    // Ask the page sequence frame to print all the pages    nsIPageSequenceFrame* pageSequence;    //     ps->GetPageSequenceFrame(&pageSequence);    NS_ASSERTION(nsnull != pageSequence, "no page sequence frame");         if (nsnull != mFilePointer) {      // output the regression test      nsIFrameDebug* fdbg;      nsIFrame* root;      ps->GetRootFrame(&root);      if (NS_SUCCEEDED(root->QueryInterface(NS_GET_IID(nsIFrameDebug), (void**) &fdbg))) {        fdbg->DumpRegressionData(cx, mFilePointer, 0, PR_TRUE);      }      fclose(mFilePointer);          } else {      nsIFrame* rootFrame;      ps->GetRootFrame(&rootFrame);#if defined(DEBUG_rods) || defined(DEBUG_dcone)      DumpLayoutData(cx, aDContext, rootFrame, aParent);#endif      nsPrintRange printRangeType = ePrintRange_AllPages;      NS_WITH_SERVICE(nsIPrintOptions, printService, kPrintOptionsCID, &rv);      if (NS_SUCCEEDED(rv) && printService) {        PRInt32 printType;        printService->GetPrintRange(&printType);        printRangeType = (nsPrintRange)printType;        // get the document title        const nsString* docTitle = mDocument->GetDocumentTitle();        if( docTitle != nsnull) {          PRUnichar * docStr = docTitle->ToNewUnicode();          printService->SetTitle(docStr);          nsMemory::Free(docStr);        }        // Get Document URL String        nsCOMPtr<nsIURI> url(getter_AddRefs(mDocument->GetDocumentURL()));        char * urlCStr;        url->GetSpec(&urlCStr);        nsAutoString urlStr;        urlStr.AssignWithConversion(urlCStr);        PRUnichar * urlUStr = urlStr.ToNewUnicode();        printService->SetURL(urlUStr);        nsMemory::Free(urlUStr);        nsMemory::Free(urlCStr);        if (ePrintRange_Selection == printRangeType) {          cx->SetIsRenderingOnlySelection(PR_TRUE);          // temporarily creating rendering context          // which is needed to dinf the selection frames          nsCOMPtr<nsIRenderingContext> rc;          aDContext->CreateRenderingContext(*getter_AddRefs(rc));          // find the starting and ending page numbers          // via the selection          nsIFrame* startFrame;          nsIFrame* endFrame;          PRInt32   startPageNum;          PRInt32   endPageNum;          nsRect    startRect;          nsRect    endRect;          rv = GetPageRangeForSelection(ps, cx, *rc, selectionPS, pageSequence,                                         &startFrame, startPageNum, startRect,                                         &endFrame, endPageNum, endRect);          if (NS_SUCCEEDED(rv)) {            printService->SetPageRange(startPageNum, endPageNum);            if (startPageNum == endPageNum) {              nsIFrame * seqFrame;              if (NS_FAILED(pageSequence->QueryInterface(NS_GET_IID(nsIFrame), (void **)&seqFrame))) {                return NS_ERROR_FAILURE;              }              nsRect rect(0,0,0,0);              nsRect areaRect;              nsIFrame * areaFrame = FindFrameByType(cx, startFrame, nsHTMLAtoms::body, rect, areaRect);              if (areaFrame) {                areaRect.y = areaRect.y - startRect.y;                areaFrame->SetRect(cx, areaRect);              }            }          }        }        nsIFrame * seqFrame;        if (NS_FAILED(pageSequence->QueryInterface(NS_GET_IID(nsIFrame), (void **)&seqFrame))) {          return NS_ERROR_FAILURE;        }        nsRect srect;        seqFrame->GetRect(srect);        nsRect r;        rootView->GetBounds(r);        r.height = srect.height;        rootView->SetBounds(r);        rootFrame->GetRect(r);        r.height = srect.height;        rootFrame->SetRect(cx, r);        pageSequence->Print(cx, printService, nsnull);      } else {        // not sure what to do here!        return NS_ERROR_FAILURE;      }    }    aDContext->EndDocument();    ps->EndObservingDocument();  }  return NS_OK;}