2 * \file InsetMathMacro.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
9 * Full author contact details are available in file CREDITS.
14 #include "InsetMathMacro.h"
15 #include "MathSupport.h"
16 #include "MathExtern.h"
17 #include "MathStream.h"
22 #include "BufferView.h"
23 #include "LaTeXFeatures.h"
24 #include "frontends/Painter.h"
36 /// This class is the value of a macro argument, technically
37 /// a wrapper of the cells of MathMacro.
38 class MathMacroArgumentValue : public InsetMathDim {
41 MathMacroArgumentValue(MathArray const * value, docstring const & macroName)
42 : value_(value), macroName_(macroName) {}
44 bool metrics(MetricsInfo & mi, Dimension & dim) const;
46 void draw(PainterInfo &, int x, int y) const;
49 std::auto_ptr<InsetBase> doClone() const;
50 MathArray const * value_;
55 auto_ptr<InsetBase> MathMacroArgumentValue::doClone() const
57 return auto_ptr<InsetBase>(new MathMacroArgumentValue(*this));
61 bool MathMacroArgumentValue::metrics(MetricsInfo & mi, Dimension & dim) const
63 // unlock outer macro in arguments, and lock it again later
64 MacroTable::globalMacros().get(macroName_).unlock();
65 value_->metrics(mi, dim);
66 MacroTable::globalMacros().get(macroName_).lock();
75 void MathMacroArgumentValue::draw(PainterInfo & pi, int x, int y) const
77 // unlock outer macro in arguments, and lock it again later
78 MacroTable::globalMacros().get(macroName_).unlock();
79 value_->draw(pi, x, y);
80 MacroTable::globalMacros().get(macroName_).lock();
84 MathMacro::MathMacro(docstring const & name, int numargs)
85 : InsetMathNest(numargs), name_(name)
89 auto_ptr<InsetBase> MathMacro::doClone() const
91 return auto_ptr<InsetBase>(new MathMacro(*this));
95 docstring MathMacro::name() const
101 void MathMacro::cursorPos(BufferView const & bv,
102 CursorSlice const & sl, bool boundary, int & x, int & y) const
104 // We may have 0 arguments, but InsetMathNest requires at least one.
106 InsetMathNest::cursorPos(bv, sl, boundary, x, y);
110 bool MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
112 if (!MacroTable::globalMacros().has(name())) {
113 mathed_string_dim(mi.base.font, "Unknown: " + name(), dim);
115 MacroData const & macro = MacroTable::globalMacros().get(name());
116 if (macro.locked()) {
117 mathed_string_dim(mi.base.font, "Self reference: " + name(), dim);
118 expanded_ = MathArray();
119 } else if (editing(mi.base.bv)) {
121 asArray(macro.def(), tmpl_);
122 LyXFont font = mi.base.font;
123 augmentFont(font, from_ascii("lyxtex"));
124 tmpl_.metrics(mi, dim);
126 dim.wid += mathed_string_width(font, name()) + 10;
128 int ww = mathed_string_width(font, from_ascii("#1: "));
129 for (idx_type i = 0; i < nargs(); ++i) {
130 MathArray const & c = cell(i);
132 dim.wid = max(dim.wid, c.width() + ww);
133 dim.des += c.height() + 10;
136 // create MathMacroArgumentValue object pointing to the cells of the macro
137 MacroData const & macro = MacroTable::globalMacros().get(name());
138 vector<MathArray> values(nargs());
139 for (size_t i = 0; i != nargs(); ++i)
140 values[i].insert(0, MathAtom(new MathMacroArgumentValue(&cells_[i], name())));
141 macro.expand(values, expanded_);
143 MacroTable::globalMacros().get(name()).lock();
144 expanded_.metrics(mi, dim);
145 MacroTable::globalMacros().get(name()).unlock();
148 metricsMarkers2(dim);
156 void MathMacro::draw(PainterInfo & pi, int x, int y) const
158 if (!MacroTable::globalMacros().has(name())) {
160 drawStrRed(pi, x, y, "Unknown: " + name());
162 MacroData const & macro = MacroTable::globalMacros().get(name());
163 if (macro.locked()) {
165 drawStrRed(pi, x, y, "Self reference: " + name());
166 } else if (editing(pi.base.bv)) {
167 LyXFont font = pi.base.font;
168 augmentFont(font, from_ascii("lyxtex"));
169 int h = y - dim_.ascent() + 2 + tmpl_.ascent();
170 pi.pain.text(x + 3, h, name(), font);
171 int const w = mathed_string_width(font, name());
172 tmpl_.draw(pi, x + w + 12, h);
173 h += tmpl_.descent();
175 docstring t = from_ascii("#1: ");
176 mathed_string_dim(font, t, ldim);
177 for (idx_type i = 0; i < nargs(); ++i) {
178 MathArray const & c = cell(i);
179 h += max(c.ascent(), ldim.asc) + 5;
180 c.draw(pi, x + ldim.wid, h);
181 char_type str[] = { '#', '1', ':', '\0' };
182 str[1] += static_cast<char_type>(i);
183 pi.pain.text(x + 3, h, str, font);
184 h += max(c.descent(), ldim.des) + 5;
187 MacroTable::globalMacros().get(name()).lock();
188 expanded_.draw(pi, x, y);
189 MacroTable::globalMacros().get(name()).unlock();
192 drawMarkers2(pi, x, y);
196 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
198 // We may have 0 arguments, but InsetMathNest requires at least one.
200 InsetMathNest::drawSelection(pi, x, y);
204 void MathMacro::validate(LaTeXFeatures & features) const
206 if (name() == "binom" || name() == "mathcircumflex")
207 features.require(to_utf8(name()));
211 InsetBase * MathMacro::editXY(LCursor & cur, int x, int y)
213 // We may have 0 arguments, but InsetMathNest requires at least one.
215 // Prevent crash due to cold coordcache
216 // FIXME: This is only a workaround, the call of
217 // InsetMathNest::editXY is correct. The correct fix would
218 // ensure that the coordcache of the arguments is valid.
219 if (!editing(&cur.bv())) {
223 return InsetMathNest::editXY(cur, x, y);
229 bool MathMacro::idxFirst(LCursor & cur) const
231 cur.updateFlags(Update::Force);
232 return InsetMathNest::idxFirst(cur);
236 bool MathMacro::idxLast(LCursor & cur) const
238 cur.updateFlags(Update::Force);
239 return InsetMathNest::idxLast(cur);
243 bool MathMacro::notifyCursorLeaves(LCursor & cur)
245 cur.updateFlags(Update::Force);
246 return InsetMathNest::notifyCursorLeaves(cur);
250 void MathMacro::maple(MapleStream & os) const
253 lyx::maple(expanded_, os);
257 void MathMacro::mathmlize(MathStream & os) const
260 lyx::mathmlize(expanded_, os);
264 void MathMacro::octave(OctaveStream & os) const
267 lyx::octave(expanded_, os);
271 void MathMacro::updateExpansion() const
273 //expanded_.substitute(*this);
277 void MathMacro::infoize(odocstream & os) const
279 os << "Macro: " << name();
283 void MathMacro::infoize2(odocstream & os) const
285 os << "Macro: " << name();