/**************************************************************************\
*
* Copyright (c)  炫彩界面库. All Rights Reserved.
*
* 炫彩界面库 - 官方网站 : http://www.xcgui.com
*
\**************************************************************************/
#include "stdafx.h"

HWINDOW hWindow=NULL;
HELE hPane1=NULL;
HELE hPane2=NULL;
HELE hPane3=NULL;
HELE hPane4=NULL;

//更新菜单状态
void CALLBACK OnMenu_UpdateUI(HMENUX hMenu,int id)
{
	XTRACE("OnMenu_UpdateUI() id=%d \n",id);

	HELE hPaneCur=NULL;
	switch(id)
	{
	case 301: hPaneCur=hPane1; break;
	case 302: hPaneCur=hPane2; break;
	case 303: hPaneCur=hPane3;break;
	case 304: hPaneCur=hPane4; break;
	default: return ;
	}

	if(NULL==hPaneCur) return ;
	pane_state_ state=XPane_GetState(hPaneCur);
	switch(state)
	{
	case pane_state_lock:
		XMenu_SetItemCheck(hMenu,id,TRUE);
		break;
	case pane_state_dock:
		XMenu_SetItemCheck(hMenu,id,FALSE);
		break;
	case pane_state_hide:
		XMenu_SetItemCheck(hMenu,id,FALSE);
		break;
	case pane_state_float:
		{
			HWINDOW hFloatWnd=XEle_GetHWindow(hPaneCur);
			if(hFloatWnd && XC_FLOATWND==XC_GetObjectType(hFloatWnd))
			{
				if(IsWindowVisible(XEle_GetHWnd(hPaneCur)))
					XMenu_SetItemCheck(hMenu,id,TRUE);
				else
					XMenu_SetItemCheck(hMenu,id,FALSE);
			}
		}break;
	}
	return ;
}

//菜单选择事件,显示隐藏窗格
BOOL CALLBACK OnEventMenuSelect(HELE hEle,HELE hEventEle,int id)
{
	if(hEle!=hEventEle)
		return FALSE;

	if(302==id)
	{
		XPane_Destroy(hPane2);
		XFrameWnd_AdjustLayout(hWindow);
		XWnd_ReDrawWnd(hWindow);
		return FALSE;
	}
	HELE hPaneCur=NULL;
	switch(id)
	{
	case 301: hPaneCur=hPane1; break;
	case 302: hPaneCur=hPane2; break;
	case 303: hPaneCur=hPane3;break;
	case 304: hPaneCur=hPane4; break;
	default: return FALSE;
	}
	if(NULL==hPaneCur) return FALSE;

	HMENUX hMenu=XMenuBar_GetMenu(hEle,2);

	pane_state_ state=XPane_GetState(hPaneCur);
	switch(state)
	{
	case pane_state_lock:
		XPane_Hide(hPaneCur);
		break;
	case pane_state_dock:
		XPane_popup(hPaneCur);
		break;
	case pane_state_hide:
		XPane_Show(hPaneCur);
		break;
	case pane_state_float:
		{
			HWINDOW hFloatWnd=XEle_GetHWindow(hPaneCur);
			if(hFloatWnd && XC_FLOATWND==XC_GetObjectType(hFloatWnd))
			{
				HWND hWnd=XEle_GetHWnd(hPaneCur);
				if(IsWindowVisible(hWnd))
					XWnd_ShowWindow(hFloatWnd,SW_HIDE);
				else
					XWnd_ShowWindow(hFloatWnd,SW_SHOW);
			}
		}break;
	}
	XFrameWnd_AdjustLayout(XEle_GetHWindow(hEle));
	XWnd_ReDrawWnd(hWindow);

	return FALSE;
}

int APIENTRY _tWinMain(HINSTANCE hInstance,  HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	XInitXCGUI();

	//创建窗口
	hWindow=XFrameWnd_CreateWindow(0,0,600,500,L"炫彩界面库-框架窗口");

	HELE hMenuBar=XMenuBar_Create(10,10,300,26,hWindow);
	XMenuBar_AddButton(hMenuBar,L"视图");

	//添加弹出菜单项
	XMenuBar_AddMenuItem(hMenuBar,0,301,L"视图-1");
	XMenuBar_AddMenuItem(hMenuBar,0,302,L"视图-2");
	XMenuBar_AddMenuItem(hMenuBar,0,303,L"视图-3");
	XMenuBar_AddMenuItem(hMenuBar,0,304,L"视图-4");

	HMENUX hMenux=XMenuBar_GetMenu(hMenuBar,0);
	XMenu_SetUserUpdateUI(hMenux,OnMenu_UpdateUI); //更新菜单状态

	//创建窗格
	hPane1=XPane_Create(L"窗格1",hWindow);
	hPane2=XPane_Create(L"窗格2",hWindow);
	hPane3=XPane_Create(L"窗格3",hWindow);
 	hPane4=XPane_Create(L"窗格3",hWindow);

	XFrameWnd_AddPane(hWindow,NULL,hPane1,align_any);        //增加窗格hPane1到框架窗口
	XFrameWnd_AddPane(hWindow,hPane1,hPane4,align_right,50); //增加窗格到右侧

	HPANE_GROUP hGroup=XFrameWnd_MergePane(hWindow,hPane1,hPane2); //将新窗格hPane2合并为组
	XFrameWnd_MergePaneToGroup(hWindow,hGroup,hPane3);             //将新窗格hPane3合并到组

	HELE hEleView=XEle_Create(0,0,100,100,hPane1);
	XEle_SetBkColor(hEleView,RGB(255,0,0));
	XEle_EnableBorder(hEleView,FALSE);
	XPane_SetView(hPane1,hEleView); //为窗格hPane1绑定视图

	XPane_SetFixedSize(hPane4,TRUE); //固定窗格4大小

	XEle_RegisterEvent(hMenuBar,XE_MENUSELECT,OnEventMenuSelect);

	XFrameWnd_AdjustLayout(hWindow); //调整框架窗口布局
	XWnd_ShowWindow(hWindow,SW_SHOW);

	XRunXCGUI();
	return 0;
}
