#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
#include <GetNBox.h>

#include <..\originlab\file_utils.h>
/// AW 07/19/06 WAV_UNDEPENDENT_IMPOT_UTIL
//#include <..\originlab\Filter_Utils.h>  
//#include <..\originlab\import_utils.h>
/// END WAV_UNDEPENDENT_IMPOT_UTIL
#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>// Hong 9/11/06 SUPPORT_MULTI_FILES_INXF
#include <iw_xf.h>
#include <..\originlab\import_utils.h> ///---Sim 09-28-2006 FIX_IMPORT_MODE_SHOWING_ORDER
///---Sim 08-18-2006 HANDLE_ERR_MESSAGE_USE_DLL
#include <oErrMsg.h>
#include <ocu.h>
///---END HANDLE_ERR_MESSAGE_USE_DLL
#include <..\originlab\XFunctionEx.h> /// AW 09/25/06 MORE_WORK_ON_IMPWAV

//put additional include files here

//put your own support static functions here

////////////////////////////////////////////////////////////////////
/// Modification Log:
/// EJP 2006-10-26 v8.0501 QA70-9073 XUNIT_AND_XLONGNAME_FOR_X_AXIS_TITLE
/// EJP 2006-10-30 v8.0502 QA70-9073 ADD_XUNITS_AND_XLONGNAME_ARGS_TO_EVENSAMPLING
/// Hong 03/21/07 v8.0586 FIX_FAIL_DUMP_CORRECT_ERROR_STRING
/// 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-11-2007 FIX_8_BITS_DATA_TYPE
/// Sim 05-14-2007 SUPPORT_32_BITS_PCM
/// Sim 05-14-2007 HIDDEN_UNKNOWN_CHUNK_WARNING_MSG
/// Sim 05-17-2007 FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
/// Sim 05-18-2007 REMOVE_UID_VARIABLE
/// EJP 2007-05-30 v8.0631 QA70-9796 OC_ACCESS_TO_FDLOG
/// Sim 06-25-2007 UPDATE_ORGANIZER
/// Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
/// Sim 12-13-2007 IMPROVE_CONVERT_SAMPLE_BITS
/// Sim 10-27-2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
/// Sim 02-04-2010 QA81-15063 MOVE_IMP_FILE_INFO_OUT_FROM_COL_USER_INFO_TREE
/// Sim 02-05-2010 QA81-15063 ROLL_BACK_MOVE_COL_INFO_OUT_OF_USER_TREE
////////////////////////////////////////////////////////////////////

#define IMPWAV_TIME 0x00000001

#define GET_NAME_FROM_ID(_lpcszName_, _nSize_, _id_ ) char _lpcszName_[_nSize_]; \
	memcpy(_lpcszName_, &_id_, _nSize_ -1 ); 

#define WAV_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 WAV_IMPORT_MODE_LIST	(WAV_1ST_FILE_IMPORT_MODE_LIST & (~IW_FLAG_IMPMODE_REPLACE)) ///---Sim 05-08-2007 REMOVE_REPLACE_OUT_OF_MULTI_FILE_IMPORT_MODE
	
///---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


class WAVFile : public BinFile
{
public:
	///---Sim 02-07-2007 NOT_DELETE_ORIGINAL_COLUMN
	//WAVFile(BOOL bImport = true) 
	//:BinFile(bImport)
	//{
	//}
	WAVFile(LPCSTR lpcszFileName, BOOL bImport = true) 
	:BinFile(lpcszFileName, bImport)
	{
	}
	///---END NOT_DELETE_ORIGINAL_COLUMN
	
public:
	///---Sim 05-17-2007 FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
	//virtual int Import(LPCSTR lpcszFileName, Worksheet &wks, TreeNode& trFileInfo, int& nCols, bool bAddTiemCol = FALSE, int nC1 = 0)
	virtual int Import(Worksheet &wks, TreeNode& trFileInfo, int& nCols, bool bAddTiemCol = FALSE, int nC1 = 0)
	///---END FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
	{
		///---Sim 05-17-2007 FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
		if ( !IsOpen() )
			return IMPERR_FAILED_OPEN;
		
		int nRet; 
		//if ( nRet = ReadHeader(lpcszFileName, trFileInfo ) )
		if ( nRet = ReadHeader(trFileInfo ) )
			return nRet;
		///---END FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
		DWORD dwFlags =  (bAddTiemCol) ? IMPWAV_TIME : 0;
		///---Sim 02-07-2007 NOT_DELETE_ORIGINAL_COLUMN
		//wks.SetSize(-1, nC1); // Hong 12/13/06 REMOVE_EMPTY_COLUMN
		///---END NOT_DELETE_ORIGINAL_COLUMN
		//wks.SetName(GetFileName(lpcszFileName, true));
		// return ReadData(wks, trFileInfo, nCols, true, dwFlags, nC1); ///---Sim 07-12-2006 NO_NEED_BDATA
		///---Sim 02-07-2007 ADD_RANGE_INFO
		//return ReadData(wks, lpcszFileName, trFileInfo, nCols, dwFlags, nC1);///---Sim 07-12-2006 NO_NEED_BDATA
		if ( m_orng )
			m_orng.Reset();
		///---Sim 05-17-2007 FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
		//return ReadData(wks, lpcszFileName, trFileInfo, dwFlags, nC1);
		return ReadData(wks, trFileInfo, dwFlags, nC1);
		///---END FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
		///---END ADD_RANGE_INFO
	}

	///---Sim 05-17-2007 FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
	//virtual int ReadHeader(LPCSTR lpcszFileName, TreeNode& trFileInfo)
	virtual int ReadHeader(TreeNode& trFileInfo)
	///---END FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
	{
		///---Sim 05-17-2007 FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
		//if ( !Open(lpcszFileName) )
		if ( !IsOpen() )
			return IMPERR_FAILED_OPEN; ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL	
		///---END FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE

		Seek(0, file::begin);

		RIFFHDR rh;
		READ_DATA(&rh, sizeof(RIFFHDR), IMPERR_FAILED_READ) ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
		ENSURE(RIFF_ID, rh.id, IMPERR_UNKNOW_TYPE) ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
		WAVEHDR wh;
		READ_DATA(&wh, sizeof(WAVEHDR), IMPERR_FAILED_READ) ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
		ENSURE(WAVE_ID, wh.id, IMPERR_UNKNOW_TYPE) ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL

		///---Sim 02-14-2007 NOT_SHOW_WAVE_TYPE
		//GET_NAME_FROM_ID(lpcszFileType, 5, wh.id)
		//trFileInfo.AddTextNode(lpcszFileType, "Type");
		///---END NOT_SHOW_WAVE_TYPE
		return CER_NO_ERROR;  ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
	}
	//virtual int ReadData(Worksheet& wks, TreeNode& trFileInfo, int& nCols,\
					bool bData = TRUE, DWORD dwFlags = 0, int nC1 = 0) ///---Sim 07-12-2006 NO_NEED_BDATA
	///---Sim 02-07-2007 ADD_RANGE_INFO
	//virtual int ReadData(Worksheet& wks, LPCSTR lpcszFileName, TreeNode& trFileInfo, int& nCols,\
					DWORD dwFlags = 0, int nC1 = 0) ///---Sim 07-12-2006 NO_NEED_BDATA			
	///---Sim 05-17-2007 FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
	//virtual int ReadData(Worksheet& wks, LPCSTR lpcszFileName, TreeNode& trFileInfo, DWORD dwFlags = 0, int nC1 = 0)
	virtual int ReadData(Worksheet& wks, TreeNode& trFileInfo, DWORD dwFlags = 0, int nC1 = 0)
	///---END FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
	///---END ADD_RANGE_INFO
	{
		int nUnKnownChunks = 0; ///---Sim 02-08-2006 HOLD_UNKNOWN_CHUNKS_INFO
		
		//int err = 0; ///---Sim 07-12-2006 NO_NEED_VAR_ERR
		int nCols = 0; 	///---Sim 02-07-2007 ADD_RANGE_INFO
		int nRet;
//		char* apszWavFormatType[] = {"Unknown", "PCM/uncompressed", "Microsoft ADPCM", "ITU G.711 a-law", "ITU G.711 ?-law", "IMA ADPCM", "ITU G.723 ADPCM (Yamaha)",\
									"GSM 6.10", "ITU G.721 ADPCM", "MPEG", };
		///---Sim 02-07-2007 ADD_RANGE_INFO
		//if (&nCols != NULL)	nCols = ncols; ///---Sim 07-12-2006 INIT_NUM_OF_COL_IMPORTED
		///---END ADD_RANGE_INFO
		
		if (!IsOpen())
			return IMPERR_FAILED_OPEN; ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
		
		int iFileSize = GetLength(); // Get the length of the file in bytes
		
		int nSize;   /// AW 08/01/06 ADD_DATA_RANGE_IN_XF_IMPORT_FILE
	
	
		// Should be a good WAV file. Still need to verify that it's PCM encoded
		CHUNKHDR ch;
		FMTWAVEFORMAT fmt;
		int iPos = GetPosition();
		
		///---Sim 11-15-2006 FIX_BUG_FOR_UNKNOW_CHUNK
		/*
		// read fmt chunk
		READ_DATA(&ch, sizeof(CHUNKHDR), IMPERR_FAILED_READ) ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
		ENSURE(RIFFCHUNK_FMT, ch.id, IMPERR_UNKNOW_TYPE) ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
		READ_DATA(&fmt, sizeof(FMTWAVEFORMAT), IMPERR_FAILED_READ); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
		if ( WAVE_FORMAT_PCM != fmt.wFormatTag )	// Only PCM formatting supported
		{
			///---Sim 08-18-2006 HANDLE_ERR_MESSAGE_USE_DLL
			//out_str("Unsupport Wave type");// %s", apszWavFormatType[fmt.wFormatTag]);
			//XF_THROW(IMPERR_WAVE_UNKNOW_TYPE);
			///---END HANDLE_ERR_MESSAGE_USE_DLL
			return IMPERR_WAVE_PCM; ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
		}
		// Put the PCM format information as header info into trResult
		if( trFileInfo )
		{
			TreeNode trHeader = tree_check_get_node(trFileInfo, IMPTREE_NODE_HEADER);
			TreeNode trFMT = tree_check_get_node(trHeader, IMPTREE_NODE_FMT);
			ASSERT(trFMT);
			trFMT = fmt;
			trFMT.AddTextNode("PCM", "WaveType");
		}
		
		
		iPos = GetNextChunKPos(iPos, ch.len);
		*/
		bool bReadFmt = false;
		///---END FIX_BUG_FOR_UNKNOW_CHUNK

		while( iPos < iFileSize )
		{
			READ_DATA(&ch, sizeof(CHUNKHDR), IMPERR_FAILED_READ) ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
	
			switch (ch.id)
			{
			case  RIFFCHUNK_FMT:
				///---Sim 11-15-2006 FIX_BUG_FOR_UNKNOW_CHUNK
				if ( bReadFmt )
					return IMPERR_WAVE_DUPLICA_FMT;  ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
				
				bReadFmt = true;
					
				// read fmt chunk
				READ_DATA(&fmt, sizeof(FMTWAVEFORMAT), IMPERR_FAILED_READ); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
				if ( WAVE_FORMAT_PCM != fmt.wFormatTag )	// Only PCM formatting supported
				{
					///---Sim 08-18-2006 HANDLE_ERR_MESSAGE_USE_DLL
					//out_str("Unsupport Wave type");// %s", apszWavFormatType[fmt.wFormatTag]);
					//XF_THROW(IMPERR_WAVE_UNKNOW_TYPE);
					///---END HANDLE_ERR_MESSAGE_USE_DLL
					return IMPERR_WAVE_PCM; ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
				}
				
				// Put the PCM format information as header info into trResult
				if( trFileInfo )
				{
					TreeNode trHeader = tree_check_get_node(trFileInfo, IMPTREE_NODE_HEADER);
					TreeNode trFMT = tree_check_get_node(trHeader, IMPTREE_NODE_FMT);
					ASSERT(trFMT);
					///---Sim 01-16-2007 FOR_SHOW_ORGANIZER_TO_USER
					//trFMT = fmt;
					FMTWAVEFORMAT_SHOW_USER fmtForUser;
					ASSERT(sizeof(FMTWAVEFORMAT) == sizeof(FMTWAVEFORMAT_SHOW_USER));
					memcpy(&fmtForUser, &fmt, sizeof(fmt));
					trFMT = fmtForUser;
					trFMT.Format.strVal = GetFormatString(fmtForUser.Format);
					//trFMT.AddTextNode("PCM", "WaveType");
					///---END FOR_SHOW_ORGANIZER_TO_USER
				}
				
				break;
				///---END FIX_BUG_FOR_UNKNOW_CHUNK
			case RIFFCHUNK_DATA:
				///---Sim 11-15-2006 FIX_BUG_FOR_UNKNOW_CHUNK
				if ( !bReadFmt ) // must read format first
				{
					XF_WARN(IMPWARN_WAVE_LOST_DATA_FOR_NOT_FORMAT)
					break;
				}
				///---END FIX_BUG_FOR_UNKNOW_CHUNK
				
				//-------------- set X column -----------------
				int	iNumCols = wks.GetNumCols();
				if ( nC1 < 0 || nC1 > iNumCols )
					return IMPERR_WRONG_COL;  ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
				int iIndexCol = nC1;
				if( iNumCols < iIndexCol+1 )
					wks.AddCol();
				double dTimeStep = 1.0/fmt.dwSamplesPerSec;
				if( dwFlags & IMPWAV_TIME )
				{
					// configure Time column property
					///---Sim 02-07-2007 ADD_RANGE_INFO
					//m_orng.Add(wks, iIndexCol, "X");	/// AW 08/01/06 ADD_DATA_RANGE_IN_XF_IMPORT_FILE
					///---END ADD_RANGE_INFO
					Column cc(wks, iIndexCol++);
					cc.SetLabel("Time");
					cc.SetType(OKDATAOBJ_DESIGNATION_X);
					cc.SetInternalDataType(FSI_DOUBLE, 0);
					//cc.SetUpperBound(iSamples-1);
					
					nCols++; ///---Sim 02-07-2007 ADD_RANGE_INFO
					
					// Attach columns
					Dataset	dsTime(wks, iIndexCol-1);
					
					// calculate sample and data sizes
					int iDataSize = fmt.wBitsPerSample / 8;
					int iSamples = ch.len / (fmt.wChannels * iDataSize);
					
					// Fill Time column, - Using OriginC equivalent of data(min,max,inc)
					dsTime.Data(0, dTimeStep * (iSamples - 1), dTimeStep);   // starts from 0
				}
				
				//-------------- prepare channel column -----------------
				if( wks.GetNumCols() < iIndexCol+1 )
					wks.AddCol();
				if( 1 == fmt.wChannels )
				{
					///---Sim 07-17-2006 ADD_FILE_NAME_TO_COL_AS_LONG_NAME
					//wks.Columns(iIndexCol).SetLabel("Data");
					wks.Columns(iIndexCol).SetLabel(GetFileName(m_strFileName, TRUE));
					///---End
					nCols++; ///---Sim 02-07-2007 ADD_RANGE_INFO
				}
				else
				{
					ASSERT(2 == fmt.wChannels);
					///---Sim 07-17-2006 ADD_FILE_NAME_TO_COL_AS_LONG_NAME
					//wks.Columns(iIndexCol).SetLabel("Left");
					wks.Columns(iIndexCol).SetLabel(GetFileName(m_strFileName, TRUE)+"-Left");
					///---End
					nCols++; ///---Sim 02-07-2007 ADD_RANGE_INFO
					if( wks.GetNumCols() <= iIndexCol+1 )
						wks.AddCol();
					///---Sim 07-17-2006 ADD_FILE_NAME_TO_COL_AS_LONG_NAME
					//wks.Columns(iIndexCol+1).SetLabel("Right");
					wks.Columns(iIndexCol+1).SetLabel(GetFileName(m_strFileName, TRUE)+"-Right");
					///---End
					nCols++; ///---Sim 02-07-2007 ADD_RANGE_INFO
				}
				/// AW 09/25/06 MORE_WORK_ON_IMPWAV
				/*
				//---- CPY 8/11/06 SAMPLE_RATE_IN_COL_LABEL
				//wks.ShowLabels();
				if( dwFlags & IMPWAV_TIME )
					wks_set_show_labels(wks, RCLT_LONG_NAME);
				else
					wks_set_show_labels(wks, RCLT_LONG_NAME, RCLT_SAMPLE_RATE);
				//----	
				*/
				/// END MORE_WORK_ON_IMPWAV
				
				int nDataType;
				nSize = ch.len;
				switch(fmt.wBitsPerSample)
				{
				case 8:
					///---Sim 12-13-2007 IMPROVE_CONVERT_SAMPLE_BITS
					/////---Sim 05-11-2007 FIX_8_BITS_DATA_TYPE
					////nDataType = FSI_BYTE;
					//nDataType = FSI_CHAR;
					/////---END FIX_8_BITS_DATA_TYPE
					nDataType = FSI_BYTE;
					///---END IMPROVE_CONVERT_SAMPLE_BITS
					nSize = ch.len;
					break;
				case 16:
					nDataType = FSI_SHORT;
					nSize = ch.len/2;
					break;
				///---Sim 05-14-2007 SUPPORT_32_BITS_PCM
				case 32:
					nDataType = FSI_LONG;
					nSize = ch.len/4;
					break;
				///---END SUPPORT_32_BITS_PCM
				default:
					return IMPERR_WAVE_SAMPLE_BIT; ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
				}
				
				nSize = nSize / fmt.wChannels;
				for( int ich = 0; ich < fmt.wChannels; ich++ )
				{
					// Get the ich channel's data
					Column cc(wks, ich+iIndexCol);
					if ( !(dwFlags & IMPWAV_TIME ) )
					{
						cc.SetType(OKDATAOBJ_DESIGNATION_Y);
						
						///---Sim 02-07-2007 ADD_RANGE_INFO
						//m_orng.Add(wks, ich+iIndexCol, "Y");	/// AW 08/01/06 ADD_DATA_RANGE_IN_XF_IMPORT_FILE
						///---END ADD_RANGE_INFO

						/// EJP 2006-10-30 v8.0502 QA70-9073 ADD_XUNITS_AND_XLONGNAME_ARGS_TO_EVENSAMPLING
						///	if ( nRet = wks.SetEvenSampling(0, dTimeStep, cc ) )
						///		return nRet;		// maybe need change here
						///	/// EJP 2006-10-26 v8.0501 QA70-9073 XUNIT_AND_XLONGNAME_FOR_X_AXIS_TITLE
						///	if( cc )
						///		setColXUnitsAndLongName(cc, "second", _L("Time"));
						///	/// end XUNIT_AND_XLONGNAME_FOR_X_AXIS_TITLE
						if( nRet = wks.SetEvenSampling(0, dTimeStep, cc, _L("second"), _L("Time")) )
							///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL	
							//return nRet;		// maybe need change here
							return IMPERR_SAMPLE_RATE;
							///---END HANDLE_ERR_MESSAGE_USE_DLL
						/// end ADD_XUNITS_AND_XLONGNAME_ARGS_TO_EVENSAMPLING
					}
					cc.SetUpperBound(nSize-1);
					cc.SetLowerBound(0);			/// AW 07/17/06 NEED_SET_FOR_REPLACE_DATA_MODE
					
					/// AW 07/24/06 TEMP_FIXING_MULTI_CHANNEL_BUG
					/*
	 				//int nRet = file_read_data(*(file*)this, cc, nDataType, ch.len/fmt.wChannels);/// crash
					if (  nRet = file_read_data(*this, cc, nDataType, ch.len/fmt.wChannels) )
						return nRet;
					*/
					cc.SetInternalDataType(nDataType, 0);  // need remove after the bug fixed tomorrow
					/// END TEMP_FIXING_MULTI_CHANNEL_BUG
					
					///---Sim 02-04-2010 QA81-15063 MOVE_IMP_FILE_INFO_OUT_FROM_COL_USER_INFO_TREE
					///Cheney 2007-1-27 ADD_USER_INFO_TREE
					//string strColumnInfo = "ColumnInfo";
					//Tree trColumnInfo;
					/////---Sim 05-17-2007 FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
					////trColumnInfo.AddTextNode(lpcszFileName, "ImportFile");
					//trColumnInfo.AddTextNode(m_strFileName, "ImportFile");
					/////---END FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
					//trColumnInfo.Enable = ENABLE_READ_ONLY;
					//trColumnInfo.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_OPEN);
					//bool bRet = set_user_info(cc, strColumnInfo, trColumnInfo);
					/////end ADD_USER_INFO_TREE
					fu_set_import_file_name_info(cc, m_strFileName, IMPORT_INFO_TO_USER_TREE);
					///---Sim 02-05-2010 QA81-15063 ROLL_BACK_MOVE_COL_INFO_OUT_OF_USER_TREE
					// roll back move column info out of user tree, as CP said
					//fu_set_import_file_name_info(cc, m_strFileName);
					///---END QA81-15063 ROLL_BACK_MOVE_COL_INFO_OUT_OF_USER_TREE
					///---END QA81-15063 MOVE_IMP_FILE_INFO_OUT_FROM_COL_USER_INFO_TREE
				}
				
				//-------------- read channel data -----------------
				/// AW 07/24/06 TEMP_FIXING_MULTI_CHANNEL_BUG
				switch(fmt.wBitsPerSample)
				{
				case 8:
					///---Sim 12-13-2007 IMPROVE_CONVERT_SAMPLE_BITS
					/////---Sim 05-11-2007 FIX_8_BITS_DATA_TYPE
					////IMPORT_WKS_FOR_ROW_CONTINUE(byte, wks, 0, iIndexCol, nSize, fmt.wChannels) 
					//IMPORT_WKS_FOR_ROW_CONTINUE(char, wks, 0, iIndexCol, nSize, fmt.wChannels) 
					/////---END FIX_8_BITS_DATA_TYPE
					IMPORT_WKS_FOR_ROW_CONTINUE(byte, wks, 0, iIndexCol, nSize, fmt.wChannels) 
					///---END IMPROVE_CONVERT_SAMPLE_BITS
				case 16:
					IMPORT_WKS_FOR_ROW_CONTINUE(short, wks, 0, iIndexCol, nSize, fmt.wChannels)
				///---Sim 05-14-2007 SUPPORT_32_BITS_PCM
				case 32:
					IMPORT_WKS_FOR_ROW_CONTINUE(int, wks, 0, iIndexCol, nSize, fmt.wChannels)
				///---END SUPPORT_32_BITS_PCM
				default:
					return IMPERR_WAVE_SAMPLE_BIT; ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
				}
				/// END TEMP_FIXING_MULTI_CHANNEL_BUG
				
				break;
			case  RIFFCHUNK_LIST:
			
				// Get LIST subchunk
				char szSubChunkName[5];
				
				READ_DATA(&szSubChunkName, 4, IMPERR_FAILED_READ); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
				
				szSubChunkName[4] = '\0';
				
				// Add "LIST" chunk into Log
				if( trFileInfo )
				{
					TreeNode trnLog = tree_check_get_node(trFileInfo, IMPTREE_NODE_LOG);
					TreeNode trLIST = tree_check_get_node(trnLog, IMPTREE_NODE_LIST);
					ASSERT(trLIST);
					
					int nChunkSize = ch.len - 4;
					
					if( lstrcmpi(szSubChunkName, "INFO") )
					{
						// Not "INFO" subchunk, just add, more work if needed
						TreeNode trSubChunk = tree_check_get_node(trLIST, szSubChunkName);
						ASSERT(trSubChunk);
						
						vector<char> vchunk;
						vchunk.SetSize(nChunkSize);
						
						READ_DATA(vchunk, nChunkSize, IMPERR_FAILED_READ); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
						
						trSubChunk.bVals = vchunk;
					}
					else
					{
						// "INFO" subchunk
						TreeNode trSubInfo = tree_check_get_node(trLIST, szSubChunkName);
						ASSERT(trSubInfo);
						
						int nRet = add_LIST_INFO_to_tree(trSubInfo, nChunkSize); 
						if( nRet )
							 return nRet;
					}
				}
				break;
			default:
				GET_NAME_FROM_ID(lpcszUnknownChunkType, 5, ch.id )
				///---Sim 02-08-2007 HOLD_UNKNOWN_CHUNKS_INFO
				TreeNode trUnKnownChunk = trFileInfo.GetNode("UnKnownChunks");
				if ( !trUnKnownChunk )
					trUnKnownChunk = trFileInfo.AddNode("UnKnownChunks");
				trUnKnownChunk.AddTextNode(lpcszUnknownChunkType, "Chunk" + (++nUnKnownChunks));
				//XF_WARN_EX(IMPWARN_WAVE_UNKNOWN_CHUNK, lpcszUnknownChunkType); ///---Sim 11-15-2006 SHOW_WARNING_MESSAGE
				///---END HOLD_UNKNOWN_CHUNKS_INFO
			}
	
			// Position to next Chunk
			iPos = GetNextChunKPos(iPos, ch.len);
		}
		
		///---Sim 02-07-2007 ADD_RANGE_INFO
		//if (&nCols != NULL)
			//nCols = ncols;
		
		///// AW 08/01/06 ADD_DATA_RANGE_IN_XF_IMPORT_FILE
		//DataRange dr;
		//dr.Add("Import", wks, 0, nC1, nSize - 1 , nC1 + ncols - 1);
		//TreeNode trRange;
		//trRange = trFileInfo.AddNode("FileDataRange");
		//dr.GetTree(trRange);
		///// END ADD_DATA_RANGE_IN_XF_IMPORT_FILE
		//m_orng.Add(wks, nC1, STR_RANGE_STR, nC1 + nCols - 1, 0, nSize - 1);
		m_orng.Add(wks, nC1, STR_RANGE_STR, nC1 + nCols - 1);
		///---END ADD_RANGE_INFO
		
		///---Sim 08-02-2006 WHERE_GET_IMPORT_INFO_FROM_DATA_RANGE
		//m_orng.SetRange(0, wks, 0, nC1, nSize - 1 , nC1 + ncols - 1);
		///---END WHERE_GET_IMPORT_INFO_FROM_DATA_RANGE
		return CER_NO_ERROR;  ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL	
	}
	
protected:
	///---Sim 01-16-2007 GET_FORMAT_STRING
	string GetFormatString(int nFmtTag)
	{
		string strFmt;
		
		switch ( nFmtTag )
		{
		case WAVE_FORMAT_PCM:
			strFmt = "PCM";
		default:
			strFmt = "Unknow";
		}
		
		return strFmt;
	}
	///---END GET_FORMAT_STRING
	int GetNextChunKPos(int iPos, int nLength)
	{
		// Position to next Chunk
		Seek(iPos + sizeof(CHUNKHDR) + nLength, file::begin);
		iPos = GetPosition();

		// Align on 16 bit boundary
		if( iPos & 1)
		{
			Seek(1, file::current);
			iPos++;
		}
		return iPos;
	}
	/// before call this function, you need set right size first 
//	int ReadToData(DataObject& dd, int nInternalType, UINT nBufferSize);
private:
//	void id_to_name(LPSTR lpsz, DWORD id);
	int add_LIST_INFO_to_tree(TreeNode& trInfo, int nChunkSize)
	{
		int nBytesRead = 0;
		CHUNKHDR chSub;
		string strValue;
		LPSTR lpsz = NULL;
		TreeNode trSubCk;
		int iPos;
	
		while( nBytesRead < nChunkSize )
		{
			// Read subchunk
			if (Read(&chSub, sizeof(CHUNKHDR)) != sizeof(CHUNKHDR))
				return IMPERR_FAILED_READ;  ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
				
			// Read subchunk data
			lpsz = strValue.GetBuffer(chSub.len + 1);
			Read(lpsz, chSub.len);
			strValue.ReleaseBuffer();
	
			// Add subchunk to INFO branch
			GET_NAME_FROM_ID(szCkName, 5, chSub.id);
			trSubCk = tree_check_get_node(trInfo, szCkName);
			ASSERT(trSubCk);
			trSubCk.strVal = strValue;
					
			nBytesRead += sizeof(CHUNKHDR) + chSub.len;
			
			iPos = GetPosition();
			// Align on 16 bit boundary
			if( iPos & 1) 
			{
				Seek(1, file::current);
				nBytesRead++;
			}
		}
		
		return CER_NO_ERROR;  ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL	
	}
};

/// 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;
	//TreeNode trFile, trFileInfo; ///---Sim 11-08-2006 NO_NEED
	if ( trInfo )
		trInfo.RemoveChildrenWithPrefix();
	GETN_USE(trInfo)
	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));
		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.trFiles;
	
	// 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


void impWav(const string& fname, const TreeNode& trFiles, 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
	
	
	//ImportWAV impwav;	
	Worksheet ws;
	//---- CPY 7/20/06 NEW_MAT_WKS_SUPPORT
	//int r1,r2,c1,c2; 
	//orng.GetRange(0, r1, c1, r2, c2, ws); //we only need wks and c1
	int nC1,nC2;
	///---Sim 12-20-2006 FIX_BUG_WHEN_INPUT_PARA_IS_MATRIX
	//orng.GetRange(ws, c1, c2);	
	Datasheet ds;
	/// Hong 03/28/07 v8.0591 CENTRALIZE_CHECK_CALID_RANGE
	//orng.GetRange(ds, nC1, nC2);
	Page pgTarget;
	if ( !GetRangeInfo(orng, ds, pgTarget, nC1, nC2) )
		XF_THROW(XFERR_INVALID_RANGE);
	/// end CENTRALIZE_CHECK_CALID_RANGE
	ws = ds;
	///---END FIX_BUG_WHEN_INPUT_PARA_IS_MATRIX
	if(!ws)
		XF_THROW(CER_INVALID_WKS);///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
	//----
	///---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
	
	///---Sim 10-30-2006 MAKE_INDIVIDUAL_FILE_INFO_FOR_EACH_FILE	
	/*
	Tree trInfo;
	if ( finfo )
		trInfo = finfo;
	*/
	Tree trBaseInfo; 
	/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
	//if ( finfo )
		//trBaseInfo = finfo;
	/// end FINFO_IS_ONLY_OUTPUT
	///---END MAKE_INDIVIDUAL_FILE_INFO_FOR_EACH_FILE	
	/// Hong 01/25/07 REMOVE_AUTO_IMPORT_MODE
	/*
	///---Sim 09-28-2006 FIX_IMPORT_MODE_SHOWING_ORDER
	int nFileImportMode = _GetImportModeFromListIndex(impmode);
	///---END FIX_IMPORT_MODE_SHOWING_ORDER
	///---Sim 11-3-2006 SUPPORT_AUTO_MODE
	if ( ASCIMP_MODE_AUTO == nFileImportMode )
		nFileImportMode = ASCIMP_MODE_APPEND_COLS;
	///---END SUPPORT_AUTO_MODE
	*/
	//int nFileImportMode = _GetImportModeFromListIndex(impmode + 1); ///---Sim 01-29-2007 MORE_OPTIONS_FOR_IMPORT remove	
	/// end REMOVE_AUTO_IMPORT_MODE
	///---Sim 09-22-2006 GET_TIME_COL_OPTION_IF_FILTER_HAS_IT
	/*
	BOOL bShowTimeCol = timecol;
	if ( trFilter && trFilter.IsValid() )
	{
		bShowTimeCol = fuGetShowTime(trFilter)
	}
	*/
	///---END GET_TIME_COL_OPTION_IF_FILTER_HAS_IT
	
	///---Sim 01-29-2007 MORE_OPTIONS_FOR_IMPORT
	TreeNode tnUsingFilter; // filter is none
	double dDefault = 0.;
	if ( fuGetDouble(options, IDE_DISP_IMP_ORIGINVER, dDefault) )
		tnUsingFilter = options;
	///end ADD_FILTER
	
	int nFileImportMode = fuGetImportMode(tnUsingFilter);
	
	///---Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
	/*
	bool bSparkLines = fuGetSparkLine(tnUsingFilter);
	/// AW 09/25/06 MORE_WORK_ON_IMPWAV
	XFunction xfSparkLine;
	TreeNode trXFSparkLine;
	
	if ( bSparkLines )
	{
		if(!xfSparkLine.Load(&trXFSparkLine, "sparklines", 0, true, true, true))
		{
			XF_THROW(IMPERR_IMPORT_FUNC_ERR);// a better global err for not matching book would be better
				return;
		}
		xfSparkLine.SetArg("sel", 0);
	}
	/// END MORE_WORK_ON_IMPWAV
	*/
	int nSparklines = fuGetSparkLine(tnUsingFilter);
	///---END SUPPORT_SMART_SPARKLINE
	
	//bool bTimeCol = timecol; ///---Sim 11-08-2006 MORE_WORK_ON_SHOW_TIME_COL
	bool bTimeCol = fuGetShowTime(tnUsingFilter);
	///---END MORE_OPTIONS_FOR_IMPORT
	
	//WAVFile myWav(); 	///---Sim 05-17-2007 FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE remove
	int nRet, nCols;
	/// Hong 9/11/06 SUPPORT_MULTI_FILES_INXF
	/*
	if ( nRet = myWav.Import(fname, ws, trInfo, nCols, timecol, c1 ) )
	{
		XF_THROW(nRet);
	}
	else //if( !trFilter )   ///AW 08/01/06 MORE_ON_TRINFO CAN"T SEE any reason need trFilter be invalid here  
	{
		//dwTime = GetTickCount() - dwTime;
		//printf("\nTime cost %f \n",dwTime); 
		/// AW 08/01/06 ADD_DATA_RANGE_IN_XF_IMPORT_FILE
		//	orng = myWav.GetDataRange();  
		myWav.GetDataRange().Clone(orng);
		/// END ADD_DATA_RANGE_IN_XF_IMPORT_FILE
		///Hong 8/04/06 ADD_FILTER_INFO
		//impinfo_AddFile(ws, fname, trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC);
		impinfo_AddFile(ws, fname, trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, trFilter);
		///end ADD_FILTER_INFO
	}
	*/
	/// Hong 03/28/07 v8.0591 CENTRALIZE_CHECK_CALID_RANGE
	//Page pgTarget;
	//ws.GetParent(pgTarget);
	/// end CENTRALIZE_CHECK_CALID_RANGE
	StringArray vsFileNames;
	//str_separate(fname, "\n", vsFileNames); 
	separate_multiline_files(fname, vsFileNames);// Hong 9/12/06 FIX_SEPARATE_FILENAME 
	
	int nFileCount = vsFileNames.GetSize();
	///---Sim 09-28-2006 CHECK_VALID_FILE_NAME
	if ( nFileCount <= 0 )
		XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
	///---END CHECK_VALID_FILE_NAME
	///---Sim 09-06-2006 FIX_EXCESSIVE_EMPTY_SHEET
	//int nIndexLayer;
	int nIndexLayer = ws.GetIndex();
	///---END FIX_EXCESSIVE_EMPTY_SHEET
	///---Sim 11-3-2006 MORE_SOFTWARE_QUALITY
	{
		waitCursor wc; 
	///---END MORE_SOFTWARE_QUALITY
		for ( int ii = 0; ii < nFileCount; ii++ )
		{
			/// Hong 9/12/06 SUPPORT_MULTI_FILES_APPEND_COLS_MODE
			/*
			if( ASCIMP_MODE_APPEND_COLS ==  fuGetImportMode(trFilter))
				c1 = wks_find_empty_column(ws);
			/// Hong 9/13/06 FIX_NOT_EMPTY_COLUMN
			if ( c1 < 0 )
			{
				Worksheet wksTemp = ws;      
				c1 = wksTemp.GetNumCols();
				wksTemp.AddCol();
			}
			/// end FIX_NOT_EMPTY_COLUMN
			*/
			/// end SUPPORT_MULTI_FILES_APPEND_COLS_MODE
			///---Sim 09-19-2006 SUPPORT_DATA_RANGE
			//ws = GetTargetLayer(pgTarget, nIndexLayer, ii, trFilter); ///---Sim 09-06-2006 FIX_EXCESSIVE_EMPTY_SHEET
		
			/// AW 09/25/06 MORE_WORK_ON_IMPWAV
			//ws = GetTargetLayer(pgTarget, nIndexLayer, ii, trFilter, true, orng, ASCIMP_MODE_APPEND_COLS, c1);
			///---Sim 04-17-2007 FIX_MODIFY_CURRENT_DATA_RANGE_WILL_CAUSE_THE_RANGE_IN_PAGE_STORE_IS_CHANGED_UNEXPECTED
			//ws = GetTargetLayer(pgTarget, nIndexLayer, ii, tnUsingFilter, true, orng, nFileImportMode, nC1, bTimeCol); ///---Sim 09-28-2006 FIX_IMPORT_MODE_SHOWING_ORDER
			///---Sim 04-30-2007 TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
			//if ( 0 == ii && !reimp )
			//{
				//int nImportMode = get_first_file_import_mode(FILE_TYPE_WAV);
				//TreeNode trInValid;
				//
				//ws = GetTargetLayer(pgTarget, nIndexLayer, ii, trInValid, true, drTarget, nImportMode, nC1, bTimeCol);
			//}
			//else
			//{
			ws = GetTargetLayer(pgTarget, nIndexLayer, ii, tnUsingFilter, true, drTarget, nFileImportMode, nC1, bTimeCol);
			//}
			///---END TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
			///---END FIX_MODIFY_CURRENT_DATA_RANGE_WILL_CAUSE_THE_RANGE_IN_PAGE_STORE_IS_CHANGED_UNEXPECTED
			/// END MORE_WORK_ON_IMPWAV
			
			///---END SUPPORT_DATA_RANGE
			///---Sim 10-30-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
			
			WAVFile myWav(vsFileNames[ii]); ///---Sim 05-17-2007 FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
	
			///---Sim 05-17-2007 FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
			//if ( nRet = myWav.Import(vsFileNames[ii], ws, trInfo, nCols, bTimeCol, nC1 ) ) ///---Sim 11-08-2006 MORE_WORK_ON_SHOW_TIME_COL
			if ( nRet = myWav.Import(ws, trInfo, nCols, bTimeCol, nC1 ) )
			///---END FIX_NOT_CLOSE_FILE_WHEN_IMPORT_MULTI_FILE
			{
				XF_THROW_EX(nRet, GetFileName(vsFileNames[ii])); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
			}
			else //if( !trFilter )   ///AW 08/01/06 MORE_ON_TRINFO CAN"T SEE any reason need trFilter be invalid here  
			{
				///---Sim 05-14-2007 HIDDEN_UNKNOWN_CHUNK_WARNING_MSG
				/*
				///---Sim 02-08-2007 SHOW_ERROR_MESSAGE_FOR_UNKNOWN_CHUNKS
				if ( trInfo.UnKnownChunks )
				{
					XF_WARN("There are some unknown chunks in file, see more details in book organizer.");
				}
				///---END SHOW_ERROR_MESSAGE_FOR_UNKNOWN_CHUNKS
				*/
				///---END HIDDEN_UNKNOWN_CHUNK_WARNING_MSG
				
				///---Sim 02-28-2007 QA80-9391 IMPORT_FILE_SKIP_DATA_POINTS
				myWav.GetDataRange().GetRange(ds, nC1, nC2);
				reduce_data(ws, nC1, nC2, tnUsingFilter);
				///---END QA80-9391 IMPORT_FILE_SKIP_DATA_POINTS
				
				///---Sim 02-01-2007 ADD_AUTO_NAME
				// rename 
				///---Sim 04-17-2007 FIX_APPEND_COMMENT_WHEN_REIMPORT
				DWORD dwCtrl = 0;
				dwCtrl |= (reimp) ? COVER_LAST_ADDED_COMMENT : 0;
				setPageAndLayerName(myWav.GetDataRange(), vsFileNames[ii], tnUsingFilter, dwCtrl);
				///---END FIX_APPEND_COMMENT_WHEN_REIMPORT
				///---END ADD_AUTO_NAME
				
				//dwTime = GetTickCount() - dwTime;
				//printf("\nTime cost %f \n",dwTime); 
				/// AW 08/01/06 ADD_DATA_RANGE_IN_XF_IMPORT_FILE
				//	orng = myWav.GetDataRange();  
				myWav.GetDataRange().Clone(orng); // Sim 04-17-2007 only for single file, need continue to code for multiple range
				/// END ADD_DATA_RANGE_IN_XF_IMPORT_FILE
				
				/// Hong 12/07/06 FIX_SPARK_LINE_DISAPPEAR
				/*
				/// AW 09/25/06 MORE_WORK_ON_IMPWAV
				int nLine4 = -1;
				int nLine5 = -1;
				if( sparklines )
				{
					xfSparkLine.SetArg("wks", ws);
					if ( !xfSparkLine.Evaluate() )
					{
						XF_THROW(XFERR_INVALID_MATOBJ);
						return;	
					}
				}
				if( bTimeCol ) ///---Sim 11-08-2006 MORE_WORK_ON_SHOW_TIME_COL
				{
					if ( sparklines )
						nLine4 = RCLT_SPARKLINE;	
				}
				else
				{
					nLine4 = RCLT_SAMPLE_RATE;		
					if ( sparklines ) 
						nLine5 = RCLT_SPARKLINE;	
				}
				wks_set_show_labels(ws, RCLT_LONG_NAME, nLine4, nLine5);
				/// END MORE_WORK_ON_IMPWAV
				*/
				//int nColNum = ws.GetNumCols();///---Sim 04-17-2007 FIX_RANGE
				///---Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
				/*
				if( bSparkLines )
				{
					xfSparkLine.SetArg("c1", nC1+1);
					
					//xfSparkLine.SetArg("c2", nColNum);
					xfSparkLine.SetArg("c2", nC2+1);
					///---END FIX_RANGE
					xfSparkLine.SetArg("wks", ws);
					if ( !xfSparkLine.Evaluate() )
					{
						XF_THROW(XFERR_INVALID_MATOBJ);
						return;	
					}
				}
				*/
				bool bSparklines = update_sparkline(nSparklines, ws, nC1, nC2);
				///---END SUPPORT_SMART_SPARKLINE
				//vector<int> vn = {RCLT_LONG_NAME, RCLT_UNIT, RCLT_COMMENT};
				vector<int> vn = {RCLT_LONG_NAME, RCLT_UNIT, RCLT_COMMENT}; 
				/*
				if( bTimeCol )
				{
					if ( bSparkLines )
						vn.Add(RCLT_SPARKLINE);	
				}
				else
				{
					vn.Add(RCLT_SAMPLE_RATE);
					if ( bSparkLines ) 
						vn.Add(RCLT_SPARKLINE);	
				}
				*/
				if( !bTimeCol )
					vn.Add(RCLT_SAMPLE_RATE);
				///---Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
				//if ( bSparkLines ) 
				if ( bSparklines ) 
				///---END SUPPORT_SMART_SPARKLINE
					vn.Add(RCLT_SPARKLINE);	
				
				wks_add_show_labels(ws, vn, false);
				/// end FIX_SPARK_LINE_DISAPPEAR
				
				///---Sim 02-01-2007 ADD_RANGE_INFO
				// add data range info
				Tree trDataRange;
				///---Sim 05-18-2007 REMOVE_UID_VARIABLE
				//int UID = getRangeInfo(trDataRange, myWav.GetDataRange());
				getRangeInfo(trDataRange, myWav.GetDataRange());
				//vImportedRangeUID.Add(UID);
				///---END REMOVE_UID_VARIABLE
				
				// if current file cover existing file data, need update info of that files
				if ( !reimp )
					impinfo_UpdateExistingData(pgTarget, myWav.GetDataRange());
	
				///Hong 8/04/06 ADD_FILTER_INFO
				//impinfo_AddFile(ws, fname, trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC);
				//impinfo_AddFile(ws, 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(ws, vsFileNames[ii], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, tnUsingFilter, ii + 1, trDataRange);
				impinfo_AddFile(ws, vsFileNames[ii], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, tnUsingFilter, ii + 1, trDataRange, reimp);
				///---END ONLY_UPDATE_BINARY_INFO_WHEN_REIMPORT
				///end ADD_FILTER_INFO
				///---END ADD_RANGE_INFO
			}
		}
	} ///---Sim 11-3-2006 MORE_SOFTWARE_QUALITY
	/// end SUPPORT_MULTI_FILES_INXF
	//refresh_organizer_gui(pgTarget); ///---Sim 06-25-2007 UPDATE_ORGANIZER
	///---Sim 05-18-2007 REMOVE_UID_VARIABLE
	//if ( uid )
		//uid = vImportedRangeUID;
	///---END 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 impWav_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return impWav_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	OK_SURE(bOKEnable); ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
	//printf("nrow=%d, cntrl = %d\n", nRow, nCntrlType);
	//out_tree(trGetN);
	
	///---Sim 11-20-2006 SUPPORT_DND_OPEN_DIALOG
	if ( (GETNE_ON_INIT == nEventID && trGetN.trFiles.Children.Count() == 0) || 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 trFiles on GUI
			trGetN.trFiles.RemoveChildrenWithPrefix();
		
			return true;
		}
		///---END ERROR_MESSAGE
		
		///---Sim 10-30-2006 ERROR_MESSAGE
		//ASCIMP	ascimp;
		StringArray vsFileNames;
		TreeNode trInfo = trGetN.trFiles;
		separate_multiline_files(trGetN.fname.strVal, vsFileNames);
		if ( !construct_tree_setting(vsFileNames, trInfo) )
		{
			// clean the trFiles 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 impWav_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	
	string strTagName = lpcszVarName;
	
	///---Sim 01-29-2007 MORE_OPTIONS_FOR_IMPORT
	if ( 0 == strcmp(lpcszVarName, "options") )
	{
		constructOptionsForXF(tr, true, true, true, false, 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, WAV_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, WAV_IMPORT_MODE_LIST);
			trImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			trImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			trImportMode.nVal = ASCIMP_MODE_APPEND_COLS;
		}
		
		fuSetShowTime(tr, 0); // show time column default is none
		
		fuSetXFunctionName(tr, "impWav");
		
		return 0;
	}
	///---END MORE_OPTIONS_FOR_IMPORT
	if(strTagName == "tr")
	{
		/// AW 09/25/06 MORE_WORK_ON_IMPWAV
		/*
		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)
		*/
		/// END MORE_WORK_ON_IMPWAV
		
		return 0;
	}
	/// Hong 9/29/06 ADD_FILE_INFO_NODE
	if(strTagName == "trFiles")
	{
		return 0;
	}
	/// END ADD_FILE_INFO_NODE
	
	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 impWav_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	if(nGetNDialog > 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
		///---Sim 02-01-2007 MORE_OPTIONS_FOR_IMPORT
		//trGetN.trFilter.Show = 0;
		//trGetN.timecol.Show = 0; // Hong 11/20/06 NO_NEED_SHOW_TIME_COLUMN
		//trGetN.impmode.Show = 0;
		//trGetN.sparklines.Show = 0;
		///---END MORE_OPTIONS_FOR_IMPORT
	}
	/// AW 09/25/06 MORE_WORK_ON_IMPWAV
	//-------------- CPY 9/25/06 FILTER_TO_UPDATE_GUI_AND_LT_INVOKE
	//else if ( 0 == nGetNDialog )
	if(nGetNDialog >= 0)
	//--------------
	{
		if ( LTXF_FROM_GUI_MENU == dwCntrl )
			trGetN.fname.strVal = "";
		///---Sim 02-01-2007 MORE_OPTIONS_FOR_IMPORT
		//TreeNode trFilter = trGetN.trFilter;
		//if ( trFilter.IsValid() && trFilter.GetNodeCount()>0 )
		//{
			///// Hong 01/25/07 REMOVE_AUTO_IMPORT_MODE
			///*
			/////---Sim 09-28-2006 FIX_IMPORT_MODE_SHOWING_ORDER
			////trGetN.FileImpotMode.nVal = fuGetImportMode(trFilter);
			/////---Sim 09-30-2006 REMOVE_APPEND_ROWS_IMPORT_MODE_FROM_GUI
			////trGetN.impmode.nVal = _GetListIndexFromImportMode(fuGetImportMode(trFilter));
			//int nImportMode = fuGetImportMode(trFilter);
			//// alter import mode when it is append rows, default to auto
			//if ( ASCIMP_MODE_APPEND_ROWS == nImportMode )
				//nImportMode = ASCIMP_MODE_AUTO;
			//trGetN.impmode.nVal = _GetListIndexFromImportMode(nImportMode);
			/////---Sim 09-30-2006 REMOVE_APPEND_ROWS_IMPORT_MODE_FROM_GUI
			/////---END FIX_IMPORT_MODE_SHOWING_ORDER
			//*/
			//int nImportMode = fuGetImportMode(trFilter);
			//// alter import mode pclamp not support, default to new sheet
			//nImportMode = _GetListIndexFromImportMode(nImportMode);
			//// trim and convert to GUI mode
			//if( nImportMode < 1 || nImportMode > 3 )
				//nImportMode = 2;
			//else
				//nImportMode -= 1;
			//trGetN.impmode.nVal = nImportMode;
			////if()
			///// end REMOVE_AUTO_IMPORT_MODE
			//trGetN.sparklines.nVal = fuGetSparkLine(trFilter);
			//trGetN.timecol.nVal = fuGetShowTime(trFilter);
		//}
		///---END MORE_OPTIONS_FOR_IMPORT
	}
	/// END MORE_WORK_ON_IMPWAV
	/// 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))
		{
			/// EJP 2007-05-30 v8.0631 QA70-9796 OC_ACCESS_TO_FDLOG
			///int nFilesRet = get_open_box_by_file_group(strFileName, "Sound", "Open wav file", true);			
			int nShowOptionsDlg = 1;
			/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
			//int nFilesRet = get_open_box_by_file_group(strFileName, "Sound", "Open wav file",
			//	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
			//int nFilesRet = get_open_box_by_file_group(strFileName, "Sound", NULL,
				//OPENBOX_FLAG_MULTIFILES, NULL, NULL, STR_SHOW_OPT_DLG, &nShowOptionsDlg);
			int nFilesRet = get_open_box_by_file_group(strFileName, "Sound", 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
			if( nGetNDialog )
				nRet = (nShowOptionsDlg ? XFEVT_PROCEED : XFEVT_PROCEED_NO_DLG);
			else
				nRet = (nShowOptionsDlg ? XFEVT_PROCEED_WITH_DLG : XFEVT_PROCEED);
			/// end OC_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 impWav_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
