#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	type_bmp,
	type_gif,
	type_jpg,
	type_pcx,
	type_png,
	type_psd,
	type_tga,
	type_tif,
};

/// Modification Log:
/// EJP 2006-11-21 v8.0514 IMPROVE_IMG_EXT_TYPE_UTIL_FUNC
/// Jasmine 04/13/07 PRINT_XF_MSG_TO_SCRIPT_WINDOW
/// Folger 04/16/07 REPLACE_BY_OKOC_OUT_MSG
/// Sim 05-25-2007 GET_DEFAULT_FROM_ORIGIN_OPTIONS
/// Sim 06-18-2007 ADD_SHOW_DLG_CHECK_BOX_TO_SAVE_AS_DLG
/// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
/// Iris 07/04/2007 v8.0655 CREATE_PATH_IF_SPECIFIED_PATH_IS_NOT_EXISTED
/// Iris 07/13/2007 v8.0659 SHARE_CHECK_PATH_CODES_FOR_ALL_EXPORT_XFS
/// Sim 08-22-2007 MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT
/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
/// Sim 05-07-2009 QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG


//put additional include files here
#include <image_utils.h>
#include <okocUtils.h>			/// Folger 04/16/07 REPLACE_BY_OKOC_OUT_MSG
//put your own support static functions here

//put additional include files here
#include <GetNBox.h>
#include <control.h>
#include <ocGDI.h> //------- CPY 10/6/06 EMF_PREVIEW
#include <..\Originlab\theme_utils.h>
#include <..\Originlab\fu_utils.h> ///---Sim 11-30-2006 CHECK_FILE_NAME

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

#define STR_IMAGE_FILE_GROUP "Image" ///---Sim 05-25-2007 GET_DEFAULT_FROM_ORIGIN_OPTIONS
#define STR_EXPORT_ASCII_FILE_GROUP "ImageExp" ///---Sim 06-18-2007 ADD_SHOW_DLG_CHECK_BOX_TO_SAVE_AS_DLG
//#define STR_ONLY_FILE_NAME_ATTRIB  "OnlyFileName" ///---Jake 06/27/07 STORE_USER_FILE_NAME

///---Sim 08-22-2007 MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT
static 	vector<string> 	s_vsDescripsForFile; // general list of file descriptions
static 	vector<string> 	s_vsExt; // general list of file extension
///---END MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT

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

///---Sim 06-18-2007 ADD_SHOW_DLG_CHECK_BOX_TO_SAVE_AS_DLG
/*
static bool _get_image_ext_list(string& strExtList, int nCtrl)
{
// Use switch because OC does not support init const string pointer array
//	LPCSTR lpcstrList[] = {"E","EM","I","IM","R","V"};
//	if( nCtrl < 0 || IFT_COUNT <= nCtrl )
//		return -1; // error, invalid 
	string strExtTypes;
	switch( nCtrl )
	{
	case IFT_EXPORT: // graph, layout
		strExtTypes = "E";
		break;
	case IFT_EXPORT_MATRIX:
		strExtTypes = "EM";
		break;
	case IFT_IMPORT: // graph, layout
		strExtTypes = "I";
		break;
	case IFT_IMPORT_MATRIX:
		strExtTypes = "IM";
		break;
	case IFT_RASTER: // all supported raster formats
		strExtTypes = "R";
		break;
	case IFT_VECTOR: // all supported vector formats
		strExtTypes = "V";
		break;
	default:
		return -1;
	}
	string strScript;
//	strScript.Format("Image.GetExtList(z,%s);", lpcstrList[nCtrl]);
	strScript.Format("Image.GetExtList(z,%s);", strExtTypes);
	if( !LT_execute(strScript) )
		return false;
	char szExtList[MAXFULLPATH];
	if( !LT_get_str("%z", szExtList, MAXFULLPATH) )
		return false;
	
	strExtList = szExtList;
	
	return true;
}
*/

int _find_type_index_by_ext(string strExt)
{
	///---Sim 08-22-2007 MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT
	/*
	StringArray saExtList;
	/#
	string strExtList;
	//int nRet = get_image_file_types(saExtList, IFT_RASTER);
	//if ( nRet < 0 )
	if ( !_get_image_ext_list(strExtList, IFT_RASTER) )
	{
		ASSERT(false);
		return 0;
	}
	
	strExtList.GetTokens(saExtList, ' ');
	#/
	
	int nRet = get_image_file_type_extensions(saExtList, IFT_RASTER);
	if ( nRet < 0 )
	{
		ASSERT(false);
		return 0;
	}
	
	int nIndex = saExtList.Find(strExt);
	*/
	int nIndex = s_vsExt.Find(strExt);
	///---END MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT
	if ( nIndex >= 0 )
		return nIndex;
	
	return 0;
}
///---END ADD_SHOW_DLG_CHECK_BOX_TO_SAVE_AS_DLG

///---Sim 08-22-2007 MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT
static bool _get_image_file_type_descrip_and_ext(string& strType, string& strExt, int nImgType)
{
	if( nImgType < 0 || s_vsDescripsForFile.GetSize() <= nImgType )
		return false;

	strType = s_vsDescripsForFile[nImgType];
	strExt = s_vsExt[nImgType];
	
	return true;
}
///---END MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT

///---Sim 01-03-2007 FIX_GRAY_SCALE_FAIL_TO_WORK
static bool _gui_is_gray_sacle(TreeNode& trImageOptions, LPCSTR lpcszExt)
{
	string 	strExt(lpcszExt);
	strExt.MakeUpper();
	TreeNode trType = trImageOptions.GetNode(strExt);
	if(trType)
	{		
		TreeNode	trBits = trType.BitsPerPixel;
		if(trBits)
		{	
			int 	nGray = 0;
			if(8 == trBits.nVal && trBits.GetAttribute(STR_ATTRIB_GRAY_SCALE, nGray) && 1 == nGray)
				return true;
		}
	}
	return false;
}

static int _gui_get_DPI(const TreeNode& trImageOptions, LPCSTR lpcszExt)
{
	if( is_image_file_type_ext(lpcszExt, IFT_RASTER) )
	{	
		TreeNode	trImageDPI;	
		TreeNode 	trN = trImageOptions.GetNode(lpcszExt, false);
		if(trN)
		{
			trImageDPI = trN.DotsPerInch; //the name in all raster formats
			if(!trImageDPI)
				trImageDPI = trN.PostScript.DestRastOperRes; //the name of EPS
		}
		if(trImageDPI) //for some image ext, trImageDPI may be invalid, so check before use
			return trImageDPI.nVal;	
	}		
		
	return 96;//the default DPI
}
///---END FIX_GRAY_SCALE_FAIL_TO_WORK

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

///---Sim 11-21-2006 TYPE_EVENT_FUNCTION
static bool construct_type_list(TreeNode& trType)
{
	string 	strTypes;
	vector<string> vsTypes;
	///---Sim 08-22-2007 MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT
	/*
	/// EJP 2006-11-21 v8.0515 IMPROVE_IMG_EXT_TYPE_UTIL_FUNC
	///export_get_type_list(-1, &vsTypes, false);
	if( get_image_file_type_descrips(vsTypes, IFT_RASTER) < 0 )
		return false;
	/// end IMPROVE_IMG_EXT_TYPE_UTIL_FUNC
	*/
	vsTypes = s_vsDescripsForFile;
	///---END MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT
	strTypes.SetTokens(vsTypes, '|');
	trType.SetAttribute(STR_COMBO_ATTRIB, strTypes);
	
	return true;
}

static void type_event(TreeNode& trGetN)
{
	/// EJP 2006-11-21 v8.0515 IMPROVE_IMG_EXT_TYPE_UTIL_FUNC
	///	string strType = export_get_type_list(trGetN.type.nVal, NULL, false); ///---Sim 11-21-2006 NOT_USE_VECTOR_IMAGE_TYPE
	///	string strExt = export_get_ext_from_file_type(strType, true);
	
	string strType, strExt;
	///---Sim 08-22-2007 MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT
	//get_image_file_type_descrip_and_ext(strType, strExt, trGetN.type.nVal, IFT_RASTER);
	_get_image_file_type_descrip_and_ext(strType, strExt, trGetN.type.nVal);
	///---END MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT
	/// end IMPROVE_IMG_EXT_TYPE_UTIL_FUNC

	// update file extension group for save file dialog
	string str;
	str.Format("...Save:[%s] *.%s", strType, strExt);
	trGetN.fname.SetAttribute(STR_COMBO_ATTRIB, str);	
	trGetN.fname.SetAttribute(STR_DLG_OPTIONS_ATTRIB, FDLG_SUPPRESS_WARNING_SAVE_TO_EXISTING);	
	
	///Jake 06/28/07 CODES_HAVE_BEEN_MOVED
	/*
	//----Jake 06/27/07 GET_COMBO_LIST_FROM_REGISTRY verytime when file type change 
	char szLastXFName[MAXFULLPATH];
	LT_get_str(LTVAR_LAST_XF, szLastXFName, MAXFULLPATH); //Jake 06/27/07 GET_THE_NAME_OF_LAST_XF_CALLED
	TreeNode trPath = trGetN.fname;
	tree_set_browse_combo_attrib(trPath, szLastXFName, false);
	//----end GET_COMBO_LIST_FROM_REGISTRY
	*/
	tree_set_browse_combo_attrib(trGetN.fname);
	///end CODES_HAVE_BEEN_MOVED
	
	export_gui_show_options_branch(trGetN.tr, strExt);//show corresponding one while hide others
}
///---END TYPE_EVENT_FUNCTION

///---Sim 11-22-2006 CHECK_FILE_NAME
static void check_file_name(TreeNode& trGetN)
{
	string strType, strExt;
	///---Sim 08-22-2007 MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT
	//get_image_file_type_descrip_and_ext(strType, strExt, trGetN.type.nVal, IFT_RASTER);
	_get_image_file_type_descrip_and_ext(strType, strExt, trGetN.type.nVal);
	///---END MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT
		
	string strFileName = trGetN.fname.strVal;
	strFileName = cvt_predefined_path_to_real_path(strFileName); /// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	/// Iris 07/04/2007 v8.0655 CREATE_PATH_IF_SPECIFIED_PATH_IS_NOT_EXISTED
	//if ( strFileName.IsEmpty() || !GetFilePath(strFileName).IsPath() ) // invalid file path
	if ( strFileName.IsEmpty() )
	///end CREATE_PATH_IF_SPECIFIED_PATH_IS_NOT_EXISTED	
	{
		// get file name as matrix object name
		MatrixObject mo;
		//MatrixLayer ml;
		okxf_resolve_string_get_origin_object(trGetN.im.strVal, &mo);
		//mo.GetParent(ml);
		//
		//strFileName = "untitled";
		//if ( ml.IsValid() && mo.IsValid() )
			//strFileName = ml.GetName() + "_" + mo.GetName();
		_get_corresponding_file_name(strFileName, mo);
		
		// append extension and path
		strFileName += "." + strExt;
		///---Sim 05-25-2007 GET_DEFAULT_FROM_ORIGIN_OPTIONS
		//trGetN.fname.strVal = GetAppPath() + strFileName;
		trGetN.fname.strVal = _get_default_path() + strFileName;
		///---Sim 05-25-2007 GET_DEFAULT_FROM_ORIGIN_OPTIONS
	}		
	else // check extension
	{
		char 	szFileName[MAXFULLPATH];
		///---Sim 05-07-2009 QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG
#ifndef CHECK_EXCEED_FILE_PATH_LENGTH_LIMITATION
		lstrcpy(szFileName, trGetN.fname.strVal);
#else
		lstrcpyn(szFileName, trGetN.fname.strVal, MAXFULLPATH);
#endif
		///---END QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG
		check_add_file_ext(szFileName, strExt);	
		trGetN.fname.strVal = szFileName;
	}
	///---Sim 05-07-2009 QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG
#ifndef CHECK_EXCEED_FILE_PATH_LENGTH_LIMITATION
	string str = trGetN.fname.strVal;
	if ( check_trim_file_path_if_too_long(str) )
		trGetN.fname.strVal = str;
#endif
	///---END QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG
}
///---END CHECK_FILE_NAME

///---Sim 11-30-2006 CHECK_MATRIX_OBJECT
static bool check_matrix_object(const string& strmo, string& strErrMsg)
{
	MatrixObject mo;
	okxf_resolve_string_get_origin_object(strmo, &mo);
	if ( !mo.IsValid() )
	{
	strErrMsg = CER_INVALID_MATOBJ;
	return false;
	}
	return true;
}
///---END CHECK_MATRIX_OBJECT

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

void expImage(const MatrixObject& im, const int& type, const string& fname, const TreeNode& tr)
{
	
	///---Sim 11-30-2006 CHECK_MATRIX_OBJECT
	if ( !im.IsValid() )
	{
		XF_THROW(CER_INVALID_MATOBJ);
	}
	///---END CHECK_MATRIX_OBJECT
	
	///---Sim 06-18-2007 ADD_SHOW_DLG_CHECK_BOX_TO_SAVE_AS_DLG
	/*
	if( !export_check_path_show_warnning(fname) )
	{
		XF_THROW(CER_FILE_ALREADY_EXISTS);
	}
	*/
	///---END ADD_SHOW_DLG_CHECK_BOX_TO_SAVE_AS_DLG
	
	/// EJP 2006-11-21 v8.0515 IMPROVE_IMG_EXT_TYPE_UTIL_FUNC
	///	string strType = export_get_type_list(type, NULL, false); ///---Sim 11-21-2006 NOT_USE_VECTOR_IMAGE_TYPE
	///	string strExt = export_get_ext_from_file_type(strType);
	string strType, strExt;
	///---Sim 08-22-2007 MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT
	//get_image_file_type_descrip_and_ext(strType, strExt, type, IFT_RASTER); ///---Sim 11-21-2006 NOT_USE_VECTOR_IMAGE_TYPE
	_get_image_file_type_descrip_and_ext(strType, strExt, type);
	///---END MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT
	/// end IMPROVE_IMG_EXT_TYPE_UTIL_FUNC
	
	char 	szPathExt[10];
	char 	szFileName[MAXFULLPATH];
	///---Sim 05-07-2009 QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG
	#ifndef CHECK_EXCEED_FILE_PATH_LENGTH_LIMITATION
	lstrcpy(szFileName, fname);
	#else
	lstrcpyn(szFileName, fname, MAXFULLPATH);
	#endif
	///---END QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG
	check_add_file_ext(szFileName, strExt, szPathExt);	
	if( 0 != lstrlen(szPathExt) && 0 != lstrcmpi(strExt, szPathExt) )
	{
		XF_WARN(CER_TYPE_EXTENSION_IS_NOT_CONSISTENT);
	}
	
	string strPath = cvt_predefined_path_to_real_path(szFileName); /// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	/// Iris 07/04/2007 v8.0655 CREATE_PATH_IF_SPECIFIED_PATH_IS_NOT_EXISTED
	if( !GetFilePath(strPath).IsPath() )
	{
		if( !CreateDirectory(GetFilePath(strPath), NULL) )
		{
			XF_THROW(CER_FAIL_TO_CREATE_PATH);
		}
	}
	///end CREATE_PATH_IF_SPECIFIED_PATH_IS_NOT_EXISTED		
	
	///---Sim 11-20-2006 SAVE_FILE_SETTING
	TreeNode trExpTempOptions = tr.GetNode(strExt, false);
		
	/// EJP 2006-10-04 v8.0493 USE_CORRECT_KEY_NAMES_USED_BY_OIMG
	if( 0 == strExt.CompareNoCase("TIF") )
	{
		convert_node_value_from_str_to_index(trExpTempOptions.ColorSpace, "Combo");
		convert_node_value_from_str_to_index(trExpTempOptions.Compression, "Combo");
	}
	/// end USE_CORRECT_KEY_NAMES_USED_BY_OIMG
	
	/// Iris 12/04/06 FIX_GRAY_SCALE_FAIL_TO_WORK
	if( trExpTempOptions )
	{
		convert_color_depth_from_str_to_num(trExpTempOptions.BitsPerPixel);
	}
	///end FIX_GRAY_SCALE_FAIL_TO_WORK
	
	// write export settings to ini before export page
	tree_write_image_export_settings(trExpTempOptions, strExt);
	///---END SAVE_FILE_SETTING
	
	///---Sim 01-03-2007 FIX_GRAY_SCALE_FAIL_TO_WORK
	bool bGray = _gui_is_gray_sacle(tr, strExt); 
	int nDPI = _gui_get_DPI(tr, strExt);
	///---END FIX_GRAY_SCALE_FAIL_TO_WORK
	
	/// EJP 2006-11-20 v8.0514 FIX_EXPORT_MATRIX_RET_VAL
	///	//bool 	bRet = _export_image(pgTemp, path, strExt);	
	///	bool 	bRet = export_Matrix_to_image(fname, strExt,  im);//fname,strType, im);
	///	if(!bRet)
	///		XF_THROW(CER_EXPORT_IMAGE_FAIL);//CER_EXPORT_IMAGE_FAIL);
	///---Sim 01-03-2007 FIX_GRAY_SCALE_FAIL_TO_WORK
	//int nErr = export_Matrix_to_image(szFileName, strExt,  im);
	
	/// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	//int nErr = export_Matrix_to_image(szFileName, strExt,  im, nDPI, bGray);
	int nErr = export_Matrix_to_image(strPath, strExt,  im, nDPI, bGray);
	///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	
	///---END FIX_GRAY_SCALE_FAIL_TO_WORK
	
	/// EJP 2006-10-04 v8.0493 USE_CORRECT_KEY_NAMES_USED_BY_OIMG
	if( 0 == strExt.CompareNoCase("TIF") )
	{
		convert_node_value_from_index_to_str(trExpTempOptions.ColorSpace, "Combo");
		convert_node_value_from_index_to_str(trExpTempOptions.Compression, "Combo");
	}
	/// end USE_CORRECT_KEY_NAMES_USED_BY_OIMG
	
	/// Iris 12/04/06 FIX_GRAY_SCALE_FAIL_TO_WORK
	if( trExpTempOptions )
	{
		convert_color_depth_from_num_to_str(trExpTempOptions.BitsPerPixel);
	}
	///end FIX_GRAY_SCALE_FAIL_TO_WORK
	
	///Folger 06/05/07 ADD_FAILURE_MSG
	//if( nErr )
		//XF_THROW_EX(XFERR_EXP_MAT_TO_IMG, nErr);
	/// end FIX_EXPORT_MATRIX_RET_VAL
	///End ADD_FAILURE_MSG
	
	///Jasmine 04/13/07 PRINT_XF_MSG_TO_SCRIPT_WINDOW
	string strMessage, strRange;
	im.GetRangeString(strRange);
	///Folger 05/24/07 MODIFY_OUTPUT_INFO_MSG
	//strMessage.Format("Export matrix %s to image file %s", strRange, szFileName);
	///Folger 06/05/07 ADD_FAILURE_MSG
	if (nErr == 0)
		ocu_load_msg_str(XF_EXPORT_SUCCESS, &strMessage, strRange, NULL, strPath);
	else
		ocu_load_msg_str(XF_EXPORT_FAILED, &strMessage, strRange, NULL, strPath);
	///End ADD_FAILURE_MSG
	///End MODIFY_OUTPUT_INFO_MSG
	
	///Jake 06/28/07 FUNCTION_SUPPORT_DEFAULT_DEY_NAME
	/*
	//----Jake 06/27/07 GET_COMBO_LIST_FROM_REGISTRY
	char szLastXFName[MAXFULLPATH];
	LT_get_str(LTVAR_LAST_XF, szLastXFName, MAXFULLPATH);
	update_recent_path_list(szLastXFName,fname);//update path to registry
	//----end GET_COMBO_LIST_FROM_REGISTRY
	*/
	update_recent_path_list(strPath);
	///Jake 06/28/07 FUNCTION_SUPPORT_DEFAULT_DEY_NAME
	
	///Folger 04/16/07 REPLACE_BY_OKOC_OUT_MSG
	//xf_print_msg(strMessage);
	okoc_out_msg(strMessage, 'I');
	///End REPLACE_BY_OKOC_OUT_MSG
	///End PRINT_XF_MSG_TO_SCRIPT_WINDOW
	
}

//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 expImage_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return expImage_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	///Kyle 09/24/08 SET_NO_NEED_TO_LOCALIZE
	if ( GETNE_ON_INIT == nEventID )
	{
		trGetN.type.SetAttribute(STR_OCNTRL_OPTNS, OCNTRLPARAM1_NOT_LOCALIZED);
	}
	///End SET_NO_NEED_TO_LOCALIZE
	
	OK_SURE(bOKEnable); ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
	
	string strVarName = lpcszNodeName;
	
	///---Sim 11-21-2006 MORE_WORK_ON_CONSTRUCT
	if ( GETNE_ON_INIT == nEventID || GETNE_ON_THEME == nEventID )
	{
		construct_type_list(trGetN.type);
		/// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		// should update filename and extension first, and then in type_event will update filename and extension for Special Path in funciton tree_set_browse_combo_attrib
		//type_event(trGetN);
		//check_file_name(trGetN);
		check_file_name(trGetN);
		type_event(trGetN);
		///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		
		s_bFileValid = check_exported_file_name(trGetN.fname.strVal, strErrMsg);
		OK_SURE(bOKEnable);
		s_bMatValid = check_matrix_object(trGetN.im.strVal, strErrMsg);
		OK_SURE(bOKEnable);
		return true;
	}
	
	if ( "type" == strVarName )
	{
		/// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		//type_event(trGetN);
		//check_file_name(trGetN);
		check_file_name(trGetN);
		type_event(trGetN);
		///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		return true;
	}
	else
	if ( "fname" == strVarName )
	{
		///Jake 06/28/07 CODES_HAVE_BEEN_MOVED
		/*
		//----Jake 06/27/07 GET_COMBO_LIST_FROM_REGISTRY
		string		strPath = trGetN.fname.strVal;
		int nID = okutil_cvt_str_to_predefined_type(strPath);
		if( nID != PDS_INVALID && nID != PDS_USER)
		{
			strPath = cvt_predefined_path_to_real_path(strPath);
			string strFileName;
			trGetN.fname.GetAttribute(STR_ONLY_FILE_NAME_ATTRIB,strFileName);
			strPath = GetFilePath(strPath) + strFileName;
			trGetN.fname.strVal = strPath;
		}
		_set_only_file_name_attrib(trGetN,strPath);
		*/
		///end CODES_HAVE_BEEN_MOVED
		/// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		/*
		TreeNode trPath = trGetN.fname;
		tree_check_set_real_path(trPath);
		tree_set_only_file_name_attrib(trPath,trPath.strVal);
		*/
		///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		
		//----end GET_COMBO_LIST_FROM_REGISTRY
		/// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		//s_bFileValid = check_exported_file_name(trGetN.fname.strVal, strErrMsg);
		/// Iris 07/13/2007 v8.0659 SHARE_CHECK_PATH_CODES_FOR_ALL_EXPORT_XFS
		//string strPath = cvt_predefined_path_to_real_path(trGetN.fname.strVal);
		//s_bFileValid = check_exported_file_name(strPath, strErrMsg);
		s_bFileValid = check_exported_file_name(trGetN.fname.strVal, strErrMsg);
		//end SHARE_CHECK_PATH_CODES_FOR_ALL_EXPORT_XFS
		///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		OK_SURE(bOKEnable);
		return true;
	}
	else
	if ( "im" == strVarName )
	{
		s_bMatValid = check_matrix_object(trGetN.im.strVal, strErrMsg);
		OK_SURE(bOKEnable);
		return true;
	}
	///---END MORE_WORK_ON_CONSTRUCT
	return false;	

}

int expImage_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	string strVarName = lpcszVarName;
	
	if ( "tr" == strVarName )
	{
		export_gui_construct_options_branch(tr);
		if ( tr.NoteInfo )
			tr.NoteInfo.Remove();
		///---Sim 01-24-2007 SUPPORT_DPI
		//if ( tr.GIF.DotsPerInch)
			//tr.GIF.DotsPerInch.Remove();
		//if ( tr.TGA.DotsPerInch)
			//tr.TGA.DotsPerInch.Remove();
		///---END SUPPORT_DPI
		if ( tr.XPM.DotsPerInch)
			tr.XPM.DotsPerInch.Remove();
		if ( tr.XWD.DotsPerInch)
			tr.XWD.DotsPerInch.Remove();
		return 0;
	}
	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 expImage_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	///---Sim 06-18-2007 ADD_SHOW_DLG_CHECK_BOX_TO_SAVE_AS_DLG
	/*
	bool bOpenDlg = 0 == nGetNDialog? false : true;
	//	s_bThemeUsed = LTXF_THEME_USED == (dwCntrl & LTXF_THEME_USED)? true : false;
	//	bool bFromPrompt = LTXF_FROM_GUI_PROMPT == (dwCntrl & LTXF_FROM_GUI_PROMPT)? true : false;
	
	//	s_nType = trGetN.type.nVal;
	
	string strPath = trGetN.fname.strVal;
	if(!bOpenDlg && is_empty_or_unassigned(strPath))
	{
		/// EJP 2006-11-21 v8.0515 IMPROVE_IMG_EXT_TYPE_UTIL_FUNC
		///	string strType = export_get_type_list(trGetN.type.nVal, NULL, false); ///---Sim 11-21-2006 NOT_USE_VECTOR_IMAGE_TYPE
		///	string strExt = export_get_ext_from_file_type(strType);
		string strType, strExt;
		get_image_file_type_descrip_and_ext(strType, strExt, trGetN.type.nVal);
		/// end IMPROVE_IMG_EXT_TYPE_UTIL_FUNC
		
		string 		strFileType;
		strFileType.Format("[%s] *.%s", strType, strExt);
		StringArray 	saFileTypes;
		saFileTypes.Add(strFileType);
		
		MatrixObject moi;
		if(okxf_resolve_string_get_origin_object(trGetN.im.strVal, &moi))
		{
			///---Sim 11-30-2006 FIX_BUG_FOR_OPEN_DIALOG
			//string 	strFile = moi.GetLongName();
			string strFile;
			_get_corresponding_file_name(strFile, moi);
			
			///---Sim 05-25-2007 GET_DEFAULT_FROM_ORIGIN_OPTIONS
			//if(okutil_FileDialog(&strFile, true, &saFileTypes, GetAppPath(), FDLG_SUPPRESS_WARNING_SAVE_TO_EXISTING, NULL, "Save As"))
			if(okutil_FileDialog(&strFile, true, &saFileTypes, _get_default_path(), 
							FDLG_SUPPRESS_WARNING_SAVE_TO_EXISTING, NULL, "Save As"))
			///---END GET_DEFAULT_FROM_ORIGIN_OPTIONS
				trGetN.fname.strVal = strFile;
			else
			{
				nRet = XFEVT_ABORT;
				XF_WARN(XFERR_UI_USER_CANCEL);
				return;
			}
			///---END FIX_BUG_FOR_OPEN_DIALOG
		}
	}
	*/
	
	///---Sim 08-22-2007 MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT
	if(nGetNDialog >= 0)
	{
		if( get_image_file_type_descrips_sorted_by_extension(s_vsDescripsForFile, IFT_RASTER) < 0 ) 
		{
			nRet = XFEVT_ABORT;
			return;
		}
		
		if( get_image_file_type_extensions_sorted(s_vsExt, IFT_RASTER) < 0 )
		{
			nRet = XFEVT_ABORT;
			return;
		}
	}
	///---END MAKE_IMAGE_TYPE_LIST_SORTED_BY_EXT
	
	if(nGetNDialog > -1)
	{
			
		MatrixObject im;
		okxf_resolve_string_get_origin_object(trGetN.im.strVal, &im);
		if(!im)
		{
			nRet = XFEVT_ABORT;
			warning_msg_box(CER_NO_MATRIX, true);
			return;
		}	
		
		string strFileName = trGetN.fname.strVal;
		
		if ( LTXF_FROM_GUI_MENU & dwCntrl ) // if from menu, ignore default value fname$
			strFileName = "";
		
		if( is_empty_or_unassigned(strFileName) )
		{
			string strMatName;
			_get_corresponding_file_name(strMatName, im);
			
			int nFilesRet;
			if ( 0 != nGetNDialog )
			{
				int nShowOptionsDlg = 1;
				/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				//nFilesRet = get_saveas_box_by_file_group(strFileName, STR_EXPORT_ASCII_FILE_GROUP, _L("Image File"),
				//	0, NULL, strMatName, _L("Show Options Dialog"), &nShowOptionsDlg);
				nFilesRet = get_saveas_box_by_file_group(strFileName, STR_EXPORT_ASCII_FILE_GROUP, NULL,
					0, NULL, strMatName, STR_SHOW_OPT_DLG, &nShowOptionsDlg);
				/// end CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				nRet = (nShowOptionsDlg ? XFEVT_PROCEED : XFEVT_PROCEED_NO_DLG);
			}
			else
			{
				//nFilesRet = get_saveas_box_by_file_group(strFileName, STR_EXPORT_ASCII_FILE_GROUP, _L("Image File"), 0, NULL, strMatName);
				nFilesRet = get_saveas_box_by_file_group(strFileName, STR_EXPORT_ASCII_FILE_GROUP, NULL, 0, NULL, strMatName); /// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
			}
			
			if ( 0 == nFilesRet )
			{
				nRet = XFEVT_ABORT;
				//warning_msg_box(XFERR_UI_USER_CANCEL, false); // Hong 03/14/07 NO_NEED_CANCEL_WARNING
				return;
			}
		
			trGetN.fname.strVal = strFileName;
			
			/// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
			//tree_set_only_file_name_attrib(trGetN.fname,strFileName);	//---Jake 06/27/07 STORE_USER_FILE_NAME
			///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
			
			// reset file type
			string strFile, strExt;
			separate_file_name_ext(strFileName, strFile, strExt);
			trGetN.type.nVal = _find_type_index_by_ext(strExt);
		}
	}
	///---END ADD_SHOW_DLG_CHECK_BOX_TO_SAVE_AS_DLG
	
}
//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 expImage_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
