- Rewrite the way we handle print preview so that it can be used in any XUL app. This is achieved by adding a new "public" method to PrintUtils:

PrintUtils.printPreview(enterPPCallback, exitPPCallback) where the optional callback functions are called just before entering and after exiting PP mode

- disable all short keys in PP mode except ctrl-W and fix bug 178485 and bug 215277. Now clicking the close window icon actually closes the window.

- global vars gPrintSettingsAreGlobal, gSavePrintSettings, gPrintSettings, gOldCloseHandler, gInPrintPreviewMode, gWebProgress are removed.

- A lot of cleanup.
This commit is contained in:
chanial%noos.fr 2003-10-29 08:01:27 +00:00
Родитель f8dcb83ab6
Коммит 940d9e626c
9 изменённых файлов: 192 добавлений и 344 удалений

Просмотреть файл

@ -19,8 +19,8 @@
<menuitem id="menu_sendLink" label="&sendPageCmd.label;" accesskey="&sendPageCmd.accesskey;" command="Browser:SendLink"/>
#endif
<menuseparator/>
<menuitem label="&printSetupCmd.label;" accesskey="&printSetupCmd.accesskey;" oncommand="BrowserPrintSetup();"/>
<menuitem label="&printPreviewCmd.label;" accesskey="&printPreviewCmd.accesskey;" oncommand="BrowserPrintPreview();"/>
<menuitem label="&printSetupCmd.label;" accesskey="&printSetupCmd.accesskey;" oncommand="PrintUtils.showPageSetup();"/>
<menuitem label="&printPreviewCmd.label;" accesskey="&printPreviewCmd.accesskey;" oncommand="PrintUtils.printPreview(onEnterPrintPreview, onExitPrintPreview);"/>
<menuitem label="&printCmd.label;" accesskey="&printCmd.accesskey;" key="printKb" command="Browser:Print"/>
#ifndef XP_MACOSX
<menuseparator/>

Просмотреть файл

@ -1,6 +1,7 @@
<script type="application/x-javascript" src="chrome://global/content/nsDragAndDrop.js"/>
<script type="application/x-javascript" src="chrome://global/content/globalOverlay.js"/>
<script type="application/x-javascript" src="chrome://global/content/findUtils.js"/>
<script type="application/x-javascript" src="chrome://global/content/printUtils.js"/>
<script type="application/x-javascript" src="chrome://browser/content/utilityOverlay.js"/>
<script type="application/x-javascript" src="chrome://browser/content/bookmarks/bookmarks.js"/>
<script type="application/x-javascript" src="chrome://browser/content/bookmarks/bookmarksMenu.js"/>

Просмотреть файл

@ -18,8 +18,7 @@
oncommandupdate="goUpdatePasteMenuItems()"/>
<commandset id="mainCommandSet">
<command id="cmd_newNavigator"
oncommand="OpenBrowserWindow()"/>
<command id="cmd_newNavigator" oncommand="OpenBrowserWindow()"/>
<command id="cmd_bm_open" oncommand="goDoCommand('cmd_bm_open');"/>
<command id="cmd_bm_openinnewwindow" oncommand="goDoCommand('cmd_bm_openinnewwindow');"/>
<command id="cmd_bm_openinnewtab" oncommand="goDoCommand('cmd_bm_openinnewtab');"/>
@ -29,14 +28,9 @@
<command id="cmd_bm_newfolder" oncommand="goDoCommand('cmd_bm_newfolder');"/>
<command id="cmd_bm_newbookmark" oncommand="goDoCommand('cmd_bm_newbookmark');"/>
<command id="cmd_bm_newseparator" oncommand="goDoCommand('cmd_bm_newseparator');"/>
<command id="cmd_bm_find" oncommand="goDoCommand('cmd_bm_find');"/>
<command id="cmd_bm_properties" oncommand="goDoCommand('cmd_bm_properties');"/>
<command id="cmd_bm_rename" oncommand="goDoCommand('cmd_bm_rename');"/>
<command id="cmd_bm_moveBookmark" oncommand="goDoCommand('cmd_bm_moveBookmark');"/>
<command id="cmd_bm_cut" oncommand="goDoCommand('cmd_bm_cut');"/>
<command id="cmd_bm_copy" oncommand="goDoCommand('cmd_bm_copy');"/>
<command id="cmd_bm_paste" oncommand="goDoCommand('cmd_bm_paste');"/>
<command id="cmd_bm_delete" oncommand="goDoCommand('cmd_bm_delete');"/>
#ifdef XP_WIN
<command id="cmd_handleBackspace" oncommand="BrowserHandleBackspace();" />
@ -47,7 +41,7 @@
<command id="Browser:SavePage" oncommand="saveDocument(window._content.document);"/>
<command id="Browser:SendLink" oncommand="sendLink(Components.lookupMethod(window._content, 'location').call(window._content).href,
Components.lookupMethod(window._content.document, 'title').call(window._content.document));"/>
<command id="Browser:Print" oncommand="BrowserPrint();"/>
<command id="Browser:Print" oncommand="PrintUtils.print();"/>
<command id="cmd_close" oncommand="BrowserCloseTabOrWindow()"/>
<command id="cmd_closeWindow" oncommand="BrowserCloseWindow()"/>
<command id="cmd_CustomizeToolbars" oncommand="BrowserCustomizeToolbar()"/>

Просмотреть файл

@ -90,13 +90,7 @@ var gBrowser = null;
// Global variable that holds the nsContextMenu instance.
var gContextMenu = null;
var gPrintSettingsAreGlobal = true;
var gSavePrintSettings = true;
var gPrintSettings = null;
var gChromeState = null; // chrome state before we went into print preview
var gOldCloseHandler = null; // close handler before we went into print preview
var gInPrintPreviewMode = false;
var gWebProgress = null;
var gFormHistory = null;
var gFormFillEnabled = true;
@ -390,7 +384,6 @@ function delayedStartup()
// loads the services
initServices();
initBMService();
gBrowser.addEventListener("load", function(evt) { setTimeout(loadEventHandlers, 0, evt); }, true);
window.addEventListener("keypress", ctrlNumberTabSelection, false);
@ -415,7 +408,7 @@ function delayedStartup()
BMSVC.readBookmarks();
var bt = document.getElementById("bookmarks-ptf");
if (bt) {
var btf = BMSVC.getBookmarksToolbarFolder().Value
var btf = BMSVC.getBookmarksToolbarFolder().Value;
bt.ref = btf;
document.getElementById("bookmarks-chevron").ref = btf;
bt.database.AddObserver(BookmarksToolbarRDFObserver);
@ -885,11 +878,9 @@ function openLocation()
function BrowserOpenTab()
{
if (!gInPrintPreviewMode) {
gBrowser.selectedTab = gBrowser.addTab('about:blank');
if (gURLBar)
setTimeout("gURLBar.focus();", 0);
}
gBrowser.selectedTab = gBrowser.addTab('about:blank');
if (gURLBar)
setTimeout("gURLBar.focus();", 0);
}
/* Called from the openLocation dialog. This allows that dialog to instruct
@ -1409,7 +1400,6 @@ function BrowserFullScreen()
function onFullScreen()
{
FullScreen.toggle();
}
function getWebNavigation()
@ -1449,261 +1439,31 @@ function toggleAffectedChrome(aHide)
// (*) menubar
// (*) navigation bar
// (*) bookmarks toolbar
// (*) tab browser ``strip''
// (*) sidebar
if (!gChromeState)
gChromeState = new Object;
var navToolbox = document.getElementById("navigator-toolbox");
navToolbox.hidden = aHide;
if (aHide)
{
// going into print preview mode
//deal with tab browser
gChromeState.hadTabStrip = gBrowser.getStripVisibility();
gBrowser.setStripVisibilityTo(false);
gChromeState = {};
var sidebar = document.getElementById("sidebar-box");
gChromeState.sidebarOpen = !sidebar.hidden;
if (gChromeState.sidebarOpen) {
toggleSidebar();
}
}
else
{
// restoring normal mode (i.e., leaving print preview mode)
//restore tab browser
gBrowser.setStripVisibilityTo(gChromeState.hadTabStrip);
if (gChromeState.sidebarOpen) {
toggleSidebar();
}
}
if (gChromeState.sidebarOpen)
toggleSidebar(); //pch doesn't work.
}
function showPrintPreviewToolbar()
function onEnterPrintPreview()
{
toggleAffectedChrome(true);
var printPreviewTB = document.createElementNS(kXULNS, "toolbar");
printPreviewTB.setAttribute("printpreview", true);
printPreviewTB.setAttribute("id", "print-preview-toolbar");
var navToolbox = document.getElementById("navigator-toolbox");
navToolbox.parentNode.insertBefore(printPreviewTB, navToolbox);
}
function BrowserExitPrintPreview()
function onExitPrintPreview()
{
gInPrintPreviewMode = false;
gBrowser.setAttribute("handleCtrlPageUpDown", "true");
// exit print preview galley mode in content area
var ifreq = _content.QueryInterface(
Components.interfaces.nsIInterfaceRequestor);
var webBrowserPrint = ifreq.getInterface(
Components.interfaces.nsIWebBrowserPrint);
webBrowserPrint.exitPrintPreview();
_content.focus();
// remove the print preview toolbar
var navToolbox = document.getElementById("navigator-toolbox");
var printPreviewTB = document.getElementById("print-preview-toolbar");
navToolbox.parentNode.removeChild(printPreviewTB);
// restore chrome to original state
toggleAffectedChrome(false);
// restore old onclose handler if we found one before previewing
var mainWin = document.getElementById("main-window");
mainWin.setAttribute("onclose", gOldCloseHandler);
}
function setPrinterDefaultsForSelectedPrinter(aPrintService)
{
if (gPrintSettings.printerName == "") {
gPrintSettings.printerName = aPrintService.defaultPrinterName;
}
// First get any defaults from the printer
aPrintService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings);
// now augment them with any values from last time
aPrintService.initPrintSettingsFromPrefs(gPrintSettings, true, gPrintSettings.kInitSaveAll);
}
function GetPrintSettings()
{
var prevPS = gPrintSettings;
try {
if (gPrintSettings == null) {
var pref = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
if (pref) {
gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
}
var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
.getService(Components.interfaces.nsIPrintSettingsService);
if (gPrintSettingsAreGlobal) {
gPrintSettings = printService.globalPrintSettings;
setPrinterDefaultsForSelectedPrinter(printService);
} else {
gPrintSettings = printService.newPrintSettings;
}
}
} catch (e) {
dump("GetPrintSettings() "+e+"\n");
}
return gPrintSettings;
}
// This observer is called once the progress dialog has been "opened"
var gPrintPreviewObs = {
observe: function(aSubject, aTopic, aData)
{
setTimeout(FinishPrintPreview, 0);
},
QueryInterface : function(iid)
{
if (iid.equals(Components.interfaces.nsIObserver) || iid.equals(Components.interfaces.nsISupportsWeakReference))
return this;
throw Components.results.NS_NOINTERFACE;
}
};
function BrowserPrintPreview()
{
var ifreq;
var webBrowserPrint;
try {
ifreq = _content.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
webBrowserPrint = ifreq.getInterface(Components.interfaces.nsIWebBrowserPrint);
gPrintSettings = GetPrintSettings();
} catch (e) {
// 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
// dump(e); // if you need to debug
}
// Here we get the PrintingPromptService tso we can display the PP Progress from script
// For the browser implemented via XUL with the PP toolbar we cannot let it be
// automatically opened from the print engine because the XUL scrollbars in the PP window
// will layout before the content window and a crash will occur.
//
// Doing it all from script, means it lays out before hand and we can let printing do it's own thing
gWebProgress = new Object();
var printPreviewParams = new Object();
var notifyOnOpen = new Object();
var printingPromptService = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"]
.getService(Components.interfaces.nsIPrintingPromptService);
if (printingPromptService) {
// just in case we are already printing,
// an error code could be returned if the Prgress Dialog is already displayed
try {
printingPromptService.showProgress(this, webBrowserPrint, gPrintSettings, gPrintPreviewObs, false, gWebProgress,
printPreviewParams, notifyOnOpen);
if (printPreviewParams.value) {
var webNav = getWebNavigation();
printPreviewParams.value.docTitle = webNav.document.title;
printPreviewParams.value.docURL = webNav.currentURI.spec;
}
// this tells us whether we should continue on with PP or
// wait for the callback via the observer
if (!notifyOnOpen.value.valueOf() || gWebProgress.value == null) {
FinishPrintPreview();
}
} catch (e) {
FinishPrintPreview();
}
}
}
function FinishPrintPreview()
{
try {
var ifreq = _content.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
var webBrowserPrint = ifreq.getInterface(Components.interfaces.nsIWebBrowserPrint);
if (webBrowserPrint) {
gPrintSettings = GetPrintSettings();
webBrowserPrint.printPreview(gPrintSettings, null, gWebProgress.value);
}
gBrowser.setAttribute("handleCtrlPageUpDown", "false");
var mainWin = document.getElementById("main-window");
// save previous close handler to restoreon exiting print preview mode
if (mainWin.hasAttribute("onclose"))
gOldCloseHandler = mainWin.getAttribute("onclose");
else
gOldCloseHandler = null;
mainWin.setAttribute("onclose", "BrowserExitPrintPreview(); return false;");
// show the toolbar after we go into print preview mode so
// that we can initialize the toolbar with total num pages
showPrintPreviewToolbar();
_content.focus();
} catch (e) {
// 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
// dump(e); // if you need to debug
}
gInPrintPreviewMode = true;
}
function BrowserPrintSetup()
{
var didOK = false;
try {
var ifreq = _content.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
var webBrowserPrint = ifreq.getInterface(Components.interfaces.nsIWebBrowserPrint);
if (webBrowserPrint) {
gPrintSettings = GetPrintSettings();
}
didOK = goPageSetup(window, gPrintSettings); // from utilityOverlay.js
if (didOK) { // from utilityOverlay.js
if (webBrowserPrint) {
if (gPrintSettingsAreGlobal && gSavePrintSettings) {
var psService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
.getService(Components.interfaces.nsIPrintSettingsService);
psService.savePrintSettingsToPrefs(gPrintSettings, false, gPrintSettings.kInitSaveNativeData);
}
}
}
} catch (e) {
dump("BrowserPrintSetup "+e);
}
return didOK;
}
function BrowserPrint()
{
try {
var ifreq = _content.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
var webBrowserPrint = ifreq.getInterface(Components.interfaces.nsIWebBrowserPrint);
if (webBrowserPrint) {
gPrintSettings = GetPrintSettings();
webBrowserPrint.print(gPrintSettings, null);
}
} catch (e) {
// 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
// dump(e); // if you need to debug
}
}
function BrowserFind()
@ -2736,8 +2496,7 @@ nsBrowserContentListener.prototype =
// |forceOpen| is a bool that indicates that the sidebar should be forced open. In other words
// the toggle won't be allowed to close the sidebar.
function toggleSidebar(aCommandID, forceOpen) {
if (gInPrintPreviewMode)
return;
var sidebarBox = document.getElementById("sidebar-box");
if (!aCommandID)
aCommandID = sidebarBox.getAttribute("sidebarcommand");

Просмотреть файл

@ -48,26 +48,6 @@ function getBrowserURL()
return "chrome://browser/content/browser.xul";
}
function goPageSetup(domwin, printSettings)
{
try {
if (printSettings == null) {
alert("PrintSettings arg is null!");
}
// This code calls the printoptions service to bring up the printoptions
// dialog. This will be an xp dialog if the platform did not override
// the ShowPrintSetupDialog method.
var printingPromptService = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"]
.getService(Components.interfaces.nsIPrintingPromptService);
printingPromptService.showPageSetup(domwin, printSettings, null);
return true;
} catch(e) {
return false;
}
return true;
}
function goToggleToolbar( id, elementID )
{
var toolbar = document.getElementById(id);

Просмотреть файл

@ -1665,6 +1665,18 @@ var BookmarkEditMenuTxnListener =
}
#ifdef DEBUG
var _dumpTIME;
function dumpTIME(aString)
{
var now=Date.now();
dump(aString);
if (_dumpTIME)
dump(now-_dumpTIME+'ms\n');
else
_dumpTIME = now;
}
function dumpOBJ (aObj)
{
if (!aObj)

Просмотреть файл

@ -38,6 +38,7 @@
# ***** END LICENSE BLOCK ***** -->
# this file depends on printUtils.js
# caller must define the routine |exitPrintPreview| to restore the normal state
<!DOCTYPE window SYSTEM "chrome://global/locale/printPreview.dtd">
@ -118,14 +119,11 @@
<xul:toolbarseparator class="toolbarseparator-primary"/>
<xul:button label="&close.label;" accesskey="&close.accesskey;"
oncommand="BrowserExitPrintPreview();"/>
oncommand="PrintUtils.exitPrintPreview();"/>
<xul:data value="&customPrompt.title;"/>
</content>
<implementation>
<field name="mDebug">
false
</field>
<field name="mPrintButton">
document.getAnonymousNodes(this)[0]
</field>
@ -160,12 +158,10 @@
<constructor>
<![CDATA[
this._debug("constructor");
var print = _getWebBrowserPrint();
var print = getWebBrowserPrint();
this.mTotalPages.value = print.printPreviewNumPages;
_getValuesFromPS();
this.getValuesFromPS();
// Hide the ``Print...'' button when the underlying gfx code does not
// support multiple devicecontext to be used concurrently
@ -223,10 +219,10 @@
var didOK = BrowserPrintSetup();
if (didOK) {
// the changes that effect the UI
this._getValuesFromPS();
this.getValuesFromPS();
// Now do PrintPreview
var print = this._getWebBrowserPrint();
var print = this.getWebBrowserPrint();
var settings = print.currentPrintSettings;
this.doPrintPreview(print, settings, this.mTotalPages, null);
}
@ -239,11 +235,8 @@
<parameter name="aPageNum"/>
<parameter name="aHomeOrEnd"/>
<body>
<![CDATA[
this._debug("navigate: " + aDirection + " " + aPageNum +
" " + aHomeOrEnd);
var print = this._getWebBrowserPrint();
<![CDATA[
var print = this.getWebBrowserPrint();
var validInput = false;
var total;
@ -320,7 +313,6 @@
var renameTitle = this.mCustomTitle;
var result = {value:value};
var confirmed = promptService.prompt(window, renameTitle, promptStr, result, null, {value:value});
this._debug("confirmed: " + confirmed);
if (!confirmed || (!result.value) || (result.value == "") || result.value == value) {
return(-1);
}
@ -434,14 +426,11 @@
</body>
</method>
<method name="scale">
<parameter name="aValue"/>
<body>
<![CDATA[
this._debug("scale: " + aValue);
var print = this._getWebBrowserPrint();
var print = this.getWebBrowserPrint();
var settings = print.currentPrintSettings;
if (aValue == "ShrinkToFit") {
if (!settings.shrinkToFit) {
@ -455,7 +444,6 @@
if (aValue == "Custom") {
aValue = this.promptForScaleValue(settings.scaling * 100.0);
this._debug("promptForScaleValue "+aValue);
if (aValue >= 10) {
aValue /= 100.0;
} else {
@ -478,8 +466,6 @@
<parameter name="aOrientation"/>
<body>
<![CDATA[
this._debug("orient: " + aOrientation);
var orientValue;
const kIPrintSettings = Components.interfaces.nsIPrintSettings;
if (aOrientation == "portrait")
@ -495,7 +481,7 @@
orientValue = kIPrintSettings.kLandscapeOrientation;
}
var print = this._getWebBrowserPrint();
var print = this.getWebBrowserPrint();
var settings = print.currentPrintSettings;
settings.orientation = orientValue;
this.doPrintPreview(print, settings, this.mTotalPages, null);
@ -503,7 +489,7 @@
</body>
</method>
<method name="_getWebBrowserPrint">
<method name="getWebBrowserPrint">
<body>
<![CDATA[
var ifreq = _content.QueryInterface(
@ -513,10 +499,10 @@
</body>
</method>
<method name="_getValuesFromPS">
<method name="getValuesFromPS">
<body>
<![CDATA[
var print = this._getWebBrowserPrint();
var print = this.getWebBrowserPrint();
var settings = print.currentPrintSettings;
var isPortrait = settings.orientation == Components.interfaces.nsIPrintSettings.kPortraitOrientation;
@ -525,22 +511,10 @@
this.mLandscapeButton.checked = !isPortrait;
if (settings.shrinkToFit) {
this._debug("setting ShrinkToFit");
this.mScaleCombobox.value = "ShrinkToFit";
} else {
this.setScaleCombobox(settings.scaling);
}
]]>
</body>
</method>
<method name="_debug">
<parameter name="aMsg"/>
<body>
<![CDATA[
if (this.mDebug)
dump("\t *** pptoolbar: " + aMsg + "\n");
]]>
</body>
</method>

Просмотреть файл

@ -21,6 +21,7 @@
#
# Contributors:
# Rod Spears <rods@netscape.com>
# Pierre Chanial <p_ch@verizon.net>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
@ -66,13 +67,14 @@ function elipseString(aStr, doFront)
// all progress notifications are done through the nsIWebProgressListener implementation...
var progressListener = {
onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus)
onStateChange: function (aWebProgress, aRequest, aStateFlags, aStatus)
{
if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP)
window.close();
},
onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
onProgressChange: function (aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
{
if (!progressParams)
return;
@ -89,27 +91,19 @@ var progressListener = {
}
},
onLocationChange: function(aWebProgress, aRequest, aLocation)
{
// we can ignore this notification
},
onLocationChange: function (aWebProgress, aRequest, aLocation) {},
onSecurityChange: function (aWebProgress, aRequest, state) {},
onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage)
onStatusChange: function (aWebProgress, aRequest, aStatus, aMessage)
{
if (aMessage != "")
if (aMessage)
dialog.title.setAttribute("value", aMessage);
},
onSecurityChange: function(aWebProgress, aRequest, state)
{
// we can ignore this notification
},
QueryInterface : function(iid)
QueryInterface: function (iid)
{
if (iid.equals(Components.interfaces.nsIWebProgressListener) || iid.equals(Components.interfaces.nsISupportsWeakReference))
return this;
return this;
throw Components.results.NS_NOINTERFACE;
}
}

Просмотреть файл

@ -79,13 +79,10 @@ var PrintUtils = {
print: function ()
{
var webBrowserPrint = this.getWebBrowserPrint();
var printSettings = this.getPrintSettings();
try {
var ifreq = _content.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
var webBrowserPrint = ifreq.getInterface(Components.interfaces.nsIWebBrowserPrint);
if (webBrowserPrint) {
var printSettings = this.getPrintSettings();
webBrowserPrint.print(printSettings, null);
}
webBrowserPrint.print(printSettings, null);
} catch (e) {
// Pressing cancel is expressed as an NS_ERROR_ABORT return value,
// causing an exception to be thrown which we catch here.
@ -94,11 +91,52 @@ var PrintUtils = {
}
},
printPreview: function (aEnterPPCallback, aExitPPCallback)
{
this._onEnterPP = aEnterPPCallback;
this._onExitPP = aExitPPCallback;
var webBrowserPrint = this.getWebBrowserPrint();
var printSettings = this.getPrintSettings();
this._webProgressPP = {};
var ppParams = {};
var notifyOnOpen = {};
// Here we get the PrintingPromptService so we can display the PP Progress from script
// For the browser implemented via XUL with the PP toolbar we cannot let it be
// automatically opened from the print engine because the XUL scrollbars in the PP window
// will layout before the content window and a crash will occur.
// Doing it all from script, means it lays out before hand and we can let printing do it's own thing
var PPROMPTSVC = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"]
.getService(Components.interfaces.nsIPrintingPromptService);
// just in case we are already printing,
// an error code could be returned if the Prgress Dialog is already displayed
try {
PPROMPTSVC.showProgress(this, webBrowserPrint, printSettings, this._obsPP, false,
this._webProgressPP, ppParams, notifyOnOpen);
if (ppParams.value) {
var webNav = getBrowser().webNavigation;
ppParams.value.docTitle = webNav.document.title;
ppParams.value.docURL = webNav.currentURI.spec;
}
// 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();
} catch (e) {
this.enterPrintPreview();
}
},
////////////////////////////////////////
// "private" methods. Don't use them. //
////////////////////////////////////////
getWebBrowserPrint: function ()
{
return _content.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebBrowserPrint);
},
setPrinterDefaultsForSelectedPrinter: function (aPRINTSVC, aPrintSettings)
{
if (!aPrintSettings.printerName)
@ -129,5 +167,101 @@ var PrintUtils = {
dump("getPrintSettings: "+e+"\n");
}
return printSettings;
},
_chromeState: {},
_commandsPP: {},
_webProgressPP: null,
_onEnterPP: null,
_onExitPP: null,
// This observer is called once the progress dialog has been "opened"
_obsPP:
{
observe: function(aSubject, aTopic, aData)
{
// delay the print preview to show the content of the progress dialog
setTimeout("PrintUtils.enterPrintPreview();", 0);
},
QueryInterface : function(iid)
{
if (iid.equals(Components.interfaces.nsIObserver) || iid.equals(Components.interfaces.nsISupportsWeakReference))
return this;
throw Components.results.NS_NOINTERFACE;
}
},
enterPrintPreview: function ()
{
var webBrowserPrint = this.getWebBrowserPrint();
var printSettings = this.getPrintSettings();
try {
webBrowserPrint.printPreview(printSettings, null, this._webProgressPP.value);
} catch (e) {
// 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
// dump(e); // if you need to debug
}
// show the toolbar after we go into print preview mode so
// that we can initialize the toolbar with total num pages
var printPreviewTB = document.createElementNS(XUL_NS, "toolbar");
printPreviewTB.setAttribute("printpreview", true);
printPreviewTB.setAttribute("id", "print-preview-toolbar");
getBrowser().parentNode.insertBefore(printPreviewTB, getBrowser());
// Tab browser...
this._chromeState.hadTabStrip = getBrowser().getStripVisibility();
getBrowser().setStripVisibilityTo(false);
// disable chrome shortcuts...
window.addEventListener("keypress", this.onKeyPressPP, true);
_content.focus();
// on Enter PP Call back
if (this._onEnterPP)
this._onEnterPP();
},
exitPrintPreview: function ()
{
window.removeEventListener("keypress", this.onKeyPressPP, true);
getBrowser().setStripVisibilityTo(this._chromeState.hadTabStrip);
var webBrowserPrint = this.getWebBrowserPrint();
webBrowserPrint.exitPrintPreview();
// remove the print preview toolbar
var printPreviewTB = document.getElementById("print-preview-toolbar");
getBrowser().parentNode.removeChild(printPreviewTB);
_content.focus();
// on Exit PP Call back
if (this._onExitPP)
this._onExitPP();
},
onKeyPressPP: function (aEvent)
{
var closeKey;
try {
closeKey = document.getElementById("key_close")
.getAttribute("key");
closeKey = aEvent["DOM_VK_"+closeKey];
} catch (e) {}
var isModif = aEvent.ctrlKey || aEvent.metaKey;
// ESC and Ctrl-W exits the PP
if (aEvent.keyCode == aEvent.DOM_VK_ESCAPE || isModif &&
(aEvent.charCode == closeKey || aEvent.charCode == closeKey + 32))
PrintUtils.exitPrintPreview();
// cancel shortkeys
if (isModif) {
aEvent.preventDefault();
aEvent.stopPropagation();
}
}
}