]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_parinset.C
mathed31.diff
[lyx.git] / src / mathed / math_parinset.C
index a676e9ebadd6114aff962705041d7fd4a3b3f172..862f4554d043422a35f06192bb8fd51f80146bf1 100644 (file)
@@ -10,6 +10,7 @@
 #include "math_parser.h"
 #include "math_rowst.h"
 #include "math_parinset.h"
+#include "debug.h"
 
 using std::endl;
 
@@ -25,7 +26,6 @@ MathedRowSt * MathParInset::getRowSt() const
 MathParInset::MathParInset(short st, string const & nm, short ot)
        : MathedInset(nm, ot, st)
 {
-       array = 0;
        ascent = 8;
        width = 4;
        descent = 0;
@@ -35,46 +35,35 @@ MathParInset::MathParInset(short st, string const & nm, short ot)
 }
 
 
-MathParInset::MathParInset(MathParInset * p)
-       : MathedInset(p)
-{
-       flag = p->flag;
-       p->setArgumentIdx(0);
-       MathedIter it(p->GetData());
-       SetData(it.Copy());
-}
-
-
 MathParInset::~MathParInset()
 {
-       if (array) {
-               MathedIter it(array);
-               it.Clear();
-               delete array;
-       }
 }
 
 
 MathedInset * MathParInset::Clone()
 {
-       return new MathParInset(this);
+       return new MathParInset(*this);
 }
 
 
-void MathParInset::SetData(MathedArray * a)
+void MathParInset::setData(MathedArray * a)
 {
-       array = a;
+       if (!a) {
+               lyxerr << "can't set Data from NULL pointer" << endl;
+               array = MathedArray();
+               return;
+       }
+
+       array = *a;
        
        // A standard paragraph shouldn't have any tabs nor CRs.
-       if (array) {
-               MathedIter it(array);
-               while (it.OK()) {
-                       char c = it.GetChar();
-                       if (c == LM_TC_TAB || c == LM_TC_CR) 
-                               it.Delete();
-                       else
-                               it.Next();
-               }
+       MathedIter it(&array);
+       while (it.OK()) {
+               char c = it.GetChar();
+               if (c == LM_TC_TAB || c == LM_TC_CR) 
+                       it.Delete();
+               else
+                       it.Next();
        }
 }
 
@@ -84,15 +73,15 @@ MathParInset::draw(Painter & pain, int x, int y)
 {
        byte cxp = 0;
        int xp = 0;
-       int asc = df_asc, des = 0;
+       int asc = df_asc;
+       int des = 0;
        bool limits = false;
        
-       xo = x;  yo = y; 
-       if (!array || array->empty()) {
-               if (array) {
-                       MathedXIter data(this);
-                       data.GetPos(x, y);
-               }
+       xo_ = x;
+       yo_ = y; 
+       if (array.empty()) {
+               MathedXIter data(this);
+               data.GetPos(x, y);
                pain.rectangle(x, y - df_asc, df_width, df_asc, LColor::mathline);
                return;
        }  
@@ -103,8 +92,8 @@ MathParInset::draw(Painter & pain, int x, int y)
                byte cx = data.GetChar();
                if (cx >= ' ') {
                        string s = data.GetString();
-                       drawStr(pain, data.FCode(), size, x, y, s);
-                       mathed_char_height(LM_TC_CONST, size, 'y', asc, des);
+                       drawStr(pain, data.fcode(), size(), x, y, s);
+                       mathed_char_height(LM_TC_CONST, size(), 'y', asc, des);
                        limits = false;
                }
                else {
@@ -184,8 +173,7 @@ MathParInset::Metrics()
        ascent = df_asc;//mathed_char_height(LM_TC_VAR, size, 'I', asc, des); 
        width = df_width;
        descent = 0;
-       if (!array) return;
-       if (array->empty()) return;
+       if (array.empty()) return;
        
        ascent = 0;
        MathedXIter data(this);
@@ -194,15 +182,16 @@ MathParInset::Metrics()
                cx = data.GetChar();      
                if (cx >= ' ') {
                        string s = data.GetString();
-                       mathed_string_height(data.FCode(), size, s, asc, des);
+                       mathed_string_height(data.fcode(),
+                                            size(), s, asc, des);
                        if (asc > ascent) ascent = asc;
                        if (des > descent) descent = des;
                        limits = false;
-                       mathed_char_height(LM_TC_CONST, size, 'y', asc, des);
+                       mathed_char_height(LM_TC_CONST, size(), 'y', asc, des);
                } else
                        if (MathIsInset(cx)) {
                                MathedInset * p = data.GetInset();
-                               p->SetStyle(size);   
+                               p->SetStyle(size());   
                                p->Metrics();
                                if (cx == LM_TC_UP) {
                                        asc += (limits) ? p->Height() + 4: p->Ascent() + 
@@ -280,16 +269,16 @@ MathParInset::Metrics()
 
 void MathParInset::Write(ostream & os, bool fragile)
 {
-       if (!array) return;
+       if (array.empty()) return;
        int brace = 0;
-       latexkeys * l;
-       MathedIter data(array);
+       latexkeys const * l;
+       MathedIter data(&array);
        // hack
        MathedRowSt const * crow = getRowSt();   
        data.Reset();
        
        if (!Permit(LMPF_FIXED_SIZE)) { 
-               l = lm_get_key_by_id(size, LM_TK_STY);
+               l = lm_get_key_by_id(size(), LM_TK_STY);
                if (l) {
                        os << '\\' << l->name << ' ';
                }
@@ -299,39 +288,39 @@ void MathParInset::Write(ostream & os, bool fragile)
                if (cx >= ' ') {
                        string str = data.GetString();
                        
-                       if (data.FCode() >= LM_TC_RM && data.FCode() <= LM_TC_TEXTRM) {
-                               os << '\\' << math_font_name[data.FCode()-LM_TC_RM] << '{';
+                       if (data.fcode() >= LM_TC_RM && data.fcode() <= LM_TC_TEXTRM) {
+                               os << '\\' << math_font_name[data.fcode()-LM_TC_RM] << '{';
                        }
                        for (string::const_iterator s = str.begin();
                             s != str.end(); ++s) {
                                byte c = *s;
-                               if (MathIsSymbol(data.FCode())) {
-                                       l = lm_get_key_by_id(c, (data.FCode() == LM_TC_BSYM) ?
+                               if (MathIsSymbol(data.fcode())) {
+                                       l = lm_get_key_by_id(c, (data.fcode() == LM_TC_BSYM) ?
                                                             LM_TK_BIGSYM : LM_TK_SYM);
                                        if (l) {
                                                os << '\\' << l->name << ' ';
                                        } else {
 #warning this does not compile on gcc 2.97
                                                //lyxerr << "Illegal symbol code[" << c
-                                               //   << " " << str.end() - s << " " << data.FCode() << "]";
+                                               //   << " " << str.end() - s << " " << data.fcode() << "]";
                                        }
                                } else {
                                        // Is there a standard logical XOR?
-                                       if ((data.FCode() == LM_TC_TEX && c != '{' && c != '}') ||
-                                           (data.FCode() == LM_TC_SPECIAL))
+                                       if ((data.fcode() == LM_TC_TEX && c != '{' && c != '}') ||
+                                           (data.fcode() == LM_TC_SPECIAL))
                                                os << '\\';
                                        else {
                                                if (c == '{') ++brace;
                                                if (c == '}') --brace;
                                        }
-                                       if (c == '}' && data.FCode() == LM_TC_TEX && brace < 0) 
+                                       if (c == '}' && data.fcode() == LM_TC_TEX && brace < 0) 
                                                lyxerr <<"Math warning: Unexpected closing brace."
                                                       << endl;
                                        else           
                                                os << char(c);
                                }
                        }
-                       if (data.FCode()>= LM_TC_RM && data.FCode()<= LM_TC_TEXTRM)
+                       if (data.fcode()>= LM_TC_RM && data.fcode()<= LM_TC_TEXTRM)
                                os << '}';
                } else     
                        if (MathIsInset(cx)) {
@@ -392,3 +381,63 @@ void MathParInset::Write(ostream & os, bool fragile)
        if (brace > 0)
                os << string(brace, '}');
 }
+
+
+void MathParInset::clear()
+{
+       array.clear();
+}
+
+bool MathParInset::Inside(int x, int y) 
+{
+  return (x >= xo() && x <= xo() + width
+         && y <= yo() + descent && y >= yo() - ascent);
+}
+
+
+void MathParInset::GetXY(int & x, int & y) const
+{
+   x = xo();
+   y = yo();
+}
+
+
+void MathParInset::UserSetSize(short sz)
+{
+   if (sz >= 0) {
+       size(sz);      
+       flag = flag & ~LMPF_FIXED_SIZE;
+   }
+}
+
+
+void MathParInset::SetStyle(short sz) 
+{
+    if (Permit(LMPF_FIXED_SIZE)) {
+       if (Permit(LMPF_SCRIPT)) 
+         sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
+       if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
+           ++sz;
+       } 
+       MathedInset::SetStyle(sz);
+    }
+}
+
+
+bool MathParInset::Permit(short f) const
+{
+       return bool(f & flag);
+}
+
+
+MathedArray * MathParInset::GetData()
+{
+       return &array;
+}
+
+
+void MathParInset::setXY(int x, int y)
+{
+       xo_ = x;
+       yo_ = y;
+}