]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_inset.C
Always use std::endl with lyxerr
[lyx.git] / src / mathed / math_inset.C
index 0882396fc187899dc7b6b274e427185cd5535709..1a9b26d3e5d3924af921f8105d1240eea70ff8fd 100644 (file)
@@ -1,9 +1,9 @@
 /*
  *  File:        math_inset.C
  *  Purpose:     Implementation of insets for mathed
- *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
+ *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
  *  Created:     January 1996
- *  Description: 
+ *  Description:
  *
  *  Dependencies: Xlib, XForms
  *
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation "math_inset.h"
-#endif
-
-#include "math_iter.h"
 #include "math_inset.h"
-#include "symbol_def.h"
+#include "math_scriptinset.h"
+#include "math_mathmlstream.h"
+#include "math_cursor.h"
+#include "debug.h"
+
+#include "frontends/LyXView.h"
+#include "frontends/Dialogs.h"
+#include "BufferView.h"
+#include "formulabase.h"
+
+using std::ostream;
+using std::endl;
 
 
-MathedInset::MathedInset(MathedInset * inset) 
+BufferView * MathInset::view() const
 {
-   if (inset) {
-      name = inset->GetName();
-      objtype = inset->GetType();
-      size = inset->GetStyle();
-      width = inset->Width();
-      ascent = inset->Ascent();
-      descent = inset->Descent();
-   } else {
-      objtype = LM_OT_UNDEF;
-      size = LM_ST_TEXT;
-      width = ascent = descent = 0;
-      //name = 0;
-   }
+       return mathcursor ? mathcursor->formula()->view() : 0;
 }
 
 
-MathFuncInset::MathFuncInset(string const & nm, short ot, short st)
-       : MathedInset("", ot, st)
+MathInset::size_type MathInset::nargs() const
 {
-   ln = 0;
-   lims = (GetType() == LM_OT_FUNCLIM);
-    if (GetType() == LM_OT_UNDEF) {
-       fname = nm;
-       SetName(fname);
-    } else {
-       //fname = 0;
-       SetName(nm);
-    }
+       return 0;
 }
 
 
-MathedInset * MathFuncInset::Clone()
+MathArray dummyCell;
+
+MathArray & MathInset::cell(idx_type)
 {
-   return new MathFuncInset(name, GetType(), GetStyle());
+       lyxerr << "I don't have a cell 1" << endl;
+       return dummyCell;
 }
 
 
-MathSpaceInset::MathSpaceInset(int sp, short ot, short st)
-       : MathedInset("", ot, st), space(sp)
-{}
+MathArray const & MathInset::cell(idx_type) const
+{
+       lyxerr << "I don't have a cell 2" << endl;
+       return dummyCell;
+}
 
 
-MathedInset * MathSpaceInset::Clone()
+MathInset::idx_type MathInset::index(row_type row, col_type col) const
 {
-   return new MathSpaceInset(space, GetType(), GetStyle());
+       if (row != 0)
+               lyxerr << "illegal row: " << row << endl;
+       if (col != 0)
+               lyxerr << "illegal col: " << col << endl;
+       return 0;
 }
 
+void MathInset::substitute(MathMacro const &)
+{}
+
 
-MathParInset::MathParInset(short st, string const & nm, short ot)
-       : MathedInset(nm, ot, st)
+bool MathInset::idxNext(idx_type &, pos_type &) const
 {
-    array = 0;
-    ascent = 8;
-    width = 4;
-    descent = 0;
-    flag = 1;
-    if (objtype == LM_OT_SCRIPT)
-      flag |= LMPF_SCRIPT;
+       return false;
 }
 
 
-MathParInset::MathParInset(MathParInset * p)
-       : MathedInset(p)
+bool MathInset::idxRight(idx_type &, pos_type &) const
 {
-    flag = p->flag;
-    p->setArgumentIdx(0);
-    MathedIter it(p->GetData());
-    SetData(it.Copy());
+       return false;
 }
 
 
-MathParInset::~MathParInset()
+bool MathInset::idxPrev(idx_type &, pos_type &) const
 {
-   if (array) {
-      MathedIter it(array);
-      it.Clear();
-      delete array;
-   }
+       return false;
 }
 
 
-MathedInset * MathParInset::Clone()
+bool MathInset::idxLeft(idx_type &, pos_type &) const
 {
-   return new MathParInset(this);
+       return false;
 }
 
 
-void MathParInset::SetData(MathedArray * a)
+bool MathInset::idxUpDown(idx_type &, pos_type &, bool, int) const
 {
-    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();
-       }
-   }
+       return false;
 }
 
 
-MathSqrtInset::MathSqrtInset(short st)
-       : MathParInset(st, "sqrt", LM_OT_SQRT) {}
+bool MathInset::idxUpDown2(idx_type &, pos_type &, bool, int) const
+{
+       return false;
+}
 
 
-MathedInset * MathSqrtInset::Clone()
-{   
-   MathSqrtInset * p = new MathSqrtInset(GetStyle());
-   MathedIter it(array);
-   p->SetData(it.Copy());
-   return p;
+bool MathInset::idxFirst(idx_type &, pos_type &) const
+{
+       return false;
 }
 
 
-bool MathSqrtInset::Inside(int x, int y) 
+bool MathInset::idxLast(idx_type &, pos_type &) const
 {
-       return x >= xo - hmax
-               && x <= xo + width - hmax
-               && y <= yo + descent
-               && y >= yo - ascent;
+       return false;
 }
 
 
-MathDelimInset::MathDelimInset(int l, int r, short st)
-       : MathParInset(st, "", LM_OT_DELIM), left(l), right(r) {}
+bool MathInset::idxHome(idx_type &, pos_type &) const
+{
+       return false;
+}
 
 
-MathedInset * MathDelimInset::Clone()
-{   
-   MathDelimInset * p = new MathDelimInset(left, right, GetStyle());
-   MathedIter it(array);
-   p->SetData(it.Copy());
-   return p;
+bool MathInset::idxEnd(idx_type &, pos_type &) const
+{
+       return false;
 }
 
 
-MathDecorationInset::MathDecorationInset(int d, short st)
-       : MathParInset(st, "", LM_OT_DECO), deco(d)
+void MathInset::getPos(idx_type, pos_type, int & x, int & y) const
 {
-   upper = (deco!= LM_underline && deco!= LM_underbrace);
+       lyxerr << "MathInset::getPos() called directly!" << endl;
+       x = y = 0;
 }
 
 
-MathedInset * MathDecorationInset::Clone()
-{   
-   MathDecorationInset * p = new MathDecorationInset(deco, GetStyle());
-   MathedIter it(array);
-   p->SetData(it.Copy());
-   return p;
+void MathInset::dump() const
+{
+       lyxerr << "---------------------------------------------" << endl;
+       WriteStream wi(lyxerr, false, true);
+       write(wi);
+       lyxerr << "\n---------------------------------------------" << endl;
 }
 
 
-MathFracInset::MathFracInset(short ot)
-       : MathParInset(LM_ST_TEXT, "frac", ot)
+bool MathInset::idxBetween(idx_type idx, idx_type from, idx_type to) const
 {
-       
-    den = new MathParInset(LM_ST_TEXT); // this leaks
-    dh = 0;
-    idx = 0;
-    if (objtype == LM_OT_STACKREL) {
-       flag |= LMPF_SCRIPT;
-       SetName("stackrel");
-    }
+       return from <= idx && idx <= to;
 }
 
 
-MathFracInset::~MathFracInset()
+void MathInset::drawSelection(PainterInfo &,
+       idx_type, pos_type, idx_type, pos_type) const
 {
-    delete den;
+       lyxerr << "MathInset::drawSelection() called directly!" << endl;
 }
 
 
-MathedInset * MathFracInset::Clone()
-{   
-    MathFracInset * p = new MathFracInset(GetType());
-    MathedIter itn(array);
-    MathedIter itd(den->GetData());
-    p->SetData(itn.Copy(), itd.Copy());
-    p->idx = idx;
-    p->dh = dh;
-   return p;
+void MathInset::metricsT(TextMetricsInfo const &, Dimension &) const
+{
+#ifdef WITH_WARNINGS
+       lyxerr << "MathInset::metricsT(Text) called directly!" << endl;
+#endif
 }
 
 
-bool MathFracInset::setArgumentIdx(int i)
+void MathInset::drawT(TextPainter &, int, int) const
 {
-   if (i == 0 || i == 1) {
-       idx = i;
-       return true;
-   } else 
-      return false;
+#ifdef WITH_WARNINGS
+       lyxerr << "MathInset::drawT(Text) called directly!" << endl;
+#endif
 }
 
 
-void MathFracInset::SetStyle(short st)
+
+void MathInset::write(WriteStream & os) const
 {
-    MathParInset::SetStyle(st);
-    dh = 0;
-    den->SetStyle((size == LM_ST_DISPLAY) ?
-                 static_cast<short>(LM_ST_TEXT)
-                 : size);
+       os << '\\' << name().c_str();
+       os.pendingSpace(true);
 }
 
 
-void MathFracInset::SetData(MathedArray * n, MathedArray * d)
+void MathInset::normalize(NormalStream & os) const
 {
-   den->SetData(d);
-   MathParInset::SetData(n);
+       os << '[' << name().c_str() << "] ";
 }
 
 
-void MathFracInset::SetData(MathedArray * d)
+void MathInset::octave(OctaveStream & os) const
 {
-   if (idx == 0)
-     MathParInset::SetData(d);
-   else {
-      den->SetData(d);
-   }
+       NormalStream ns(os.os());
+       normalize(ns);
 }
 
 
-void MathFracInset::GetXY(int & x, int & y) const
-{  
-   if (idx == 0)
-     MathParInset::GetXY(x, y);
-   else
-     den->GetXY(x, y);
+void MathInset::maple(MapleStream & os) const
+{
+       NormalStream ns(os.os());
+       normalize(ns);
 }
 
 
-MathedArray * MathFracInset::GetData()
+void MathInset::maxima(MaximaStream & os) const
 {
-   if (idx == 0)
-     return array;
-   else
-     return den->GetData();
+       MapleStream ns(os.os());
+       maple(ns);
 }
 
 
-bool MathFracInset::Inside(int x, int y) 
+void MathInset::mathematica(MathematicaStream & os) const
 {
-    int xx = xo - (width - w0) / 2;
-    
-    return x >= xx && x <= xx + width && y <= yo + descent && y >= yo - ascent;
+       NormalStream ns(os.os());
+       normalize(ns);
 }
 
 
-void MathFracInset::SetFocus(int /*x*/, int y)
-{  
-//    lyxerr << "y " << y << " " << yo << " " << den->yo << " ";
-    idx = (y > yo) ? 1: 0;
+void MathInset::mathmlize(MathMLStream & os) const
+{
+       NormalStream ns(os.os());
+       normalize(ns);
 }
 
 
-MathMatrixInset::MathMatrixInset(int m, int n, short st)
-       : MathParInset(st, "array", LM_OT_MATRIX), nc(m), nr(0), ws_(m),
-       v_align(0), h_align(nc, 'c')
+int MathInset::ascii(std::ostream &, int) const
 {
-    row = 0;
-    flag = 15;
-    if (n > 0) {
-           row = new MathedRowSt(nc+1);
-       MathedXIter it(this);
-       for (int j = 1; j < n; ++j) it.addRow();
-       nr = n;
-       if (nr == 1 && nc > 1) {
-           for (int j = 0; j < nc - 1; ++j) 
-             it.Insert('T', LM_TC_TAB);
-       }
-    } else if (n < 0) {
-           row = new MathedRowSt(nc + 1);
-       nr = 1;
-    }
+       return 0;
 }
 
 
-MathMatrixInset::MathMatrixInset(MathMatrixInset * mt)
-       : MathParInset(mt->GetStyle(), mt->GetName(), mt->GetType()),
-         nc(mt->nc), nr(0), ws_(mt->nc), v_align(mt->v_align), h_align(mt->h_align)
+int MathInset::linuxdoc(std::ostream &) const
 {
-    MathedIter it;
-    it.SetData(mt->GetData());
-    array = it.Copy();
-    if (mt->row != 0) {
-       MathedRowSt * r, * ro= 0, * mrow = mt->row;
-       //mrow = mt->row; // This must be redundant...
-       while (mrow) {
-           r = new MathedRowSt(nc + 1);
-           r->setNumbered(mrow->isNumbered());
-           //if (mrow->label) 
-             r->setLabel(mrow->getLabel());
-           if (!ro) 
-             row = r;
-           else
-             ro->setNext(r);
-           mrow = mrow->getNext();
-           ro = r;
-           ++nr;
-       } 
-    } else         
-      row = 0;
-    flag = mt->flag;
+       return 0;
 }
 
 
-MathMatrixInset::~MathMatrixInset()
+int MathInset::docbook(std::ostream &, bool) const
 {
-    MathedRowSt * r = row;
-    while (r) {
-       MathedRowSt * q = r->getNext();
-       delete r;
-       r = q;
-    }
-}
-
-
-MathedInset * MathMatrixInset::Clone()
-{
-    return new MathMatrixInset(this);
-}
-
-
-void MathMatrixInset::SetAlign(char vv, string const & hh)
-{
-   v_align = vv;
-   h_align = hh.substr(0, nc); // usr just h_align = hh; perhaps
-}
-
-
-// Check the number of tabs and crs
-void MathMatrixInset::SetData(MathedArray * a)
-{
-    if (!a) return;
-    MathedIter it(a);
-    int nn = nc - 1;
-    nr = 1;
-    // count tabs per row
-    while (it.OK()) {
-       if (it.IsTab()) {
-           if (nn < 0) { 
-               it.Delete();
-               continue;
-           } else {
-//           it.Next();
-               --nn;
-           }
-       }
-       if (it.IsCR()) {
-           while (nn > 0) {
-               it.Insert(' ', LM_TC_TAB);
-               --nn;
-           }
-           nn = nc - 1;
-           ++nr;
-       }
-       it.Next();
-    }
-    it.Reset();
-
-    // Automatically inserts tabs around bops
-    // DISABLED because it's very easy to insert tabs 
-    array = a;
-}
-
-
-void MathMatrixInset::draw(Painter & pain, int x, int baseline)
-{
-    MathParInset::draw(pain, x, baseline);
-}
-
-
-void MathMatrixInset::Metrics()
-{
-    int i, hl, h = 0;
-    MathedRowSt * cprow= 0;
-
-    if (!row) {
-//     lyxerr << " MIDA ";
-       MathedXIter it(this);
-       row = it.adjustVerticalSt();
-    } 
-    
-    // Clean the arrays      
-    MathedRowSt * cxrow = row;
-    while (cxrow) {   
-       for (i = 0; i <= nc; ++i) cxrow->setTab(i, 0);
-       cxrow = cxrow->getNext();
-    }
-    
-    // Basic metrics
-    MathParInset::Metrics();
-           
-    if (nc <= 1 && !row->getNext()) {
-       row->ascent(ascent);
-       row->descent(descent);
-    }
-    
-    // Vertical positions of each row
-    cxrow = row;     
-    while (cxrow) {
-       for (i = 0; i < nc; ++i) {
-           if (cxrow == row || ws_[i] < cxrow->getTab(i))
-                   ws_[i] = cxrow->getTab(i);
-           if (cxrow->getNext() == 0 && ws_[i] == 0) ws_[i] = df_width;
-       }
-       
-       cxrow->setBaseline((cxrow == row) ?
-                          cxrow->ascent() :
-                  cxrow->ascent() + cprow->descent()
-                          + MATH_ROWSEP + cprow->getBaseline());
-       h += cxrow->ascent() + cxrow->descent() + MATH_ROWSEP;  
-       cprow = cxrow;
-       cxrow = cxrow->getNext();
-    }
-    
-    hl = Descent();
-    h -= MATH_ROWSEP;
-
-    //  Compute vertical align
-    switch (v_align) {
-     case 't': ascent = row->getBaseline(); break;
-     case 'b': ascent = h - hl; break;
-     default:  ascent = (row->getNext()) ? h / 2: h - hl; break;
-    }
-    descent = h - ascent + 2;
-    
-    // Increase ws_[i] for 'R' columns (except the first one)
-    for (i = 1; i < nc; ++i)
-       if (h_align[i] == 'R')
-           ws_[i] += 10*df_width;
-    // Increase ws_[i] for 'C' column
-    if (h_align[0] == 'C')
-       if (ws_[0] < 7*workWidth/8)
-           ws_[0] = 7*workWidth/8;
-
-   // Adjust local tabs
-    cxrow = row;
-    width = MATH_COLSEP;
-    while (cxrow) {   
-           int rg = MATH_COLSEP, ww, lf = 0; //, * w = cxrow->w;
-       for (i = 0; i < nc; ++i) {
-           bool isvoid = false;
-           if (cxrow->getTab(i) <= 0) {
-               cxrow->setTab(i, df_width);
-               isvoid = true;
-           }
-           switch (h_align[i]) {
-           case 'l':
-               lf = 0;
-               break;
-           case 'c':
-               lf = (ws_[i] - cxrow->getTab(i))/2; 
-               break;
-           case 'r':
-           case 'R':
-               lf = ws_[i] - cxrow->getTab(i);
-               break;
-           case 'C':
-               if (cxrow == row)
-                   lf = 0;
-               else if (!cxrow->getNext())
-                    lf = ws_[i] - cxrow->getTab(i);
-               else
-                   lf = (ws_[i] - cxrow->getTab(i))/2; 
-               break;
-           }
-           ww = (isvoid) ? lf : lf + cxrow->getTab(i);
-           cxrow->setTab(i, lf + rg);
-           rg = ws_[i] - ww + MATH_COLSEP;
-           if (cxrow == row) width += ws_[i] + MATH_COLSEP;
-       }
-       cxrow->setBaseline(cxrow->getBaseline() - ascent);
-       cxrow = cxrow->getNext();
-    }
-}
-
-
-MathAccentInset::MathAccentInset(byte cx, MathedTextCodes f, int cd, short st)
-       : MathedInset("", LM_OT_ACCENT, st), c(cx), fn(f), code(cd)
-{
-    inset = 0;
-}
-
-
-MathAccentInset::MathAccentInset(MathedInset *ins, int cd, short st)
-       : MathedInset("", LM_OT_ACCENT, st),
-         c(0), fn(LM_TC_MIN), code(cd), inset(ins) {}
-
-
-MathAccentInset::~MathAccentInset()
-{
-       delete inset;
-}
-
-
-MathedInset * MathAccentInset::Clone()
-{   
-    MathAccentInset * p;
-    
-    if (inset) 
-      p = new MathAccentInset(inset->Clone(), code, GetStyle());
-    else
-      p = new MathAccentInset(c, fn, code, GetStyle());
-    
-    return p;
-}
-
-
-MathBigopInset::MathBigopInset(string const & nam, int id, short st)
-       : MathedInset(nam, LM_OT_BIGOP, st), sym(id)
+       return 0;
+}
+
+
+string const & MathInset::getType() const
 {
-   lims = -1;
+       static string const t("none");
+       return t;
 }
 
 
-MathedInset * MathBigopInset::Clone()
+string MathInset::name() const
 {
-   return new MathBigopInset(name, sym, GetStyle());
+       return "unknown";
 }
 
 
-MathDotsInset::MathDotsInset(string const & nam, int id, short st)
-       : MathedInset(nam, LM_OT_DOTS, st), code(id) {}
+ostream & operator<<(ostream & os, MathAtom const & at)
+{
+       WriteStream wi(os, false, false);
+       at->write(wi);
+       return os;
+}
 
 
-MathedInset * MathDotsInset::Clone()
+string MathInset::fileInsetLabel() const
 {
-   return new MathDotsInset(name, code, GetStyle());
-}     
+       return "Formula";
+}