// CAtlMainDialogWrapperGenerator.cpp
//

#include "stdafx.h"
#include "AtlMainDialogWrapperGenerator.h"
#include "AtlMainDialogGenerator.h"
#include "XmlProjectSettings.h"
#include "XmlClassInfo.h"
#include "XmlMemberInfo.h"
#include "XmlProject.h"
#include "XmlProjectIcons.h"
#include "enums.h"

CAtlMainDialogWrapperGenerator::CAtlMainDialogWrapperGenerator()
{}

CAtlMainDialogWrapperGenerator::~CAtlMainDialogWrapperGenerator()
{}


void CAtlMainDialogWrapperGenerator::GenerateHeader()
{
	cmString fileName;
	cmString pathName;

	// header file generation
	//
	headCode_ = _T("");
	Head_generateHeader();
	Head_generateIfDefBegin();
	Head_generateIncludeSection();
	Head_generateClassBegin();
	Head_generateConstructor();
	Head_generateDestructor();
	//Head_generateInterfaceImplementation();
	Head_generateDialogData();
	Head_generateGetSetData();
	Head_generateOverrides();
	Head_generateMessageMap();
	Head_generateDataMembers();
	//Head_generateBuildNode(classInfo_, "");
	Head_generateInitializeTree();
	Head_generateStoreNodeData();
	Head_generateGetNodeInfo();
	Head_generateOnOK();
	//Head_generateUpdateWindowSize();
	//Head_generateDisplayPopupMenu();
	//Head_generateDeleteNode();
	//Head_generateUpdateNodeImages();
	//Head_generateGetChildDialog();
	Head_generateClassEnd();
	Head_generateIfDefEnd();

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

	hGeneratedFiles_[fileName] = pathName + fileName;
}

void CAtlMainDialogWrapperGenerator::GenerateImplementation()
{
	int count = 0;
	cmString fileName;
	cmString pathName;

	// generate implementation file
	//
	implCode_ = _T("");
	Impl_generateHeader();
	Impl_generateIncludeSection();
	Impl_generateDebugSymbols();
	Impl_generateMainTitle();
	Impl_generateConstructor();
	Impl_generateDestructor();
	Impl_generateGetSetData();
	
	// save implementation file
	//
	pathName = BuildGeneratedSourcePath() + _T("src\\");
	fileName = GetMainDlgName() + _T(".cpp");
	SaveFile(pathName + fileName, implCode_);

	cppGeneratedFiles_[fileName] = pathName + fileName;
}

void CAtlMainDialogWrapperGenerator::Generate()
{
	GenerateHeader();
	GenerateImplementation();

}

///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// HEADER FILE GENERATORS
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

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

void CAtlMainDialogWrapperGenerator::Head_generateIfDefBegin()
{
	headCode_ += _T("#if !defined(") + DefineOf(GetMainDlgName()) + _T(")") + xCrLf;
	headCode_ += _T("#define ") + DefineOf(GetMainDlgName()) + xCrLf;
	headCode_ += xCrLf;

	headCode_ += _T("#ifdef IDD_CFG_MGR_TEMPLATE") + xCrLf;
	headCode_ += xTab + _T("#undef IDD_CFG_MGR_TEMPLATE") + xCrLf;
	headCode_ += _T("#endif") + xCrLf;
	headCode_ += xCrLf;
	headCode_ += _T("#define IDD_CFG_MGR_TEMPLATE                1000") + xCrLf;
	headCode_ += xCrLf;
}

void CAtlMainDialogWrapperGenerator::Head_generateIncludeSection()
{
	headCode_ += _T("#include \"stdafx.h\"") + xCrLf;
	headCode_ += _T("#include \"resource.h\"") + xCrLf;
	headCode_ += _T("#include \"") + GetClassName() + _T(".h\"") + xCrLf;
	headCode_ += _T("#include \"") + GetMainDlgName() + _T(".h\"") + xCrLf;
	headCode_ += xCrLf;
	Head_generateDlgInclude(classInfo_);
	headCode_ += xCrLf;	
	
	headCode_ += xCrLf;	

}

void CAtlMainDialogWrapperGenerator::Head_generateDlgInclude(CXmlClassInfo* classInfo)
{
	/*
	if (classInfo->GetDefinitionInheritance())
		return;

	if (classInfo->GetRenderingMode() != enRenderingMode_None)
		headCode_ += "#include \"" + GetDlgName(classInfo) + ".h\"" + xCrLf;

	// generate include for class dialogs
	//
	ClassMemberMap* members;
	ClassMemberMap::iterator it;
	CXmlBaseElement* member;

	members = classInfo->GetMembers();

	for (it = members->begin(); it != members->end(); it++) {
		member = it->second;
		if (member->GetDataType() == enDataType_Class)
			Head_generateDlgInclude(dynamic_cast<CXmlClassInfo*>(member));
	}
	*/
}



void CAtlMainDialogWrapperGenerator::Head_generateClassBegin()
{
	cmString name;
	cmString prefix;
	cmString coclassName;

	name = GetMainDlgName();
	prefix = atlSettings_->GetClassPrefix();
	coclassName = prefix + _T("MainDialog");

	headCode_ += _T("/////////////////////////////////////////////////////////////////////////////") + xCrLf;
	headCode_ += _T("// ") + name + xCrLf;
	headCode_ += _T("class ATL_NO_VTABLE ") + name + _T(":") + xCrLf; 
	headCode_ += _T("public CComObjectRootEx<CComSingleThreadModel>,") + xCrLf;
	headCode_ += _T("public CComCoClass<") + name + _T(", &CLSID_MainDialog>,") + xCrLf;
	headCode_ += _T("public IDispatchImpl<I") + coclassName + _T(", &IID_I") + coclassName + _T(", &LIBID_") + ToUpperCase(prefix) + _T("Lib>") + xCrLf;
	headCode_ += _T("{") + xCrLf;
	headCode_ += xCrLf;
	headCode_ += _T("public:") + xCrLf;
	headCode_ += xTab + _T("DECLARE_REGISTRY_RESOURCEID(IDR_MAINDIALOG)") + xCrLf;
	headCode_ += xTab + _T("DECLARE_NOT_AGGREGATABLE(") + name + _T(")") + xCrLf;
	headCode_ += xCrLf;
	headCode_ += xTab + _T("DECLARE_PROTECT_FINAL_CONSTRUCT()") + xCrLf;
	headCode_ += xCrLf;
	headCode_ += xTab + _T("BEGIN_COM_MAP(") + name + _T(")") + xCrLf;
	headCode_ += xTab + xTab + _T("COM_INTERFACE_ENTRY(I") + coclassName + _T(")") + xCrLf;
	headCode_ += xTab + xTab + _T("COM_INTERFACE_ENTRY(IDispatch)") + xCrLf;
	headCode_ += xTab + _T("END_COM_MAP()") + xCrLf;
	headCode_ += xCrLf;
}

void CAtlMainDialogWrapperGenerator::Head_generateConstructor()
{
	//headCode_ += "public:" + xCrLf;
	//headCode_ += xTab + GetMainDlgName() + "(CWnd* pParent = NULL);" + xCrLf;
}

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

void CAtlMainDialogWrapperGenerator::Head_generateDialogData()
{
	/*
	headCode_ += "// Dialog Data" + xCrLf;
	headCode_ += xTab + "//{{AFX_DATA(" +  GetMainDlgName() + ")" + xCrLf;
	headCode_ += xTab + xTab + "enum { IDD = IDD_CFG_MGR_TEMPLATE };"  + xCrLf;
	headCode_ += xTab + xTab + "// NOTE: the ClassWizard will add data members here"  + xCrLf;
	headCode_ += xTab + "//}}AFX_DATA" + xCrLf;
	headCode_ += xCrLf;
	headCode_ += xTab + "CTreeCtrl* tree_;" + xCrLf;
	headCode_ += xTab + "CImageList imageList_;" + xCrLf;
	headCode_ += xCrLf;

	//headCode_ += xTab + "CDialog emptyDlg_;" + xCrLf; 
	//Head_generateDlgDeclare(classInfo_);
	
	headCode_ += xTab + "int viewIndex_EmptyDlg_;" + xCrLf;
	Head_generateViewIndexDeclare(classInfo_);

	headCode_ += xCrLf;
	headCode_ += xTab + "CButton cmdOk_;" + xCrLf; 
	headCode_ += xTab + "CButton cmdCancel_;" + xCrLf; 
	headCode_ += xTab + "AW_CMultiViewSplitter splitterWnd_;" + xCrLf;
	headCode_ += xTab + "CFrameWnd* dummyFrame_;" + xCrLf;
	headCode_ += xCrLf;
	*/

	//headCode_ += xTab + CAtlMainDialogGenerator::GetMainDlgName(atlSettings_->GetClassPrefix()) + " dlg_;" + xCrLf;
}

void CAtlMainDialogWrapperGenerator::Head_generateOverrides()
{
	/*
	headCode_ += "// Overrides" + xCrLf;
	headCode_ += xTab + "// ClassWizard generated virtual function overrides" + xCrLf;
	headCode_ += xTab + "//{{AFX_VIRTUAL(" + GetMainDlgName() + ")" + xCrLf;
	headCode_ += xTab + "protected:" + xCrLf;
	headCode_ += xTab + xTab + "virtual BOOL PreTranslateMessage(MSG* pMsg);" + xCrLf;
	headCode_ += xTab + "//virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support" + xCrLf;
	headCode_ += xTab + "//}}AFX_VIRTUAL" + xCrLf + xCrLf;
	*/
}

void CAtlMainDialogWrapperGenerator::Head_generateMessageMap()
{
	/*
	headCode_ += "// Implementation" + xCrLf;
	headCode_ += "protected:" + xCrLf;
	headCode_ += xCrLf;
	headCode_ += xTab + "// Generated message map functions" + xCrLf;
	headCode_ += xTab + "//{{AFX_MSG(" + GetMainDlgName() + ")" + xCrLf;
	headCode_ += xTab + "virtual BOOL OnInitDialog();" + xCrLf;
	//headCode_ += xTab + "afx_msg void OnTreeNodeSelectionXXX(NMHDR* pNMHDR, LRESULT* pResult);" + xCrLf;
	headCode_ += xTab + "afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);" + xCrLf;
	headCode_ += xTab + "afx_msg void OnSize(UINT nType, int cx, int cy);" + xCrLf;
	headCode_ += xTab + "afx_msg void OnMnuAppendItem_Array();" + xCrLf;
	headCode_ += xTab + "afx_msg void OnMnuInsertItem_ArrayItem();" + xCrLf; 
	headCode_ += xTab + "afx_msg void OnMnuAppendItem_ArrayItem();" + xCrLf; 
	headCode_ += xTab + "afx_msg void OnMnuRemoveItem_ArrayItem();" + xCrLf; 
	headCode_ += xTab + "afx_msg void OnMnuCreateItem_Map();" + xCrLf;       
	headCode_ += xTab + "afx_msg void OnMnuRemoveItem_MapItem();" + xCrLf;   
	headCode_ += xTab + "afx_msg void OnMnuCreateItem_MapItem();" + xCrLf;   
	headCode_ += xTab + "//}}AFX_MSG" + xCrLf;
	headCode_ += xTab + "DECLARE_MESSAGE_MAP()" + xCrLf;
	
	headCode_ += xTab + "void OnTreeNodeSelection();" + xCrLf;
	headCode_ += xCrLf;
	*/
}

void CAtlMainDialogWrapperGenerator::Head_generateDataMembers()
{
	/*
	headCode_ += "// data members" + xCrLf;
	headCode_ += "//" + xCrLf;
	headCode_ += "protected:" + xCrLf;
	headCode_ += xTab + "map<HTREEITEM, " + atlSettings_->GetClassPrefix() + "TreeNodeInfo> node2InfoMap_;" + xCrLf;
	headCode_ += xTab + "CComObject<" + GetClassName() + "> " + GetCamelCase(classInfo_->GetName()) + "_;" + xCrLf;
	headCode_ += xTab + "int lastSelectedViewIndex_;" + xCrLf;
	headCode_ += xTab + "HTREEITEM lastSelNode_;" + xCrLf;
	headCode_ += xCrLf;
	*/
}

void CAtlMainDialogWrapperGenerator::Head_generateInitializeTree()
{
	//headCode_ += xTab + "void InitializeTree();" + xCrLf;
}

void CAtlMainDialogWrapperGenerator::Head_generateStoreNodeData()
{
	//headCode_ += xTab + "void StoreNodeData(HTREEITEM node);" + xCrLf;
}

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

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

void CAtlMainDialogWrapperGenerator::Head_generateGetChildDialog()
{
	//headCode_ += xTab +"CDialog* GetChildDialog(HTREEITEM treeItem);" + xCrLf;
}

void CAtlMainDialogWrapperGenerator::Head_generateGetSetData()
{
	//headCode_ += xTab + "STDMETHOD(GetData)(/*[out, retval]*/I" + GetClassName() + "** data);" + xCrLf;
	//headCode_ += xTab + "STDMETHOD(SetData)(/*[int]*/I" + GetClassName() + "* retval);" + xCrLf;
	headCode_ += xTab + _T("STDMETHOD(Display)(/*[int]*/I") + GetClassName() + _T("* data, /*[out, retval]*/ VARIANT_BOOL* retval);") + xCrLf;
	headCode_ += xCrLf;
}

void CAtlMainDialogWrapperGenerator::Head_generateOnOK()
{
	//headCode_ += xTab + "void OnOK();" + xCrLf;
}

void CAtlMainDialogWrapperGenerator::Head_generateGetNodeInfo()
{
	//headCode_ += xTab + atlSettings_->GetClassPrefix() + "TreeNodeInfo GetNodeInfo(HTREEITEM treeNode);" + xCrLf;
}

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

void CAtlMainDialogWrapperGenerator::Impl_generateHeader()
{
	implCode_ += _T("// ") + GetMainDlgName() + _T(".cpp : implementation file") + xCrLf;
	implCode_ += _T("//") + xCrLf;
	implCode_ += GetGenerationTimeStamp();
	implCode_ += xCrLf;
	//implCode_ += "#pragma warning(disable:4786)" + xCrLf;
	//implCode_ += "#pragma warning(disable:4060) // warning C4060: switch statement contains no 'case' or 'default' labels" + xCrLf;
	
	implCode_ += xCrLf;
}

void CAtlMainDialogWrapperGenerator::Impl_generateIncludeSection()
{

	implCode_ += _T("#include \"stdafx.h\"") + xCrLf;
	implCode_ += _T("#include \"") + GetMainDlgName() + _T(".h\"") + xCrLf;
	implCode_ += _T("#include \"") + CAtlBaseGenerator::GetMainDlgName() + _T(".h\"") + xCrLf;
	implCode_ += xCrLf;
}

void CAtlMainDialogWrapperGenerator::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 CAtlMainDialogWrapperGenerator::Impl_generateMainTitle()
{
	implCode_ += _T("/////////////////////////////////////////////////////////////////////////////") + xCrLf;
	implCode_ += _T("// ") + GetMainDlgName() + _T("") + xCrLf;
	implCode_ += xCrLf;
}

void CAtlMainDialogWrapperGenerator::Impl_generateConstructor()
{
	
	//implCode_ += GetMainDlgName() +"::" + GetMainDlgName() + "(CWnd* pParent /*=NULL*/):" + xCrLf;
	//implCode_ += xTab + xTab + "CDialog(" + GetMainDlgName() + "::IDD, pParent)," + xCrLf;
	//implCode_ += xTab + xTab + "lastSelectedViewIndex_(-1)," + xCrLf;
	//implCode_ += xTab + xTab + "dummyFrame_(NULL)," + xCrLf;
	//implCode_ += xTab + xTab + "lastSelNode_(NULL)" + xCrLf;
	//implCode_ += "{" + xCrLf;
	//implCode_ += xTab + "//{{AFX_DATA_INIT(" + GetMainDlgName() + ")" + xCrLf;
	//implCode_ += xTab + xTab + "// NOTE: the ClassWizard will add member initialization here" + xCrLf;
	//implCode_ += xTab + "//}}AFX_DATA_INIT" + xCrLf;
	//implCode_ += "}" + xCrLf;
	//implCode_ += xCrLf;	
	
}

void CAtlMainDialogWrapperGenerator::Impl_generateDestructor()
{
	implCode_ += GetMainDlgName() + _T("::~") + GetMainDlgName() + _T("()") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	//implCode_ += xTab + "if (dummyFrame_) " + xCrLf;
	//implCode_ += xTab + xTab + "dummyFrame_ = NULL;" + xCrLf;
	//implCode_ += xCrLf;
	implCode_ += _T("}") + xCrLf + xCrLf;
}



cmString CAtlMainDialogWrapperGenerator::GetMainDlgName()
{
	return GetMainDlgName(atlSettings_->GetClassPrefix());
}

cmString CAtlMainDialogWrapperGenerator::GetMainDlgName(const cmString& classPrefix)
{
	return classPrefix + _T("MainDialog");
}

void CAtlMainDialogWrapperGenerator::Impl_generateGetSetData()
{
	implCode_ += _T("STDMETHODIMP ") + GetMainDlgName() + _T("::Display(I") + GetClassName() + _T("* data, VARIANT_BOOL* retval)") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("AFX_MANAGE_STATE(AfxGetStaticModuleState())") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("int ret;") + xCrLf;
	implCode_ += xTab + CAtlBaseGenerator::GetMainDlgName() + _T(" dlg;") + xCrLf;
	implCode_ += xTab + _T("I") + GetClassName() + _T("* modifiedData;") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("if (data == NULL)") + xCrLf;
	implCode_ += xTab + xTab + _T("return E_INVALIDARG;") + xCrLf; 
	implCode_ += xCrLf;
	implCode_ += xTab + _T("dlg.SetData(data);") + xCrLf;
	implCode_ += xTab + _T("ret = dlg.DoModal();") + xCrLf;
	implCode_ += xTab + _T("if (ret == IDOK) {") + xCrLf;
	implCode_ += xTab + xTab + _T("dlg.GetData(&modifiedData);") + xCrLf;
	implCode_ += xTab + xTab + _T("data->Copy(modifiedData);") + xCrLf;
	implCode_ += xTab + xTab + _T("modifiedData->Release();") + xCrLf;
	implCode_ += xTab + _T("}") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("*retval =  (ret == IDOK) ? VARIANT_TRUE : VARIANT_FALSE;") + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + _T("return S_OK;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;
}



