#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	option_page,
	option_folder,
	option_recursive,
	option_open,
	option_embed,
	option_project,
	option_specified,
};
//put additional include files here

#include <../OriginLab/theme_utils.h>
#define PAGE_OPTION_OFFSET			0

//put your own support static functions here

static void _init_theme_list(TreeNode& trTheme)	
{
	init_theme_list(trTheme);
	//string strCombo;
	//vector<string> vsThemeNames;
	//trTheme.GetAttribute(STR_COMBO_ATTRIB, strCombo);
	//strCombo.GetTokens(vsThemeNames, '|');
	//strCombo += '|';
	//trTheme.SetAttribute(STR_COMBO_ATTRIB, strCombo);
	if (trTheme.strVal.IsEmpty())
	{
		string strCombo;
		trTheme.GetAttribute(STR_COMBO_ATTRIB, strCombo);
		if (strCombo.GetNumTokens())
			trTheme.strVal = strCombo.GetToken(0, '|');
	}
}

static bool _gui_apply_theme(LPCSTR lpcszTheme, PageBase& pb)
{
	string 	strTheme(lpcszTheme);
	string strSubFolder = "Themes\\Graph\\" + strTheme + ".OTH";
	string strFile = GetAppPath(true) + strSubFolder;
	if(!strFile.IsFile())
		strFile = GetAppPath() + strSubFolder;
	//------ Folger 08/07/08 MAKE_APPLY_FORMAT_UNDOABLE
	//return pb.ApplyFormat(strFile);
	return pb.ApplyFormat(strFile, TRUE, FALSE, TRUE);
	//------ End MAKE_APPLY_FORMAT_UNDOABLE
}

static void _refresh_graphs(TreeNode &trGetN)
{
	vector<string> vsGraphs;
	int nGraphs = get_folder_pages_name(vsGraphs, trGetN.option.nVal, PAGE_OPTION_OFFSET, trGetN.graphs.strVal);
	trGetN.graphs.strVal.SetTokens(vsGraphs, STR_PAGE_LIST_SEPARATOR);
	///Kyle 07/16/08 SET_AS_ENABLE_READONLY_SCROLL_WHEN_IT_IS_NOT_SPECIFIED
	//trGetN.graphs.Enable = trGetN.option.nVal == OPTION_SPECIFIED;
	trGetN.graphs.Enable = trGetN.option.nVal == OPTION_SPECIFIED?ENABLE : ENABLE_READONLY_SCROLL;
	///End SET_AS_ENABLE_READONLY_SCROLL_WHEN_IT_IS_NOT_SPECIFIED
}


void themeApply2g(const string& theme, const int& option, const string& graphs)
{
	vector<string> vsGraphs;
	graphs.GetTokens(vsGraphs, STR_PAGE_LIST_SEPARATOR);
	int nSize = vsGraphs.GetSize();
	for (int ii=0; ii<nSize; ii++)
	{
		PageBase pb(vsGraphs[ii]);
		if (pb)
		{
			_gui_apply_theme(theme, pb);
		}
	}
}

//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int themeApply2g_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return themeApply2g_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	///Folger 06/21/07 REFRESH_GRAPHS_WHEN_THEME_CHANGE
	if (nEventID == GETNE_ON_THEME)
	{
		_init_theme_list(trGetN.theme);
		_refresh_graphs(trGetN);
	}
	///End REFRESH_GRAPHS_WHEN_THEME_CHANGE
	if (nEventID > 0)
	{
		if (!lstrcmpi(lpcszNodeName, "option"))
		{
			_refresh_graphs(trGetN);			
		}
		else if (!lstrcmpi(lpcszNodeName, "graphs"))
		{
			vector<string> vsNames;
			trGetN.graphs.strVal.GetTokens(vsNames, STR_PAGE_LIST_SEPARATOR);
			for (int ii=0; ii<vsNames.GetSize(); ii++)
			{
				PageBase pb(vsNames[ii]);
				if (!pb)
				{
					vsNames.RemoveAt(ii--);
				}
			}
			trGetN.graphs.strVal.SetTokens(vsNames, STR_PAGE_LIST_SEPARATOR);
		}
	}
	
	if (trGetN.graphs.strVal.IsEmpty())
	{
		///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
		//strErrMsg = CER_EMPTY_VARIABLE;
		//strErrMsg += ":" + _L("Graphs");
		strErrMsg = CER_NO_GRAPH;
		///end UPDATE_XF_ERR_THROW
		bOKEnable = false;
		return false;
	}
	return false;

}

int themeApply2g_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return false;
}
//nGetNDialog = 0,1,2, corresponding to the XF GetN dropdown, 0 = dialog is not used
//you can change nRet to be one of XFEVT_PROCEED (defualt), XFEVT_ABORT, XFEVT_PROCEED_NO_DLG or XFEVT_PROCEED_WITH_DLG(when nGetNDialog = 0)
//nGetNDialog = -1 when Ok on GetN dialog to allow handler to preocess <auto> etc variables
//dwCntrl is combination of bits
//LTXF_FROM_GUI_MENU, LTXF_FROM_GUI_PROMPT, LTXF_FROM_AUTO_UPDATE, LTXF_FROM_FUNCTION, LTXF_THEME_USED, LTXF_CHANGE_PARAM
void themeApply2g_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if (nGetNDialog >= 0)
	{
		if (nGetNDialog == 1)
			_init_theme_list(trGetN.theme);
		_refresh_graphs(trGetN);
	}
}
//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int themeApply2g_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
