X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2FMathRow.cpp;h=3263d1c48315f373bd04cd5c642a236839de0e40;hb=ad79ac406f9c8fc85880cfeb5416b0299ee9a617;hp=703d25e60f7e74d6ad3206fc54834d5dbff364f0;hpb=2acc4fc54c178744467b89b469f10bda90bb0310;p=lyx.git diff --git a/src/mathed/MathRow.cpp b/src/mathed/MathRow.cpp index 703d25e60f..3263d1c483 100644 --- a/src/mathed/MathRow.cpp +++ b/src/mathed/MathRow.cpp @@ -15,7 +15,6 @@ #include "InsetMath.h" #include "MathClass.h" #include "MathData.h" -#include "MathMacro.h" #include "MathSupport.h" #include "BufferView.h" @@ -36,56 +35,69 @@ using namespace std; namespace lyx { -MathRow::Element::Element(Type t, MathClass mc) - : type(t), mclass(mc), before(0), after(0), inset(0), - compl_unique_to(0), macro(0), color(Color_red) +MathRow::Element::Element(MetricsInfo const & mi, Type t, MathClass mc) + : type(t), mclass(mc), before(0), after(0), macro_nesting(mi.base.macro_nesting), + marker(InsetMath::NO_MARKER), inset(0), compl_unique_to(0), ar(0), + color(Color_red) {} MathRow::MathRow(MetricsInfo & mi, MathData const * ar) { // First there is a dummy element of type "open" - push_back(Element(DUMMY, MC_OPEN)); + push_back(Element(mi, DUMMY, MC_OPEN)); // Then insert the MathData argument bool const has_contents = ar->addToMathRow(*this, mi); - // empty arrays are visible when they are editable - // we reserve the necessary space anyway (even if nothing gets drawn) + // A MathRow should not be completely empty if (!has_contents) { - Element e(BOX, MC_ORD); + Element e(mi, BOX, MC_ORD); + // empty arrays are visible when they are editable e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none; push_back(e); } // Finally there is a dummy element of type "close" - push_back(Element(DUMMY, MC_CLOSE)); + push_back(Element(mi, DUMMY, MC_CLOSE)); /* Do spacing only in math mode. This test is a bit clumsy, * but it is used in other places for guessing the current mode. */ - if (!isMathFont(mi.base.fontname)) - return; + bool const dospacing = isMathFont(mi.base.fontname); // update classes - for (int i = 1 ; i != static_cast(elements_.size()) - 1 ; ++i) { - if (elements_[i].mclass == MC_UNKNOWN) - continue; - update_class(elements_[i].mclass, elements_[before(i)].mclass, - elements_[after(i)].mclass); + if (dospacing) { + for (int i = 1 ; i != static_cast(elements_.size()) - 1 ; ++i) { + if (elements_[i].mclass != MC_UNKNOWN) + update_class(elements_[i].mclass, elements_[before(i)].mclass, + elements_[after(i)].mclass); + } } // set spacing // We go to the end to handle spacing at the end of equation for (int i = 1 ; i != static_cast(elements_.size()) ; ++i) { - if (elements_[i].mclass == MC_UNKNOWN) + Element & e = elements_[i]; + + if (e.mclass == MC_UNKNOWN) continue; + Element & bef = elements_[before(i)]; - int spc = class_spacing(bef.mclass, elements_[i].mclass, mi.base); - bef.after = spc / 2; - // this is better than spc / 2 to avoid rounding problems - elements_[i].before = spc - spc / 2; + if (dospacing) { + int spc = class_spacing(bef.mclass, e.mclass, mi.base); + bef.after += spc / 2; + // this is better than spc / 2 to avoid rounding problems + e.before += spc - spc / 2; + } + + // finally reserve space for markers + if (bef.marker != Inset::NO_MARKER) + bef.after = max(bef.after, 1); + if (e.marker != Inset::NO_MARKER) + e.before = max(e.before, 1); } + // Do not lose spacing allocated to extremities if (!elements_.empty()) { elements_[after(0)].before += elements_.front().after; @@ -120,14 +132,11 @@ void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const dim.wid = 0; // In order to compute the dimension of macros and their // arguments, it is necessary to keep track of them. - map dim_macros; - map dim_arrays; - // this vector remembers the stack of macro nesting values - vector macro_nesting; - macro_nesting.push_back(mi.base.macro_nesting); + vector> dim_insets; + vector> dim_arrays; CoordCache & coords = mi.base.bv->coordCache(); for (Element const & e : elements_) { - mi.base.macro_nesting = macro_nesting.back(); + mi.base.macro_nesting = e.macro_nesting; Dimension d; switch (e.type) { case DUMMY: @@ -137,48 +146,55 @@ void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const d.wid += e.before + e.after; coords.insets().add(e.inset, d); break; - case BEG_MACRO: - macro_nesting.push_back(e.macro->nesting()); - e.macro->macro()->lock(); - // Add a macro to current list - dim_macros[e.macro] = Dimension(); - break; - case END_MACRO: - LATTEST(dim_macros.find(e.macro) != dim_macros.end()); - macro_nesting.pop_back(); - e.macro->macro()->unlock(); - // Cache the dimension of the macro and remove it from - // tracking map. - coords.insets().add(e.macro, dim_macros[e.macro]); - dim_macros.erase(e.macro); - break; - // This is basically like macros - case BEG_ARG: - if (e.macro) { - macro_nesting.push_back(e.macro->nesting()); - e.macro->macro()->unlock(); + case BEGIN: + if (e.inset) { + dim_insets.push_back(make_pair(e.inset, Dimension())); + e.inset->beforeMetrics(); } - dim_arrays[e.ar] = Dimension(); + if (e.ar) + dim_arrays.push_back(make_pair(e.ar, Dimension())); break; - case END_ARG: - LATTEST(dim_arrays.find(e.ar) != dim_arrays.end()); - if (e.macro) { - macro_nesting.pop_back(); - e.macro->macro()->lock(); + case END: + if (e.inset) { + e.inset->afterMetrics(); + LATTEST(dim_insets.back().first == e.inset); + coords.insets().add(e.inset, dim_insets.back().second); + dim_insets.pop_back(); + } + if (e.ar) { + LATTEST(dim_arrays.back().first == e.ar); + coords.arrays().add(e.ar, dim_arrays.back().second); + dim_arrays.pop_back(); } - coords.arrays().add(e.ar, dim_arrays[e.ar]); - dim_arrays.erase(e.ar); break; case BOX: d = theFontMetrics(mi.base.font).dimension('I'); - d.wid += e.before + e.after; + if (e.color != Color_none) { + // allow for one pixel before/after the box. + d.wid += e.before + e.after + 2; + } else { + // hide the box, but give it some height + d.wid = 0; + } break; } + // handle vertical space for markers + switch(e.marker) { + case InsetMath::NO_MARKER: + break; + case InsetMath::MARKER: + ++d.des; + break; + case InsetMath::MARKER2: + ++d.asc; + ++d.des; + } + if (!d.empty()) { dim += d; // Now add the dimension to current macros and arguments. - for (auto & dim_macro : dim_macros) + for (auto & dim_macro : dim_insets) dim_macro.second += d; for (auto & dim_array : dim_arrays) dim_array.second += d; @@ -190,10 +206,47 @@ void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const augmentFont(font, "mathnormal"); dim.wid += mathed_string_width(font, e.compl_text); } - LATTEST(dim_macros.empty() && dim_arrays.empty()); + LATTEST(dim_insets.empty() && dim_arrays.empty()); } +namespace { + +void drawMarkers(PainterInfo const & pi, MathRow::Element const & e, int const x, int const y) +{ + if (e.marker == InsetMath::NO_MARKER) + return; + + CoordCache const & coords = pi.base.bv->coordCache(); + Dimension const dim = coords.getInsets().dim(e.inset); + + // the marker is before/after the inset. Normally some space has been reserved already. + int const l = x + e.before - 1; + int const r = x + dim.width() - e.after; + + // Duplicated from Inset.cpp and adapted. It is believed that the + // Inset version should die eventually + ColorCode pen_color = e.inset->mouseHovered(pi.base.bv) || e.inset->editing(pi.base.bv)? + Color_mathframe : Color_mathcorners; + + int const d = y + dim.descent(); + pi.pain.line(l, d - 3, l, d, pen_color); + pi.pain.line(r, d - 3, r, d, pen_color); + pi.pain.line(l, d, l + 3, d, pen_color); + pi.pain.line(r - 3, d, r, d, pen_color); + + if (e.marker == InsetMath::MARKER) + return; + + int const a = y - dim.ascent(); + pi.pain.line(l, a + 3, l, a, pen_color); + pi.pain.line(r, a + 3, r, a, pen_color); + pi.pain.line(l, a, l + 3, a, pen_color); + pi.pain.line(r - 3, a, r, a, pen_color); +} + +} + void MathRow::draw(PainterInfo & pi, int x, int const y) const { CoordCache & coords = pi.base.bv->coordCache(); @@ -211,34 +264,33 @@ void MathRow::draw(PainterInfo & pi, int x, int const y) const e.inset->draw(pi, x + e.before, y); coords.insets().add(e.inset, x, y); coords.insets().add(e.inset, d); + drawMarkers(pi, e, x, y); x += d.wid; break; } - case BEG_MACRO: - coords.insets().add(e.macro, x, y); - break; - case BEG_ARG: - coords.arrays().add(e.ar, x, y); - // if the macro is being edited, then the painter is in - // monochrome mode. - if (e.macro->editMetrics(pi.base.bv)) - pi.pain.leaveMonochromeMode(); + case BEGIN: + if (e.inset) { + coords.insets().add(e.inset, x, y); + drawMarkers(pi, e, x, y); + e.inset->beforeDraw(pi); + } + if (e.ar) + coords.arrays().add(e.ar, x, y); break; - case END_ARG: - if (e.macro->editMetrics(pi.base.bv)) - pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend); + case END: + if (e.inset) + e.inset->afterDraw(pi); break; case BOX: { + if (e.color == Color_none) + break; Dimension const d = theFontMetrics(pi.base.font).dimension('I'); - // the box is not visible in non-editable context (except for grey macro boxes). - if (e.color != Color_none) - pi.pain.rectangle(x + e.before, y - d.ascent(), - d.width(), d.height(), e.color); - x += d.wid + e.before + e.after; + pi.pain.rectangle(x + e.before + 1, y - d.ascent(), + d.width() - 1, d.height() - 1, e.color); + x += d.wid + 2 + e.before + e.after; break; } case DUMMY: - case END_MACRO: break; } @@ -287,18 +339,18 @@ ostream & operator<<(ostream & os, MathRow::Element const & e) << to_utf8(class_to_string(e.mclass)) << "-" << e.after << ">"; break; - case MathRow::BEG_MACRO: - os << "\\" << to_utf8(e.macro->name()) - << "^" << e.macro->nesting() << "["; - break; - case MathRow::END_MACRO: - os << "]"; - break; - case MathRow::BEG_ARG: - os << "#("; + case MathRow::BEGIN: + if (e.inset) + os << "\\" << to_utf8(e.inset->name()) + << "^" << e.macro_nesting << "["; + if (e.ar) + os << "("; break; - case MathRow::END_ARG: - os << ")"; + case MathRow::END: + if (e.ar) + os << ")"; + if (e.inset) + os << "]"; break; case MathRow::BOX: os << "<" << e.before << "-[]-" << e.after << ">";