DocumentViewerImpl::Init(nsIWidget* aParentWidget, nsIDeviceContext* aDeviceContext, const nsRect& aBounds) { NS_ASSERTION(aParentWidget != nsnull, "Null aParentWidget");
void Init(DocumentViewerImpl* aDocViewerImpl, nsIPresContext*     aPresContext, nsIPrintSettings*   aPrintSettings, PrintObject*        aPO, PRUint32            aDelay) { NS_IF_RELEASE(mDocViewer); mDocViewer       = aDocViewerImpl; NS_ADDREF(mDocViewer);
DocumentViewerImpl::Init(nsIWidget* aParentWidget,                         nsIDeviceContext* aDeviceContext,                         const nsRect& aBounds){  NS_ASSERTION(aParentWidget != nsnull, "Null aParentWidget");#ifdef NS_PRINT_PREVIEW  mParentWidget = aParentWidget; // not ref counted#endif  nsresult rv;  NS_ENSURE_TRUE(mDocument, NS_ERROR_NULL_POINTER);  mDeviceContext = dont_QueryInterface(aDeviceContext);#ifdef NS_PRINT_PREVIEW  // Clear PrintPreview Alternate Device  if (mDeviceContext) {    mDeviceContext->SetAltDevice(nsnull);    mDeviceContext->SetCanonicalPixelScale(1.0);  }#endif  PRBool makeCX = PR_FALSE;  if (!mPresContext) {    // Create presentation context    if (mIsCreatingPrintPreview) {      mPresContext = do_CreateInstance(kPrintPreviewContextCID,&rv);    } else {      mPresContext = do_CreateInstance(kGalleyContextCID,&rv);    }    if (NS_FAILED(rv)) return rv;    mPresContext->Init(aDeviceContext); #ifdef NS_PRINT_PREVIEW    makeCX = !mIsDoingPrintPreview; // needs to be true except when we are already in PP#else    makeCX = PR_TRUE;#endif  }  nsCOMPtr<nsIInterfaceRequestor> requestor(do_QueryInterface(mContainer));  if (requestor) {    nsCOMPtr<nsILinkHandler> linkHandler;    requestor->GetInterface(NS_GET_IID(nsILinkHandler),        getter_AddRefs(linkHandler));    mPresContext->SetContainer(mContainer);    mPresContext->SetLinkHandler(linkHandler);    if (!mIsDoingPrintPreview) {      // Set script-context-owner in the document      nsCOMPtr<nsIScriptGlobalObjectOwner> owner;      requestor->GetInterface(NS_GET_IID(nsIScriptGlobalObjectOwner),                              getter_AddRefs(owner));      if (owner) {        nsCOMPtr<nsIScriptGlobalObject> global;        owner->GetScriptGlobalObject(getter_AddRefs(global));        if (global) {          mDocument->SetScriptGlobalObject(global);          nsCOMPtr<nsIDOMDocument> domdoc(do_QueryInterface(mDocument));          if (domdoc) {            global->SetNewDocument(domdoc, PR_TRUE);          }        }      }    }  }  // Create the ViewManager and Root View...  rv = MakeWindow(aParentWidget, aBounds);  Hide();  if (NS_FAILED(rv)) return rv;  // Create the style set...  nsIStyleSet* styleSet;  rv = CreateStyleSet(mDocument, &styleSet);  if (NS_FAILED(rv)) return rv;    // Now make the shell for the document    rv = mDocument->CreateShell(mPresContext, mViewManager, styleSet,                                getter_AddRefs(mPresShell));    NS_RELEASE(styleSet);  if (NS_FAILED(rv)) return rv;  mPresShell->BeginObservingDocument();      // Initialize our view manager      nsRect bounds;      mWindow->GetBounds(bounds);      nscoord width = bounds.width;      nscoord height = bounds.height;      float p2t;      mPresContext->GetPixelsToTwips(&p2t);      width = NSIntPixelsToTwips(width, p2t);      height = NSIntPixelsToTwips(height, p2t);      mViewManager->DisableRefresh();      mViewManager->SetWindowDimensions(width, height);      /* Setup default view manager background color */      /* This may be overridden by the docshell with the background color for the         last document loaded into the docshell */      nscolor bgcolor = NS_RGB(0, 0, 0);      mPresContext->GetDefaultBackgroundColor(&bgcolor);      mViewManager->SetDefaultBackgroundColor(bgcolor);      if (!makeCX) {        // Make shell an observer for next time        // XXX - we observe the docuement always, see above after preshell is created        // mPresShell->BeginObservingDocument();//XXX I don't think this should be done *here*; and why paint nothing//(which turns out to cause black flashes!)???        // Resize-reflow this time        mPresShell->InitialReflow(width, height);        // Now trigger a refresh        if (mEnableRendering) {          mViewManager->EnableRefresh(NS_VMREFRESH_IMMEDIATE);        }      }  // now register ourselves as a selection listener, so that we get called  // when the selection changes in the window  nsDocViewerSelectionListener *selectionListener;  NS_NEWXPCOM(selectionListener, nsDocViewerSelectionListener);  if (!selectionListener) return NS_ERROR_OUT_OF_MEMORY;  selectionListener->Init(this);    // this is the owning reference. The nsCOMPtr will take care of releasing  // our ref to the listener on destruction.  NS_ADDREF(selectionListener);  mSelectionListener = do_QueryInterface(selectionListener);  NS_RELEASE(selectionListener);  if (NS_FAILED(rv)) return rv;    nsCOMPtr<nsISelection> selection;  rv = GetDocumentSelection(getter_AddRefs(selection));  if (NS_FAILED(rv)) return rv;    nsCOMPtr<nsISelectionPrivate> selPrivate(do_QueryInterface(selection));  rv = selPrivate->AddSelectionListener(mSelectionListener);  if (NS_FAILED(rv)) return rv;    //focus listener  // now register ourselves as a focus listener, so that we get called  // when the focus changes in the window  nsDocViewerFocusListener *focusListener;  NS_NEWXPCOM(focusListener, nsDocViewerFocusListener);  if (!focusListener) return NS_ERROR_OUT_OF_MEMORY;  focusListener->Init(this);    // this is the owning reference. The nsCOMPtr will take care of releasing  // our ref to the listener on destruction.  NS_ADDREF(focusListener);  mFocusListener = do_QueryInterface(focusListener);  NS_RELEASE(focusListener);  if (NS_FAILED(rv))     return rv;  if(mDocument)  {    // get the DOM event receiver    nsCOMPtr<nsIDOMEventReceiver> erP (do_QueryInterface(mDocument, &rv));    if(NS_FAILED(rv))       return rv;    if(!erP)      return NS_ERROR_FAILURE;    rv = erP->AddEventListenerByIID(mFocusListener, NS_GET_IID(nsIDOMFocusListener));    NS_ASSERTION(NS_SUCCEEDED(rv), "failed to register focus listener");  }  return rv;}