]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathHull.cpp
Reimplement support for numbering of equation previews
[lyx.git] / src / mathed / InsetMathHull.cpp
index b8808959dee466f49cc03263e7e7f9dc9758a3ff..8ea552dd29ef15ed6f4b6cec1b59639b290a2d63 100644 (file)
@@ -253,20 +253,9 @@ void InsetMathHull::updateBuffer(ParIterator const & it, UpdateType utype, bool
                Counters & cnts =
                        buffer_->masterBuffer()->params().documentClass().counters();
 
-               // right now, we only need to do this at export time
-               if (utype == OutputUpdate) {
-                       for (size_t i = 0; i < numcnts; ++i) {
-                               docstring const cnt = from_ascii(counters_to_save[i]);
-                               if (cnts.hasCounter(cnt))
-                                       counter_map[cnt] = cnts.value(cnt);
-                       }
-               }
-
                // this has to be done separately
                docstring const eqstr = from_ascii("equation");
                if (cnts.hasCounter(eqstr)) {
-                       if (utype == OutputUpdate)
-                               counter_map[eqstr] = cnts.value(eqstr);
                        for (size_t i = 0; i != label_.size(); ++i) {
                                if (numbered(i)) {
                                        Paragraph const & par = it.paragraph();
@@ -520,9 +509,9 @@ void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
                int extra_offset = 0;
                for (row_type row = 0; row < nrows(); ++row) {
                        rowinfo(row).offset[mi.base.bv] += extra_offset;
-                       if (!numbered(row))
-                               continue;
                        docstring const nl = nicelabel(row);
+                       if (nl.empty())
+                               continue;
                        Dimension dimnl;
                        mathed_string_dim(mi.base.font, nl, dimnl);
                        int const ind = indent(*mi.base.bv);
@@ -589,13 +578,9 @@ void InsetMathHull::drawBackground(PainterInfo & pi, int x, int y) const
                    dim.wid, dim.asc + dim.des, backgroundColor(pi));
                return;
        }
-       // If there are numbers, the margins around the (displayed)
-       // equation have to be cleared.
-       if (numberedType())
-               pi.pain.fillRectangle(pi.leftx, y - dim.asc,
-                               pi.rightx - pi.leftx, dim.height(), pi.background_color);
+
        pi.pain.fillRectangle(x + 1, y - dim.asc + 1, dim.wid - 2,
-                       dim.asc + dim.des - 1, pi.backgroundColor(this));
+                             dim.height() - 1, pi.backgroundColor(this));
 }
 
 
@@ -626,11 +611,6 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
        }
 
        // First draw the numbers
-       ColorCode color = pi.selected && lyxrc.use_system_colors
-                               ? Color_selectiontext : standardColor();
-       bool const really_change_color = pi.base.font.color() == Color_none;
-       Changer dummy0 = really_change_color ? pi.base.font.changeColor(color)
-               : noChange();
        if (numberedType()) {
                BufferParams::MathNumber const math_number = buffer().params().getMathNumber();
                for (row_type row = 0; row < nrows(); ++row) {
@@ -639,18 +619,37 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
                        Dimension dimnl;
                        mathed_string_dim(pi.base.font, nl, dimnl);
                        if (math_number == BufferParams::LEFT) {
+                               ColorCode const col = pi.selected_left
+                                       ? Color_selectiontext
+                                       : pi.base.font.color();
+                               Changer dummy0 = pi.base.font.changeColor(col);
                                if (dimnl.wid > x - pi.leftx)
                                        yy += rowinfo(row).descent + dimnl.asc;
+                               pi.pain.fillRectangle(pi.leftx, yy - dimnl.asc,
+                                       dimnl.width(), dimnl.height(),
+                                       pi.selected_left ? Color_selection : pi.background_color);
                                pi.draw(pi.leftx, yy, nl);
                        } else {
+                               ColorCode const col = pi.selected_right
+                                       ? Color_selectiontext
+                                       : pi.base.font.color();
+                               Changer dummy0 = pi.base.font.changeColor(col);
                                if (dimnl.wid > pi.rightx - x - dim.wid)
                                        yy += rowinfo(row).descent + dimnl.asc;
+                               pi.pain.fillRectangle(pi.rightx - dimnl.wid, yy - dimnl.asc,
+                                       dimnl.width(), dimnl.height(),
+                                       pi.selected_right ? Color_selection : pi.background_color);
                                pi.draw(pi.rightx - dimnl.wid, yy, nl);
                        }
                }
        }
 
        // Then the equations
+       ColorCode color = pi.selected && lyxrc.use_system_colors
+               ? Color_selectiontext : standardColor();
+       bool const really_change_color = pi.base.font.color() == Color_none;
+       Changer dummy0 = really_change_color ? pi.base.font.changeColor(color)
+               : noChange();
        Changer dummy1 = pi.base.changeFontSet(standardFont());
        Changer dummy2 = pi.base.font.changeStyle(display() ? DISPLAY_STYLE
                                                            : TEXT_STYLE);
@@ -827,29 +826,7 @@ void InsetMathHull::preparePreview(DocIterator const & pos,
        if (lsize != "normalsize" && !prefixIs(lsize, "error"))
                setfont += from_ascii("\\" + lsize + '\n');
 
-       docstring setcnt;
-       if (forexport && haveNumbers()) {
-               docstring eqstr = from_ascii("equation");
-               CounterMap::const_iterator it = counter_map.find(eqstr);
-               if (it != counter_map.end()) {
-                       int num = it->second;
-                       if (num >= 0)
-                               setcnt += from_ascii("\\setcounter{") + eqstr + '}' +
-                                         '{' + convert<docstring>(num) + '}' + '\n';
-               }
-               for (size_t i = 0; i != numcnts; ++i) {
-                       docstring cnt = from_ascii(counters_to_save[i]);
-                       it = counter_map.find(cnt);
-                       if (it == counter_map.end())
-                                       continue;
-                       int num = it->second;
-                       if (num > 0)
-                               setcnt += from_ascii("\\setcounter{") + cnt + '}' +
-                                         '{' + convert<docstring>(num) + '}';
-               }
-       }
-       docstring const snippet = macro_preamble + setfont + setcnt
-                                 + latexString(*this) + endfont;
+       docstring const snippet = macro_preamble + setfont + latexString(*this) + endfont;
        LYXERR(Debug::MACROS, "Preview snippet: " << snippet);
        preview_->addPreview(snippet, *buffer, forexport);
 }
@@ -915,10 +892,6 @@ void InsetMathHull::label(row_type row, docstring const & label)
 void InsetMathHull::numbered(row_type row, Numbered num)
 {
        numbered_[row] = num;
-       if (!numbered(row) && label_[row]) {
-               delete label_[row];
-               label_[row] = 0;
-       }
 }
 
 
@@ -978,7 +951,7 @@ bool InsetMathHull::outerDisplay() const
 }
 
 
-Inset::RowFlags InsetMathHull::rowFlags() const
+int InsetMathHull::rowFlags() const
 {
        switch (type_) {
        case hullUnknown:
@@ -1367,8 +1340,11 @@ void InsetMathHull::delCol(col_type col)
 
 docstring InsetMathHull::nicelabel(row_type row) const
 {
-       if (!numbered(row))
-               return docstring();
+       if (!numbered(row)) {
+               if (!label_[row])
+                       return docstring();
+               return '[' + label_[row]->screenLabel() + ']';
+       }
        docstring const & val = numbers_[row];
        if (!label_[row])
                return '(' + val + ')';
@@ -1671,27 +1647,30 @@ void InsetMathHull::mutate(HullType newtype)
 }
 
 
-docstring InsetMathHull::eolString(row_type row, bool fragile, bool latex,
-               bool last_eoln) const
+void InsetMathHull::eol(TeXMathStream & os, row_type row, bool fragile, bool latex,
+                        bool last_eoln) const
 {
-       docstring res;
        if (numberedType()) {
-               if (label_[row] && numbered(row)) {
+               if (label_[row]) {
                        docstring const name =
                                latex ? escape(label_[row]->getParam("name"))
                                      : label_[row]->getParam("name");
-                       res += "\\label{" + name + '}';
+                       os << "\\label{" + name + '}';
                }
                if (type_ != hullMultline) {
                        if (numbered_[row]  == NONUMBER)
-                               res += "\\nonumber ";
+                               os << "\\nonumber ";
                        else if (numbered_[row]  == NOTAG)
-                               res += "\\notag ";
+                               os<< "\\notag ";
+               }
+               if (os.output() == TeXMathStream::wsPreview && !numbers_[row].empty()) {
+                       os << "\\global\\def\\theequation{" << numbers_[row] << "}\n";
                }
+
        }
        // Never add \\ on the last empty line of eqnarray and friends
        last_eoln = false;
-       return res + InsetMathGrid::eolString(row, fragile, latex, last_eoln);
+       InsetMathGrid::eol(os, row, fragile, latex, last_eoln);
 }
 
 void InsetMathHull::write(TeXMathStream & os) const
@@ -1898,7 +1877,7 @@ void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
                        // if there is an argument, find the corresponding label, else
                        // check whether there is at least one label.
                        for (row = 0; row != nrows(); ++row)
-                               if (numbered(row) && label_[row]
+                               if (label_[row]
                                          && (cmd.argument().empty() || label(row) == cmd.argument()))
                                        break;
                }
@@ -2101,12 +2080,12 @@ bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
                        // if there is no argument and we're inside math, we retrieve
                        // the row number from the cursor position.
                        row_type row = (type_ == hullMultline) ? nrows() - 1 : cur.row();
-                       enabled = numberedType() && label_[row] && numbered(row);
+                       enabled = numberedType() && label_[row];
                } else {
                        // if there is an argument, find the corresponding label, else
                        // check whether there is at least one label.
                        for (row_type row = 0; row != nrows(); ++row) {
-                               if (numbered(row) && label_[row] &&
+                               if (label_[row] &&
                                        (cmd.argument().empty() || label(row) == cmd.argument())) {
                                                enabled = true;
                                                break;
@@ -2244,7 +2223,10 @@ void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
 
 void InsetMathHull::edit(Cursor & cur, bool front, EntryDirection entry_from)
 {
-       InsetMathNest::edit(cur, front, entry_from);
+       cur.push(*this);
+       bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_LEFT ||
+               (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
+       enter_front ? idxFirst(cur) : idxLast(cur);
        // The inset formula dimension is not necessarily the same as the
        // one of the instant preview image, so we have to indicate to the
        // BufferView that a metrics update is needed.
@@ -2344,12 +2326,19 @@ int InsetMathHull::plaintext(odocstringstream & os,
        odocstringstream oss;
        otexrowstream ots(oss);
        Encoding const * const enc = encodings.fromLyXName("utf8");
-       TeXMathStream wi(ots, false, true, TeXMathStream::wsDefault, enc);
 
+       TeXMathStream::OutputType ot;
+       if (op.find_effective())
+               ot = TeXMathStream::wsSearchAdv;
+       else
+               ot = TeXMathStream::wsDefault;
        // Fix Bug #6139
-       if (type_ == hullRegexp)
+       if (type_ == hullRegexp) {
+               TeXMathStream wi(ots, false, true, ot, enc);
                write(wi);
+       }
        else {
+               TeXMathStream wi(ots, false, true, ot, enc);
                for (row_type r = 0; r < nrows(); ++r) {
                        for (col_type c = 0; c < ncols(); ++c)
                                wi << (c == 0 ? "" : "\t") << cell(index(r, c));
@@ -2527,15 +2516,26 @@ void InsetMathHull::htmlize(HtmlStream & os) const
 // and we simply do not have that in InsetMathGrid.
 void InsetMathHull::mathmlize(MathMLStream & ms) const
 {
-       bool const havenumbers = haveNumbers();
-       bool const havetable = havenumbers || nrows() > 1 || ncols() > 1;
+       bool const havetable = haveNumbers() || nrows() > 1 || ncols() > 1;
+
+    if (havetable) {
+        if (getType() == hullSimple) {
+               ms << MTag("mtable");
+        } else if (getType() >= hullAlign && getType() <= hullXXAlignAt) {
+            string alignment;
+            for (col_type col = 0; col < ncols(); ++col) {
+                alignment += (col % 2) ? "left " : "right ";
+            }
+            ms << MTag("mtable", "displaystyle='true' columnalign='" + alignment + "'");
+           } else {
+               ms << MTag("mtable", "displaystyle='true'");
+        }
+    }
 
-       if (havetable)
-               ms << MTag("mtable");
        char const * const celltag = havetable ? "mtd" : "mrow";
        // FIXME There does not seem to be wide support at the moment
        // for mlabeledtr, so we have to use just mtr for now.
-       // char const * const rowtag = havenumbers ? "mlabeledtr" : "mtr";
+       // char const * const rowtag = haveNumbers() ? "mlabeledtr" : "mtr";
        char const * const rowtag = "mtr";
        for (row_type row = 0; row < nrows(); ++row) {
                if (havetable)
@@ -2546,12 +2546,12 @@ void InsetMathHull::mathmlize(MathMLStream & ms) const
                           << ETag(celltag);
                }
                // fleqn?
-               if (havenumbers) {
+               if (haveNumbers()) {
                        ms << MTag("mtd");
                        docstring const & num = numbers_[row];
                        if (!num.empty())
                                ms << MTagInline("mtext") << '(' << num << ')' << ETagInline("mtext");
-                 ms << ETag("mtd");
+                   ms << ETag("mtd");
                }
                if (havetable)
                        ms << ETag(rowtag);