#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
#include <image_utils.h>
#include <..\Originlab\matdata_utils.h>	
#include <GetNBox.h>
//put additional include files here
//enum{
	//PAL_GRAYSCALE = 0,
	//PAL_RAINBOW,
	//PAL_RWB,
//};
//
//#define PAL_FILE_NAME_GRAYSCALE  GetOriginPath(ORIGIN_PATH_SYSTEM)+"Palettes\\grayscale.pal"
//
//#define PAL_FILE_NAME_RAINBOW GetOriginPath(ORIGIN_PATH_SYSTEM)+"Palettes\\rainbow.pal"
//
//#define PAL_FILE_NAME_REDWHITEBLUE GetOriginPath(ORIGIN_PATH_SYSTEM)+"Palettes\\redwhiteblue.pal"

#define PAL_FOLDER_PATH GetOriginPath(ORIGIN_PATH_SYSTEM)+"Palettes\\"
//
//put your own support static functions here


//static string _get_pal_file_string()
//{
	//string strFile;
	//string strExt = ".pal";
		//
	////-- if empty or no path
	////-- PATH
	//BOOL bOpenFileDlg = FALSE;
	//string strFileNameWithExt;
//
	//string strPath = GetOriginPath(ORIGIN_PATH_SYSTEM)+"Palettes\\";
	//if( !strFile.IsEmpty() )
	//{
		////-- check extension
 		//strFileNameWithExt = GetFileName(strFile);
 		//string strExtensionPlusDot = strFileNameWithExt.Mid( strFileNameWithExt.ReverseFind( '.' ) );
 		//
 		////--check path
 		//strPath = GetFilePath(strFile);
 		//if( !strPath.IsPath() )
 		//{
 			//strPath = GetAppPath();
 			//bOpenFileDlg = TRUE;
 		//}
 		//
 		////-- combine path and filename
 		//strFile = strPath + strFileNameWithExt;	
	//}
	//else
		//bOpenFileDlg = TRUE;
		//
	//
	//if(bOpenFileDlg)
	//{
		//string strFileOpen = GetOpenBox( "*.pal PAL", PAL_FOLDER_PATH, strFileNameWithExt );
		//if(strFileOpen.IsEmpty())
		//{
			//return "";
//
		//}
		//else
			//strFile = strFileOpen;
//
	//}
	//
	//return strFile;	
//}
//

static bool _apply_palette_in_preview(BITMAPHANDLE* pPreviewBitmap, string strFile, string& strErr)
{
	int nRet = apply_pal_to_LT_bmp(pPreviewBitmap,strFile);
	if( SUCCESS != nRet )
	{
		if(nRet == -13)
			strErr = XFERR_NOT_SUPPORT_MORE_THAN_255;
		else
		{
			///Kyle 07/12/2010 ORG-234-P6 SHOW_PROPER_WARNING_MESSAGE_IMG_PALETTE
			//strErr = nRet;
			if( nRet > SUCCESS )
				strErr = nRet;
			else
			{
				strErr = XFERR_LEADTOOLS_FUNCTION_UNKNOWN_ERR;
				strErr += ":" + nRet;
			}
			///End SHOW_PROPER_WARNING_MESSAGE_IMG_PALETTE
		}
		
		return false;
	}	
	
	return true;
}

void imgPalette(const Image& img, const string& palfile, Image& oimg)
{
	
	oimg.SetLBmp(img.GetLBmp(), TRUE);
	//pBITMAPHANDLE phBmp = imgo.GetLBmp();
	
	int nRet = apply_pal_to_image(oimg, palfile);
	
	if(nRet < MAX_ERROR_CODE_FROM_LT)
	{
		if(nRet == -13)
			XF_THROW(XFERR_NOT_SUPPORT_MORE_THAN_256)
		else
			XF_THROW_EX(XFERR_LEADTOOLS_FUNCTION_UNKNOWN_ERR, nRet)
	}
		
	
	if(nRet !=  SUCCESS)
	{
		XF_THROW(nRet);
	}
	
	
}

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

}

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


// this is called when the GetNImageBox dialog is open
// bUpdateGrid = false when entering this function
// you need to set bUpdateGrid = true if you need to modify trGetN data and want to see the
// grid updated, for example
// trGetN.angle.nVal = 0; // force rotation to init to 0
// bUpdateGrid = true;
// bOKEnable is default = true, you may add code to check trGetN and decide that the OK button should be disabled, then set this to false
// example: 
// if(0==trGetN.angle.nVal) // no change, no point to click Ok to do nothing
//    bOKEnable = false; 
static void GetNImageBox_OnInit(TreeNode& trGetN, ImageControlBase* pimgCntrl, bool& bUpdateGrid, bool& bOKEnable, string& strErrMsg)
{

	string strFile = PAL_FOLDER_PATH + "grayscale.pal";
	
	tree_set_browse_combo_attrib(trGetN.palfile);
	
	strFile = cvt_predefined_path_to_real_path(strFile); 
	
	trGetN.palfile.strVal = strFile;
	pBITMAPHANDLE pPreviewBitmap;
	pPreviewBitmap=pimgCntrl->GetBitmapHandlePtr();
	
	//int nLen=get_image_data_max_val(pPreviewBitmap)+1;
	//if(nLen>256)
	//{
		//strErrMsg = XFERR_CAN_NOT_SUPPORT_16BIT_COLOR_OR_UPWARDS;
		//bOKEnable = false;
		//return;
	//}
	
	bOKEnable = _apply_palette_in_preview(pPreviewBitmap, strFile, strErrMsg);
	
	bUpdateGrid = true;
}

// this is called whenever any controls in the grid is modified by the user
// upon entering function
// bUpdatePreview = true;// to update the preview image pPreviewBitmap
// bUpdateGrid = false; // event triggered by grid change, so default no need to update again
// if you want to modify other controls in the grid, then you need to set bUpdateGrid to true
// bOKEnable is default = true, you may add code to check trGetN and decide that the OK button should be disabled, then set this to false
static void GetNImageBox_OnChange(BITMAPHANDLE* pPreviewBitmap, TreeNode& trGetN, TreeNode& trNode, ImageControlBase* pimgCntrl, bool& bUpdatePreview, bool& bUpdateGrid, bool& bOKEnable, string& strErrMsg)
{
	string strFile = trGetN.palfile.strVal;
	//if(trNode.tagName == "palfile")
		//strFile = _get_pal_file_string(); 

		
	//int nLen=get_image_data_max_val(pPreviewBitmap)+1;
	
	//int nRet = apply_pal_to_LT_bmp(pPreviewBitmap,strFile);
	//if( SUCCESS != nRet )
	//{
		//if(nRet == -13)
			//strErrMsg = XFERR_CAN_NOT_SUPPORT_16BIT_COLOR_OR_UPWARDS;
		//else
			//strErrMsg = nRet;
		//bOKEnable = false;
		//return;
	//}
	bOKEnable = _apply_palette_in_preview(pPreviewBitmap, strFile, strErrMsg);
	
	if(bOKEnable)
	{
		update_recent_path_list(strFile);
		bUpdatePreview = true;
	}
}
	
int GetNImageBox_imgPalette(TreeNode& trGetN, int nRow, int nCol, TreeNode& trNode, DWORD dwCntrl, int nType, WndContainer& theDlg, ImageControlBase* pimgCntrl)
{
	return xf_GetNImageBox_event_handler(trGetN, nRow, nCol, trNode, dwCntrl, nType, pimgCntrl,
								GetNImageBox_OnInit, GetNImageBox_OnChange);	
}
