Bug 487667 - Clone documents for printing, r=roc+jst+joe+dbaron

This commit is contained in:
Olli Pettay
2009-12-10 20:02:13 -08:00
parent be28d4906a
commit 2e3d0c93fe
73 changed files with 1422 additions and 495 deletions

View File

@@ -39,6 +39,12 @@
#include "nsIContentViewer.h"
#include "nsIDOMDocument.h"
#include "nsContentUtils.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsPIDOMWindow.h"
#include "nsGkAtoms.h"
#include "nsComponentManagerUtils.h"
#include "nsIDocShellTreeOwner.h"
#include "nsIDocShellTreeItem.h"
//---------------------------------------------------
//-- nsPrintObject Class Impl
@@ -65,23 +71,49 @@ nsPrintObject::~nsPrintObject()
//------------------------------------------------------------------
// Resets PO by destroying the presentation
nsresult
nsPrintObject::Init(nsIDocShell* aDocShell)
nsPrintObject::Init(nsIDocShell* aDocShell, nsIDOMDocument* aDoc,
PRBool aPrintPreview)
{
mDocShell = aDocShell;
NS_ENSURE_TRUE(mDocShell, NS_ERROR_FAILURE);
nsresult rv;
nsCOMPtr<nsIContentViewer> viewer;
rv = mDocShell->GetContentViewer(getter_AddRefs(viewer));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIDOMDocument> doc;
viewer->GetDOMDocument(getter_AddRefs(doc));
NS_ENSURE_SUCCESS(rv, rv);
mDocument = do_QueryInterface(doc);
NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE);
mPrintPreview = aPrintPreview;
if (mPrintPreview || mParent) {
mDocShell = aDocShell;
} else {
nsCOMPtr<nsIDocShellTreeOwner> owner = do_GetInterface(aDocShell);
nsCOMPtr<nsIDocShellTreeItem> item = do_QueryInterface(aDocShell);
PRInt32 itemType = 0;
item->GetItemType(&itemType);
// Create a container docshell for printing.
mDocShell = do_CreateInstance("@mozilla.org/docshell;1");
NS_ENSURE_TRUE(mDocShell, NS_ERROR_OUT_OF_MEMORY);
nsCOMPtr<nsIDocShellTreeItem> newItem = do_QueryInterface(mDocShell);
newItem->SetItemType(itemType);
newItem->SetTreeOwner(owner);
}
NS_ENSURE_TRUE(mDocShell, NS_ERROR_FAILURE);
nsCOMPtr<nsIDOMDocument> dummy = do_GetInterface(mDocShell);
nsCOMPtr<nsIContentViewer> viewer;
mDocShell->GetContentViewer(getter_AddRefs(viewer));
NS_ENSURE_STATE(viewer);
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDoc);
NS_ENSURE_STATE(doc);
if (mParent) {
nsCOMPtr<nsPIDOMWindow> window = doc->GetWindow();
if (window) {
mContent = do_QueryInterface(window->GetFrameElementInternal());
}
mDocument = doc;
return NS_OK;
}
mDocument = doc->CreateStaticClone(mDocShell);
nsCOMPtr<nsIDOMDocument> clonedDOMDoc = do_QueryInterface(mDocument);
NS_ENSURE_STATE(clonedDOMDoc);
viewer->SetDOMDocument(clonedDOMDoc);
return NS_OK;
}