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 "MathMacro.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"
33 /// This class is the value of a macro argument, technically
34 /// a wrapper of the cells of MathMacro.
35 class MathMacroArgumentValue : public InsetMath {
38 MathMacroArgumentValue(MathMacro const & mathMacro, size_t idx)
39 : mathMacro_(mathMacro), idx_(idx) {}
41 void metrics(MetricsInfo & mi, Dimension & dim) const;
43 void draw(PainterInfo &, int x, int y) const;
45 int kerning() const { return mathMacro_.cell(idx_).kerning(); }
48 Inset * clone() const;
49 MathMacro const & mathMacro_;
54 Inset * MathMacroArgumentValue::clone() const
56 return new MathMacroArgumentValue(*this);
60 void MathMacroArgumentValue::metrics(MetricsInfo & mi, Dimension & dim) const
62 // unlock outer macro in arguments, and lock it again later
63 MacroData const & macro = MacroTable::globalMacros().get(mathMacro_.name());
65 mathMacro_.cell(idx_).metrics(mi, dim);
70 void MathMacroArgumentValue::draw(PainterInfo & pi, int x, int y) const
72 // unlock outer macro in arguments, and lock it again later
73 MacroData const & macro = MacroTable::globalMacros().get(mathMacro_.name());
75 mathMacro_.cell(idx_).draw(pi, x, y);
80 MathMacro::MathMacro(docstring const & name, int numargs)
81 : InsetMathNest(numargs), name_(name), editing_(false)
85 Inset * MathMacro::clone() const
87 MathMacro * x = new MathMacro(*this);
88 x->expanded_ = MathData();
89 x->macroBackup_ = MacroData();
94 docstring MathMacro::name() const
100 void MathMacro::cursorPos(BufferView const & bv,
101 CursorSlice const & sl, bool boundary, int & x, int & y) const
103 // We may have 0 arguments, but InsetMathNest requires at least one.
105 InsetMathNest::cursorPos(bv, sl, boundary, x, y);
109 void 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());
117 if (macroBackup_ != macro)
120 if (macro.locked()) {
121 mathed_string_dim(mi.base.font, "Self reference: " + name(), dim);
122 } else if (editing(mi.base.bv)) {
123 Font font = mi.base.font;
124 augmentFont(font, from_ascii("lyxtex"));
125 tmpl_.metrics(mi, dim);
127 dim.wid += mathed_string_width(font, name()) + 10;
129 int ww = mathed_string_width(font, from_ascii("#1: "));
130 for (idx_type i = 0; i < nargs(); ++i) {
131 MathData const & c = cell(i);
133 dim.wid = max(dim.wid, c.width() + ww);
134 dim.des += c.height() + 10;
139 expanded_.metrics(mi, dim);
141 kerning_ = expanded_.kerning();
145 // Cache the inset dimension.
146 setDimCache(mi, dim);
150 void MathMacro::draw(PainterInfo & pi, int x, int y) const
152 if (!MacroTable::globalMacros().has(name())) {
154 drawStrRed(pi, x, y, "Unknown: " + name());
156 MacroData const & macro = MacroTable::globalMacros().get(name());
159 for (size_t i = 0; i < nargs(); ++i)
160 cell(i).setXY(*pi.base.bv, x, y);
162 if (macro.locked()) {
164 drawStrRed(pi, x, y, "Self reference: " + name());
165 } else if (editing_) {
166 Font font = pi.base.font;
167 augmentFont(font, from_ascii("lyxtex"));
168 Dimension const dim = dimension(*pi.base.bv);
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 MathData 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;
188 expanded_.draw(pi, x, y);
192 // edit mode changed?
193 if (editing_ != editing(pi.base.bv) || macroBackup_ != macro)
194 pi.base.bv->cursor().updateFlags(Update::Force);
199 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
201 // We may have 0 arguments, but InsetMathNest requires at least one.
203 InsetMathNest::drawSelection(pi, x, y);
207 void MathMacro::validate(LaTeXFeatures & features) const
209 string const require = MacroTable::globalMacros().get(name()).requires();
210 if (!require.empty())
211 features.require(require);
213 if (name() == "binom" || name() == "mathcircumflex")
214 features.require(to_utf8(name()));
218 Inset * MathMacro::editXY(Cursor & cur, int x, int y)
220 // We may have 0 arguments, but InsetMathNest requires at least one.
222 // Prevent crash due to cold coordcache
223 // FIXME: This is only a workaround, the call of
224 // InsetMathNest::editXY is correct. The correct fix would
225 // ensure that the coordcache of the arguments is valid.
226 if (!editing(&cur.bv())) {
230 return InsetMathNest::editXY(cur, x, y);
236 bool MathMacro::idxFirst(Cursor & cur) const
238 cur.updateFlags(Update::Force);
239 return InsetMathNest::idxFirst(cur);
243 bool MathMacro::idxLast(Cursor & cur) const
245 cur.updateFlags(Update::Force);
246 return InsetMathNest::idxLast(cur);
250 bool MathMacro::idxUpDown(Cursor & cur, bool up) const
257 if (cur.idx() + 1 >= nargs())
261 cur.pos() = cell(cur.idx()).x2pos(cur.x_target());
266 bool MathMacro::notifyCursorLeaves(Cursor & cur)
268 cur.updateFlags(Update::Force);
269 return InsetMathNest::notifyCursorLeaves(cur);
273 void MathMacro::maple(MapleStream & os) const
276 lyx::maple(expanded_, os);
280 void MathMacro::mathmlize(MathStream & os) const
283 lyx::mathmlize(expanded_, os);
287 void MathMacro::octave(OctaveStream & os) const
290 lyx::octave(expanded_, os);
294 void MathMacro::updateExpansion() const
296 MacroData const & macro = MacroTable::globalMacros().get(name());
298 // create MathMacroArgumentValue object pointing to the cells of the macro
299 std::vector<MathData> values(nargs());
300 for (size_t i = 0; i != nargs(); ++i)
301 values[i].insert(0, MathAtom(new MathMacroArgumentValue(*this, i)));
302 macro.expand(values, expanded_);
303 asArray(macro.def(), tmpl_);
304 macroBackup_ = macro;
308 void MathMacro::infoize(odocstream & os) const
310 os << "Macro: " << name();
314 void MathMacro::infoize2(odocstream & os) const
316 os << "Macro: " << name();