X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2Fmath_inset.C;h=d4dc10b1a519cd50d8e1547f1800a4746e95a787;hb=4590c8cfab02a3bc56813cfb1f2e80bd1119af9e;hp=dfcce43cd83360797b01f5bc4644c91eb1454747;hpb=45a03f4f67bb00f8142e465c615f348f0622eb32;p=lyx.git diff --git a/src/mathed/math_inset.C b/src/mathed/math_inset.C index dfcce43cd8..d4dc10b1a5 100644 --- a/src/mathed/math_inset.C +++ b/src/mathed/math_inset.C @@ -18,540 +18,155 @@ #include #ifdef __GNUG__ -#pragma implementation "math_inset.h" +#pragma implementation #endif +#include "debug.h" #include "math_iter.h" #include "math_inset.h" #include "symbol_def.h" +#include "lyxfont.h" +#include "mathed/support.h" +#include "Painter.h" -static inline -char * strnew(char const * s) -{ - char * s1 = new char[strlen(s)+1]; - return strcpy(s1, s); -} - - -MathedInset::MathedInset(MathedInset * inset) -{ - 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; - } -} - - -MathFuncInset::MathFuncInset(string const & nm, short ot, short st) - : MathedInset("", ot, st) -{ - ln = 0; - lims = (GetType() == LM_OT_FUNCLIM); - if (GetType() == LM_OT_UNDEF) { - fname = nm; - SetName(fname); - } else { - //fname = 0; - SetName(nm); - } -} - - -MathedInset * MathFuncInset::Clone() -{ - return new MathFuncInset(name, GetType(), GetStyle()); -} +// Initialize some static class variables. +int MathedInset::df_asc; +int MathedInset::df_des; +int MathedInset::df_width; +int MathedInset::workWidth; -MathSpaceInset::MathSpaceInset(int sp, short ot, short st) - : MathedInset("", ot, st), space(sp) +MathedInset::MathedInset(string const & nm, short ot, short st) + : name(nm), objtype(ot), width(0), ascent(0), descent(0), + size_(st) {} -MathedInset * MathSpaceInset::Clone() -{ - return new MathSpaceInset(space, GetType(), GetStyle()); -} - - -MathParInset::MathParInset(short st, string const & nm, short ot) - : MathedInset(nm, ot, st) -{ - array = 0; - ascent = 8; - width = 4; - descent = 0; - flag = 1; - if (objtype == LM_OT_SCRIPT) - flag |= LMPF_SCRIPT; -} - - -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); -} - - -void MathParInset::SetData(LyxArrayBase * a) -{ - 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(); - } - } -} - - -MathSqrtInset::MathSqrtInset(short st) - : MathParInset(st, "sqrt", LM_OT_SQRT) {} - - -MathedInset * MathSqrtInset::Clone() -{ - MathSqrtInset * p = new MathSqrtInset(GetStyle()); - MathedIter it(array); - p->SetData(it.Copy()); - return p; -} - - -bool MathSqrtInset::Inside(int x, int y) -{ - return x >= xo - hmax - && x <= xo + width - hmax - && y <= yo + descent - && y >= yo - ascent; -} - - -MathDelimInset::MathDelimInset(int l, int r, short st) - : MathParInset(st, "", LM_OT_DELIM), left(l), right(r) {} - - -MathedInset * MathDelimInset::Clone() -{ - MathDelimInset * p = new MathDelimInset(left, right, GetStyle()); - MathedIter it(array); - p->SetData(it.Copy()); - return p; -} - - -MathDecorationInset::MathDecorationInset(int d, short st) - : MathParInset(st, "", LM_OT_DECO), deco(d) -{ - upper = (deco!= LM_underline && deco!= LM_underbrace); -} - - -MathedInset * MathDecorationInset::Clone() -{ - MathDecorationInset * p = new MathDecorationInset(deco, GetStyle()); - MathedIter it(array); - p->SetData(it.Copy()); - return p; -} - - -MathFracInset::MathFracInset(short ot) - : MathParInset(LM_ST_TEXT, "frac", ot) -{ +// In a near future maybe we use a better fonts renderer +void MathedInset::drawStr(Painter & pain, short type, int siz, + int x, int y, string const & s) +{ + string st; + if (MathIsBinary(type)) + for (string::const_iterator it = s.begin(); + it != s.end(); ++it) { + st += ' '; + st += *it; + st += ' '; + } + else + st = s; - den = new MathParInset(LM_ST_TEXT); // this leaks - dh = 0; - idx = 0; - if (objtype == LM_OT_STACKREL) { - flag |= LMPF_SCRIPT; - SetName("stackrel"); - } -} - - -MathFracInset::~MathFracInset() -{ - delete den; + LyXFont const mf = mathed_get_font(type, siz); + pain.text(x, y, st, mf); } - -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; -} - - -bool MathFracInset::setArgumentIdx(int i) +void MathedInset::substitute(MathMacro *) { - if (i == 0 || i == 1) { - idx = i; - return true; - } else - return false; } -void MathFracInset::SetStyle(short st) +int MathedInset::Ascent() const { - MathParInset::SetStyle(st); - dh = 0; - den->SetStyle((size == LM_ST_DISPLAY) ? - static_cast(LM_ST_TEXT) - : size); + return ascent; } -void MathFracInset::SetData(LyxArrayBase * n, LyxArrayBase * d) +int MathedInset::Descent() const { - den->SetData(d); - MathParInset::SetData(n); + return descent; } -void MathFracInset::SetData(LyxArrayBase * d) +int MathedInset::Width() const { - if (idx == 0) - MathParInset::SetData(d); - else { - den->SetData(d); - } + return width; } -void MathFracInset::GetXY(int & x, int & y) const -{ - if (idx == 0) - MathParInset::GetXY(x, y); - else - den->GetXY(x, y); -} - - -LyxArrayBase * MathFracInset::GetData() +int MathedInset::Height() const { - if (idx == 0) - return array; - else - return den->GetData(); + return ascent + descent; } -bool MathFracInset::Inside(int x, int y) +bool MathedInset::GetLimits() const { - int xx = xo - (width - w0) / 2; - - return x >= xx && x <= xx + width && y <= yo + descent && y >= yo - ascent; + return false; } -void MathFracInset::SetFocus(int /*x*/, int y) -{ -// lyxerr << "y " << y << " " << yo << " " << den->yo << " "; - idx = (y > yo) ? 1: 0; -} +void MathedInset::SetLimits(bool) {} -MathMatrixInset::MathMatrixInset(int m, int n, short st) - : MathParInset(st, "array", LM_OT_MATRIX), nc(m) +string const & MathedInset::GetName() const { - ws = new int[nc]; - v_align = 0; - h_align = new char[nc + 1]; - for (int i = 0; i < nc; ++i) h_align[i] = 'c'; - h_align[nc] = '\0'; - nr = 0; - 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 name; } -MathMatrixInset::MathMatrixInset(MathMatrixInset * mt) - : MathParInset(mt->GetStyle(), mt->GetName(), mt->GetType()) +short MathedInset::GetType() const { - nr = 0; - nc = mt->nc; - ws = new int[nc]; - h_align = new char[nc + 1]; - strcpy(h_align, mt->GetAlign(&v_align)); - 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->numbered = mrow->numbered; - //if (mrow->label) - r->label = mrow->label; - if (!ro) - row = r; - else - ro->next = r; - mrow = mrow->next; - ro = r; - ++nr; - } - } else - row = 0; - flag = mt->flag; + return objtype; } -MathMatrixInset::~MathMatrixInset() +short MathedInset::GetStyle() const { - delete[] ws; - - MathedRowSt * r = row; - while (r) { - MathedRowSt * q = r->next; - delete r; - r = q; - } + return size_; } -MathedInset * MathMatrixInset::Clone() +void MathedInset::SetType(short t) { - return new MathMatrixInset(this); + objtype = t; } -void MathMatrixInset::SetAlign(char vv, char const * hh) +void MathedInset::SetStyle(short st) { - v_align = vv; - strncpy(h_align, hh, nc); + size_ = st; } -// Check the number of tabs and crs -void MathMatrixInset::SetData(LyxArrayBase * a) +void MathedInset::SetName(string const & n) { - 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; + name = n; } -void MathMatrixInset::draw(Painter & pain, int x, int baseline) +void MathedInset::defaultAscent(int da) { - MathParInset::draw(pain, x, baseline); + df_asc = da; } -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->w[i] = 0; - cxrow = cxrow->next; - } - - // Basic metrics - MathParInset::Metrics(); - - if (nc <= 1 && !row->next) { - row->asc = ascent; - row->desc = descent; - } - - // Vertical positions of each row - cxrow = row; - while (cxrow) { - for (i = 0; i < nc; ++i) { - if (cxrow == row || ws[i]w[i]) ws[i]= cxrow->w[i]; - if (cxrow->next == 0 && ws[i] == 0) ws[i] = df_width; - } - - cxrow->y = (cxrow == row) ? cxrow->asc: - cxrow->asc + cprow->desc + MATH_ROWSEP + cprow->y; - h += cxrow->asc + cxrow->desc + MATH_ROWSEP; - cprow = cxrow; - cxrow = cxrow->next; - } - - hl = Descent(); - h -= MATH_ROWSEP; - - // Compute vertical align - switch (v_align) { - case 't': ascent = row->y; break; - case 'b': ascent = h - hl; break; - default: ascent = (row->next) ? h / 2: h - hl; break; - } - descent = h - ascent + 2; - - - // 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 (w[i] <= 0) { - w[i] = df_width; - isvoid = true; - } - switch (h_align[i]) { - case 'l': lf = 0; break; - case 'c': lf = (ws[i] - w[i])/2; - break; - case 'r': lf = ws[i] - w[i]; break; - } - ww = (isvoid) ? lf: lf + w[i]; - w[i] = lf + rg; - rg = ws[i] - ww + MATH_COLSEP; - if (cxrow == row) width += ws[i] + MATH_COLSEP; - } - cxrow->y -= ascent; - cxrow = cxrow->next; - } -} - -MathAccentInset::MathAccentInset(byte cx, MathedTextCodes f, int cd, short st) - : MathedInset("", LM_OT_ACCENT, st), c(cx), fn(f), code(cd) +void MathedInset::defaultDescent(int dd) { - inset = 0; + df_des = dd; } -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() +void MathedInset::defaultWidth(int dw) { - 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; + df_width = dw; } -MathBigopInset::MathBigopInset(string const & nam, int id, short st) - : MathedInset(nam, LM_OT_BIGOP, st), sym(id) +short MathedInset::size() const { - lims = -1; + return size_; } -MathedInset * MathBigopInset::Clone() +void MathedInset::size(short s) { - return new MathBigopInset(name, sym, GetStyle()); + size_ = s; } - -MathDotsInset::MathDotsInset(string const & nam, int id, short st) - : MathedInset(nam, LM_OT_DOTS, st), code(id) {} - - -MathedInset * MathDotsInset::Clone() +void MathedInset::incSize() { - return new MathDotsInset(name, code, GetStyle()); -} + ++size_; +}