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

#ifndef __UI_LOOKRENDERER_G__
#define __UI_LOOKRENDERER_G__

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

#include "dmcmn.h"
#include "eq_solver.h"
#include "myutils.h"

struct EdgeLookInfo
{
	EdgeLookInfo() : mWidth(0), mLeftMargin(0), mRightMargin(0), mInset(0) {}

	int mWidth;
	int mLeftMargin;
	int mRightMargin;
	int mInset;
};

enum // indexes of for mEdges array
{
	LEFT_LOOK_EDGE,
	RIGHT_LOOK_EDGE,
	TOP_LOOK_EDGE,
	BOTTOM_LOOK_EDGE
};

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

	virtual void Prepare();
	virtual void CleanUp();

	wxString mName;

	wxString mTextExpr;
	wxString mTextXExpr;
	wxString mTextYExpr;
	wxString mWidthExpr;
	wxString mHeightExpr;
	wxString mStyleFilterExpr;
	wxString mClassHint;

	wxString mAltText;
	wxColour mBgColor;
	wxColour mFgColor;
	wxColour mBgSusbstColor;

	bool	 mBoldText;
	bool	 mOpaqueText;
	bool	 mIsOpaque;

	wxImage* mpImage;

	EdgeLookInfo mEdges[4];

	// created temporarely
	EqSolverNode* mpTextExprNode;
	EqSolverNode* mpTextXExprNode;
	EqSolverNode* mpTextYExprNode;
	EqSolverNode* mpWidthExprNode;
	EqSolverNode* mpHeightExprNode;
	EqSolverNode* mpStyleFilterExprNode;
};

WX_DEFINE_ARRAY( wxUILookInfo*, UILookInfoListT );

class Record;
class wxUILookRenderer : public wxObject
{
public:
	wxUILookRenderer();
	~wxUILookRenderer();

	bool ParseScript( const wxString& script );
	bool ParseScriptFromFile( const wxString& fname );

	bool StoreLookInfos( const wxString& fname );

	virtual bool RenderItemLook( wxUILookInfo& info, const wxSize& size, wxWindow* wnd,
								 wxBitmap& toBmp, wxDC& dc, 
								 const wxFont& font, const wxColour& bg, const wxColour& fg,
								 int align = wxALIGN_LEFT,
								 EqSolver* pSolver = NULL );

	virtual wxString ReduceItemText( wxUILookInfo& info, EqSolver* pSolver );

	virtual bool CalcItemParams( wxUILookInfo& info, const wxSize& size,
								 wxDC& dc, wxString& text,
								 int& textX, int& textY,
								 int& itemWidth, int& itemHeight,
								 const wxFont& font, const wxColour& bg, const wxColour& fg,
								 EqSolver* pSolver );

	virtual wxFont GetFontForItem( wxUILookInfo& info, wxDC& dc );

	virtual void SetEdgeVars( EdgeLookInfo& edge, const wxString& edgeName, EqSolver* pSolver );

	EqSolver& GetEqSolver();

	UILookInfoListT& GetInfoItems();

	wxUILookInfo* FindLookByClassHint( const wxString& hint );

	virtual void StoreToRecord( wxUILookInfo& info, Record& rec );

	virtual void DrawLook( wxDC& toDc, wxUILookInfo& look, wxBitmap& bmp, int x, int y );

	virtual wxString ImageToStr( wxImage& img );
	static wxImage* StrToImage( const wxString& imgStr );

	virtual Record* ImageToRecord( wxImage& img );
	static wxImage* RecordToImage( Record* pRecord );
	static wxBitmap RecordToBitmap( Record* pRecord );

protected:
	virtual void ExtractEdgeInfo( Record& rec, const wxString& edgeName, EdgeLookInfo& edge );
	virtual void StoreEdgeInfo( Record& rec,   const wxString& edgeName, EdgeLookInfo& edge );

	wxColour StrToColour( const wxString& rgbStr );
	wxString ColourToStr( const wxColour& col );

	void CopyRegion( wxImage& src, wxImage& dest, 
					 int destX, int destY, int destWidth, int destHeight,
					 int srcX, int srcY );

	void TilePattern( wxImage& src, wxImage& dest, 
					  int destX, int destY, int destWidth, int destHeight,
					  int srcX, int srcY, int srcWidth, int srcHeight );
			
	void FillImage( wxImage& img, const wxColour& col );

	void RenderEdge( wxImage& src, wxImage& dest, EdgeLookInfo& edge, 
					 int srcX, int srcY, int destX, int destY,
					 bool isVertical );

	void DrawMultilineText( const wxString& text, wxDC& dc, int x, int y, int &w, int& h, 
							bool calcExtent, int align = wxALIGN_LEFT );

	void DrawMarkedTextLine( const wxString& text, wxDC& dc, 
							 int x, int y, int &w, int& h, bool calcExtent,
							 wxFont* pUnderlinedFnt = NULL );

	void SubstituteColour( wxImage& img, const wxColour& match, const wxColour& subst );

	virtual void CleanUp();

protected:
	wxHashTable		mHintHash; // maps "mHintHash"s to wxUILookInfo
	UILookInfoListT mItems;
	EqSolver		mSolver;

};

#endif // __UI_LOOKRENDERER_G__