// TexturePropertyView.cpp : implementation file
//

#include "stdafx.h"
#include "Megatron.h"
#include "TexturePropertyView.h"
#include "MegatronDoc.h"
#include "TexturePropPage.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTexturePropertyView

IMPLEMENT_DYNCREATE(CTexturePropertyView, CView)

CTexturePropertyView::CTexturePropertyView()
{
}

CTexturePropertyView::~CTexturePropertyView()
{
}


BEGIN_MESSAGE_MAP(CTexturePropertyView, CView)
	//{{AFX_MSG_MAP(CTexturePropertyView)
	ON_WM_ERASEBKGND()
	ON_WM_SIZE()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTexturePropertyView drawing

void CTexturePropertyView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CTexturePropertyView diagnostics

#ifdef _DEBUG
void CTexturePropertyView::AssertValid() const
{
	CView::AssertValid();
}

void CTexturePropertyView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTexturePropertyView message handlers

//CDialog dlg;

void CTexturePropertyView::OnInitialUpdate() 
{
	CRect rc;
	GetClientRect(&rc);

	//m_wndPropertySheet.Construct("My Sheet",this);

	CTexturePropPage *page = new CTexturePropPage();
	m_wndPropertySheet.AddPage(page);

	m_wndPropertySheet.Construct("My Sheet",this);
	m_wndPropertySheet.EnableStackedTabs(false);
	m_wndPropertySheet.Create(this, WS_VISIBLE | WS_CHILD | DS_MODALFRAME);
	m_wndPropertySheet.ShowWindow(SW_SHOW);
	m_wndPropertySheet.MoveWindow(rc);

	CTabCtrl *pTab = m_wndPropertySheet.GetTabControl();
	m_wndPropertySheet.GetClientRect(&rc);
	rc.top-=15 * pTab->GetRowCount();
	rc.left+=2;
	pTab->AdjustRect(false,&rc);
	pTab->MoveWindow(&rc);

	m_wndPropertySheet.SetActivePage(0);

	CView::OnInitialUpdate();
}

void CTexturePropertyView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	switch (lHint)
	{
	case UPDATE_HINTFILES:
	case UPDATE_BUILDFILES:
		{

			break;
		}
	case UPDATE_TEXTUREHINT:
		{
			LockWindowUpdate();
			
			CTexturePropPage *activepage = (CTexturePropPage *)m_wndPropertySheet.GetActivePage();
			activepage->m_pPage = NULL;
			for (int x=m_wndPropertySheet.GetPageCount();x;x--)
			{
				CPropertyPage *page = m_wndPropertySheet.GetPage(x-1);
				if (page != activepage)
				{
					m_wndPropertySheet.RemovePage(page);
					delete page;
				}
			}
			m_wndPropertySheet.RemovePage(activepage);
			delete activepage;

			CPtrList *list = (CPtrList *)pHint;
			if (list->GetCount())
			{
				POSITION pos = list->GetHeadPosition();
				while (pos)
				{
					Stuff::Page *page = (Stuff::Page *)list->GetNext(pos);
					CString imagename = page->GetName();

					CTexturePropPage *ppage = new CTexturePropPage();
					ppage->m_psp.dwFlags |= PSP_USETITLE;
					ppage->m_psp.pszTitle = imagename;
					ppage->m_pPage = page;
					m_wndPropertySheet.AddPage(ppage);
				}
			}
			else
			{
				CTexturePropPage *page = new CTexturePropPage();
				m_wndPropertySheet.AddPage(page);
			}
			m_wndPropertySheet.SetActivePage(0);

			UnlockWindowUpdate();
			break;
		}
	case UPDATE_IMAGESELECT:
		{
			Image *pImage = (Image *)pHint;

			CTexturePropPage *page = (CTexturePropPage *)m_wndPropertySheet.GetActivePage();
			//if (page)
			//	page->SetImageData(pImage);

			for (int x=m_wndPropertySheet.GetPageCount();x;x--)
			{
				CTexturePropPage *page = (CTexturePropPage *)m_wndPropertySheet.GetPage(x-1);
				page->SetImageData(pImage);
			}
			break;
		}
	default:
		{
		}
	}
}

BOOL CTexturePropertyView::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	return CView::OnCommand(wParam, lParam);
}

BOOL CTexturePropertyView::OnEraseBkgnd(CDC* pDC) 
{
	return true	;
	return CView::OnEraseBkgnd(pDC);
}

void CTexturePropertyView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	if (m_wndPropertySheet.GetSafeHwnd())
	{
		m_wndPropertySheet.MoveWindow(0,0,cx,cy);
		CTabCtrl *pTab = m_wndPropertySheet.GetTabControl();
		RECT rc;
		m_wndPropertySheet.GetClientRect(&rc);
		rc.top-=15 * pTab->GetRowCount();
		rc.left+=2;
		pTab->AdjustRect(false,&rc);
		pTab->MoveWindow(&rc);
	}		
}

void CTexturePropertyView::OnDestroy() 
{
	CTexturePropPage *activepage = (CTexturePropPage *)m_wndPropertySheet.GetActivePage();
	activepage->m_pPage = NULL;
	for (int x=m_wndPropertySheet.GetPageCount();x;x--)
	{
		CPropertyPage *page = m_wndPropertySheet.GetPage(x-1);
		if (page != activepage)
		{
			m_wndPropertySheet.RemovePage(page);
			delete page;
		}
	}
	m_wndPropertySheet.RemovePage(activepage);
	delete activepage;

	/*
	while (m_wndPropertySheet.GetPageCount())
	{
		CPropertyPage *page = m_wndPropertySheet.GetPage(0);
		//m_wndPropertySheet.RemovePage(page);
		delete page;
	}
*/
	CView::OnDestroy();
}

BOOL CTexturePropertyView::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
{
	return CView::OnNotify(wParam, lParam, pResult);
}
