mDocViewer = aDocViewer; return NS_OK;
nsresult rv; NS_ENSURE_TRUE(mDocument, NS_ERROR_NULL_POINTER);  mDeviceContext = dont_QueryInterface(aDeviceContext);  PRBool makeCX = PR_FALSE; if (!mPresContext) {  #if 1 mPresContext = do_CreateInstance(kGalleyContextCID,&rv); #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); Hide();  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);    nscolor bgcolor = NS_RGB(0, 0, 0); mPresContext->GetDefaultBackgroundColor(&bgcolor); mViewManager->SetDefaultBackgroundColor(bgcolor);  if (!makeCX) {        mPresShell->InitialReflow(width, height);   if (mEnableRendering) { mViewManager->EnableRefresh(NS_VMREFRESH_IMMEDIATE); } }    nsDocViewerSelectionListener *selectionListener; NS_NEWXPCOM(selectionListener, nsDocViewerSelectionListener); if (!selectionListener) return NS_ERROR_OUT_OF_MEMORY; selectionListener->Init(this);    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;     nsDocViewerFocusListener *focusListener; NS_NEWXPCOM(focusListener, nsDocViewerFocusListener); if (!focusListener) return NS_ERROR_OUT_OF_MEMORY; focusListener->Init(this);    NS_ADDREF(focusListener); mFocusListener = do_QueryInterface(focusListener); NS_RELEASE(focusListener); if (NS_FAILED(rv)) return rv;  if(mDocument) {  nsCOMPtr<nsIDOMEventReceiver> erP (do_QueryInterface(mDocument, &rv)); 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;
nsresult nsDocViewerSelectionListener::Init(DocumentViewerImpl *aDocViewer){  mDocViewer = aDocViewer;  return NS_OK;}