]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathMacro.cpp
Fix bug 5802 (http://bugzilla.lyx.org/show_bug.cgi?id=5802)
[lyx.git] / src / mathed / MathMacro.cpp
index 784c725de3b8c37d60191e438cf1597b8d67472f..d5e31f6cdd7448f8ac5229875d20137566f65ef6 100644 (file)
@@ -4,7 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Alejandro Aguilar Sierra
- * \author André Pönitz
+ * \author André Pönitz
  * \author Stefan Schimanski
  *
  * Full author contact details are available in file CREDITS.
 #include <config.h>
 
 #include "MathMacro.h"
-#include "MathSupport.h"
+
+#include "InsetMathChar.h"
+#include "MathCompletionList.h"
 #include "MathExtern.h"
 #include "MathStream.h"
+#include "MathSupport.h"
 
 #include "Buffer.h"
 #include "BufferView.h"
@@ -31,6 +34,8 @@
 #include "frontends/Painter.h"
 
 #include "support/debug.h"
+#include "support/lassert.h"
+#include "support/textutils.h"
 
 #include <ostream>
 #include <vector>
@@ -55,13 +60,12 @@ public:
        ///
        void metrics(MetricsInfo & mi, Dimension & dim) const {
                mathMacro_.macro()->unlock();
-               if (!mathMacro_.editMetrics(mi.base.bv) 
+               mathMacro_.cell(idx_).metrics(mi, dim);
+
+               if (!mathMacro_.editMetrics(mi.base.bv)
                    && mathMacro_.cell(idx_).empty())
                        def_.metrics(mi, dim);
-               else {
-                       CoordCache & coords = mi.base.bv->coordCache();
-                       dim = coords.arrays().dim(&mathMacro_.cell(idx_));
-               }
+
                mathMacro_.macro()->lock();
        }
        ///
@@ -131,8 +135,8 @@ docstring MathMacro::name() const
 {
        if (displayMode_ == DISPLAY_UNFOLDED)
                return asString(cell(0));
-       else
-               return name_;
+
+       return name_;
 }
 
 
@@ -192,7 +196,7 @@ void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
                   && editing_[mi.base.bv]) {
                // Macro will be edited in a old-style list mode here:
 
-               BOOST_ASSERT(macro_ != 0);
+               LASSERT(macro_ != 0, /**/);
                Dimension fontDim;
                FontInfo labelFont = sane_font;
                math_font_max_dim(labelFont, fontDim.asc, fontDim.des);
@@ -229,15 +233,20 @@ void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
                dim.wid += 2;
                metricsMarkers2(dim);
        } else {
-               BOOST_ASSERT(macro_ != 0);
+               LASSERT(macro_ != 0, /**/);
 
-               // metrics are computed here for the cells,
-               // in the proxy we will then use the dim from the cache
-               InsetMathNest::metrics(mi);
-               
-               // calculate metrics finally
+               // calculate metrics, hoping that all cells are seen
                macro_->lock();
                expanded_.cell(0).metrics(mi, dim);
+
+               // otherwise do a manual metrics call
+               CoordCache & coords = mi.base.bv->coordCache();
+               for (idx_type i = 0; i < nargs(); ++i) {
+                       if (!coords.getArrays().has(&cell(i))) {
+                               Dimension tdim;
+                               cell(i).metrics(mi, tdim);
+                       }
+               }
                macro_->unlock();
 
                // calculate dimension with label while editing
@@ -333,23 +342,19 @@ void MathMacro::draw(PainterInfo & pi, int x, int y) const
        int expy = y;
 
        if (displayMode_ == DISPLAY_INIT || displayMode_ == DISPLAY_INTERACTIVE_INIT) {         
-               PainterInfo pi2(pi.base.bv, pi.pain);
-               pi2.base.font.setColor(macro_ ? Color_latex : Color_error);
-               //pi2.base.style = LM_ST_TEXT;
-               pi2.pain.text(x, y, from_ascii("\\") + name(), pi2.base.font);
+               FontSetChanger dummy(pi.base, "lyxtex");
+               pi.pain.text(x, y, from_ascii("\\") + name(), pi.base.font);
        } else if (displayMode_ == DISPLAY_UNFOLDED) {
-               PainterInfo pi2(pi.base.bv, pi.pain);
-               pi2.base.font.setColor(macro_ ? Color_latex : Color_error);
-               //pi2.base.style = LM_ST_TEXT;
-               pi2.pain.text(x, y, from_ascii("\\"), pi2.base.font);
-               x += mathed_string_width(pi2.base.font, from_ascii("\\")) + 1;
-               cell(0).draw(pi2, x, y);
-               drawMarkers(pi2, expx, expy);
+               FontSetChanger dummy(pi.base, "lyxtex");
+               pi.pain.text(x, y, from_ascii("\\"), pi.base.font);
+               x += mathed_string_width(pi.base.font, from_ascii("\\")) + 1;
+               cell(0).draw(pi, x, y);
+               drawMarkers(pi, expx, expy);
        } else if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_LIST
                   && editing_[pi.base.bv]) {
                // Macro will be edited in a old-style list mode here:
                
-               CoordCache & coords = pi.base.bv->coordCache();
+               CoordCache const & coords = pi.base.bv->coordCache();
                FontInfo const & labelFont = sane_font;
                
                // markers and box needs two pixels
@@ -366,8 +371,7 @@ void MathMacro::draw(PainterInfo & pi, int x, int y) const
 
                // draw definition
                definition_.draw(pi, x, y);
-               Dimension defDim
-               = coords.arrays().dim(&definition_);
+               Dimension const & defDim = coords.getArrays().dim(&definition_);
                y += max(fontDim.des, defDim.des);
                                
                // draw parameters
@@ -377,8 +381,7 @@ void MathMacro::draw(PainterInfo & pi, int x, int y) const
                
                for (idx_type i = 0; i < nargs(); ++i) {
                        // position of label
-                       Dimension cdim
-                       = coords.arrays().dim(&cell(i));
+                       Dimension const & cdim = coords.getArrays().dim(&cell(i));
                        x = expx + 2;
                        y += max(fontDim.asc, cdim.asc) + 1;
                        
@@ -513,8 +516,14 @@ void MathMacro::validate(LaTeXFeatures & features) const
        if (!requires_.empty())
                features.require(requires_);
 
-       if (name() == "binom" || name() == "mathcircumflex")
-               features.require(to_utf8(name()));
+       if (name() == "binom")
+               features.require("binom");
+       
+       // validate the cells and the definition
+       if (displayMode() == DISPLAY_NORMAL) {
+               definition_.validate(features);
+               InsetMathNest::validate(features);
+       }
 }
 
 
@@ -538,7 +547,7 @@ Inset * MathMacro::editXY(Cursor & cur, int x, int y)
 
 void MathMacro::removeArgument(Inset::pos_type pos) {
        if (displayMode_ == DISPLAY_NORMAL) {
-               BOOST_ASSERT(size_t(pos) < cells_.size());
+               LASSERT(size_t(pos) < cells_.size(), /**/);
                cells_.erase(cells_.begin() + pos);
                if (size_t(pos) < attachedArgsNum_)
                        --attachedArgsNum_;
@@ -553,7 +562,7 @@ void MathMacro::removeArgument(Inset::pos_type pos) {
 
 void MathMacro::insertArgument(Inset::pos_type pos) {
        if (displayMode_ == DISPLAY_NORMAL) {
-               BOOST_ASSERT(size_t(pos) <= cells_.size());
+               LASSERT(size_t(pos) <= cells_.size(), /**/);
                cells_.insert(cells_.begin() + pos, MathData());
                if (size_t(pos) < attachedArgsNum_)
                        ++attachedArgsNum_;
@@ -567,7 +576,7 @@ void MathMacro::insertArgument(Inset::pos_type pos) {
 
 void MathMacro::detachArguments(vector<MathData> & args, bool strip)
 {
-       BOOST_ASSERT(displayMode_ == DISPLAY_NORMAL);   
+       LASSERT(displayMode_ == DISPLAY_NORMAL, /**/);  
        args = cells_;
 
        // strip off empty cells, but not more than arity-attachedArgsNum_
@@ -588,7 +597,7 @@ void MathMacro::detachArguments(vector<MathData> & args, bool strip)
 
 void MathMacro::attachArguments(vector<MathData> const & args, size_t arity, int optionals)
 {
-       BOOST_ASSERT(displayMode_ == DISPLAY_NORMAL);
+       LASSERT(displayMode_ == DISPLAY_NORMAL, /**/);
        cells_ = args;
        attachedArgsNum_ = args.size();
        cells_.resize(arity);
@@ -646,15 +655,18 @@ bool MathMacro::folded() const
 
 void MathMacro::write(WriteStream & os) const
 {
+       MathEnsurer ensurer(os, macro_ != 0, true);
+
        // non-normal mode
        if (displayMode_ != DISPLAY_NORMAL) {
-               os << "\\" << name() << " ";
-               os.pendingSpace(true);
+               os << "\\" << name();
+               if (name().size() != 1 || isAlphaASCII(name()[0]))
+                       os.pendingSpace(true);
                return;
        }
 
        // normal mode
-       BOOST_ASSERT(macro_);
+       LASSERT(macro_, /**/);
 
        // optional arguments make macros fragile
        if (optionals_ > 0 && os.fragile())
@@ -684,7 +696,8 @@ void MathMacro::write(WriteStream & os) const
        // Print remaining macros 
        for (; i < cells_.size(); ++i) {
                if (cell(i).size() == 1 
-                        && cell(i)[0].nucleus()->asCharInset()) {
+                       && cell(i)[0].nucleus()->asCharInset()
+                       && cell(i)[0].nucleus()->asCharInset()->getChar() < 0x80) {
                        if (first)
                                os << " ";
                        os << cell(i);
@@ -770,7 +783,7 @@ bool MathMacro::automaticPopupCompletion() const
 }
 
 
-Inset::CompletionList const * 
+CompletionList const * 
 MathMacro::createCompletionList(Cursor const & cur) const
 {
        if (displayMode() != DISPLAY_UNFOLDED)