Click here to Skip to main content
12,500,937 members (51,082 online)
Click here to Skip to main content
Add your own
alternative version

Stats

390.8K views
12.6K downloads
353 bookmarked
Posted

Formula Editor

, 15 Apr 2003
Rate this:
Please Sign up or sign in to vote.
Formula-editor for editing and exporting mathematical content

 FormulaDlg Image Settings Image

We needed a mathematical formula editor that was able to export the content of the formulas into a programming language (FORTRAN 77 in our case). Therefore we developed a set of classes described below.

Parts of the code are based on the work of

  • Chris Maunder (CGridCtrl, CColourPopup),
  • Alexander Bischofberger (CColourPopup),
  • Matt Weagle (CFormulaInplaceEdit),
  • Norm Almond (CFontCombo),
  • Shekar Narayanan and S. D. Rajan (CToolBarBtn),
  • Keith Rule (CUndo)
Thanks to you for your excellent work.

General appearance

The following files and classes are included into the source code :

Core Data Structure

bintree.cpp, bintree.hCBinTreeThe binary-tree datastructure.
node.cpp, node.hCNodeThe core node datastructure.
nodeentities.cpp, nodeentities.hsee table 1)The special node entities.
bintreeformat.cpp, bintreeformat.hCBinTreeFormat, CFormatInfoObFormatting information
undo.hCUndoUndo/Redo functions
 

Editing and Visualizing

formuladlg.cpp, formuladlg.h, formuladlg_toolbar.cppCFormulaDlgA Dialog that holds a CFormulaCtrl and toolbar-buttons supporting formula-editing. The dialog provides Callback-functions which handle the CToolBarBtn notifications
formulactrl.cpp, formulactrl.hCFormulaCtrlCustom control that handles keyboard- and mouse-input.
formulainplaceedit.cpp, formulainplaceedit.hCFormulaInPlaceEditInplace Edit-Control. Behaves like inplace-edit control in CGridCtrl
formuladroptarget.cpp, formuladroptarget.hCFormulaDropTargetThe drop target for the formula-control
matrixdlg.cpp, matrixdlg.hCMatrixDlgA Subdialog for parametrizing matrix dimensions.
formulasettingsdialog.cpp, formulasettingsdialog.hCFormulaSettingsDlgSub dialog supporting the customizing of the formula settings (eg. colours, fonts ...)

Helper classes

fontcombobox.cpp, fontcombobox.hCFontComboA CComboBox derived control supporting fontselection.
colourpicker.cpp, colourpicker.hCColourPickerA CButton derived control supporting colour-selection.
colourpopup.cpp, colourpopup.hCColourPopupA CWnd derived control supporting colour-selection (used by CColourPicker).
toolbarbtn.cpp, toolbarbtn.hCToolBarBtnA CButton derived control supporting the bitmap-based node-entity selections.
toolbarbtnctrl.cpp, toolbarbtnctrl.h, toolbardlg.cpp, toolbardlg.hCToolBarBtnCtrl, CToolBarDlgused by CToolBarBtn

The basic approach of the formula-editor is the representation of mathematical content in a binary-tree data-structure. the extraction of the content is performed by an inorder-traversal through the binary tree.

AlgorithmBinary treeOutput
inorder traversal through a binary tree
inorder (node) { inorder (left_child); process (this); inorder (right_child); }
Node ClassesNode Classes

The base class CNode was originally designed as an abstract base class, but it currently provides the standard behaviour of a node. (draw my left child on the left, draw my right child on the right and draw my own content into the middle). If you want to integrate a new node you have to do the following steps:

  1. Define a new node-type (insert a new unique NT_* define into the node.h)
  2. Declare a new node-entity in the nodeentities.h (derive it public from CNode)
    class CMyNode : public CNode
  3. Implement the methods
    virtual void Serialize(CArchive& ar, CBinTree* pTree);
    virtual void TransformRects(CRect& ,CRect& ,CRect& ,CRect& );
    virtual CRect GetContentRect(CDC* pDC);
    virtual void DrawContent(CDC* pDC, CRect rect, DWORD dwSelect);
  4. Insert a new button into a already present ToolbarBtn or build a new ToolbarBtn with your own node
  5. Handle the ToolBarCtrlSelected Event for your node (in the FormulaDlg_ToolBar.cpp file)

Please have a look on the implementation of the already integrated nodes for further details. Here is a list of them and ther interdependencies:

Node classTypeLeft ChildRight Child
CNodeNT_STANDARD
CNablaNodeNT_NABLANULLCIndexNode
CIndexNodeNT_INDEXSuperscriptSubscript
CPartialDerivativeNodeNT_PARTDERIVEDerivation variableContent of the derivation
CDerivativeNodeNT_DERIVEDerivation variableContent of the derivation
CPlaceHolderNodeNT_PLACEHOLDERNULLCIndexNode
CNthRootNodeNT_NTHROOT, NT_ROOTContent of the rootBasis
CPowerToNodeNT_POWERTOBasisExponent
CSumNodeNT_SUMCRangeNode

Content of the Sum

CProdNodeNT_PRODCRangeNodeContent of the Product
CRangeNodeNT_RANGEUpper RangeLower Range
CIntegrandNodeNT_INTEGRANDContent of the integralIntegration variable
CLimesNodeNT_LIMESCOperatorNodeContent of the limes
CBinomialNodeNT_BINOMIALUpperLower
CMatrixNodeNT_MATRIXCLineNodeNULL
CLineNodeNT_LINECLineNodeCElementNode
CElementNodeNT_ELEMENTContent of the MatrixelementCElementNode
CValueNode
CVariableNodeNT_VARIABLENULLCIndexNode
CConstantNodeNT_CONSTANTNULLCIndexNode
CInfinityNodeNT_INFINITYNULLCIndexNode
CPlanckNodeNT_PLANCKNULLCIndexNode
CLambdaNodeNT_LAMBDANULLCIndexNode
CNumberNodeNT_NUMBERNULLCIndexNode
CTypedNode
CFunctionNode
CUserFuncNodeNT_USERFUNCTIONCBraceNodeNULL
CFuncNodeNT_FUNCCBraceNodeNULL
CExtFuncNodeNT_EXTFUNCCBraceNodeNULL
COpNode
CDivisionNodeNT_DIVISIONLeft hand sideRight hand side
CEquationNodeNT_EQUATIONLeft hand sideRight hand side
COperatorNodeNT_OPERATORLeft hand sideRight hand side
CBraceNodeNT_BRACEContent of the braceNULL
CPoisonNodeNT_POISON**
CVectorNodeNT_VECTORContent of the VectorNULL
CIntegralNodeNT_INTEGRALCRangeNodeCIntegrandNode
COverlineNodeNT_OVERLINE**
CArrowNodeNT_ARROWSLeft hand sideRight hand side
CPlusNodeNT_PLUSLeft hand sideRight hand side
CMinusNodeNT_MINUSLeft hand sideRight hand side
CTimesNodeNT_TIMESLeft hand sideRight hand side
CCrossNodeNT_CROSSLeft hand sideRight hand side
table 1) List of already integrated nodes and their dependecies

Please see the implemented TransformRect and DrawContent methods as an example! There are a lot of ways to code them much better. Any suggestions and refinements are welcome.

If you want to use this code in your project you only have to import the classes and to call the DoModal-method of the FormulaDlg.Please be careful with the resources. The resources for the toolbar of the formula-dialog are included via compiletime-directives (resource includes-submenu in the view -menu of the developer-studio)

#include "mathsym.h"
#include "mathsym.rc"
#include "FormulaDlg.h"

void CMainFrame::OnFormula() 
{
    CFormulaDlg FormulaDlg;
    FormulaDlg.DoModal();
}

The core-classes with there attributes and methods look as follow:

CBinTree

CNode* m_pRootNodeThe rootnode of the tree.
CNode* m_pSelectNode The selected node.
DWORD m_dwSelectType The node-selection type (one of NS_*, see node.h).
CString m_strName The name of the tree.
CNode* CreateTree(CString strType) Creates a tree with a root node of type strType.
CNode* GetRootNode() Returns the root node.
void SetRootNode(CNode* pNode) Sets the root node.
void SetName(CString) Sets the Name of the Bintree.
CString GetName() Returns the name of the Bintree.
void SelectNode(CNode* pNode, DWORD dwSelectType) Selects the node pNode with nodeselectiontype dwSelectType.
CNode* GetSelectedNode() Returns the selected node.
void SetNodeSelectionType(DWORD dwSelectType) Sets the nodeselectiontype for the tree.
void GetNodeSelectionType(DWORD dwSelectType) Returns the nodeselectiontype for the tree.
void ResetTree() Resets the tree and delete all nodes.
CNode* GetParent(CNode* pNode) Returns the parent of node pNode.
CNode* CreateNode(CString strType) Creates a node of type strType.
CNode* ReplaceNode(CNode*, CString) void ReplaceNode(CNode*, CNode*) CNode* ReplaceSubTree(CNode*, CString) CNode* InsertNode(CNode*, CNode*, CString, DWORD ) Some handy functions for rearrangement of the tree.
void DrawTree(CDC* pDC, CRect rect) Draws the tree on a dc.
CRect GetRect(CDC* pDC) Returns the destination-rect of the tree.
CNode* GetNodeFromPoint(CDC*, CRect&, CPoint) Returns a node that draws itself into a rect (resultRect) that contains point.
void ReformatNodes(CBinTreeFormat* pFormat=NULL) Reformat the tree with a special formatter.
void SortPreOrder(CNode* pOldNode) CNode* SwapLeftChildNode(CNode*, CNode*) CNode* SwapRightChildNode(CNode*, CNode*) Sorting the tree in associative order (currently not used).
BOOL WriteDIB(CString strFile) Writes the binary-tree content into a dib.
BOOL PrintDDB(CDC* pDC) Prints the binary-tree content into a DC.
CString CBinTree::WriteFormula(int nLanguageType) Write the binary-tree content into a CString.
virtual void DeleteContents() Resets the tree and delete all nodes (from CUndo).
void DeleteNode(CNode* pNode) Deletes a node.
CNode* CloneNode(CNode* pNode, BOOLbCloneSubTree) Clones a node with or without its subtree.
 

CNode

CNode* m_pParentPointer to parent.
CNode* m_pLeftChildPointer to left child.
CNode* m_pRightChildPointer to right child.
int m_nAssociativityLevelassoicativity level for mathematical analysis
DWORD m_dwEditModeedit mode of the node (one of the NE_* defines)
int m_nSubLevelsublevel (subscript, sub-subscript ...)
int m_nSubLevelLeftInc int m_nSubLevelRightIncthe increments of the sublevel for the children
CString m_strNodeTypenode type (one of the NT_* defines)
CString m_strShortCutthe shortcut of the node (sin for sinus)
CString m_strNamethe name of the node (sinus for sinus)
COLORREF m_crColorcolor of the node (modified by the formatter class)
LOGFONT m_lfLOGFONT node (modified by the formatter class)
CStringArray m_saKeyWordKeywords are stored in a Stringarray (Use the define LT_* to access)
CRect m_NodeRectthe destination rectangle for the ouput
int m_nBaseLinethe baseline of the node (with childs )
// e.g. the division line for the CDivisionNode
CString GetNodeType()Returns the node type
void SetParentDirect(CNode* pNode)Sets the parent node directly
CNode* GetParentDirect()Returns the parent node
void SetLeftChild(CNode* pNode)Sets the left child (and sets left child's new parent as this)
CNode* GetLeftChild()Returns the left child
void SetRightChild(CNode* pNode)Sets the right child (and sets right child's new parent as this)
CNode* GetRightChild()Returns the right child
void SetName(CString strName)Sets the name of the node
CString GetName()Returns the name of the node
void SetShortCut(CString strShortCut)Sets the shortcut of the node (used for rendering)
<CODE><CODE>CString GetShortCut()Returns the shortcut of the node
void SetEditMode(DWORD dwEditMode)Sets the edit mode of the node (Combination of NE_ defines)
DWORD GetEditMode()Returns the edit mode of the node
void SetItalic(BOOL bItalic)Sets the italic attribute in the LOGFONT structure
BOOL GetItalic()Returns the italic attribute in the LOGFONT structure
void SetBold(BOOL bBold)Sets the lfWeight-value of the LOGFONT structure (FW_BOLD for bBold==TRUE and FW_NORMAL for bBold==FALSE)
BOOL GetBold()Returns the weight of LOGFONT structure (TRUE for lfWeight==FW_BOLD and FALSE for bBold==FW_NORMAL)
void SetColor(COLORREF crColor)Sets the RGB colour of the node
COLORREF GetColor()Returns the RGB colour of the node
void SetSubLevel(int nSubLevel)Sets the sublevel of the node
int GetSubLevel()Returns the sublevel of the node
int GetSubLevelLeftInc()Returns the sublevel-increment of the left children
int GetSubLevelRightInc()Returns the sublevel-increment of the right children
void SetFaceName(CString strFaceName)Sets the facename in the LOGFONT structure
CString GetFaceName()Returns the facename of the LOGFONT structure as a CString
virtual int GetAssociativityLevel()Returns the Associativity Level of the node (for symbolical postprocessing, currently not used)
virtual BOOL IsNodeLeaf()Returns TRUE if the node has no valid left and right child
virtual void DrawNode(CDC*, CRect&,                       CNode*, DWORD) virtual CNode* GetNodeFromPoint(CDC*, CRect&,                                  CPoint) virtual void FormatNode(                     CBinTreeFormat* pFormat=NULL) virtual CRect GetRect(CDC* pDC)drawing functions
CNode* FindParentPraeorder(CNode* pSearchNode)searching praeorder for the parent of a given node
virtual void Serialize(CArchive& ar,                        CBinTree* pTree)function to overload by subentities
virtual void TransformRects(CRect&, CRect&,                             CRect&, CRect&) virtual CRect GetContentRect(CDC* pDC) virtual void DrawContent(CDC*, CRect, DWORD)standardimplementation (default behaviour)
virtual CString WriteNode(int nLanguageType)Output of the formula in the language LT_*
LOGFONT* GetLogFont()Returns a pointer to the LOGFONT structure m_lf
 

Features

The formula-control has got the following additional features:

  • Copy, cut and paste (currently you are allowed to paste into placeholder-nodes only )
  • Drag & Drop (currently you are allowed to drop onto placeholder-nodes only)
  • Export into a Bitmap
  • Printing and Print-Preview is realized via DDB
  • Sourcecode export (currently only FORTRAN 77 is implemented)

How to use...

Select an equation-node from the equation-toolbar-button. Then you get an equation with placeholders on the left and on the right side. By moving the mouse over the equation the nodes are highlited. Select a placeholder with a single left-mouseclick. Now you can overwrite the placeholder with any node you want. By selecting a node with a double-click, the whole subtree of the node is selcted (e.g select the = from an equation with a doubleclick and either the left and the right side of the equation are selected). If you select a variable-node, a constant-node, a number-node or a userfunction-node you get into the editing by another left-mouseclick.

History

for detailed information please have a look on the file-header-comments
Version 1.0.0.1 - 15-Apr-2003
  • Reduced Flickering (Scrolling and Updating)
  • Drag&Drop refined
  • Modified FormulaInPlaceEdit
  • Added WMF export
  • CRangeNode refined
Version 1.0.0.0 - 09-Apr-2003
First Version

Environment:
Windows NT 4.0 Service Pack 6
MFC Version: 6.0
Service Pack: 5

DataPool engineering
Essener Str. 99
D-46047 Oberhausen

Internet: www.dp-e.de
thorsten.wack@dp-e.de

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

Share

About the Author

tbw
Web Developer
Germany Germany
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
Andrewpeter2-Sep-12 6:39
memberAndrewpeter2-Sep-12 6:39 
QuestionI want to have a C# version of it? Pin
Andrewpeter2-Sep-12 6:38
memberAndrewpeter2-Sep-12 6:38 
GeneralAn Equation Evaluator / SIMPLIFIER Coming in the future [modified] Pin
Ashok Gowtham10-May-10 13:58
memberAshok Gowtham10-May-10 13:58 
GeneralGreat stuff Pin
Moped1-Apr-10 2:38
memberMoped1-Apr-10 2:38 
QuestionHelp~ me. Pin
Yails6-Jun-06 20:34
memberYails6-Jun-06 20:34 
AnswerRe: Help~ me. Pin
gmaoyou28-Sep-06 5:09
membergmaoyou28-Sep-06 5:09 
GeneralGreat Article Pin
Hadi Dayvary27-May-06 20:38
memberHadi Dayvary27-May-06 20:38 
GeneralGreat work [modified] Pin
hamo200823-May-06 6:37
memberhamo200823-May-06 6:37 
GeneralThank u Pin
Keyvan Nayyeri5-Jul-05 5:15
memberKeyvan Nayyeri5-Jul-05 5:15 
GeneralFirst char uppercase Pin
Bruno Norberto17-Feb-05 1:30
memberBruno Norberto17-Feb-05 1:30 
GeneralRe: First char uppercase Pin
Ramon Pardo28-Jun-06 1:17
memberRamon Pardo28-Jun-06 1:17 
GeneralGreat control Pin
AliRafiee18-Jan-05 14:26
memberAliRafiee18-Jan-05 14:26 
GeneralAnd &amp; Or Pin
Bruno Norberto29-Jul-04 9:50
memberBruno Norberto29-Jul-04 9:50 
GeneralRe: And &amp; Or Pin
Bruno Norberto31-Aug-04 4:58
memberBruno Norberto31-Aug-04 4:58 
Generalrequest! Pin
some_one_16-May-04 22:01
susssome_one_16-May-04 22:01 
GeneralRe: request! Pin
cherish8-Aug-04 21:10
membercherish8-Aug-04 21:10 
GeneralEXCELLENT! Pin
boon31330-Mar-04 1:14
memberboon31330-Mar-04 1:14 
GeneralSome questions, please help me !! Pin
cherish17-Dec-03 15:54
membercherish17-Dec-03 15:54 
GeneralRe: Some questions, please help me !! Pin
cherish18-Dec-03 17:11
membercherish18-Dec-03 17:11 
GeneralRe: Some questions, please help me !! Pin
Anonymous4-Jan-04 22:22
sussAnonymous4-Jan-04 22:22 
GeneralRe: Some questions, please help me !! Pin
cherish12-Jan-04 1:28
membercherish12-Jan-04 1:28 
GeneralRe: Some questions, please help me !! Pin
tbw15-Jan-04 22:47
membertbw15-Jan-04 22:47 
GeneralRe: Some questions, please help me !! Pin
Xiaopf20-Jan-06 16:10
memberXiaopf20-Jan-06 16:10 
GeneralRe: Some questions, please help me !! Pin
Prosperb5-Apr-07 10:28
memberProsperb5-Apr-07 10:28 
QuestionMy Error is same~! Pin
Yails6-Jun-06 20:38
memberYails6-Jun-06 20:38 
GeneralGreat work! Pin
dswigger4-Dec-03 8:19
memberdswigger4-Dec-03 8:19 
GeneralGreat - but some errors when compiling for VC.NET Pin
Alexandru Savescu10-Jun-03 3:46
memberAlexandru Savescu10-Jun-03 3:46 
GeneralRe: Great - but some errors when compiling for VC.NET Pin
SohailB29-Mar-05 20:57
memberSohailB29-Mar-05 20:57 
GeneralGreat! Pin
Doan Quang Minh31-May-03 17:36
memberDoan Quang Minh31-May-03 17:36 
Generalthe Formula Editor is very good! Pin
qshljg30-May-03 14:30
memberqshljg30-May-03 14:30 
QuestionMultiple formulas in one file? Pin
IndrekSnt27-May-03 2:18
memberIndrekSnt27-May-03 2:18 
GeneralWow Pin
IndrekSnt27-May-03 1:51
memberIndrekSnt27-May-03 1:51 
GeneralRe: to see a good formula editor go to http://www.soft4science.com Pin
Jørd16-May-03 0:43
memberJørd16-May-03 0:43 
GeneralRe: to see a good formula editor go to http://www.soft4science.com Pin
toxcct29-Jan-05 3:03
membertoxcct29-Jan-05 3:03 
GeneralGreat! Pin
Boat12-May-03 16:49
memberBoat12-May-03 16:49 
GeneralIncredible Pin
Hockey23-Apr-03 20:58
memberHockey23-Apr-03 20:58 
GeneralSo Cool! Pin
fhchina21-Apr-03 0:28
memberfhchina21-Apr-03 0:28 
GeneralGood Pin
zhangliuxue17-Apr-03 16:13
memberzhangliuxue17-Apr-03 16:13 
Generalbaseline is not correct for squareroot and powerto node for complicate formulas Pin
GE17-Apr-03 14:33
memberGE17-Apr-03 14:33 
GeneralThe zip file is corrupted! Pin
gzk5555515-Apr-03 1:09
membergzk5555515-Apr-03 1:09 
GeneralRe: The zip file is corrupted! Pin
Tim Smith17-Apr-03 7:00
memberTim Smith17-Apr-03 7:00 
GeneralRe: The zip file is corrupted! Pin
gzk5555520-Apr-03 16:40
membergzk5555520-Apr-03 16:40 
Generalnot bad, but... Pin
Alex Mol14-Apr-03 3:38
memberAlex Mol14-Apr-03 3:38 
GeneralRe: not bad, but... Pin
tbw14-Apr-03 4:16
membertbw14-Apr-03 4:16 
GeneralRe: not bad, but... Pin
Alex Mol14-Apr-03 20:58
memberAlex Mol14-Apr-03 20:58 
GeneralRe: not bad, but... Pin
Paul Selormey15-Apr-03 1:56
memberPaul Selormey15-Apr-03 1:56 
GeneralGreat work! Pin
Gregor S.13-Apr-03 6:19
memberGregor S.13-Apr-03 6:19 
Generalselection bug Pin
Paolo Messina12-Apr-03 20:53
memberPaolo Messina12-Apr-03 20:53 
GeneralRe: selection bug Pin
tbw13-Apr-03 1:06
membertbw13-Apr-03 1:06 
GeneralRe: selection bug Pin
Paolo Messina13-Apr-03 8:26
memberPaolo Messina13-Apr-03 8:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160919.1 | Last Updated 16 Apr 2003
Article Copyright 2003 by tbw
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid