#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
////////////////////////////////////////////////////////////////////
/// Modification Log:
/// Sim 04-30-2007 TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
/// Sim 06-25-2007 UPDATE_ORGANIZER
/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
/// Sim 02-16-2009 IMPORT_IMG_FROM_GRAPH_NO_NEED_DLG
/// Sim 03-03-2009 QA80-13209 IMPORT_IMG_SUPPORT_REIMPORT
/// Hong 04/14/09 QA80-13432 FIX_RUNTIME_ERROR_WHEN_ACTIVE_PAGE_IS_WKS
/// Sim 07-07-2009 QA81-13817 SUPPORT_IMPORT_IMAGE_WHEN_WKS_ACTIVE
/// Sim 07-20-2009 QA81-13817 DIRTY_FIX_GUI_ISSUE_OF_IMPORT_MAT_FILE_TO_WKS
/// Kenny 07/21/2009 QA80-13980 CLEAN_UP_AND_IMPROVE_XF_IMP_IMAGE_TO_TWO_SEPARATE_XF
/// Sophy 6/4/2010 ORG-220 IMPORT_MULTI_IMAGES_WITH_SAME_DIMS_INTO_SAME_MATRIXSHEET
/// Sophy 6/4/2010 ORG-150 IMAGE_GROUP_FOR_IMPORT_IMAGE_FILE_DLG
/// Folger 10/20/2010 ORG-1294-P1 IMPORT_IMAGE_CREATE_NEW_SHEET_FOR_REPLACE_DATA_ONLY_IF_MORE_THAN_ONE_OBJECT_IN_ONE_SHEET
////////////////////////////////////////////////////////////////////

#include <GetNBox.h>
#include <..\originlab\file_utils.h> //CPY 8/10/06 I found failed if used 1st
#include <image_utils.h> //CPY 9/13/06 BRING_BACK_IMGREAD_XF
#include <..\originlab\imagefile.h>
#include <..\originlab\fu_utils.h>
#include <iw_xf.h>
#include <..\Originlab\theme_utils.h>	///Sophy 6/24/2010 ORG-220-S6 SHOW_OPTIONS_DIALOG_CHECKBOX_WHEN_OPEN_DLG

///---Sim 11-16-2006 RELIABLE_OK_ENABLE
static bool s_bFileValid = true;
#define OK_SURE(_OK_ENABLE) _OK_ENABLE = s_bFileValid; //also can be _OK_ENABLE = s_bFileValid && s_bSetting
///---END RELIABLE_OK_ENABLE

/// Hong 9/29/06 ADD_FILE_INFO_NODE
static BOOL construct_tree_setting(StringArray& vsFileNames, TreeNode& trInfo)
{
	int nCount = vsFileNames.GetSize();
	string strFileName, strTag, strInfo;
//	ASCIMP ascimp; 
	//TreeNode trFile, trFileInfo; ///---Sim 11-08-2006 NO_NEED
	if ( trInfo )
		trInfo.RemoveChildrenWithPrefix();
	GETN_USE(trInfo)
	GETN_OPTION_BRANCH(GETNBRANCH_OPEN)	//CPY 10/1/06
	for ( int nIndex = 0; nIndex < nCount; nIndex++ )
	{
		strFileName = vsFileNames[nIndex];
		if ( !strFileName.IsFile() )
			return false;
		///---Sim 11-08-2006 FIX_SET_TAGNAME_AS_FILE_NAME
		//string strTag = GetFileName(strFileName, true);
		//strTag.MakeValidCName('a');
		strTag.Format("file%d", nIndex+1);
		///---END FIX_SET_TAGNAME_AS_FILE_NAME
//		trFile = trInfo.AddNode(strTag);
//		trFile.SetAttribute(STR_LABEL_ATTRIB, GetFileName(strFileName, false));
//		trFileInfo = trFile.AddNode("FileInfo");
		strInfo.Format("File Size: %s ", GetFileSize(strFileName));
	/*	if(AscImpReadFileStruct(strFileName,&ascimp)==0)
		{
			strInfo.Format( "%s Numer of columns found %d",strInfo, ascimp.iNumColumns);
		} */
		GETN_STR(strTag, GetFileName(strFileName, false), strInfo )
		GETN_READ_ONLY
//		GETN_MULTILINE_TEXT(INFO, "HeaderInfo", strInfo) 
	}
	return true;
}
/// END ADD_FILE_INFO_NODE

///---Sim 11-20-2006 SUPPORT_DND_OPEN_DIALOG
static bool file_name_event(TreeNode& trGetN, string& strErrMsg)
{
	string strFileName = trGetN.fname.strVal;
	TreeNode trNode = trGetN.info;

	// clean the trFiles on GUI
	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 SUPPORT_DND_OPEN_DIALOG

///Sophy 6/4/2010 ORG-220 IMPORT_MULTI_IMAGES_WITH_SAME_DIMS_INTO_SAME_MATRIXSHEET
static bool _get_image_dimension(LPCSTR lpcszImageFile, uint& nWidth, uint& nHeight)
{
	FILEINFO fi;
	if( SUCCESS != L_FILE_INFO(lpcszImageFile, &fi, 0, NULL) )
	{
		return false;
	}
	nWidth = fi.Width;
	nHeight = fi.Height;
	return true;
}
///end IMPORT_MULTI_IMAGES_WITH_SAME_DIMS_INTO_SAME_MATRIXSHEET

///Sophy 6/7/2010 ORG-220-P4 SORT_MULTIPLE_IMAGE_BY_DIMENSION
///Sophy 6/11/2010 ORG-220-P6 STABLE_SORT_FOR_KEEP_ORIGINAL_ORDER_OF_IMAGE
#define	_STR_SWAP(_STR1, _STR2)	{string _strTemp = _STR1; _STR1 = _STR2; _STR2 = _strTemp;}
static bool _bubble_sort(const vector<string>& vsKeys, vector<string>& vsValues)
{
	int nSize = vsKeys.GetSize();
	if ( nSize != vsValues.GetSize() )
	{
		ASSERT(FALSE);
		return false;
	}
	for ( int ii = 0; ii < nSize; ii++ )
	{
		for ( int jj = ii + 1; jj < nSize; jj++ )
		{
			if ( vsKeys[ii].CompareNoCase(vsKeys[jj]) > 0 )
			{
				_STR_SWAP(vsKeys[ii], vsKeys[jj]);
				_STR_SWAP(vsValues[ii], vsValues[jj]);
			}
		}
	}
	return true;
}
///end STABLE_SORT_FOR_KEEP_ORIGINAL_ORDER_OF_IMAGE
static bool _sort_images(vector<string>& vsFileNames)
{
	vector<string> vsWH(0);
	int iFile, nFiles = vsFileNames.GetSize();
	uint nW, nH;
	string strT;
	for ( iFile = 0; iFile < nFiles; iFile++ )
	{
		_get_image_dimension(vsFileNames[iFile], nW, nH);
		strT.Format("%d,%d", nW, nH);
		vsWH.Add(strT);
	}
	///Sophy 6/11/2010 ORG-220-P6 STABLE_SORT_FOR_KEEP_ORIGINAL_ORDER_OF_IMAGE
	//vector<uint> vnIndices(vsWH.GetSize());
	//vsWH.Sort(SORT_ASCENDING, TRUE, vnIndices);
	//vsFileNames.Reorder(vnIndices);
	_bubble_sort(vsWH, vsFileNames);
	///end STABLE_SORT_FOR_KEEP_ORIGINAL_ORDER_OF_IMAGE
	return true;
}
///end SORT_MULTIPLE_IMAGE_BY_DIMENSION

///------ Folger 10/20/2010 ORG-1294-P1 IMPORT_IMAGE_CREATE_NEW_SHEET_FOR_REPLACE_DATA_ONLY_IF_MORE_THAN_ONE_OBJECT_IN_ONE_SHEET
static	void	_check_update_import_mode(int& nFileImportMode, MatrixLayer& ml, LPCSTR lpcszFileName, BOOL bNewObject)
{
	if ( !ml )
		return;
	
	int		nCount = ml.MatrixObjects.Count();
	if ( bNewObject && nCount > 0 || !bNewObject && nCount > 1 )
	{
		uint nW = 0, nH = 0;
		_get_image_dimension(lpcszFileName, nW, nH);
		if ( nW != ml.GetNumCols() || nH != ml.GetNumRows() )
			nFileImportMode = ASCIMP_MODE_NEW_SHEETS;
	}
}
///------ End IMPORT_IMAGE_CREATE_NEW_SHEET_FOR_REPLACE_DATA_ONLY_IF_MORE_THAN_ONE_OBJECT_IN_ONE_SHEET

///Sophy 6/12/2010 ORG-220-S4 NEW_RENAME_OPTIONS_FOR_IMPIMAGE
#define	LABELNODE(_Node, _Label)	if ( _Node )_Node.SetAttribute(STR_LABEL_ATTRIB, _Label);
#define	SHOWNODE(_Node, _Show)		if ( _Node )_Node.Show = _Show;
static bool _update_rename_options_labels(TreeNode& trOptions)
{
	if ( !trOptions.IsValid() )
		return false;
	
	TreeNode trNames = trOptions.GetNode(STR_FILTER_FILENAME_NODE);
	if ( !trNames )
		return false;
	
	tree_set_attribute_to_all_nodes(trNames, STR_SHOW_ATTRIB, "0");
	
	LABELNODE(trNames, STR_LBL_RENAME_MATRIX_OPTIONS);	SHOWNODE(trNames, 1);
	TreeNode trMatSheet = tree_get_node_by_id(trOptions, IDE_XFIMP_RENAMEWKS, true);
	LABELNODE(trMatSheet, STR_LBL_RENAME_MATRIX_SHEET);	SHOWNODE(trMatSheet, 1)
	TreeNode trMatBook = tree_get_node_by_id(trOptions, IDE_XFIMP_FILENAME2WKBK, true);
	LABELNODE(trMatBook, STR_LBL_RENAME_MATRIX_BOOK);	SHOWNODE(trMatBook, 1);
	TreeNode trRenameLNOnly = tree_get_node_by_id(trOptions, IDE_XFIMP_WKBKLNONLY, true);
	SHOWNODE(trRenameLNOnly, 1);
	TreeNode trIncludePath = tree_get_node_by_id(trOptions, IDE_XFIMP_ADDPATH4WKBK, true);
	SHOWNODE(trIncludePath, 1);
	
	TreeNode trAppToBkComm = tree_get_node_by_id(trOptions, IDE_XFIMP_FILENAME2WKBKCOMMENT, true);
	trAppToBkComm.nVal = 0; //uncheck

	return true;
}
///end NEW_RENAME_OPTIONS_FOR_IMPIMAGE


void impImage(const string& fname, const TreeNode& info, const Page& ipg, const TreeNode& options, Range& orng, TreeNode& finfo, const TreeNode& trFilter, int& fIndex, const int& reimp)
{
	/// Hong 04/14/09 QA80-13432 FIX_RUNTIME_ERROR_WHEN_ACTIVE_PAGE_IS_WKS
	///Sophy 6/18/2010 JUNK_CODE_DUE_TO_SEPARATE_XF_INSRETIMG2G
	//GraphPageBase		igpb;
	//igpb = (GraphPageBase)ipg;
	///end JUNK_CODE_DUE_TO_SEPARATE_XF_INSRETIMG2G
	/// end FIX_RUNTIME_ERROR_WHEN_ACTIVE_PAGE_IS_WKS
	///---Sim 12-23-2006 SUPPORT_IMPORT_IMAGE_TO_GRAPH
	Layer lyTarget;
	/// Hong 06/30/07 QA80-9910-P6 FIX_FAIL_IMPORT_INTO_GRAPH_CONTAIN_DATAPLOT
	/*
	if ( orng.GetNumRanges() > 0 )
	{
		int c1,c2; 
		//Datasheet lyTarget;
		//orng.GetRange(lyTarget, c1, c2);
		//MatrixLayer mlTarget(lyTarget);
		//if(!mlTarget.IsValid())
			//XF_THROW(CER_INVALID_MATRIX); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
		Datasheet ds;
		orng.GetRange(ds, c1, c2);
		lyTarget = ds;
		if ( !lyTarget )
			XF_THROW(IMPERR_INVALID_DATASHEET);
	}
	else if ( ipg )
	{
		lyTarget = ipg.Layers();
	}
	*/
	/// Hong 09/11/07 v8.0700 ADD_IMP_MODE_OPTIONS
	TreeNode tnUsingFilter;
	int nFileImportMode;
	/// end ADD_IMP_MODE_OPTIONS
	/// Hong 09/11/07 v8.0700 ADD_IMP_MODE_OPTIONS
	//if ( ipg )
	//{
	//	lyTarget = ipg.Layers();
	//}
	//else if ( orng && orng.GetNumRanges() > 0 )
	if ( orng && orng.GetNumRanges() > 0 )
	/// end ADD_IMP_MODE_OPTIONS
	{
		int c1,c2; 
		Datasheet ds;
		orng.GetRange(ds, c1, c2);
		/// Hong 09/26/07 QA80-9467 V8.0709 FIX_FAIL_IMPORT_TO_GRAPH_IF_HAS_DATAPLOT
		//lyTarget = ds;
		//if ( !lyTarget )
			//XF_THROW(IMPERR_INVALID_DATASHEET);
		///---Sim 07-07-2009 QA81-13817 SUPPORT_IMPORT_IMAGE_WHEN_WKS_ACTIVE
		//MatrixLayer ml(ds);
		//if ( ml )
		//{
			//lyTarget = ml;
		lyTarget = ds;
		if ( lyTarget )
		{
		///---END QA81-13817 SUPPORT_IMPORT_IMAGE_WHEN_WKS_ACTIVE
		/// end FIX_FAIL_IMPORT_TO_GRAPH_IF_HAS_DATAPLOT
			/// Hong 09/11/07 v8.0700 ADD_IMP_MODE_OPTIONS		
			double dDefault = 0.;
			if ( fuGetDouble(options, IDE_DISP_IMP_ORIGINVER, dDefault) )
				tnUsingFilter = options;
			
			nFileImportMode = fuGetImportMode(tnUsingFilter);
			
			if ( trFilter && trFilter.GetNodeCount() > 0 )
				tnUsingFilter = trFilter;
			/// end ADD_IMP_MODE_OPTIONS
		/// Hong 09/26/07 QA80-9467 V8.0709 FIX_FAIL_IMPORT_TO_GRAPH_IF_HAS_DATAPLOT
		}
		/// Hong 04/14/09 QA80-13432 FIX_RUNTIME_ERROR_WHEN_ACTIVE_PAGE_IS_WKS
		//else if ( ipg ) // check if active page is Graph as active Range will be target worksheet if active Graph page have dataplot
		//{
		//	lyTarget = ipg.Layers();
		//}
		///Sophy 6/18/2010 JUNK_CODE_DUE_TO_SEPARATE_XF_INSRETIMG2G
		//else if ( igpb )
		//{
			//lyTarget = igpb.Layers();
		//}
		///end JUNK_CODE_DUE_TO_SEPARATE_XF_INSRETIMG2G
		/// end FIX_RUNTIME_ERROR_WHEN_ACTIVE_PAGE_IS_WKS
		else
			XF_THROW(IMPERR_INVALID_DATASHEET);	
		/// end FIX_FAIL_IMPORT_TO_GRAPH_IF_HAS_DATAPLOT
	}
	/// Hong 09/11/07 v8.0700 ADD_IMP_MODE_OPTIONS
	/// Hong 04/14/09 QA80-13432 FIX_RUNTIME_ERROR_WHEN_ACTIVE_PAGE_IS_WKS
	//else if ( ipg )
	//{
		//lyTarget = ipg.Layers();
	//}
	///Sophy 6/18/2010 JUNK_CODE_DUE_TO_SEPARATE_XF_INSRETIMG2G
	//else if ( igpb )
	//{
		//lyTarget = igpb.Layers();
	//}
	///end JUNK_CODE_DUE_TO_SEPARATE_XF_INSRETIMG2G
	/// end FIX_RUNTIME_ERROR_WHEN_ACTIVE_PAGE_IS_WKS
	/// end ADD_IMP_MODE_OPTIONS
	else
		XF_THROW(CER_INVALID_PAGE);
	///---END SUPPORT_IMPORT_IMAGE_TO_GRAPH
	
	///---Sim 12-23-2006 SUPPORT_IMPORT_IMAGE_TO_GRAPH
	/////---Sim 11-08-2006 MAKE_INDIVIDUAL_FILE_INFO_FOR_EACH_FILE	
	///*
	//Tree trInfo;
	//if ( finfo )
		//trInfo = finfo;
	//*/
	//Tree trBaseInfo; 
	//if ( finfo )
		//trBaseInfo = finfo;
	/////---END MAKE_INDIVIDUAL_FILE_INFO_FOR_EACH_FILE	
	//
	//ImageFile myImgFile;
	//
	//int nRet;
	///---END SUPPORT_IMPORT_IMAGE_TO_GRAPH
	/// AW 08/30/06 SUPPORT_MULTI_FILES_INXF
	/*
	if ( nRet = myImgFile.Import(fname, mlTarget, trInfo, trFilter) )
	{
		XF_THROW(nRet);
	}
	else //if( !trFilter )   ///---Sim 08/11/06 MORE_ON_TRINFO CAN"T SEE any reason need trFilter be invalid here  
	
	{
		impinfo_AddFile(mlTarget, fname, trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, trFilter);
	}
	*/
	
	///---Sim 12-23-2006 SUPPORT_IMPORT_IMAGE_TO_GRAPH
	//Page pgTarget;
	//mlTarget.GetParent(pgTarget);
	///---END SUPPORT_IMPORT_IMAGE_TO_GRAPH
	StringArray vsFileNames;
	//separate_file_names(fname, "\n", vsFileNames); 
	separate_multiline_files(fname, vsFileNames);// Hong 9/13/06 FIX_SEPARATE_FILENAME 
	
	int nFileCount = vsFileNames.GetSize();
	///---Sim 09-28-2006 CHECK_VALID_FILE_NAME
	if ( nFileCount <= 0 )
		XF_THROW(CER_INVALID_FILENAME); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
	///---END CHECK_VALID_FILE_NAME
	
	///---Sim 12-23-2006 SUPPORT_IMPORT_IMAGE_TO_GRAPH
	Page pgTarget;
	lyTarget.GetParent(pgTarget);
	int nType = pgTarget.GetType();
	
	switch ( nType )
	{
	///---Sim 07-07-2009 QA81-13817 SUPPORT_IMPORT_IMAGE_WHEN_WKS_ACTIVE
	case EXIST_WKS:
		{
			if ( EXIST_WKS == fuGetTargetPage(tnUsingFilter) )
				fuSetTargetPage(tnUsingFilter, EXIST_MATRIX);
		}
	///---END QA81-13817 SUPPORT_IMPORT_IMAGE_WHEN_WKS_ACTIVE
	case EXIST_MATRIX:
		{
			MatrixLayer mlTarget = lyTarget;
		
			Tree trBaseInfo; 
			/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
			//if ( finfo )
				//trBaseInfo = finfo;
			/// end FINFO_IS_ONLY_OUTPUT
			
			ImageFile myImgFile;
			
			int nRet;
			
			///---Sim 09-06-2006 FIX_EXCESSIVE_EMPTY_SHEET
			//int nIndexLayer;
			///---Sim 07-07-2009 QA81-13817 SUPPORT_IMPORT_IMAGE_WHEN_WKS_ACTIVE
			//int nIndexLayer = mlTarget.GetIndex();
			int nIndexLayer = lyTarget.GetIndex();
			///---END QA81-13817 SUPPORT_IMPORT_IMAGE_WHEN_WKS_ACTIVE
			///---END FIX_EXCESSIVE_EMPTY_SHEET
			///Sophy 6/4/2010 ORG-220 IMPORT_MULTI_IMAGES_WITH_SAME_DIMS_INTO_SAME_MATRIXSHEET
			uint	nLastCols = 0, nLastRows = 0;	
			int		n1stFileImportMode = fuGet1stImportMode(tnUsingFilter);
			///end IMPORT_MULTI_IMAGES_WITH_SAME_DIMS_INTO_SAME_MATRIXSHEET
			for ( int ii = 0; ii < nFileCount; ii++ )
			{
				/// Hong 09/11/07 v8.0700 ADD_IMP_MODE_OPTIONS
				/*
				///---Sim 04-30-2007 TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
				if ( 0 == ii )
				{
					int nImportMode = get_first_file_import_mode(FILE_TYPE_IMAGE);
					TreeNode trInValid;
					
					mlTarget = GetTargetLayer(pgTarget, nIndexLayer, ii, trInValid, true, orng, nImportMode);
				}
				else
				{
					mlTarget = GetTargetLayer(pgTarget, nIndexLayer, ii, trFilter); ///---Sim 09-06-2006 FIX_EXCESSIVE_EMPTY_SHEET
				}
				///---END TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
				*/
				///Sophy 6/4/2010 ORG-220 IMPORT_MULTI_IMAGES_WITH_SAME_DIMS_INTO_SAME_MATRIXSHEET
				//mlTarget = GetTargetLayer(pgTarget, nIndexLayer, ii, tnUsingFilter, true, orng, nFileImportMode);
				int nMoIndex = -1;
				int nFileImportModeTemp = nFileImportMode;
				TreeNode trFilterTemp; //make invalid filter tree
				///Sophy 6/18/2010 ORg-339 MULTIFRAME_TIFF_IMPORT
				int nTiffFrames;
				if ( (ASCIMP_MODE_NEW_OBJECTS == n1stFileImportMode && ii == 0 || (ASCIMP_MODE_NEW_OBJECTS == nFileImportMode && ii > 0) ) && image_frame_counts(vsFileNames[ii], nTiffFrames) && nTiffFrames > 1 )
				{
					nFileImportModeTemp = ASCIMP_MODE_NEW_SHEETS;
				}
				///end MULTIFRAME_TIFF_IMPORT
				if ( ii > 0 && ASCIMP_MODE_NEW_OBJECTS == nFileImportMode ) //not allow to same sheet, when image have different DIM.
				{
					uint nW = 0, nH = 0;
					_get_image_dimension(vsFileNames[ii], nW, nH);
					if ( nW != nLastCols || nH != nLastRows )
					{
						nFileImportModeTemp = ASCIMP_MODE_NEW_SHEETS;
					}
				}
				///Sophy 6/8/2010 ORG-220-P5 SUPPORT_APPEND_MATRIXOBJECT_OF_SAME_DIM_TO_EXISTING_SHEET
				else if ( ii == 0 && (ASCIMP_MODE_NEW_OBJECTS == n1stFileImportMode || ASCIMP_MODE_REPLACE_DATA == n1stFileImportMode) )
				{
					///------ Folger 10/20/2010 ORG-1294-P1 IMPORT_IMAGE_CREATE_NEW_SHEET_FOR_REPLACE_DATA_ONLY_IF_MORE_THAN_ONE_OBJECT_IN_ONE_SHEET
					//uint nW = 0, nH = 0;
					//_get_image_dimension(vsFileNames[ii], nW, nH);
					//if ( mlTarget.IsValid() && !is_sheet_empty(mlTarget) && (nW != mlTarget.GetNumCols() || nH != mlTarget.GetNumRows()) )
					//{
						//nFileImportModeTemp = ASCIMP_MODE_NEW_SHEETS;
					//}
					_check_update_import_mode(nFileImportModeTemp, mlTarget, vsFileNames[ii], ASCIMP_MODE_NEW_OBJECTS == n1stFileImportMode);
					///------ End IMPORT_IMAGE_CREATE_NEW_SHEET_FOR_REPLACE_DATA_ONLY_IF_MORE_THAN_ONE_OBJECT_IN_ONE_SHEET
				}
				///end SUPPORT_APPEND_MATRIXOBJECT_OF_SAME_DIM_TO_EXISTING_SHEET
				if ( nFileImportModeTemp == nFileImportMode )
				{
					trFilterTemp = tnUsingFilter;
				}
				mlTarget = GetTargetLayer(pgTarget, nIndexLayer, ii, trFilterTemp, true, orng, nFileImportModeTemp, nMoIndex);
				///end IMPORT_MULTI_IMAGES_WITH_SAME_DIMS_INTO_SAME_MATRIXSHEET
				/// end ADD_IMP_MODE_OPTIONS				
				///---Sim 11-08-2006 MAKE_INDIVIDUAL_FILE_INFO_FOR_EACH_FILE
				/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
				//Tree trInfo;
				//trInfo.Replace(trBaseInfo);
				string strFile;
				strFile.Format("File%d", ii + 1); // file index to be 1
				TreeNode trInfo = trBaseInfo.AddNode(strFile);
				/// end FINFO_IS_ONLY_OUTPUT
				///---END MAKE_INDIVIDUAL_FILE_INFO_FOR_EACH_FILE
				//if ( nRet = myImgFile.Import(vsFileNames[ii], mlTarget, trInfo, trFilter) )
				///Sophy 6/4/2010 ORG-220 IMPORT_MULTI_IMAGES_WITH_SAME_DIMS_INTO_SAME_MATRIXSHEET
				//if ( nRet = myImgFile.Import(vsFileNames[ii], mlTarget, trInfo, tnUsingFilter) ) /// Hong 09/11/07 v8.0700 ADD_IMP_MODE_OPTIONS
				if ( nRet = myImgFile.Import(vsFileNames[ii], mlTarget, trInfo, tnUsingFilter, nMoIndex) ) /// Hong 09/11/07 v8.0700 ADD_IMP_MODE_OPTIONS
				///end IMPORT_MULTI_IMAGES_WITH_SAME_DIMS_INTO_SAME_MATRIXSHEET
				{
					XF_THROW_EX(nRet, GetFileName(vsFileNames[ii], false)); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
				}
				else //if( !trFilter )   ///---Sim 08/11/06 MORE_ON_TRINFO CAN"T SEE any reason need trFilter be invalid here  
				
				{
					///---Sim 03-03-2009 QA80-13209 IMPORT_IMG_SUPPORT_REIMPORT
					// add data range info
					Tree trDataRange;
					getRangeInfo(trDataRange, myImgFile.GetDataRange());
					
					// if current file cover existing file data, need update info of that files
					if ( !reimp )
						impinfo_UpdateExistingData(pgTarget, myImgFile.GetDataRange());
					///---END QA80-13209 IMPORT_IMG_SUPPORT_REIMPORT
					
					/// Hong 03/13/07 FIX_COMPILE_ERROR
					// fIndex should not been used any more as we support multi files
					//impinfo_AddFile(mlTarget, vsFileNames[ii], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, trFilter, fIndex);
					//impinfo_AddFile(mlTarget, vsFileNames[ii], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, trFilter, ii + 1);
					///---Sim 03-03-2009 QA80-13209 IMPORT_IMG_SUPPORT_REIMPORT
					//impinfo_AddFile(mlTarget, vsFileNames[ii], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, tnUsingFilter, ii + 1); /// Hong 09/11/07 v8.0700 ADD_IMP_MODE_OPTIONS
					impinfo_AddFile(mlTarget, vsFileNames[ii], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, tnUsingFilter, ii + 1, trDataRange, reimp);
					///---END QA80-13209 IMPORT_IMG_SUPPORT_REIMPORT
					/// end FIX_COMPILE_ERROR
					
					///Sophy 6/4/2010 ORG-220 IMPORT_MULTI_IMAGES_WITH_SAME_DIMS_INTO_SAME_MATRIXSHEET
					nLastCols = mlTarget.GetNumCols();
					nLastRows = mlTarget.GetNumRows();
					///end IMPORT_MULTI_IMAGES_WITH_SAME_DIMS_INTO_SAME_MATRIXSHEET
					setPageAndLayerName(myImgFile.GetDataRange(), vsFileNames[ii], tnUsingFilter, 0); ///Sophy 6/11/2010 ORG-220-S3 ADD_IMPORT_OPTIONS_TO_CONTROL_RENAME_RESULT_BOOKSHEET
					///Sophy 6/11/2010 ORG-292 ADD_SHOW_IMAGE_THUMBNAIL_CHECKBOX
					int nShowThumbnails = options.thumbnail.nVal;
					MatrixPage mpTarget(pgTarget);
					mpTarget.ShowImageThumbnails(nShowThumbnails);
					///end ADD_SHOW_IMAGE_THUMBNAIL_CHECKBOX
				}
				///---Sim 09-06-2006 FIX_EXCESSIVE_EMPTY_SHEET
				//if ( ii < nFileCount - 1 ) 
					//mlTarget = GetTargetLayer(pgTarget, nIndexLayer, ii, trFilter);
				///---END FIX_EXCESSIVE_EMPTY_SHEET
			}
			//refresh_organizer_gui(pgTarget); ///---Sim 06-25-2007 UPDATE_ORGANIZER			
			if ( finfo ) /// Hong 09/12/09 v8.0701 INFO_SHOULD_ONLY_DUMP_IF_USER_NEEDED
				finfo.Replace(trBaseInfo.Clone(), true, true);/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
			break;
		}
	/// Kenny 07/21/2009 QA80-13980 CLEAN_UP_AND_IMPROVE_XF_IMP_IMAGE_TO_TWO_SEPARATE_XF
	//case EXIST_LAYOUT:
	//case EXIST_PLOT:
		//{
			//set_active_layer(lyTarget); 
			//for ( int ii = 0; ii < nFileCount; ii++ )
				//image_import_to_active_graph_layer(vsFileNames[ii]);
					//
			//break;
		//}
	/// End QA80-13980 CLEAN_UP_AND_IMPROVE_XF_IMP_IMAGE_TO_TWO_SEPARATE_XF
	default:
		XF_THROW(IMPERR_INVALID_DATASHEET);
		break;
	}
	///---END SUPPORT_IMPORT_IMAGE_TO_GRAPH
	/// END SUPPORT_MULTI_FILES_INXF
}

//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 impImage_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return impImage_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	OK_SURE(bOKEnable); ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
	//printf("nEventID=%d, node = %s\n", nEventID, lpcszNodeName);
	//out_tree(trGetN);
	
	///---Sim 11-20-2006 SUPPORT_DND_OPEN_DIALOG
	if ( GETNE_ON_INIT == nEventID || GETNE_ON_THEME == nEventID )
	{
		s_bFileValid = file_name_event(trGetN, strErrMsg);
		OK_SURE(bOKEnable);
		return true;
	}
	///---END SUPPORT_DND_OPEN_DIALOG
	
	/// Hong 9/29/06 ADD_FILE_INFO_NODE
	string strTagName = lpcszNodeName;
	if(strTagName == "fname") // file name changed, either on start of later user changed
	{
		///---Sim 11-20-2006 SUPPORT_DND_OPEN_DIALOG
		s_bFileValid = file_name_event(trGetN, strErrMsg);
		OK_SURE(bOKEnable);
		return true;
		/*
		s_bFileValid = true; ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
		OK_SURE(bOKEnable); ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
		
		///---Sim 10-30-2006 ERROR_MESSAGE
		int nErrCode = CER_NO_ERROR;
		int nInvalidFileIdx = 0;
	
		if ( trGetN.fname.strVal.IsEmpty() )
		{
			nErrCode = CER_EMPTY_FILENAME;
		}
		else if ( !is_valid_filename(trGetN.fname.strVal, nInvalidFileIdx) )
		{
			nErrCode = CER_INVALID_FILENAME_INDEX;
		}
				
		if(CER_NO_ERROR != nErrCode)
		{
			strErrMsg = nErrCode;
			
			if ( CER_INVALID_FILENAME_INDEX == nErrCode )
			{
				strErrMsg += ":";
				strErrMsg += nInvalidFileIdx;
			}
			
			//DynaCntrlContainer.PostMessage(WM_USER_MSG_ON_DLG, nErrCode, nInvalidFileIdx);    
			s_bFileValid = false; ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
			OK_SURE(bOKEnable); ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
	
			// clean the info on GUI
			trGetN.info.RemoveChildrenWithPrefix();
		
			return true;
		}
		///---END ERROR_MESSAGE
		
		///---Sim 10-30-2006 ERROR_MESSAGE
		StringArray vsFileNames;
		TreeNode trInfo = trGetN.info;
		separate_multiline_files(trGetN.fname.strVal, vsFileNames);
		if ( !_construct_tree_setting(vsFileNames, trInfo) )
		{
			// clean the info on GUI
			trInfo.RemoveChildrenWithPrefix();
			
			//XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
			//return false;
			strErrMsg = CER_INVALID_IMPORT_DATA_FILE;
			s_bFileValid = false; ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
			OK_SURE(bOKEnable); ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
	
			return true;
		}
		///---END ERROR_MESSAGE
		return true;
		*/
		///---END SUPPORT_DND_OPEN_DIALOG
	}
	/// END ADD_FILE_INFO_NODE
	return false;

}

int impImage_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	/*
	string strTagName = lpcszVarName;
	
	if(strTagName == "tr")
	{
		GETN_USE(tr)
	
		//GETN_OPTION_GRIDLINE(flexGridRaisedVert) // Option to add vertical divider line
		//GETN_OPTION_EDIT_ALIGN("0|0|0") // left align checkbox|numbers|strings
	
		///GETN_STR(Filename, "File Name", lpcszFileName) GETN_READ_ONLY
		
		GETN_CHECK(AddTimeCol, "Create Time Column", 1)
		
		return 0;
	}
	*/
	/// Hong 9/29/06 ADD_FILE_INFO_NODE
	string strTagName = lpcszVarName;
	if(strTagName == "info")
	{
		return 0;
	}
	/// END ADD_FILE_INFO_NODE
	/// Hong 09/11/07 v8.0700 ADD_IMP_MODE_OPTIONS
	if ( strTagName == "options" )
	{
		///Sophy 6/11/2010 ORG-220-S3 ADD_IMPORT_OPTIONS_TO_CONTROL_RENAME_RESULT_BOOKSHEET
		//constructOptionsForXF(tr, false, true, false, false, false, false);
		constructOptionsForXF(tr, true, true, false, false, false, false);
		///end ADD_IMPORT_OPTIONS_TO_CONTROL_RENAME_RESULT_BOOKSHEET	
		///Sophy 6/12/2010 ORG-220-S4 NEW_RENAME_OPTIONS_FOR_IMPIMAGE
		_update_rename_options_labels(tr);
		///end NEW_RENAME_OPTIONS_FOR_IMPIMAGE
		///Sophy 6/11/2010 ORG-292 ADD_SHOW_IMAGE_THUMBNAIL_CHECKBOX
	#define	IDE_SHOW_THUMBNAIL	(IDE_DISPIMP_1STMODE + 500)
		TreeNode trThumbnail = tr.InsertNode(tr.FirstNode, "thumbnail", TRGP_CHECK);
		trThumbnail.SetAttribute(STR_LABEL_ATTRIB, _L("Show Image Thumbnails"));
		trThumbnail.nVal = 1;	///Sophy 6/12/2010 ORG-292-S1 IMPIMAGE_SHOW_IMAGE_THUMBNAILS_BY_DEFAULT
		trThumbnail.DataID = IDE_SHOW_THUMBNAIL;
		///end ADD_SHOW_IMAGE_THUMBNAIL_CHECKBOX
		TreeNode tr1stImportMode = tree_get_node_by_id(tr, IDE_DISPIMP_1STMODE, true);
		if ( tr1stImportMode )
		{
			string strCombo, strMapping;
			///Sophy 6/8/2010 ORG-220-S1 SUPPORT_APPEND_MATRIXOBJECT_OF_SAME_DIM_TO_EXISTING_SHEET
			//strCombo = getImportModeListString(strMapping, LIST_IMPORT_MODE_REPLACE_BOOK_SHEET);
			strCombo = getImportModeListString(strMapping, LIST_IMPORT_MODE_REPLACE_BOOK_SHEET_OBJECT);
			///end SUPPORT_APPEND_MATRIXOBJECT_OF_SAME_DIM_TO_EXISTING_SHEET
			tr1stImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			tr1stImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			tr1stImportMode.nVal = ASCIMP_MODE_REPLACE_DATA;
		}
	
		TreeNode trImportMode = tree_get_node_by_id(tr, IDE_IMP_IMPORTMODE, true);
		if ( trImportMode )
		{
			string strCombo, strMapping;
			///Sophy 6/4/2010 ORG-220 IMPORT_MULTI_IMAGES_WITH_SAME_DIMS_INTO_SAME_MATRIXSHEET
			//strCombo = getImportModeListString(strMapping, LIST_IMPORT_MODE_BOOK_SHEET);
			strCombo = getImportModeListString(strMapping, LIST_IMPORT_MODE_BOOK_SHEET_OBJECT);
			///end IMPORT_MULTI_IMAGES_WITH_SAME_DIMS_INTO_SAME_MATRIXSHEET
			trImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			trImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			///Sophy 6/7/2010 ORG-220-P2 MAKE_START_NEW_OBJECTS_AS_DEFAULT_IMPORT_MODE
			//trImportMode.nVal = ASCIMP_MODE_NEW_BOOKS;
			trImportMode. nVal = ASCIMP_MODE_NEW_OBJECTS;
			///end MAKE_START_NEW_OBJECTS_AS_DEFAULT_IMPORT_MODE
		}
		
		fuSetTargetPage(tr, EXIST_MATRIX);
		fuSetXFunctionName(tr, "impImage");
		return 0;
	}
	/// end ADD_IMP_MODE_OPTIONS
	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 impImage_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if(nGetNDialog > 0)
	{
		trGetN.finfo.Show = 0;
		trGetN.trFilter.Show = 0;
		trGetN.fIndex.Show = 0;
	
		trGetN.ipg.Show = 0;
		/// Kenny 07/21/2009 QA80-13980 CLEAN_UP_AND_IMPROVE_XF_IMP_IMAGE_TO_TWO_SEPARATE_XF
		//bool bIsFromGraph = false; ///---Sim 07-20-2009 QA81-13817 DIRTY_FIX_GUI_ISSUE_OF_IMPORT_MAT_FILE_TO_WKS
		////Page ipg;
		//GraphPage gp;
		//LayoutPage lyop;
		////if(okxf_resolve_string_get_origin_object(trGetN.ipg.strVal, &ipg))
		//if(okxf_resolve_string_get_origin_object(trGetN.ipg.strVal, &gp)
			//|| okxf_resolve_string_get_origin_object(trGetN.ipg.strVal, &lyop) )
		//{
			////int nType = ipg.GetType();
			////if ( EXIST_PLOT == nType || EXIST_LAYOUT == nType )
			//if ( gp || lyop )
			//{
				//trGetN.ipg.Show = 1;
				//trGetN.orng.Show = 0;
				//trGetN.options.Show = 0; /// Hong 09/11/07 v8.0700 ADD_IMP_MODE_OPTIONS
				//nRet = XFEVT_PROCEED_NO_DLG; ///---Sim 02-16-2009 IMPORT_IMG_FROM_GRAPH_NO_NEED_DLG
				//bIsFromGraph = true; ///---Sim 07-20-2009 QA81-13817 DIRTY_FIX_GUI_ISSUE_OF_IMPORT_MAT_FILE_TO_WKS
			//}
		//}
		//
		/////---Sim 07-20-2009 QA81-13817 DIRTY_FIX_GUI_ISSUE_OF_IMPORT_MAT_FILE_TO_WKS
		//if ( !bIsFromGraph )
		//{
			//Worksheet wks;
			//if ( okxf_resolve_string_get_origin_object(trGetN.orng.strVal, &wks) && wks )
			//{
				//trGetN.orng.strVal = STR_NEW;
			//}
		//}
		/////---END QA81-13817 DIRTY_FIX_GUI_ISSUE_OF_IMPORT_MAT_FILE_TO_WKS
		/// End QA80-13980 CLEAN_UP_AND_IMPROVE_XF_IMP_IMAGE_TO_TWO_SEPARATE_XF
	}
	if(nGetNDialog >= 0)
	{
		if ( LTXF_FROM_GUI_MENU & dwCntrl )
			trGetN.fname.strVal = "";
	}
	/// 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))
		{
			//int nFilesRet = get_open_box_by_file_group(strFileName, "Image", "Image", true);
			///Sophy 6/4/2010 ORG-150 IMAGE_GROUP_FOR_IMPORT_IMAGE_FILE_DLG
			//int nFilesRet = get_open_box_by_file_group(strFileName, "Image", NULL, true); /// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
			///Sophy 6/24/2010 ORG-220-S6 SHOW_OPTIONS_DIALOG_CHECKBOX_WHEN_OPEN_DLG
			//int nFilesRet = get_open_box_by_file_group(strFileName, "ImageImp", "Images", true);
			string strOptionsLabel = (nGetNDialog != 0) ? get_show_options_label(trGetN) : "";
			int nShowDlg = 1;
			int nFilesRet = get_open_box_by_file_group(strFileName, "ImageImp", "Images", true, NULL, NULL, strOptionsLabel, &nShowDlg);
			if ( nGetNDialog != 0 && nShowDlg == 0 )
				nRet = XFEVT_PROCEED_NO_DLG;
			///end SHOW_OPTIONS_DIALOG_CHECKBOX_WHEN_OPEN_DLG
			///end IMAGE_GROUP_FOR_IMPORT_IMAGE_FILE_DLG
			/// 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
	
	///---Sim 07-20-2009 QA81-13817 DIRTY_FIX_GUI_ISSUE_OF_IMPORT_MAT_FILE_TO_WKS
	/// Kenny 07/21/2009 QA80-13980 CLEAN_UP_AND_IMPROVE_XF_IMP_IMAGE_TO_TWO_SEPARATE_XF
	//if ( -1 == nGetNDialog )
	//{
		//if ( PDS_NEW == cvt_str_to_predefined_type(trGetN.orng.strVal) )
		//{
			//MatrixPage mp;
			//mp.Create("Origin");
			//MatrixLayer ml = mp.Layers();
			//if ( ml )
			//{
				//DataRange dr;
		        //dr.Add("DataRange", ml, 0, 0, -1, 0);
		        //trGetN.orng.strVal = dr.GetDescription();
			//}
		//}
	//}
	/// End QA80-13980 CLEAN_UP_AND_IMPROVE_XF_IMP_IMAGE_TO_TWO_SEPARATE_XF
	///---END QA81-13817 DIRTY_FIX_GUI_ISSUE_OF_IMPORT_MAT_FILE_TO_WKS
	
}
//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 impImage_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
