]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathMacro.cpp
* src/frontends/controllers/Dialog.{cpp,h}:
[lyx.git] / src / mathed / MathMacro.cpp
index 1ce383b555f1fb504cbee82c1abbe2159af08850..f68529c8e071a635919fc9cce8e0926bcdca9a17 100644 (file)
@@ -33,38 +33,40 @@ using std::endl;
 using std::vector;
 
 
-/// This class is the value of a macro argument, technically 
+/// This class is the value of a macro argument, technically
 /// a wrapper of the cells of MathMacro.
-class MathMacroArgumentValue : public InsetMathDim {
+class MathMacroArgumentValue : public InsetMath {
 public:
        ///
-       MathMacroArgumentValue(MathArray const * value, docstring const & macroName) 
-               : value_(value), macroName_(macroName) {}
+       MathMacroArgumentValue(MathMacro const & mathMacro, size_t idx)
+               : mathMacro_(mathMacro), idx_(idx) {}
        ///
        bool metrics(MetricsInfo & mi, Dimension & dim) const;
        ///
        void draw(PainterInfo &, int x, int y) const;
-       
+       ///
+       int kerning() const { return mathMacro_.cell(idx_).kerning(); }
+
 private:
-       std::auto_ptr<InsetBase> doClone() const;
-       MathArray const * value_;
-       docstring macroName_;
+       std::auto_ptr<Inset> doClone() const;
+       MathMacro const & mathMacro_;
+       size_t idx_;
 };
 
 
-auto_ptr<InsetBase> MathMacroArgumentValue::doClone() const 
+auto_ptr<Inset> MathMacroArgumentValue::doClone() const
 {
-       return auto_ptr<InsetBase>(new MathMacroArgumentValue(*this));
+       return auto_ptr<Inset>(new MathMacroArgumentValue(*this));
 }
 
 
-bool MathMacroArgumentValue::metrics(MetricsInfo & mi, Dimension & dim) const 
+bool MathMacroArgumentValue::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        // unlock outer macro in arguments, and lock it again later
-       MacroTable::globalMacros().get(macroName_).unlock();
-       value_->metrics(mi, dim);
-       MacroTable::globalMacros().get(macroName_).lock();
-       metricsMarkers2(dim);
+       MacroData const & macro = MacroTable::globalMacros().get(mathMacro_.name());
+       macro.unlock();
+       mathMacro_.cell(idx_).metrics(mi, dim);
+       macro.lock();
        if (dim_ == dim)
                return false;
        dim_ = dim;
@@ -72,23 +74,27 @@ bool MathMacroArgumentValue::metrics(MetricsInfo & mi, Dimension & dim) const
 }
 
 
-void MathMacroArgumentValue::draw(PainterInfo & pi, int x, int y) const 
+void MathMacroArgumentValue::draw(PainterInfo & pi, int x, int y) const
 {
        // unlock outer macro in arguments, and lock it again later
-       MacroTable::globalMacros().get(macroName_).unlock();
-       value_->draw(pi, x, y);
-       MacroTable::globalMacros().get(macroName_).lock();
+       MacroData const & macro = MacroTable::globalMacros().get(mathMacro_.name());
+       macro.unlock();
+       mathMacro_.cell(idx_).draw(pi, x, y);
+       macro.lock();
 }
 
 
 MathMacro::MathMacro(docstring const & name, int numargs)
-       : InsetMathNest(numargs), name_(name)
+       : InsetMathNest(numargs), name_(name), editing_(false)
 {}
 
 
-auto_ptr<InsetBase> MathMacro::doClone() const
+auto_ptr<Inset> MathMacro::doClone() const
 {
-       return auto_ptr<InsetBase>(new MathMacro(*this));
+       MathMacro * x = new MathMacro(*this);
+       x->expanded_ = MathData();
+       x->macroBackup_ = MacroData();
+       return auto_ptr<Inset>(x);
 }
 
 
@@ -109,17 +115,19 @@ void MathMacro::cursorPos(BufferView const & bv,
 
 bool MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
 {
+       kerning_ = 0;
        if (!MacroTable::globalMacros().has(name())) {
                mathed_string_dim(mi.base.font, "Unknown: " + name(), dim);
        } else {
                MacroData const & macro = MacroTable::globalMacros().get(name());
+
+               if (macroBackup_ != macro)
+                       updateExpansion();
+
                if (macro.locked()) {
                        mathed_string_dim(mi.base.font, "Self reference: " + name(), dim);
-                       expanded_ = MathArray();
                } else if (editing(mi.base.bv)) {
-                       // FIXME UNICODE
-                       asArray(macro.def(), tmpl_);
-                       LyXFont font = mi.base.font;
+                       Font font = mi.base.font;
                        augmentFont(font, from_ascii("lyxtex"));
                        tmpl_.metrics(mi, dim);
                        // FIXME UNICODE
@@ -127,25 +135,20 @@ bool MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
                        // FIXME UNICODE
                        int ww = mathed_string_width(font, from_ascii("#1: "));
                        for (idx_type i = 0; i < nargs(); ++i) {
-                               MathArray const & c = cell(i);
+                               MathData const & c = cell(i);
                                c.metrics(mi);
                                dim.wid  = max(dim.wid, c.width() + ww);
                                dim.des += c.height() + 10;
                        }
+                       editing_ = true;
                } else {
-                       // create MathMacroArgumentValue object pointing to the cells of the macro
-                       MacroData const & macro = MacroTable::globalMacros().get(name());
-                       vector<MathArray> values(nargs());
-                       for (size_t i = 0; i != nargs(); ++i) 
-                               values[i].insert(0, MathAtom(new MathMacroArgumentValue(&cells_[i], name())));
-                       macro.expand(values, expanded_);
-                       
-                       MacroTable::globalMacros().get(name()).lock();
+                       macro.lock();
                        expanded_.metrics(mi, dim);
-                       MacroTable::globalMacros().get(name()).unlock();
+                       macro.unlock();
+                       kerning_ = expanded_.kerning();
+                       editing_ = false;
                }
        }
-       metricsMarkers2(dim);
        if (dim_ == dim)
                return false;
        dim_ = dim;
@@ -160,11 +163,16 @@ void MathMacro::draw(PainterInfo & pi, int x, int y) const
                drawStrRed(pi, x, y, "Unknown: " + name());
        } else {
                MacroData const & macro = MacroTable::globalMacros().get(name());
+
+               // warm up cache
+               for (size_t i = 0; i < nargs(); ++i)
+                       cell(i).setXY(*pi.base.bv, x, y);
+
                if (macro.locked()) {
                        // FIXME UNICODE
                        drawStrRed(pi, x, y, "Self reference: " + name());
-               } else if (editing(pi.base.bv)) {
-                       LyXFont font = pi.base.font;
+               } else if (editing_) {
+                       Font font = pi.base.font;
                        augmentFont(font, from_ascii("lyxtex"));
                        int h = y - dim_.ascent() + 2 + tmpl_.ascent();
                        pi.pain.text(x + 3, h, name(), font);
@@ -175,7 +183,7 @@ void MathMacro::draw(PainterInfo & pi, int x, int y) const
                        docstring t = from_ascii("#1: ");
                        mathed_string_dim(font, t, ldim);
                        for (idx_type i = 0; i < nargs(); ++i) {
-                               MathArray const & c = cell(i);
+                               MathData const & c = cell(i);
                                h += max(c.ascent(), ldim.asc) + 5;
                                c.draw(pi, x + ldim.wid, h);
                                char_type str[] = { '#', '1', ':', '\0' };
@@ -184,12 +192,15 @@ void MathMacro::draw(PainterInfo & pi, int x, int y) const
                                h += max(c.descent(), ldim.des) + 5;
                        }
                } else {
-                       MacroTable::globalMacros().get(name()).lock();
+                       macro.lock();
                        expanded_.draw(pi, x, y);
-                       MacroTable::globalMacros().get(name()).unlock();
+                       macro.unlock();
                }
+
+               // edit mode changed?
+               if (editing_ != editing(pi.base.bv) || macroBackup_ != macro)
+                       pi.base.bv->cursor().updateFlags(Update::Force);
        }
-       drawMarkers2(pi, x, y);
 }
 
 
@@ -203,12 +214,16 @@ void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
 
 void MathMacro::validate(LaTeXFeatures & features) const
 {
+       string const require = MacroTable::globalMacros().get(name()).requires();
+       if (!require.empty())
+               features.require(require);
+
        if (name() == "binom" || name() == "mathcircumflex")
                features.require(to_utf8(name()));
 }
 
 
-InsetBase * MathMacro::editXY(Cursor & cur, int x, int y)
+Inset * MathMacro::editXY(Cursor & cur, int x, int y)
 {
        // We may have 0 arguments, but InsetMathNest requires at least one.
        if (nargs() > 0) {
@@ -226,20 +241,36 @@ InsetBase * MathMacro::editXY(Cursor & cur, int x, int y)
 }
 
 
-bool MathMacro::idxFirst(Cursor & cur) const 
+bool MathMacro::idxFirst(Cursor & cur) const
 {
        cur.updateFlags(Update::Force);
        return InsetMathNest::idxFirst(cur);
 }
 
 
-bool MathMacro::idxLast(Cursor & cur) const 
+bool MathMacro::idxLast(Cursor & cur) const
 {
        cur.updateFlags(Update::Force);
        return InsetMathNest::idxLast(cur);
 }
 
 
+bool MathMacro::idxUpDown(Cursor & cur, bool up) const
+{
+       if (up) {
+               if (cur.idx() == 0)
+                       return false;
+               --cur.idx();
+       } else {
+               if (cur.idx() + 1 >= nargs())
+                       return false;
+               ++cur.idx();
+       }
+       cur.pos() = cell(cur.idx()).x2pos(cur.x_target());
+       return true;
+}
+
+
 bool MathMacro::notifyCursorLeaves(Cursor & cur)
 {
        cur.updateFlags(Update::Force);
@@ -270,7 +301,15 @@ void MathMacro::octave(OctaveStream & os) const
 
 void MathMacro::updateExpansion() const
 {
-       //expanded_.substitute(*this);
+       MacroData const & macro = MacroTable::globalMacros().get(name());
+
+       // create MathMacroArgumentValue object pointing to the cells of the macro
+       vector<MathData> values(nargs());
+       for (size_t i = 0; i != nargs(); ++i)
+                               values[i].insert(0, MathAtom(new MathMacroArgumentValue(*this, i)));
+       macro.expand(values, expanded_);
+       asArray(macro.def(), tmpl_);
+       macroBackup_ = macro;
 }