#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here

#include <GetNBox.h>
#include <..\originlab\theme_utils.h> ///---Sim 10-27-2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
#include <..\originlab\fu_utils.h>
#include <..\originlab\import_utils.h> 
#include <..\originlab\XFunctionEx.h> 
/// Hong 02/16/07 FIX_FAIL_COMPLIE
#ifdef _FOR_SMART_LOADING_ONLY
	#include <..\originlab\file_utils.h>
#endif // _FOR_SMART_LOADING_ONLY
/// end FIX_FAIL_COMPLIE
#include <..\originlab\spcfile.h>
//put your own support static functions here
///////////////////////////////////////////////////////////////////////
//////// modification log
/// Hong 03/28/07 v8.0591 CENTRALIZE_CHECK_CALID_RANGE
/// Hong 04/10/07 v8.0597b IGNORE_FNAME$_WHEN_FROM_MENU
/// Sim 04-17-07 QA80-9418 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
/// Sim 04-17-2007 FIX_APPEND_COMMENT_WHEN_REIMPORT
/// Sim 04-17-2007 QA80-9418-S1 ONLY_UPDATE_BINARY_INFO_WHEN_REIMPORT
/// Sim 04-17-2007 FIX_MODIFY_CURRENT_DATA_RANGE_WILL_CAUSE_THE_RANGE_IN_PAGE_STORE_IS_CHANGED_UNEXPECTED
/// Sim 04-30-2007 TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
/// Sim 05-08-2007 ADD_OPTIONS_FOR_IMPORT_MODE_OF_1ST_FILE
/// Sim 05-18-2007 REMOVE_UID_VARIABLE
/// Sim 06-25-2007 UPDATE_ORGANIZER
/// Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
/// Hong 09/01/07 QA80-7225 v8.0693 MATRIX_START_NEW_COLUMNS_CHANGE_NEW_SHEET
/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
/// Hong 04/21/08 QA80-7225 ADD_OPTION_CONTROL_WHETHER_RENAME_COL_SNAME
/// Sim 10-27-2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
///////////////////////////////////////////////////////////////////////


///Cheney 2006-12-7 PREPARE_TRINFO_WHICH_WANT_TO_SHOW_IN_ORGANIZER
#define DATAOFFSET 2415018
//enum {
	//IML_REPLACE = 0, // ASCIMP_MODE_REPLACE_DATA,
	//IML_NEW_BK, // ASCIMP_MODE_NEW_BOOKS,
	//IML_NEW_SHT, // ASCIMP_MODE_NEW_SHEETS,
//};
///end PREPARE_TRINFO_WHICH_WANT_TO_SHOW_IN_ORGANIZER

#define SPC_1ST_FILE_IMPORT_MODE_LIST 	LIST_IMPORT_MODE_REPLACE_BOOK_SHEET_COLUMN ///---Sim 05-08-2007 ADD_OPTIONS_FOR_IMPORT_MODE_OF_1ST_FILE
#define SPC_IMPORT_MODE_LIST	(SPC_1ST_FILE_IMPORT_MODE_LIST & (~IW_FLAG_IMPMODE_REPLACE)) ///---Sim 05-08-2007 REMOVE_REPLACE_OUT_OF_MULTI_FILE_IMPORT_MODE

///Cheney 2006-12-9 PREPARE_ERR_MESSAGE
static bool s_bFileValid = true;
static bool s_bRangeValid = true;
static bool s_bTreeShownValid = true;
#define OK_SURE(_OK_ENABLE) _OK_ENABLE = (s_bFileValid && s_bRangeValid && s_bTreeShownValid);

static void _shift_error_code(int& nRet)
{
	switch ( nRet )
	{
	case spcErrorNoZAxis:
		nRet = IMPERR_SPC_NO_Z_AXIS;
		break;
	case spcErrorZNotEven:
		nRet = IMPERR_SPC_Z_AXIS_NOT_EVEN;
		break;
	case spcErrorXNotEven:
		nRet = IMPERR_SPC_X_AXIS_NOT_EVEN;
		break;
	case spcErrorInitFailed:
		nRet = IMPERR_FAILED_OPEN;
		break;
	case spcErrorInvalidDataFile:
		nRet = IMPERR_INVALID_FILE;
		break;
	default:
		nRet = CER_UNKNOWN_ERROR;
	}
}

static BOOL _construct_tree_setting(StringArray& vsFileNames, TreeNode& trInfo)
{
	int nCount = vsFileNames.GetSize();
	string strFileName, strTag, strInfo;
	if ( trInfo )
		trInfo.RemoveChildrenWithPrefix();
	
	GETN_USE(trInfo)
	for ( int nIndex = 0; nIndex < nCount; nIndex++ )
	{
		strFileName = vsFileNames[nIndex];
		if ( !strFileName.IsFile() )
			return false;

		strTag.Format("file%d", nIndex+1);
		strInfo.Format("File Size: %s ", GetFileSize(strFileName));
		GETN_STR(strTag, GetFileName(strFileName, false), strInfo )
		GETN_READ_ONLY
	}
	return true;
}

static bool _file_name_event(TreeNode& trGetN, string& strErrMsg)
{
	string strFileName = trGetN.fname.strVal;
	TreeNode trNode = trGetN.trfiles;
	
	trNode.RemoveChildrenWithPrefix();
	
	if ( !check_imported_file_name(strFileName, strErrMsg) )
		return false;
	
	StringArray vsFileNames;
	separate_multiline_files(strFileName, vsFileNames);
	if ( !_construct_tree_setting(vsFileNames, trNode) )
	{
		// clean the trfiles on GUI
		trNode.RemoveChildrenWithPrefix();
		strErrMsg = CER_INVALID_IMPORT_DATA_FILE;
		return false;
	}
	
	return true;
}
///end PREPARE_ERR_MESSAGE

///Cheney 2006-12-6 HIDE_SPARKLINES_IF_IMPORT_TO_MATRIX
static bool _update_tree_shown(TreeNode& trGetN, string& strErrMsg)
{
	Tree trRange;
	trRange.AddNode(trGetN.orng);
	DataRange dr;
	dr.Create(trRange);
	
	Datasheet ds;
	int c1,c2;
	dr.GetRange(ds, c1, c2);
	
	if(!ds)
	{
		strErrMsg = CER_INVALID_RANGE; ///---Sim 04-17-2007 CORRECT_ERROR_MSG
		return false;
	}
	
	Page pgTarget;
	ds.GetParent(pgTarget);
	//if is matrix page, should set sparklines not check and hide it
	if(EXIST_MATRIX == pgTarget.GetType())
	{
		///---Sim 01-29-2007 MORE_OPTIONS_FOR_IMPORT
		//trGetN.sparklines.nVal = 0;
		//trGetN.sparklines.Show = false;
		TreeNode trSparkLines = tree_get_node_by_id(trGetN, IDE_DISP_IMP_SPARKLINES, true);
		if ( trSparkLines )
		{
			trSparkLines.nVal = 0;
			trSparkLines.Show = false;			
		}
		
		///---END MORE_OPTIONS_FOR_IMPORT
	}
	return true;
}
///end HIDE_SPARKLINES_IF_IMPORT_TO_MATRIX

///Cheney 2006-12-7 PREPARE_TRINFO_WHICH_WANT_TO_SHOW_IN_ORGANIZER
static string _get_technique_type(int nTechType)
{
	string strTechType; 
	switch(nTechType)
	{
	case spcTechGen:
		strTechType = "General";
		break;
    case spcTechGC:
    	strTechType = "Gas Chromatogram";
    	break;
    case spcTechCgm:
    	strTechType = "General Chromatogram";
    	break;
    case spcTechHPLC:
    	strTechType = "HPLC Chromatogram";
    	break;
    case spcTechFTIR:
    	strTechType = "FT-IR, FT-NIR, FT-Raman Spectrum";
    	break;
    case spcTechNIR:
    	strTechType = "NIR Spectrum";
    	break;
    case spcTechUV:
    	strTechType = "UV-VIS Spectrum";
    	break;
    case spcTechXry:
    	strTechType = "X-ray Diffraction Spectrum";
    	break;
    case spcTechMS:
    	strTechType = "Mass Spectrum";
    	break;
    case spcTechNMR:
    	strTechType = "NMR Spectrum";
    	break;
    case spcTechRmn:
    	strTechType = "Raman Spectrum";
    	break;
    case spcTechFlr:
    	strTechType = "Fluorescence Spectrum";
    	break;
    case spcTechAtm:
    	strTechType = "Atomic Spectrum";
    	break;
    case spcTechDAD:
    	strTechType = "Chromatography Diode Array Spectra";
    	break;
    case spcTechThrm:
    	strTechType = "Thermal Analysis";
    	break;
    case spcTechCD:
    	strTechType = "Circular Dichroism";
    	break;
    case spcTechCNMR:
    	strTechType = "C13 NMR Spectrum";
    	break;
    case spcTechHNMR:
    	strTechType = "H1 NMR Spectrum";
    	break;
    case spcTechDNMR:
    	strTechType = "DEPT NMR Spectrum";
    	break;
    case spcTechANMR:
    	strTechType = "APT NMR Spectrum";
    	break;
	default:
	}
	return strTechType;
}

static string _get_filetype_type(int nFileType)
{
	string strFileType; 
	switch(nFileType)
	{
	case spcFileTypeEven:
		strFileType = "Evenly spaced";
		break;
    case spcFileTypeXYY:
    	strFileType = "Unevenly spaced, single X axis";
    	break;
    case spcFileTypeXYXY:
    	strFileType = "Unevenly spaced, an X axis for each subfile";
    	break;
	default:
	}
	return strFileType;
}

static void _prepare_tree_info(TreeNode& trInfo, bool bZPlane = false, bool bWPlane = false)
{
	trInfo.RemoveChild("FirstPoint");
	trInfo.RemoveChild("LastPoint");
	trInfo.RemoveChild("LogBinary");
	trInfo.RemoveChild("LogBinarySize");
	trInfo.RemoveChild("LogDisk");
	trInfo.RemoveChild("LogDiskSize");
	trInfo.RemoveChild("LogTextSize");
	trInfo.RemoveChild("ModificationBitmask");
	trInfo.RemoveChild("NumPoints");
	trInfo.RemoveChild("SubFileIndex");
	trInfo.RemoveChild("WIncrement");
	//trInfo.RemoveChild("WNumPlanes");
	trInfo.RemoveChild("WPlaneValue");
	trInfo.RemoveChild("WType");
	trInfo.RemoveChild("XPoints");
	trInfo.RemoveChild("XType");
	trInfo.RemoveChild("YPoints");
	trInfo.RemoveChild("YPrecision");
	trInfo.RemoveChild("YType");
	trInfo.RemoveChild("ZEnd");
	trInfo.RemoveChild("ZIncrement");
	trInfo.RemoveChild("ZIncrementType");;
	trInfo.RemoveChild("ZIncrementTypeDefault");
	trInfo.RemoveChild("ZStart");
	trInfo.RemoveChild("ZType");
	
	Tree trPeakPoint;
	TreeNode trtemp = trPeakPoint.AddNode("PeakPointNumberForInterferograms");
	trtemp.nVal = trInfo.PeakPoint.nVal;
	trInfo.PeakPoint.Replace(trtemp);
	
	if(!bZPlane)
	{
		trInfo.RemoveChild("ZLabel");
		trInfo.RemoveChild("ZCustomLabel");
	}
	
	if(!bWPlane)
	{
		trInfo.RemoveChild("WLabel");
		trInfo.RemoveChild("WNumPlanes");
	}
	else
	{
		Tree trWNumPlanes;
		trtemp = trWNumPlanes.AddNode("NumberOfWPlane");
		trtemp.nVal = trInfo.WNumPlanes.nVal;
		trInfo.WNumPlanes.Replace(trtemp);
	}
	
	trInfo.TechniqueType.strVal = _get_technique_type(trInfo.TechniqueType.nVal);
	trInfo.FileType.strVal 		= _get_filetype_type(trInfo.FileType.nVal);
	if(!is_missing_value(trInfo.Date.dVal))
	{
		trInfo.Date.dVal += DATAOFFSET;
		trInfo.Date.ID = TRGP_DOUBLE;
		trInfo.Date.SetAttribute(STR_ATTRIB_NUMFMT, "D9");
	}
}
///end PREPARE_TRINFO_WHICH_WANT_TO_SHOW_IN_ORGANIZER


void impSPC(const string& fname, const TreeNode& trfiles, const int& sname, const TreeNode& options, Range& orng, TreeNode& trheader, const int& reimp)
{
	
	//vector vImportedRangeUID; ///---Sim 02-06-2007 GET_RANGE_UID_OF_IMPORTED_FILE ///---Sim 05-18-2007 REMOVE_UID_VARIABLE remove
	
	Datasheet ds;	
	int c1,c2;
	/// Hong 03/28/07 v8.0591 CENTRALIZE_CHECK_CALID_RANGE
	//orng.GetRange(ds, c1, c2);
	Page pgTarget;
	if ( !GetRangeInfo(orng, ds, pgTarget, c1, c2) )
		XF_THROW(XFERR_INVALID_RANGE);
	/// end CENTRALIZE_CHECK_CALID_RANGE
	if(!ds)
		///Cheney 2006-12-9 PREPARE_ERR_MESSAGE
		//XF_THROW(XFERR_INVALID_MATOBJ);// a better global err for not matching book would be better
		XF_THROW(CER_INVALID_PAGE);
		///end PREPARE_ERR_MESSAGE
	/// Hong 03/28/07 v8.0591 CENTRALIZE_CHECK_CALID_RANGE
	//Page pgTarget;
	//ds.GetParent(pgTarget);
	/// end CENTRALIZE_CHECK_CALID_RANGE
		
	///---Sim 03-09-2007 TEMP_FIX_HIGHLINE_REPLACE_DATA
	///---Sim 04-17-2007 FIX_MODIFY_CURRENT_DATA_RANGE_WILL_CAUSE_THE_RANGE_IN_PAGE_STORE_IS_CHANGED_UNEXPECTED
	//if ( orng )
	{
		//orng.Reset();
		//orng.Add(ds, 0);
	}
	DataRange drTarget;
	orng.Clone(drTarget);
	
	if ( !reimp && drTarget )
	{	// avoid to process highline replace data		
		/// Hong 09/08/07 v8.0698b FIX_FAIL_DUMP_OUTPUT_RANGE_ORNG
		/*
		drTarget.Reset();
		drTarget.Add(ds, 0);
		*/
		reset_range_to_first_obj(drTarget, ds);
		/// end FIX_FAIL_DUMP_OUTPUT_RANGE_ORNG
	}
	///---END FIX_MODIFY_CURRENT_DATA_RANGE_WILL_CAUSE_THE_RANGE_IN_PAGE_STORE_IS_CHANGED_UNEXPECTED
	///---END TEMP_FIX_HIGHLINE_REPLACE_DATA
	
	/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT	
	Tree trBaseInfo;
	//if( finfo )
		//trInfo = finfo;
	/// end FINFO_IS_ONLY_OUTPUT
	///Cheney 2006-12-8 FIX_IMPORT_MODE_NOT_WORK
	//int nListIndexToImportMode[] = {
		//ASCIMP_MODE_REPLACE_DATA,
		//ASCIMP_MODE_NEW_BOOKS,
		//ASCIMP_MODE_NEW_SHEETS
	//};
	//int nFileImportMode = ASCIMP_MODE_NEW_SHEETS;
	//if( 0 <= impmode && impmode < (sizeof(nListIndexToImportMode) / sizeof(int)) )
		//nFileImportMode = nListIndexToImportMode[impmode];	
	//int nFileImportMode = _GetImportModeFromListIndex(impmode);
	//if ( ASCIMP_MODE_AUTO == nFileImportMode )			
		//nFileImportMode = ASCIMP_MODE_NEW_SHEETS;
	//int nFileImportMode = _GetImportModeFromListIndex(impmode + 1);///---Sim 01-29-2007 MORE_OPTIONS_FOR_IMPORT remove
	
	//support default filter is empty
	TreeNode tnUsingFilter;
	double dDefault = 0.;
	if ( fuGetDouble(options, IDE_DISP_IMP_ORIGINVER, dDefault) )
		tnUsingFilter = options;
	///end FIX_IMPORT_MODE_NOT_WORK
	
	///---Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
	///---Sim 01-29-2007 MORE_OPTIONS_FOR_IMPORT
	int nFileImportMode = fuGetImportMode(tnUsingFilter);
	
	/*
	bool bSparkLines = fuGetSparkLine(tnUsingFilter);
	///---END MORE_OPTIONS_FOR_IMPORT
	
	XFunction xfSparkLine;
	TreeNode trXFSparkLine;
	int nPgType = pgTarget.GetType();
	
	if ( EXIST_WKS == nPgType && bSparkLines )
	{
		if(!xfSparkLine.Load(&trXFSparkLine, "sparklines", 0, true, true, true))
		{
			XF_THROW(IMPERR_IMPORT_FUNC_ERR);
		}
		xfSparkLine.SetArg("sel", 0);
	}
	*/
	int nSparklines = fuGetSparkLine(tnUsingFilter);
	///---END SUPPORT_SMART_SPARKLINE
	
	int nRet, nCols;
	SPCFile spcFile;
	
	StringArray vsFileNames;
	separate_multiline_files(fname, vsFileNames); 
	
	int nFileCount = vsFileNames.GetSize();
	if ( nFileCount <= 0 )
		XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
	
	int nIndexLayer = ds.GetIndex();
	///Cheney 2006-12-8 FIX_IMPORT_MODE_NOT_WORK
	Layer lyOlder;
	bool bNewLayer, bEmptyLayer;
	set_active_layer(ds);
	///end FIX_IMPORT_MODE_NOT_WORK
	
	/// Hong 04/21/08 QA80-7225 ADD_OPTION_CONTROL_WHETHER_RENAME_COL_SNAME
	DWORD	dwOption = 0;
	if ( sname )
		dwOption |= SPC_RENAME_SNAME;
	/// end ADD_OPTION_CONTROL_WHETHER_RENAME_COL_SNAME
	
	waitCursor wc; 
	for ( int ii = 0; ii < nFileCount; ii++ )
	{
		///Cheney 2006-12-8 FIX_IMPORT_MODE_NOT_WORK
		lyOlder = pgTarget.Layers();
		///---Sim 04-17-2007 FIX_MODIFY_CURRENT_DATA_RANGE_WILL_CAUSE_THE_RANGE_IN_PAGE_STORE_IS_CHANGED_UNEXPECTED
		//ds = GetTargetLayer(pgTarget, nIndexLayer, ii, tnUsingFilter, true, orng, nFileImportMode, c1);
		///---Sim 04-30-2007 TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
		//if ( 0 == ii && !reimp )
		//{
			//int nImportMode = get_first_file_import_mode(FILE_TYPE_SPC);
			//TreeNode trInValid;
			//
			//ds = GetTargetLayer(pgTarget, nIndexLayer, ii, trInValid, true, drTarget, nImportMode, c1);
		//}
		//else
		//{
		/// Hong 09/01/07 QA80-7225 v8.0693 MATRIX_START_NEW_COLUMNS_CHANGE_NEW_SHEET
		//ds = GetTargetLayer(pgTarget, nIndexLayer, ii, tnUsingFilter, true, drTarget, nFileImportMode, c1);
		int nTempMutilMode = nFileImportMode;
		int nTemp1stMode = fuGet1stImportMode(tnUsingFilter);
		
		MatrixLayer ml(ds);
		if ( ml ) // for matrix data, need change "Start New Columns" -> "Start New Sheet"
		{
			if ( ASCIMP_MODE_APPEND_COLS == nTemp1stMode )
				fuSet1stImportMode(tnUsingFilter, ASCIMP_MODE_NEW_SHEETS);
			
			if ( ASCIMP_MODE_APPEND_COLS == nTempMutilMode )
				fuSetImportMode(tnUsingFilter, ASCIMP_MODE_NEW_SHEETS);
		}
		
		ds = GetTargetLayer(pgTarget, nIndexLayer, ii, tnUsingFilter, true, drTarget, nFileImportMode, c1);
		
		if ( ml )
		{
			if ( ASCIMP_MODE_APPEND_COLS == nTemp1stMode )
				fuSet1stImportMode(tnUsingFilter, nTemp1stMode);
			
			if ( ASCIMP_MODE_APPEND_COLS == nTempMutilMode )
				fuSetImportMode(tnUsingFilter, nTempMutilMode);
		}
		/// end MATRIX_START_NEW_COLUMNS_CHANGE_NEW_SHEET
		//}
		///---END TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
		///---END FIX_MODIFY_CURRENT_DATA_RANGE_WILL_CAUSE_THE_RANGE_IN_PAGE_STORE_IS_CHANGED_UNEXPECTED
		bNewLayer = !is_same_layer(lyOlder, ds); 
		//ds = GetTargetLayer(pgTarget, nIndexLayer, ii, trFilter, true, orng, nFileImportMode, c1);
		///end FIX_IMPORT_MODE_NOT_WORK
		/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
		string strFile;
		strFile.Format("File%d", ii + 1); // file index to be 1
		TreeNode trInfo = trBaseInfo.AddNode(strFile);
		/// end FINFO_IS_ONLY_OUTPUT
		/// Hong 04/21/08 QA80-7225 ADD_OPTION_CONTROL_WHETHER_RENAME_COL_SNAME
		//if ( nRet = spcFile.Import(ds,vsFileNames[ii], trInfo, c1) )
		if ( nRet = spcFile.Import(ds,vsFileNames[ii], trInfo, c1, dwOption) )
		/// end ADD_OPTION_CONTROL_WHETHER_RENAME_COL_SNAME
		{
			///Cheney 2006-12-9 PREPARE_ERR_MESSAGE
			//XF_THROW(nRet);
			_shift_error_code(nRet);
			XF_THROW_EX(nRet, vsFileNames[ii]);
			///end PREPARE_ERR_MESSAGE
		}
		
		///---Sim 04-17-2007 FIX_GET_EXACT_RESULT_RANGE
	
		/*
		///Cheney 2006-12-8 FIX_IMPORT_MODE_NOT_WORK
		bEmptyLayer = is_sheet_empty(ds);
		if ( bNewLayer && bEmptyLayer )
		{
			ds.Destroy();
		}
		set_active_layer(pgTarget.Layers()); 
		ds = pgTarget.Layers();
		///end FIX_IMPORT_MODE_NOT_WORK
		*/
		
		spcFile.GetDataRange().GetRange(ds, c1, c2);
		///---END FIX_GET_EXACT_RESULT_RANGE
		
		///---Sim 02-01-2007 RENAME_CENTRALIZED
		// rename 
		///---Sim 04-17-2007 FIX_APPEND_COMMENT_WHEN_REIMPORT
		DWORD dwCtrl = 0;
		dwCtrl |= (reimp) ? COVER_LAST_ADDED_COMMENT : 0;
		setPageAndLayerName(spcFile.GetDataRange(), vsFileNames[ii], tnUsingFilter, dwCtrl);
		///---END FIX_APPEND_COMMENT_WHEN_REIMPORT
		///---END RENAME_CENTRALIZED
	
		///Cheney 2006-12-7 PREPARE_TRINFO_WHICH_WANT_TO_SHOW_IN_ORGANIZER
		bool bZPlane = trInfo.HeaderInfo.NumSubfiles.nVal == 1? false : true;
		bool bWPlane = trInfo.HeaderInfo.WNumPlanes.nVal == 0? false : true;
		_prepare_tree_info(trInfo.HeaderInfo, bZPlane, bWPlane);
		///end PREPARE_TRINFO_WHICH_WANT_TO_SHOW_IN_ORGANIZER
		
		///---Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
		//if ( EXIST_WKS == nPgType )
		if ( EXIST_WKS == pgTarget.GetType() )
		///---END SUPPORT_SMART_SPARKLINE
		{
			Worksheet wks = ds;
			vector<int> vnIDs;
			wks_get_show_labels(wks, vnIDs);
			
			vector<uint> vIndex;
			if(spcFile.IfSetSampInterv() && vnIDs.Find(vIndex, RCLT_SAMPLE_RATE) <= 0)
				vnIDs.Add(RCLT_SAMPLE_RATE);
			///---Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
			/*
			if( bSparkLines )
			{
				xfSparkLine.SetArg("c1", c1+1); // XF sparklines use LT index
				xfSparkLine.SetArg("c2", c2+1); ///---Sim 04-17-2007 FIX_RANGE
				xfSparkLine.SetArg("wks", wks);
				if ( !xfSparkLine.Evaluate() )
				{
					XF_THROW(XFERR_INVALID_MATOBJ);
					return;	
				}
				if(vnIDs.Find(vIndex, RCLT_SPARKLINE) <= 0)
					vnIDs.Add(RCLT_SPARKLINE);
			}
			*/
			bool bSparklines = update_sparkline(nSparklines, wks, c1, c2);
			
			if (bSparklines && vnIDs.Find(vIndex, RCLT_SPARKLINE) <= 0)
				vnIDs.Add(RCLT_SPARKLINE);
			///---END SUPPORT_SMART_SPARKLINE
			if(bZPlane && vnIDs.Find(vIndex, RCLT_PARAM) <= 0)
				vnIDs.Add(RCLT_PARAM);
			wks_add_show_labels(wks, vnIDs, true);
		}
		
		///---Sim 02-01-2007 ADD_RANGE_INFO
		// add data range info
		Tree trDataRange;
		///---Sim 05-18-2007 REMOVE_UID_VARIABLE
		//int UID = getRangeInfo(trDataRange, spcFile.GetDataRange());
		getRangeInfo(trDataRange, spcFile.GetDataRange());
		//vImportedRangeUID.Add(UID);
		///---Sim 05-18-2007 REMOVE_UID_VARIABLE
		
		// if current file cover existing file data, need update info of that files
		if ( !reimp )
			impinfo_UpdateExistingData(pgTarget, spcFile.GetDataRange());
	
		//impinfo_AddFile(ds, vsFileNames[ii], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, tnUsingFilter); ///---Sim 02-01-2007 UPDATE_FILTER_TREE
		///---Sim 04-17-2007 QA80-9418-S1 ONLY_UPDATE_BINARY_INFO_WHEN_REIMPORT
		//impinfo_AddFile(ds, vsFileNames[ii], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, tnUsingFilter, ii + 1, trDataRange);
		impinfo_AddFile(ds, vsFileNames[ii], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, tnUsingFilter, ii + 1, trDataRange, reimp);
		///---END QA80-9418-S1 ONLY_UPDATE_BINARY_INFO_WHEN_REIMPORT
		///---END ADD_RANGE_INFO
	} 
	///---Sim 05-18-2007 REMOVE_UID_VARIABLE
	//refresh_organizer_gui(pgTarget); ///---Sim 06-25-2007 UPDATE_ORGANIZER
	//if ( uid )
		//uid = vImportedRangeUID;
	///---Sim 05-18-2007 REMOVE_UID_VARIABLE
	if ( trheader )
		trheader.Replace(trBaseInfo.Clone(), true, true);/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
}

//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 impSPC_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return impSPC_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	///Cheney 2006-12-9 PREPARE_ERR_MESSAGE
	s_bFileValid 		= true;
	s_bTreeShownValid 	= true;
	s_bRangeValid 		= true;
	OK_SURE(bOKEnable);
	
	// process of initialize xf dialog
	if ( GETNE_ON_INIT == nEventID || GETNE_ON_THEME == nEventID)
	{
		if ( GETNE_ON_INIT == nEventID )
		{
			s_bTreeShownValid = _update_tree_shown(trGetN, strErrMsg);
			if ( !s_bTreeShownValid )
			{
				OK_SURE(bOKEnable);		
				return false;
			}
		}
	
		///support D&D show Dlg, if trfiles no child, should construct it
		if ( (GETNE_ON_INIT == nEventID && trGetN.trfiles.Children.Count() == 0) || GETNE_ON_THEME == nEventID )
		{
			s_bFileValid = _file_name_event(trGetN, strErrMsg);
			s_bRangeValid = !trGetN.orng.strVal.IsEmpty();
			if(!s_bRangeValid)
				strErrMsg = CER_INVALID_PAGE;
		}
		
		OK_SURE(bOKEnable);		
		return true;
	}
	
	//check file name if is invalid
	if ( 0 == lstrcmp(lpcszNodeName, "fname") )
	{
		s_bFileValid = _file_name_event(trGetN, strErrMsg);
		OK_SURE(bOKEnable);
		return true;
	}
	
	//check range if is invalid
	if( 0 == lstrcmp(lpcszNodeName, "orng") )
	{
		s_bRangeValid = !trGetN.orng.strVal.IsEmpty();
		if(!s_bRangeValid)
			strErrMsg = CER_INVALID_PAGE;
		
		OK_SURE(bOKEnable);
		return true;
	}
	///end PREPARE_ERR_MESSAGE
	return false;

}

int impSPC_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	
	///---Sim 01-29-2007 MORE_OPTIONS_FOR_IMPORT
	if ( 0 == strcmp(lpcszVarName, "options") )
	{
		constructOptionsForXF(tr, true, true, true);
		
		// custom import mode list
		///---Sim 05-08-2007 ADD_OPTIONS_FOR_IMPORT_MODE_OF_1ST_FILE
		TreeNode tr1stImportMode = tree_get_node_by_id(tr, IDE_DISPIMP_1STMODE, true);
		if ( tr1stImportMode )
		{
			string strCombo, strMapping;
			strCombo = getImportModeListString(strMapping, SPC_1ST_FILE_IMPORT_MODE_LIST);
			tr1stImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			tr1stImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			tr1stImportMode.nVal = ASCIMP_MODE_REPLACE_DATA;
		}
		///---END ADD_OPTIONS_FOR_IMPORT_MODE_OF_1ST_FILE
		TreeNode trImportMode = tree_get_node_by_id(tr, IDE_IMP_IMPORTMODE, true);
		if ( trImportMode )
		{
			string strCombo, strMapping;
			strCombo = getImportModeListString(strMapping, SPC_IMPORT_MODE_LIST);
			trImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			trImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			//trImportMode.nVal = ASCIMP_MODE_NEW_SHEETS;
			trImportMode.nVal = ASCIMP_MODE_APPEND_COLS;
		}
		
		fuSetTargetPage(tr, EXIST_NONE); // spc support both worksheet and matrix
		
		fuSetShowTime(tr, 0); // show time column default is none
		
		fuSetXFunctionName(tr, "impSPC");
	
		return 0;
	}
	///---END MORE_OPTIONS_FOR_IMPORT
	return -1;
	
}
//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 impSPC_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	if(nGetNDialog > 0)
	{
		//trGetN.finfo.Show = 0;
		//trGetN.trFilter.Show = 0;
		//trGetN.uid.Show = 0; 	///---Sim 05-18-2007 REMOVE_UID_VARIABLE
		trGetN.trheader.Show = 0;
		///---Sim 04-17-07 QA80-9418 REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
		// disable option not allow edit when do repeat import
		//trGetN.reimp.Show = 0;
		if(1 == trGetN.reimp.nVal)
		{
			trGetN.fname.Enable = 2;
			trGetN.orng.Enable = 2;
		}
		///---END REPEAR_IMPORT_NEED_ALLOW_PASS_DATA_SELECTION
	}
	if(nGetNDialog >= 0)
	{
		if ( LTXF_FROM_GUI_MENU == dwCntrl )
			trGetN.fname.strVal = "";
		//TreeNode trFilter = trGetN.trFilter;
		//if ( trFilter.IsValid() && trFilter.GetNodeCount()>0 )
		//{
			/////Cheney 2006-12-8 FIX_IMPORT_MODE_NOT_WORK
			////int nImportMode = fuGetImportMode(trFilter);
			////if ( ASCIMP_MODE_APPEND_ROWS == nImportMode )
				////nImportMode = ASCIMP_MODE_AUTO;
			////trGetN.impmode.nVal = _GetListIndexFromImportMode(nImportMode);
			//int nImportMode = IML_NEW_SHT; // default new sheet
			//switch ( fuGetImportMode(trFilter) )
			//{
			//case ASCIMP_MODE_REPLACE_DATA:
				//nImportMode = IML_REPLACE;
				//break;
			//case ASCIMP_MODE_NEW_BOOKS:
				//nImportMode = IML_NEW_BK;
				//break;
			//case ASCIMP_MODE_NEW_SHEETS:
				//nImportMode = IML_NEW_SHT;
				//break;
			//default:
			//}
			//trGetN.impmode.nVal = nImportMode; 
			/////end FIX_IMPORT_MODE_NOT_WORK
			//trGetN.sparklines.nVal = fuGetSparkLine(trFilter);
		//}
	}
	/// Hong 12/22/06 CHECK_FNAME_EMPTY_UNASSIGNED
	if(nGetNDialog > -1)
	{
		string strFileName = trGetN.fname.strVal;
		
		/// Hong 04/10/07 v8.0597b IGNORE_FNAME$_WHEN_FROM_MENU
		if ( LTXF_FROM_GUI_MENU & dwCntrl ) // if from menu, ignore default value fname$
			strFileName = "";
		/// end IGNORE_FNAME$_WHEN_FROM_MENU
		
		if(is_empty_or_unassigned(strFileName))
		{			
			/// Hong 05/31/07 v8.0630 MORE_ON_ACCESS_TO_FDLOG
			//int nFilesRet = get_open_box_by_file_group(strFileName, "ThermoGalactic", "import Thermo Galactic file", true);
			int nFilesRet;
			if ( 0 != nGetNDialog )
			{
				int nShowOptionsDlg = 1;
				/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				//nFilesRet = get_open_box_by_file_group(strFileName, "ThermoGalactic", _L("Import Thermo Files"),
				//	OPENBOX_FLAG_MULTIFILES, NULL, NULL, _L("Show Options Dialog"), &nShowOptionsDlg);
				///---Sim 10-27-2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
				//nFilesRet = get_open_box_by_file_group(strFileName, "ThermoGalactic", NULL,
					//OPENBOX_FLAG_MULTIFILES, NULL, NULL, STR_SHOW_OPT_DLG, &nShowOptionsDlg);
				nFilesRet = get_open_box_by_file_group(strFileName, "ThermoGalactic", NULL,
					OPENBOX_FLAG_MULTIFILES, NULL, NULL, get_show_options_label(trGetN), &nShowOptionsDlg);
				///---END QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
				/// end CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				nRet = (nShowOptionsDlg ? XFEVT_PROCEED : XFEVT_PROCEED_NO_DLG);
			}
			else
			{
				/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				//nFilesRet = get_open_box_by_file_group(strFileName, "ThermoGalactic", _L("Import Thermo Files"), true);
				nFilesRet = get_open_box_by_file_group(strFileName, "ThermoGalactic", NULL, true);
				/// end CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
			}
			/// end MORE_ON_ACCESS_TO_FDLOG
			/// end THEME_FILENAME_WORKBOOK_AND_WORKSHEET
			if ( 0 == nFilesRet )
			{
				nRet = XFEVT_ABORT;
				//warning_msg_box(XFERR_UI_USER_CANCEL, false);
				return;
			}
			///---END HANDLE_USER_CANCEL_OPEN_FILE_BOX
			trGetN.fname.strVal = strFileName;
			string strErrMsg;
			if( !_file_name_event(trGetN, strErrMsg) )
			{
				/// Hong 03/21/07 v8.0586 FIX_FAIL_DUMP_CORRECT_ERROR_STRING
				//nRet = XFEVT_ABORT;
				//warning_msg_box(strErrMsg, false);
				int nErrMsg = atoi(strErrMsg);
				nRet = XFEVT_ABORT;
				warning_msg_box(nErrMsg, false, 'E'); // should pass an int ///---Sim 06-19-2007 IMPROVE_WARNING_MESSAGE_POST_FUNCTION
				/// end FIX_FAIL_DUMP_CORRECT_ERROR_STRING
				return;
			}
		}
	}
	/// end CHECK_FNAME_EMPTY_UNASSIGNED
}
//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 impSPC_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
