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

#include <event_utils.h>
#include <..\Originlab\graph_utils.h>

////////////////////////////////////////////////////////////////////////
//	Modification Log:		
// Jasmine 01/07/09 v8.0994d QA80-10257 REWRTIE_WITH_APPLY_FORMAT_TO_UNDO
////////////////////////////////////////////////////////////////////////


void laycolor(const GraphPage& igp, const string& layer, const int& color, const int& fill, const int& border)
{
	if(!igp.IsValid())
		XF_THROW(CER_INVALID_GRAPH);
	vector<int> vNums;
	int nErr = get_indices_from_origin_object_index_range_string(layer, vNums, igp.Layers.Count());
	if(CER_NO_ERROR != nErr)
		XF_THROW(nErr);
	int nSize = vNums.GetSize();
	int nColor = okutil_ocolor_LT_color_convert(color, false);
	int nFillColor = okutil_ocolor_LT_color_convert(fill, false);
	int nBorderColor = okutil_ocolor_LT_color_convert(border, false);
	for(int ii = 0; ii < nSize; ii++)
	{
		GraphLayer gl = igp.Layers(vNums[ii] - 1);//LT to OriginC
		if(gl)
		{
			///Jasmine 01/07/09 v8.0994d QA80-10257 REWRTIE_WITH_APPLY_FORMAT_TO_UNDO
			//fill_layer_background_color(gl, color, border);
			//fill_layer_background_color(gl, &nColor, &nFillColor, &nBorderColor);	
			fill_layer_background_color(gl, &nColor, &nFillColor, &nBorderColor, true);	
		}
	}
}

//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 laycolor_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return laycolor_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	strErrMsg.Empty();
	GraphPage gp;
	if(strErrMsg.IsEmpty())
	{
		okxf_resolve_string_get_origin_object(trGetN.igp.strVal, &gp);
		if(!gp.IsValid())
			strErrMsg = CER_INVALID_GRAPH;
	}
	if(strErrMsg.IsEmpty())
	{
		vector<int> vNums;
		int nErr = get_indices_from_origin_object_index_range_string(trGetN.layer.strVal, vNums, gp.Layers.Count());
		if(CER_NO_ERROR != nErr)
			strErrMsg = nErr;
	}
	bOKEnable = strErrMsg.IsEmpty();
	return false;

}

int laycolor_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 laycolor_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	int nColor, nFillColor, nBorderColor;
	GraphPage gp;
	GraphLayer gl;
	okxf_resolve_string_get_origin_object(trGetN.igp.strVal, &gp);
	if(gp)
	{
		vector<int> vNums;
		get_indices_from_origin_object_index_range_string(trGetN.layer.strVal, vNums, gp.Layers.Count());
		gl = gp.Layers(vNums[0] - 1);//LT to OriginC
	}
	if(gl)
		get_layer_background_color(gl, nColor, &nFillColor, &nBorderColor);
	if(PDS_AUTO == okutil_cvt_str_to_predefined_type(trGetN.color.strVal))
		trGetN.color.nVal = okutil_ocolor_LT_color_convert(nColor, true);
	if(PDS_AUTO == okutil_cvt_str_to_predefined_type(trGetN.fill.strVal))
		trGetN.fill.nVal = okutil_ocolor_LT_color_convert(nFillColor, true);
	if(PDS_AUTO == okutil_cvt_str_to_predefined_type(trGetN.border.strVal))
		trGetN.border.nVal = okutil_ocolor_LT_color_convert(nBorderColor, true);
}
//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 laycolor_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
