#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	insert_pic,
	insert_embed,
};
//put additional include files here
#include <..\originlab\PowerPointExport.h>

///////////// Modification log //////////////////
// CPY 7/22/09 INSERT_AS_LINK_REMOVED_FOR_LATER
// Sim 07-27-2009 SHOW_WARN_WHEN_EXPORT_LAYOUT_AS_OLE_OBJ
/// Iris 10/21/2010 ORG-1303 CLEANUP_XFS_ERROR_CODE


///---Sim 07-27-2009 SHOW_WARN_WHEN_EXPORT_LAYOUT_AS_OLE_OBJ
static bool _is_support_ole_object(LPCSTR lpcszPageName, PPSlideTransitionInfo& stuExportInfo)
{
	Page pg(lpcszPageName);
	if ( !pg )
		return false;
	
	int nType = pg.GetType();
	if ( EXIST_LAYOUT == nType && DATA_TYPE_PICTURE != stuExportInfo.nDataType )
	{
		XF_WARN_EX(XFWARN_LAYOUT_NOT_SUPPORT_OLE, lpcszPageName);
		return false;
	}		
	
	return true;
}
///---END SHOW_WARN_WHEN_EXPORT_LAYOUT_AS_OLE_OBJ


void pemp_pptslide(const int& insert, const double& top, const double& left, const double& bottom, const double& right, const int& title)
{
	vector<string> vsPageSNs;
	if(get_sel_page_names(vsPageSNs, EXIST_GRAPH) < 1)
	{
		/// Iris 10/21/2010 ORG-1303 CLEANUP_XFS_ERROR_CODE
		//return;// nothing to do, no graphs at all
		XF_THROW(XF_NO_GRAPH);
		///End CLEANUP_XFS_ERROR_CODE
	}
	
	PowerPointExport ppt;
	
	PPSlideTransitionInfo stuExportInfo;
	stuExportInfo.nSlideTransition=ppTransitionSpeedFast;
	stuExportInfo.bOnClick = true;
	///---Sim 07-22-2009 QA81-4184 MORE_CHANGE_EXPORT_PPT
	//stuExportInfo.bOnTime = timer;
	//stuExportInfo.nSlideStayTime = period;
	stuExportInfo.bOnTime = false;
	///---END QA81-4184 MORE_CHANGE_EXPORT_PPT
	stuExportInfo.nEntryEffectpp = ppEffectBlindsHorizontal;
	//stuExportInfo.nGradientType = 0;
	//stuExportInfo.strSlideTitle = "Slide Title";
	stuExportInfo.nPosSlideTitle = title;
	///---Sim 07-21-2009 QA81-4184 SUPPORT_POSIT_GRAPH_ON_SLIDE
	///---Sim 07-22-2009 QA81-4184 MORE_CHANGE_EXPORT_PPT
	//stuExportInfo.dTop = top;
	//stuExportInfo.dLeft = left;
	//stuExportInfo.dRatio = scale;
	stuExportInfo.dMarginTop = top;
	stuExportInfo.dMarginLeft = left;
	stuExportInfo.dMarginBottom = bottom;
	stuExportInfo.dMarginRight = right;
	
	stuExportInfo.nDataType = insert;
	///---END QA81-4184 MORE_CHANGE_EXPORT_PPT
	///---END QA81-4184 SUPPORT_POSIT_GRAPH_ON_SLIDE
	
	//Open PowerPoint Application
	bool bRet = ppt.OpenPowerPoint();
	if(!bRet)
	{
		// to do, need better error message
		/// Iris 10/21/2010 ORG-1303 CLEANUP_XFS_ERROR_CODE
		//error_report(_L("can't get the template"));
		//XF_THROW(CER_EXPORT_IMAGE_FAIL);
		XF_THROW(XF_FAIL_TO_CONNECT_PPT);
		///End CLEANUP_XFS_ERROR_CODE
	}
	///---Sim 07-22-2009 QA81-4184 MORE_CHANGE_EXPORT_PPT
	//if ( !tname.IsEmpty() )
	//{
		//bRet = ppt.ApplyTemplate(tname);
		//if(!bRet)
		//{
			//// to do, need better error message
			//error_report("apply template error");
			////XF_WARN();
		//}
	//}
	///---END QA81-4184 MORE_CHANGE_EXPORT_PPT
	
	if(!ppt.IsValid())
	{
		// to do, need better error message
		/// Iris 10/21/2010 ORG-1303 CLEANUP_XFS_ERROR_CODE
		//error_report(_L("the powerpoint application not exist"));
		//XF_THROW(CER_EXPORT_IMAGE_FAIL);
		XF_THROW(XF_FAIL_TO_CONNECT_PPT);
		///End CLEANUP_XFS_ERROR_CODE
	}
	
	//Add all page info into tree
	int nSize = vsPageSNs.GetSize();
	for(int i = 0 ; i< nSize; i++)
	{
		///---Sim 07-27-2009 SHOW_WARN_WHEN_EXPORT_LAYOUT_AS_OLE_OBJ
		if ( !_is_support_ole_object(vsPageSNs[i], stuExportInfo) )
			continue;
		///---END SHOW_WARN_WHEN_EXPORT_LAYOUT_AS_OLE_OBJ
		//ppt.AddPageInfo(vsPageNames[i], vnPageTypes[i], stuExportInfo);
		stuExportInfo.strSlideTitle = Project.GetFolderWithPage(vsPageSNs[i]).GetPath() + vsPageSNs[i];
		ppt.AddPageInfo(vsPageSNs[i], stuExportInfo);
	}
	
	///---Sim 07-22-2009 QA81-4184 MORE_CHANGE_EXPORT_PPT
	//int nRet = ppt.ExportPowerPointFile(fname);
	int nRet = ppt.ExportPowerPoint();
	///---END QA81-4184 MORE_CHANGE_EXPORT_PPT
	 
	//To get the path saved ppt or pps files
	
	if(nRet == -1)
	{
		// to do, need better error message
		/// Iris 10/21/2010 ORG-1303 CLEANUP_XFS_ERROR_CODE
		//XF_THROW(CER_EXPORT_IMAGE_FAIL);
		XF_THROW(XF_FAIL_TO_CONNECT_PPT);
		///End CLEANUP_XFS_ERROR_CODE
	}
	else if(nRet == -2)
	{
		// to do, need better error message
		/// Iris 10/21/2010 ORG-1303 CLEANUP_XFS_ERROR_CODE
		//error_report(_L("Can't export the page"));
		//XF_THROW(CER_EXPORT_IMAGE_FAIL);
		XF_THROW(XF_FAIL_TO_EXPORT_PAGE_TO_PPT);
		///End CLEANUP_XFS_ERROR_CODE
	}
	
	
}

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

}

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