/////////////////////////////////////////////////////////////////////////////
// Name:        No names yet.
// Purpose:     Contrib. demo
// Author:      Aleksandras Gluchovas
// Modified by:
// Created:     2000/02/08
// RCS-ID:      $Id: eventgen.h,v 1.5 2001/04/04 12:42:37 aleksandras Exp $
// Copyright:   (c) Aleksandras Gluchovas
// Licence:   	GPL (version 2.0 or later)
/////////////////////////////////////////////////////////////////////////////

#ifndef __EVENTGEN_G__
#define __EVENTGEN_G__

#include "wx/dynarray.h"
#include "wx/hash.h"
#include "scriptproc.h"
#include "myutils.h"
#include "eventinfo.h"

class GenRuleInfo : public wxObject
{
public:
	GenRuleInfo();

	wxString	   mTag;
	ScriptTemplate mSubscript;
	int			   mFlags;

	enum RULE_CONTENT_FLAGS
	{
		COLLECT_EVENT_ENTRIES = 0x01,
		COLLECT_EVENT_BODIES  = 0x02,
		COLLECT_CTL_MEMBERS   = 0x04, // name, ctl_member_name
		COLLECT_MEMBERS		  = 0x08, // name, type, member_name, init_value
		COLLECT_SETTERS		  = 0x10, // name, setter_name
		COLLECT_GETTERS		  = 0x20, // name, getter_name
	};
};

WX_DEFINE_ARRAY( GenRuleInfo*, GenRuleInfoListT );

class GenRuleSet : public wxObject
{
public:
	~GenRuleSet();

	wxString mName;
	GenRuleInfoListT mRules;
};

WX_DEFINE_ARRAY( GenRuleSet*, GenRuleSetListT );

class GenBindRuleVarTypeInfo
{
public:
	wxString mBoundAs;
	ScriptTemplate mMemberNameHint;
	ScriptTemplate mDeclSource;
	ScriptTemplate mInitSource;
	ScriptTemplate mToCtlSource;
	ScriptTemplate mToMemberSource;
};

WX_DEFINE_ARRAY( GenBindRuleVarTypeInfo*, GenBindRuleVarTypeInfoListT );

class GenBindRuleInfo : public wxObject
{
public:
	wxString mCtlClass;
	ScriptTemplate mCtlMemberName;
	
	ScriptTemplate mCtlMemberNameHint;
	ScriptTemplate mGetterNameHint;
	ScriptTemplate mSetterNameHint;

	ScriptTemplate mCtlDeclSource;
	ScriptTemplate mCtlInitSource;

	bool mCanHaveMember;

	ScriptTemplate mGetterDeclSource;
	ScriptTemplate mSetterDeclSource;
	ScriptTemplate mGetterImpSource;
	ScriptTemplate mSetterImpSource;

	GenBindRuleVarTypeInfoListT mVarTypes;
};

WX_DEFINE_ARRAY( GenBindRuleInfo*, GenBindRuleInfoListT );

class GenBindRuleSet : public wxObject
{
public:
	~GenBindRuleSet();

	GenBindRuleInfo* FindRule( const wxString& ctrlClass );

	wxString mName;

	GenBindRuleInfoListT mRules;
};

WX_DEFINE_ARRAY( GenBindRuleSet*, GenBindRuleSetListT );

class GenSourceTemplate : public wxObject
{
public:
	GenSourceTemplate();
	~GenSourceTemplate();

	GenRuleInfo* FindRule( const wxString& forTag );
	GenBindRuleInfo* FindBindRule( const wxString& forCtrlClass );

	wxString mName;
	wxString mLang;
	bool mHasHeader;
	wxString mHdrExt;
	wxString mImpExt;
	GenRuleSet*		mpRuleSet;
	GenBindRuleSet* mpBindRuleSet;
	ScriptTemplate  mHdrSubscript;
	ScriptTemplate  mImpSubscript;
};


WX_DEFINE_ARRAY( GenSourceTemplate*, GenSourceTemplateListT );

class GenTaggedBlockInfo
{
public:
	GenTaggedBlockInfo();

	wxString mTag;
	size_t mFromIdx;
	size_t mTillIdx;
};

WX_DEFINE_ARRAY( GenTaggedBlockInfo*, GenTaggedBlockInfoListT );

class GenSourceInfo : public wxObject
{
public:
	GenSourceInfo();
	~GenSourceInfo();

	wxString mText;
	wxString mFName;
	wxString mOtherFName;
	wxString mResFile;
	wxString mWndRes;
	wxString mMenuRes;
	wxString mClassName;
	bool	 mIsHeader;

	GenSourceTemplate* mpTempl;

	GenTaggedBlockInfoListT mBlocks;
};

class GenSourceProcessor;

// overrides methods of ScriptTemplate to handle substitutions
// specific to source-code generation templates

class GenScriptTemplate : public BasicScriptProcessorTemplate
{
public:
	GenScriptTemplate( ScriptProcessor* pProc );

	// overriden method of ScriptTemplate

	virtual wxString HandleSpecialSubstitution( char* at, ScriptTemplate& templ  );

protected:
	virtual wxString Handle_event_body_at( char* at, ScriptTemplate& templ );
	virtual wxString Handle_event_list( char* at, ScriptTemplate& templ );
	virtual wxString Handle_member_list( char* at, ScriptTemplate& templ );

	enum MEMBERGEN_SUBSCRIBT_TYPES
	{
		decl_source,
		ctl_decl_source,
		init_source,
		ctl_init_source,
		to_ctl_source,
		to_member_source,
		getter_decl_source,
		setter_decl_source,
		getter_imp_source,
		setter_imp_source
	};
};

class GenSourceProcessor : public ScriptProcessor
{
public:
	GenSourceProcessor();
	~GenSourceProcessor();

	// overriden method of ScriptTemplate class
	virtual bool ProcessSection( Record& rec );

	virtual GenSourceTemplateListT& GetTemplates();
	virtual GenSourceTemplate* FindTemplate( const wxString& name );

	virtual void InitScaner();
	virtual bool ScanHeader( const wxString& hdrFName, const wxString& src );
	virtual bool ScanImplementation( const wxString& impFName, const wxString& src );
	virtual bool ScanSources( const wxString& impFName, const wxString& impSrc,
							  const wxString& hdrFName, const wxString& hdrSrc );

	virtual bool IsGeneratedSource( const wxString& srcText );

	virtual EventEntryInfoListT& GetEvents();

	virtual void ReGenerateSources();
	virtual void GenerateNewSources( const wxString& fname, 
									 const wxString& className,
									 const wxString& resFile,
									 const wxString& wndRes,
									 const wxString& menuRes,
									 GenSourceTemplate& templ );

	virtual void SetWindowResource( const wxString& wndRes );
	virtual void SetMenuResource( const wxString& menuRes );
	virtual void SetResourceFile( const wxString& resFile );

	virtual size_t GetSourceCodeLineNoForEvent( EventEntryInfo* pEntry );

	virtual ResourceItemInfoListT& GetResourceItems();

	virtual bool FlushSourcesToDisk();
	virtual void SetCommonSourceVars( GenSourceInfo& info, ScriptTemplate& script );
	virtual void SetCommonItemVars( ResourceItemInfo& item, ScriptTemplate& script );

	virtual GenSourceInfo& GetImpSource();
	virtual GenSourceInfo& GetHdrSource();

	virtual void PrepareResourceItems();
	virtual bool ProcessBindings( GenSourceInfo& info ); 

	ScriptTemplate& GetTopSubscript();

	virtual void CleanUp();

protected:
	virtual bool DoScanSource( GenSourceInfo& info ); 
	virtual bool DoProcessSource( GenSourceInfo& info ); 

	virtual void CollectEvents( GenSourceInfo& info, GenTaggedBlockInfo& block, 
								char* begin, char* end );

	virtual void CollectEventBodies( GenSourceInfo& info, GenTaggedBlockInfo& block, 
									 char* begin, char* end );

	virtual void DoRegenerateSource( GenSourceInfo& info );

	virtual void DoGenerateNewSource( GenSourceInfo& info, ScriptTemplate& script );

	wxString GetCapitalizedFName( const wxString& fname );
	wxString GetDateStr();

	wxString ExtractMethod( const wxString& scopedMtd );
	wxString ExtractId( const wxString& id );

protected:
	GenSourceTemplateListT	mTemplates;
	GenRuleSetListT			mRuleSets;
	GenBindRuleSetListT		mBindRuleSets;

	GenSourceInfo			mImpSrc;
	GenSourceInfo			mHdrSrc;

	ResourceItemInfoListT   mResItems;
	EventEntryInfoListT		mEvents;

	size_t					mCurBodyCount;
};

#endif // __EVENTGEN_G__