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"
36 /// This class is the value of a macro argument, technically
37 /// a wrapper of the cells of MathMacro.
38 class MathMacroArgumentValue : public InsetMath {
41 MathMacroArgumentValue(MathMacro const & mathMacro, size_t idx)
42 : mathMacro_(mathMacro), idx_(idx) {}
44 bool metrics(MetricsInfo & mi, Dimension & dim) const;
46 void draw(PainterInfo &, int x, int y) const;
48 int kerning() const { return mathMacro_.cell(idx_).kerning(); }
51 std::auto_ptr<Inset> doClone() const;
52 MathMacro const & mathMacro_;
57 auto_ptr<Inset> MathMacroArgumentValue::doClone() const
59 return auto_ptr<Inset>(new MathMacroArgumentValue(*this));
63 bool MathMacroArgumentValue::metrics(MetricsInfo & mi, Dimension & dim) const
65 // unlock outer macro in arguments, and lock it again later
66 MacroData const & macro = MacroTable::globalMacros().get(mathMacro_.name());
68 mathMacro_.cell(idx_).metrics(mi, dim);
77 void MathMacroArgumentValue::draw(PainterInfo & pi, int x, int y) const
79 // unlock outer macro in arguments, and lock it again later
80 MacroData const & macro = MacroTable::globalMacros().get(mathMacro_.name());
82 mathMacro_.cell(idx_).draw(pi, x, y);
87 MathMacro::MathMacro(docstring const & name, int numargs)
88 : InsetMathNest(numargs), name_(name), editing_(false)
92 auto_ptr<Inset> MathMacro::doClone() const
94 MathMacro * x = new MathMacro(*this);
95 x->expanded_ = MathData();
96 x->macroBackup_ = MacroData();
97 return auto_ptr<Inset>(x);
101 docstring MathMacro::name() const
107 void MathMacro::cursorPos(BufferView const & bv,
108 CursorSlice const & sl, bool boundary, int & x, int & y) const
110 // We may have 0 arguments, but InsetMathNest requires at least one.
112 InsetMathNest::cursorPos(bv, sl, boundary, x, y);
116 bool MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
119 if (!MacroTable::globalMacros().has(name())) {
120 mathed_string_dim(mi.base.font, "Unknown: " + name(), dim);
122 MacroData const & macro = MacroTable::globalMacros().get(name());
124 if (macroBackup_ != macro)
127 if (macro.locked()) {
128 mathed_string_dim(mi.base.font, "Self reference: " + name(), dim);
129 } else if (editing(mi.base.bv)) {
130 Font font = mi.base.font;
131 augmentFont(font, from_ascii("lyxtex"));
132 tmpl_.metrics(mi, dim);
134 dim.wid += mathed_string_width(font, name()) + 10;
136 int ww = mathed_string_width(font, from_ascii("#1: "));
137 for (idx_type i = 0; i < nargs(); ++i) {
138 MathData const & c = cell(i);
140 dim.wid = max(dim.wid, c.width() + ww);
141 dim.des += c.height() + 10;
146 expanded_.metrics(mi, dim);
148 kerning_ = expanded_.kerning();
159 void MathMacro::draw(PainterInfo & pi, int x, int y) const
161 if (!MacroTable::globalMacros().has(name())) {
163 drawStrRed(pi, x, y, "Unknown: " + name());
165 MacroData const & macro = MacroTable::globalMacros().get(name());
168 for (size_t i = 0; i < nargs(); ++i)
169 cell(i).setXY(*pi.base.bv, x, y);
171 if (macro.locked()) {
173 drawStrRed(pi, x, y, "Self reference: " + name());
174 } else if (editing_) {
175 Font font = pi.base.font;
176 augmentFont(font, from_ascii("lyxtex"));
177 int h = y - dim_.ascent() + 2 + tmpl_.ascent();
178 pi.pain.text(x + 3, h, name(), font);
179 int const w = mathed_string_width(font, name());
180 tmpl_.draw(pi, x + w + 12, h);
181 h += tmpl_.descent();
183 docstring t = from_ascii("#1: ");
184 mathed_string_dim(font, t, ldim);
185 for (idx_type i = 0; i < nargs(); ++i) {
186 MathData const & c = cell(i);
187 h += max(c.ascent(), ldim.asc) + 5;
188 c.draw(pi, x + ldim.wid, h);
189 char_type str[] = { '#', '1', ':', '\0' };
190 str[1] += static_cast<char_type>(i);
191 pi.pain.text(x + 3, h, str, font);
192 h += max(c.descent(), ldim.des) + 5;
196 expanded_.draw(pi, x, y);
200 // edit mode changed?
201 if (editing_ != editing(pi.base.bv) || macroBackup_ != macro)
202 pi.base.bv->cursor().updateFlags(Update::Force);
207 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
209 // We may have 0 arguments, but InsetMathNest requires at least one.
211 InsetMathNest::drawSelection(pi, x, y);
215 void MathMacro::validate(LaTeXFeatures & features) const
217 string const require = MacroTable::globalMacros().get(name()).requires();
218 if (!require.empty())
219 features.require(require);
221 if (name() == "binom" || name() == "mathcircumflex")
222 features.require(to_utf8(name()));
226 Inset * MathMacro::editXY(Cursor & cur, int x, int y)
228 // We may have 0 arguments, but InsetMathNest requires at least one.
230 // Prevent crash due to cold coordcache
231 // FIXME: This is only a workaround, the call of
232 // InsetMathNest::editXY is correct. The correct fix would
233 // ensure that the coordcache of the arguments is valid.
234 if (!editing(&cur.bv())) {
238 return InsetMathNest::editXY(cur, x, y);
244 bool MathMacro::idxFirst(Cursor & cur) const
246 cur.updateFlags(Update::Force);
247 return InsetMathNest::idxFirst(cur);
251 bool MathMacro::idxLast(Cursor & cur) const
253 cur.updateFlags(Update::Force);
254 return InsetMathNest::idxLast(cur);
258 bool MathMacro::idxUpDown(Cursor & cur, bool up) const
265 if (cur.idx() + 1 >= nargs())
269 cur.pos() = cell(cur.idx()).x2pos(cur.x_target());
274 bool MathMacro::notifyCursorLeaves(Cursor & cur)
276 cur.updateFlags(Update::Force);
277 return InsetMathNest::notifyCursorLeaves(cur);
281 void MathMacro::maple(MapleStream & os) const
284 lyx::maple(expanded_, os);
288 void MathMacro::mathmlize(MathStream & os) const
291 lyx::mathmlize(expanded_, os);
295 void MathMacro::octave(OctaveStream & os) const
298 lyx::octave(expanded_, os);
302 void MathMacro::updateExpansion() const
304 MacroData const & macro = MacroTable::globalMacros().get(name());
306 // create MathMacroArgumentValue object pointing to the cells of the macro
307 vector<MathData> values(nargs());
308 for (size_t i = 0; i != nargs(); ++i)
309 values[i].insert(0, MathAtom(new MathMacroArgumentValue(*this, i)));
310 macro.expand(values, expanded_);
311 asArray(macro.def(), tmpl_);
312 macroBackup_ = macro;
316 void MathMacro::infoize(odocstream & os) const
318 os << "Macro: " << name();
322 void MathMacro::infoize2(odocstream & os) const
324 os << "Macro: " << name();