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

////////////////////////////////////////////////////////////////////
/// Modification Log:
////////////////////////////////////////////////////////////////////

#include <GetNBox.h>
#include <image_utils.h>


void insertImg2g(const string& fname, const Page& ipg, const int& x, const int& y)
{
	GraphPageBase		igpb;
	igpb = (GraphPageBase)ipg;
	
	Layer lyTarget;
	if ( igpb )
	{
		lyTarget = igpb.Layers();
	}
	else
		XF_THROW(CER_INVALID_PAGE);
	
	StringArray vsFileNames;
	
	separate_multiline_files(fname, vsFileNames);
	
	int nFileCount = vsFileNames.GetSize();
	
	if ( nFileCount <= 0 )
		XF_THROW(CER_INVALID_FILENAME);
	
	Page pgTarget;
	lyTarget.GetParent(pgTarget);
	int nType = pgTarget.GetType();
	
	switch ( nType )
	{
	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], x, y);
			}	
			break;
		}
	default:
		XF_THROW(IMPERR_INVALID_DATASHEET);
		break;
	}
}

//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 insertImg2g_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if(nGetNDialog > 0)
	{
		trGetN.ipg.Show = 0;
	
		GraphPage gp;
		LayoutPage lyop;
		if(okxf_resolve_string_get_origin_object(trGetN.ipg.strVal, &gp)
			|| okxf_resolve_string_get_origin_object(trGetN.ipg.strVal, &lyop) )
		{
			if ( gp || lyop )
			{
				trGetN.ipg.Show = 1;
				trGetN.orng.Show = 0;
				trGetN.options.Show = 0;
				nRet = XFEVT_PROCEED_NO_DLG;
			}
		}
	}
	if(nGetNDialog >= 0)
	{
		if ( LTXF_FROM_GUI_MENU & dwCntrl )
			trGetN.fname.strVal = "";
	}
	if(nGetNDialog > -1)
	{
		string strFileName = trGetN.fname.strVal;
	
		if ( LTXF_FROM_GUI_MENU & dwCntrl )
			strFileName = "";
		
		if(is_empty_or_unassigned(strFileName))
		{
			///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);
			int nFilesRet = get_open_box_by_file_group(strFileName, "ImageImp", "Images", true);
			///end IMAGE_GROUP_FOR_IMPORT_IMAGE_FILE_DLG
			if ( 0 == nFilesRet )
			{
				nRet = XFEVT_ABORT;
				return;
			}
			trGetN.fname.strVal = strFileName;
		}
	}
}
