// Test.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "Test.h"

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

class CMyWindowTree
{
public:
	HWINDOW m_hWindow;
	HELE    m_hTree;
	HIMAGE  m_hVip;
	HIMAGE  m_hQZone;
	HIMAGE  m_hAvatar;
	HIMAGE  m_hAvatarLarge;

	HIMAGE  m_hExpand;
	HIMAGE  m_hExpandNo;

	template_info_i  *m_pTemplate_group;
	template_info_i  *m_pTemplate_sel;
	CMyWindowTree()
	{
		m_pTemplate_group=NULL;
		Init();
	}
	void Release()
	{
		if(m_pTemplate_group) XC_TemplateDestroy(m_pTemplate_group);
		if(m_hExpand) XImage_Destroy(m_hExpand);
		if(m_hExpandNo) XImage_Destroy(m_hExpandNo);
	}
	void Init()
	{
		//加载资源文件
		XC_LoadResource(L"resource.xml");
		//加载布局文件
		HXCGUI hXCGUI=XC_LoadLayout(L"Layout.xml");
		m_hWindow=(HWINDOW)hXCGUI;

		//m_hWindow = XWnd_Create(0, 0, 320, 520, L"炫彩界面库窗口",NULL, xc_window_style_default);
		//XBtn_SetType(XBtn_Create(5, 3, 60, 20, L"Close", m_hWindow),button_type_close);

		//m_hTree= XTree_Create(20,40,280,450,m_hWindow);
		m_hTree=(HELE)XC_GetObjectByID(m_hWindow,101);
		XTree_EnableConnectLine(m_hTree,FALSE,FALSE);
		XSView_ShowSBarH(m_hTree,FALSE);
		XTree_SetIndentation(m_hTree,0);
		XTree_SetItemHeightDefault(m_hTree,28,54);
		XTree_SetItemTemplateXML(m_hTree,L"xml-template-test\\Tree_Item_friend.xml");
		XTree_SetItemTemplateXMLSel(m_hTree,L"xml-template-test\\Tree_Item_friend_sel.xml");
		m_pTemplate_group=XC_LoadTemplate(XC_TREE,L"xml-template-test\\Tree_Item_group.xml");

		m_hVip=XImage_LoadFile(L"QQImage\\SuperVIP_LIGHT.png");
		m_hQZone=XImage_LoadFile(L"QQImage\\QQZone.png");
		m_hAvatar=XImage_LoadFile(L"QQImage\\avatar.png");
		m_hAvatarLarge=XImage_LoadFile(L"QQImage\\avatar_large.png");

		m_hExpand=XImage_LoadFile(L"Image\\expand.png");
		m_hExpandNo=XImage_LoadFile(L"Image\\expandno.png");
		XImage_EnableAutoDestroy(m_hExpandNo,FALSE);
		XImage_EnableAutoDestroy(m_hExpand,FALSE);
		
		HXCGUI hAdapter=XAdapterTree_Create();
		XTree_BindAdapter(m_hTree,hAdapter);
		XAdapterTree_AddColumn(hAdapter,XC_NAME); //昵称 分组名
		XAdapterTree_AddColumn(hAdapter,XC_NAME2); //签名 
		XAdapterTree_AddColumn(hAdapter,XC_NAME3); //小头像
		XAdapterTree_AddColumn(hAdapter,XC_NAME4); //大头像
		XAdapterTree_AddColumn(hAdapter,XC_NAME5); //VIP 图标
		XAdapterTree_AddColumn(hAdapter,XC_NAME6); //空间图标

		wchar_t  buf[256]={0};
		int nGroupID=0;
		int nItemID=0;
		for (int iGroup=0;iGroup<5;iGroup++)
		{
			wsprintf(buf,L"好友分组-%d",iGroup);
			nGroupID=XAdapterTree_InsertItemText(hAdapter,buf,XC_ID_ROOT,XC_ID_LAST);
			XTree_SetItemHeight(m_hTree,nGroupID,26,26);
			for (int i=0;i<3;i++)
			{
				wsprintf(buf,L"我的好友-%d",i);
				nItemID=XAdapterTree_InsertItemText(hAdapter,buf,nGroupID,XC_ID_LAST);
				XAdapterTree_SetItemTextEx(hAdapter,nItemID,XC_NAME2,L"我的个性签名!");
				XAdapterTree_SetItemImageEx(hAdapter,nItemID,XC_NAME5,m_hVip);
				XAdapterTree_SetItemImageEx(hAdapter,nItemID,XC_NAME6,m_hQZone);
				XAdapterTree_SetItemImageEx(hAdapter,nItemID,XC_NAME3,m_hAvatar);
				XAdapterTree_SetItemImageEx(hAdapter,nItemID,XC_NAME4,m_hAvatarLarge);
			}
		}

		XEle_RegEventCPP(m_hTree,XE_TREE_SELECT,&CMyWindowTree::OnTreeSelect);
		XEle_RegEventCPP(m_hTree,XE_TREE_EXPAND,&CMyWindowTree::OnTreeExpand);

		XEle_RegEventCPP(m_hTree,XE_TREE_TEMP_CREATE,&CMyWindowTree::OnTemplateCreate);
		XEle_RegEventCPP(m_hTree,XE_TREE_TEMP_CREATE_END,&CMyWindowTree::OnTreeTemplateCreateEnd);
		XEle_RegEventCPP(m_hTree,XE_TREE_TEMP_DESTROY,&CMyWindowTree::OnTemplateDestroy);
		XEle_RegEventCPP(m_hTree,XE_TREE_TEMP_ADJUST_COORDINATE,&CMyWindowTree::OnTemplateAdjustCoordinate);

		XWnd_AdjustLayout(m_hWindow);
		XWnd_ShowWindow(m_hWindow,SW_SHOW);
	}
	int OnTreeSelect(int nItemID,BOOL *pbHandled)
	{
		*pbHandled=TRUE;
		return 0;
	}
	int OnTreeExpand(int id,BOOL bExpand,BOOL *pbHandled)
	{
		*pbHandled=TRUE;
		return 0;
	}
	int  OnTemplateCreate(tree_item_i *pItem,BOOL *pbHandled)
	{
		if(XC_ID_ERROR!=XTree_GetFirstChildItem(m_hTree,pItem->nID))
		{
			if(m_pTemplate_group)
				pItem->pInfo=m_pTemplate_group;
		}
		*pbHandled=TRUE;
		return 0;
	}
	int  OnTreeTemplateCreateEnd(tree_item_i* pItem,BOOL *pbHandled)
	{
		HELE hButtonExpand=(HELE)XTree_GetTemplateObject(m_hTree,pItem->nID,1);
		if(hButtonExpand && XC_IsHELE(hButtonExpand))
		{
			if(m_hExpandNo)
			{
			//	XBtn_SetImageLeave(hButtonExpand,m_hExpandNo);
			//	XBtn_SetImageDown(hButtonExpand,m_hExpandNo);
			//	XBtn_SetImageStay(hButtonExpand,m_hExpandNo);
				XBtn_AddBkImage(hButtonExpand,button_state_leave,m_hExpandNo);
				XBtn_AddBkImage(hButtonExpand,button_state_stay,m_hExpandNo);
				XBtn_AddBkImage(hButtonExpand,button_state_down,m_hExpandNo);
			}
			if(m_hExpand)
				//XBtn_SetImageCheck(hButtonExpand,m_hExpand);
				XBtn_AddBkImage(hButtonExpand,button_state_check,m_hExpand);

			XEle_EnableBkTransparent(hButtonExpand,TRUE);
			XBtn_SetStyle(hButtonExpand, button_style_default);
			XEle_EnableFocus(hButtonExpand,FALSE);
			//XEle_EnableBorder(hButtonExpand,FALSE);
		}
		return 0;
	}
	int  OnTemplateDestroy(tree_item_i *pItem,BOOL *pbHandled)
	{
		*pbHandled=TRUE;
		return 0;
	}
	int  OnTemplateAdjustCoordinate(tree_item_i *pItem,BOOL *pbHandled)
	{
		*pbHandled=TRUE;
		return 0;
	}
};

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	XInitXCGUI();
	CMyWindowTree  MyWindow;
	XRunXCGUI();
	MyWindow.Release();
	XExitXCGUI();
	return TRUE;
}
