// GridColumnSelectorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AsCfgBuilder.h"
#include "GridColumnSelectorDlg.h"
#include "XmlClassInfo.h"
#include "enums.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGridColumnSelectorDlg dialog


CGridColumnSelectorDlg::CGridColumnSelectorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGridColumnSelectorDlg::IDD, pParent),
	classInfo_(NULL)
{
	//{{AFX_DATA_INIT(CGridColumnSelectorDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CGridColumnSelectorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGridColumnSelectorDlg)
	DDX_Control(pDX, IDC_CBO_FIELDS, cboFields_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CGridColumnSelectorDlg, CDialog)
	//{{AFX_MSG_MAP(CGridColumnSelectorDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGridColumnSelectorDlg message handlers

BOOL CGridColumnSelectorDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	ClassMemberMap* members;
	ClassMemberMap::iterator it;
	CXmlBaseElement* member;
	bool alreadyUsed;
	cmString id;

	members = classInfo_->GetMembers();
	for (it = members->begin(); it != members->end(); it++) {
		member = it->second;
		
		if (member->GetDataType() != enDataType_Class) {
			alreadyUsed = false;
			for (int i = 0; i < usedMembers_->size(); i++) {
				id = (*usedMembers_)[i];
					
				alreadyUsed = (id == member->GetId());
				if (alreadyUsed)
					break;
			}

			if (!alreadyUsed)
				cboFields_.AddString(member->GetName());
		}
	}
	
	if (cboFields_.GetCount()) {
		cboFields_.SetCurSel(0);
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CGridColumnSelectorDlg::SetData(CXmlClassInfo* classInfo, vector<cmString>* usedMembers)
{
	classInfo_ = classInfo;
	usedMembers_ = usedMembers;
}

void CGridColumnSelectorDlg::OnOK() 
{
	cmString fieldName;
	cmString fieldId;
	CXmlBaseElement* member;

	if (cboFields_.GetCurSel() == CB_ERR)
		return;

	cboFields_.GetLBText(cboFields_.GetCurSel(), fieldName);
	
	member = classInfo_->GetMember(fieldName);
	if (member) {
		fieldId = member->GetId();

		usedMembers_->push_back(fieldId);
	}

	CDialog::OnOK();
}
