// CppGridDialogGenerator.cpp: implementation of the CCppGridDialogGenerator class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CSharpGridDialogGenerator.h"
#include "XmlClassInfo.h"
#include "XmlMemberInfo.h"
#include "XmlFormInfo.h"
#include "XmlBaseFormItem.h"
#include "XmlFormItem_Static.h"
#include "XmlFormItem_TextBox.h"
#include "XmlFormItem_CheckBox.h"
#include "XmlFormItem_ComboBox.h"
#include "XmlProjectSettings.h"
#include "XmlGuiCategoryInfoList.h"
#include "XmlGuiCategoryInfo.h"
#include "XmlGuiCategoryField.h"
#include "XmlMemberValidation_StaticEnum.h"
#include "XmlMemberValidation_StaticEnumItem.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCSharpGridDialogGenerator::CCSharpGridDialogGenerator()
{

}

CCSharpGridDialogGenerator::~CCSharpGridDialogGenerator()
{

}

void CCSharpGridDialogGenerator::Generate()
{
	//GenerateHeader();
	//GenerateImplementation();
}

/****
void CCppGridDialogGenerator::GenerateHeader()
{
	cmString fileName;
	cmString pathName;

	// header file generation
	//
	headCode_ = _T("");
	Head_generateHeader();
	Head_generateIfDefBegin();
	Head_generateIncludeSection();
	Head_generateForwardDeclarations();
	Head_generateDefineSection();
	Head_generateClassBegin();
	Head_generateConstructor();
	Head_generateDestructor();
	Head_generatePublicMethods();
	Head_generateDialogData();
	Head_generateOverrides();
	Head_generateMessageMap();
	Head_generateProtectedMembers();
	Head_generateUpdateWindowSize();
	Head_generateSetupRow();
	Head_generateDataMembers();
	Head_generateClassEnd();
	Head_generateIfDefEnd();

	// save implementation file
	//
	pathName = BuildGeneratedSourcePath() + _T("include\\");
	fileName = GetDlgName() + _T(".h");
	
	SaveFile(pathName + fileName, headCode_);

	hGeneratedFiles_[fileName] = pathName + fileName;
}

void CCppGridDialogGenerator::GenerateImplementation()
{
	cmString fileName;
	cmString pathName;

	// generate implementation file
	//
	implCode_ = _T("");
	Impl_generateHeader();
	Impl_generateIncludeSection();
	Impl_generateDebugSymbols();
	Impl_generateMainTitle();
	Impl_generateConstructor();
	Impl_generateDestructor();
	Impl_generatePublicMethods();
	Impl_generateMessageMap();
	Impl_generateOnInitDialog();
	Impl_generateOnSize();
	Impl_generateProtectedMembers();
	Impl_generateUpdateWindowSize();
	Impl_generateSetupRow();

	// save implementation file
	//
	pathName = BuildGeneratedSourcePath() + _T("src\\");
	fileName = GetDlgName() + _T(".cpp");
	SaveFile(pathName + fileName, implCode_);

	cppGeneratedFiles_[fileName] = pathName + fileName;
}

void CCppGridDialogGenerator::Head_generateHeader()
{
	headCode_ += _T("// ") + GetDlgName() + _T(".h : header file") + xCrLf;
	headCode_ += _T("//") + xCrLf + xCrLf;
	headCode_ += GetGenerationTimeStamp();
}

void CCppGridDialogGenerator::Head_generateIfDefBegin()
{
	headCode_ += _T("#if !defined(") + DefineOf(GetDlgName()) + _T(")") + xCrLf;
	headCode_ += _T("#define ") + DefineOf(GetDlgName()) + xCrLf + xCrLf;
}

void CCppGridDialogGenerator::Head_generateIncludeSection()
{
	headCode_ += _T("#include \"stdafx.h\"") + xCrLf;
	headCode_ += _T("#include \"resource.h\"") + xCrLf;
	headCode_ += _T("#include \"") + GetResourceFileName() + _T(".h\"") + xCrLf;
	headCode_ += _T("#include \"") + GetClassName() + _T(".h\"") + xCrLf;
	headCode_ += xCrLf;
}

void CCppGridDialogGenerator::Head_generateForwardDeclarations()
{
	headCode_ += _T("// forward declarations") + xCrLf;
	headCode_ += _T("//") + xCrLf;
	headCode_ += _T("class ") + GetClassName() + GetCardinalityPostfix(classInfo_) + _T(";") + xCrLf;
	headCode_ += _T("class CAsTable;") + xCrLf;
	headCode_ += _T("class CAsTableRow;") + xCrLf;
	headCode_ += xCrLf;
}

void CCppGridDialogGenerator::Head_generateClassBegin()
{
	headCode_ += _T("/////////////////////////////////////////////////////////////////////////////") + xCrLf;
	headCode_ += _T("// ") + GetDlgName() + _T(" dialog") + xCrLf + xCrLf;

	headCode_ += _T("class ") + GetDlgName() + _T(" : public CDialog")  + xCrLf;
	headCode_ += _T("{")  + xCrLf + xCrLf;
}

void CCppGridDialogGenerator::Head_generateConstructor()
{
	headCode_ += _T("public:") + xCrLf;
	headCode_ += xTab + GetDlgName() + _T("(CWnd* pParent = NULL);") + xCrLf;
}

void CCppGridDialogGenerator::Head_generateDestructor()
{
	headCode_ += xTab + _T("virtual ~") + GetDlgName() + _T("();") + xCrLf + xCrLf;
}

void CCppGridDialogGenerator::Head_generatePublicMethods()
{
	headCode_ += xTab + _T("public:") + xCrLf;
	headCode_ += xTab + _T("virtual void SetData(") + GetClassName() + GetCardinalityPostfix(classInfo_) + _T("* ") + GetCamelCase(classInfo_->GetName()) + _T(");") + xCrLf;
	headCode_ += xTab + _T("virtual void StoreData();") + xCrLf;
	headCode_ += xCrLf;
}

void CCppGridDialogGenerator::Head_generateDialogData()
{
	// dialog data section
	//
	headCode_ += _T("// Dialog Data") + xCrLf;
	headCode_ += xTab + _T("//{{AFX_DATA(") +  GetDlgName() + _T(")") + xCrLf;
	headCode_ += _T("enum { IDD = IDD_CFG_TEMPLATE };")  + xCrLf;
	headCode_ += xTab + xTab + _T("// NOTE: the ClassWizard will add data members here")  + xCrLf;
	headCode_ += xTab + _T("//}}AFX_DATA")  + xCrLf + xCrLf;
}

void CCppGridDialogGenerator::Head_generateOverrides()
{
	headCode_ += _T("// Overrides") + xCrLf;
	headCode_ += xTab + _T("// ClassWizard generated virtual function overrides") + xCrLf;
	headCode_ += xTab + _T("//{{AFX_VIRTUAL(") + GetDlgName() + _T(")") + xCrLf;
	headCode_ += xTab + _T("protected:") + xCrLf;
	headCode_ += xTab + _T("//virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support") + xCrLf;
	headCode_ += xTab + _T("//}}AFX_VIRTUAL") + xCrLf + xCrLf;
}

void CCppGridDialogGenerator::Head_generateMessageMap()
{
	headCode_ += _T("// Implementation") + xCrLf;
	headCode_ += _T("//") + xCrLf;
	headCode_ += _T("protected:") + xCrLf;
	headCode_ += xCrLf;
	headCode_ += xTab + _T("// Generated message map functions") + xCrLf;
	headCode_ += xTab + _T("//{{AFX_MSG(") + GetDlgName() +_T(")") + xCrLf;
	headCode_ += xTab + _T("virtual BOOL OnInitDialog();")+ xCrLf;
	headCode_ += xTab + _T("afx_msg void OnSize(UINT nType, int cx, int cy);") + xCrLf;
	headCode_ += xTab + _T("//}}AFX_MSG") + xCrLf;
	headCode_ += xTab + _T("DECLARE_MESSAGE_MAP()") + xCrLf;
	headCode_ += xCrLf;
}

void CCppGridDialogGenerator::Head_generateProtectedMembers()
{
	headCode_ += xTab + _T("// Implementation") + xCrLf;
	headCode_ += xTab + _T("//") + xCrLf;
	headCode_ += xTab + _T("protected:") + xCrLf;
	headCode_ += xTab + xTab +_T("DECLARE_DYNCREATE(") + GetDlgName() + _T(");") + xCrLf;
	headCode_ += xCrLf;
	headCode_ += xTab + xTab + _T("virtual void DisplayData();") + xCrLf;
	headCode_ += xTab + xTab + _T("virtual void OnOK();") + xCrLf;
	headCode_ += xTab + xTab + _T("virtual void OnCancel();") + xCrLf;
	if (classInfo_->GetGridInfo()->GetButtonBarEnabled()) { 
		headCode_ += xTab + xTab + _T("virtual void OnAppend();") + xCrLf;
		headCode_ += xTab + xTab +_T("virtual void OnInsert();") + xCrLf;
		headCode_ += xTab + xTab + _T("virtual void OnDelete();") + xCrLf;
	}
	headCode_ += xCrLf;
}

void CCppGridDialogGenerator::Head_generateUpdateWindowSize()
{
	headCode_ += xTab + _T("void UpdateWindowSize();") + xCrLf;
	headCode_ += xCrLf;
}

void CCppGridDialogGenerator::Head_generateDataMembers()
{
	headCode_ += xTab + _T("// data members") + xCrLf;
	headCode_ += xTab + _T("//") + xCrLf;
	headCode_ += xTab + _T("protected:") + xCrLf;
	headCode_ += xTab + GetClassName() + GetCardinalityPostfix(classInfo_) + _T("* ") + GetCamelCase(classInfo_->GetName()) + GetCardinalityPostfix(classInfo_) + _T("_;") + xCrLf;
	headCode_ += xTab + _T("CAsTable* table_;")  + xCrLf;
	if (classInfo_->GetGridInfo()->GetButtonBarEnabled()) {
		headCode_ += xTab + _T("CButton cmdAppend_;") + xCrLf; 
		headCode_ += xTab + _T("CButton cmdInsert_;") + xCrLf; 
		headCode_ += xTab + _T("CButton cmdDelete_;") + xCrLf; 
	}
	headCode_ += xTab + _T("bool initialized_;") + xCrLf;
	headCode_ += xTab + _T("CFont font_;") + xCrLf;

	headCode_ += xCrLf;
}

void CCppGridDialogGenerator::Head_generateClassEnd()
{
	headCode_ += _T("};") + xCrLf + xCrLf;
}

void CCppGridDialogGenerator::Head_generateIfDefEnd()
{
	headCode_ += _T("#endif // !defined(") + DefineOf(GetDlgName()) + _T(")") + xCrLf + xCrLf;
}

///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// IMPLEMENTATION FILE GENERATORS
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

void CCppGridDialogGenerator::Impl_generateHeader()
{
	implCode_ += _T("// ") + GetDlgName() + _T(".cpp : implementation file") + xCrLf;
	implCode_ += _T("//") + xCrLf;
	implCode_ += GetGenerationTimeStamp();
	implCode_ += xCrLf;
}

void CCppGridDialogGenerator::Impl_generateIncludeSection()
{
	implCode_ += _T("#include \"stdafx.h\"") + xCrLf;
	implCode_ += _T("#include \"") + GetDlgName() + _T(".h\"") + xCrLf;
	implCode_ += _T("#include \"") + GetClassName() + GetCardinalityPostfix(classInfo_) + _T(".h\"") + xCrLf;
	implCode_ += _T("#include \"resource.h\"") + xCrLf;
	implCode_ += _T("#include \"AsTable.h\"") + xCrLf;
	implCode_ += _T("#include \"AsTableRow.h\"") + xCrLf;
	implCode_ += _T("#include \"AsBaseCell.h\"") + xCrLf;
	implCode_ += _T("#include \"AsGenericTextCell.h\"") + xCrLf;
	implCode_ += _T("#include \"AsNumericTextCell.h\"") + xCrLf;
	implCode_ += _T("#include \"AsHexadecimalTextCell.h\"") + xCrLf;
	implCode_ += _T("#include \"AsCheckBoxCell.h\"") + xCrLf;
	implCode_ += _T("#include \"AsComboBoxCell.h\"") + xCrLf;
	implCode_ += xCrLf;
}

void CCppGridDialogGenerator::Impl_generateDebugSymbols()
{
	//// implCode_ += "#ifdef _DEBUG" + xCrLf;
	//// implCode_ += "#define new DEBUG_NEW" + xCrLf;
	//// implCode_ += "#undef THIS_FILE" + xCrLf;
	//// implCode_ += "static char THIS_FILE[] = __FILE__;" + xCrLf;
	//// implCode_ += "#endif" + xCrLf + xCrLf;
	
}

void CCppGridDialogGenerator::Impl_generateMainTitle()
{
	implCode_ += _T("/////////////////////////////////////////////////////////////////////////////") + xCrLf;
	implCode_ += _T("// ") + GetClassName() + _T(" dialog") + xCrLf + xCrLf;

	implCode_ += _T("IMPLEMENT_DYNCREATE(") + GetDlgName() + _T(", CDialog)") + xCrLf;
	implCode_ += xCrLf;
	if (classInfo_->GetGridInfo()->GetButtonBarEnabled()) {
		implCode_ += _T("#ifdef BUTTON_WIDTH") + xCrLf;
		implCode_ += _T("#undef BUTTON_WIDTH") + xCrLf;
		implCode_ += _T("#endif BUTTON_WIDTH") + xCrLf;
		implCode_ += _T("#define BUTTON_WIDTH     75") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += _T("#ifdef BUTTON_HEIGHT") + xCrLf;
		implCode_ += _T("#undef BUTTON_HEIGHT") + xCrLf;
		implCode_ += _T("#endif BUTTON_HEIGHT") + xCrLf;
		implCode_ += _T("#define BUTTON_HEIGHT    23") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += _T("#ifdef BUTTON_DISTANCE") + xCrLf;
		implCode_ += _T("#undef BUTTON_DISTANCE") + xCrLf;
		implCode_ += _T("#endif BUTTON_DISTANCE") + xCrLf;
		implCode_ += _T("#define BUTTON_DISTANCE  5") + xCrLf;
		implCode_ += xCrLf;
		
		implCode_ += _T("#define IDC_BTN_APPEND    1000") + xCrLf;
		implCode_ += _T("#define IDC_BTN_INSERT    1001") + xCrLf;
		implCode_ += _T("#define IDC_BTN_DELETE    1002") + xCrLf;
	} else {
		implCode_ += _T("#ifdef BUTTON_WIDTH") + xCrLf;
		implCode_ += _T("#undef BUTTON_WIDTH") + xCrLf;
		implCode_ += _T("#endif BUTTON_WIDTH") + xCrLf;
		implCode_ += _T("#define BUTTON_WIDTH     0") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += _T("#ifdef BUTTON_HEIGHT") + xCrLf;
		implCode_ += _T("#undef BUTTON_HEIGHT") + xCrLf;
		implCode_ += _T("#endif BUTTON_HEIGHT")+ xCrLf;
		implCode_ += _T("#define BUTTON_HEIGHT    0") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += _T("#ifdef BUTTON_DISTANCE") + xCrLf;
		implCode_ += _T("#undef BUTTON_DISTANCE") + xCrLf;
		implCode_ += _T("#endif BUTTON_DISTANCE") + xCrLf;
		implCode_ += _T("#define BUTTON_DISTANCE  0") + xCrLf;		
		implCode_ += xCrLf;
	}

	implCode_ += _T("#define HEADER_BACK_COLOR ::GetSysColor(COLOR_BTNFACE)") + xCrLf;
	implCode_ += _T("#define HEADER_FORE_COLOR ::GetSysColor(COLOR_BTNTEXT)") + xCrLf;

	implCode_ += xCrLf;
}

void CCppGridDialogGenerator::Impl_generateConstructor()
{
	implCode_ += GetDlgName() +_T("::") + GetDlgName() + _T("(CWnd* pParent ):") + xCrLf;
	implCode_ += xTab + _T("CDialog(") + GetDlgName() + _T("::IDD, pParent),") + xCrLf;
	implCode_ += xTab + GetCamelCase(classInfo_->GetName()) + GetCardinalityPostfix(classInfo_) + _T("_(NULL),") + xCrLf;
	implCode_ += xTab + _T("initialized_(false)") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("//{{AFX_DATA_INIT(") + GetDlgName() + _T(")") + xCrLf;
	implCode_ += xTab + xTab + _T("// NOTE: the ClassWizard will add member initialization here") + xCrLf;
	implCode_ += xTab + _T("//}}AFX_DATA_INIT") + xCrLf;
	implCode_ += xTab + _T("table_ = new CAsTable;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;
}

void CCppGridDialogGenerator::Impl_generateDestructor()
{
	implCode_ += GetDlgName() + _T("::~") + GetDlgName() + _T("()") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("delete table_;") + xCrLf;
	implCode_ += _T("}") + xCrLf + xCrLf;
}

void CCppGridDialogGenerator::Impl_generatePublicMethods()
{
	// SetData
	//
	cmString p;

	p = GetCardinalityPostfix(classInfo_);
	implCode_ += _T("void ") + GetDlgName() + _T("::SetData(") + GetClassName() + p + _T("* ") + GetCamelCase(classInfo_->GetName()) + p + _T(")") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("if (!initialized_)") + xCrLf;
	implCode_ += xTab + xTab + _T("OnInitDialog();") + xCrLf;
	
	implCode_ += xCrLf;
	implCode_ += xTab + GetCamelCase(classInfo_->GetName()) + p + _T("_ = ") + GetCamelCase(classInfo_->GetName()) + p +_T(";") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("// display data") + xCrLf;
	implCode_ += xTab + _T("//") + xCrLf;
	implCode_ += xTab + _T("DisplayData();") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	// StoreData
	//
	implCode_ += _T("void ") + GetDlgName() + _T("::StoreData()") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	
	implCode_ += xTab + _T("if (!GetSafeHwnd() || ") + GetCamelCase(classInfo_->GetName()) + p + _T("_ == NULL)") + xCrLf;
	implCode_ += xTab + xTab + _T("return;") + xCrLf;
	implCode_ += xCrLf;
	
	cmString arrayMemberName;


	implCode_ += xTab + _T("CString tmp;") + xCrLf;
	implCode_ += xTab + _T("CAsNumericTextCell* numCell = NULL;") + xCrLf;
	implCode_ += xTab + _T("CAsHexadecimalTextCell* hexCell = NULL;") + xCrLf;
	implCode_ += xTab + _T("CAsCheckBoxCell* chkCell = NULL;") + xCrLf;
	implCode_ += xTab + _T("CAsGenericTextCell* textCell = NULL;") + xCrLf;
	implCode_ += xTab + _T("CAsComboBoxCell* comboCell = NULL;") + xCrLf;
	implCode_ += xTab + _T("CAsTableRow* row;") + xCrLf;
	implCode_ += xTab + _T("int i;") + xCrLf;
	implCode_ += xTab + _T("int arrayIndex = 0;") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("for (i = table_->GetFixedRowsNumber(); i < table_->GetRowCount(); i++) {") + xCrLf;
	implCode_ += xTab + xTab + _T("row = table_->GetRow(i);") + xCrLf;
	arrayMemberName = GetCamelCase(classInfo_->GetName()) + GetCardinalityPostfix(classInfo_) + _T("_");
	implCode_ += xTab + xTab + _T("if (arrayIndex < ") + arrayMemberName + _T("->GetSize()) {") + xCrLf;
	
	implCode_ += xTab + xTab + xTab + GetClassName(classInfo_->GetName()) + _T("& item = (*") + arrayMemberName + _T(")[arrayIndex];") + xCrLf;
	implCode_ += xCrLf;
	Impl_GenerateRowStore();
		
	implCode_ += xTab + xTab + _T("} else {") + xCrLf;
	implCode_ += xTab + xTab + xTab + GetClassName(classInfo_->GetName()) + _T(" item;") + xCrLf;
	implCode_ += xCrLf;
	Impl_GenerateRowStore();
	implCode_ += xTab + xTab + xTab + arrayMemberName + _T("->Insert(arrayIndex, item);") + xCrLf;
	implCode_ += xTab + xTab + _T("}") + xCrLf;
	implCode_ += xTab + xTab + _T("arrayIndex++;") + xCrLf;
	implCode_ += xTab + _T("}") + xCrLf;
	implCode_ += xCrLf;
	///////////////////////
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;
}

void CCppGridDialogGenerator::Impl_GenerateRowStore()
{
	cmString className;
	cmString memberName;
	CXmlGuiGridInfo* gridInfo;
	CXmlMemberInfo* member;
	GridColumns* columns;
	int firstColIndex = 0;
	cmString colIndexStr;
	int colIndex = 0;
	CString tmp;

	gridInfo = classInfo_->GetGridInfo();
	columns = gridInfo->GetColumns();

	if (gridInfo->GetColumnCountEnabled())
		firstColIndex++;

	className = _T("(*") + GetCamelCase(classInfo_->GetName()) + GetCardinalityPostfix(classInfo_) + _T("_)[i].") ;
	for (int i = 0; i < columns->size(); i++) {
		member = dynamic_cast<CXmlMemberInfo*>(classInfo_->GetMemberById((*columns)[i]));
		if (member == NULL)
			continue;
		
		memberName = _T("Get") + member->GetName() + _T("()");

		if (member->IsHidden()) {
			implCode_ += xTab + xTab + xTab + _T("// field \"") + member->GetName() + _T("\" is hidden") + xCrLf;
			implCode_ += xTab + xTab + xTab + _T("//") + xCrLf;		
			
			continue;
		}
		
		implCode_ += xTab + xTab + xTab + _T("// field \"") + member->GetName() + _T("\"") + xCrLf;
		implCode_ += xTab + xTab + xTab + _T("//") + xCrLf;

		tmp.Format(_T("%d"), colIndex + firstColIndex);
		colIndexStr = (const wchar_t*)tmp;
		if (member->GetValidationInfo()->GetValidationMode() == enValidationMode_StaticEnum) {	
			implCode_ += xTab + xTab + xTab + _T("comboCell = dynamic_cast<CAsComboBoxCell*>(row->GetCell(") + colIndexStr + _T("));") + xCrLf;
			implCode_ += xTab + xTab + xTab + _T("comboCell->KillFocus();") + xCrLf;
			implCode_ += xTab + xTab + xTab + _T("item.Set") + member->GetName() + _T("((") + GetClassName(classInfo_->GetName()) + _T("::en") + member->GetName() + _T(")comboCell->GetSelectedItemKey());") + xCrLf;
		} else if (member->GetDataType() == enDataType_Long) {
			switch (member->GetLongMemberGuiInfo()->GetDisplayMode()) {
				case enLongMemberDisplayMode_Decimal:
					implCode_ += xTab + xTab + xTab + _T("numCell = dynamic_cast<CAsNumericTextCell*>(row->GetCell(") + colIndexStr + _T("));") + xCrLf;
					implCode_ += xTab + xTab + xTab + _T("numCell->KillFocus();") + xCrLf;
					implCode_ += xTab + xTab + xTab + _T("item.Set") + member->GetName() + _T("(numCell->GetValue());") + xCrLf;
					break;

				case enLongMemberDisplayMode_Hex:
					implCode_ += xTab + xTab + xTab + _T("hexCell = dynamic_cast<CAsHexadecimalTextCell*>(row->GetCell(") + colIndexStr + _T("));") + xCrLf;
					implCode_ += xTab + xTab + xTab + _T("hexCell->KillFocus();") + xCrLf;
					implCode_ += xTab + xTab + xTab + _T("item.Set") + member->GetName() + _T("(hexCell->GetValue());") + xCrLf;
					break;
			}			
		} else if (member->GetDataType() == enDataType_Double) {
			implCode_ += xTab + xTab + xTab + _T("numCell = dynamic_cast<CAsNumericTextCell*>(row->GetCell(") + colIndexStr + _T("));") + xCrLf;
			implCode_ += xTab + xTab + xTab + _T("numCell->KillFocus();") + xCrLf;
			implCode_ += xTab + xTab + xTab + _T("item.Set") + member->GetName() + _T("(numCell->GetValue());") + xCrLf;
		} else if (member->GetDataType() == enDataType_String) {
			implCode_ += xTab + xTab + xTab + _T("textCell = dynamic_cast<CAsGenericTextCell*>(row->GetCell(") + colIndexStr + _T("));") + xCrLf;
			implCode_ += xTab + xTab + xTab + _T("textCell->KillFocus();") + xCrLf;
			implCode_ += xTab + xTab + xTab + _T("item.Set") + member->GetName() + _T("(textCell->GetText());") + xCrLf;
		} else if (member->GetDataType() == enDataType_Boolean) {
			implCode_ += xTab + xTab + xTab + _T("chkCell = dynamic_cast<CAsCheckBoxCell*>(row->GetCell(") + colIndexStr + _T("));") + xCrLf;
			implCode_ += xTab + xTab + xTab + _T("chkCell->KillFocus();") + xCrLf;
			implCode_ += xTab + xTab + xTab + _T("item.Set") + member->GetName() + _T("(chkCell->GetChecked());")+ xCrLf;
		}

		colIndex++;
		implCode_ += xCrLf;
	}
	

}

void CCppGridDialogGenerator::Impl_generateMessageMap()
{
	implCode_ += _T("BEGIN_MESSAGE_MAP(") + GetDlgName() + _T(", CDialog)") + xCrLf;
	implCode_ += xTab + _T("//{{AFX_MSG_MAP(getDlgName())") + xCrLf;
	//implCode_ += xTab + "ON_WM_CREATE()" + xCrLf;
	implCode_ += xTab + _T("ON_WM_SIZE()") + xCrLf;
	
	if (classInfo_->GetGridInfo()->GetButtonBarEnabled()) {
		implCode_ += xTab + _T("ON_BN_CLICKED(IDC_BTN_APPEND, OnAppend)") + xCrLf;
		implCode_ += xTab + _T("ON_BN_CLICKED(IDC_BTN_INSERT, OnInsert)") + xCrLf;
		implCode_ += xTab + _T("ON_BN_CLICKED(IDC_BTN_DELETE, OnDelete)") + xCrLf;
	}
	
	implCode_ += xTab + _T("//}}AFX_MSG_MAP") + xCrLf;
	implCode_ += _T("END_MESSAGE_MAP()") + xCrLf + xCrLf;
}

void CCppGridDialogGenerator::Impl_generateOnInitDialog()
{
	implCode_ += _T("/////////////////////////////////////////////////////////////////////////////") + xCrLf;
	implCode_ += _T("// ") + GetDlgName() + _T(" message handlers") + xCrLf + xCrLf;

	implCode_ += _T("BOOL ") + GetDlgName() + _T("::OnInitDialog()") + xCrLf ;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("initialized_ = true;") + xCrLf;
	implCode_ += xTab + _T("CDialog::OnInitDialog();") + xCrLf + xCrLf;
	
	implCode_ += xTab + _T("CRect rc;") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("GetClientRect(rc);") + xCrLf;

	implCode_ += xTab + _T("font_.CreateFont(-11, ") + xCrLf;
	implCode_ += xTab + _T("                 0,                       ") + xCrLf;
	implCode_ += xTab + _T("                 0,					   ") + xCrLf;
	implCode_ += xTab + _T("                 0,					   ") + xCrLf;
	implCode_ += xTab + _T("                 FW_NORMAL,			   ") + xCrLf;
	implCode_ += xTab + _T("                 0,					   ") + xCrLf;
	implCode_ += xTab + _T("                 0,					   ") + xCrLf;
	implCode_ += xTab + _T("                 0,					   ") + xCrLf;
	implCode_ += xTab + _T("                 ANSI_CHARSET,			   ") + xCrLf;
	implCode_ += xTab + _T("                 OUT_DEFAULT_PRECIS,	   ") + xCrLf;
	implCode_ += xTab + _T("                 CLIP_DEFAULT_PRECIS,	   ") + xCrLf;
	implCode_ += xTab + _T("                 DEFAULT_QUALITY,		   ") + xCrLf;
	implCode_ += xTab + _T("                 DEFAULT_PITCH,		   ") + xCrLf;
	implCode_ += xTab + _T("                 _T(\"tahoma\"));") + xCrLf;


	implCode_ += xTab + _T("ModifyStyle(WS_POPUP, WS_CHILD | WS_CLIPCHILDREN);") + xCrLf;
	implCode_ += xCrLf;

	if (classInfo_->GetGridInfo()->GetButtonBarEnabled()) {
		implCode_ += xTab + _T("// create buttons") + xCrLf;
		implCode_ += xTab + _T("//") + xCrLf;
		implCode_ += xTab + _T("cmdAppend_.Create(_T(\"Append\"), WS_VISIBLE | BS_PUSHBUTTON, CRect(0, 0, 0, 0), this, IDC_BTN_APPEND);") + xCrLf;
		implCode_ += xTab + _T("cmdAppend_.SetFont(&font_);") + xCrLf;
		implCode_ += xTab + _T("cmdInsert_.Create(_T(\"Insert\"), WS_VISIBLE | BS_PUSHBUTTON, CRect(0, 0, 0, 0), this, IDC_BTN_INSERT);") + xCrLf;
		implCode_ += xTab + _T("cmdInsert_.SetFont(&font_);") + xCrLf;
		implCode_ += xTab + _T("cmdDelete_.Create(_T(\"Delete\"), WS_VISIBLE | BS_PUSHBUTTON, CRect(0, 0, 0, 0), this, IDC_BTN_DELETE);") + xCrLf;
		implCode_ += xTab + _T("cmdDelete_.SetFont(&font_);") + xCrLf;
	}

	// create table
	//
	GridColumns* columns;
	int columnsCount;
	int firstColumnIndex = 0;
	TCHAR tmp[10];

	columns = classInfo_->GetGridInfo()->GetColumns();
	columnsCount = columns->size();
	if (classInfo_->GetGridInfo()->GetColumnCountEnabled())
		firstColumnIndex = 1;
	columnsCount += firstColumnIndex;

	implCode_ += xTab + _T("// create and configure table") + xCrLf;
	implCode_ += xTab + _T("//") + xCrLf;
	implCode_ += xTab + _T("CAsTableRow* newRow;") + xCrLf;
	implCode_ += xTab + _T("CAsBaseCell* baseCell;") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("table_->Create(rc, this, WS_CHILD | WS_VISIBLE, -1);") + xCrLf;
	implCode_ += xTab + _T("table_->SetColumnsNumber(") + _itow(columnsCount, tmp, 10) + _T(");") + xCrLf;
	if (classInfo_->GetGridInfo()->GetColumnCountEnabled())
		implCode_ += xTab + _T("table_->SetFixedColumnsNumber(1);") + xCrLf;
	implCode_ += xTab + _T("table_->SetFixedRowsNumber(1);") + xCrLf;
	implCode_ += xCrLf;	
	// set headers name
	//
	implCode_ += xTab + _T("newRow = table_->AppendRow(") + _itow(columnsCount, tmp, 10) + _T(");") + xCrLf;
	if (classInfo_->GetGridInfo()->GetColumnCountEnabled()) {
		implCode_ += xTab + _T("baseCell = newRow->CreateCell(0, enTableCellType_Caption);") + xCrLf;
		implCode_ += xTab + _T("baseCell->SetBackColor(HEADER_BACK_COLOR);") + xCrLf;
		implCode_ += xTab + _T("baseCell->SetForeColor(HEADER_FORE_COLOR);") + xCrLf;
		implCode_ += xCrLf;
	}

	for (int i = 0; i < columns->size(); i++) {
		cmString id;
		CXmlMemberInfo* member;

		id = (*columns)[i];
		member = dynamic_cast<CXmlMemberInfo*>(classInfo_->GetMemberById(id));
		if (member == NULL || member->IsHidden())
			continue;
		
		//implCode_ += xTab + "grid_.GetCell(0, " + itoa(firstColumnIndex + i, tmp, 10) + ")->SetText(\"" + member->GetCaption() + "\");" + xCrLf;

		implCode_ += xTab + _T("baseCell = newRow->CreateCell(") + _itow(i + 1, tmp, 10) + _T(", enTableCellType_Caption);") + xCrLf;
		implCode_ += xTab + _T("baseCell->SetBackColor(HEADER_BACK_COLOR);") + xCrLf;
		implCode_ += xTab + _T("baseCell->SetForeColor(HEADER_FORE_COLOR);") + xCrLf;
		implCode_ += xTab + _T("baseCell->SetText(_T(\"") + member->GetCaption() + _T("\"));") + xCrLf;
		implCode_ += xCrLf;
	}

	implCode_ += xTab + _T("UpdateWindowSize();") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("// display data") + xCrLf;
	implCode_ += xTab + _T("//") + xCrLf;
	implCode_ += xTab + _T("DisplayData();") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("return TRUE;  // return TRUE unless you set the focus to a control") + xCrLf;
	implCode_ += xTab + _T("			   // EXCEPTION: OCX Property Pages should return FALSE")  + xCrLf;
	implCode_ += _T("}") + xCrLf + xCrLf;
}

void CCppGridDialogGenerator::Impl_generateProtectedMembers()
{
	implCode_ += _T("void ") + GetDlgName() + _T("::DisplayData()") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("if (!GetSafeHwnd() || ") + GetCamelCase(classInfo_->GetName()) + GetCardinalityPostfix(classInfo_) + _T("_ == NULL)") + xCrLf;
	implCode_ += xTab + xTab + _T("return;") + xCrLf;
	implCode_ += xCrLf;

	// display data
	//
	CXmlGuiGridInfo* gridInfo;
	GridColumns* columns;
	int firstColIndex = 0;
	CString digits;
	CString decimals;
	string memberName;
	string className;
	int columnsCount;
	int firstColumnIndex = 0;
	_TCHAR tmp[10];

	columns = classInfo_->GetGridInfo()->GetColumns();
	columnsCount = columns->size();
	if (classInfo_->GetGridInfo()->GetColumnCountEnabled())
		firstColumnIndex = 1;
	columnsCount += firstColumnIndex;

	gridInfo = classInfo_->GetGridInfo();
	columns = gridInfo->GetColumns();
	
	//implCode_ += xTab + "int newRowIndex;" + xCrLf;
	implCode_ += xTab + _T("CAsTableRow* row;") + xCrLf;
	

	implCode_ += xCrLf;
	implCode_ += xTab + _T("table_->DeleteNonFixedRows();") + xCrLf;
	//implCode_ += xTab + "table_->DeleteNonFixedRows();" + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("for (int i = 0; i < ") + GetCamelCase(classInfo_->GetName()) + GetCardinalityPostfix(classInfo_) + _T("_->GetSize(); i++) {") + xCrLf;
	implCode_ += xTab + xTab + _T("row = table_->GetRow(i + table_->GetFixedRowsNumber());") + xCrLf;
	implCode_ += xTab + xTab + _T("if(row == NULL)") + xCrLf;
	implCode_ += xTab + xTab + xTab + _T("row = table_->AppendRow(") + _itow(columnsCount, tmp, 10) + _T(", false);") + xCrLf;
	implCode_ += xTab + xTab + _T("SetupRow(row, i + table_->GetFixedRowsNumber(), (*") + GetCamelCase(classInfo_->GetName()) + GetCardinalityPostfix(classInfo_) + _T("_)[i]);") + xCrLf;
	
	implCode_ += xTab + _T("}") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("table_->RedrawWindow();") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	// OnOK()
	//
	implCode_ += _T("void ") + GetDlgName() + _T("::OnOK()") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	// OnCancel()
	//
	implCode_ += _T("void ") + GetDlgName() + _T("::OnCancel()") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	// OnAppend()
	//
	if (classInfo_->GetGridInfo()->GetButtonBarEnabled()) {
		cmString varName;
		int columnsCount;
		int firstColumnIndex = 0;
		_TCHAR tmp[10];

		columns = classInfo_->GetGridInfo()->GetColumns();
		columnsCount = columns->size();
		if (classInfo_->GetGridInfo()->GetColumnCountEnabled())
			firstColumnIndex = 1;
		columnsCount += firstColumnIndex;
		wsprintf(tmp, _T("%d"), columnsCount);

		varName = GetCamelCase(classInfo_->GetName()) + GetCardinalityPostfix(classInfo_) + _T("_");
		implCode_ += _T("void ") + GetDlgName() + _T("::OnAppend()") + xCrLf;
		implCode_ += _T("{") + xCrLf;
		implCode_ += xTab + GetClassName() + _T(" newItem;") + xCrLf;
		implCode_ += xTab + _T("CAsTableRow* newRow;") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += xTab + _T("newRow = table_->AppendRow(") + tmp + _T(", false);") + xCrLf;
		implCode_ += xTab + varName + _T("->Insert(" + varName + _T("->GetSize(), newItem);")) + xCrLf;
		implCode_ += xTab + _T("SetupRow(newRow, table_->GetRowCount() - 1, (*") + varName + _T(")[") + varName + _T("->GetSize() - 1]);") + xCrLf;
		implCode_ += xTab + _T("table_->RedrawWindow();") + xCrLf;
		implCode_ += _T("}") + xCrLf;
		implCode_ += xCrLf;
	}

	// OnInsert()
	//
	if (classInfo_->GetGridInfo()->GetButtonBarEnabled()) {
		cmString varName;
		int columnsCount;
		int firstColumnIndex = 0;
		_TCHAR tmp[10];

		columns = classInfo_->GetGridInfo()->GetColumns();
		columnsCount = columns->size();
		if (classInfo_->GetGridInfo()->GetColumnCountEnabled())
			firstColumnIndex = 1;
		columnsCount += firstColumnIndex;
		wsprintf(tmp, _T("%d"), columnsCount);

		implCode_ += _T("void ") + GetDlgName() + _T("::OnInsert()") + xCrLf;
		implCode_ += _T("{") + xCrLf;
		implCode_ += xTab + _T("CAsTableRow* newRow;") + xCrLf;
		implCode_ += xTab + _T("CAsTableRow* selRow;") + xCrLf;
		implCode_ += xTab + _T("CAsBaseCell* selCell;") + xCrLf;
		implCode_ += xTab + _T("int selRowIndex;") + xCrLf;
		implCode_ += xTab + _T("int selColIndex;") + xCrLf;
		implCode_ += xTab + GetClassName(classInfo_->GetName()) + _T(" newItem;") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += xTab + _T("if (!table_->GetSelectedItem(&selRow, &selCell, selRowIndex, selColIndex))") + xCrLf;
		implCode_ += xTab + xTab + _T("return;") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += xTab + _T("newRow = table_->InsertRow(selRowIndex, ") + tmp + _T(", false);") + xCrLf;
		implCode_ += xTab + GetCamelCase(classInfo_->GetName()) + _T("Array_->Insert(selRowIndex, newItem);") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += xTab + _T("SetupRow(newRow, selRowIndex, newItem);") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += xTab + _T("table_->RedrawWindow();") + xCrLf;
		implCode_ += _T("}") + xCrLf;
		implCode_ += xCrLf;
	}

	// OnDelete()
	//
	if (classInfo_->GetGridInfo()->GetButtonBarEnabled()) {
		implCode_ += _T("void ") + GetDlgName() + _T("::OnDelete()") + xCrLf;
		implCode_ += _T("{") + xCrLf;
		implCode_ += xTab + _T("CAsTableRow* selRow;") + xCrLf;
		implCode_ += xTab + _T("CAsBaseCell* selCell;") + xCrLf;
		implCode_ += xTab + _T("int selRowIndex;") + xCrLf;
		implCode_ += xTab + _T("int selColIndex;") + xCrLf;
		implCode_ += xTab + _T("int ret;") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += xTab + _T("if (!table_->GetSelectedItem(&selRow, &selCell, selRowIndex, selColIndex))") + xCrLf;
		implCode_ += xTab + xTab + _T("return;") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += xTab + _T("ret = MessageBox(_T(\"Delete selected row ?\"), _T(\"Delete row\"), MB_OKCANCEL | MB_ICONQUESTION);") + xCrLf;
		implCode_ += xTab + _T("if (ret == IDOK) {") + xCrLf;
		implCode_ += xTab + xTab + _T("table_->DeleteRow(selRowIndex, false);") + xCrLf;
		implCode_ += xTab + xTab + GetCamelCase(classInfo_->GetName()) + _T("Array_->Delete(selRowIndex - 1);") + xCrLf;
		if (classInfo_->GetGridInfo()->GetColumnCountEnabled()) {
			implCode_ += xTab + xTab + _T("// renumber rows") + xCrLf;
			implCode_ += xTab + xTab + _T("//") + xCrLf;
			implCode_ += xTab + xTab + _T("cmString tmp;") + xCrLf;
			implCode_ += xTab + xTab + _T("CAsTableRow* row;") + xCrLf;
			implCode_ += xTab + xTab + _T("CAsBaseCell* cell;") + xCrLf;
			implCode_ += xCrLf;
			implCode_ += xTab + xTab + _T("for (int i = 1; i < table_->GetRowCount(); i++) {") + xCrLf;
			if (classInfo_->GetGridInfo()->GetZeroAlignedCount())
				implCode_ += xTab + xTab + xTab + _T("tmp.Format(_T(\"%d\"), i - 1);") + xCrLf;
			else
				implCode_ += xTab + xTab + xTab + _T("tmp.Format(_T(\"%d\"), i);") + xCrLf;
			implCode_ += xTab + xTab + xTab + _T("row = table_->GetRow(i);") + xCrLf;
			implCode_ += xTab + xTab + xTab + _T("cell = row->GetCell(0);") + xCrLf;
			implCode_ += xTab + xTab + xTab + _T("cell->SetText(tmp);") + xCrLf;
			implCode_ += xTab + xTab + _T("}") + xCrLf;
		}
		implCode_ += xCrLf;
		implCode_ += xTab + xTab + _T("table_->RedrawWindow();") + xCrLf;
		implCode_ += xTab + _T("}") + xCrLf;
		implCode_ += _T("}") + xCrLf;
		implCode_ += xCrLf;
	}
	
}

void CCppGridDialogGenerator::Head_generateDefineSection()
{
	headCode_ += _T("#ifdef IDD_CFG_TEMPLATE") + xCrLf;
	headCode_ += xTab + _T("#undef IDD_CFG_TEMPLATE") + xCrLf;
	headCode_ += _T("#endif") + xCrLf;
	headCode_ += xCrLf;
	headCode_ += _T("#define IDD_CFG_TEMPLATE                1001") + xCrLf;
	headCode_ += xCrLf;
}

void CCppGridDialogGenerator::Impl_generateOnSize()
{
	implCode_ += _T("void ") + GetDlgName() + _T("::OnSize(UINT nType, int cx, int cy)") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("CDialog::OnSize(nType, cx, cy);") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("UpdateWindowSize();") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;
}

void CCppGridDialogGenerator::Impl_generateUpdateWindowSize()
{
	implCode_ += _T("void ") + GetDlgName() + _T("::UpdateWindowSize()") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	
	implCode_ += xTab + _T("if (!GetSafeHwnd())") + xCrLf;
	implCode_ += xTab + xTab + _T("return;") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("CRect rc;") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("GetClientRect(rc);") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("if (table_->GetSafeHwnd())") + xCrLf;
	implCode_ += xTab + xTab + _T("table_->SetWindowPos(&CWnd::wndTop, rc.left, rc.top, rc.Width(), rc.Height() - BUTTON_HEIGHT - 2 * BUTTON_DISTANCE, SWP_SHOWWINDOW);") + xCrLf;
	implCode_ += xCrLf;

	if (classInfo_->GetGridInfo()->GetButtonBarEnabled()) {
		implCode_ += xTab + _T("if (!cmdAppend_.GetSafeHwnd() || !cmdInsert_.GetSafeHwnd() || !cmdDelete_.GetSafeHwnd())") + xCrLf;
		implCode_ += xTab + xTab + _T("return;") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += xTab + _T("int buttonBarMinWidth;") + xCrLf;
		implCode_ += xTab + _T("int buttonWidth;") + xCrLf;
		implCode_ += xTab + _T("int buttonLeft;") + xCrLf;
		implCode_ += xTab + _T("int buttonTop;") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += xTab + _T("buttonBarMinWidth = BUTTON_WIDTH * 3 - BUTTON_DISTANCE * 2;") + xCrLf;
		implCode_ += xTab + _T("if (rc.Width() >= buttonBarMinWidth)") + xCrLf; 
		implCode_ += xTab + xTab + _T("buttonWidth = BUTTON_WIDTH;") + xCrLf;
		implCode_ += xTab + _T("else") + xCrLf;
		implCode_ += xTab + xTab + _T("buttonWidth = (rc.Width() - BUTTON_DISTANCE * 2) / 3;") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += xTab + _T("buttonTop = rc.bottom - BUTTON_HEIGHT - BUTTON_DISTANCE;") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += xTab + _T("CPaintDC dc(this);") + xCrLf;
		implCode_ += xTab + _T("CBrush b(::GetSysColor(COLOR_BTNFACE));") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += xTab + _T("dc.FillRect(CRect(0, buttonTop - BUTTON_DISTANCE, rc.right, rc.bottom), &b);") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += xTab + _T("buttonLeft = rc.right - buttonWidth * 3 - BUTTON_DISTANCE * 2;") + xCrLf;
		implCode_ += xTab + _T("cmdAppend_.SetWindowPos(&CWnd::wndTop, buttonLeft, buttonTop, buttonWidth, BUTTON_HEIGHT, 0);") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += xTab + _T("buttonLeft = rc.right - buttonWidth * 2 - BUTTON_DISTANCE;") + xCrLf;
		implCode_ += xTab + _T("cmdInsert_.SetWindowPos(&CWnd::wndTop, buttonLeft, buttonTop, buttonWidth, BUTTON_HEIGHT, 0);") + xCrLf;
		implCode_ += xCrLf;
		implCode_ += xTab + _T("buttonLeft = rc.right - buttonWidth;") + xCrLf;
		implCode_ += xTab + _T("cmdDelete_.SetWindowPos(&CWnd::wndTop, buttonLeft, buttonTop, buttonWidth, BUTTON_HEIGHT, 0);") + xCrLf;
	}
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;
}

void CCppGridDialogGenerator::Head_generateSetupRow()
{
	headCode_ += xTab + _T("void SetupRow(CAsTableRow* row, int rowIndex, const ") + GetClassName() + _T("& item);") + xCrLf;
	headCode_ += xCrLf;
}

void CCppGridDialogGenerator::Impl_generateSetupRow()
{
	CXmlMemberInfo* member;
	int firstColIndex = 0;
	CXmlGuiGridInfo* gridInfo;
	GridColumns* columns;
	CString digits;
	CString decimals;
	CString tmp;
	CString dummy;;
	cmString memberName;
	cmString className;
	cmString colIndexStr;
	int colIndex = 0;

	gridInfo = classInfo_->GetGridInfo();
	columns = gridInfo->GetColumns();

	implCode_ += _T("void ") + GetDlgName() + _T("::SetupRow(CAsTableRow* row, int rowIndex, const ") + GetClassName() + _T("& item)") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	
	implCode_ += xTab + _T("CAsNumericTextCell* numCell = NULL;") + xCrLf;
	implCode_ += xTab + _T("CAsHexadecimalTextCell* hexCell = NULL;") + xCrLf;
	implCode_ += xTab + _T("CAsCheckBoxCell* chkCell = NULL;") + xCrLf;
	implCode_ += xTab + _T("CAsGenericTextCell* textCell = NULL;") + xCrLf;
	implCode_ += xTab + _T("CAsComboBoxCell* comboCell = NULL;") + xCrLf;
	implCode_ += xTab + _T("CAsBaseCell* cell = NULL;") + xCrLf;
	implCode_ += xTab + _T("cmString tmp;") + xCrLf;
	implCode_ += xCrLf;

	// generate header creation
	//
	if (gridInfo->GetColumnCountEnabled()) {
		implCode_ += xTab + _T("// row number \"") + xCrLf;
		implCode_ += xTab + _T("//") + xCrLf;
		if (gridInfo->GetZeroAlignedCount())
			implCode_ += xTab + _T("tmp.Format(_T(\"%d\"), rowIndex - 1);") + xCrLf;
		else
			implCode_ += xTab + _T("tmp.Format(_T(\"%d\"), rowIndex);") + xCrLf;
		implCode_ += xTab + _T("cell = row->GetCell(0);") + xCrLf;
		implCode_ += xTab + _T("if (cell == NULL)") + xCrLf;
		implCode_ += xTab + xTab + _T("cell = row->CreateCell(0, enTableCellType_Caption);") + xCrLf;
		implCode_ += xTab + _T("cell->SetBackColor(HEADER_BACK_COLOR);") + xCrLf;
		implCode_ += xTab + _T("cell->SetForeColor(HEADER_FORE_COLOR);") + xCrLf;
		implCode_ += xTab + _T("cell->SetText(tmp, false);") + xCrLf;
		implCode_ += xCrLf;
		firstColIndex = 1;
	}

	className = _T("item."); //"(*" + GetCamelCase(classInfo_->GetName()) + GetCardinalityPostfix(classInfo_) + "_)[i]." ;
	for (int i = 0; i < columns->size(); i++) {
		member = dynamic_cast<CXmlMemberInfo*>(classInfo_->GetMemberById((*columns)[i]));
		if (member == NULL)
			continue;

		if (member->IsHidden()) {
			implCode_ += xTab + _T("// field \"") + member->GetName() + _T("\" is hidden") + xCrLf;
			implCode_ += xTab + _T("//") + xCrLf;
			continue;
		}

		implCode_ += xTab + _T("// field \"") + member->GetName() + _T("\"") + xCrLf;
		implCode_ += xTab + _T("//") + xCrLf;

		tmp.Format(_T("%d"), colIndex + firstColIndex);
		colIndexStr = (const _TCHAR*)tmp;

		memberName = _T("Get") + member->GetName() + _T("()");
		if (member->GetValidationInfo()->GetValidationMode() == enValidationMode_StaticEnum) {
			CXmlMemberValidation_StaticEnum* staticEnum;
			CXmlMemberValidation_StaticEnumItem* item;
			StaticEnumItemsMap* items;
			StaticEnumItemsMap::iterator it;

			staticEnum = member->GetValidationInfo()->GetStaticEnum();
			items = staticEnum->GetItems();
			
			implCode_ += xTab + _T("cell = row->GetCell(") + colIndexStr + _T(");") + xCrLf;
			implCode_ += xTab + _T("if (cell == NULL)") + xCrLf;
			implCode_ += xTab + xTab + _T("cell = row->CreateCell(") + colIndexStr + _T(", enTableCellType_ComboBox);") + xCrLf;
			implCode_ += xTab + _T("comboCell = dynamic_cast<CAsComboBoxCell*>(cell);") + xCrLf;
			implCode_ += xTab + _T("comboCell->DeleteAllItems();") + xCrLf;
			for (it = items->begin(); it != items->end(); it++) {
				item = it->second;
				implCode_ += xTab + _T("comboCell->AddItem(_T(\"") + item->GetItemDescription() + _T("\"), ") + GetClassName(classInfo_->GetName()) + _T("::en") + member->GetName() + _T("_") + item->GetItemName() + _T(");") + xCrLf;
			}
			implCode_ += xTab + _T("comboCell->SelectItem(") + className + memberName + _T(");") + xCrLf;
			
			
		} else if (member->GetDataType() == enDataType_Long) {
			switch (member->GetLongMemberGuiInfo()->GetDisplayMode()) {
				case enLongMemberDisplayMode_Decimal:
					implCode_ += xTab + _T("cell = row->GetCell(") + colIndexStr + _T(");") + xCrLf;
					implCode_ += xTab + _T("if (cell == NULL)") + xCrLf;
					implCode_ += xTab + xTab + _T("cell = row->CreateCell(") + colIndexStr + _T(", enTableCellType_NumTextBox);") + xCrLf;
					implCode_ += xTab + _T("numCell = dynamic_cast<CAsNumericTextCell*>(cell);") + xCrLf;
					implCode_ += xTab + _T("numCell->SetPrecision(0);") + xCrLf;
					implCode_ += xTab + _T("numCell->SetValue(") + className + memberName + _T(", false);") + xCrLf;
					if (member->IsReadOnly())
						implCode_ += xTab + _T("numCell->SetReadOnly(true);") + xCrLf;
					break;

				case enLongMemberDisplayMode_Hex:
					dummy.Format(_T("%d"), member->GetLongMemberGuiInfo()->GetHexBitCount());
					implCode_ += xTab + _T("cell = row->GetCell(") + colIndexStr + _T(");") + xCrLf;
					implCode_ += xTab + _T("if (cell == NULL)") + xCrLf;
					implCode_ += xTab + xTab + _T("cell = row->CreateCell(") + colIndexStr + _T(", enTableCellType_HexTextBox);") + xCrLf;
					implCode_ += xTab + _T("hexCell = dynamic_cast<CAsHexadecimalTextCell*>(cell);") + xCrLf;
					implCode_ += xTab + _T("hexCell->SetBitCount(") + (const _TCHAR*)dummy + _T(");") + xCrLf;
					implCode_ += xTab + _T("hexCell->SetValue(") + className + memberName + _T(", false);") + xCrLf;
					if (member->IsReadOnly())
						implCode_ += xTab + _T("hexCell->SetReadOnly(true);") + xCrLf;
					break;
			}
		} else if (member->GetDataType() == enDataType_Double) {
			decimals.Format(_T("%d"), member->GetDecimalPlaces());
			implCode_ += xTab + _T("cell = row->GetCell(") + colIndexStr + _T(");") + xCrLf;
			implCode_ += xTab + _T("if (cell == NULL)") + xCrLf;
			implCode_ += xTab + xTab + _T("cell = row->CreateCell(") + colIndexStr + _T(", enTableCellType_NumTextBox);") + xCrLf;
			implCode_ += xTab + _T("numCell = dynamic_cast<CAsNumericTextCell*>(cell);") + xCrLf;
			implCode_ += xTab + _T("numCell->SetPrecision(") + (const _TCHAR*)decimals + _T(");") + xCrLf;
			implCode_ += xTab + _T("numCell->SetValue(") + className + memberName + _T(", false);") + xCrLf;
			if (member->IsReadOnly())
				implCode_ += xTab + _T("numCell->SetReadOnly(true);") + xCrLf;

		} else if (member->GetDataType() == enDataType_String) {
			implCode_ += xTab + _T("cell = row->GetCell(") + colIndexStr + _T(");") + xCrLf;
			implCode_ += xTab + _T("if (cell == NULL)") + xCrLf;
			implCode_ += xTab + xTab + _T("cell = row->CreateCell(") + colIndexStr + _T(", enTableCellType_TextBox);") + xCrLf;
			implCode_ += xTab + _T("textCell = dynamic_cast<CAsGenericTextCell*>(cell);") + xCrLf;
			implCode_ += xTab + _T("textCell->SetText(") + className + memberName + _T(", false);") + xCrLf;
			if (member->IsReadOnly())
				implCode_ += xTab + _T("textCell->SetReadOnly(true);") + xCrLf;

		} else if (member->GetDataType() == enDataType_Boolean) {
			implCode_ += xTab + _T("cell = row->GetCell(") + colIndexStr + _T(");") + xCrLf;
			implCode_ += xTab + _T("if (cell == NULL)") + xCrLf;
			implCode_ += xTab + xTab + _T("cell = row->CreateCell(") + colIndexStr + _T(", enTableCellType_CheckBox);") + xCrLf;
			implCode_ += xTab + _T("chkCell = dynamic_cast<CAsCheckBoxCell*>(cell);") + xCrLf;
			implCode_ += xTab + _T("chkCell->SetChecked((BOOL)") + className + memberName + _T(", false);") + xCrLf;
			if (member->IsReadOnly())
				implCode_ += xTab + _T("chkCell->SetReadOnly(true);") + xCrLf;
		}
		
		colIndex++;
		implCode_ += xCrLf;
	}
	
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;
}
***/