gecko-dev/suite/common/pref/pref-fonts.js

334 lines
13 KiB
JavaScript
Raw Normal View History

try
2006-05-17 02:24:52 +00:00
{
var enumerator = Components.classes["@mozilla.org/gfx/fontenumerator;1"].createInstance();
2006-05-17 02:24:52 +00:00
if( enumerator )
enumerator = enumerator.QueryInterface(Components.interfaces.nsIFontEnumerator);
var fontCount = { value: 0 }
fonts = enumerator.EnumerateAllFonts(fontCount);
var pref = Components.classes["@mozilla.org/preferences;1"].getService( Components.interfaces.nsIPref );
2006-05-17 02:24:52 +00:00
}
catch(e)
2006-05-17 02:24:52 +00:00
{
}
2006-05-17 02:24:52 +00:00
var fontTypes = ["serif","sans-serif", /*"cursive", "fantasy",*/"monospace"];
var variableSize, fixedSize, languageList;
var languageData = [];
var currentLanguage;
var gPrefutilitiesBundle;
2006-05-17 02:22:17 +00:00
2006-05-17 02:24:52 +00:00
// manual data retrieval function for PrefWindow
function GetFields()
{
var dataObject = [];
2006-05-17 02:24:52 +00:00
// store data for language independent widgets
var lists = ["selectLangs", "defaultFont"];
for( var i = 0; i < lists.length; i++ )
{
if( !dataObject.dataEls )
dataObject.dataEls = [];
dataObject.dataEls[ lists[i] ] = [];
dataObject.dataEls[ lists[i] ].value = document.getElementById( lists[i] ).value;
2006-05-17 02:24:52 +00:00
}
dataObject.defaultFont = document.getElementById( "defaultFont" ).value;
2006-05-17 02:24:52 +00:00
dataObject.fontDPI = document.getElementById( "browserScreenResolution" ).value;
dataObject.useDocFonts = document.getElementById( "browserUseDocumentFonts" ).checked ? 0 : 1;
// save current state for language dependent fields and store
saveState();
dataObject.languageData = languageData;
return dataObject;
2006-05-17 02:24:52 +00:00
}
// manual data setting function for PrefWindow
2006-05-17 02:24:52 +00:00
function SetFields( aDataObject )
{
languageData = aDataObject.languageData ? aDataObject.languageData : languageData ;
currentLanguage = aDataObject.currentLanguage ? aDataObject.currentLanguage : null ;
var lists = ["selectLangs", "defaultFont"];
for( var i = 0; i < lists.length; i++ )
{
var element = document.getElementById( lists[i] );
if( aDataObject.dataEls )
2006-05-17 02:24:52 +00:00
{
element.value = aDataObject.dataEls[ lists[i] ].value;
element.selectedItem = element.getElementsByAttribute( "value", aDataObject.dataEls[ lists[i] ].value )[0];
2006-05-17 02:24:52 +00:00
}
else
2006-05-17 02:24:52 +00:00
{
var prefstring = element.getAttribute( "prefstring" );
var preftype = element.getAttribute( "preftype" );
if( prefstring && preftype )
{
var prefvalue = parent.hPrefWindow.getPref( preftype, prefstring );
element.value = prefvalue;
element.selectedItem = element.getElementsByAttribute( "value", prefvalue )[0];
2006-05-17 02:24:52 +00:00
}
}
}
var resolutionField = document.getElementById( "browserScreenResolution" );
if( aDataObject.fontDPI != undefined )
resolutionField.value = aDataObject.fontDPI;
else
{
var prefvalue = parent.hPrefWindow.getPref( resolutionField.getAttribute("preftype"), resolutionField.getAttribute("prefstring") );
if( prefvalue != "!/!ERROR_UNDEFINED_PREF!/!" )
resolutionField.value = prefvalue;
}
var useDocFontsCheckbox = document.getElementById( "browserUseDocumentFonts" );
if( aDataObject.useDocFonts != undefined )
useDocFontsCheckbox.checked = aDataObject.useDocFonts ? false : true;
else
{
var prefvalue = parent.hPrefWindow.getPref( useDocFontsCheckbox.getAttribute("preftype"), useDocFontsCheckbox.getAttribute("prefstring") );
if( prefvalue != "!/!ERROR_UNDEFINED_PREF!/!" )
useDocFontsCheckbox.checked = prefvalue ? false : true ;
}
}
2006-05-17 02:24:52 +00:00
function Startup()
{
variableSize = document.getElementById( "sizeVar" );
fixedSize = document.getElementById( "sizeMono" );
languageList = document.getElementById( "selectLangs" );
gPrefutilitiesBundle = document.getElementById("bundle_prefutilities");
2006-05-17 02:24:52 +00:00
// register our ok callback function
parent.hPrefWindow.registerOKCallbackFunc( saveFontPrefs );
2006-05-17 02:24:52 +00:00
// eventually we should detect the default language and select it by default
selectLanguage();
2006-05-17 02:24:52 +00:00
}
2006-05-17 02:24:52 +00:00
function listElement( aListID )
{
this.listElement = document.getElementById( aListID );
}
listElement.prototype =
2006-05-17 02:24:52 +00:00
{
clearList:
2006-05-17 02:24:52 +00:00
function ()
{
// remove the menupopup node child of the menulist.
2006-05-17 02:24:52 +00:00
this.listElement.removeChild( this.listElement.firstChild );
},
appendString:
function ( aString )
{
var menuItemNode = document.createElement( "menuitem" );
if( menuItemNode )
{
menuItemNode.setAttribute( "label", aString );
2006-05-17 02:24:52 +00:00
this.listElement.firstChild.appendChild( menuItemNode );
}
},
2006-05-17 02:24:52 +00:00
appendStrings:
function ( aDataObject )
{
var popupNode = document.createElement( "menupopup" );
faces = aDataObject.toString().split(",");
faces.sort();
2006-05-17 02:24:52 +00:00
for( var i = 0; i < faces.length; i++ )
{
if( faces[i] == "" )
{
this.listElement.setAttribute( "value", faces[i] );
this.listElement.setAttribute( "label",
gPrefutilitiesBundle.getString("nofontsforlang") );
this.listElement.setAttribute( "disabled", "true" );
gNoFontsForThisLang = true; // hack, hack hack!
}
else
{
var itemNode = document.createElement( "menuitem" );
itemNode.setAttribute( "value", faces[i] );
itemNode.setAttribute( "label", faces[i] );
this.listElement.removeAttribute( "disabled" );
gNoFontsForThisLang = false; // hack, hack hack!
popupNode.appendChild( itemNode );
}
2006-05-17 02:24:52 +00:00
}
this.listElement.appendChild( popupNode );
}
2006-05-17 02:24:52 +00:00
};
function saveFontPrefs()
{
// if saveState function is available, assume can call it.
// why is this extra qualification required?!!!!
if( top.hPrefWindow.wsm.contentArea.saveState )
{
saveState();
parent.hPrefWindow.wsm.dataManager.pageData["chrome://communicator/content/pref/pref-fonts.xul"] = GetFields();
2006-05-17 02:24:52 +00:00
}
2006-05-17 02:24:52 +00:00
// saving font prefs
var dataObject = parent.hPrefWindow.wsm.dataManager.pageData["chrome://communicator/content/pref/pref-fonts.xul"];
2006-05-17 02:24:52 +00:00
var pref = parent.hPrefWindow.pref;
for( var language in dataObject.languageData )
{
for( var type in dataObject.languageData[language].types )
{
var fontPrefString = "font.name." + type + "." + language;
var currValue = "";
try
2006-05-17 02:24:52 +00:00
{
currValue = pref.CopyUnicharPref( fontPrefString );
}
catch(e)
{
}
if( currValue != dataObject.languageData[language].types[type] )
pref.SetUnicharPref( fontPrefString, dataObject.languageData[language].types[type] );
2006-05-17 02:24:52 +00:00
}
var variableSizePref = "font.size.variable." + language;
var fixedSizePref = "font.size.fixed." + language;
var currVariableSize = 12, currFixedSize = 12;
try
2006-05-17 02:24:52 +00:00
{
currVariableSize = pref.GetIntPref( variableSizePref );
currFixedSize = pref.GetIntPref( fixedSizePref );
}
catch(e)
{
}
if( currVariableSize != dataObject.languageData[language].variableSize )
pref.SetIntPref( variableSizePref, dataObject.languageData[language].variableSize );
if( currFixedSize != dataObject.languageData[language].fixedSize )
pref.SetIntPref( fixedSizePref, dataObject.languageData[language].fixedSize );
2006-05-17 02:24:52 +00:00
}
// font scaling
2006-05-17 02:24:52 +00:00
var fontDPI = parseInt( dataObject.fontDPI );
var documentFonts = dataObject.useDocFonts;
var defaultFont = dataObject.defaultFont;
2006-05-17 02:24:52 +00:00
try
{
var currDPI = pref.GetIntPref( "browser.display.screen_resolution" );
var currFonts = pref.GetIntPref( "browser.display.use_document_fonts" );
var currDefault = pref.CopyUnicharPref( "font.default" );
2006-05-17 02:24:52 +00:00
}
catch(e)
{
}
if( currDPI != fontDPI )
pref.SetIntPref( "browser.display.screen_resolution", fontDPI );
2006-05-17 02:24:52 +00:00
if( currFonts != documentFonts )
pref.SetIntPref( "browser.display.use_document_fonts", documentFonts );
2006-05-17 02:24:52 +00:00
if( currDefault != defaultFont )
{
pref.SetUnicharPref( "font.default", defaultFont );
2006-05-17 02:24:52 +00:00
}
}
2006-05-17 02:24:52 +00:00
function saveState()
{
for( var i = 0; i < fontTypes.length; i++ )
{
// preliminary initialisation
if( currentLanguage && !languageData[currentLanguage] )
languageData[currentLanguage] = [];
if( currentLanguage && !languageData[currentLanguage].types )
languageData[currentLanguage].types = [];
// save data for the previous language
if( currentLanguage && languageData[currentLanguage] &&
languageData[currentLanguage].types )
languageData[currentLanguage].types[fontTypes[i]] = document.getElementById( fontTypes[i] ).value;
2006-05-17 02:24:52 +00:00
}
if( currentLanguage && languageData[currentLanguage] &&
languageData[currentLanguage].types )
2006-05-17 02:24:52 +00:00
{
languageData[currentLanguage].variableSize = parseInt( variableSize.value );
languageData[currentLanguage].fixedSize = parseInt( fixedSize.value );
2006-05-17 02:24:52 +00:00
}
}
2006-05-17 02:24:52 +00:00
function selectLanguage()
{
2006-05-17 02:24:52 +00:00
// save current state
saveState();
2006-05-17 02:24:52 +00:00
if( !currentLanguage )
currentLanguage = languageList.value;
2006-05-17 02:24:52 +00:00
for( var i = 0; i < fontTypes.length; i++ )
{
// build and populate the font list for the newly chosen font type
var strFontFaces = enumerator.EnumerateFonts(languageList.value, fontTypes[i], fontCount);
var strDefaultFontFace = strFontFaces[0];
2006-05-17 02:24:52 +00:00
var selectElement = new listElement( fontTypes[i] );
selectElement.clearList();
selectElement.appendStrings(strFontFaces);
//the first font face name returned by the enumerator is our last resort
var defaultListSelection = selectElement.listElement.getElementsByAttribute( "value", strDefaultFontFace)[0];
if( languageData[languageList.value] )
2006-05-17 02:24:52 +00:00
{
// data exists for this language, pre-select items based on this information
var dataElements = selectElement.listElement.getElementsByAttribute( "value", languageData[languageList.value].types[fontTypes[i]] );
var selectedItem = dataElements.length ? dataElements[0] : defaultListSelection;
if (!gNoFontsForThisLang)
{
selectElement.listElement.selectedItem = selectedItem;
variableSize.removeAttribute("disabled");
fixedSize.removeAttribute("disabled");
variableSize.selectedItem = variableSize.getElementsByAttribute( "value", languageData[languageList.value].variableSize )[0];
fixedSize.selectedItem = fixedSize.getElementsByAttribute( "value", languageData[languageList.value].fixedSize )[0];
}
else
{
variableSize.setAttribute("disabled","true");
fixedSize.setAttribute("disabled","true");
}
2006-05-17 02:24:52 +00:00
}
else
{
try
2006-05-17 02:24:52 +00:00
{
var fontPrefString = "font.name." + fontTypes[i] + "." + languageList.value;
2006-05-17 02:24:52 +00:00
var selectVal = parent.hPrefWindow.pref.CopyUnicharPref( fontPrefString );
var dataEls = selectElement.listElement.getElementsByAttribute( "value", selectVal );
selectedItem = dataEls.length ? dataEls[0] : defaultListSelection;
if (selectedItem)
{
selectElement.listElement.value = selectVal;
selectElement.listElement.selectedItem = selectedItem;
var variableSizePref = "font.size.variable." + languageList.value;
var fixedSizePref = "font.size.fixed." + languageList.value;
var sizeVarVal = parent.hPrefWindow.pref.GetIntPref( variableSizePref );
var sizeFixedVal = parent.hPrefWindow.pref.GetIntPref( fixedSizePref );
variableSize.removeAttribute("disabled");
variableSize.value = sizeVarVal;
variableSize.selectedItem = variableSize.getElementsByAttribute( "value", sizeVarVal )[0];
fixedSize.removeAttribute("disabled");
fixedSize.value = sizeFixedVal;
fixedSize.selectedItem = fixedSize.getElementsByAttribute( "value", sizeFixedVal )[0];
}
else
{
variableSize.setAttribute("disabled","true");
fixedSize.setAttribute("disabled","true");
}
2006-05-17 02:24:52 +00:00
}
catch(e)
{
// if we don't find an existing pref, it's no big deal.
2006-05-17 02:24:52 +00:00
}
}
}
currentLanguage = languageList.value;
2006-05-17 02:24:52 +00:00
}