// ObjectiveListDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed2.h"
#include "ObjectManager.h"
#include "ObjectiveListDlg.h"
#include "ObjectiveProps.h"
#include "GameInterface.h"
#include "EdObjective.h"


/////////////////////////////////////////////////////////////////////////////
// CObjectiveListDlg dialog


CObjectiveListDlg::CObjectiveListDlg(ObjectManager *gobj,UndoCommand **cmd,CWnd* pParent /*=NULL*/)
	: CDialog(CObjectiveListDlg::IDD, pParent)
{
	ObjMan=gobj;
	CmdList=cmd;
	//{{AFX_DATA_INIT(CObjectiveListDlg)
	//}}AFX_DATA_INIT
}


void CObjectiveListDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CObjectiveListDlg)
	DDX_Control(pDX, IDC_OBJLIST, m_ObjList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CObjectiveListDlg, CDialog)
	//{{AFX_MSG_MAP(CObjectiveListDlg)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_WM_CHAR()
	ON_LBN_DBLCLK(IDC_OBJLIST, OnDblclkObjlist)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CObjectiveListDlg message handlers

void CObjectiveListDlg::OnAdd() 
{
	ObjMan->AddObjective();
	RefreshList();	

}

void CObjectiveListDlg::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default

	switch(nChar)
	{
	case VK_DELETE:
	OnDelete();
	break;
	}

  CDialog::OnChar(nChar, nRepCnt, nFlags);
}

void CObjectiveListDlg::OnDblclkObjlist() 
{
	// TODO: Add your control notification handler code here
	EdObjective *obj=(EdObjective *)m_ObjList.GetItemDataPtr(m_ObjList.GetCurSel());	
	if(obj)
	{
	CObjectiveProps dlg(obj,this);

	if(dlg.DoModal()==IDOK)
		RefreshList();
	}
}

BOOL CObjectiveListDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	RefreshList();	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CObjectiveListDlg::RefreshList()
{
	m_ObjList.ResetContent();
	int itm;
	
	for(int i=0;i<ObjMan->ObjectiveList.GetObjectCount();i++)
	{
	EdObjective *obj=(EdObjective *)ObjMan->ObjectiveList.GetObject(i);
	itm=m_ObjList.AddString(obj->GetName());
	m_ObjList.SetItemDataPtr(itm,obj);
	}

}


void CObjectiveListDlg::OnOK() 
{
}


void CObjectiveListDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnOK();	
}

void CObjectiveListDlg::OnDelete() 
{
	// TODO: Add your control notification handler code here
	int sel=m_ObjList.GetCurSel();
	if(sel<0) return;
	EdObjective *obj=(EdObjective *)m_ObjList.GetItemDataPtr(m_ObjList.GetCurSel());	
	if(obj)
	{
	ObjMan->ObjectiveList.RemoveObject(obj);
	delete obj;
	RefreshList();
	}
}
