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

#ifndef __RES_TEMPL_G__
#define __RES_TEMPL_G__

#include "dmcmn.h"
#include "txtrecset.h"
#include "strlist.h"
#include "eq_solver.h"

#include "wx/dynarray.h"
#include "wx/hash.h"

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

	void SetGroupInfo( wxDMAttrGroupInfo* pInfo );
	wxDMAttrGroupInfo* GetGroupInfo();

	void* GetData();

	void CopyDataTo( ResGroupData& other );

	// removes both data and reference to group-info
	void CleanUp();

public:
	wxArrayPtrVoid	   mAttrData;
	wxDMAttrGroupInfo* mpGroupInfo;
};

class ResIntAttrInfo : public wxDMAttrInfo
{
public:
	// overriden methods of wxDMAttrInfo class

	virtual wxString GetStringValue( void* pGroupData );
	virtual void SetStringValue( const wxString& value, void* pGroupData );
	virtual bool ValidateStringValue( const wxString& value, const wxString* pErrMsg = NULL );
	virtual void InitValue( void* pGroupData );
	virtual void CopyValue( void* pDestData, void* pSrcData );
};

class ResBoolAttrInfo : public wxDMAttrInfo
{
public:
	// overriden methods of wxDMAttrInfo class

	virtual wxString GetStringValue( void* pGroupData );
	virtual void SetStringValue( const wxString& value, void* pGroupData );
	virtual bool ValidateStringValue( const wxString& value, const wxString* pErrMsg = NULL );
	virtual void InitValue( void* pGroupData );
	virtual void CopyValue( void* pDestData, void* pSrcData );

	virtual bool ShownAsBooleanField();
};

class ResStringAttrInfo : public wxDMAttrInfo
{
public:
	// overriden methods of wxDMAttrInfo class

	virtual wxString GetStringValue( void* pGroupData );
	virtual void SetStringValue( const wxString& value, void* pGroupData );
	virtual bool ValidateStringValue( const wxString& value, const wxString* pErrMsg = NULL );
	virtual void InitValue( void* pGroupData );
	virtual void DestroyValue( void* pGroupData );
	virtual void CopyValue( void* pDestData, void* pSrcData );

	DECLARE_CLASS( ResStringAttrInfo )
};

class ResColorAttrInfo : public wxDMAttrInfo
{
public:
	// overriden methods of wxDMAttrInfo class

	virtual wxString GetStringValue( void* pGroupData );
	virtual void SetStringValue( const wxString& value, void* pGroupData );
	virtual bool ValidateStringValue( const wxString& value, const wxString* pErrMsg = NULL );

	virtual bool HasValueSelectionDialog();
	virtual bool ShowValueSelectonDialog( void* pGroupData );

	virtual void InitValue( void* pGroupData );
	virtual void CopyValue( void* pDestData, void* pSrcData );

	virtual wxColour GetColorValue( void* pGroupData );

	DECLARE_CLASS( ResColorAttrInfo ) 
};

class ResFontAttrInfo : public ResStringAttrInfo
{
public:
	// overriden methods of wxDMAttrInfo class

	virtual bool HasValueSelectionDialog();
	virtual bool ShowValueSelectonDialog( void* pGroupData );

	// methods specific to this class

	virtual wxFont* CreateFontObject( void* pGroupData );
	virtual void SetValuesToRecord( Record& rec, void* pGroupData );
	virtual void GetValuesFromRecord( Record& rec, void* pGroupData );

	DECLARE_CLASS( ResFontAttrInfo ) 
};

class ResRestrictedStringAttrInfo : public wxDMAttrInfo
{
public:
	ResRestrictedStringAttrInfo();

	// overriden methods of wxDMAttrInfo class

	virtual wxString GetStringValue( void* pGroupData );
	virtual void SetStringValue( const wxString& value, void* pGroupData );
	virtual bool ValidateStringValue( const wxString& value, const wxString* pErrMsg = NULL );

	virtual bool HasRestrictedValues( void* pGroupData );
	virtual bool SetRestrictedValues( wxStringList& list );
	virtual void GetRestrictedValues( wxStringList& toLst, void* pGroupData );

	virtual void InitValue( void* pGroupData );
	virtual void CopyValue( void* pDestData, void* pSrcData );

	virtual bool ShownAsBooleanField();
	virtual void SetShownAsBoolean( bool on );

protected:
	wxStringList mValues;
	bool	     mShownAsBool;
};

class ResStringListAttrInfo : public wxDMAttrInfo
{
public:
	// overriden methods of wxDMAttrInfo class

	virtual wxString GetStringValue( void* pGroupData );
	virtual void SetStringValue( const wxString& value, void* pGroupData );
	virtual bool ValidateStringValue( const wxString& value, const wxString* pErrMsg = NULL );

	virtual bool HasMultipleValues( void* pGroupData );
	virtual void SetMultipleValues( wxStringList& fromList, void *pGroupData );
	virtual void GetMultipleValues( wxStringList& toList, void* pGroupData );

	virtual void InitValue( void* pGroupData );
	virtual void DestroyValue( void* pGroupData );
	virtual void CopyValue( void* pDestData, void* pSrcData );

	DECLARE_CLASS( ResStringListAttrInfo ) 
};

enum RESOURCE_PROPERTY_TYPE
{
	RESPROP_NUMBER,
	RESPROP_BOOLEAN,
	RESPROP_STRING,
	RESPROP_COLOR,
	RESPROP_FONT,
	RESPROP_RESTRICTED_STRING,
	RESPROP_RESTRICTED_STRING_AS_BOOLEAN,
	RESPROP_STRING_LIST,
	RESPROP_PROPSET,
	RESPORP_CONCAT_PROPS
};

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

	int GetType() { return mType; }

	bool HasRestrictedValue( const wxString& value );
	virtual wxString TranslateToDispValue( const wxString& value );
	virtual wxString TranslateFromDispValue( const wxString& dispValue );

	wxString mName;
	wxString mDispName; // display name
	wxString mDefaultValue;
	wxString mInitValue;
	wxStringList mRestrValues;
	wxStringList mRestrDispValues;
	int		 mType;
	bool	 mVisibileOnly;
};

WX_DEFINE_ARRAY( ResPropInfo*, ResPropInfoListT );

class ResPropSetInfo : public ResPropInfo
{
public:
	~ResPropSetInfo();

	ResPropInfoListT mProps;
	wxString		 mGluedWith; // empty if not paired
};

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

public:
	wxString		   mTag;
	ResPropSetInfo	   mPropSet;
	ResPropSetInfo	   mExtraPropSet;

	wxString		   mTextPropName;
	wxString		   mAlignPropName;

	wxString		   mExtraPropsTagName;
	wxString		   mDisplayNameExpr;

	wxString		   mWidgetClass;
	wxString		   mBasedOnClass;
	wxString		   mNodeClass;
	wxString		   mPreviewerClass;

	bool			   mCanHaveChildren;
	bool			   mInheritsBg;
	bool			   mInheritsFg;
	bool			   mInheritsFont;

	wxDMAttrGroupInfo* mpWidgetAttrGroup;
	wxDMAttrGroupInfo* mpExtraAttrGroup;

	wxBitmap		   mToolBarBitmap;
	int				   mBitmapId; // assigned by browser's tree control (if any)
};

WX_DEFINE_ARRAY( WidgetResTemplate*, WidgetResTemplateListT );

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

	bool InitGroupData( ResPropSetInfo& set, ResGroupData& data );

	bool LoadGroupData( ResPropSetInfo& set, Record& fromRec, ResGroupData& data );
	bool StoreGroupData( ResPropSetInfo& set, Record& toRec, ResGroupData& data );

	bool IsKnownClass( const wxString& wClass );
	WidgetResTemplate* FindWidgetTemplateForClass( const wxString& wClass );

	void GetTopLevelRecordsByName( const wxString& name, FieldListT& toLst );

	bool LoadResourceRecords( const wxString& fname );
	bool SaveResourceRecords( const wxString& fname );

	bool LoadResourceTemplates( const wxString& fname );

	WidgetResTemplateListT& GetTemplates();
	WidgetResTemplate& GetUnknownWidgetTemplate();

	Record* GetRootRecord();

protected:
	void CreateGroupRecursive( wxDMAttrGroupInfo& group, 
							   ResPropSetInfo& set, 
							   int& curIndex );

	void SetGroupValuesRecursive( ResGroupData& groupData,
								  ResPropSetInfo& set,
								  Record& rec, bool init );

	void GetGroupValuesRecursive( ResGroupData& groupData,
								  ResPropSetInfo& set,
								  Record& rec );

	void LoadPropSetInfo( Record& rec, ResPropSetInfo& set,
						  Record& propsRec );

	ResPropInfo* SearchPropertyTree( ResPropSetInfo& set,
									 const wxString& propName );

	WidgetResTemplate* DoLoadWidget( Record& rec, const wxString& actualClass,
									 Record* pParentRec );

protected:
	WidgetResTemplateListT mWTemplList;
	WidgetResTemplate	   mUnknownWidgetTempl;

	wxHashTable		 mClassHash;
	ResPropInfoListT mPropSets;
};

#endif // __RES_TEMPL_G__