#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	ch_red,
	ch_green,
	ch_blue,
};
//put additional include files here

//put your own support static functions here



//put additional include files here
#include <GetNbox.h>
#include <xfGraph_utils.h>
#include <..\originlab\grobj_utils.h>
#include <..\originlab\OCPalEdit.h>
///------ Folger 10/26/09 ADD_F1_MAPPING_IN_ORIGIN_CHM
#include <o8dlg.h>
///------ End ADD_F1_MAPPING_IN_ORIGIN_CHM

/*------------------------------------------------------------------------------*
* Modification Log:																*
*	Kyle 07/30/2009 QA80-13832 CHECK_COLOR_SEGMENTS_FIRST						*
*	Folger 10/26/09 ADD_F1_MAPPING_IN_ORIGIN_CHM								*
*	Kyle 09/17/2010 ORG-1075-P1 CHANGE_DEFAULT_PATH_FOR_SAVING_PALETTE_TO_UFF_PALETTES
*------------------------------------------------------------------------------*/


/// Hong 09/28/07 v8.0710c IMPROVE_CODE
static bool s_bOpenFile = false;
/// end IMPROVE_CODE

#define STR_INTERPOLATION_TYPE _L("Keep Current|Constant|Straight Line|Sigmoid")		///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT

enum{
	 _ID_USER_EVENT_APPLY  = 0,
	 _ID_USER_EVENT_SLIDER,
	 _ID_USER_EVENT_RECT,
	 _ID_USER_EVENT_CONTROL_LINE,	
	
};

//-------- CPY 2/24/2007 ADDING_CUSTOM_BUTTON4
enum {
	CUSTOM_BOTTON_NEW = 1,
	CUSTOM_BUTTON_OPEN,
	CUSTOM_BUTTON_SAVEAS,
	CUSTOM_BUTTON_SAVE,
};
//------- end ADDING_CUSTOM_BUTTON4

///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
//void _event_from_control_line(int nID)
//{
	//post_getn_dialog_message(WM_USER_ON_GRAPH_OBJECT_CHANGE, _ID_USER_EVENT_CONTROL_LINE);
//}
void _event_from_control_line(int nIndex, int nID)
{
	post_getn_dialog_message(WM_USER_ON_GRAPH_OBJECT_CHANGE, _ID_USER_EVENT_CONTROL_LINE, nIndex);
	Selection.Reset();
}
///End PALETTE_EDITOR_IMPORTMENT

void _event_from_palRect(int nID)
{
	post_getn_dialog_message(WM_USER_ON_GRAPH_OBJECT_CHANGE, _ID_USER_EVENT_RECT);
	Selection.Reset();		///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT

}
void _event_from_apply_button(int nID)
{

	post_getn_dialog_message(WM_USER_ON_GRAPH_OBJECT_CHANGE, _ID_USER_EVENT_APPLY);
	
}


static void _events_from_edit_silder_moving(OriginObject& oo, int nAnchorID, double dPos, TreeNode& trUserInfo)
{
	GraphLayer gl;
	oo.GetParent(gl);
	
	GraphPage gp = gl.GetPage();
	HWND hwnd;
	Tree trStorge;
	load_GetN_from_page(gp, trStorge,  hwnd, STR_STORAGE_NAME);	
	
	//current control line
	trStorge.currY.nVal = dPos*255;
	GraphObject go = gl.GraphObjects(oo.GetName());
	int nX = go.X;
	trStorge.currX.nVal = min(nX, 255);

	save_GetN_to_page(gp, trStorge,  hwnd, STR_STORAGE_NAME);
	
	post_getn_dialog_message(WM_USER_ON_GRAPH_OBJECT_CHANGE, _ID_USER_EVENT_SLIDER);
}


static void _create_seg_branch_for_channels(TreeNode& tr)
{

	int nBranchID =  _ID_SEL_DEFINE +  _ID_RED_CHANNEL + _ID_SEG_START;
	
	GETN_USE(tr)
	GETN_BEGIN_BRANCH(red, _L("Red"))	GETN_ID_BRANCH(nBranchID)  GETN_OPTION_BRANCH(GETNBRANCH_OPEN)
		///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
		//GETN_CHECK(fw, _L("Fixed"), 0)	GETN_ID(nBranchID + 10)
		GETN_CHECK(fw, _L("Use Same Width for All Segments"), 0)	GETN_ID(nBranchID + 10)
		///End PALETTE_EDITOR_IMPORTMENT
		GETN_NUM(width, _L("Width"), 64) GETN_ID(nBranchID  + 20)
		GETN_NUM(seg0, _L("#1 from:"), 0) GETN_ID(nBranchID  + 1)		
		GETN_NUM(seg1, _L("#1 to:"), 31) GETN_ID(nBranchID + 2)		
		GETN_NUM(seg2, _L("#2 to:"), 63) GETN_ID(nBranchID + 3)		
		GETN_NUM(seg3, _L("#3 to:"), 95) GETN_ID(nBranchID + 4)		
		GETN_NUM(seg4, _L("#4 to:"), 127) GETN_ID(nBranchID + 5)		
		GETN_NUM(seg5, _L("#5 to:"), 159) GETN_ID(nBranchID + 6)		
		GETN_NUM(seg6, _L("#6 to:"), 191) GETN_ID(nBranchID + 7)		
		GETN_NUM(seg7, _L("#7 to:"), 223) GETN_ID(nBranchID + 8)
		GETN_NUM(seg8, _L("#8 to:"), 255) GETN_ID(nBranchID + 9)
	GETN_END_BRANCH(red)	
	
	tr.red.seg0.Enable = false;
	tr.red.seg8.Enable = false;
	
	////////green channel//////////
	nBranchID =  _ID_SEL_DEFINE +  _ID_GREEN_CHANNEL + _ID_SEG_START;
	
	//GETN_USE(tr)
	GETN_BEGIN_BRANCH(green, _L("Green"))	GETN_ID_BRANCH(nBranchID)  GETN_OPTION_BRANCH(GETNBRANCH_OPEN)
		///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
		//GETN_CHECK(fw, _L("Fixed"), 0)	GETN_ID(nBranchID + 10)
		GETN_CHECK(fw, _L("Use Same Width for All Segments"), 0)	GETN_ID(nBranchID + 10)
		///End PALETTE_EDITOR_IMPORTMENT
		GETN_NUM(width, _L("Width"), 64) GETN_ID(nBranchID  + 20)
		GETN_NUM(seg0, _L("#1 from:"), 0) GETN_ID(nBranchID  + 1)		
		GETN_NUM(seg1, _L("#1 to:"), 31) GETN_ID(nBranchID + 2)		
		GETN_NUM(seg2, _L("#2 to:"), 63) GETN_ID(nBranchID + 3)		
		GETN_NUM(seg3, _L("#3 to:"), 95) GETN_ID(nBranchID + 4)		
		GETN_NUM(seg4, _L("#4 to:"), 127) GETN_ID(nBranchID + 5)		
		GETN_NUM(seg5, _L("#5 to:"), 159) GETN_ID(nBranchID + 6)		
		GETN_NUM(seg6, _L("#6 to:"), 191) GETN_ID(nBranchID + 7)		
		GETN_NUM(seg7, _L("#7 to:"), 223) GETN_ID(nBranchID + 8)
		GETN_NUM(seg8, _L("#8 to:"), 255) GETN_ID(nBranchID + 9)
	GETN_END_BRANCH(green)	
	
	tr.green.seg0.Enable = false;
	tr.green.seg8.Enable = false;
	
	/////////blue channel/////////
	nBranchID =  _ID_SEL_DEFINE +  _ID_BLUE_CHANNEL + _ID_SEG_START;
	
	//GETN_USE(tr)
	GETN_BEGIN_BRANCH(blue, _L("Blue"))	GETN_ID_BRANCH(nBranchID)  GETN_OPTION_BRANCH(GETNBRANCH_OPEN)
		///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
		//GETN_CHECK(fw, _L("Fixed"), 0)	GETN_ID(nBranchID + 10)
		GETN_CHECK(fw, _L("Use Same Width for All Segments"), 0)	GETN_ID(nBranchID + 10)
		///End PALETTE_EDITOR_IMPORTMENT
		GETN_NUM(width, _L("Width"), 64) GETN_ID(nBranchID  + 20)
		GETN_NUM(seg0, _L("#1 from:"), 0) GETN_ID(nBranchID  + 1)		
		GETN_NUM(seg1, _L("#1 to:"), 31) GETN_ID(nBranchID + 2)		
		GETN_NUM(seg2, _L("#2 to:"), 63) GETN_ID(nBranchID + 3)		
		GETN_NUM(seg3, _L("#3 to:"), 95) GETN_ID(nBranchID + 4)		
		GETN_NUM(seg4, _L("#4 to:"), 127) GETN_ID(nBranchID + 5)		
		GETN_NUM(seg5, _L("#5 to:"), 159) GETN_ID(nBranchID + 6)		
		GETN_NUM(seg6, _L("#6 to:"), 191) GETN_ID(nBranchID + 7)		
		GETN_NUM(seg7, _L("#7 to:"), 223) GETN_ID(nBranchID + 8)
		GETN_NUM(seg8, _L("#8 to:"), 255) GETN_ID(nBranchID + 9)
	GETN_END_BRANCH(blue)	
	
	tr.blue.seg0.Enable = false;
	tr.blue.seg8.Enable = false;
}

//static void _create_linetype_branch_for_channel(TreeNode& tr, int nChID, string strChName)
static void _create_linetype_branch_for_channels(TreeNode& tr)
{
	
	////red//////////////
	int nBranchID =  _ID_SEL_DEFINE +  _ID_RED_CHANNEL + _ID_LINE_TYPE;
	
	GETN_USE(tr)
	GETN_BEGIN_BRANCH(red, _L("Red"))	GETN_ID_BRANCH(nBranchID )  GETN_OPTION_BRANCH(GETNBRANCH_OPEN)
		///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
		//GETN_CHECK(ft, _L("Fixed Function"), 1)	GETN_ID(nBranchID + 10)
		//GETN_LIST(type, _L("Line Type"), 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID  + 20)	
		//GETN_LIST(seg1, "#1:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 1)
		//GETN_LIST(seg2, "#2:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 2)		
		//GETN_LIST(seg3, "#3:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 3)		
		//GETN_LIST(seg4, "#4:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 4)		
		//GETN_LIST(seg5, "#5:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 5)		
		//GETN_LIST(seg6, "#6:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 6)		
		//GETN_LIST(seg7, "#7:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 7)		
		//GETN_LIST(seg8, "#8:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 8)		
		GETN_CHECK(ft, _L("Use Same Type for All Segments"), 1)	GETN_ID(nBranchID + 10)
		GETN_LIST(type, _L("Gradient Type"), 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID  + 20)
		GETN_LIST(seg1, "#1:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 1)
		GETN_LIST(seg2, "#2:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 2)
		GETN_LIST(seg3, "#3:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 3)
		GETN_LIST(seg4, "#4:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 4)
		GETN_LIST(seg5, "#5:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 5)
		GETN_LIST(seg6, "#6:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 6)
		GETN_LIST(seg7, "#7:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 7)
		GETN_LIST(seg8, "#8:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 8)
		///End PALETTE_EDITOR_IMPORTMENT
	GETN_END_BRANCH(red)	
	
	////green//////////////
	nBranchID =  _ID_SEL_DEFINE +  _ID_GREEN_CHANNEL + _ID_LINE_TYPE;
	
	//GETN_USE(tr)
	GETN_BEGIN_BRANCH(green, _L("Green"))	GETN_ID_BRANCH(nBranchID )  GETN_OPTION_BRANCH(GETNBRANCH_OPEN)
		///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
		//GETN_CHECK(ft, _L("Fixed Function"), 1)	GETN_ID(nBranchID + 10)
		//GETN_LIST(type, _L("Line Type"), 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID  + 20)	
		//GETN_LIST(seg1, "#1:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 1)
		//GETN_LIST(seg2, "#2:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 2)		
		//GETN_LIST(seg3, "#3:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 3)		
		//GETN_LIST(seg4, "#4:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 4)		
		//GETN_LIST(seg5, "#5:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 5)		
		//GETN_LIST(seg6, "#6:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 6)		
		//GETN_LIST(seg7, "#7:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 7)		
		//GETN_LIST(seg8, "#8:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 8)		
		GETN_CHECK(ft, _L("Use Same Type for All Segments"), 1)	GETN_ID(nBranchID + 10)
		GETN_LIST(type, _L("Gradient Type"), 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID  + 20)
		GETN_LIST(seg1, "#1:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 1)
		GETN_LIST(seg2, "#2:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 2)
		GETN_LIST(seg3, "#3:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 3)
		GETN_LIST(seg4, "#4:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 4)
		GETN_LIST(seg5, "#5:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 5)
		GETN_LIST(seg6, "#6:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 6)
		GETN_LIST(seg7, "#7:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 7)
		GETN_LIST(seg8, "#8:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 8)
		///End PALETTE_EDITOR_IMPORTMENT
	GETN_END_BRANCH(green)	
	
	////blue//////////////
	nBranchID =  _ID_SEL_DEFINE +  _ID_BLUE_CHANNEL + _ID_LINE_TYPE;
	
	//GETN_USE(tr)
	GETN_BEGIN_BRANCH(blue, _L("Blue"))	GETN_ID_BRANCH(nBranchID )  GETN_OPTION_BRANCH(GETNBRANCH_OPEN)
		///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
		//GETN_CHECK(ft, _L("Fixed Function"), 1)	GETN_ID(nBranchID + 10)
		//GETN_LIST(type, _L("Line Type"), 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID  + 20)	
		//GETN_LIST(seg1, "#1:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 1)
		//GETN_LIST(seg2, "#2:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 2)		
		//GETN_LIST(seg3, "#3:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 3)		
		//GETN_LIST(seg4, "#4:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 4)		
		//GETN_LIST(seg5, "#5:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 5)		
		//GETN_LIST(seg6, "#6:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 6)		
		//GETN_LIST(seg7, "#7:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 7)		
		//GETN_LIST(seg8, "#8:", 1, "Constant|Line|Sigmoid") GETN_ID(nBranchID + 8)		
		GETN_CHECK(ft, _L("Use Same Type for All Segments"), 1)	GETN_ID(nBranchID + 10)
		GETN_LIST(type, _L("Gradient Type"), 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID  + 20)
		GETN_LIST(seg1, "#1:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 1)
		GETN_LIST(seg2, "#2:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 2)
		GETN_LIST(seg3, "#3:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 3)
		GETN_LIST(seg4, "#4:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 4)
		GETN_LIST(seg5, "#5:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 5)
		GETN_LIST(seg6, "#6:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 6)
		GETN_LIST(seg7, "#7:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 7)
		GETN_LIST(seg8, "#8:", 0, STR_INTERPOLATION_TYPE) GETN_ID(nBranchID + 8)
		///End PALETTE_EDITOR_IMPORTMENT
	GETN_END_BRANCH(blue)	
}



void palEdit(const string& load, const int& ch, const int& div_r, const int& div_g, const int& div_b, const TreeNode& seg_w, const TreeNode& seg_t, int& p)
{
	
}

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

}

int palEdit_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	
	if(tr.IsValid())
	{
		string strVarName = lpcszVarName;
		int iSeg;
		if(strVarName == "seg_w")
		{
			_create_seg_branch_for_channels(tr);
		}	
		
		if(strVarName == "seg_t")
		{
			_create_linetype_branch_for_channels(tr);
	
		}
		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 palEdit_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if (nGetNDialog <= 0 )
	{ 
		nRet = XFEVT_PROCEED_WITH_DLG; 
	}
	
	///------ Folger 10/26/09 ADD_F1_MAPPING_IN_ORIGIN_CHM
	if ( is_xf_startup(nGetNDialog, dwCntrl) )
	{
		trGetN.SetAttribute(STR_HELPID_ATTRIB, IDD_XF_palEdit);
	}
	///------ End ADD_F1_MAPPING_IN_ORIGIN_CHM
}
//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 palEdit_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}


// put your grid initialization here
// you may also initialize wksTemp here
static void GetNGraphPreview_OnInitGrid(TreeNode& trGetN, PageBase& pgTemp, Datasheet& wksTemp,
PictureControl& pict, // optional tab for showing an image or EMF, as requested in OnDialogSetup 
DWORD& dwUpdateGraph,  // default to 0, UPDATE_GRAPH_CHANGED to cause graphs to be updated, UPDATE_GRAPH_INVALID to disable preview button
DWORD& dwUpdateGrid, // default to 0, set to UPDATE_GRID_SIMPLE to update the DynaControl grid, UPDATE_GRID_RESET to reset DynaControl grid
DWORD& dwEnables,// bOKEnable, bBTN1Enable, bBTN2Enable and bBTN3Enable :default to true, can set to false to disable buttons
string& strErrMsg) // optional err msg to be shown on dialog
{
	DECLARE_BUTTON_ENABLES
	DECLARE_UPDATE_GRAPH
	DECLARE_UPDATE_GRID

	trGetN.SetAttribute(STR_ATTRIB_POST_USER_MESSAGE, 1);
	trGetN.load.Show = false;	
	bBTN4Enable = false; /// Hong 09/28/07 v8.0710c IMPROVE_CODE
	//trGetN.dlg.Show = false;
	trGetN.p.Show = false;
	
	OCPalEdit* pPalEdit = new OCPalEdit;
	trGetN.p.nVal = (DWORD)pPalEdit;
	
	GraphPage gp = pgTemp;
	Worksheet wks = wksTemp;
	
	pPalEdit->SetUpPreviewGraph(wks, gp, trGetN);	
	
	//pPalEdit->UpdateGetNTree(trGetN);
	
	trGetN.load.strVal = GetOriginPath(ORIGIN_PATH_SYSTEM)+"Palettes\\grayscale.pal";
	if(!pPalEdit->InitPreviewByLoading(trGetN.load.strVal))
	{
		strErrMsg = XFERR_INVALID_PALETTE_FILE;
		return;
	}
	
	pPalEdit->UpdateGetNTree(trGetN);
	pPalEdit->UpdateCurrColor(trGetN.ch.nVal);		///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
	pPalEdit->ApplyCurr(trGetN.ch.nVal);
	
	pPalEdit->ConvertOColor2RGBCol();
	pPalEdit->UpdateCurrSegmentInfo(trGetN);
			

	
}

///////////////////////////////////////////////////////

// pgTemp is the graph for preview or control
// wksTemp is the the temp wks created to hold data to plot into pgTemp
static void GetNGraphPreview_OnInitGraph(TreeNode& trGetN, PageBase& pgTemp, Datasheet& wksTemp,
PictureControl& pict, // optional tab for showing an image or EMF, as requested in OnDialogSetup  
DWORD& dwUpdateGrid, // default to 0, set to UPDATE_GRID_SIMPLE to update the DynaControl grid, UPDATE_GRID_RESET to reset DynaControl grid
DWORD& dwEnables,// bOKEnable, bBTN1Enable, bBTN2Enable and bBTN3Enable :default to true, can set to false to disable buttons
string& strErrMsg) // optional err msg to be shown on dialog
{
	DECLARE_BUTTON_ENABLES
	DECLARE_UPDATE_GRID

	//OCPalEdit* pPalEdit = (OCPalEdit*)(trGetN.p.nVal);
	//
	//GraphPage gp = pgTemp;
	//Worksheet wks = wksTemp;
	//
	//pPalEdit->SetUpPreviewGraph(wks, gp, trGetN);
	OCPalEdit* pPalEdit = (OCPalEdit*)(trGetN.p.nVal);
	
	pPalEdit->UpdateSegmentLines(trGetN);
	pPalEdit->UpdateRect(trGetN);
	pPalEdit->ApplyColorBar();


}

///////////////////////////////////////////////////////

static void GetNGraphPreview_OnUpdateGraph(TreeNode& trGetN, PageBase& pgTemp, Datasheet& wksTemp, PictureControl& pict)
{
	
	OCPalEdit* pPalEdit = (OCPalEdit*)(trGetN.p.nVal);
	
	pPalEdit->UpdateSegmentLines(trGetN);
	pPalEdit->UpdateRect(trGetN);
	///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
	//pPalEdit->ApplyColorBar();
	///End PALETTE_EDITOR_IMPORTMENT

}

///////////////////////////////////////////////////////

static void GetNGraphPreview_OnChange(TreeNode& trGetN, TreeNode& trNode, PageBase& pgTemp, Datasheet& wksTemp,
PictureControl& pict, // optional tab for showing an image or EMF that might be present, as requested in OnDialogSetup  
bool bChangedByGraph, // called when graph has been modified by user, like dragging a vertical line
DWORD& dwUpdateGraph,  // default to 0, UPDATE_GRAPH_CHANGED to cause graphs to be updated, UPDATE_GRAPH_INVALID to disable preview button
DWORD& dwUpdateGrid,  // default to 0, set to UPDATE_GRID_SIMPLE to update the DynaControl grid, UPDATE_GRID_RESET to reset DynaControl grid
DWORD& dwEnables,	// bOKEnable, bBTN1Enable, bBTN2Enable and bBTN3Enable :default to true, can set to false to disable buttons
string& strErrMsg) // optional err msg to be shown on dialog
{
	DECLARE_BUTTON_ENABLES
	DECLARE_UPDATE_GRAPH
	DECLARE_UPDATE_GRID
	
	OCPalEdit* pPalEdit = (OCPalEdit*)(trGetN.p.nVal);
	bBTN4Enable = s_bOpenFile; /// Hong 09/28/07 v8.0710c IMPROVE_CODE
	//Worksheet wks = wksTemp;
	//GraphPage gp = pgTemp;
	//GraphLayer gl = gp.Layers(0);
	///////////////////update
	///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
	//pPalEdit->UpdateGetNTree(trGetN,trNode);
	///End PALETTE_EDITOR_IMPORTMENT
	
	if(trNode)// on change from variable
	{
		///Kyle 07/30/2009 QA80-13832 CHECK_COLOR_SEGMENTS_FIRST
		bool bSegments = trNode.tagName == "div_r" ||trNode.tagName == "div_g"|| trNode.tagName == "div_b";
		if(bSegments)
		{
			trNode.nVal = min(8, trNode.nVal);
			trNode.nVal = max(1, trNode.nVal);
		}
		///End CHECK_COLOR_SEGMENTS_FIRST
		///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
		pPalEdit->UpdateGetNTree(trGetN,trNode);
		///End PALETTE_EDITOR_IMPORTMENT

		///Kyle 07/30/2009 QA80-13832 CHECK_COLOR_SEGMENTS_FIRST
		//if(trNode.tagName == "div_r" ||trNode.tagName == "div_g"|| trNode.tagName == "div_b")
		if(bSegments)
		///End CHECK_COLOR_SEGMENTS_FIRST
		{
			///Kyle 07/30/2009 QA80-13832 CHECK_COLOR_SEGMENTS_FIRST
			//trNode.nVal = min(8, trNode.nVal);
			//trNode.nVal = max(1, trNode.nVal);
			///End CHECK_COLOR_SEGMENTS_FIRST
			_set_curr_channel_to_fixed_width(trGetN);	
			_reinit_gradient_type_on_segments(trGetN);			///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
		}
		
		else if(trNode.tagName == "ch")
		{
			pPalEdit->UpdateCurrColor(trGetN.ch.nVal);		///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
			pPalEdit->ApplyCurr(trGetN.ch.nVal);
		}

		///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
		else if(trNode.tagName == "fw")
		{
			if(trNode.nVal)
			{
				pPalEdit->UpdatePreviewOnType(trGetN, -1);
				pPalEdit->ApplyCurr(trGetN.ch.nVal, false);
			}
		}
		else if(trNode.tagName == "ft")
		{
			pPalEdit->OnFixLineType(trGetN);
			if(trNode.nVal)
			{
				pPalEdit->UpdatePreviewOnType(trGetN, -1);
				pPalEdit->ApplyCurr(trGetN.ch.nVal, false);
			}
		}
		else if(trNode.tagName == "type")
		{
			pPalEdit->UpdatePreviewOnType(trGetN, -1);
			pPalEdit->ApplyCurr(trGetN.ch.nVal, false);
		}
		else if((trNode.DataID % _ID_RED_CHANNEL) > _ID_LINE_TYPE)
		{
			int nSegIndex = trNode.DataID % _ID_LINE_TYPE;	// 1 offset
			pPalEdit->UpdatePreviewOnType(trGetN, nSegIndex);
			pPalEdit->ApplyCurr(trGetN.ch.nVal, false);
		}
		else	// to
		{
			int nDataID = trNode.DataID;
			TreeNode trPrevSeg = tree_find_node_by_dataID(trGetN.seg_w, nDataID - 1);
			TreeNode trCurrSeg = tree_find_node_by_dataID(trGetN.seg_w, nDataID);
			TreeNode trNextSeg = tree_find_node_by_dataID(trGetN.seg_w, nDataID + 1);
			bool bChanged = true;
			if(trPrevSeg && trCurrSeg && trNextSeg)
			{
				if(trCurrSeg.nVal <= trPrevSeg.nVal || trCurrSeg.nVal >= trNextSeg.nVal)
				{
					int nOldValue;
					if(trNode.GetAttribute(STR_CHANGED_ATTRIB, nOldValue))
					{
						bChanged = false;
						trCurrSeg.nVal = nOldValue;
					}
				}
			}
			if(bChanged)
			{
				int nSegIndex = nDataID % _ID_SEG_START;	// 1 offset
				if(nSegIndex > 1)
					pPalEdit->UpdatePreviewOnType(trGetN, nSegIndex - 1);
				pPalEdit->UpdatePreviewOnType(trGetN, nSegIndex);
				pPalEdit->ApplyCurr(trGetN.ch.nVal, false);
			}
		}
		///End PALETTE_EDITOR_IMPORTMENT
		///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
		//pPalEdit->UpdateGetNTree(trGetN,trNode);
		///End PALETTE_EDITOR_IMPORTMENT
		pPalEdit->UpdateCurrSegmentInfo(trGetN);
		pPalEdit->ConvertOColor2RGBCol();
		
		bUpdateGrid = true;	
		bUpdateGraph = true;
		return;
			
	}
	///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
	else
		pPalEdit->UpdateGetNTree(trGetN, trNode);
	///End PALETTE_EDITOR_IMPORTMENT


	int		nUserEvent = -1;
	if(trGetN.GetAttribute(STR_GO_MSG_INFO_1_ATTRIB, nUserEvent))
	{
		bUpdateGraph = true;
		switch(nUserEvent)
		{
		case _ID_USER_EVENT_APPLY:
			pPalEdit->ApplyCurr(trGetN.ch.nVal, false);
			pPalEdit->ConvertOColor2RGBCol();
			break;
			
		case _ID_USER_EVENT_SLIDER:

			pPalEdit->UpdateCurrSegmentInfo(trGetN, true);
			pPalEdit->UpdatePreviewBySliders(trGetN);
			pPalEdit->ApplyCurr(trGetN.ch.nVal, false);		///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
			pPalEdit->ConvertOColor2RGBCol();
			break;
			
		case _ID_USER_EVENT_RECT:
			pPalEdit->UpdateCurrSegmentInfo(trGetN);
			break;
			
		case _ID_USER_EVENT_CONTROL_LINE:
			///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
			int nIndex;
			if(trGetN.GetAttribute(STR_GO_MSG_INFO_2_ATTRIB, nIndex) )
			{
				pPalEdit->OnMoveSegmentLine(trGetN, nIndex);
				pPalEdit->ApplyCurr(trGetN.ch.nVal, false);
				trGetN.RemoveAttribute(STR_GO_MSG_INFO_2_ATTRIB);
			}
			///End PALETTE_EDITOR_IMPORTMENT
			pPalEdit->UpdateCurrSegmentInfo(trGetN, false, true);//wait to add code to support move line by hand
			pPalEdit->ConvertOColor2RGBCol();
			bUpdateGrid = true;	
			break;
			
		default:
			break;
		}
		
		trGetN.RemoveAttribute(STR_GO_MSG_INFO_1_ATTRIB);

	}




}

// called from OnInitDialog to allow options on setting up of dialog
// dwOptions = 0 (default) will create a line graph with data in a temp worksheet
// dwOptions = XFGSETUP_IMAGEGRAPH_CNTRL to create an image graph with data in a temp matrix book
// dwOptions = XFGSETUP_MATRIX_CNTRL to have a MatrixControl with data in a temp matrix book
// dwOptions = XFGSETUP_2_TABS | XFGSETUP_EMF_1ST to have a PictureControl on 1st tab and a GraphControl on 2nd tab
// vsTabTitles is empty on entering the event, but you can put tab title strings in this array.
static void GetNGraphPreview_OnDialogSetup(TreeNode& trGetN, WndContainer& theDlg, DWORD& dwOptions, vector<string>& vsTabTitles)
{

}

// called when dialog is being destroyed, either as OK button or as Cancel button being pressed
static void GetNGraphPreview_OnDestroy(TreeNode& trGetN, PageBase& pgTemp, Datasheet& wksTemp, PictureControl& pict)
{

}
//------------
//
static void GetNGraphPreview_OnCustomButton(TreeNode& trGetN, int nButton, PageBase& pgTemp, Datasheet& wksTemp,
PictureControl& pict, // optional tab for showing an image or EMF that might be present, as requested in OnDialogSetup  
bool bChangedByGraph, // called when graph has been modified by user, like dragging a vertical line
DWORD& dwUpdateGraph,  // default to 0, UPDATE_GRAPH_CHANGED to cause graphs to be updated, UPDATE_GRAPH_INVALID to disable preview button
DWORD& dwUpdateGrid,  // default to 0, set to UPDATE_GRID_SIMPLE to update the DynaControl grid, UPDATE_GRID_RESET to reset DynaControl grid
DWORD& dwEnables,	// bOKEnable, bBTN1Enable, bBTN2Enable and bBTN3Enable :default to true, can set to false to disable buttons
string& strErrMsg) // optional err msg to be shown on dialog
{
	DECLARE_BUTTON_ENABLES
	DECLARE_UPDATE_GRAPH
	DECLARE_UPDATE_GRID
	
	OCPalEdit* pPalEdit = (OCPalEdit*)(trGetN.p.nVal);
	
	Worksheet wks = wksTemp;
	//if(!wks.IsValid())
	//{
		//strErrMsg = CER_INVALID_WKS;
		//return;
	//}
	
	string strFile;
	/// Hong 09/29/07 v8.0711b USING_TYPE_GROUP_DEFAULT_PATH
	/*
	StringArray saTypes;
	saTypes.SetSize(1);
	//saTypes[0] = "[Palette*.pal]*.pal";
	saTypes[0] = "[Palette (*.pal)] *.pal"; /// Hong 09/28/07 v8.0710c IMPROVE_CODE
	*/
	/// end USING_TYPE_GROUP_DEFAULT_PATH
	
	//GraphPage gp = pgTemp;
	//GraphLayer gl1 = gp.Layers(0);	
	//GraphLayer gl2 = gp.Layers(1);
	bool bClickSave = false;
	switch(nButton)
	{
		case CUSTOM_BUTTON_OPEN:
		{
			string strFileNameNew;
			//if(1 == get_open_box_by_file_group(strFileNameNew, "*.pal", "Open Palette", false, false))
			//if(1 == okutil_FileDialog(&strFileNameNew, false, &saTypes, NULL)) /// Hong 09/28/07 v8.0710c IMPROVE_CODE
			if(1 == get_open_box_by_file_group(strFileNameNew, "Palette", NULL, false)) /// Hong 09/29/07 v8.0711b USING_TYPE_GROUP_DEFAULT_PATH
			{				
				s_bOpenFile = true; /// Hong 09/28/07 v8.0710c IMPROVE_CODE
				trGetN.load.strVal = strFileNameNew;
				trGetN.load.Show = true;
				trGetN.load.Enable = false;
				bUpdateGrid = true;
				
				///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
				pPalEdit->ReInitGetNTree(trGetN, false);
				pPalEdit->UpdateCurrColor(trGetN.ch.nVal);
				///End PALETTE_EDITOR_IMPORTMENT
				
				pPalEdit->UpdateGetNTree(trGetN);
	
				if(!pPalEdit->InitPreviewByLoading(trGetN.load.strVal))
				{
					strErrMsg = XFERR_INVALID_PALETTE_FILE;
				}				

				pPalEdit->ApplyCurr(trGetN.ch.nVal);
				pPalEdit->ConvertOColor2RGBCol();
				
				pPalEdit->UpdateSegmentLines(trGetN);
				pPalEdit->UpdateCurrSegmentInfo(trGetN);
				pPalEdit->UpdateRect(trGetN);
				
				///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
				//pPalEdit->ApplyColorBar();
				///End PALETTE_EDITOR_IMPORTMENT
				bUpdateGraph = true;
				bUpdateGrid = true;
	
			}			
			break;
			
		}
		case CUSTOM_BUTTON_SAVEAS:
		{
			 pPalEdit->ApplyPreviewData();
			//if( true == okutil_FileDialog(&strFile, true,   &saTypes))
			///Kyle 09/17/2010 ORG-1075-P1 CHANGE_DEFAULT_PATH_FOR_SAVING_PALETTE_TO_UFF_PALETTES
			//if( 0 != get_saveas_box_by_file_group(strFile, "Palette",   NULL)) /// Hong 09/29/07 v8.0711b USING_TYPE_GROUP_DEFAULT_PATH
			string strDefPath = okutil_get_origin_path(ORIGIN_PATH_USER, "Palettes", true);
			if( !strDefPath.IsPath() )
				CreateDirectory(strDefPath, NULL);

			if( 0 != get_saveas_box_by_file_group(strFile, "Palette", NULL, 0, strDefPath) )
			///End CHANGE_DEFAULT_PATH_FOR_SAVING_PALETTE_TO_UFF_PALETTES
			{
				if(!save_palette_to_file(strFile, wks, COL_INDEX_PAL_R, COL_INDEX_PAL_G, COL_INDEX_PAL_B))
				{
					strErrMsg = XFERR_INVALID_PALETTE_FILE;
				}
			}
			break;
		}
		case CUSTOM_BUTTON_SAVE:
		{
	
			pPalEdit->ApplyPreviewData();
			strFile = trGetN.load.strVal;
			if(!save_palette_to_file(strFile, wks, COL_INDEX_PAL_R, COL_INDEX_PAL_G, COL_INDEX_PAL_B))
			{
				strErrMsg = XFERR_INVALID_PALETTE_FILE;
			}
			else
				bClickSave = true;//Sandy 2008-8-29 add to disable save button right after save
			break;
				
		}
		case CUSTOM_BOTTON_NEW:
		{			
			s_bOpenFile = false; /// Hong 09/28/07 v8.0710c IMPROVE_CODE
			trGetN.load.Show = false;
			trGetN.load.strVal = GetOriginPath(ORIGIN_PATH_SYSTEM)+"Palettes\\grayscale.pal";
			if(!pPalEdit->InitPreviewByLoading(trGetN.load.strVal))
			{
				strErrMsg = XFERR_INVALID_PALETTE_FILE;
				return;
			}
			///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
			pPalEdit->ReInitGetNTree(trGetN, true);
			pPalEdit->UpdateCurrColor(trGetN.ch.nVal);
			///End PALETTE_EDITOR_IMPORTMENT
			pPalEdit->UpdateGetNTree(trGetN);
			pPalEdit->ApplyCurr(trGetN.ch.nVal);
			
			pPalEdit->ConvertOColor2RGBCol();
			
			
			pPalEdit->UpdateSegmentLines(trGetN);
			pPalEdit->UpdateCurrSegmentInfo(trGetN);
			pPalEdit->UpdateRect(trGetN);
			///Kyle 02/11/2009 QA80-13002 PALETTE_EDITOR_IMPORTMENT
			//pPalEdit->ApplyColorBar();
			///End PALETTE_EDITOR_IMPORTMENT
			bUpdateGrid = true;
			bUpdateGraph = true;

			break;
		}
		default:
			break;
	}
	//bBTN4Enable = s_bOpenFile;/// Hong 09/28/07 v8.0710c IMPROVE_CODE
	bBTN4Enable = (s_bOpenFile && !bClickSave)
	

}
//


///////////////////////////////////////////////////////
	
int GetNGraphBox_palEdit(TreeNode& trGetN, int nRow, int nCol, TreeNode& trNode, DWORD dwCntrl, int nType, WndContainer& theDlg, PageBase& pgTemp, Datasheet& wksTemp, PictureControl& pict)
{
	return xf_GetNPreviewDlg_event_handler(trGetN, nRow, nCol, trNode, dwCntrl, nType, theDlg, pgTemp, wksTemp, pict,
	GetNGraphPreview_OnInitGrid, GetNGraphPreview_OnInitGraph,
	GetNGraphPreview_OnChange, GetNGraphPreview_OnUpdateGraph, GetNGraphPreview_OnDialogSetup, GetNGraphPreview_OnDestroy,
	GetNGraphPreview_OnCustomButton);	
}


//This file is the template for GetNEvent4

///////////////////////////////////////////////////////

