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

#include <image_utils.h>
#include <okocUtils.h>		///Folger 04/17/07 REPLACE_BY_OKOC_OUT_MSG

void img2GIF(const MatrixLayer& ims, const string& fname)
{
	if( true == is_sheet_empty(ims))
		XF_THROW(CER_NO_DATA);
	
	foreach(MatrixObject mo in ims.MatrixObjects)
	{
		
		Image imgi(mo);
		if(!imgi.IsValid())
			XF_THROW(XFERR_INVALID_IMAGE);
	}
	
	int nRet = save_ml_to_gif(ims, fname);
	if( nRet!=0 && nRet < MAX_ERROR_CODE_FROM_LT)
		XF_THROW_EX(XFERR_LEADTOOLS_FUNCTION_UNKNOWN_ERR, nRet);	
	string strMessage;
	strMessage.Format("Convert Image %s to GIF %s", ims.GetName(), fname);
	///Folger 04/17/07 REPLACE_BY_OKOC_OUT_MSG
	//xf_print_msg(strMessage);
	okoc_out_msg(strMessage, 'I');
	///End REPLACE_BY_OKOC_OUT_MSG
}

//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 img2GIF_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return img2GIF_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	return false;

}

int img2GIF_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return false;
}
//nGetNDialog = 0,1,2, corresponding to the XF GetN dropdown, 0 = dialog is not used
//you can change nRet to be one of XFEVT_PROCEED (defualt), XFEVT_ABORT, XFEVT_PROCEED_NO_DLG or XFEVT_PROCEED_WITH_DLG(when nGetNDialog = 0)
//nGetNDialog = -1 when Ok on GetN dialog to allow handler to preocess <auto> etc variables
//dwCntrl is combination of bits
//LTXF_FROM_GUI_MENU, LTXF_FROM_GUI_PROMPT, LTXF_FROM_AUTO_UPDATE, LTXF_FROM_FUNCTION, LTXF_THEME_USED, LTXF_CHANGE_PARAM
void img2GIF_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
		string strFile = trGetN.fname.strVal;
		string strExt = ".gif";
			
		//-- if empty or no path
		//-- PATH
		BOOL bOpenSaveAsDlg=FALSE;
	
		string strFileNameWithExt;
		string strBookSheetname = trGetN.ims.strVal;
		if(strBookSheetname[0]=='[')
		{
			int nBookEnd = strBookSheetname.Find(']');
			if(nBookEnd>1)
				strFileNameWithExt = strBookSheetname.Mid(nBookEnd + 1);
		}
		else
			strFileNameWithExt = strBookSheetname;	
	
		string strPath = GetAppPath();
		if( !strFile.IsEmpty() )
		{
			//-- check extension
	 		strFileNameWithExt = GetFileName(strFile);
	 		string strExtensionPlusDot = strFileNameWithExt.Mid( strFileNameWithExt.ReverseFind( '.' ) );
	 		
	 		//--check path
	 		strPath = GetFilePath(strFile);
	 		if( !strPath.IsPath() )
	 		{
	 			strPath = GetAppPath();
	 			bOpenSaveAsDlg = TRUE;
	 		}
	 		
	 		//-- combine path and filename
	 		strFile = strPath + strFileNameWithExt;	
		}
		else
			bOpenSaveAsDlg = TRUE;
			
		
		if(bOpenSaveAsDlg)
		{
			string strFileSaveAs = GetSaveAsBox( "*.gif GIF", strPath, strFileNameWithExt, "Multiple Images Save As GIF" );
			if(strFileSaveAs.IsEmpty())
			{
				nRet = XFEVT_ABORT;
				return;
			}
			else
				strFile = strFileSaveAs;
				
			nRet = XFEVT_PROCEED_NO_DLG;
		}
		
		trGetN.fname.strVal = strFile;
}
//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 img2GIF_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
