// PanelHolderDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed2.h"
#include "PanelHolderDlg.h"


/////////////////////////////////////////////////////////////////////////////
// CPanelHolderDlg dialog


CPanelHolderDlg::CPanelHolderDlg(UINT  nIDTemplate,CWnd* pParent /*=NULL*/)
: CDialog(nIDTemplate, pParent)
{
	PanelCount=0;
	//{{AFX_DATA_INIT(CPanelHolderDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CPanelHolderDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPanelHolderDlg)
	// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPanelHolderDlg, CDialog)
//{{AFX_MSG_MAP(CPanelHolderDlg)
// NOTE: the ClassWizard will add message map macros here
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPanelHolderDlg message handlers

void CPanelHolderDlg::OnOK() 
{
	BOOL res=TRUE;
	int i;
	
	for(i=0;i<PanelCount && res;i++) 
		res&=DlgList[i]->ValidateData();
	
	if(res)
	{
		
		for(i=0;i<PanelCount;i++) DlgList[i]->OnOK();
		CDialog::OnOK();
	}
	
}

void CPanelHolderDlg::OnCancel()
{
	int i;
	for(i=0;i<PanelCount;i++) DlgList[i]->OnCancel();
	CDialog::OnCancel();
}

void CPanelHolderDlg::AddPanel(CPanelDlg *dlg)
{
	DlgList[PanelCount++]=dlg;
	ASSERT(PanelCount<MAXPANELS);
}

int CPanelHolderDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	if(PanelCount<=0) return 0;
	int i;
	bool *placeflag=new bool[PanelCount];
	int placed=0;
	int ypos=10,xpos=10,ylimit;
	CRect rct,crct;
	GetClientRect(&crct);
	
	ylimit=crct.bottom-25;
	
	int ymax=0,xmax=0;
	for(i=0;i<PanelCount;i++) 
	{
		DlgList[i]->Create(this);
		DlgList[i]->GetWindowRect(&rct);
		ymax=ymax>rct.Height()?ymax:rct.Height();
		xmax=xmax>rct.Width()?xmax:rct.Width();
		placeflag[i]=false;
	}
	
	switch(LMode)
	{
	case PDL_FIXVERT:
		{
			int willplace,maxwidth=0;
			while(placed<PanelCount)
			{
				willplace=-1;
				
				for(int i=0;i<PanelCount && willplace==-1;i++) 
					if(!placeflag[i])
					{
						DlgList[i]->GetWindowRect(&rct);
						if((ypos+rct.Height())<ylimit)
						{
							willplace=i;
						}
					}
					
					if(willplace==-1)
					{
						xpos+=maxwidth+5;
						ypos=10;
						maxwidth=0;
					}
					else
					{
						DlgList[willplace]->SetWindowPos(NULL,xpos,ypos,-1,-1,SWP_NOSIZE|SWP_NOZORDER|SWP_SHOWWINDOW);
						DlgList[willplace]->GetWindowRect(&rct);
						ypos+=rct.Height();
						placed++;
						placeflag[willplace]=true;
						maxwidth=rct.Width()>maxwidth?rct.Width():maxwidth;
					}
			}
			
			xpos+=maxwidth;
			
			GetWindowRect(&rct);
			SetWindowPos(NULL,-1,-1,xpos+20,rct.Height(),SWP_NOMOVE|SWP_NOZORDER);
		}
		break;
		case PDL_FIXHOR:
		{
			for(int i=0;i<PanelCount;i++) 
			{
			DlgList[i]->SetWindowPos(NULL,xpos,ypos,-1,-1,SWP_NOSIZE|SWP_NOZORDER|SWP_SHOWWINDOW);
			DlgList[i]->GetWindowRect(&rct);
			ypos+=rct.Height();
			}
			GetWindowRect(&rct);
			SetWindowPos(NULL,-1,-1,xmax+20,GetSystemMetrics(SM_CYCAPTION)+ypos+20,SWP_NOMOVE|SWP_NOZORDER);
		}
		break;
	}
	delete placeflag;	
	return 0;
}
