// LayoutDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "Layout.h"
#include "LayoutDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include "zLayout.h"

CLayoutDlg::CLayoutDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLayoutDlg::IDD, pParent), m_pLayout(NULL)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

CLayoutDlg::~CLayoutDlg()
{
	if (m_pLayout)
	{
		delete m_pLayout;
		m_pLayout = NULL;
	}
}

void CLayoutDlg::CreateLayout()
{
	m_pLayout = new CzLayout(this);

	//users must know that where the control is layed(the start position & how many cols and rows)
	//TODO: now the control must be layed as some arrange(from up to down and from left to right)
	//				it is not a better design, and should be changed.
	m_pLayout->AddThings(&m_StaticCtrls[0] , 0, 0, 1, 1);
	m_pLayout->AddThings(&m_StaticCtrls[5] , 0, 1, 1, 1);
	m_pLayout->AddThings(&m_StaticCtrls[2], 0, 2, 2, 1);
	m_pLayout->AddThings(&m_text, 1, 0, 2, 1);
	m_pLayout->AddThings(&m_StaticCtrls[4], 1, 1, 1, 1);
	m_pLayout->AddThings(&m_StaticCtrls[3], 2, 1, 1, 1);
	m_pLayout->AddThings(&m_StaticCtrls[1], 2, 2, 1, 1);
}

void CLayoutDlg::CreateCtrls()
{
	DWORD dwStyle = WS_VISIBLE | WS_CHILD |SS_LEFT;
	CRect rect(0, 0, 100, 50);
	m_StaticCtrls[0].Create(TEXT("North"), dwStyle, rect, this);
	m_StaticCtrls[1].Create(TEXT("West"), dwStyle, rect, this);
	m_StaticCtrls[2].Create(TEXT("East 1"), dwStyle, rect, this);
	m_StaticCtrls[3].Create(TEXT("East 2"), dwStyle, rect, this);
	m_StaticCtrls[4].Create(TEXT("South"), dwStyle, rect, this);
	
	dwStyle =  WS_VISIBLE | WS_CHILD |  WS_TABSTOP ;
	m_StaticCtrls[5].Create(TEXT("button"), dwStyle, rect, this);
	dwStyle = WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL | WS_TABSTOP;
	m_text.Create(dwStyle, rect, this, IDC_NUMBER);
}

void CLayoutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CLayoutDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


BOOL CLayoutDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	SetIcon(m_hIcon, TRUE);			
	SetIcon(m_hIcon, FALSE);		

	CreateCtrls();
	CreateLayout();

	return TRUE;
}

void CLayoutDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	CDialog::OnSysCommand(nID, lParam);
}

void CLayoutDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this);

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
//
HCURSOR CLayoutDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CLayoutDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	if (m_pLayout)
	{
		m_pLayout->Resize();
	}
}