]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathHull.C
This commit fixes a crash when accessing a math inset. This was due to an invalid...
[lyx.git] / src / mathed / InsetMathHull.C
index 81af5ffa747914ad4e631ec4682452eb29d61cb9..095bf92dd6acffe60008e3876d96e55a2842f25f 100644 (file)
@@ -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"
 
@@ -350,9 +347,8 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
                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);
+                       docstring const nl = nicelabel(row);
+                       pi.draw(xx, yy, nl);
                }
        }
        setPosCache(pi, x, y);
@@ -466,11 +462,12 @@ bool InsetMathHull::display() const
 }
 
 
-void InsetMathHull::getLabelList(Buffer const &, vector<string> & labels) const
+void InsetMathHull::getLabelList(Buffer const &, vector<docstring> & labels) const
 {
        for (row_type row = 0; row < nrows(); ++row)
                if (!label_[row].empty() && nonum_[row] != 1)
-                       labels.push_back(label_[row]);
+                       // FIXME UNICODE
+                       labels.push_back(lyx::from_utf8(label_[row]));
 }
 
 
@@ -511,37 +508,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<unsigned int>((ncols() + 1)/2) << "}\n";
+               break;
 
-       else if (type_ == hullXXAlignAt)
-               os << "\\begin{" << type_ << '}'
+       case hullXXAlignAt:
+               os << "\\begin{" << hullName(type_) << '}'
                  << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
+               break;
 
-       else
+       default:
                os << "\\begin{unknown" << star(n) << '}';
+               break;
+       }
 }
 
 
@@ -549,29 +555,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;
+       }
 }
 
 
@@ -645,13 +662,14 @@ 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 '(' + label_[row] + ')';
+               return lyx::from_ascii("(#)");
+       // FIXME UNICODE
+       return lyx::from_utf8('(' + label_[row] + ')');
 }
 
 
@@ -876,8 +894,9 @@ void InsetMathHull::mutate(HullType newtype)
        }
 
        else {
-               lyxerr << "mutation from '" << type_
-                      << "' to '" << newtype << "' not implemented" << endl;
+               lyxerr << "mutation from '" << hullName(type_)
+                      << "' to '" << hullName(newtype)
+                      << "' not implemented" << endl;
        }
 }
 
@@ -905,7 +924,7 @@ void InsetMathHull::write(WriteStream & os) const
 
 void InsetMathHull::normalize(NormalStream & os) const
 {
-       os << "[formula " << type_ << ' ';
+       os << "[formula " << hullName(type_) << ' ';
        InsetMathGrid::normalize(os);
        os << "] ";
 }
@@ -919,7 +938,7 @@ void InsetMathHull::mathmlize(MathMLStream & os) const
 
 void InsetMathHull::infoize(ostream & os) const
 {
-       os << "Type: " << type_;
+       os << "Type: " << hullName(type_);
 }
 
 
@@ -961,7 +980,8 @@ void InsetMathHull::doExtern(LCursor & cur, FuncRequest & func)
                size_type pos = cur.cell().find_last(eq);
                MathArray ar;
                if (cur.inMathed() && cur.selection()) {
-                       asArray(grabAndEraseSelection(cur), ar);
+                       // FIXME UNICODE
+                       asArray(lyx::from_utf8(grabAndEraseSelection(cur)), ar);
                } else if (pos == cur.cell().size()) {
                        ar = cur.cell();
                        lyxerr << "use whole cell: " << ar << endl;
@@ -1081,7 +1101,7 @@ void InsetMathHull::doDispatch(LCursor & cur, FuncRequest & cmd)
                string const data = InsetCommandMailer::params2string("label", p);
 
                if (cmd.argument().empty())
-                       cur.bv().owner()->getDialogs().show("label", data, 0);
+                       cur.bv().showInsetDialog("label", data, 0);
                else {
                        FuncRequest fr(LFUN_INSET_INSERT, data);
                        dispatch(cur, fr);
@@ -1103,18 +1123,14 @@ void InsetMathHull::doDispatch(LCursor & cur, FuncRequest & cmd)
                                numbered(r, true);
                        string 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:
@@ -1176,15 +1192,12 @@ 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;
@@ -1261,10 +1274,10 @@ void InsetMathHull::mutateToText()
        string::const_iterator cit = str.begin();
        string::const_iterator end = str.end();
        for (; cit != end; ++cit)
-               view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
+               view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
 
        // remove ourselves
-       //view_->owner()->dispatch(LFUN_ESCAPE);
+       //lyx::dispatch(LFUN_ESCAPE);
 #endif
 }
 
@@ -1414,7 +1427,7 @@ void InsetMathHull::read(Buffer const &, LyXLex & lex)
 }
 
 
-int InsetMathHull::plaintext(Buffer const &, ostream & os,
+int InsetMathHull::plaintext(Buffer const &, lyx::odocstream & os,
                        OutputParams const &) const
 {
        if (0 && display()) {
@@ -1428,8 +1441,11 @@ int InsetMathHull::plaintext(Buffer const &, ostream & os,
                //metrics();
                return tpain.textheight();
        } else {
-               WriteStream wi(os, false, true);
+               std::ostringstream oss;
+               WriteStream wi(oss, false, true);
                wi << cell(0);
+               // FIXME UNICODE
+               os << lyx::from_utf8(oss.str());
                return wi.line();
        }
 }
@@ -1487,7 +1503,7 @@ int InsetMathHull::docbook(Buffer const & buf, ostream & os,
 }
 
 
-int InsetMathHull::textString(Buffer const & buf, ostream & os,
+int InsetMathHull::textString(Buffer const & buf, lyx::odocstream & os,
                       OutputParams const & op) const
 {
        return plaintext(buf, os, op);