// TestXC.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#pragma comment(lib, "XCGUI.lib")
#include "xcgui.h"

#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>

class CMyWindowDemo
{
public:
    HWINDOW m_hWindow;
	CChart m_Chart;

    CMyWindowDemo()
    {
        Init();
    }
    void Init()
    {
        XC_LoadResource(L".\\Simple\\resource.res");  //加载资源文件
        HXCGUI hXCGUI=XC_LoadLayout(L".\\Simple\\layout.xml"); //加载布局文件
        if(XC_IsHWINDOW(hXCGUI))
        {
            m_hWindow=(HWINDOW)hXCGUI;
            //XC_GetObjectByID(m_hWindow, 100); //获取指定ID对象
            //XC_GetObjectByName(L"name") //获取指定name对象
            //XC_ShowLayoutFrame(TRUE); //显示布局边界

			HELE hCloseBtn = (HELE)XC_GetObjectByID(m_hWindow, 121);
			XBtn_SetType(hCloseBtn,button_type_close);

			HELE hPanel = (HELE)XC_GetObjectByID(m_hWindow, 151);
			XEle_RegEventCPP(hPanel, XE_PAINT, &CMyWindowDemo::OnPanelDraw);

			XEle_RegEventCPP(hPanel, XE_LBUTTONDOWN, &CMyWindowDemo::OnEleLButtonDown);
			XEle_RegEventCPP(hPanel, XE_LBUTTONUP, &CMyWindowDemo::OnEleLButtonUp);
			XEle_RegEventCPP(hPanel, XE_LBUTTONDBCLICK, &CMyWindowDemo::OnEleLButtonDblClk);
			XEle_RegEventCPP(hPanel, XE_RBUTTONDOWN, &CMyWindowDemo::OnEleRButtonDown);
			XEle_RegEventCPP(hPanel, XE_MOUSEMOVE, &CMyWindowDemo::OnEleMouseMove);

			HELE hDrawBtn = (HELE)XC_GetObjectByID(m_hWindow, 122);
			XEle_RegEventCPP(hDrawBtn,XE_BNCLICK,&CMyWindowDemo::My_EventDrawBtnClick); //注册按钮点击事件

			XWnd_AdjustLayout(m_hWindow);
            XWnd_ShowWindow(m_hWindow,SW_SHOW);
        }else
        {
            //错误
        }
		double pi=3.1415926516;
		for(int i=0; i<720; ++i)
		{
			m_Chart.AddPoint2D(i, 1.3*sin(i*2.0*pi/360));
		}
		m_Chart.SetTitle(_T("炫彩界面库"));
	}

	int OnPanelDraw(HDRAW hDraw,BOOL *pbHandled)
	{
		 RECT rect;
		 HELE hPanel = (HELE)XC_GetObjectByID(m_hWindow, 151);
		 XEle_GetClientRect(hPanel, &rect);
		 HDC hDC = XDraw_GetHDC(hDraw);
		 XEle_DrawEle(hPanel, hDraw);
		 m_Chart.OnDraw(hDC, rect);
		 
		 *pbHandled=TRUE;
		 return 0;
	}

	int OnEleLButtonDown(UINT nFlags,POINT *pPt,BOOL *pbHandled)
    {
          HWND hWnd = XWnd_GetHWND(m_hWindow);
		  if(m_Chart.OnLButtonDown(hWnd, *pPt, 0))
		  {
			  XWnd_RedrawWnd(m_hWindow);
		  }
		  *pbHandled = FALSE;
		  return 0;
	}
	int OnEleLButtonUp(UINT flags,POINT *pPt,BOOL *pbHandled)
	{
		HWND hWnd = XWnd_GetHWND(m_hWindow);
		if(m_Chart.OnLButtonUp(hWnd, *pPt, 0))
		{
			XWnd_RedrawWnd(m_hWindow);
		}
		*pbHandled = FALSE;
		return 0;
	}

    int OnEleLButtonDblClk(HWINDOW hWindow,UINT nFlags,POINT *pPt, BOOL *pbHandled)
	{
		HWND hWnd = XWnd_GetHWND(m_hWindow);
		if(m_Chart.OnLButtonDblClk(hWnd, *pPt, 0))
		{
			XWnd_RedrawWnd(m_hWindow);
		}
		*pbHandled = FALSE;
		return 0;
	}

    int OnEleRButtonDown(HWINDOW hWindow,UINT nFlags,POINT *pPt, BOOL *pbHandled)
	{
		HELE hPanel = (HELE)XC_GetObjectByID(m_hWindow, 151);
		XEle_PointClientToWndClient(hPanel, pPt);

		HWND hWnd = XWnd_GetHWND(m_hWindow);
		ClientToScreen(hWnd, pPt);
		if(m_Chart.OnContextMenu(NULL, hWnd, *pPt))
		{
			XWnd_RedrawWnd(m_hWindow);
		}
		*pbHandled = FALSE;
		return 0;
	}

    int OnEleMouseMove(HWINDOW hWindow,UINT nFlags,POINT *pPt, BOOL *pbHandled)
	{
		HWND hWnd = XWnd_GetHWND(m_hWindow);
		if(m_Chart.OnMouseMove(hWnd, *pPt, 0))
		{
			XWnd_RedrawWnd(m_hWindow);
		}
		*pbHandled = FALSE;
		return 0;
	}

	int My_EventDrawBtnClick(BOOL *pbHandled)
	{
		MessageBoxW(XWnd_GetHWND(m_hWindow),L"右半边是绘图区域",L"提示",MB_OK);
		*pbHandled=TRUE;
		return 0;
	}
};

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    XInitXCGUI(FALSE);
    CMyWindowDemo  MyWindow;
    XRunXCGUI();
    XExitXCGUI();
    return 0;
}
