#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here

//#include <..\OriginLab\DialogEx.h>
//#include <..\OriginLab\ColumnList.h>
//#include <..\OriginLab\FunctionsMenu.h>
//
#include <..\Originlab\ExtractWksData.h>
//#include <..\OriginLab\ListMenu.h>
//#include <..\Originlab\ExtractWksDataDlg.h>

#define		STR_EXTRACTWKSDATADLG_FILE		"Originlab\\ExtractWksDataDlg.c"

//put your own support static functions here
 /*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Folger 10/14/2010 ORG-1259-P1 EXTRACT_WKS_DATA_SHOULD_NOT_ACTIVE_OUTPUT_SHEET_WHEN_RECALCULATE
 *------------------------------------------------------------------------------*/

///------ Folger 01/22/10 QA81-12954-P3 WEXTRACT_FAILS_TO_OPEN_EXTRACTWKSDATA_DIALOG_IN_81
typedef void	(*pfnExtractDlgModeless)();
static	void	OpenExtractWksDataDlgModeless()
{
	pfnExtractDlgModeless pfnOpenDlg = Project.FindFunction("OpenExtractWksModelssDialog", STR_EXTRACTWKSDATADLG_FILE, true);
	if( pfnOpenDlg )
		pfnOpenDlg();
	else
		ASSERT(FALSE);
}
///------ End WEXTRACT_FAILS_TO_OPEN_EXTRACTWKSDATA_DIALOG_IN_81

typedef bool (*pfnExtractDlg)(TreeNode& trGetN, HWND hWndParent = NULL, PEVENT_GETN pfnGetN = NULL);

bool OpenExtractWksDataDlg(TreeNode& tr, PEVENT_FUNC pfn, LPCSTR lpcszTitle, LPCSTR lpcszDescription, HWND hWndParent)
{
	///---Sim 01-29-2010 QA81-12954-P3 OPEN_WEXTRACT_DIALOG_SHOULD_USE_DASH_D
	bool bFromAutoUpdate = ( 0 == tr.AutoUpdate.Enable ); // assumption for XF low level behavior!!!
	if ( !bFromAutoUpdate )
	{
		OpenExtractWksDataDlgModeless();
		return false; // don't go into xf body
	}
	///---END QA81-12954-P3 OPEN_WEXTRACT_DIALOG_SHOULD_USE_DASH_D
	
	pfnExtractDlg pfnOpenDlg = Project.FindFunction("ExtractDataBox", STR_EXTRACTWKSDATADLG_FILE, true);
	if( pfnOpenDlg )
		return pfnOpenDlg(tr, hWndParent, NULL);
	return error_report("Failed to load ExtractDataBox");
}


void wextract(const Range& iy, const TreeNode& settings, Range& oy)
{
	Worksheet		wksSrc, wksDest;
	int 			nSrcC1, nSrcC2;
	int 			nDestC1, nDestC2;
	if ( iy )
		iy.GetRange(wksSrc, nSrcC1, nSrcC2);
	if ( oy )
		oy.GetRange(wksDest, nDestC1, nDestC2);
	
	if ( !wksSrc )
		XF_THROW(XFERR_FAILED_GET_DATA_FROM_DATARANGE);
	if ( !wksDest )
	{
		nDestC1 = -1;
		nDestC2 = -1;
	}
	
	ExtractWksDataSettings extSettings;
	extSettings.SetTree(settings);
	
	ExtractWksData extwks;
	extwks.SetSettings(extSettings);
	///------ Folger 10/14/2010 ORG-1259-P1 EXTRACT_WKS_DATA_SHOULD_NOT_ACTIVE_OUTPUT_SHEET_WHEN_RECALCULATE
	//if ( !extwks.Extract(wksSrc, wksDest, &nDestC1, &nDestC2) )
	DWORD	dwOptions = 0;
	XFExeContext* pExeCtxt = Project.GetCurrentXFExeCtxt();
	O_SET_BIT(dwOptions, EXTRACT_NOT_ACTIVE_OUTPUT_SHEET, pExeCtxt->IsEvaluateFromAutoUpdate());
	if ( !extwks.Extract(wksSrc, wksDest, &nDestC1, &nDestC2, dwOptions) )
	///------ End EXTRACT_WKS_DATA_SHOULD_NOT_ACTIVE_OUTPUT_SHEET_WHEN_RECALCULATE
	{
		error_report("Error happen in extract_data");
		XF_THROW(2); // need a good error message code
	}
	
	// Sim, hard code, but hard to imporve
	//oy.SetRange(0, wksDest, 0, nC1, -1, wksDest.GetNumCols() - 1);
	if ( oy && wksDest )
		oy.SetRange(0, wksDest, 0, nDestC1, -1, nDestC2);
	
}

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

}

int wextract_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	if ( 0 == lstrcmp(lpcszVarName, "settings") )
	{
		ExtractWksDataSettings extSettings;
		extSettings.GetTree(tr);
		tree_set_attribute_to_all_nodes(tr, STR_SHOW_ATTRIB, "1");
		
		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 wextract_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	///---Sim 01-29-2010 QA81-12954-P3 OPEN_WEXTRACT_DIALOG_SHOULD_USE_DASH_D
	//if ( nGetNDialog > 0 )
	//{
		//if ( !( (LTXF_FROM_AUTO_UPDATE & dwCntrl) || (LTXF_CHANGE_PARAM & dwCntrl) ) )
		//{
			//warning_msg_box(_L("If want to open dialog, please go to menu Worksheet->Extract Worksheet Data..."), false);
			//nRet = XFEVT_ABORT;
			//return;
		//}
	//}	
	///---END QA81-12954-P3 OPEN_WEXTRACT_DIALOG_SHOULD_USE_DASH_D
	
}
//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 wextract_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
