#ifndef __XCCHART_H__
#define __XCCHART_H__

/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// CChart相关头文件和库文件、名字空间

#include "Chart.h"
#if defined(_UNICODE) || defined(UNICODE)
#	pragma comment(lib,"CChartu.lib")
#else
#	pragma comment(lib,"CChart.lib")
#endif

using namespace NsCChart;

#include <math.h>

/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// 在炫彩界面库这种DirectUI类库中，只有一个主窗口的HWND
// CChart是基于HWND的，在炫彩元素中绘图时，可能会认错父窗口（把主窗口认为是父窗口），在一些消息中鼠标位置会错位
// 此宏命令对此进行修正
// 注意，此宏命令需要放置在XWnd_ShowWindow之后调用
#define	XC_CChart_EleMsgRepair(hWindowM, chartM, IDS) {\
	HELE hMyPanel = (HELE)XC_GetObjectByIDName(hWindowM, IDS);\
	RECT rect;\
	XEle_GetWndClientRect(hMyPanel, &rect);\
	chartM.SetAdditionalCanvasOffset(rect.left, rect.top);\
	chartM.SetDuiRepair(true);\
	}\

/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// 通过宏命令的方法，以C和C++两种方式注册炫彩元件中用到的消息，以简化用户调用

/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// C++方式注册

#define XC_CChart_RegEleEventCPP(XCWinClass, hWindowM, IDS) {\
	HELE hMyPanel = (HELE)XC_GetObjectByIDName(hWindowM, IDS);\
	XEle_RegEventCPP(hMyPanel, XE_PAINT, &##XCWinClass##::OnEleDraw);\
	XEle_RegEventCPP(hMyPanel, XE_LBUTTONDOWN, &##XCWinClass##::OnEleLButtonDown);\
	XEle_RegEventCPP(hMyPanel, XE_LBUTTONUP, &##XCWinClass##::OnEleLButtonUp);\
	XEle_RegEventCPP(hMyPanel, XE_LBUTTONDBCLICK, &##XCWinClass##::OnEleLButtonDblClk);\
	XEle_RegEventCPP(hMyPanel, XE_RBUTTONDOWN, &##XCWinClass##::OnEleRButtonDown);\
	XEle_RegEventCPP(hMyPanel, XE_MOUSEMOVE, &##XCWinClass##::OnEleMouseMove);\
	XEle_RegEventCPP(hMyPanel, XE_MOUSEWHEEL, &##XCWinClass##::OnEleMouseWheel);\
	XEle_RegEventCPP(hMyPanel, XE_KEYDOWN, &##XCWinClass##::OnEleKeyDown);\
	XEle_EnableEvent_XE_MOUSEWHEEL(hMyPanel, TRUE);\
	}\
	
#define XC_CChart_DecEleEventCPP(hWindowM, chartM, IDS)\
	int OnEleDraw(HDRAW hDraw, BOOL *pbHandled)\
	{\
		RECT rect;\
		HELE hMyPanel = (HELE)XC_GetObjectByIDName(hWindowM, IDS);\
		XEle_GetClientRect(hMyPanel, &rect);\
		HDC hDC = XDraw_GetHDC(hDraw);\
		XEle_DrawEle(hMyPanel, hDraw);\
		if(XC_IsEnableD2D())\
		{\
			chartM.OnDraw(hDC, rect);\
		}\
		else\
		{\
			chartM.OnDraw(hDC, rect);\
		}\
		\
		*pbHandled=TRUE;\
		return 0;\
	}\
	int OnEleLButtonDown(UINT nFlags, POINT *pPt, BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnLButtonDown(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\
	int OnEleLButtonUp(UINT flags, POINT *pPt, BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnLButtonUp(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\
	int OnEleLButtonDblClk(UINT nFlags, POINT *pPt, BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnLButtonDblClk(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\
	int OnEleRButtonDown(UINT nFlags, POINT *pPt, BOOL *pbHandled)\
	{\
		HELE hMyPanel = (HELE)XC_GetObjectByIDName(hWindowM, IDS);\
		XEle_PointClientToWndClient(hMyPanel, pPt);\
		\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		ClientToScreen(hWnd, pPt);\
		if(chartM.OnContextMenu(NULL, hWnd, *pPt))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\
	int OnEleMouseMove(UINT nFlags, POINT *pPt, BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnMouseMove(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\
	int OnEleMouseWheel(UINT nFlags,POINT *pPt,BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnMouseWheel(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\
	int OnEleKeyDown(WPARAM wParam, LPARAM lParam, BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		UINT	key = (UINT)wParam;\
		if(chartM.OnKeyDown(hWnd, key))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\

/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// C方式注册

#define XC_CChart_RegEleEventC(hWindowM, IDS) {\
	HELE hMyPanel = (HELE)XC_GetObjectByIDName(hWindowM, IDS);\
	XEle_RegEventC(hMyPanel, XE_PAINT, OnEleDraw);\
	XEle_RegEventC(hMyPanel, XE_LBUTTONDOWN, OnEleLButtonDown);\
	XEle_RegEventC(hMyPanel, XE_LBUTTONUP, OnEleLButtonUp);\
	XEle_RegEventC(hMyPanel, XE_LBUTTONDBCLICK, OnEleLButtonDblClk);\
	XEle_RegEventC(hMyPanel, XE_RBUTTONDOWN, OnEleRButtonDown);\
	XEle_RegEventC(hMyPanel, XE_MOUSEMOVE, OnEleMouseMove);\
	XEle_RegEventC(hMyPanel, XE_MOUSEWHEEL, OnEleMouseWheel);\
	XEle_RegEventC(hMyPanel, XE_KEYDOWN, OnEleKeyDown);\
	XEle_EnableEvent_XE_MOUSEWHEEL(hMyPanel, TRUE);\
	}\

#define XC_CChart_DecEleEventC(hWindowM, chartM, IDS)\
	int CALLBACK OnEleDraw(HDRAW hDraw, BOOL *pbHandled)\
	{\
		RECT rect;\
		HELE hMyPanel = (HELE)XC_GetObjectByIDName(hWindowM, IDS);\
		XEle_GetClientRect(hMyPanel, &rect);\
		HDC hDC = XDraw_GetHDC(hDraw);\
		XEle_DrawEle(hMyPanel, hDraw);\
		if(XC_IsEnableD2D())\
		{\
			chartM.OnDraw(hDC, rect);\
		}\
			else\
		{\
			chartM.OnDraw(hDC, rect);\
		}\
		\
		*pbHandled=TRUE;\
		return 0;\
	}\
	int CALLBACK OnEleLButtonDown(UINT nFlags, POINT *pPt, BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnLButtonDown(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\
	int CALLBACK OnEleLButtonUp(UINT flags, POINT *pPt, BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnLButtonUp(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\
	int CALLBACK OnEleLButtonDblClk(UINT nFlags, POINT *pPt, BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnLButtonDblClk(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\
	int CALLBACK OnEleRButtonDown(UINT nFlags, POINT *pPt, BOOL *pbHandled)\
	{\
		HELE hMyPanel = (HELE)XC_GetObjectByIDName(hWindowM, IDS);\
		XEle_PointClientToWndClient(hMyPanel, pPt);\
		\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		ClientToScreen(hWnd, pPt);\
		if(chartM.OnContextMenu(NULL, hWnd, *pPt))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\
	int CALLBACK OnEleMouseMove(UINT nFlags, POINT *pPt, BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnMouseMove(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\
	int CALLBACK OnEleMouseWheel(UINT nFlags,POINT *pPt,BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnMouseWheel(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\
	int CALLBACK OnEleKeyDown(WPARAM wParam, LPARAM lParam, BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		UINT	key = (UINT)wParam;\
		if(chartM.OnKeyDown(hWnd, key))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\

/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// 通过宏命令的方法，以C和C++两种方式注册炫彩主窗口中用到的消息，以简化用户调用

/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// C++方式注册

#define XC_CChart_RegWndEventCPP(XCWinClass, hWindowM) {\
	XWnd_RegEventCPP(hWindowM, WM_PAINT, &##XCWinClass##::OnWndDrawWindow);\
	XWnd_RegEventCPP(hWindowM,WM_LBUTTONDOWN, &##XCWinClass##::OnWndLButtonDown);\
	XWnd_RegEventCPP(hWindowM,WM_LBUTTONUP, &##XCWinClass##::OnWndLButtonUp);\
	XWnd_RegEventCPP(hWindowM,WM_LBUTTONDBLCLK, &##XCWinClass##::OnWndLButtonDblClk);\
	XWnd_RegEventCPP(hWindowM,WM_RBUTTONDOWN, &##XCWinClass##::OnWndRButtonDown);\
	XWnd_RegEventCPP(hWindowM,WM_MOUSEMOVE, &##XCWinClass##::OnWndMouseMove);\
	XWnd_RegEventCPP(hWindowM, WM_MOUSEWHEEL, &##XCWinClass##::OnWndMouseWheel);\
	XWnd_RegEventCPP(hWindowM, WM_KEYDOWN, &##XCWinClass##::OnWndKeyDown);\
	}\

#define XC_CChart_DecWndEventCPP(hWindowM, chartM)\
	int OnWndDrawWindow(HDRAW hDraw, BOOL *pbHandled)\
	{\
		RECT rect;\
		XWnd_GetBodyRect(hWindowM, &rect);\
		HDC hDC = XDraw_GetHDC(hDraw);\
		\
		XWnd_DrawWindow(hWindowM, hDraw);\
		chartM.OnDraw(hDC, rect);\
		*pbHandled=TRUE;\
		return 0;\
	}\
	int OnWndLButtonDown(UINT nFlags, POINT *pPt, BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnLButtonDown(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM, TRUE);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\
   int OnWndLButtonUp(UINT flags,POINT *pPt,BOOL *pbHandled)\
    {\
        HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnLButtonUp(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM, TRUE);\
		}\
		*pbHandled = FALSE;\
		return 0;\
    }\
    int OnWndLButtonDblClk(UINT nFlags,POINT *pPt, BOOL *pbHandled)\
    {\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnLButtonDblClk(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM, TRUE);\
		}\
		*pbHandled = FALSE;\
		return 0;\
    }\
	int OnWndRButtonDown(UINT nFlags,POINT *pPt, BOOL *pbHandled)\
    {\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		ClientToScreen(hWnd, pPt);\
		if(chartM.OnContextMenu(NULL, hWnd, *pPt))\
		{\
			XWnd_Redraw(hWindowM, TRUE);\
		}\
		*pbHandled = FALSE;\
		return 0;\
    }\
	int OnWndMouseMove(UINT nFlags,POINT *pPt, BOOL *pbHandled)\
    {\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnMouseMove(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM, TRUE);\
		}\
		*pbHandled = FALSE;\
		return 0;\
    }\
	int OnWndMouseWheel(UINT nFlags,POINT *pPt, BOOL *pbHandled)\
    {\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnMouseWheel(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM, TRUE);\
		}\
		*pbHandled = FALSE;\
		return 0;\
    }\
	int OnWndKeyDown(WPARAM wParam,LPARAM lParam,BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		UINT	key = (UINT)wParam;\
		if(chartM.OnKeyDown(hWnd, key))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\

/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// C方式注册

#define XC_CChart_RegWndEventC(hWindowM) {\
	XWnd_RegEventC(hWindowM, WM_PAINT, OnWndDrawWindow);\
	XWnd_RegEventC(hWindowM,WM_LBUTTONDOWN, OnWndLButtonDown);\
	XWnd_RegEventC(hWindowM,WM_LBUTTONUP, OnWndLButtonUp);\
	XWnd_RegEventC(hWindowM,WM_LBUTTONDBLCLK, OnWndLButtonDblClk);\
	XWnd_RegEventC(hWindowM,WM_RBUTTONDOWN, OnWndRButtonDown);\
	XWnd_RegEventC(hWindowM,WM_MOUSEMOVE, OnWndMouseMove);\
	XWnd_RegEventC(hWindowM, WM_MOUSEWHEEL, OnWndMouseWheel);\
	XWnd_RegEventC(hWindowM, WM_KEYDOWN, OnWndKeyDown);\
	}\
	
#define XC_CChart_DecWndEventC(hWindowM, chartM)\
	int CALLBACK OnWndDrawWindow(HDRAW hDraw, BOOL *pbHandled)\
	{\
		RECT rect;\
		XWnd_GetBodyRect(hWindowM, &rect);\
		HDC hDC = XDraw_GetHDC(hDraw);\
		\
		XWnd_DrawWindow(hWindowM, hDraw);\
		chartM.OnDraw(hDC, rect);\
		*pbHandled=TRUE;\
		return 0;\
	}\
	int CALLBACK OnWndLButtonDown(UINT nFlags, POINT *pPt, BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnLButtonDown(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM, TRUE);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\
	int CALLBACK OnWndLButtonUp(UINT flags,POINT *pPt,BOOL *pbHandled)\
    {\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnLButtonUp(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM, TRUE);\
		}\
		*pbHandled = FALSE;\
		return 0;\
    }\
    int CALLBACK OnWndLButtonDblClk(UINT nFlags,POINT *pPt, BOOL *pbHandled)\
    {\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnLButtonDblClk(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM, TRUE);\
		}\
		*pbHandled = FALSE;\
		return 0;\
    }\
	int CALLBACK OnWndRButtonDown(UINT nFlags,POINT *pPt, BOOL *pbHandled)\
    {\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		ClientToScreen(hWnd, pPt);\
		if(chartM.OnContextMenu(NULL, hWnd, *pPt))\
		{\
			XWnd_Redraw(hWindowM, TRUE);\
		}\
		*pbHandled = FALSE;\
		return 0;\
    }\
	int CALLBACK OnWndMouseMove(UINT nFlags,POINT *pPt, BOOL *pbHandled)\
    {\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnMouseMove(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM, TRUE);\
		}\
		*pbHandled = FALSE;\
		return 0;\
    }\
	int CALLBACK OnWndMouseWheel(UINT nFlags,POINT *pPt, BOOL *pbHandled)\
    {\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		if(chartM.OnMouseWheel(hWnd, *pPt, 0))\
		{\
			XWnd_Redraw(hWindowM, TRUE);\
		}\
		*pbHandled = FALSE;\
		return 0;\
    }\
	int CALLBACK OnWndKeyDown(WPARAM wParam,LPARAM lParam,BOOL *pbHandled)\
	{\
		HWND hWnd = XWnd_GetHWND(hWindowM);\
		UINT	key = (UINT)wParam;\
		if(chartM.OnKeyDown(hWnd, key))\
		{\
			XWnd_Redraw(hWindowM);\
		}\
		*pbHandled = FALSE;\
		return 0;\
	}\

#endif