00001 00002 // MathCore = a WYSIWYG equation editor + a powerful math engine // 00003 // Copyright (C) 2003 by Francesco Montorsi // 00004 // // 00005 // This library is free software; you can redistribute it and/or // 00006 // modify it under the terms of the GNU Lesser General Public // 00007 // License as published by the Free Software Foundation; either // 00008 // version 2.1 of the License, or (at your option) any later // 00009 // version. // 00010 // // 00011 // This library is distributed in the hope that it will be useful, // 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of // 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 00014 // GNU Lesser General Public License for more details. // 00015 // // 00016 // You should have received a copy of the GNU Lesser General Public // 00017 // License along with this program; if not, write to the Free // 00018 // Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, // 00019 // MA 02111-1307, USA. // 00020 // // 00021 // For any comment, suggestion or feature request, please contact // 00022 // the administrator of the project at frm@users.sourceforge.net // 00023 // // 00030 00031 00032 00033 00034 // optimization for GCC compiler 00035 #ifdef __GNUG__ 00036 #pragma implementation "Operator.h" 00037 #endif 00038 00039 // includes 00040 #include "mc/mcprec.h" 00041 #ifdef __BORLANDC__ 00042 #pragma hdrstop 00043 #endif 00044 00045 #ifndef mcPRECOMP 00046 #include <wx/dcscreen.h> 00047 #include "mc/Operator.h" 00048 #include "mc/MathUtils.h" 00049 #endif 00050 00051 00052 00053 mcIMPLEMENT_ABSTRACT_CLASS(mcOperator, mcElement); 00054 00055 00056 // setup customizable variables 00057 wxString mcOperatorHelpers::strOpSymbol[] = { wxT("+"), wxT("-"), wxT("*"), wxT("/:") }; 00058 wxSize mcOperatorHelpers::sgui_szReal[mcNUM_OPERATORS][mcELEMENTMATH_MAX_EXPDEPTH]; 00059 wxSize mcOperatorHelpers::sgui_sz[mcELEMENTMATH_MAX_EXPDEPTH]; 00060 const mcStyleArray *mcOperatorHelpers::sgui_pLastStyle = NULL; 00061 00062 00063 00064 00065 // ---------------------- 00066 // mcOPERATORDATA 00067 // ---------------------- 00068 00069 bool mcOperatorHelpers::data_isAllowedBefore(mcElementType type) const 00070 { 00071 if (mcOperatorHelpers::data_isOp(type)) 00072 return FALSE; // two operators cannot be adiacent... 00073 return TRUE; 00074 } 00075 00076 00077 00078 // ---------------------- 00079 // mcOPERATORGUI 00080 // ---------------------- 00081 00082 void mcOperatorHelpers::gui_DoRecalcSize() 00083 { 00084 if (gui_GetStyleArray() == sgui_pLastStyle) { 00085 00086 mcGUILOG(wxT("mcOperatorHelpers::gui_DoRecalcSize - skipped an operator recalc process")); 00087 mgui_sz = sgui_sz[gui_GetExpDepth()]; 00088 return; // no need to recalc anything 00089 } 00090 00091 mcGUILOG(wxT("mcOperatorHelpers::gui_DoRecalcSize - recalculating ALL operator' sizes")); 00092 sgui_pLastStyle = gui_GetStyleArray(); 00093 wxScreenDC dc; 00094 00095 // save the width and height of this operator (they are set 00096 // to the maximum width and height found among all the 00097 // operators, so all the operators occupy the same size) 00098 //gui_SelectStyle(dc); 00099 for (int j=0; j < mcELEMENTMATH_MAX_EXPDEPTH; j++) { 00100 00101 // set the right style for this exponent depth 00102 gui_GetStyleArray()->Get(j)->gui_Select(dc, this); 00103 sgui_sz[j].Set(-1, -1); 00104 00105 for (int i=0; i < mcNUM_OPERATORS; i++) { 00106 wxString str = strOpSymbol[i].GetChar(0); 00107 00108 int w = gui_GetWidthOf(&dc, str); 00109 int h = gui_GetHeightOf(&dc, str); 00110 00111 sgui_sz[j].SetWidth(mcMAX(w, sgui_sz[j].GetWidth())); 00112 sgui_sz[j].SetHeight(mcMAX(h, sgui_sz[j].GetHeight())); 00113 00114 // save the real size for this op 00115 sgui_szReal[i][j].Set(w, h); 00116 } 00117 } 00118 00119 // last update our internal pointer 00120 mgui_sz = sgui_sz[gui_GetExpDepth()]; 00121 } 00122 00123 int mcOperatorHelpers::gui_Draw(wxDC &hDC, int x, int y, long flags, const wxPoint &pt) const 00124 { 00125 wxSize real(gui_GetRealSize()); 00126 mcGUILOG(wxT("mcOperatorHelpers::gui_Draw [%s] - size [%d;%d], real size [%d;%d]"), 00127 mcTXTTHIS, mgui_sz.GetWidth(), mgui_sz.GetHeight(), 00128 real.GetWidth(), real.GetHeight()); 00129 00130 // draw the symbol using correct colors and fonts 00131 gui_SelectStyle(hDC); 00132 00133 hDC.DrawText(data_GetOpSymbol(), 00134 x+(mgui_sz.GetWidth()-real.GetWidth())/2, 00135 y+(mgui_sz.GetHeight()-real.GetHeight())/2); 00136 00137 return data_GetID(); 00138 } 00139 00140 mcInputRes mcOperatorHelpers::gui_Input(const mcKey &key, mcElement *) 00141 { 00142 wxString str = (wxChar)key.GetKeyCode(); 00143 00144 // be sure the given char is the char which defines hlp() 00145 // operator 00146 mcASSERT(data_GetOpSymbol().Contains(str), 00147 wxT("Given char wasn't the right symbol for this operator")); 00148 00149 // update size of hlp() op 00150 gui_RecalcSize(); 00151 00152 return mcIR_OKAY; 00153 } 00154 00155 mcMoveCursorRes mcOperatorHelpers::gui_MoveCursor(mcMoveCursorFlag flag, long modifiers) 00156 { 00157 // if mcOperatorHelpers::gui_MoveCursor is called, it means that the cursor 00158 // position is wrong... 00159 mcASSERT(0, wxT("Code should never get here; cursor position is wrong")); 00160 /*if (flag == mcMCF_LEFT) return mcMCR_SETFOCUS_PREVIOUS; 00161 if (flag == mcMCF_RIGHT) return mcMCR_SETFOCUS_NEXT; 00162 if (flag == mcMCF_UP) return mcMCR_SETFOCUS_ABOVE; 00163 if (flag == mcMCF_DOWN) return mcMCR_SETFOCUS_BELOW; 00164 */ 00165 // just to make compiler happy 00166 return mcMCR_OKAY; 00167 } 00168 00169 int mcOperatorHelpers::gui_MoveCursorUsingPoint(wxDC &dc, const wxPoint &p) 00170 { 00171 mcASSERT(0, wxT("Code should never get here; cursor position is wrong")); 00172 00173 // just to make compiler happy 00174 return mcMCR_OKAY; 00175 } 00176 00177 int mcOperatorHelpers::gui_GetRelCursorPos(wxDC &hDC, wxPoint *pt) const 00178 { 00179 // if mcOperatorHelpers::gui_GetRelCursorPos is called, it means that the 00180 // cursor position is wrong... 00181 mcASSERT(0, wxT("Code should never get here; cursor position is wrong")); 00182 pt->x = 0; 00183 pt->y = 0; 00184 00185 // just to make compiler happy 00186 return gui_GetHeight(); 00187 } 00188 00189 bool mcOperatorHelpers::gui_isBeginKey(const mcKey &key) const 00190 { 00191 wxString str = (wxChar)key.GetKeyCode(); 00192 if (key.GetKeyCode() != 0 && key.GetModifiers() == 0 && 00193 data_GetOpSymbol().Contains(str)) 00194 return TRUE; 00195 return FALSE; 00196 } 00197 00198 00199 00200 00201 00202 // ---------------------- 00203 // mcOPERATORIO 00204 // ---------------------- 00205 00206 bool mcOperatorHelpers::io_isBeginTag(const wxXml2Node &tag) const 00207 { 00208 if (tag.GetName().CmpNoCase(wxT("mo")) != 0) 00209 return FALSE; 00210 if (tag.GetChildren().GetType() != wxXML_TEXT_NODE) 00211 return FALSE; // ERROR !!!! NOT WELL-FORMED MATHML 00212 00213 wxString str = tag.GetChildren().GetContent(); 00214 for (int n=0; n < mcNUM_OPERATORS; n++) 00215 if (strOpSymbol[n].Contains(str)) 00216 return n; 00217 00218 return FALSE; 00219 } 00220 00221 bool mcOperatorHelpers::io_isBeginChar(const wxString &str) const 00222 { 00223 if (data_GetOpSymbol().Contains(str)) 00224 return TRUE; 00225 return FALSE; 00226 } 00227 00228 wxXml2Node mcOperatorHelpers::io_GetMathML(bool bGetPresentation) const 00229 { 00230 // set up the given tag to represent an operator of hlp() type 00231 return wxXml2Node(wxXML_TEXT_NODE, wxXml2EmptyDoc, wxT("mo"), data_GetOpSymbol()); 00232 } 00233 00234 wxString mcOperatorHelpers::io_GetInlinedExpr() const 00235 { 00236 return data_GetOpSymbol().Left(1); 00237 } 00238 00239 bool mcOperatorHelpers::io_ImportPresentationMathML(wxXml2Node tag, wxString &pErr) 00240 { 00241 mcASSERT(tag.GetName() == wxT("mo"), wxT("Error in mcOperatorHelpers::io_isBeginTag()")); 00242 00243 if (tag.GetChildren().GetType() != wxXML_TEXT_NODE) { 00244 00245 // ooooooops 00246 pErr = wxT("The MathML to import is not valid MathML 2.0\n") 00247 wxT("or there was an error while parsing it."); 00248 return FALSE; 00249 } 00250 00251 // extract data & check it 00252 wxString str = tag.GetChildren().GetContent(); 00253 mcASSERT(data_GetOpSymbol().Contains(str), 00254 wxT("Error in mcOperatorHelpers::io_isBeginTag")); 00255 00256 return TRUE; 00257 00258 } 00259 00260 bool mcOperatorHelpers::io_ImportInlinedExpr(const wxString &str, int *count, wxString &pErr) 00261 { 00262 wxString opchar(str.GetChar(0)); 00263 mcASSERT(data_GetOpSymbol().Contains(opchar), 00264 wxT("Error in mcOperatorHelpers::io_isBeginChar")); 00265 00266 // there are no info to import in a mcOperator.... 00267 *count = 1; 00268 return TRUE; 00269 } 00270 00271 00272 00273 00274 00275 00276 00277
[ Top ] |