XUL based print dialog on UNIX paltform,r=syd

This commit is contained in:
tajima%eng.sun.com 2000-06-23 08:00:10 +00:00
parent ad5c60eb5b
commit 4228cbcd61
6 changed files with 543 additions and 56 deletions

View File

@ -26,9 +26,17 @@
#include "nsIServiceManager.h"
#include "nsIPref.h"
#include "prenv.h" /* for PR_GetEnv */
#include "nsIAppShellComponentImpl.h"
#include "nsIDOMWindow.h"
#include "nsIServiceManager.h"
#include "nsIDialogParamBlock.h"
#include "nsINetSupportDialogService.h"
static NS_DEFINE_IID( kAppShellServiceCID, NS_APPSHELL_SERVICE_CID );
static NS_DEFINE_CID(kDialogParamBlockCID, NS_DialogParamBlock_CID);
//#include "prmem.h"
//#include "plstr.h"
@ -119,71 +127,107 @@ NS_IMETHODIMP nsDeviceContextSpecGTK :: Init(PRBool aQuiet)
char *path;
PRBool reversed = PR_FALSE, color = PR_FALSE, landscape = PR_FALSE;
PRBool tofile = PR_FALSE;
PRInt32 paper_size = NS_LETTER_SIZE;
int ileft = 500, iright = 0, itop = 500, ibottom = 0;
char *command;
nsresult rv;
char *printfile = nsnull;
nsCOMPtr<nsIPref> pPrefs = do_GetService(NS_PREF_PROGID, &rv);
// NS_WITH_SERVICE(nsIPref, pPrefs, NS_PREF_PROGID, &rv);
if (NS_SUCCEEDED(rv) && pPrefs) {
(void) pPrefs->GetBoolPref("print.print_reversed", &reversed);
(void) pPrefs->GetBoolPref("print.print_color", &color);
(void) pPrefs->GetBoolPref("print.print_landscape", &landscape);
(void) pPrefs->GetIntPref("print.print_paper_size", &paper_size);
(void) pPrefs->CopyCharPref("print.print_command", (char **) &command);
(void) pPrefs->GetIntPref("print.print_margin_top", &itop);
(void) pPrefs->GetIntPref("print.print_margin_left", &ileft);
(void) pPrefs->GetIntPref("print.print_margin_bottom", &ibottom);
(void) pPrefs->GetIntPref("print.print_margin_right", &iright);
sprintf( mPrData.command, command );
} else {
nsresult rv = NS_OK;
nsCOMPtr<nsIDialogParamBlock> ioParamBlock;
rv = nsComponentManager::CreateInstance(kDialogParamBlockCID,
nsnull,
NS_GET_IID(nsIDialogParamBlock),
getter_AddRefs(ioParamBlock));
if (NS_SUCCEEDED(rv)) {
NS_WITH_SERVICE(nsIAppShellService, appShell, kAppShellServiceCID, &rv);
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsIDOMWindow> hiddenWindow;
nsCOMPtr<nsIDOMWindow> mWindow;
JSContext *jsContext;
rv = appShell->GetHiddenWindowAndJSContext(getter_AddRefs(hiddenWindow), &jsContext);
if (NS_SUCCEEDED(rv)) {
void *stackPtr;
jsval *argv = JS_PushArguments(jsContext,
&stackPtr,
"sss%ip",
"chrome://global/content/printdialog.xul",
"_blank",
"chrome,modal",
(const nsIID *) (&NS_GET_IID(nsIDialogParamBlock)),
(nsISupports *) ioParamBlock);
if (argv) {
nsCOMPtr<nsIDOMWindow> newWindow;
rv = hiddenWindow->OpenDialog(jsContext,
argv,
4,
getter_AddRefs(newWindow));
if (NS_SUCCEEDED(rv)) {
JS_PopArguments(jsContext, stackPtr);
PRInt32 buttonPressed = 0;
ioParamBlock->GetInt(0, &buttonPressed);
if (buttonPressed == 0) {
nsCOMPtr<nsIPref> pPrefs = do_GetService(NS_PREF_PROGID, &rv);
if (NS_SUCCEEDED(rv) && pPrefs) {
(void) pPrefs->GetBoolPref("print.print_reversed", &reversed);
(void) pPrefs->GetBoolPref("print.print_color", &color);
(void) pPrefs->GetBoolPref("print.print_landscape", &landscape);
(void) pPrefs->GetIntPref("print.print_paper_size", &paper_size);
(void) pPrefs->CopyCharPref("print.print_command", (char **) &command);
(void) pPrefs->GetIntPref("print.print_margin_top", &itop);
(void) pPrefs->GetIntPref("print.print_margin_left", &ileft);
(void) pPrefs->GetIntPref("print.print_margin_bottom", &ibottom);
(void) pPrefs->GetIntPref("print.print_margin_right", &iright);
(void) pPrefs->CopyCharPref("print.print_file", (char **) &printfile);
(void) pPrefs->GetBoolPref("print.print_tofile", &tofile);
sprintf( mPrData.command, command );
sprintf( mPrData.path, printfile );
} else {
#ifndef VMS
sprintf( mPrData.command, "lpr" );
sprintf( mPrData.command, "lpr" );
#else
// Note to whoever puts the "lpr" into the prefs file. Please contact me
// as I need to make the default be "print" instead of "lpr" for OpenVMS.
sprintf( mPrData.command, "print" );
// Note to whoever puts the "lpr" into the prefs file. Please contact me
// as I need to make the default be "print" instead of "lpr" for OpenVMS.
sprintf( mPrData.command, "print" );
#endif
}
mPrData.top = itop / 1000.0;
mPrData.bottom = ibottom / 1000.0;
mPrData.left = ileft / 1000.0;
mPrData.right = iright / 1000.0;
mPrData.fpf = !reversed;
mPrData.grayscale = !color;
mPrData.size = paper_size;
mPrData.toPrinter = !tofile;
// PWD, HOME, or fail
if (!printfile) {
if ( ( path = PR_GetEnv( "PWD" ) ) == (char *) NULL )
if ( ( path = PR_GetEnv( "HOME" ) ) == (char *) NULL )
strcpy( mPrData.path, "mozilla.ps" );
if ( path != (char *) NULL )
sprintf( mPrData.path, "%s/mozilla.ps", path );
else
return NS_ERROR_FAILURE;
}
return NS_OK;
}
}
}
}
}
}
mPrData.top = itop / 1000.0;
mPrData.bottom = ibottom / 1000.0;
mPrData.left = ileft / 1000.0;
mPrData.right = iright / 1000.0;
mPrData.toPrinter = PR_TRUE;
mPrData.fpf = !reversed;
mPrData.grayscale = !color;
mPrData.size = paper_size;
return NS_ERROR_FAILURE;
// PWD, HOME, or fail
if ( ( path = PR_GetEnv( "PWD" ) ) == (char *) NULL )
if ( ( path = PR_GetEnv( "HOME" ) ) == (char *) NULL )
strcpy( mPrData.path, "mozilla.ps" );
if ( path != (char *) NULL )
sprintf( mPrData.path, "%s/mozilla.ps", path );
else
return NS_ERROR_FAILURE;
::UnixPrDialog( &mPrData );
if ( mPrData.cancel == PR_TRUE )
return NS_ERROR_FAILURE;
else {
if(pPrefs) {
pPrefs->SetBoolPref("print.print_reversed", !mPrData.fpf);
pPrefs->SetBoolPref("print.print_color", !mPrData.grayscale);
pPrefs->SetBoolPref("print.print_landscape", landscape);
pPrefs->SetIntPref("print.print_paper_size", mPrData.size);
pPrefs->SetIntPref("print.print_margin_top", (int)(mPrData.top * 1000));
pPrefs->SetIntPref("print.print_margin_left", (int)(mPrData.left * 1000));
pPrefs->SetIntPref("print.print_margin_bottom", (int)(mPrData.bottom * 1000));
pPrefs->SetIntPref("print.print_margin_right", (int)(mPrData.right * 1000));
if ( mPrData.toPrinter == PR_FALSE )
pPrefs->SetCharPref("print.print_command", mPrData.command);
}
return NS_OK;
}
}
NS_IMETHODIMP nsDeviceContextSpecGTK :: GetToPrinter( PRBool &aToPrinter )

View File

@ -38,6 +38,8 @@ CHROME_CONTENT = \
platformEditorBindings.xul \
platformTextAreaBindings.xul \
platformHTMLBindings.xml \
printdialog.xul \
printdialog.js \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,280 @@
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s): Masaki Katakai <katakai@japan.sun.com>
*/
var dialog;
var prefs = null;
var default_command = "lpr";
var default_file = "mozilla.ps";
function initDialog()
{
dialog = new Object;
dialog.findKey = document.getElementById("dialog.findKey");
dialog.caseSensitive = document.getElementById("dialog.caseSensitive");
dialog.wrap = document.getElementById("dialog.wrap");
dialog.searchBackwards = document.getElementById("dialog.searchBackwards");
dialog.find = document.getElementById("ok");
dialog.cancel = document.getElementById("cancel");
dialog.fileLabel = document.getElementById("fileLabel");
dialog.cmdLabel = document.getElementById("cmdLabel");
dialog.fileRadio = document.getElementById("fileRadio");
dialog.printerRadio = document.getElementById("printerRadio");
dialog.firstRadio = document.getElementById("firstRadio");
dialog.lastRadio = document.getElementById("lastRadio");
dialog.colorRadio = document.getElementById("colorRadio");
dialog.grayRadio = document.getElementById("grayRadio");
dialog.a4Radio = document.getElementById("a4Radio");
dialog.letterRadio = document.getElementById("letterRadio");
dialog.legalRadio = document.getElementById("legalRadio");
dialog.exectiveRadio = document.getElementById("exectiveRadio");
dialog.topInput = document.getElementById("topInput");
dialog.bottomInput = document.getElementById("bottomInput");
dialog.leftInput = document.getElementById("leftInput");
dialog.rightInput = document.getElementById("rightInput");
dialog.cmdInput = document.getElementById("cmdInput");
dialog.fileInput = document.getElementById("fileInput");
dialog.chooseButton = document.getElementById("chooseFile");
dialog.print = document.getElementById("ok");
dialog.enabled = false;
}
function checkValid(elementID)
{
var editField = document.getElementById( elementID );
if ( !editField )
return;
var stringIn = editField.value;
if (stringIn && stringIn.length > 0)
{
stringIn = stringIn.replace(/[^\.|^0-9]/g,"");
if (!stringIn) stringIn = "";
editField.value = stringIn;
}
}
function doPrintToFile( value )
{
if (value == true ) {
dialog.fileLabel.removeAttribute("disabled");
dialog.cmdLabel.setAttribute("disabled","true" );
dialog.fileInput.removeAttribute("disabled");
dialog.chooseButton.removeAttribute("disabled");
dialog.cmdInput.setAttribute("disabled","true" );
} else {
dialog.cmdLabel.removeAttribute("disabled");
dialog.fileLabel.setAttribute("disabled","true" );
dialog.fileInput.setAttribute("disabled","true" );
dialog.chooseButton.setAttribute("disabled","true" );
dialog.cmdInput.removeAttribute("disabled");
}
}
function loadDialog()
{
var print_tofile = false;
var print_reversed = false;
var print_color = true;
var print_landscape = true;
var print_paper_size = 0;
var print_margin_top = 500;
var print_margin_left = 500;
var print_margin_bottom = 0;
var print_margin_right = 0;
var print_command = default_command;
var print_file = default_file;
try {
prefs = Components.classes["component://netscape/preferences"];
if (prefs) {
prefs = prefs.getService();
if (prefs)
prefs = prefs.QueryInterface(Components.interfaces.nsIPref);
}
} catch(e) { }
if (prefs) {
try { print_tofile = prefs.GetBoolPref("print.print_tofile"); } catch(e) { }
try { print_reversed = prefs.GetBoolPref("print.print_reversed"); } catch(e) { }
try { print_color = prefs.GetBoolPref("print.print_color"); } catch(e) { }
try { print_paper_size = prefs.GetIntPref("print.print_paper_size"); } catch(e) { }
try { print_margin_top = prefs.GetIntPref("print.print_margin_top"); } catch(e) { }
try { print_margin_left = prefs.GetIntPref("print.print_margin_left"); } catch(e) { }
try { print_margin_bottom = prefs.GetIntPref("print.print_margin_bottom"); } catch(e) { }
try { print_margin_right = prefs.GetIntPref("print.print_margin_right"); } catch(e) { }
try { print_command = prefs.CopyCharPref("print.print_command"); } catch(e) { }
try { print_file = prefs.CopyCharPref("print.print_file"); } catch(e) { }
}
if ( print_tofile == true) {
dialog.fileRadio.checked = true;
doPrintToFile( true );
} else {
dialog.printerRadio.checked = true;
doPrintToFile( false );
}
if ( print_color == true) {
dialog.colorRadio.checked = true;
} else {
dialog.grayRadio.checked = true;
}
if ( print_reversed == true) {
dialog.lastRadio.checked = true;
} else {
dialog.firstRadio.checked = true;
}
if ( print_paper_size == 0 ) {
dialog.letterRadio.checked = true;
} else if ( print_paper_size == 1 ) {
dialog.legalRadio.checked = true;
} else if ( print_paper_size == 2 ) {
dialog.exectiveRadio.checked = true;
} else if ( print_paper_size == 3 ) {
dialog.a4Radio.checked = true;
}
dialog.topInput.value = print_margin_top * 0.001;
dialog.bottomInput.value = print_margin_bottom * 0.001;
dialog.leftInput.value = print_margin_left * 0.001;
dialog.rightInput.value = print_margin_right * 0.001;
dialog.cmdInput.value = print_command;
dialog.fileInput.value = print_file;
dialog.print.setAttribute("value",
document.getElementById("printButton").getAttribute("value"));
}
var param;
function onLoad()
{
// Init dialog.
initDialog();
// setup the dialogOverlay.xul button handlers
doSetOKCancel(onOK, onCancel);
param = window.arguments[0].QueryInterface(Components.interfaces.nsIDialogParamBlock);
if( !param ) {
return;
}
param.SetInt(0, 1 );
loadDialog();
}
function onUnload()
{
}
function onOK()
{
var print_paper_size = 0;
if (prefs) {
if (dialog.fileRadio.checked == true) {
prefs.SetBoolPref("print.print_tofile", true);
} else {
prefs.SetBoolPref("print.print_tofile", false);
}
if (dialog.lastRadio.checked == true) {
prefs.SetBoolPref("print.print_reversed", true);
} else {
prefs.SetBoolPref("print.print_reversed", false);
}
if (dialog.colorRadio.checked == true) {
prefs.SetBoolPref("print.print_color", true);
} else {
prefs.SetBoolPref("print.print_color", false);
}
if (dialog.letterRadio.checked == true) {
print_paper_size = 0;
} else if (dialog.legalRadio.checked == true) {
print_paper_size = 1;
} else if (dialog.exectiveRadio.checked == true) {
print_paper_size = 2;
} else if (dialog.a4Radio.checked == true) {
print_paper_size = 3;
}
prefs.SetIntPref("print.print_paper_size", print_paper_size);
prefs.SetIntPref("print.print_margin_top", dialog.topInput.value * 1000);
prefs.SetIntPref("print.print_margin_left", dialog.leftInput.value * 1000);
prefs.SetIntPref("print.print_margin_bottom", dialog.bottomInput.value *1000);
prefs.SetIntPref("print.print_margin_right", dialog.rightInput.value * 1000);
prefs.SetCharPref("print.print_command", dialog.cmdInput.value);
prefs.SetCharPref("print.print_file", dialog.fileInput.value);
}
if (param) {
param.SetInt(0, 0 );
}
return true;
}
function onCancel()
{
if (param) {
param.SetInt(0, 1 );
}
return true;
}
const nsIFilePicker = Components.interfaces.nsIFilePicker;
function onChooseFile()
{
if (dialog.fileRadio.checked == false) {
return;
}
try {
var fp = Components.classes["component://mozilla/filepicker"].createInstance(nsIFilePicker);
fp.init(window, document.getElementById("fpDialog").getAttribute("value"), nsIFilePicker.modeSave);
fp.appendFilters(nsIFilePicker.filterAll);
fp.show();
if (fp.file && fp.file.path.length > 0) {
dialog.fileInput.value = fp.file.path;
}
} catch(ex) {
dump(ex);
}
}

View File

@ -0,0 +1,125 @@
<?xml version="1.0"?> <!-- -*- Mode: HTML -*- -->
<!--
The contents of this file are subject to the Netscape Public
License Version 1.1 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at http://www.mozilla.org/NPL/
Software distributed under the License is distributed on an "AS
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
implied. See the License for the specific language governing
rights and limitations under the License.
The Original Code is Mozilla Communicator client code, released
March 31, 1998.
The Initial Developer of the Original Code is Netscape
Communications Corporation. Portions created by Netscape are
Copyright (C) 1998-1999 Netscape Communications Corporation. All
Rights Reserved.
Contributor(s): Masaki Katakai <katakai@japan.sun.com>
-->
<?xml-stylesheet href="chrome://navigator/skin/navigator.css" type="text/css"?>
<?xul-overlay href="chrome://global/content/dialogOverlay.xul"?>
<!DOCTYPE window SYSTEM "chrome://global/locale/printdialog.dtd">
<window xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
id="printDialog"
onload="onLoad();"
onunload="onUnload();"
title="&printDialog.title;"
debug="false"
class="dialog"
align="vertical"
persist="screenX screenY"
screenX="24" screenY="24">
<script language="JavaScript" src="chrome://global/content/printdialog.js"/>
<grid>
<columns><column/><column flex="1"/></columns>
<rows>
<row>
<text class="label" value="&printTo.label;" flex ="1"/>
<radiogroup id="destGroup" orient="horizontal">
<radio group = "destGroup" id = "printerRadio" value = "&printerRadio.label;"
oncommand = "doPrintToFile( false );"/>
<radio group = "destGroup" id = "fileRadio" value = "&fileRadio.label;"
oncommand = "doPrintToFile( true );"/>
</radiogroup>
</row>
<row>
<text id="cmdLabel" class="label" value="&cmdInput.label;" flex ="1" align="left"/>
<textfield id="cmdInput" flex ="1"/>
</row>
<row valign="baseline">
<text id="fileLabel" class="label" value="&fileInput.label;"/>
<box>
<textfield id="fileInput" flex ="1"/>
<button id="chooseFile" value = "&chooseButton.label;" oncommand = "onChooseFile()"/>
</box>
</row>
<row>
<text class="label" value="&reverseGroup.label;" flex ="1"/>
<radiogroup id="reverseGroup" orient="horizontal">
<radio group = "reverseGroup" id = "firstRadio" value = "&firstRadio.label;" />
<radio group = "reverseGroup" id = "lastRadio" value = "&lastRadio.label;" />
</radiogroup>
</row>
<row>
<text class="label" value="&colorGroup.label;" flex ="1"/>
<radiogroup id="colorGroup" orient="horizontal">
<radio group = "colorGroup" id = "grayRadio" value = "&grayRadio.label;" />
<radio group = "colorGroup" id = "colorRadio" value = "&colorRadio.label;"/>
</radiogroup>
</row>
<row>
<text class="label" value="&paperGroup.label;" flex ="1"/>
<radiogroup id="paperGroup" orient="vertical">
<radio group = "paperGroup" id = "letterRadio" value = "&letterRadio.label;"/>
<radio group = "paperGroup" id = "legalRadio" value = "&legalRadio.label;"/>
<radio group = "paperGroup" id = "exectiveRadio" value = "&exectiveRadio.label;"/>
<radio group = "paperGroup" id = "a4Radio" value = "&a4Radio.label;" />
</radiogroup>
</row>
<row>
<text class="label" value="&marginInput.label;" flex ="1"/>
<box orient="horizontal">
<box orient="horizontal">
<text class = "label" id = "topLabel" for = "topInput" value = "&topInput.label;" />
<textfield id = "topInput" style = "width:5em;" onkeyup="checkValid(this.id)"/>
</box>
<box orient="horizontal">
<text class = "label" id = "bottomLabel" for = "bottomInput" value = "&bottomInput.label;" />
<textfield id = "bottomInput" style = "width:5em;" onkeyup="checkValid(this.id)"/>
</box>
<box orient="horizontal">
<text class = "label" id = "leftLabel" for = "leftInput" value = "&leftInput.label;" />
<textfield id = "leftInput" style = "width:5em;" onkeyup="checkValid(this.id)"/>
</box>
<box orient="horizontal">
<text class = "label" id = "rightLabel" for = "rightInput" value = "&rightInput.label;" />
<textfield id = "rightInput" style = "width:5em;" onkeyup="checkValid(this.id)"/>
</box>
</box>
</row>
</rows>
</grid>
<!-- used to store the print -->
<foo style="display:none;" id="printButton" value="&printButton.label;"/>
<!-- used to store the print -->
<foo style="display:none;" id="fpDialog" value="&fpDialog.title;"/>
<separator/>
<!-- Places to overlay common dialog buttons and keyset -->
<box id="okCancelButtons"/>
<keyset id="keyset"/>
</window>

View File

@ -32,6 +32,7 @@ CHROME_L10N_DIR=global/locale
CHROME_L10N = \
platformGlobalOverlay.dtd \
platformDialogOverlay.dtd \
printdialog.dtd \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,35 @@
<!-- extracted from printdialog.xul -->
<!ENTITY printButton.label "Print">
<!ENTITY printDialog.title "Print">
<!ENTITY fpDialog.title "Save File">
<!ENTITY printTo.label "Print To:">
<!ENTITY printerRadio.label "Printer">
<!ENTITY fileRadio.label "File">
<!ENTITY cmdInput.label "Print Command:">
<!ENTITY fileInput.label "File:">
<!ENTITY chooseButton.label "Choose File...">
<!ENTITY reverseGroup.label "Print:">
<!ENTITY firstRadio.label "First Page First">
<!ENTITY lastRadio.label "Last Page First">
<!ENTITY colorGroup.label "Color:">
<!ENTITY grayRadio.label "GrayScale">
<!ENTITY colorRadio.label "Color">
<!ENTITY paperGroup.label "Paper Size:">
<!ENTITY letterRadio.label "Letter (8 1/2 x 11 in.)">
<!ENTITY legalRadio.label "Legal (8 1/2 x 14 in.)">
<!ENTITY exectiveRadio.label "Executive (7 1/2 x 10 in.)">
<!ENTITY a4Radio.label "A4 (210 x 297 mm)">
<!ENTITY marginInput.label "Margins(inches):">
<!ENTITY topInput.label "Top:">
<!ENTITY bottomInput.label "Bottom:">
<!ENTITY leftInput.label "Left:">
<!ENTITY rightInput.label "Right:">