// 16KPageContainer.cpp: файл реализации
//

#include "stdafx.h"
#include "Defines.h"
#include "16KPageContainer.h"
#include "Screen.h"

// C16KPageContainer

IMPLEMENT_DYNAMIC(C16KPageContainer, CWnd)

C16KPageContainer::C16KPageContainer(UINT nNumPages)
{
	if (nNumPages > C16KP_NUM_PAGES)
	{
		nNumPages = C16KP_NUM_PAGES;
	}

	m_nNumPages = nNumPages;

	for (UINT i = 0; i < C16KP_NUM_PAGES; ++i)
	{
		m_s16kpage[i].pPage = nullptr;
		m_s16kpage[i].pBuffer = nullptr;
		m_s16kpage[i].nBufSize = 0;
		m_s16kpage[i].nPageNum = i;
		m_s16kpage[i].strScrName = _T("");
	}
}

C16KPageContainer::~C16KPageContainer()
{
}

BEGIN_MESSAGE_MAP(C16KPageContainer, CWnd)
	ON_WM_CREATE()
	ON_WM_DESTROY()
END_MESSAGE_MAP()


// обработчики сообщений C16KPageContainer


int C16KPageContainer::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
	{
		return -1;
	}

	int cyCaption = GetSystemMetrics(SM_CYSMCAPTION);
	int cxBorder = GetSystemMetrics(SM_CXFRAME);
	int cyBorder = GetSystemMetrics(SM_CYFRAME);
	CRect rect(0, 0, SCREEN_WIDTH + VW16K_BUTTONPAD_WIDTH + cxBorder * 2, SCREEN_HEIGHT);

	for (UINT i = 0; i < m_nNumPages; ++i)
	{
		m_s16kpage[i].pPage = new C16KPageView(m_s16kpage[i].pBuffer, m_s16kpage[i].nBufSize,
		                                       m_s16kpage[i].nPageNum, m_s16kpage[i].strScrName);

		if (m_s16kpage[i].pPage)
		{
			if (!m_s16kpage[i].pPage->Create(nullptr, _T("BK16KPageView_") + m_s16kpage[i].strScrName,
			                                 WS_VISIBLE | WS_CHILD, rect, this, 0))
			{
				TRACE(_T("Не удалось создать вид ") + (m_s16kpage[i].strScrName) + _T("\n"));
				return -1; // не удалось создать
			}

			CRect r;
			m_s16kpage[i].pPage->GetWindowRect(r);
			rect.top += r.Height();
			rect.bottom += r.Height();
		}
	}

	MoveWindow(0, 0, rect.Width(), rect.top + cyCaption + cyBorder * 2, FALSE);
	return 0;
}

void C16KPageContainer::SetBuffer_Param(UINT nPageNum, BYTE *buffer, size_t size, UINT num, CString &strName)
{
	if (nPageNum < C16KP_NUM_PAGES)
	{
		m_s16kpage[nPageNum].pBuffer = buffer;
		m_s16kpage[nPageNum].nBufSize = size;
		m_s16kpage[nPageNum].nPageNum = num;
		m_s16kpage[nPageNum].strScrName = strName;
	}
}

void C16KPageContainer::Draw16KPages()
{
	m_s16kpage[C16KP_FIRST_PAGE].pPage->Draw16KView();

	if (m_nNumPages > C16KP_SECOND_PAGE)
	{
		m_s16kpage[C16KP_SECOND_PAGE].pPage->Draw16KView();
	}
}

void C16KPageContainer::OnDestroy()
{
	for (UINT i = 0; i < m_nNumPages; ++i)
	{
		C16KPageView *pv = m_s16kpage[i].pPage;

		if (pv)
		{
			pv->DestroyWindow();
			delete pv;
		}

		m_s16kpage[i].pPage = nullptr;
	}

	CWnd::OnDestroy();
}
