nsresult rv;  if (!mDocument) { return NS_ERROR_NULL_POINTER; }  mDeviceContext = dont_QueryInterface(aDeviceContext);  PRBool makeCX = PR_FALSE; if (!mPresContext) {  #if 1 rv = NS_NewGalleyContext(getter_AddRefs(mPresContext)); #else rv = NS_NewPrintPreviewContext(getter_AddRefs(mPresContext)); #endif if (NS_FAILED(rv)) return rv;  mPresContext->Init(aDeviceContext); makeCX = PR_TRUE; }  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);   nsCOMPtr<nsIScriptGlobalObjectOwner> owner; requestor->GetInterface(NS_GET_IID(nsIScriptGlobalObjectOwner), getter_AddRefs(owner)); if (nsnull != owner) { nsCOMPtr<nsIScriptGlobalObject> global; rv = owner->GetScriptGlobalObject(getter_AddRefs(global)); if (NS_SUCCEEDED(rv) && (nsnull != global)) { mDocument->SetScriptGlobalObject(global); nsCOMPtr<nsIDOMDocument> domdoc(do_QueryInterface(mDocument)); if (nsnull != domdoc) { global->SetNewDocument(domdoc); } } } }   rv = MakeWindow(aParentWidget, aBounds); if (NS_FAILED(rv)) return rv;   nsIStyleSet* styleSet; rv = CreateStyleSet(mDocument, &styleSet); if (NS_FAILED(rv)) return rv;   rv = mDocument->CreateShell(mPresContext, mViewManager, styleSet, getter_AddRefs(mPresShell)); NS_RELEASE(styleSet); if (NS_FAILED(rv)) return rv; mPresShell->BeginObservingDocument();   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);  if (!makeCX) {        mPresShell->InitialReflow(width, height);   if (mEnableRendering) { mViewManager->EnableRefresh(NS_VMREFRESH_IMMEDIATE); } }    nsDocViwerSelectionListener *selectionListener; NS_NEWXPCOM(selectionListener, nsDocViwerSelectionListener); if (!selectionListener) return NS_ERROR_OUT_OF_MEMORY; selectionListener->Init(this);    NS_ADDREF(selectionListener); rv = selectionListener->QueryInterface(NS_GET_IID(nsISelectionListener), getter_AddRefs(mSelectionListener)); 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;     nsDocViewerFocusListener *focusListener; NS_NEWXPCOM(focusListener, nsDocViewerFocusListener); if (!focusListener) return NS_ERROR_OUT_OF_MEMORY; focusListener->Init(this);    NS_ADDREF(focusListener); rv = focusListener->QueryInterface(NS_GET_IID(nsIDOMFocusListener), getter_AddRefs(mFocusListener)); NS_RELEASE(focusListener); if (NS_FAILED(rv)) return rv;  if(mDocument) {  nsCOMPtr<nsIDOMEventReceiver> erP; rv = mDocument->QueryInterface(NS_GET_IID(nsIDOMEventReceiver), getter_AddRefs(erP)); if(NS_FAILED(rv) || !erP) return rv?rv:NS_ERROR_FAILURE;  rv = erP->AddEventListenerByIID(mFocusListener, NS_GET_IID(nsIDOMFocusListener)); NS_ASSERTION(NS_SUCCEEDED(rv), "failed to register focus listener"); } return rv;
mDocViewer = aDocViewer; return NS_OK;
DocumentViewerImpl::Init(nsIWidget* aParentWidget,                         nsIDeviceContext* aDeviceContext,                         const nsRect& aBounds){  nsresult rv;  if (!mDocument) {    return NS_ERROR_NULL_POINTER;  }  mDeviceContext = dont_QueryInterface(aDeviceContext);  PRBool makeCX = PR_FALSE;  if (!mPresContext) {    // Create presentation context#if 1     rv = NS_NewGalleyContext(getter_AddRefs(mPresContext));#else // turn on print preview for debugging until print preview is fixed    rv = NS_NewPrintPreviewContext(getter_AddRefs(mPresContext));#endif    if (NS_FAILED(rv)) return rv;    mPresContext->Init(aDeviceContext);     makeCX = PR_TRUE;  }  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);    // Set script-context-owner in the document    nsCOMPtr<nsIScriptGlobalObjectOwner> owner;    requestor->GetInterface(NS_GET_IID(nsIScriptGlobalObjectOwner),       getter_AddRefs(owner));    if (nsnull != owner) {      nsCOMPtr<nsIScriptGlobalObject> global;      rv = owner->GetScriptGlobalObject(getter_AddRefs(global));      if (NS_SUCCEEDED(rv) && (nsnull != global)) {        mDocument->SetScriptGlobalObject(global);        nsCOMPtr<nsIDOMDocument> domdoc(do_QueryInterface(mDocument));        if (nsnull != domdoc) {          global->SetNewDocument(domdoc);        }      }    }  }  // Create the ViewManager and Root View...  rv = MakeWindow(aParentWidget, aBounds);  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);      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  nsDocViwerSelectionListener *selectionListener;  NS_NEWXPCOM(selectionListener, nsDocViwerSelectionListener);  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);  rv = selectionListener->QueryInterface(NS_GET_IID(nsISelectionListener), getter_AddRefs(mSelectionListener));  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);  rv = focusListener->QueryInterface(NS_GET_IID(nsIDOMFocusListener), getter_AddRefs(mFocusListener));  NS_RELEASE(focusListener);  if (NS_FAILED(rv))     return rv;  if(mDocument)  {    // get the DOM event receiver    nsCOMPtr<nsIDOMEventReceiver> erP;    rv = mDocument->QueryInterface(NS_GET_IID(nsIDOMEventReceiver), getter_AddRefs(erP));    if(NS_FAILED(rv) || !erP)      return rv?rv:NS_ERROR_FAILURE;    rv = erP->AddEventListenerByIID(mFocusListener, NS_GET_IID(nsIDOMFocusListener));    NS_ASSERTION(NS_SUCCEEDED(rv), "failed to register focus listener");  }  return rv;}