#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
/////////////////////////////////////////////////////////////////
//Modification Log
//--- CPY 4/4/08 ADD_CHKBOX_TO_CONTROL_CELL_RESIZE
/////////////////////////////////////////////////////////////////

#include <image_utils.h>
#include <event_utils.h>

///Jake 06/25/07 ADD_ERROR_CHECKING
static bool _check_error(const string& strFileNames) 
{
	strFileNames.TrimRight();	///Sophy 6/8/2010 REMOVE_SUFFIX_NEWLINE_CHARS_FROM_IMAGE_DLG
	vector<string> vsPages;
	if(strFileNames.Find("|")>0)
		strFileNames.GetTokens(vsPages, '|');
	else
		strFileNames.GetTokens(vsPages, STR_PAGE_LIST_SEPARATOR);//'/n'
	int nSize = vsPages.GetSize();
	for(int ii = 0; ii < nSize; ii++)
	{
		string strFileName = vsPages[ii];
		if(!strFileName.IsFile())
			break;
	}
	return (nSize && nSize == ii);
}
///end Add_ERROR_CHECKING

static bool _insert_lead_bmp(Worksheet& wks, int nr, int nc, LPCSTR lpcszFile, bool bResizeCell)
{
	FILEINFO fi;
	int nErr;
	pBITMAPHANDLE pLbmp = image_import(lpcszFile, &fi, &nErr);
	if(NULL == pLbmp)
	{
		printf("error loading %s, err = %d, abort!\n", lpcszFile, nErr);
		return false;
	}
	Image img; img.SetLBmp(pLbmp, false);
	/*
	//---- to show putting info tree
			Tree trInfo;
			trInfo = fi;
			printf("%d:%s\t%d x %d\n", ii+1, trInfo.Name.strVal, trInfo.Width.nVal, trInfo.Height.nVal);
			//----
	*/
	/// TD 10-10-2007 QA80-10510 UNDO_SUPPORT_FOR_ATTACHMENTS_IN_WKS_CELL
	//return wks.SetCell(nr, nc, img);
	//--- CPY 4/4/08 ADD_CHKBOX_TO_CONTROL_CELL_RESIZE
	//return wks.SetCell(nr, nc, img, EMBEDGRAPH_DO_UNDO);
	return wks.SetCell(nr, nc, img, EMBEDGRAPH_DO_UNDO | (bResizeCell?0:EMBEDGRAPH_NO_RESIZE_CELL));
	//---
	/// end UNDO_SUPPORT_FOR_ATTACHMENTS_IN_WKS_CELL
}
static bool _is_metafile(LPCSTR lpcszFilename)
{
	string strExt;
	separate_file_name_ext(lpcszFilename, NULL, &strExt);
	strExt.MakeLower();
	if(strExt == "emf" || strExt == "wmf")
		return true;
	
	return false;
}

//put your own support static functions here
static void _load_images(Worksheet& wks, int nR1, int nC1, vector<string>& vsFiles, bool bResizeCells)
{
	for(int ii = 0, nr = nR1; ii < vsFiles.GetSize(); nr++)
	{
		for(int nc = nC1; nc < wks.GetNumCols(); nc++)
		{
			if(_is_metafile(vsFiles[ii]))
				wks.AttachPicture(nr, nc, vsFiles[ii]);
			else
				_insert_lead_bmp(wks, nr, nc, vsFiles[ii], bResizeCells);
			
			ii++;
			if(ii >= vsFiles.GetSize())
				break;
		}
		nc = nC1;
	}
	// code to resize all the cells loaded, assuming that we start from (0,0)
	//----- CPY 4/4/08 ADD_CHKBOX_TO_CONTROL_CELL_RESIZE
	if(!bResizeCells)
		return;
	//-----
	vector<int> vnCols;
	vector<int> vnRows;
	vnRows.Data(nR1, nr);
	vnCols.Data(nC1, wks.GetNumCols()-1);
	wks.SetSelectedRange(vnRows, vnCols);
	wks.AutoSize(AS_SELECTION);
	wks.SetSelectedRange(NULL);
}


void insertImg(const string& fname, const int& resizecell, Range& orng)
{
	///Jake 06/25/07 ADD_ERROR_CHECKING
	int nErr = check_1_cell(orng);
	if(nErr != CER_NO_ERROR)
	///---Sim 12-14-2007 FIX_CHECK_RANGE_ERR_MSG
		//XF_THROW(nErr);
	{
		if(CER_NO_DATA == nErr)
			XF_THROW(CER_INVALID_RANGE);
		XF_THROW(nErr);
	}
	///---END FIX_CHECK_RANGE_ERR_MSG
	if(!_check_error(fname))
		XF_THROW(CER_INVALID_FILENAME);
	///end ADD_ERROR_CHECKING
	
	Worksheet wks;
	int r1,r2,c1,c2;
	orng.GetRange(0, r1, c1, r2, c2, wks);
	if(!wks)
		XF_THROW(XFERR_INVALID_ARG); 
	vector<string> vsFiles;
	str_separate(fname, "\n", vsFiles);
	if(vsFiles.GetSize() < 1)
		return;
	
	_load_images(wks, r1, c1, vsFiles, resizecell);	
	
}

//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 insertImg_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return insertImg_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	int nErr = check_1_cell(trGetN.orng);
	if(nErr != CER_NO_ERROR)
	{
		bOKEnable = false;
		///---Sim 12-14-2007 FIX_CHECK_RANGE_ERR_MSG
		if(CER_NO_DATA == nErr)
			strErrMsg = CER_INVALID_RANGE;
		else
		///---END FIX_CHECK_RANGE_ERR_MSG
			strErrMsg = nErr;
		return false;
	}
	///Jake 06/25/07 ADD_ERROR_CHECKING
	if(!_check_error(trGetN.fname.strVal))
	{
		bOKEnable = false;
		strErrMsg = CER_INVALID_FILENAME
	}
	///end ADD_ERROR_CHECKING
	return true;

}

int insertImg_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 insertImg_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

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