#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	type_dat,
	type_txt,
	type_csv,
	type_all,
};
enum{
	separator_tab,
	separator_comma,
	separator_space,
	separator_ct,
	separator_cs,
};
//put additional include files here
#include <GetNBox.h>
#include <oExtFile.h>
#include <okocUtils.h>		///Folger 04/16/07 REPLACE_BY_OKOC_OUT_MSG

// modofication log
/*
* Folger 04/16/07 REPLACE_BY_OKOC_OUT_MSG
*/
//--- Sim 05-25-2007 GET_DEFAULT_FROM_ORIGIN_OPTIONS
//--- Sim 06-05-2007 WARNING_EXPORT_TO_EXISTING_FILE
//--- Sim 06-18-2007 ADD_SHOW_DLG_CHECK_BOX_TO_SAVE_AS_DLG
//--- Sim 06-18-2007 CENTRALIZED_FILE_NAME
/// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
/// Sim 07-13-2007 FIX_CHECK_PATH
/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
/// Hong 02/19/08 v8.0807 FIX_EXPORT_FAIL_WHEN_PATH_NOT_EXIST
/// Hong 02/22/08 v8.0810 FIX_EXPORT_FAIL_CREATE_PATH_IF_MULTI_LEVEL_NOT_EXIST
/// Sim 05-07-2009 QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG
/// Sim 09-28-2009 QA81-14383-P1 FIX_LOAD_THEME_FAILED_TO_UPDATE_FILE_EXTENSION
/// Sim 07-09-2009 QA80-13891-P2 EXPORT_FILE_SUPPRESS_FILE_EXIST_PROMPT
/// Hong 10/16/09 QA80-13230-P5 EXPORT_ASCII_FAIL_WITH_SHEET_NAME_ALL_NUMERIC
/// Hong 11/20/09 QA80-14383-P2 FIX_MAT_DIGITMODE_CHANGE_IF_EXP_XY
enum{
	FILE_TYPE_DAT,
	FILE_TYPE_TXT,
	FILE_TYPE_CSV,
	FILE_TYPE_ALL	
};

enum{
	SEP_TAB,
	SEP_COMMA,
	SEP_SPACE,
	SEP_COMMA_AND_TAB,
	SEP_COMMA_AND_SPACE
};

enum{
	PATH_CONSISTENT_WITH_TYPE,
	TYPE_CONSISTENT_WITH_PATH,
	TYPE_PATH_JUST_CHECK_VALUES
};

#define STR_NODE_TYPE "type"
#define STR_NODE_PATH "path"
#define STR_NODE_SEP  "separator"

#define	CHAR_SLASH					'\\'
#define	CHAR_POINT					'.'
#define STR_STAR_POINT				"*."
#define STR_LEFT_SQUARE_BRACKET		"["
#define STR_RIGHT_SQUARE_BRACKET	"]"

#define STR_ERR_FAIL_UPDATE_PATH "Failed to update path"

#define STR_ATTRIB_OLD_SEP 		"oldsep"
#define STR_ATTRIB_OLD_TYPE		"oldtype"

#define STR_ASCII_FILE_GROUP "ASCII" ///---Sim 05-25-2007 GET_DEFAULT_FROM_ORIGIN_OPTIONS

#define STR_EXPORT_ASCII_FILE_GROUP "ASCIIEXP" ///---Sim 06-18-2007 ADD_SHOW_DLG_CHECK_BOX_TO_SAVE_AS_DLG

//#define STR_ONLY_FILE_NAME_ATTRIB  "OnlyFileName" ///---Jake 06/27/07 STORE_USER_FILE_NAME

///---Sim 05-25-2007 GET_DEFAULT_FROM_ORIGIN_OPTIONS
static string _get_default_path()
{
	string strPath = get_file_group_default_path(STR_ASCII_FILE_GROUP);
	if ( strPath.IsEmpty() )
		strPath = GetAppPath(FALSE);
	
	return strPath;
}
///---END GET_DEFAULT_FROM_ORIGIN_OPTIONS

static bool _get_file_ext(string& strExt, int nType)
{
	vector<string> vs = {"dat", "txt", "csv"};
	if(nType < 0 || nType >= FILE_TYPE_ALL)
		return false;
	strExt = vs[nType];
	return true;
}
static bool _check_add_file_ext(string& strFile, int nType)
{
	///---Sim 03-17-2008 FIX_CHECK_FILE_EXTENSION
	//if(nType < 0 || nType >= FILE_TYPE_ALL)
		//return false;
	//string strFilepath = strFile;
	//string strFname, strFext, strExt;
	//_get_file_ext(strExt, nType);	
	//if(!separate_file_name_ext(strFilepath, strFname, strFext) || strExt.CompareNoCase(strFext) != 0)
	//{
		//strFile = strFname + "." + strExt;
		//return true;
	//}
	//return false; // no change
	string strExt;
	if ( !_get_file_ext(strExt, nType) )
		return false;
	///---Sim 05-07-2009 QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG
#ifndef CHECK_EXCEED_FILE_PATH_LENGTH_LIMITATION
	return add_file_extension(strFile, strExt);
#else
	bool bRet = add_file_extension(strFile, strExt);
	if ( bRet )
		check_trim_file_path_if_too_long(strFile);
	return bRet;
#endif
	///---END QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG
	///---END FIX_CHECK_FILE_EXTENSION
}
	
//put your own support static functions here
//if type set CSV, sep should set to comma auto, but if change type back, sep should restore
static bool _update_type_related_nodes(TreeNode& trGetN)
{
	trGetN.separator.Show = (trGetN.type.nVal == FILE_TYPE_CSV) ? false : true;
	trGetN.separator.nVal = (trGetN.type.nVal == FILE_TYPE_CSV) ? SEP_COMMA : trGetN.separator.nVal;
	
	int nOldType;
	bool bRet = trGetN.type.GetAttribute(STR_ATTRIB_OLD_TYPE, nOldType);
	if(bRet && (trGetN.type.nVal != FILE_TYPE_CSV && nOldType == FILE_TYPE_CSV) )
	{
		int nOldSep;
		if(trGetN.separator.GetAttribute(STR_ATTRIB_OLD_SEP, nOldSep))
			trGetN.separator.nVal = nOldSep;
	}
	
	if(trGetN.type.nVal != FILE_TYPE_CSV) 
		trGetN.separator.SetAttribute(STR_ATTRIB_OLD_SEP, trGetN.separator.nVal);
		
	trGetN.type.SetAttribute(STR_ATTRIB_OLD_TYPE, trGetN.type.nVal);
	return true;// to indicate DynaControl update needed
}

static bool _get_file_type(TreeNode& trType, int nType, string& strType)
{
	string strCombo;
	if(!trType.GetAttribute(STR_COMBO_ATTRIB, strCombo))
		return false;
	
	strType = strCombo.GetToken(nType, '|');
	
	if(nType == FILE_TYPE_TXT || nType == FILE_TYPE_ALL)
	{
		strType = strType.GetToken(1, '(');
		strType.TrimRight(')');
	}
		
	return true;
}

static bool _set_file_name_by_ml_name(string& strFilename, string& strPath, TreeNode& trGetN)
{
	MatrixObject	mo;
	if(!okxf_resolve_string_get_origin_object(trGetN.im.strVal, &mo))
		return false;
		
	MatrixLayer ml;
	mo.GetParent(ml);
	strFilename = ml.GetName();
	
	return true;
}

//if user change file ext in path edit box, should check and make consistent
//e.g. if type is csv, file path should be such as "c:\\test.csv" or "c:\\test", and "c:\\test"
//must change to "c:\\test.csv"
//if user input "c:\\test.dat", should change type to dat
//if user input "c:\\test.abc", should change type to All Files
//if user change type to dat, file path should change to "c:\\test.dat"
//if user change type to All files, file path should change to "c:\\test"
static string _make_type_and_ext_consistent(TreeNode& trType, TreeNode& trPath, string& strExtFromPath, int nCtrl = PATH_CONSISTENT_WITH_TYPE)
{
	string strExtFromType;
	_get_file_ext(strExtFromType, trType.nVal);
	
	strExtFromPath = strExtFromPath.IsEmpty() ? strExtFromType : strExtFromPath;
	
	if(strExtFromPath.CompareNoCase(strExtFromType) != 0)
	{
		if(nCtrl == TYPE_CONSISTENT_WITH_PATH)
		{
			int nIndex = _find_type_index_by_ext(trType, strExtFromPath);
			trType.nVal = nIndex;
		}
		else if(nCtrl == PATH_CONSISTENT_WITH_TYPE)
		{
			strExtFromPath = strExtFromType;
		}
	}
	
	return strExtFromPath;
}

static int _find_type_index_by_ext(TreeNode& trType, string& strExt)
{
	for(int ii = FILE_TYPE_DAT; ii <= FILE_TYPE_ALL; ii++)
	{
		string strTypeTemp;
		_get_file_type(trType, ii, strTypeTemp);
		strTypeTemp.TrimLeft(STR_STAR_POINT);
		if(strTypeTemp.CompareNoCase(strExt) == 0)
			break;
	}
	
	return ii = ii >=FILE_TYPE_ALL ? FILE_TYPE_ALL : ii;
}

static bool _gui_check_update_path(TreeNode& trGetN, int nCtrl = PATH_CONSISTENT_WITH_TYPE)
{	
	TreeNode 	trType = trGetN.type;
	TreeNode 	trPath = trGetN.path;
	
	//get path, if path is empty, get current file path
	string		strPath = trPath.strVal;
	if(strPath.IsEmpty())
		///---Sim 05-25-2007 GET_DEFAULT_FROM_ORIGIN_OPTIONS
		//strPath = GetAppPath();
		strPath = _get_default_path();
		///---END GET_DEFAULT_FROM_ORIGIN_OPTIONS
		
	/// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	bool 	bIsSpecialPath = str_is_predefined_path(strPath);
	if(bIsSpecialPath)
		strPath = cvt_predefined_path_to_real_path(strPath);
	///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	
	//get ext and filname from path
	string strFile, strExtFromPath;
	separate_file_name_ext(strPath, strFile, strExtFromPath);
	string strFilename = GetFileName(strFile); //strFile have path
	
	//if fil name is empty, use actived matrixlayer'name as file name
	if(strFilename.IsEmpty())
	{
		if(!_set_file_name_by_ml_name(strFilename, strPath, trGetN))
			return false;
		
		strFile += strFilename;
	}
	
	//make sure path node and type node are consistent
  	string strExt = _make_type_and_ext_consistent(trType, trPath, strExtFromPath, nCtrl);
	
	//set button control
	string strType;
	_get_file_type(trType, trType.nVal, strType);
	
	string str;
	if(trType.nVal != FILE_TYPE_ALL)
		str.Format("...Save:[%s] *.%s", strType, strExt);
	else
		str.Format("...Save");
	
	trPath.SetAttribute(STR_COMBO_ATTRIB, str);	

	/// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	/*
	///Jake 06/28/07 SUPPORT_DEFAULT_KEY_NAME
	////----Jake 06/27/07 GET_COMBO_LIST_FROM_REGISTRY verytime when file type change 
	//char szLastXFName[MAXFULLPATH];
	//LT_get_str(LTVAR_LAST_XF, szLastXFName, MAXFULLPATH); //Jake 06/27/07 GET_THE_NAME_OF_LAST_XF_CALLED
	//tree_set_browse_combo_attrib(trPath, szLastXFName, false);
	////----end GET_COMBO_LIST_FROM_REGISTRY
	tree_set_browse_combo_attrib(trPath);
	///end SUPPORT_DEFAULT_KEY_NAME
	*/
	///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	
	//update path in GUI
	/// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	if( bIsSpecialPath )
	{
		char szFileName[100];
		lstrcat(szFileName, trPath.strVal);
		check_add_file_ext(szFileName, strExt);
		strPath = szFileName;
		strPath.TrimRight('.'); /// Iris There is a '.' at the last for FILE_TYPE_ALL, so trim it if has.
	}
	else
	///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	strPath = (trGetN.type.nVal == FILE_TYPE_ALL && strExt.IsEmpty())  //if *.*, no need to add any ext
					? strFile + strExt : strFile + "." + strExt;
		
	///---Sim 05-07-2009 QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG
#ifdef CHECK_EXCEED_FILE_PATH_LENGTH_LIMITATION
	check_trim_file_path_if_too_long(strPath);
#endif
	///---END QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG
	trPath.strVal = strPath;
	
	/// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	tree_set_browse_combo_attrib(trPath);	
	///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	
	return true; 
}

static bool _check_path_show_warnning(string& strPath)
{
	while(strPath.IsFile())//check the file exist or not
	{
		string 	strErr;
		strErr.Format(_L("%s already exists.\nDo you want to replace it?"), strPath);
		if(IDNO == MessageBox(NULL, strErr, _L("Attention!"), MB_YESNO))	
		{
			string strName = InputBox(_L("Please enter a new name"));
			if(strName.IsEmpty())
				return false;
			string strExt;
			separate_file_name_ext(strPath, NULL, strExt);
			strPath = GetFilePath(strPath) + strName + "." + strExt;
			///---Sim 05-07-2009 QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG
#ifdef CHECK_EXCEED_FILE_PATH_LENGTH_LIMITATION
			check_trim_file_path_if_too_long(strPath);
#endif
			///---END QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG
		}	
		else
			return true;
	}
	return true;
}

static void _get_separator(string& strSep, int nType, int nSep)
{
	switch(nSep)
	{
	case SEP_TAB:
		strSep = "	";
		break;
	case SEP_COMMA:
		strSep = ",";
		break;
	case SEP_SPACE:
		strSep = " ";
		break;
	case SEP_COMMA_AND_TAB:
		strSep = ",	";
		break;
	case SEP_COMMA_AND_SPACE:
		strSep = ", ";
		break;
	default:
	}
}

// return true if OK should enable, false if should disable OK button
// lpcszNodeName = NULL if coming from global event like GETNE_ON_INIT
static bool _check_err_condition(TreeNode& trGetN, LPCSTR lpcszNodeName, string& strErrMsg)
{
	/// Iris 07/13/2007 v8.0659 SHARE_CHECK_PATH_CODES_FOR_ALL_EXPORT_XFS
	/*
	if(lpcszNodeName != NULL)  
	{
		/// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		//string strPath = GetFilePath(trGetN.path.strVal); //when init, path is empty, no need to check 
		string strPath = GetFilePath(strAbsolutePath);
		///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		if(!strPath.IsPath())
		{
			strErrMsg = CER_INVALID_FILEPATH;
			return false;
		}
	}
	*/
	///---Sim 07-13-2007 FIX_CHECK_PATH
	//check_relative_path(trGetN.path.strVal, false, &strErrMsg);
	///---END FIX_CHECK_PATH
	///end SHARE_CHECK_PATH_CODES_FOR_ALL_EXPORT_XFS
		
	if(NULL == lpcszNodeName || lstrcmpi(lpcszNodeName, STR_NODE_TYPE) == 0 || lstrcmpi(lpcszNodeName, STR_NODE_PATH) == 0)
	{
		///---Sim 09-28-2009 QA81-14383-P1 FIX_LOAD_THEME_FAILED_TO_UPDATE_FILE_EXTENSION
		//int nCtrl = TYPE_PATH_JUST_CHECK_VALUES;
		int nCtrl = PATH_CONSISTENT_WITH_TYPE;
		///---END QA81-14383-P1 FIX_LOAD_THEME_FAILED_TO_UPDATE_FILE_EXTENSION
		if(lpcszNodeName && lstrcmp(lpcszNodeName, STR_NODE_TYPE) == 0)
			nCtrl = PATH_CONSISTENT_WITH_TYPE;
		else if(lpcszNodeName && lstrcmp(lpcszNodeName, STR_NODE_PATH) == 0)
			nCtrl = TYPE_CONSISTENT_WITH_PATH;
		if(!_gui_check_update_path(trGetN, nCtrl))
		{
			strErrMsg = STR_ERR_FAIL_UPDATE_PATH;
			return false;
		}
	}
	
	///---Sim 07-13-2007 FIX_CHECK_PATH
	bool bOK = true;
	check_relative_path(trGetN.path.strVal, false, &strErrMsg, &bOK);
	if( !bOK )
		return false;
	///---END FIX_CHECK_PATH
	
	///---Sim 06-05-2007 WARNING_EXPORT_TO_EXISTING_FILE
	/// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	//if ( trGetN.path.strVal.IsFile() )
	string strAbsolutePath = cvt_predefined_path_to_real_path(trGetN.path.strVal); /// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	if(strAbsolutePath.IsFile())
	///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	{
		strErrMsg = EXPWARN_FILE_EXIST;
		strErrMsg += ":";
		/// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		//strErrMsg += GetFileName(trGetN.path.strVal);
		strErrMsg += GetFileName(strAbsolutePath);
		///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	}
	///---END WARNING_EXPORT_TO_EXISTING_FILE
	
	return true;
}

///---Sim 06-18-2007 CENTRALIZED_FILE_NAME
static void _get_corresponding_file_name(string& strFileName, MatrixObject& mo)
{
	strFileName = "untitled";
	
	if ( mo.IsValid() )
	{
		strFileName = mo.GetLongName();
		if ( strFileName.IsEmpty() )
		{
			MatrixLayer ml;
			mo.GetParent(ml);
			
			strFileName = ml.GetName() + "_" + mo.GetName();
		}
	}
}
///---END CENTRALIZED_FILE_NAME

///Jake 06/28/07 MOVE_CODES_TO_GLOBAL_FUN tree_set_only_file_name_attrib
/*
//---Jake 06/27/07 STORE_USER_FILE_NAME
static void _set_only_file_name_attrib(TreeNode& trGetN, LPCSTR lpcszFileName)
{
	string strOnlyFileName = GetFileName(lpcszFileName);
	trGetN.path.SetAttribute(STR_ONLY_FILE_NAME_ATTRIB,strOnlyFileName);
}
//---end STORE_USER_FILE_NAME
*/
///end MOVE_CODES_TO_GLOBAL_FUN

void expMatASC(const MatrixObject& im, const int& type, const string& path, const int& separator, const int& precision, const int& missing, const int& xygrid)
{
	
	if(!im.IsValid())
		XF_THROW(CER_NO_MATRIX);
	
	// check extention
	string strFilePath = path;
	_check_add_file_ext(strFilePath, type);
	// add code to check basic path
	strFilePath = cvt_predefined_path_to_real_path(strFilePath);/// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	
	string strPath = GetFilePath(strFilePath);
	if(!strPath.IsPath())
	{
		/// Hong 02/19/08 v8.0807 FIX_EXPORT_FAIL_WHEN_PATH_NOT_EXIST
		//XF_THROW(CER_INVALID_FILEPATH);
		//if( !CreateDirectory(strPath, NULL) )
		if( !CheckMakePath(strPath) ) /// Hong 02/22/08 v8.0810 FIX_EXPORT_FAIL_CREATE_PATH_IF_MULTI_LEVEL_NOT_EXIST
		{
			XF_THROW(CER_FAIL_TO_CREATE_PATH);
		}
		/// end FIX_EXPORT_FAIL_WHEN_PATH_NOT_EXIST
	}	
	
	if( !_check_path_show_warnning(strFilePath) )
		XF_THROW(CER_INVALID_FILEPATH);
		
	int nRowSize = im.GetNumRows();
	int nColSize = im.GetNumCols();
	
	string strSep;
	_get_separator(strSep, type, separator);
	
	file expFile;
	if(!expFile.Open(strFilePath, file::modeCreate | file::modeWrite))
		XF_THROW(XFERR_FAIL_OPEN_FILE);
	
	MatrixLayer ml;
	im.GetParent(ml);
	string strMlName = ml.GetName();
	MatrixPage mp;
	ml.GetParent(mp);
	string strMpName = mp.GetName();
	
	DWORD dwCtrl = 0;
	if(precision)
		dwCtrl |= GDAT_FULL_PRECISION;
	if(missing)
		dwCtrl |= GDAT_MISSING_AS_DASHDASH;
	
	vector<string> vsXIndex, vsYIndex;
	if(xygrid)
	{
		MatrixObject mtxObj(ml, -1);
		double dxMin, dxMax, dyMin, dyMax,dStepx,dStepy;
		mtxObj.GetXY(dxMin, dyMin, dxMax, dyMax);
		dStepx = (dxMax - dxMin) / (nColSize - 1);
		dStepy = (dyMax - dyMin) / (nRowSize - 1);
	
		vector<double> vdX, vdY;	
		vdX.Data(dxMin, dxMax, dStepx);
		vdY.Data(dyMin, dyMax, dStepy);
		///Joseph 01/24/07 FIX_BUG_ABOUT_PRECISION
		//vector<string> vsFormat;
		//vsFormat.InsertAt(0, "*15", vdX.GetSize()); //should convert with full precision
		//convert_double_vector_to_string_vector(vdX, vsXIndex, nColSize, vsFormat);
		//convert_double_vector_to_string_vector(vdY, vsYIndex, nRowSize, vsFormat);
		/// Hong 11/20/09 QA80-14383-P2 FIX_MAT_DIGITMODE_CHANGE_IF_EXP_XY
		//mtxObj.SetDigitMode(2);
		//int nDigits = mtxObj.GetDigits( );
		/// end FIX_MAT_DIGITMODE_CHANGE_IF_EXP_XY
		vector<string> vsXFormat;
		vector<string> vsYFormat;
		string strFormat = "*";
		//temp code, now full precison
		//if(!precision)
			//strFormat = strFormat + nDigits;
		//else
			strFormat = strFormat + "15";
		vsXFormat.InsertAt(0, strFormat, vdX.GetSize());
		vsYFormat.InsertAt(0, strFormat, vdY.GetSize());
		convert_double_vector_to_string_vector(vdX, vsXIndex, nColSize, vsXFormat);
		convert_double_vector_to_string_vector(vdY, vsYIndex, nRowSize, vsYFormat);
	}
	/// Hong 10/16/09 QA80-13230-P5 EXPORT_ASCII_FAIL_WITH_SHEET_NAME_ALL_NUMERIC
	//int nErr = export_matrix_ascii_data(&expFile, strMpName, strMlName, nRowSize, nColSize, strSep, &vsXIndex, &vsYIndex, dwCtrl);
	string		strWksRange;
	ml.GetRangeString(strWksRange);
	int nErr = export_matrix_ascii_data(&expFile, strWksRange, nRowSize, nColSize, strSep, &vsXIndex, &vsYIndex, dwCtrl);
	/// end EXPORT_ASCII_FAIL_WITH_SHEET_NAME_ALL_NUMERIC
	///Jasmine 04/13/07 PRINT_XF_MSG_TO_SCRIPT_WINDOW
	string strMessage, strRange;
	im.GetRangeString(strRange);
	///Folger 05/24/07 MODIFY_OUTPUT_INFO_MSG
	//strMessage.Format("Export matrix %s to ascii file %s", strRange, strFilePath);
	///Folger 06/05/07 ADD_FAILED_MSG
	if (nErr == 0)
		ocu_load_msg_str(XF_EXPORT_SUCCESS, &strMessage, strRange, NULL, strFilePath);
	else
		ocu_load_msg_str(XF_EXPORT_FAILED, &strMessage, strRange, NULL, strFilePath);
	///End ADD_FAILED_MSG
	///End MODIFY_OUTPUT_INFO_MSG
	
	///Jake 06/28/07 SUPPORT_DEFAULT_KEY_NAME
	/*
	//----Jake 06/27/07 GET_COMBO_LIST_FROM_REGISTRY
	char szLastXFName[MAXFULLPATH];
	LT_get_str(LTVAR_LAST_XF, szLastXFName, MAXFULLPATH);
	update_recent_path_list(szLastXFName,path);//update path to registry
	//----end GET_COMBO_LIST_FROM_REGISTRY
	*/
	update_recent_path_list(path);
	///end SUPPORT_DEFAULT_KEY_NAME
	
	///Folger 04/16/07 REPLACE_BY_OKOC_OUT_MSG
	//xf_print_msg(strMessage);
	okoc_out_msg(strMessage, 'I');
	///End REPLACE_BY_OKOC_OUT_MSG
	///End PRINT_XF_MSG_TO_SCRIPT_WINDOW
	expFile.Close();
}

//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 expMatASC_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return expMatASC_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	//----Jake 06/27/07 GET_COMBO_LIST_FROM_REGISTRY
	if(lstrcmpi(lpcszNodeName, STR_NODE_PATH) == 0)
	{
		/// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		/*
		///Jake 06/28/07 CODES_HAVE_BEEN_MOVED_TO_GLOBAL_FUN		
		//string		strPath = trGetN.path.strVal;
		//int nID = okutil_cvt_str_to_predefined_type(strPath);
		//if( nID != PDS_INVALID && nID != PDS_USER)
		//{
			//strPath = cvt_predefined_path_to_real_path(strPath);
			//string strFileName;
			//trGetN.path.GetAttribute(STR_ONLY_FILE_NAME_ATTRIB,strFileName);
			//strPath = GetFilePath(strPath) + strFileName;
			//trGetN.path.strVal = strPath;
		//}
		//_set_only_file_name_attrib(trGetN,strPath);		
		TreeNode trPath = trGetN.path;
		tree_check_set_real_path(trPath);
		tree_set_only_file_name_attrib(trPath,trPath.strVal);
		///end CODES_HAVE_BEEN_MOVED_TO_GLOBAL_FUN
		*/
		///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	}
	//----end GET_COMBO_LIST_FROM_REGISTRY
	///---Sim 09-28-2009 QA81-14383-P1 FIX_LOAD_THEME_FAILED_TO_UPDATE_FILE_EXTENSION
	//bOKEnable = _check_err_condition(trGetN, nEventID == GETNE_ON_INIT?NULL:lpcszNodeName, strErrMsg);
	bOKEnable = _check_err_condition(trGetN, nEventID==GETNE_ON_INIT || nEventID==GETNE_ON_THEME? NULL:lpcszNodeName, strErrMsg);
	///---END QA81-14383-P1 FIX_LOAD_THEME_FAILED_TO_UPDATE_FILE_EXTENSION
	
	if(lstrcmpi(lpcszNodeName, STR_NODE_SEP) == 0)
		trGetN.separator.SetAttribute(STR_ATTRIB_OLD_SEP, trGetN.separator.nVal);
	
	if(nEventID == GETNE_ON_INIT || lstrcmpi(lpcszNodeName, STR_NODE_TYPE) == 0 || lstrcmpi(lpcszNodeName, STR_NODE_PATH) == 0)
		return _update_type_related_nodes(trGetN);
	
	return false;// no need to redraw DynaControls

}

int expMatASC_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 expMatASC_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	if(nGetNDialog > -1)
	{
		///---Sim 05-18-2007 FIX_WRONG_MAT_OBJ
		//MatrixLayer	mo;
		MatrixObject mo;
		///---END FIX_WRONG_MAT_OBJ
		okxf_resolve_string_get_origin_object(trGetN.im.strVal, &mo);
		if(!mo.IsValid())
		{
			nRet = XFEVT_ABORT;
			warning_msg_box(CER_NO_MATRIX, true);
			return;
		}
		
		///---Sim 06-18-2007 ADD_SHOW_DLG_CHECK_BOX_TO_SAVE_AS_DLG
		
		//if apply theme, should ask user input a file name but not pop-up a dlg
		//if(nGetNDialog == 0)
		//{
		string strFileName = trGetN.path.strVal;
		
		if ( LTXF_FROM_GUI_MENU & dwCntrl ) // if from menu, ignore default value fname$
			strFileName = "";
	
		//if(is_empty_or_unassigned(trGetN.path.strVal))
		if(is_empty_or_unassigned(strFileName))
		{
			/*
			TreeNode trType = trGetN.type;
			string strExt;
			_get_file_ext(strExt, trType.nVal);
			strExt = trType.nVal == FILE_TYPE_ALL ? "*.*" : STR_STAR_POINT + strExt;
			string strFileCtrl = trType.nVal == FILE_TYPE_ALL ? STR_LEFT_SQUARE_BRACKET + "All Files" + strExt + STR_RIGHT_SQUARE_BRACKET + strExt
										: STR_LEFT_SQUARE_BRACKET + strExt + STR_RIGHT_SQUARE_BRACKET + strExt;
			StringArray saFileCtrl;
			saFileCtrl.Add(strFileCtrl);
			MatrixLayer ml;
			mo.GetParent(ml);
			string strMlName = ml.GetName();
			///---Sim 05-25-2007 GET_DEFAULT_FROM_ORIGIN_OPTIONS
			//string strPath = is_empty_or_unassigned(trGetN.path.strVal)? GetAppPath() : GetFilePath(trGetN.path.strVal);
			string strPath = is_empty_or_unassigned(trGetN.path.strVal)? 
						_get_default_path() : GetFilePath(trGetN.path.strVal);
			//string strPath = GetSaveAsBox(saFileCtrl, GetAppPath(), strMlName);
			strPath = GetSaveAsBox(saFileCtrl, strPath, strMlName);
			///---END GET_DEFAULT_FROM_ORIGIN_OPTIONS
			*/
			
			string strMatName;
			_get_corresponding_file_name(strMatName, mo);
			
			DWORD dwFlags = OPENBOX_FLAG_SUPPRESSPROMPT; ///---Sim 07-09-2009 QA80-13891-P2 EXPORT_FILE_SUPPRESS_FILE_EXIST_PROMPT
			int nFilesRet;
			if ( 0 != nGetNDialog )
			{
				int nShowOptionsDlg = 1;
				/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				//nFilesRet = get_saveas_box_by_file_group(strFileName, STR_EXPORT_ASCII_FILE_GROUP, _L("ASCII Data File"),
				//	0, NULL, strMatName, _L("Show Options Dialog"), &nShowOptionsDlg);
				///---Sim 07-09-2009 QA80-13891-P2 EXPORT_FILE_SUPPRESS_FILE_EXIST_PROMPT
				//nFilesRet = get_saveas_box_by_file_group(strFileName, STR_EXPORT_ASCII_FILE_GROUP, NULL,
					//0, NULL, strMatName, STR_SHOW_OPT_DLG, &nShowOptionsDlg);
				nFilesRet = get_saveas_box_by_file_group(strFileName, STR_EXPORT_ASCII_FILE_GROUP, NULL,
					dwFlags, NULL, strMatName, STR_SHOW_OPT_DLG, &nShowOptionsDlg);
				///---END QA80-13891-P2 EXPORT_FILE_SUPPRESS_FILE_EXIST_PROMPT
				/// end CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				nRet = (nShowOptionsDlg ? XFEVT_PROCEED : XFEVT_PROCEED_NO_DLG);
			}
			else
			{
				//nFilesRet = get_saveas_box_by_file_group(strFileName, STR_EXPORT_ASCII_FILE_GROUP, _L("ASCII Data File"), 0, NULL, strMatName);
				///---Sim 07-09-2009 QA80-13891-P2 EXPORT_FILE_SUPPRESS_FILE_EXIST_PROMPT
				//nFilesRet = get_saveas_box_by_file_group(strFileName, STR_EXPORT_ASCII_FILE_GROUP, NULL, 0, NULL, strMatName); /// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				nFilesRet = get_saveas_box_by_file_group(strFileName, STR_EXPORT_ASCII_FILE_GROUP, NULL, dwFlags, NULL, strMatName); /// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				///---END QA80-13891-P2 EXPORT_FILE_SUPPRESS_FILE_EXIST_PROMPT
			}
			
			///Cheney 2006-11-24 IF_USER_CANCEL_SHOULD_ABOUT_AND_SHOW_WARN
			//if(strPath.IsEmpty())
			if ( 0 == nFilesRet )
			{
				nRet = XFEVT_ABORT;
				//warning_msg_box(XFERR_UI_USER_CANCEL, false);
				return;
			}
			///end IF_USER_CANCEL_SHOULD_ABOUT_AND_SHOW_WARN
			
			//trGetN.path.strVal = strPath;
			trGetN.path.strVal = strFileName;
			
			/// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
			//tree_set_only_file_name_attrib(trGetN.path,strFileName);//---Jake 06/27/07 STORE_USER_FILE_NAME
			///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
			
			// reset file type
			string strFile, strExt;
			separate_file_name_ext(strFileName, strFile, strExt);
			trGetN.type.nVal = _find_type_index_by_ext(trGetN.type, strExt);
		}
		//}
		
		if(trGetN.type.nVal == FILE_TYPE_CSV) //if CSV, should not use default separator
			trGetN.separator.nVal = SEP_COMMA;
		///---END ADD_SHOW_DLG_CHECK_BOX_TO_SAVE_AS_DLG
		
		if(nGetNDialog > 0)
		{
			trGetN.im.Enable = 0;
		}
	}	
}
//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 expMatASC_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
