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

static void _add_check(TreeNode& tr, int nIndex, LPCSTR lpcszLabel, int nVal = 0)
{
	GETN_USE(tr)
	string strTag;strTag.Format("a%d", nIndex+1);
	GETN_CHECK(strTag, lpcszLabel, nVal) GETN_OPTION_DISPLAY_FORMAT(DISPLAY_EDITOR_LEFT)
}

static int _get_check_list(const vector<string> vsNames, vector<int>& vnChecks, LPCSTR lpczLabel, LPCSTR lpcszTitle)
{
	GETN_TREE(tr)
	for(int ii = 0; ii < vsNames.GetSize(); ii++)
		_add_check(tr, ii, vsNames[ii]);
	
	if(!GetNBox(tr, lpczLabel, lpcszTitle))
		return -1;
	vnChecks.SetSize(0);
	foreach(TreeNode trn in tr.Children)
		vnChecks.Add(trn.nVal > 0?1:0);
	
	return vnChecks.GetSize();
}


void dlgChkList(const string& inames, const string& title, const string& desc, vector& ochks, vector& osel, string& olist)
{
	vector<int> vn;
	vector<string> vsNames;
	inames.GetTokens(vsNames, '|');
	int nn = _get_check_list(vsNames, vn, title, desc);
	if(nn < 0)
		XF_THROW(XFERR_UI_USER_CANCEL);
	
	if(ochks) // each 1 or 0
		ochks = vn;
	
	if(osel || olist) // need to find the selected indices
	{
		vector<int> vnSels; // 1-offset index of each selected entry
		for(int ii = 0; ii < vn.GetSize(); ii++)
		{
			if(vn[ii])
				vnSels.Add(ii+1);
		}
		if(osel)
			osel = vnSels;
		if(olist)
		{
			string str;
			for(int jj = 0; jj < vnSels.GetSize(); jj++)
			{
				if(jj > 0)
					str += ","; // has to be comma separated, at least for this XF
				str += vnSels[jj];
			}
			olist = str;
		}
	}
	
}

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

}

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