]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathMacroTemplate.cpp
Merge branch 'master' of git.lyx.org:lyx
[lyx.git] / src / mathed / MathMacroTemplate.cpp
index f7214cb5b3589ade325857504237eeff3edf4003..8220aa68a636f8691ff616d6acf688659a94ba61 100644 (file)
@@ -35,7 +35,6 @@
 #include "FuncStatus.h"
 #include "Lexer.h"
 #include "LyXRC.h" 
-#include "Undo.h"
 
 #include "frontends/Painter.h"
 
@@ -48,6 +47,7 @@
 #include "support/docstream.h"
 #include "support/lstrings.h"
 
+#include <set>
 #include <sstream>
 
 using namespace std;
@@ -592,6 +592,7 @@ void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
 
 void MathMacroTemplate::draw(PainterInfo & pi, int x, int y) const
 {
+       ColorChanger dummy0(pi.base.font, Color_math);
        FontSetChanger dummy1(pi.base, from_ascii("mathnormal"));
        StyleChanger dummy2(pi.base, LM_ST_TEXT);
 
@@ -616,7 +617,7 @@ void MathMacroTemplate::draw(PainterInfo & pi, int x, int y) const
 void MathMacroTemplate::edit(Cursor & cur, bool front, EntryDirection entry_from)
 {
        updateLook();
-       cur.updateFlags(Update::SinglePar);
+       cur.screenUpdateFlags(Update::SinglePar);
        InsetMathNest::edit(cur, front, entry_from);
 }
 
@@ -625,7 +626,7 @@ bool MathMacroTemplate::notifyCursorLeaves(Cursor const & old, Cursor & cur)
 {
        commitEditChanges(cur, old);
        updateLook();
-       cur.updateFlags(Update::Force);
+       cur.screenUpdateFlags(Update::Force);
        return InsetMathNest::notifyCursorLeaves(old, cur);
 }
 
@@ -673,7 +674,7 @@ void MathMacroTemplate::shiftArguments(size_t from, int by)
 int MathMacroTemplate::maxArgumentInDefinition() const
 {
        // We don't have a buffer when pasting from the clipboard (bug 6014).
-       Buffer const * macro_buffer = this->isBufferValid() ? &buffer() : 0;
+       Buffer const * macro_buffer = isBufferLoaded() ? &buffer() : 0;
        int maxArg = 0;
        DocIterator it = doc_iterator_begin(macro_buffer, this);
        it.idx() = defIdx();
@@ -721,11 +722,11 @@ void MathMacroTemplate::changeArity(Cursor & cur,
 {
        // remove parameter which do not appear anymore in the definition
        for (int i = numargs_; i > newNumArg; --i)
-               removeParameter(cur, inset_pos, numargs_ - 1, false);
+               removeParameter(cur, inset_pos, numargs_ - 1, true);
        
        // add missing parameter
        for (int i = numargs_; i < newNumArg; ++i)
-               insertParameter(cur, inset_pos, numargs_, false, false);
+               insertParameter(cur, inset_pos, numargs_, true, false);
 }
 
 
@@ -736,7 +737,8 @@ public:
        ///
        AddRemoveMacroInstanceFix(int n, bool insert) : n_(n), insert_(insert) {}
        ///
-       void operator()(MathMacro * macro) {
+       void operator()(MathMacro * macro)
+       {
                if (macro->folded()) {
                        if (insert_)
                                macro->insertArgument(n_);
@@ -760,7 +762,8 @@ public:
        ///
        OptionalsMacroInstanceFix(int optionals) : optionals_(optionals) {}
        ///
-       void operator()(MathMacro * macro) {
+       void operator()(MathMacro * macro)
+       {
                macro->setOptionals(optionals_);
        }
 
@@ -791,18 +794,15 @@ void fixMacroInstances(Cursor & cur, DocIterator const & inset_pos,
        // remember hull to trigger preview reload
        DocIterator hull(dit.buffer());
        bool preview_reload_needed = false;
+       set<DocIterator> preview_hulls;
 
        // iterate over all positions until macro is redefined
        for (; dit; dit.forwardPos()) {
                // left the outer hull?
                if (!hull.empty() && dit.depth() == hull.depth()) {
-                       // reload the preview if necessary 
+                       // schedule reload of the preview if necessary 
                        if (preview_reload_needed) {
-                               InsetMathHull * inset_hull =
-                                       hull.nextInset()->asInsetMath()->asHullInset();
-                               LASSERT(inset_hull, /**/);
-                               inset_hull->reloadPreview(hull);
-                               cur.updateFlags(Update::Force);
+                               preview_hulls.insert(hull);
                                preview_reload_needed = false;
                        }
                        hull.clear();
@@ -838,6 +838,19 @@ void fixMacroInstances(Cursor & cur, DocIterator const & inset_pos,
                                preview_reload_needed = true;
                }
        }
+
+       if (!preview_hulls.empty()) {
+               // reload the scheduled previews
+               set<DocIterator>::const_iterator sit = preview_hulls.begin();
+               set<DocIterator>::const_iterator end = preview_hulls.end();
+               for (; sit != end; ++sit) {
+                       InsetMathHull * inset_hull =
+                               sit->nextInset()->asInsetMath()->asHullInset();
+                       LASSERT(inset_hull, /**/);
+                       inset_hull->reloadPreview(*sit);
+               }
+               cur.screenUpdateFlags(Update::Force);
+       }
 }
 
 
@@ -974,14 +987,14 @@ void MathMacroTemplate::makeNonOptional(Cursor & cur,
 void MathMacroTemplate::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        string const arg = to_utf8(cmd.argument());
-       switch (cmd.action) {
+       switch (cmd.action()) {
 
        case LFUN_MATH_MACRO_ADD_PARAM:
                if (numargs_ < 9) {
                        commitEditChanges(cur, cur);
                        cur.recordUndoFullDocument();
                        size_t pos = numargs_;
-                       if (arg.size() != 0)
+                       if (!arg.empty())
                                pos = (size_t)convert<int>(arg) - 1; // it is checked for >=0 in getStatus
                        insertParameter(cur, cur, pos);
                }
@@ -993,7 +1006,7 @@ void MathMacroTemplate::doDispatch(Cursor & cur, FuncRequest & cmd)
                        commitEditChanges(cur, cur);
                        cur.recordUndoFullDocument();
                        size_t pos = numargs_ - 1;
-                       if (arg.size() != 0)
+                       if (!arg.empty())
                                pos = (size_t)convert<int>(arg) - 1; // it is checked for >=0 in getStatus
                        removeParameter(cur, cur, pos);
                }
@@ -1064,10 +1077,10 @@ bool MathMacroTemplate::getStatus(Cursor & /*cur*/, FuncRequest const & cmd,
 {
        bool ret = true;
        string const arg = to_utf8(cmd.argument());
-       switch (cmd.action) {
+       switch (cmd.action()) {
                case LFUN_MATH_MACRO_ADD_PARAM: {
                        int num = numargs_ + 1;
-                       if (arg.size() != 0)
+                       if (!arg.empty())
                                num = convert<int>(arg);
                        bool on = (num >= optionals_
                                   && numargs_ < 9 && num <= numargs_ + 1);
@@ -1079,9 +1092,10 @@ bool MathMacroTemplate::getStatus(Cursor & /*cur*/, FuncRequest const & cmd,
                        flag.setEnabled(numargs_ < 9);
                        break;
 
+               case LFUN_MATH_MACRO_REMOVE_GREEDY_PARAM:
                case LFUN_MATH_MACRO_REMOVE_PARAM: {
                        int num = numargs_;
-                       if (arg.size() != 0)
+                       if (!arg.empty())
                                num = convert<int>(arg);
                        flag.setEnabled(num >= 1 && num <= numargs_);
                        break;
@@ -1154,8 +1168,10 @@ void MathMacroTemplate::write(WriteStream & os) const
 }
 
 
-void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) const
+int MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) const
 {
+       int num_lines = 0;
+
        if (os.latex()) {
                if (optionals_ > 0) {
                        // macros with optionals use the xargs package, e.g.:
@@ -1218,14 +1234,24 @@ void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) cons
        if (os.latex()) {
                // writing .tex. done.
                os << "\n";
+               ++num_lines;
        } else {
                // writing .lyx, write special .tex export only if necessary
-               if (!cell(displayIdx()).empty())
+               if (!cell(displayIdx()).empty()) {
                        os << "\n{" << cell(displayIdx()) << '}';
+                       ++num_lines;
+               }
        }
+
+       return num_lines;
 }
 
 
+docstring MathMacroTemplate::xhtml(XHTMLStream &, OutputParams const &) const
+{
+       return docstring();
+}
+
 int MathMacroTemplate::plaintext(odocstream & os,
                                 OutputParams const &) const
 {
@@ -1240,8 +1266,7 @@ bool MathMacroTemplate::validName() const
 {
        docstring n = name();
 
-       // empty name?
-       if (n.size() == 0)
+       if (n.empty())
                return false;
 
        // converting back and force doesn't swallow anything?
@@ -1290,7 +1315,7 @@ bool MathMacroTemplate::fixNameAndCheckIfValid()
        }
 
        // now it should be valid if anything in the name survived
-       return data.size() > 0;
+       return !data.empty();
 }
 
        
@@ -1341,9 +1366,9 @@ void MathMacroTemplate::infoize(odocstream & os) const
 }
 
 
-docstring MathMacroTemplate::contextMenu(BufferView const &, int, int) const
+string MathMacroTemplate::contextMenuName() const
 {
-       return from_ascii("context-math-macro-definition");
+       return "context-math-macro-definition";
 }
 
 } // namespace lyx