Bug 544018 - print preview doesn't work in view source , r=dao

This commit is contained in:
Olli Pettay
2010-02-09 18:24:49 +02:00
parent 358b8558f5
commit cb7087f56a
7 changed files with 97 additions and 125 deletions

View File

@@ -87,32 +87,27 @@ var PrintUtils = {
}
},
// calling PrintUtils.printPreview() requires that you have three functions
// in the global scope: getPPBrowser(), which returns the browser element in
// the window print preview uses, getNavToolbox(), which returns the element
// (usually the main toolbox element) before which the print preview toolbar
// should be inserted, and getWebNavigation(), which returns the document's
// nsIWebNavigation object
printPreview: function (aListenerOrEnterCallback, aExitCallback)
// If aCallback is not null, it must be an object which has the following methods:
// getPrintPreviewBrowser(), getSourceBrowser(),
// getNavToolbox(), onEnter() and onExit().
// If aCallback is null, then printPreview must previously have been called with
// non-null aCallback and that object will be reused.
printPreview: function (aCallback)
{
// if we're already in PP mode, don't set the callbacks; chances
// are they're null because someone is calling printPreview() to
// if we're already in PP mode, don't set the callback; chances
// are it is null because someone is calling printPreview() to
// get us to refresh the display.
if (!document.getElementById("print-preview-toolbar")) {
if (typeof aListenerOrEnterCallback == "object") {
this._onEnterPP = function () { aListenerOrEnterCallback.onEnter(); };
this._onExitPP = function () { aListenerOrEnterCallback.onExit(); };
} else {
this._onEnterPP = aListenerOrEnterCallback;
this._onExitPP = aExitCallback;
}
this._callback = aCallback;
this._sourceBrowser = aCallback.getSourceBrowser();
this._originalTitle = this._sourceBrowser.contentDocument.title;
this._originalURL = this._sourceBrowser.currentURI.spec;
} else {
// collapse the browser here -- it will be shown in
// onEnterPrintPreview; this forces a reflow which fixes display
// enterPrintPreview; this forces a reflow which fixes display
// issues in bug 267422.
var browser = getPPBrowser();
if (browser)
browser.collapsed = true;
this._sourceBrowser = this._callback.getPrintPreviewBrowser();
this._sourceBrowser.collapsed = true;
}
this._webProgressPP = {};
@@ -133,17 +128,16 @@ var PrintUtils = {
PPROMPTSVC.showProgress(window, webBrowserPrint, printSettings, this._obsPP, false,
this._webProgressPP, ppParams, notifyOnOpen);
if (ppParams.value) {
var webNav = getWebNavigation();
ppParams.value.docTitle = webNav.document.title;
ppParams.value.docURL = webNav.currentURI.spec;
ppParams.value.docTitle = this._originalTitle;
ppParams.value.docURL = this._originalURL;
}
// this tells us whether we should continue on with PP or
// wait for the callback via the observer
if (!notifyOnOpen.value.valueOf() || this._webProgressPP.value == null)
this.enterPrintPreview(aWindow);
this.enterPrintPreview();
} catch (e) {
this.enterPrintPreview(aWindow);
this.enterPrintPreview();
}
},
@@ -155,11 +149,7 @@ var PrintUtils = {
},
getPrintPreview: function() {
if (this._printPreviewTab) {
var docShell = getPPBrowser().getBrowserForTab(this._printPreviewTab).docShell;
return docShell.printPreview;
}
return null;
return this._callback.getPrintPreviewBrowser().docShell.printPreview;
},
////////////////////////////////////////
@@ -202,11 +192,12 @@ var PrintUtils = {
return printSettings;
},
_originalZoomValue: null,
_closeHandlerPP: null,
_webProgressPP: null,
_onEnterPP: null,
_onExitPP: null,
_callback: null,
_sourceBrowser: null,
_originalTitle: "",
_originalURL: "",
// This observer is called once the progress dialog has been "opened"
_obsPP:
@@ -227,44 +218,19 @@ var PrintUtils = {
}
},
_originalTab: null,
_printPreviewTab: null,
enterPrintPreview: function ()
{
gFocusedElement = document.commandDispatcher.focusedElement;
// Reset the zoom value and save it to be restored later.
if (typeof ZoomManager == "object") {
this._originalZoomValue = ZoomManager.zoom;
ZoomManager.reset();
}
var webBrowserPrint;
var printSettings = this.getPrintSettings();
var tabbrowser = getPPBrowser();
var contentWindow = null;
if (tabbrowser) {
if (this._printPreviewTab) {
contentWindow =
tabbrowser.getBrowserForTab(this._printPreviewTab).contentWindow;
} else {
this._originalTab = tabbrowser.mCurrentTab;
contentWindow = window.content
this._printPreviewTab = tabbrowser.loadOneTab("about:blank", null, null,
null, true, false);
}
}
var originalWindow = this._sourceBrowser.contentWindow;
try {
webBrowserPrint = this.getPrintPreview();
webBrowserPrint.printPreview(printSettings, contentWindow,
webBrowserPrint.printPreview(printSettings, originalWindow,
this._webProgressPP.value);
} catch (e) {
this._printPreviewTab = null;
this._originalTab = null;
if (typeof ZoomManager == "object")
ZoomManager.zoom = this._originalZoomValue;
// Pressing cancel is expressed as an NS_ERROR_ABORT return value,
// causing an exception to be thrown which we catch here.
// Unfortunately this will also consume helpful failures, so add a
@@ -275,12 +241,9 @@ var PrintUtils = {
var printPreviewTB = document.getElementById("print-preview-toolbar");
if (printPreviewTB) {
printPreviewTB.updateToolbar();
var browser = getPPBrowser();
if (browser)
browser.collapsed = false;
tabbrowser.getBrowserForTab(this._printPreviewTab).contentWindow.focus();
tabbrowser.selectedTab = this._printPreviewTab;
var browser = this._callback.getPrintPreviewBrowser();
browser.collapsed = false;
browser.contentWindow.focus();
return;
}
@@ -293,7 +256,7 @@ var PrintUtils = {
printPreviewTB.id = "print-preview-toolbar";
printPreviewTB.className = "toolbar-primary";
var navToolbox = getNavToolbox();
var navToolbox = this._callback.getNavToolbox();
navToolbox.parentNode.insertBefore(printPreviewTB, navToolbox);
// copy the window close handler
@@ -306,14 +269,12 @@ var PrintUtils = {
// disable chrome shortcuts...
window.addEventListener("keypress", this.onKeyPressPP, true);
tabbrowser.getBrowserForTab(this._printPreviewTab).contentWindow.focus();
tabbrowser.selectedTab = this._printPreviewTab;
var browser = this._callback.getPrintPreviewBrowser();
browser.collapsed = false;
browser.contentWindow.focus();
// on Enter PP Call back
if (this._onEnterPP) {
this._onEnterPP();
this._onEnterPP = null;
}
this._callback.onEnter();
},
exitPrintPreview: function ()
@@ -324,23 +285,12 @@ var PrintUtils = {
document.documentElement.setAttribute("onclose", this._closeHandlerPP);
this._closeHandlerPP = null;
var webBrowserPrint = this.getWebBrowserPrint();
var webBrowserPrint = this.getPrintPreview();
webBrowserPrint.exitPrintPreview();
var tabbrowser = getPPBrowser();
if (tabbrowser) {
tabbrowser.removeTab(this._printPreviewTab);
tabbrowser.selectedTab = this._originalTab;
this._originalTab = null;
this._printPreviewTab = null;
}
if (typeof ZoomManager == "object")
ZoomManager.zoom = this._originalZoomValue;
// remove the print preview toolbar
var printPreviewTB = document.getElementById("print-preview-toolbar");
getNavToolbox().parentNode.removeChild(printPreviewTB);
this._callback.getNavToolbox().parentNode.removeChild(printPreviewTB);
var fm = Components.classes["@mozilla.org/focus-manager;1"]
.getService(Components.interfaces.nsIFocusManager);
@@ -350,11 +300,7 @@ var PrintUtils = {
window.content.focus();
gFocusedElement = null;
// on Exit PP Call back
if (this._onExitPP) {
this._onExitPP();
this._onExitPP = null;
}
this._callback.onExit();
},
onKeyPressPP: function (aEvent)