/**************************************************************************
*
*  @@@BUILDINFO@@@ 93fontPrefs-2.jsx 2.0.1.62  22-Mar-2007
*  Copyright 2006-2007 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains the property of
* Adobe Systems Incorporated  and its suppliers,  if any.  The intellectual 
* and technical concepts contained herein are proprietary to  Adobe Systems 
* Incorporated  and its suppliers  and may be  covered by U.S.  and Foreign 
* Patents,patents in process,and are protected by trade secret or copyright 
* law.  Dissemination of this  information or reproduction of this material
* is strictly  forbidden  unless prior written permission is  obtained from 
* Adobe Systems Incorporated.
**************************************************************************/

// Each Prefs object needs five methods:
// prefsObject.create (parentPane)  - create the pane and return the Pane object.
// prefsObject.preProcess()         - preprocess the pane just before being shown.
// prefsObject.postProcess()        - do any work just before the pane is being hidden.
// prefsObject.store()              - store the preferences. Return true if the Next Time dialog is needed.
// prefsObject.toDefault()          - set back to default values

var fontPrefs = {};

fontPrefs.create = function (parentPane)
{
	this.pane = parentPane.add (
	"group {											\
		orientation		: 'column',						\
		alignChildren	: 'left',						\
		visible			: false,						\
		alignment		: ['fill','fill'],				\
		language		: Group							\
		{												\
			orientation		: 'column',					\
			alignment		: 'fill',					\
			alignChildren	: 'fill',					\
			spacing			: 4,						\
			lblLang			: StaticText				\
			{											\
				alignment	: ['fill','top'],			\
				text		: '$$$/ESToolkit/PreferencesDlg/language=&Show Settings For:' \
			},											\
			grp				: Group						\
			{											\
				orientation		: 'row',				\
				list			: DropDownList			\
				{										\
					alignment		: ['fill','top'],	\
						helpTip:'$$$/ESToolkit/PreferencesDlg/htLanguage=Choose the language to display text'	\
				}										\
			},											\
		},												\
		nameSize		: Group							\
		{												\
			orientation		: 'row', 					\
			alignChildren	: 'fill',					\
			alignment		: 'fill',					\
			fontName		: Group						\
			{											\
				orientation		: 'column',				\
				spacing			: 4,					\
				alignment		: ['fill','top'],		\
				alignChildren	: 'fill',				\
				lbl				: StaticText			\
				{										\
					alignment		: ['fill','top'],	\
					text			: '$$$/ESToolkit/PreferencesDlg/textFont=&Font:' \
				},										\
				list			: DropDownList			\
				{										\
					alignment		: ['fill','top'],	\
					helpTip			:'$$$/ESToolkit/PreferencesDlg/htTextFont=Choose the font to display text' \
				},										\
			},											\
			fontSize		: Group						\
			{											\
				orientation		: 'column',				\
				spacing			: 4,					\
				alignChildren	: 'fill',				\
				lbl				: StaticText			\
				{										\
					alignment	: ['fill','top'],		\
					text		: '$$$/ESToolkit/PreferencesDlg/lblSize=Si&ze:' \
				},										\
				list			: DropDownList			\
				{										\
					alignment	: ['fill','top'],		\
					helpTip		: '$$$/ESToolkit/PreferencesDlg/htTextSize=Choose the font size to display text' \
				},										\
			},											\
		},												\
		lowerGrp		: Group							\
		{												\
			orientation		: 'row',					\
			alignment		: ['fill','fill'],			\
			alignChildren	: 'fill',					\
			lowerLeftGrp	: Group						\
			{											\
				orientation		: 'column',				\
				alignment		: ['left','fill'],		\
				alignChildren	: 'fill',				\
				spacing			: 5,					\
				lbl				: StaticText			\
				{										\
					alignment		: ['fill','top'],	\
					text			: '$$$/ESToolkit/PreferencesDlg/lblDiplayItems=&Display Items:' \
				},										\
				styles:	ListBox							\
				{										\
					properties		:					\
					{									\
						multiselect		: true			\
					},									\
					alignment		: ['left','fill'],	\
					helpTip			: '$$$/ESToolkit/PreferencesDlg/htStyles=Choose the style to display text'	\
				}										\
			},											\
			lowerRightGrp	: Group						\
			{											\
				orientation		: 'column',				\
				alignment		: ['fill','fill'],		\
				alignChildren	: 'fill',				\
				foreground		: Group					\
				{										\
					orientation		: 'column',			\
					alignment		: ['fill','top'],	\
					alignChildren	: ['fill','top'],	\
					spacing			: 4,				\
					lbl				: StaticText		\
					{									\
						alignment		: ['fill','top'], \
						text			: '$$$/ESToolkit/PreferencesDlg/lblForeground=Item Fo&reground:' \
					},									\
					list			: DropDownList		\
					{									\
						alignment		: ['fill','top'],\
						helpTip			:'$$$/ESToolkit/PreferencesDlg/htForeground=Choose the foreground color' \
					}									\
				},										\
				background		: Group					\
				{										\
					orientation		: 'column',			\
					alignment		: ['fill','top'],	\
					alignChildren	: ['fill','top'],	\
					spacing			: 4,				\
					lbl				: StaticText		\
					{									\
						alignment		: ['fill','top'], \
						text			: '$$$/ESToolkit/PreferencesDlg/lblBackground=Item Bac&kground::' \
					},									\
					list			: DropDownList		\
					{									\
						alignment		: ['fill','top'],\
						helpTip			: '$$$/ESToolkit/PreferencesDlg/htBackground=Choose the background color' \
					}									\
				},										\
				txtStyle		: Group					\
				{										\
					orientation		: 'row',			\
					boldBox			: Checkbox			\
					{									\
						text			: '$$$/ESToolkit/PreferencesDlg/bold=&Bold', \
						helpTip			: '$$$/ESToolkit/PreferencesDlg/htBold=Use a bold typeface to display text'	\
					},									\
					italicBox		: Checkbox			\
					{									\
						text			: '$$$/ESToolkit/PreferencesDlg/italic=&Italics', \
						helpTip			: '$$$/ESToolkit/PreferencesDlg/htItalic=Use an italic typeface to display text' \
					}									\
				},										\
				keywordsGrp		: Group					\
				{										\
					orientation		: 'column',			\
					alignment		: ['fill','fill'],	\
					alignChildren	: ['fill','fill'],	\
					spacing			: 4,				\
					lbl				: StaticText		\
					{									\
							alignment	: ['left','top'],\
							text		: '$$$/ESToolkit/PreferencesDlg/lblKeywords=Key&words:' \
					},									\
					list			: DropDownList		\
					{									\
						alignment		: ['fill','top'],\
						helpTip			: '$$$/ESToolkit/PreferencesDlg/htKeyword=Choose the keyword' \
					},									\
					edit			: EditText			\
					{									\
						alignment		: ['fill','fill'],\
						preferredSize	: [10,100],		\
						properties		:				\
						{								\
							multiline		: true		\
						}								\
					}									\
				}										\
			}											\
		}												\
	}");

	// Set up element shortcuts
	var pane = this.pane;
	pane.langList		= pane.language.grp.list;
	pane.fontNameList	= pane.nameSize.fontName.list;
	pane.fontSizeList	= pane.nameSize.fontSize.list;
	pane.foreList		= pane.lowerGrp.lowerRightGrp.foreground.list;
	pane.backList		= pane.lowerGrp.lowerRightGrp.background.list;
	pane.boldBox		= pane.lowerGrp.lowerRightGrp.txtStyle.boldBox; 
	pane.italicBox		= pane.lowerGrp.lowerRightGrp.txtStyle.italicBox; 
	pane.styleList		= pane.lowerGrp.lowerLeftGrp.styles;
	pane.kwdsList		= pane.lowerGrp.lowerRightGrp.keywordsGrp.list;
	pane.kwdsEdit		= pane.lowerGrp.lowerRightGrp.keywordsGrp.edit;

	// This may not be wider
	pane.styleList.maximumSize.width = 180;
	// make sure that huge font names do not push the size DDL away
	pane.fontNameList.maximumSize.width = 300;
	
	pane.langList.pane		=
	pane.fontNameList.pane	=
	pane.fontSizeList.pane	=
	pane.foreList.pane		=
	pane.backList.pane		=
	pane.boldBox.pane		=
	pane.italicBox.pane		=
	pane.styleList.pane		= 
	pane.kwdsEdit.pane		= 
	pane.kwdsList.pane		= pane;

	this.loaded = false;
	this.pane.prefsObj = this;
	this.pane.preProcess = function()
	{
		if (!this.prefsObj.loaded)
			this.prefsObj.load();
		// Fonts may have changed...
		this.prefsObj.setupFonts();
	}
	this.pane.postProcess = function()
	{
		this.kwdsEdit.onChange();
	}

	this.pane.toDefault = function()
	{
		if (this.langList.selection)
		{
			var langID = this.langList.selection.id;
			scintillaPrefs.xml.languages[langID] = scintillaPrefs.xml.defaults.languages[langID].copy();
			this.styleList.onChange();
		}
	}

	return pane;
}

/////////////////////////////////////////////////////////////////////////
// load the font options pane

fontPrefs.load = function()
{
 	if (this.loaded)
		return;
	this.loaded = true;

	// this takes a bit...
	app.setWaitCursor (true);

	var pane = this.pane;
	var item, i;
	
	////////////////////////////// Helpers ////////////////////////////
	// Populate a color list with standard colors.
	function setupColors (listBox)
	{
	    listBox.removeAll();
	    
		for (var i in colors)
		{
			var item = listBox.add ( "item", " " + i );
			item.icon = colorIcons [i];
		}
	}
	
	//////////////////////// End Helpers ////////////////////////////

	//	Create a list of languages			
	var langObjs = [];

	for (i = 0; i < lang.langIDs.length; i++)
	{
		var langID  = lang.langIDs [i];
		var langObj = languages [langID];
		if (langID != "text")
			// use the text without any Windows '&' escapes
			langObjs.push ( { id	: langID,
							  text	: langObj.menu.toString().replace (/&([^&])/,"$1") } );
	}
	function sortfn (a, b)
	{
		if (a.text < b.text)
			return -1;
		if (a.text > b.text)
			return 1;
		return 0;
	}
	langObjs.sort (sortfn);

    pane.langList.removeAll();
	for (i = 0; i < langObjs.length; i++) 
	{
		var item = pane.langList.add ( "item", langObjs[i].text );
		item.id = langObjs[i].id;				
	}
	
	this.setupFonts();

	//	Create a list of text font sizes
	var sizes = [6,7,8,9,10,11,12,14,16,18,20,22,24,28,32,36,40,48,54,60,72];
	pane.fontSizeList.removeAll();
    for( i = 0; i < sizes.length; i++)
        pane.fontSizeList.add ( "item", sizes [i] );
        
	// Setup colors.
	setupColors (pane.foreList);
	setupColors (pane.backList);

	// language changes, updates styles
	pane.langList.onChange = function()
	{
		if (!this.selection)
			return;
		if( this.selection.text == scintillaPrefs.xml.language )
			return;
		
		scintillaPrefs.xml.language = this.selection.text;
		this.pane.styleList.removeAll();
		this.pane.styleList.selection = null;
		if( this.selection != null )
		{											
			// get the language information						
			var id = this.selection.id;						
			var oneLang = scintillaPrefs.xml.languages[id];
			
			this.pane.styleList.selection = null;

			// set keywords.
			this.pane.kwdsList.removeAll();
			this.pane.kwdsList.selection = null;

			var groupLists = oneLang.keywords.@index;
			this.pane.kwdsList.removeAll();
			for( i = 0; i < groupLists.length(); ++i )
			{
				var itemText = localize ("$$$/ESToolkit/PreferencesDlg/Fonts/GroupNumber=Group %1", (i+1));
				item = this.pane.kwdsList.add ( "item", itemText );									
				item.id = groupLists[i];
				if( this.pane.kwdsList.selection == null ) 
					this.pane.kwdsList.selection = item;							
			}
			
			//	Create a list of styles for the selected language
			// get titles
			titles = oneLang.style.@title;
			var titleArray = [];
			for (var i = 0; i < titles.length(); i++)
				titleArray.push ( { index:i, text: titles [i].toString() } );
			function sortfn (a, b)
			{
				if (a.text < b.text)
					return -1;
				if (a.text > b.text)
					return 1;
				return 0;
			}
			titleArray.sort (sortfn);

			var selStyles = oneLang.selection.toString().split (',');
			var styleItem = [];
			this.pane.styleList.removeAll();
			for( i = 0; i < titleArray.length; ++i )	
			{
				item = this.pane.styleList.add ( "item", titleArray [i].text );
				item.styleIndex = titleArray [i].index;
				// select the last style(s)
				for (var j = 0; j < selStyles.length; j++)
				{
					if ( selStyles[j] == item.index )
						styleItem.push (item);
				}
			}
			
			if( styleItem != null )
			{
				this.pane.styleList.selection = styleItem;
			}
			else
			{
				// reset the other controls...
				pane.fontNameList.selection = null;
				pane.fontSizeList.selection = null;
				pane.foreList.selection = null;
				pane.backList.selection = null;
				pane.boldBox.value = false; 
				pane.italicBox.value = false; 
			}
		}
	}

	// helper: get a style XML by index
	pane.getStyleByIndex = function (index)
	{	
		if (!this.langList.selection)
			return null;
		var langID = this.langList.selection.id;
		var oneLang = scintillaPrefs.xml.languages[langID];
		return oneLang.style [index];
	}

	// font name of style of language changes
	pane.fontNameList.onChange = function()
	{
		if( this.pane.styleList.selection != null && this.selection != null )
		{
			for (var sel = 0; sel < this.pane.styleList.selection.length; sel++)
			{
				var xml = this.pane.getStyleByIndex (this.pane.styleList.selection[sel].styleIndex);
				if (xml)
					xml.@name = this.selection.text;
			}
		}
	}

	// font name of style of language changes
	pane.fontSizeList.onChange = function()
	{
		if( this.pane.styleList.selection != null && this.selection != null )
		{
			for (var sel = 0; sel < this.pane.styleList.selection.length; sel++)
			{
				var itemText = this.selection.text;
				if( itemText[0] == ' ' )
					itemText = itemText[1];
				var langID = this.pane.langList.selection.id;
				var xml = this.pane.getStyleByIndex (this.pane.styleList.selection[sel].styleIndex);
				if (xml)
					xml.@size = itemText;
			}
		}
	}

	// foreground color of style of language changes
	pane.foreList.onChange = function()
	{
		if( this.pane.styleList.selection != null && this.selection != null)
		{
			for (var sel = 0; sel < this.pane.styleList.selection.length; sel++)
			{
				var xml = this.pane.getStyleByIndex (this.pane.styleList.selection[sel].styleIndex);
				if (xml)
					xml.@fore = this.selection.text.substr (1);
			}
		}
	}

	// background color of style of language changes
	pane.backList.onChange = function()
	{
		if( this.pane.styleList.selection != null && this.selection != null )
		{
			for (var sel = 0; sel < this.pane.styleList.selection.length; sel++)
			{
				var xml = this.pane.getStyleByIndex (this.pane.styleList.selection[sel].styleIndex);
				if (xml)
					xml.@back = this.selection.text.substr (1);
			}
		}
	}

	// bold of style of language changes;
	pane.boldBox.onClick = function()
	{
		if( this.pane.styleList.selection != null )
		{
			var value = this.value ? "true" : "false";
			for (var sel = 0; sel < this.pane.styleList.selection.length; sel++)
			{
				var xml = this.pane.getStyleByIndex (this.pane.styleList.selection[sel].styleIndex);
				if (xml)
					xml.@bold = value;
			}
		}
	}

	// italic of style of language changes
	pane.italicBox.onClick = function()
	{
		if( this.pane.styleList.selection != null )
		{
			var value = this.value ? "true" : "false";
			for (var sel = 0; sel < this.pane.styleList.selection.length; sel++)
			{
				var xml = this.pane.getStyleByIndex (this.pane.styleList.selection[sel].styleIndex);
				if (xml)
					xml.@italics = value;
			}
		}
	}
	
	// keywords group of style of language changes
	pane.kwdsList.onChange = function()
	{
		this.pane.kwdsEdit.onChange();
	}

	// keywords of style of language changes
	var oldLangID = "";
	var oldGroup = "";
	pane.kwdsEdit.onChange = function()
	{
		// apply to the old language and group
		if( oldLangID != "" )
		{
			scintillaPrefs.xml.languages[oldLangID].keywords[oldGroup].setChildren( this.text );
		}
	
		if( this.pane.kwdsList.selection == null 
		 || this.pane.kwdsList.selection == null )
		{
			oldLangID = "";
			oldGroup = "";
			this.text = "";
		}
		else
		{
			// set the text from the new language and group
			var langID = this.pane.langList.selection.id;
			var currentGroup = this.pane.kwdsList.selection.index;
			this.text = scintillaPrefs.xml.languages[langID].keywords[currentGroup].toString();
			
			// save the old language and group
			oldLangID = langID;
			oldGroup = currentGroup;
		}
	}
	
	// styles changes, updates fonts and colors
	// If multiselect, deselect anything that differs between styles
	pane.styleList.onChange = function()
	{
		if( this.selection != null && this.pane.langList.selection != null )
		{
			// Need to collect font information; if there are multiple values,
			// do not select anything
			// Start with the first style, and set differing styles to NOT_SET (which is an item never found)
			var NOT_SET = "***";
			var xml = this.pane.getStyleByIndex (this.selection[0].styleIndex);
			
			if( xml )
			{
			    var values = lang.getStyleValues( xml );
			    var fontName = values.name;
			    var fontSize = values.size;
			    var fontBold = values.bold;
			    var fontItal = values.italics;
			    var fontFore = values.foreName;
			    var fontBack = values.backName;
			    // This array collects style indexes
			    var selStyles = [];
			    // Start off again at 0
			    for (var sel = 0; sel < this.selection.length; sel++)
			    {
				    var currentStyle = this.selection[sel].index;
				    selStyles.push (currentStyle);
				    var current = this.pane.getStyleByIndex (this.selection[sel].styleIndex);
				    values = lang.getStyleValues( current );
				    // Update XML
				    if( current.@name == "" )
					    current.@name = values.name;
				    if( current.@size == "" )
					    current.@size = values.size;
				    if( current.@fore == "" )
					    current.@fore = values.foreName;
				    if( current.@back == "" )
					    current.@back = values.backName;
				    if( current.@bold == "" && values.bold)
					    current.@bold = values.bold;
				    if( current.@italics == "" && values.italics)
					    current.@italics = values.italics;
				    // Check for the same values
				    if (values.name != fontName)
					    fontName = NOT_SET;
				    if (values.size != fontSize)
					    fontSize = NOT_SET;
				    if (values.bold != fontBold)
					    fontBold = NOT_SET;
				    if (values.italics != fontItal)
					    fontItal = NOT_SET;
				    if (values.backName != fontBack)
					    fontBack = NOT_SET;
				    if (values.foreName != fontFore)
					    fontFore = NOT_SET;
			    }
			    // Remember this with the XML
			    var langID = this.pane.langList.selection.id;
			    scintillaPrefs.xml.languages [langID].selection = selStyles.toString();
			    // set font name
			    var item = this.pane.fontNameList.find( fontName );
			    this.pane.fontNameList.selection = item;
			    // set font size
			    item = this.pane.fontSizeList.find( fontSize );
			    this.pane.fontSizeList.selection = item;

			    // set bold 
			    this.pane.boldBox.value = (fontBold == true);
			    // set italic 
			    this.pane.italicBox.value = (fontItal == true);

			    // set foreground color
			    var sel = null;
			    var items = this.pane.foreList.items;
			    for( i = 0; i < items.length; i++ )
			    {
				    if( items[i].text == " " + fontFore )
				    {
					    sel = items[i];
					    break;
				    }
			    }
			    this.pane.foreList.selection = sel;
			    // set background color
			    sel = null;
			    items = this.pane.backList.items;
			    for( i = 0; i < items.length; i++ )
			    {
				    if( items[i].text == " " + fontBack )
				    {
					    sel = items[i];
					    break;
				    }
			    }						
			    this.pane.backList.selection = sel;
			}
		}
	}

	// select language
	if( pane.langList.selection == null )
	{
		item = pane.langList.find( scintillaPrefs.xml.language );
		if( item == null )
			item = pane.langList.find( "JavaScript" );
		if( item !=	null )
		{
			// select item
			scintillaPrefs.xml.language = "";
			pane.langList.selection = item;
		}
	}
	app.setWaitCursor (false);
}

fontPrefs.setupFonts = function()
{
	app.setWaitCursor (true);
	var currentFont = this.pane.fontNameList.selection ? this.pane.fontNameList.selection.text : null;
	var currentItem = null;
	//	Create a list of text font names
	var fontNames = app.getInstalledFonts();
	fontNames.sort();
	this.pane.fontNameList.removeAll();
	for( var i in fontNames )
	{
		var item = this.pane.fontNameList.add ( "item", fontNames[i]);
		if (fontNames[i] == currentFont)
			currentItem = item;
	}
	if (currentItem)
		this.pane.fontNameList.selection = currentItem;
	app.setWaitCursor (false);
}

/////////////////////////////////////////////////////////////////////////
// store preferences from the selected font options
fontPrefs.store = function()
{
	if (!this.loaded)
		return false;
	with (this.pane) 
	{
		if (language.grp.list.selection != null)
			scintillaPrefs.xml.language = language.grp.list.selection.text;
	}
	return false;
}

