Click here to Skip to main content
15,885,641 members
Articles / Desktop Programming / MFC

Formula Editor

Rate me:
Please Sign up or sign in to vote.
4.98/5 (306 votes)
15 Apr 20039 min read 558K   14.7K   361  
Formula-editor for editing and exporting mathematical content
/////////////////////////////////////////////////////////////////////////////
//  File:       node.h
//  Version:    1.0.0.0
//  Created:    03-april-2002
//
//  Author:     Thorsten Wack
//  E-mail:     wt@umsicht.fhg.de
//
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is 
// not sold for profit without the authors written consent, and 
// providing that this notice and the authors name is included. If 
// the source code in  this file is used in any commercial application 
// then a simple email would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage whatsoever.
//
// Version 1.0.0.1: CRange Node has got an own GetNodeFromPoint implementation 
//					to fix the selection miss-behaviour. Suggested by Paolo Messina
/////////////////////////////////////////////////////////////////////////////

#ifndef _NODE_H
#define _NODE_H

// maximum of three vlaues
#define ternary_max(a, b, c )						\
	((((a) >= (b)) && ((a) >= (c))) ? (a) :			\
	((((b) >= (a)) && ((b) >= (c))) ? (b) : (c)	))

// Font Height Index
// ==============================================================
#define FH_STANDARD		0		// Standard Height
#define FH_SUB			1		// Subscripts or Superscripts
#define FH_SUB_SUB		2		// Sub-Subscrips or Superscripts
#define FH_SYMBOL		3		// Symbols like Sum or Prod
#define FH_SUB_SYMBOL	4		// Sub Symbols
// ==============================================================

// Node Types
#define NT_STANDARD		"NT_STANDARD"	// standard-node
#define NT_DIVISION		"NT_DIVISION"	// x over y
#define NT_NTHROOT		"NT_NTHROOT"	// n-th root of anything
#define NT_ROOT			"NT_ROOT"		// squareroot
#define NT_POWERTO		"NT_POWERTO"	// x powered by y
#define NT_PLACEHOLDER	"NT_PLACEHOLDER"// placeholder for nodes
#define NT_BRACE		"NT_BRACE"		// Brackes {x+y}
#define NT_PLUS			"NT_PLUS"		// x + y
#define NT_MINUS		"NT_MINUS"		// x - y
#define NT_TIMES		"NT_TIMES"		// x * y
#define NT_NUMBER		"NT_NUMBER"		// 3,1 and so on
#define NT_EQUATION		"NT_EQUATION"	// equations
#define NT_PARTDERIVE	"NT_PARTDERIVE"	// part. d/dt
#define NT_DERIVE		"NT_DERIVE"		// d/dt fkt(t)
#define NT_SUM			"NT_SUM"		// Sum from i=0;to i=10, over fkt(i)
#define NT_PROD			"NT_PROD"		// Product from i=0;to i=10, over fkt(i)
#define NT_RANGE		"NT_RANGE"		// Upper and lower range
#define NT_INTEGRAL		"NT_INTEGRAL"	// Integral from i=0;to i=10, over fkt(t) dt
#define NT_INTEGRAND	"NT_INTEGRAND"	// Integrandvariable and function
#define NT_FUNC			"NT_FUNC"		// like sin, cos, tan
#define NT_EXTFUNC		"NT_EXTFUNC"	// external functions
#define NT_USERFUNC		"NT_USERFUNC"	// like f(x)
#define NT_POISON		"NT_POISON"		// like (a,b) {a,b}
#define NT_INDEX		"NT_INDEX"		// Subscript Node
#define NT_VECTOR		"NT_VECTOR"		// Overline like vector...
#define NT_CONSTANT		"NT_CONSTANT"	// Constant like pi, c...
#define NT_VARIABLE		"NT_VARIABLE"	// variable like m(mass)...
#define NT_LIMES		"NT_LIMES"		// lim(x->oo) f(x)
#define NT_OPERATOR		"NT_OPERATOR"	// x->oo
#define NT_BINOMIAL		"NT_BINOMIAL"	// n over k
#define NT_MATRIX		"NT_MATRIX"		// Matrixnode
#define NT_LINE			"NT_LINE"		// line in a matrix
#define NT_ELEMENT		"NT_ELEMENT"	// Elementnode in a matrix
#define NT_NABLA		"NT_NABLA"		// Nablaoperator
#define NT_LAPLACE		"NT_LAPLACE"	// Laplaceoperator
#define NT_CROSS		"NT_CROSS"		// Crossproduct of a vector
#define NT_ARROWS		"NT_ARROWS"		// ->, =>
#define NT_INFINITY		"NT_INFINITY"	// Infinity oo
#define NT_PLANCK		"NT_PLANCK"		// Planck h
#define NT_LAMBDA		"NT_LAMBDA"		// Lambda
#define NT_LABEL		"NT_LABEL"		// Label for vars and consts

// Node Select modes
#define NS_NODE			0x001 // NodeSelect: select only node-content
#define NS_SUBTREE		0x002 // NodeSelect: select the node with complete subtree
#define NS_DISABLED		0x004 // NodeSelect: node is disabled

// Edit modes
#define NE_ALLOWEDIT	0x001 // Node is allowed to be edited
#define NE_ALLOWDELETE	0x002 // The node is allowed to be deleted manually
#define NE_DRAGABLE		0x004 // The node is allowed to drag
#define NE_INEDITMODE	0x008 // The node is in edit mode

// Insert Modes
#define NI_CHILDLEFT	0x001 // Node insert flag (insert into left subtree)
#define NI_CHILDRIGHT	0x002 // Node insert flag (insert into right subtree)

// Range-Types (currently not used)
#define RT_STANDARD		0x001 // upper & lower range is var or number 
#define RT_EQUATION		0x002 // upper range is var or number, lower is equation 

// Language-Types (LT_FORTRAN is implemented)
// extend by inserting user-defined LT_* types and increment the 
// language count LT_COUNT
// then fill the m_strKeyWord-Array (and the m_straKeyWords-Array for typed nodes)
// also fill the m_strCodeFormat-Array (and the m_straCodeFormats-Array for typed nodes)
#define LT_FORTRAN		0 // Generate Fortran Code
#define LT_COUNT		1 // Number of implemented languages

// forward declaration
class CBinTree;
class CBinTreeFormat;
class CNode;

// service-functions to evaluate some handy rect-coordinates
void CenterRectVert(CRect& rectSource, CRect& rectDest);
void CenterRectHorz(CRect& rectSource, CRect& rectDest);

class CNode : public CObject
{
	DECLARE_DYNAMIC( CNode)
friend class CBinTree;
public:
	CNode();
	virtual ~CNode();

	BOOL operator==(const CNode& Node);
	virtual const void operator=(const CNode& Node);

protected:
	// the core data structure
	// pointer to parent
	CNode* m_pParent;
	// pointer to left child
	CNode* m_pLeftChild;
	// pointer to right child
	CNode* m_pRightChild;

	// assoicativity level for mathematical analysis
	int m_nAssociativityLevel;
	// edit mode of the node (one of the NE_* defines)
	DWORD m_dwEditMode;
	// sublevel (subscript, sub-subscript ...)
	int m_nSubLevel;
	// the increments of the sublevel for the children
	int m_nSubLevelLeftInc;
	int m_nSubLevelRightInc;

	// node type (one of the NT_* defines)
	CString m_strNodeType;
	// the shortcut of the node (sin for sinus)
	CString m_strShortCut;
	// the name of the node (sinus for sinus)
	CString m_strName;
	// color of the node (modified by the formatter class)
	COLORREF m_crColor;
	// LOGFONT node (modified by the formatter class)
	LOGFONT m_lf;
	// the Keyword for Code-export
	CString m_strKeyWord[LT_COUNT];
	// the Format for Code-export (%s %s %s) as default
	CString m_strCodeFormat[LT_COUNT];
	
public:
	// the destination rectangle for the ouput
	CRect m_NodeRect;
	// the baseline of the node (with childs ) 
	// e.g. the division line for the CDivisionNode
	int m_nBaseLine;
public:
	// Returns the node type
	CString GetNodeType();
	
	// Sets the parent node directly
	void SetParentDirect(CNode* pNode);
	// Returns the parent node
	CNode* GetParentDirect();

	// Sets the left child (and sets left child's new parent as this)
	void SetLeftChild(CNode* pNode);
	// Returns the left child
	CNode* GetLeftChild();

	// Sets the right child (and sets right child's new parent as this)
	void SetRightChild(CNode* pNode);
	// Returns the right child
	CNode* GetRightChild();
	
	// Sets the name of the node
	void SetName(CString strName);
	// Returns the name of the node
	CString GetName();

	// Sets the shortcut of the node (used for rendering)
	void SetShortCut(CString strShortCut);
	// Returns the shortcut of the node
	CString GetShortCut();

	// Sets the edit mode of the node (Combination of NE_ defines)
	void SetEditMode(DWORD dwEditMode);
	// Returns the edit mode of the node
	DWORD GetEditMode();

	// Sets the italic attribute in the LOGFONT structure
	void SetItalic(BOOL bItalic);
	// Returns the italic attribute in the LOGFONT structure
	BOOL GetItalic();
	
	// Sets the lfWeight-value of the LOGFONT structure
	// FW_BOLD for bBold==TRUE and FW_NORMAL for bBold==FALSE
	void SetBold(BOOL bBold);
	// Returns the weight of LOGFONT structure 
	// TRUE for lfWeight==FW_BOLD and FALSE for bBold==FW_NORMAL
	BOOL GetBold();

	// Sets the RGB colour of the node
	void SetColor(COLORREF crColor);
	// Returns the RGB colour of the node
	COLORREF GetColor();

	// Sets the sublevel of the node
	void SetSubLevel(int nSubLevel);
	// Returns the sublevel of the node
	int GetSubLevel();
	// Returns the sublevel-increment of the left children
	int GetSubLevelLeftInc();
	// Returns the sublevel-increment of the right children
	int GetSubLevelRightInc();

	// Sets the facename in the LOGFONT structure
	void SetFaceName(CString strFaceName);
	// Returns the facename of the LOGFONT structure as a CString
	CString GetFaceName();

	// Returns the Associativity Level of the node
	virtual int GetAssociativityLevel();
	
	// Returns a pointer to the LOGFONT structure m_lf
	LOGFONT* GetLogFont();

	// Returns TRUE if the node has no valid left and right child
	virtual BOOL IsNodeLeaf();
	
// drawing functions
	virtual void DrawNode(CDC* pDC, CRect& rect, CNode* pSelectNode, DWORD dwSelect);
	virtual CNode* GetNodeFromPoint(CDC* pDC, CRect& rect, CPoint point);
	virtual void FormatNode(CBinTreeFormat* pFormat=NULL);
	virtual CRect GetRect(CDC* pDC);

// searching praeorder for the parent of a given node
	CNode* FindParentPraeorder(CNode* pSearchNode);

public:
// function to overload by subentities
	virtual void Serialize(CArchive& ar, CBinTree* pTree);

// standardimplementation (default behaviour)	
	virtual void TransformRects(CRect& ,CRect& ,CRect& ,CRect& );
	virtual CRect GetContentRect(CDC* pDC);
	virtual void DrawContent(CDC* pDC, CRect rect, DWORD dwSelect);

// Output of the formula in the	language LT_*
	virtual CString WriteNode(int nLanguageType);

};

#endif //_NODE_H

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
tbw
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions