X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2FInsetMathHull.C;h=8ee9abe9aebc371be7c449ac3cae093edfa2d101;hb=2db7521b7080d63acbcd3bd5bfe892f5a414b2b0;hp=0054d6f46151c2f00017116eaf7637560af823c5;hpb=311ac9b192bc45f29b8c305b4519a09e3e30bccc;p=features.git diff --git a/src/mathed/InsetMathHull.C b/src/mathed/InsetMathHull.C index 0054d6f461..8ee9abe9ae 100644 --- a/src/mathed/InsetMathHull.C +++ b/src/mathed/InsetMathHull.C @@ -18,7 +18,7 @@ #include "MathExtern.h" #include "MathFactory.h" #include "InsetMathHull.h" -#include "MathMLStream.h" +#include "MathStream.h" #include "MathParser.h" #include "InsetMathSpace.h" #include "MathStream.h" @@ -50,9 +50,6 @@ #include "insets/render_preview.h" #include "insets/insetlabel.h" -#include "frontends/Dialogs.h" -#include "frontends/LyXView.h" - #include "graphics/PreviewImage.h" #include "graphics/PreviewLoader.h" @@ -63,18 +60,18 @@ #include -using lyx::docstring; -using lyx::cap::grabAndEraseSelection; -using lyx::support::bformat; -using lyx::support::subst; + +namespace lyx { + +using cap::grabAndEraseSelection; +using support::bformat; +using support::subst; using std::endl; using std::max; -using std::string; using std::ostream; using std::auto_ptr; using std::istringstream; -using std::ostream; using std::ostringstream; using std::pair; using std::swap; @@ -120,7 +117,7 @@ namespace { } // end anon namespace -HullType hullType(std::string const & s) +HullType hullType(docstring const & s) { if (s == "none") return hullNone; if (s == "simple") return hullSimple; @@ -133,28 +130,28 @@ HullType hullType(std::string const & s) if (s == "multline") return hullMultline; if (s == "gather") return hullGather; if (s == "flalign") return hullFlAlign; - lyxerr << "unknown hull type '" << s << "'" << endl; + lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl; return HullType(-1); } -std::string hullName(HullType type) +docstring hullName(HullType type) { switch (type) { - case hullNone: return "none"; - case hullSimple: return "simple"; - case hullEquation: return "equation"; - case hullEqnArray: return "eqnarray"; - case hullAlign: return "align"; - case hullAlignAt: return "alignat"; - case hullXAlignAt: return "xalignat"; - case hullXXAlignAt: return "xxalignat"; - case hullMultline: return "multline"; - case hullGather: return "gather"; - case hullFlAlign: return "flalign"; + case hullNone: return from_ascii("none"); + case hullSimple: return from_ascii("simple"); + case hullEquation: return from_ascii("equation"); + case hullEqnArray: return from_ascii("eqnarray"); + case hullAlign: return from_ascii("align"); + case hullAlignAt: return from_ascii("alignat"); + case hullXAlignAt: return from_ascii("xalignat"); + case hullXXAlignAt: return from_ascii("xxalignat"); + case hullMultline: return from_ascii("multline"); + case hullGather: return from_ascii("gather"); + case hullFlAlign: return from_ascii("flalign"); default: lyxerr << "unknown hull type '" << type << "'" << endl; - return "none"; + return from_ascii("none"); } } @@ -269,16 +266,16 @@ int InsetMathHull::defaultColSpace(col_type col) } -char const * InsetMathHull::standardFont() const +docstring InsetMathHull::standardFont() const { - return type_ == hullNone ? "lyxnochange" : "mathnormal"; + return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal"); } bool InsetMathHull::previewState(BufferView * bv) const { if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) { - lyx::graphics::PreviewImage const * pimage = + graphics::PreviewImage const * pimage = preview_->getPreviewImage(*bv->buffer()); return pimage && pimage->image(); } @@ -310,7 +307,7 @@ void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const } if (numberedType()) { - FontSetChanger dummy(mi.base, "mathbf"); + FontSetChanger dummy(mi.base, from_ascii("mathbf")); int l = 0; for (row_type row = 0; row < nrows(); ++row) l = max(l, mathed_string_width(mi.base.font, nicelabel(row))); @@ -349,10 +346,9 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20; for (row_type row = 0; row < nrows(); ++row) { int const yy = y + rowinfo_[row].offset_; - FontSetChanger dummy(pi.base, "mathrm"); - string const nl = nicelabel(row); - docstring const dnl(nl.begin(), nl.end()); - pi.draw(xx, yy, dnl); + FontSetChanger dummy(pi.base, from_ascii("mathrm")); + docstring const nl = nicelabel(row); + pi.draw(xx, yy, nl); } } setPosCache(pi, x, y); @@ -364,7 +360,7 @@ void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const if (display()) { InsetMathGrid::metricsT(mi, dim); } else { - ostringstream os; + odocstringstream os; WriteStream wi(os, false, true); write(wi); dim.wid = os.str().size(); @@ -379,7 +375,7 @@ void InsetMathHull::drawT(TextPainter & pain, int x, int y) const if (display()) { InsetMathGrid::drawT(pain, x, y); } else { - ostringstream os; + odocstringstream os; WriteStream wi(os, false, true); write(wi); pain.draw(x, y, os.str().c_str()); @@ -389,9 +385,9 @@ void InsetMathHull::drawT(TextPainter & pain, int x, int y) const namespace { -string const latex_string(InsetMathHull const & inset) +docstring const latex_string(InsetMathHull const & inset) { - ostringstream ls; + odocstringstream ls; WriteStream wi(ls, false, false); inset.write(wi); return ls.str(); @@ -400,10 +396,10 @@ string const latex_string(InsetMathHull const & inset) } // namespace anon -void InsetMathHull::addPreview(lyx::graphics::PreviewLoader & ploader) const +void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const { if (RenderPreview::status() == LyXRC::PREVIEW_ON) { - string const snippet = latex_string(*this); + docstring const snippet = latex_string(*this); preview_->addPreview(snippet, ploader); } } @@ -413,7 +409,7 @@ bool InsetMathHull::notifyCursorLeaves(LCursor & cur) { if (RenderPreview::status() == LyXRC::PREVIEW_ON) { Buffer const & buffer = cur.buffer(); - string const snippet = latex_string(*this); + docstring const snippet = latex_string(*this); preview_->addPreview(snippet, buffer); preview_->startLoading(buffer); } @@ -421,14 +417,14 @@ bool InsetMathHull::notifyCursorLeaves(LCursor & cur) } -string InsetMathHull::label(row_type row) const +docstring InsetMathHull::label(row_type row) const { BOOST_ASSERT(row < nrows()); return label_[row]; } -void InsetMathHull::label(row_type row, string const & label) +void InsetMathHull::label(row_type row, docstring const & label) { //lyxerr << "setting label '" << label << "' for row " << row << endl; label_[row] = label; @@ -466,7 +462,7 @@ bool InsetMathHull::display() const } -void InsetMathHull::getLabelList(Buffer const &, vector & labels) const +void InsetMathHull::getLabelList(Buffer const &, vector & labels) const { for (row_type row = 0; row < nrows(); ++row) if (!label_[row].empty() && nonum_[row] != 1) @@ -511,37 +507,46 @@ void InsetMathHull::header_write(WriteStream & os) const { bool n = numberedType(); - if (type_ == hullNone) - ; + switch(type_) { + case hullNone: + break; - else if (type_ == hullSimple) { + case hullSimple: os << '$'; if (cell(0).empty()) os << ' '; - } + break; - else if (type_ == hullEquation) { + case hullEquation: if (n) os << "\\begin{equation" << star(n) << "}\n"; else os << "\\[\n"; - } + break; - else if (type_ == hullEqnArray || type_ == hullAlign || type_ == -hullFlAlign - || type_ == hullGather || type_ == hullMultline) - os << "\\begin{" << type_ << star(n) << "}\n"; + case hullEqnArray: + case hullAlign: + case hullFlAlign: + case hullGather: + case hullMultline: + os << "\\begin{" << hullName(type_) << star(n) << "}\n"; + break; - else if (type_ == hullAlignAt || type_ == hullXAlignAt) - os << "\\begin{" << type_ << star(n) << '}' + case hullAlignAt: + case hullXAlignAt: + os << "\\begin{" << hullName(type_) << star(n) << '}' << '{' << static_cast((ncols() + 1)/2) << "}\n"; + break; - else if (type_ == hullXXAlignAt) - os << "\\begin{" << type_ << '}' + case hullXXAlignAt: + os << "\\begin{" << hullName(type_) << '}' << '{' << static_cast((ncols() + 1)/2) << "}\n"; + break; - else + default: os << "\\begin{unknown" << star(n) << '}'; + break; + } } @@ -549,29 +554,40 @@ void InsetMathHull::footer_write(WriteStream & os) const { bool n = numberedType(); - if (type_ == hullNone) + switch(type_) { + case hullNone: os << "\n"; + break; - else if (type_ == hullSimple) + case hullSimple: os << '$'; + break; - else if (type_ == hullEquation) + case hullEquation: if (n) os << "\\end{equation" << star(n) << "}\n"; else os << "\\]\n"; + break; - else if (type_ == hullEqnArray || type_ == hullAlign || type_ == -hullFlAlign - || type_ == hullAlignAt || type_ == hullXAlignAt - || type_ == hullGather || type_ == hullMultline) - os << "\\end{" << type_ << star(n) << "}\n"; + case hullEqnArray: + case hullAlign: + case hullFlAlign: + case hullAlignAt: + case hullXAlignAt: + case hullGather: + case hullMultline: + os << "\\end{" << hullName(type_) << star(n) << "}\n"; + break; - else if (type_ == hullXXAlignAt) - os << "\\end{" << type_ << "}\n"; + case hullXXAlignAt: + os << "\\end{" << hullName(type_) << "}\n"; + break; - else + default: os << "\\end{unknown" << star(n) << '}'; + break; + } } @@ -598,7 +614,7 @@ void InsetMathHull::addRow(row_type row) if (!rowChangeOK()) return; nonum_.insert(nonum_.begin() + row + 1, !numberedType()); - label_.insert(label_.begin() + row + 1, string()); + label_.insert(label_.begin() + row + 1, docstring()); InsetMathGrid::addRow(row); } @@ -645,12 +661,13 @@ void InsetMathHull::delCol(col_type col) } -string InsetMathHull::nicelabel(row_type row) const +docstring InsetMathHull::nicelabel(row_type row) const { if (nonum_[row]) - return string(); + return docstring(); if (label_[row].empty()) - return string("(#)"); + return from_ascii("(#)"); + // FIXME UNICODE return '(' + label_[row] + ')'; } @@ -804,7 +821,7 @@ void InsetMathHull::mutate(HullType newtype) allnonum = false; // set first non-empty label - string label; + docstring label; for (row_type row = 0; row < nrows(); ++row) { if (!label_[row].empty()) { label = label_[row]; @@ -876,15 +893,16 @@ void InsetMathHull::mutate(HullType newtype) } else { - lyxerr << "mutation from '" << type_ - << "' to '" << newtype << "' not implemented" << endl; + lyxerr << "mutation from '" << to_utf8(hullName(type_)) + << "' to '" << to_utf8(hullName(newtype)) + << "' not implemented" << endl; } } -string InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const +docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const { - string res; + docstring res; if (numberedType()) { if (!label_[row].empty() && !nonum_[row]) res += "\\label{" + label_[row] + '}'; @@ -905,21 +923,21 @@ void InsetMathHull::write(WriteStream & os) const void InsetMathHull::normalize(NormalStream & os) const { - os << "[formula " << type_ << ' '; + os << "[formula " << hullName(type_) << ' '; InsetMathGrid::normalize(os); os << "] "; } -void InsetMathHull::mathmlize(MathMLStream & os) const +void InsetMathHull::mathmlize(MathStream & os) const { InsetMathGrid::mathmlize(os); } -void InsetMathHull::infoize(ostream & os) const +void InsetMathHull::infoize(odocstream & os) const { - os << "Type: " << type_; + os << "Type: " << hullName(type_); } @@ -932,12 +950,13 @@ void InsetMathHull::check() const void InsetMathHull::doExtern(LCursor & cur, FuncRequest & func) { - string lang; - string extra; - istringstream iss(lyx::to_utf8(func.argument())); - iss >> lang >> extra; + docstring dlang; + docstring extra; + idocstringstream iss(func.argument()); + iss >> dlang >> extra; if (extra.empty()) - extra = "noextra"; + extra = from_ascii("noextra"); + std::string const lang = to_ascii(dlang); #ifdef WITH_WARNINGS #warning temporarily disabled @@ -961,6 +980,7 @@ void InsetMathHull::doExtern(LCursor & cur, FuncRequest & func) size_type pos = cur.cell().find_last(eq); MathArray ar; if (cur.inMathed() && cur.selection()) { + // FIXME UNICODE asArray(grabAndEraseSelection(cur), ar); } else if (pos == cur.cell().size()) { ar = cur.cell(); @@ -1033,7 +1053,7 @@ void InsetMathHull::doDispatch(LCursor & cur, FuncRequest & cmd) if (type_ == hullSimple || type_ == hullEquation) { recordUndoInset(cur); bool const align = - cur.bv().buffer()->params().use_amsmath == BufferParams::AMS_ON; + cur.bv().buffer()->params().use_amsmath == BufferParams::package_on; mutate(align ? hullAlign : hullEqnArray); cur.idx() = 0; cur.pos() = cur.lastpos(); @@ -1069,16 +1089,15 @@ void InsetMathHull::doDispatch(LCursor & cur, FuncRequest & cmd) case LFUN_LABEL_INSERT: { recordUndoInset(cur); row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row(); - string old_label = label(r); - string const default_label = - (lyxrc.label_init_length >= 0) ? "eq:" : ""; + docstring old_label = label(r); + docstring const default_label = from_ascii( + (lyxrc.label_init_length >= 0) ? "eq:" : ""); if (old_label.empty()) old_label = default_label; - string const contents = cmd.argument().empty() ? - old_label : lyx::to_utf8(cmd.argument()); - InsetCommandParams p("label", contents); - string const data = InsetCommandMailer::params2string("label", p); + InsetCommandParams p("label"); + p["name"] = cmd.argument().empty() ? old_label : cmd.argument(); + std::string const data = InsetCommandMailer::params2string("label", p); if (cmd.argument().empty()) cur.bv().showInsetDialog("label", data, 0); @@ -1090,31 +1109,27 @@ void InsetMathHull::doDispatch(LCursor & cur, FuncRequest & cmd) } case LFUN_INSET_INSERT: { - //lyxerr << "arg: " << lyx::to_utf8(cmd.argument()) << endl; - string const name = cmd.getArg(0); + //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl; + std::string const name = cmd.getArg(0); if (name == "label") { - InsetCommandParams p; - InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), p); - string str = p.getContents(); + InsetCommandParams p("label"); + InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p); + docstring str = p["name"]; recordUndoInset(cur); row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row(); - str = lyx::support::trim(str); + str = support::trim(str); if (!str.empty()) numbered(r, true); - string old = label(r); + docstring old = label(r); if (str != old) { - cur.bv().buffer()->changeRefsIfUnique(old, str); + cur.bv().buffer()->changeRefsIfUnique(old, str, + InsetBase::REF_CODE); label(r, str); } break; } - MathArray ar; - if (createInsetMath_fromDialogStr(lyx::to_utf8(cmd.argument()), ar)) { - recordUndo(cur); - cur.insert(ar); - } else - cur.undispatched(); - break; + InsetMathGrid::doDispatch(cur, cmd); + return; } case LFUN_MATH_EXTERN: @@ -1126,7 +1141,7 @@ void InsetMathHull::doDispatch(LCursor & cur, FuncRequest & cmd) recordUndoInset(cur); row_type row = cur.row(); col_type col = cur.col(); - mutate(hullType(lyx::to_utf8(cmd.argument()))); + mutate(hullType(cmd.argument())); cur.idx() = row * ncols() + col; if (cur.idx() > cur.lastidx()) { cur.idx() = cur.lastidx(); @@ -1176,26 +1191,23 @@ bool InsetMathHull::getStatus(LCursor & cur, FuncRequest const & cmd, case LFUN_LABEL_INSERT: status.enabled(type_ != hullSimple); return true; - case LFUN_INSET_INSERT: { - // Don't test createInsetMath_fromDialogStr(), since - // getStatus is not called with a valid reference and the - // dialog would not be applyable. - string const name = cmd.getArg(0); - status.enabled(name == "ref" || - (name == "label" && type_ != hullSimple)); - break; - } + case LFUN_INSET_INSERT: + if (cmd.getArg(0) == "label") { + status.enabled(type_ != hullSimple); + return true; + } + return InsetMathGrid::getStatus(cur, cmd, status); case LFUN_TABULAR_FEATURE: { - istringstream is(lyx::to_utf8(cmd.argument())); - string s; + istringstream is(to_utf8(cmd.argument())); + std::string s; is >> s; if (!rowChangeOK() && (s == "append-row" || s == "delete-row" || s == "copy-row")) { status.message(bformat( - lyx::from_utf8(N_("Can't change number of rows in '%1$s'")), - lyx::from_utf8(hullName(type_)))); + from_utf8(N_("Can't change number of rows in '%1$s'")), + hullName(type_))); status.enabled(false); return true; } @@ -1204,8 +1216,8 @@ bool InsetMathHull::getStatus(LCursor & cur, FuncRequest const & cmd, || s == "delete-column" || s == "copy-column")) { status.message(bformat( - lyx::from_utf8(N_("Can't change number of columns in '%1$s'")), - lyx::from_utf8(hullName(type_)))); + from_utf8(N_("Can't change number of columns in '%1$s'")), + hullName(type_))); status.enabled(false); return true; } @@ -1214,15 +1226,15 @@ bool InsetMathHull::getStatus(LCursor & cur, FuncRequest const & cmd, || type_ == hullNone) && (s == "add-hline-above" || s == "add-hline-below")) { status.message(bformat( - lyx::from_utf8(N_("Can't add horizontal grid lines in '%1$s'")), - lyx::from_utf8(hullName(type_)))); + from_utf8(N_("Can't add horizontal grid lines in '%1$s'")), + hullName(type_))); status.enabled(false); return true; } if (s == "add-vline-left" || s == "add-vline-right") { status.message(bformat( - lyx::from_utf8(N_("Can't add vertical grid lines in '%1$s'")), - lyx::from_utf8(hullName(type_)))); + from_utf8(N_("Can't add vertical grid lines in '%1$s'")), + hullName(type_))); status.enabled(false); return true; } @@ -1264,19 +1276,19 @@ void InsetMathHull::mutateToText() view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt); // remove ourselves - //view_->owner()->dispatch(LFUN_ESCAPE); + //dispatch(LFUN_ESCAPE); #endif } -void InsetMathHull::handleFont(LCursor & cur, string const & arg, - string const & font) +void InsetMathHull::handleFont(LCursor & cur, docstring const & arg, + docstring const & font) { // this whole function is a hack and won't work for incremental font // changes... recordUndo(cur); if (cur.inset().asInsetMath()->name() == font) - cur.handleFont(font); + cur.handleFont(to_utf8(font)); else { cur.handleNest(createInsetMath(font)); cur.insert(arg); @@ -1284,12 +1296,12 @@ void InsetMathHull::handleFont(LCursor & cur, string const & arg, } -void InsetMathHull::handleFont2(LCursor & cur, string const & arg) +void InsetMathHull::handleFont2(LCursor & cur, docstring const & arg) { recordUndo(cur); LyXFont font; bool b; - bv_funcs::string2font(arg, font, b); + bv_funcs::string2font(to_utf8(arg), font, b); if (font.color() != LColor::inherit) { MathAtom at = MathAtom(new InsetMathColor(true, font.color())); cur.handleNest(at, 0); @@ -1314,9 +1326,9 @@ void InsetMathHull::revealCodes(LCursor & cur) const { if (!cur.inMathed()) return; - ostringstream os; + odocstringstream os; cur.info(os); - cur.message(lyx::from_utf8(os.str())); + cur.message(os.str()); /* // write something to the minibuffer // translate to latex @@ -1400,9 +1412,11 @@ bool InsetMathHull::searchForward(BufferView * bv, string const & str, void InsetMathHull::write(Buffer const &, std::ostream & os) const { - WriteStream wi(os, false, false); - os << "Formula "; + odocstringstream oss; + WriteStream wi(oss, false, false); + oss << "Formula "; write(wi); + os << to_utf8(oss.str()); } @@ -1414,7 +1428,7 @@ void InsetMathHull::read(Buffer const &, LyXLex & lex) } -int InsetMathHull::plaintext(Buffer const &, ostream & os, +int InsetMathHull::plaintext(Buffer const &, odocstream & os, OutputParams const &) const { if (0 && display()) { @@ -1435,60 +1449,65 @@ int InsetMathHull::plaintext(Buffer const &, ostream & os, } -int InsetMathHull::docbook(Buffer const & buf, ostream & os, +int InsetMathHull::docbook(Buffer const & buf, odocstream & os, OutputParams const & runparams) const { - MathMLStream ms(os); + MathStream ms(os); int res = 0; - string name; + docstring name; if (getType() == hullSimple) - name = "inlineequation"; + name = from_ascii("inlineequation"); else - name = "informalequation"; + name = from_ascii("informalequation"); - string bname = name; + docstring bname = name; if (!label(0).empty()) - bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\""; - ms << MTag(bname.c_str()); + bname += " id='" + sgml::cleanID(buf, runparams, label(0)) + "'"; + ms << MTag(bname); - ostringstream ls; + odocstringstream ls; if (runparams.flavor == OutputParams::XML) { - ms << MTag("alt role=\"tex\" "); + ms << MTag(from_ascii("alt role='tex' ")); // Workaround for db2latex: db2latex always includes equations with // \ensuremath{} or \begin{display}\end{display} // so we strip LyX' math environment WriteStream wi(ls, false, false); InsetMathGrid::write(wi); - ms << subst(subst(ls.str(), "&", "&"), "<", "<"); - ms << ETag("alt"); - ms << MTag("math"); + ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<")); + ms << ETag(from_ascii("alt")); + ms << MTag(from_ascii("math")); + ms << ETag(from_ascii("alt")); + ms << MTag(from_ascii("math")); InsetMathGrid::mathmlize(ms); - ms << ETag("math"); + ms << ETag(from_ascii("math")); } else { - ms << MTag("alt role=\"tex\""); + ms << MTag(from_ascii("alt role='tex'")); res = latex(buf, ls, runparams); - ms << subst(subst(ls.str(), "&", "&"), "<", "<"); - ms << ETag("alt"); + ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<")); + ms << ETag(from_ascii("alt")); } - ms << ""; + ms << from_ascii("\"/>"); else - ms << "\">"; + ms << from_ascii("\">"); - ms << ETag(name.c_str()); + ms << ETag(name); return ms.line() + res; } -int InsetMathHull::textString(Buffer const & buf, ostream & os, +int InsetMathHull::textString(Buffer const & buf, odocstream & os, OutputParams const & op) const { return plaintext(buf, os, op); } + + +} // namespace lyx