]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathHull.cpp
simplify GuiToc / TocWidget interaction. Much can still be simplified...
[lyx.git] / src / mathed / InsetMathHull.cpp
index a26f7556b36d61eec59e069a1799e1245bea4032..976b4865cf2f28a88186fded7b75e0f03fa18697 100644 (file)
 #include "MathSupport.h"
 #include "InsetMathRef.h"
 
-#include "bufferview_funcs.h"
-#include "LyXText.h"
-
 #include "Buffer.h"
 #include "BufferParams.h"
 #include "BufferView.h"
 #include "CutAndPaste.h"
 #include "FuncStatus.h"
-#include "Color.h"
 #include "LaTeXFeatures.h"
 #include "Cursor.h"
-#include "debug.h"
+#include "support/debug.h"
 #include "DispatchResult.h"
 #include "FuncRequest.h"
-#include "gettext.h"
+#include "support/gettext.h"
 #include "LyXRC.h"
 #include "OutputParams.h"
 #include "sgml.h"
+#include "Text.h"
 #include "TextPainter.h"
-#include "Undo.h"
 
 #include "insets/RenderPreview.h"
 #include "insets/InsetLabel.h"
 #include "graphics/PreviewImage.h"
 #include "graphics/PreviewLoader.h"
 
-#include "support/lyxlib.h"
-#include "support/lstrings.h"
+#include "frontends/Painter.h"
 
-#include <boost/bind.hpp>
+#include "support/lstrings.h"
 
 #include <sstream>
 
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 
 using cap::grabAndEraseSelection;
-using support::bformat;
-using support::subst;
-
-using std::endl;
-using std::max;
-using std::ostream;
-using std::auto_ptr;
-using std::istringstream;
-using std::ostringstream;
-using std::pair;
-using std::swap;
-using std::vector;
-
 
 namespace {
 
@@ -162,7 +146,7 @@ InsetMathHull::InsetMathHull()
        //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
        //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
        //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
-       //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
+       //lyxerr << "sizeof FontInfo: " << sizeof(FontInfo) << endl;
        initMath();
        setDefaults();
 }
@@ -180,7 +164,7 @@ InsetMathHull::InsetMathHull(HullType type)
 InsetMathHull::InsetMathHull(InsetMathHull const & other)
        : InsetMathGrid(other),
          type_(other.type_), nonum_(other.nonum_), label_(other.label_),
-         preview_(new RenderPreview(this))
+         preview_(new RenderPreview(*other.preview_, this))
 {}
 
 
@@ -188,9 +172,9 @@ InsetMathHull::~InsetMathHull()
 {}
 
 
-auto_ptr<Inset> InsetMathHull::doClone() const
+Inset * InsetMathHull::clone() const
 {
-       return auto_ptr<Inset>(new InsetMathHull(*this));
+       return new InsetMathHull(*this);
 }
 
 
@@ -247,6 +231,8 @@ char InsetMathHull::defaultColAlign(col_type col)
 {
        if (type_ == hullEqnArray)
                return "rcl"[col];
+       if (type_ == hullGather)
+               return 'c';
        if (type_ >= hullAlign)
                return "rl"[col & 1];
        return 'c';
@@ -275,14 +261,14 @@ bool InsetMathHull::previewState(BufferView * bv) const
 {
        if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
                graphics::PreviewImage const * pimage =
-                       preview_->getPreviewImage(*bv->buffer());
+                       preview_->getPreviewImage(bv->buffer());
                return pimage && pimage->image();
        }
        return false;
 }
 
 
-bool InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
+void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        if (previewState(mi.base.bv)) {
                preview_->metrics(mi, dim);
@@ -290,10 +276,9 @@ bool InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
                dim.wid += 1;
                if (display())
                        dim.des += displayMargin();
-               if (dim_ == dim)
-                       return false;
-               dim_ = dim;
-               return true;
+               // Cache the inset dimension. 
+               setDimCache(mi, dim);
+               return;
        }
 
        FontSetChanger dummy1(mi.base, standardFont());
@@ -323,17 +308,22 @@ bool InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
        math_font_max_dim(mi.base.font, asc, des);
        dim.asc = max(dim.asc, asc);
        dim.des = max(dim.des, des);
-
-       if (dim_ == dim)
-               return false;
-       dim_ = dim;
-       return true;
+       // Cache the inset dimension.
+       // FIXME: This will overwrite InsetMathGrid dimension, is that OK?
+       setDimCache(mi, dim);
 }
 
 
 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
 {
        use_preview_ = previewState(pi.base.bv);
+       Dimension const dim = dimension(*pi.base.bv);
+
+       // background of mathed under focus is not painted because
+       // selection at the top level of nested inset is difficult to handle.
+       if (!editing(pi.base.bv))
+               pi.pain.fillRectangle(x + 1, y - dim.asc + 1, dim.wid - 2,
+                               dim.asc + dim.des - 1, Color_mathbg);
 
        if (use_preview_) {
                // one pixel gap in front
@@ -463,9 +453,9 @@ bool InsetMathHull::ams() const
 }
 
 
-bool InsetMathHull::display() const
+Inset::DisplayType InsetMathHull::display() const
 {
-       return type_ != hullSimple && type_ != hullNone;
+       return (type_ != hullSimple && type_ != hullNone) ? AlignCenter : Inline;
 }
 
 
@@ -707,7 +697,7 @@ void InsetMathHull::splitTo3Cols()
        BOOST_ASSERT(ncols() < 3);
        if (ncols() < 2)
                splitTo2Cols();
-       InsetMathGrid::addCol(1);
+       InsetMathGrid::addCol(2);
        for (row_type row = 0; row < nrows(); ++row) {
                idx_type const i = 3 * row + 1;
                if (cell(i).size()) {
@@ -729,7 +719,7 @@ void InsetMathHull::changeCols(col_type cols)
                else {
                        splitTo3Cols();
                        while (ncols() < cols)
-                               InsetMathGrid::addCol(ncols() - 1);
+                               InsetMathGrid::addCol(ncols());
                }
                return;
        }
@@ -962,10 +952,9 @@ void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
        iss >> dlang >> extra;
        if (extra.empty())
                extra = from_ascii("noextra");
-       std::string const lang = to_ascii(dlang);
+       string const lang = to_ascii(dlang);
 
-#ifdef WITH_WARNINGS
-#warning temporarily disabled
+       // FIXME: temporarily disabled
        //if (cur.selection()) {
        //      MathData ar;
        //      selGet(cur.ar);
@@ -973,7 +962,6 @@ void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
        //      insert(pipeThroughExtern(lang, extra, ar));
        //      return;
        //}
-#endif
 
        MathData eq;
        eq.push_back(MathAtom(new InsetMathChar('=')));
@@ -1020,9 +1008,7 @@ void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
                cur.pos() = 0;
                MathData ar = cur.cell();
                lyxerr << "use cell: " << ar << endl;
-#ifdef WITH_WARNINGS
-#warning temporarily disabled
-#endif
+               // FIXME: temporarily disabled
                addRow(cur.row());
                ++cur.idx();
                ++cur.idx();
@@ -1039,10 +1025,10 @@ void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
        //lyxerr << "action: " << cmd.action << endl;
        switch (cmd.action) {
 
-       case LFUN_FINISHED_LEFT:
+       case LFUN_FINISHED_BACKWARD:
+       case LFUN_FINISHED_FORWARD:
        case LFUN_FINISHED_RIGHT:
-       case LFUN_FINISHED_UP:
-       case LFUN_FINISHED_DOWN:
+       case LFUN_FINISHED_LEFT:
                //lyxerr << "action: " << cmd.action << endl;
                InsetMathGrid::doDispatch(cur, cmd);
                notifyCursorLeaves(cur);
@@ -1053,46 +1039,44 @@ void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
                // just swallow this
                break;
 
-       case LFUN_BREAK_LINE:
+       case LFUN_NEW_LINE:
                // some magic for the common case
                if (type_ == hullSimple || type_ == hullEquation) {
-                       recordUndoInset(cur);
+                       cur.recordUndoInset();
                        bool const align =
-                               cur.bv().buffer()->params().use_amsmath == BufferParams::package_on;
+                               cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
                        mutate(align ? hullAlign : hullEqnArray);
-                       cur.idx() = 0;
+                       cur.idx() = nrows() * ncols() - 1;
                        cur.pos() = cur.lastpos();
                }
                InsetMathGrid::doDispatch(cur, cmd);
                break;
 
-       case LFUN_MATH_NUMBER:
+       case LFUN_MATH_NUMBER_TOGGLE: {
                //lyxerr << "toggling all numbers" << endl;
-               if (display()) {
-                       recordUndoInset(cur);
-                       bool old = numberedType();
-                       if (type_ == hullMultline)
-                               numbered(nrows() - 1, !old);
-                       else
-                               for (row_type row = 0; row < nrows(); ++row)
-                                       numbered(row, !old);
-
-                       cur.message(old ? _("No number") : _("Number"));
-               }
+               cur.recordUndoInset();
+               bool old = numberedType();
+               if (type_ == hullMultline)
+                       numbered(nrows() - 1, !old);
+               else
+                       for (row_type row = 0; row < nrows(); ++row)
+                               numbered(row, !old);
+               
+               cur.message(old ? _("No number") : _("Number"));
                break;
+       }
 
-       case LFUN_MATH_NONUMBER:
-               if (display()) {
-                       recordUndoInset(cur);
-                       row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
-                       bool old = numbered(r);
-                       cur.message(old ? _("No number") : _("Number"));
-                       numbered(r, !old);
-               }
+       case LFUN_MATH_NUMBER_LINE_TOGGLE: {
+               cur.recordUndoInset();
+               row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
+               bool old = numbered(r);
+               cur.message(old ? _("No number") : _("Number"));
+               numbered(r, !old);
                break;
+       }
 
        case LFUN_LABEL_INSERT: {
-               recordUndoInset(cur);
+               cur.recordUndoInset();
                row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
                docstring old_label = label(r);
                docstring const default_label = from_ascii(
@@ -1100,12 +1084,12 @@ void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
                if (old_label.empty())
                        old_label = default_label;
 
-               InsetCommandParams p("label");
+               InsetCommandParams p(LABEL_CODE);
                p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
-               std::string const data = InsetCommandMailer::params2string("label", p);
+               string const data = InsetCommandMailer::params2string("label", p);
 
                if (cmd.argument().empty())
-                       cur.bv().showInsetDialog("label", data, 0);
+                       cur.bv().showDialog("label", data);
                else {
                        FuncRequest fr(LFUN_INSET_INSERT, data);
                        dispatch(cur, fr);
@@ -1115,20 +1099,20 @@ void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_INSET_INSERT: {
                //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
-               std::string const name = cmd.getArg(0);
+               string const name = cmd.getArg(0);
                if (name == "label") {
-                       InsetCommandParams p("label");
+                       InsetCommandParams p(LABEL_CODE);
                        InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
                        docstring str = p["name"];
-                       recordUndoInset(cur);
+                       cur.recordUndoInset();
                        row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
-                       str = support::trim(str);
+                       str = trim(str);
                        if (!str.empty())
                                numbered(r, true);
                        docstring old = label(r);
                        if (str != old) {
-                               cur.bv().buffer()->changeRefsIfUnique(old, str,
-                                                       Inset::REF_CODE);
+                               cur.bv().buffer().changeRefsIfUnique(old, str,
+                                                       REF_CODE);
                                label(r, str);
                        }
                        break;
@@ -1138,12 +1122,12 @@ void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
        }
 
        case LFUN_MATH_EXTERN:
-               recordUndoInset(cur);
+               cur.recordUndoInset();
                doExtern(cur, cmd);
                break;
 
        case LFUN_MATH_MUTATE: {
-               recordUndoInset(cur);
+               cur.recordUndoInset();
                row_type row = cur.row();
                col_type col = cur.col();
                mutate(hullType(cmd.argument()));
@@ -1154,12 +1138,16 @@ void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
                }
                if (cur.pos() > cur.lastpos())
                        cur.pos() = cur.lastpos();
+               
+               // FIXME: find some more clever handling of the selection,
+               // i.e. preserve it.
+               cur.clearSelection();
                //cur.dispatched(FINISHED);
                break;
        }
 
        case LFUN_MATH_DISPLAY: {
-               recordUndoInset(cur);
+               cur.recordUndoInset();
                mutate(type_ == hullSimple ? hullEquation : hullSimple);
                cur.idx() = 0;
                cur.pos() = cur.lastpos();
@@ -1178,21 +1166,33 @@ bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
                FuncStatus & status) const
 {
        switch (cmd.action) {
-       case LFUN_FINISHED_LEFT:
+       case LFUN_FINISHED_BACKWARD:
+       case LFUN_FINISHED_FORWARD:
        case LFUN_FINISHED_RIGHT:
-       case LFUN_FINISHED_UP:
-       case LFUN_FINISHED_DOWN:
-               status.enabled(true);
-               return true;
-       case LFUN_BREAK_LINE:
-       case LFUN_MATH_NUMBER:
-       case LFUN_MATH_NONUMBER:
+       case LFUN_FINISHED_LEFT:
+       case LFUN_UP:
+       case LFUN_DOWN:
+       case LFUN_NEW_LINE:
        case LFUN_MATH_EXTERN:
        case LFUN_MATH_MUTATE:
        case LFUN_MATH_DISPLAY:
                // we handle these
                status.enabled(true);
                return true;
+       case LFUN_MATH_NUMBER_TOGGLE:
+               // FIXME: what is the right test, this or the one of
+               // LABEL_INSERT?
+               status.enabled(display());
+               status.setOnOff(numberedType());
+               return true;
+       case LFUN_MATH_NUMBER_LINE_TOGGLE: {
+               // FIXME: what is the right test, this or the one of
+               // LABEL_INSERT?
+               status.enabled(display());
+               row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
+               status.setOnOff(numbered(r));
+               return true;
+       }
        case LFUN_LABEL_INSERT:
                status.enabled(type_ != hullSimple);
                return true;
@@ -1204,7 +1204,7 @@ bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
                return InsetMathGrid::getStatus(cur, cmd, status);
        case LFUN_TABULAR_FEATURE: {
                istringstream is(to_utf8(cmd.argument()));
-               std::string s;
+               string s;
                is >> s;
                if (!rowChangeOK()
                    && (s == "append-row"
@@ -1274,7 +1274,7 @@ void InsetMathHull::mutateToText()
        string str = os.str();
 
        // insert this text
-       LyXText * lt = view_->cursor().innerText();
+       Text * lt = view_->cursor().innerText();
        string::const_iterator cit = str.begin();
        string::const_iterator end = str.end();
        for (; cit != end; ++cit)
@@ -1291,7 +1291,7 @@ void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
 {
        // this whole function is a hack and won't work for incremental font
        // changes...
-       recordUndo(cur);
+       cur.recordUndo();
        if (cur.inset().asInsetMath()->name() == font)
                cur.handleFont(to_utf8(font));
        else {
@@ -1303,24 +1303,26 @@ void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
 
 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
 {
-       recordUndo(cur);
-       LyXFont font;
+       cur.recordUndo();
+       Font font;
        bool b;
-       bv_funcs::string2font(to_utf8(arg), font, b);
-       if (font.color() != Color::inherit) {
-               MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
+       font.fromString(to_utf8(arg), b);
+       if (font.fontInfo().color() != Color_inherit) {
+               MathAtom at = MathAtom(new InsetMathColor(true, font.fontInfo().color()));
                cur.handleNest(at, 0);
        }
 }
 
 
-void InsetMathHull::edit(Cursor & cur, bool left)
+void InsetMathHull::edit(Cursor & cur, bool front, EntryDirection entry_from)
 {
        cur.push(*this);
-       left ? idxFirst(cur) : idxLast(cur);
+       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. 
+       // BufferView that a metrics update is needed.
        cur.updateFlags(Update::Force);
 }
 
@@ -1367,7 +1369,7 @@ void InsetMathHull::revealCodes(Cursor & cur) const
 }
 
 
-Inset::Code InsetMathHull::lyxCode() const
+InsetCode InsetMathHull::lyxCode() const
 {
        return MATH_CODE;
 }
@@ -1380,9 +1382,7 @@ Inset::Code InsetMathHull::lyxCode() const
 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
                                     bool, bool)
 {
-#ifdef WITH_WARNINGS
-#warning completely broken
-#endif
+       // FIXME: completely broken
        static InsetMathHull * lastformula = 0;
        static CursorBase current = DocIterator(ibegin(nucleus()));
        static MathData ar;
@@ -1419,7 +1419,7 @@ bool InsetMathHull::searchForward(BufferView * bv, string const & str,
 #endif
 
 
-void InsetMathHull::write(Buffer const &, std::ostream & os) const
+void InsetMathHull::write(Buffer const &, ostream & os) const
 {
        odocstringstream oss;
        WriteStream wi(oss, false, false);
@@ -1438,7 +1438,7 @@ void InsetMathHull::read(Buffer const &, Lexer & lex)
 
 
 int InsetMathHull::plaintext(Buffer const &, odocstream & os,
-                             OutputParams const &) const
+                            OutputParams const &) const
 {
        if (0 && display()) {
                Dimension dim;
@@ -1463,7 +1463,7 @@ int InsetMathHull::plaintext(Buffer const &, odocstream & os,
 
 
 int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
-                           OutputParams const & runparams) const
+                          OutputParams const & runparams) const
 {
        MathStream ms(os);
        int res = 0;
@@ -1476,28 +1476,29 @@ int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
        docstring bname = name;
        if (!label(0).empty())
                bname += " id='" + sgml::cleanID(buf, runparams, label(0)) + "'";
-       ms << MTag(bname);
+
+       ++ms.tab(); ms.cr(); ms.os() << '<' << bname << '>';
 
        odocstringstream ls;
        if (runparams.flavor == OutputParams::XML) {
-               ms << MTag(from_ascii("alt role='tex' "));
+               ms << MTag("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 << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&amp;"), "<", "&lt;"));
-               ms << ETag(from_ascii("alt"));
-               ms << MTag(from_ascii("math"));
-               ms << ETag(from_ascii("alt"));
-               ms << MTag(from_ascii("math"));
+               ms << ETag("alt");
+               ms << MTag("math");
+               ms << ETag("alt");
+               ms << MTag("math");
                InsetMathGrid::mathmlize(ms);
-               ms << ETag(from_ascii("math"));
+               ms << ETag("math");
        } else {
-               ms << MTag(from_ascii("alt role='tex'"));
+               ms << MTag("alt role='tex'");
                res = latex(buf, ls, runparams);
                ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&amp;"), "<", "&lt;"));
-               ms << ETag(from_ascii("alt"));
+               ms << ETag("alt");
        }
 
        ms << from_ascii("<graphic fileref=\"eqn/");
@@ -1511,7 +1512,8 @@ int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
        else
                ms << from_ascii("\">");
 
-       ms << ETag(name);
+       ms.cr(); --ms.tab(); ms.os() << "</" << name << '>';
+
        return ms.line() + res;
 }