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;
49 std::auto_ptr<Inset> doClone() const;
50 MathMacro const & mathMacro_;
55 auto_ptr<Inset> MathMacroArgumentValue::doClone() const
57 return auto_ptr<Inset>(new MathMacroArgumentValue(*this));
61 bool MathMacroArgumentValue::metrics(MetricsInfo & mi, Dimension & dim) const
63 // unlock outer macro in arguments, and lock it again later
64 MacroData const & macro = MacroTable::globalMacros().get(mathMacro_.name());
66 mathMacro_.cell(idx_).metrics(mi, dim);
76 void MathMacroArgumentValue::draw(PainterInfo & pi, int x, int y) const
78 // unlock outer macro in arguments, and lock it again later
79 MacroData const & macro = MacroTable::globalMacros().get(mathMacro_.name());
81 mathMacro_.cell(idx_).draw(pi, x, y);
86 MathMacro::MathMacro(docstring const & name, int numargs)
87 : InsetMathNest(numargs), name_(name), editing_(false)
91 auto_ptr<Inset> MathMacro::doClone() const
93 MathMacro * x = new MathMacro(*this);
94 x->expanded_ = MathData();
95 x->macroBackup_ = MacroData();
96 return auto_ptr<Inset>(x);
100 docstring MathMacro::name() const
106 void MathMacro::cursorPos(BufferView const & bv,
107 CursorSlice const & sl, bool boundary, int & x, int & y) const
109 // We may have 0 arguments, but InsetMathNest requires at least one.
111 InsetMathNest::cursorPos(bv, sl, boundary, x, y);
115 bool MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
117 if (!MacroTable::globalMacros().has(name())) {
118 mathed_string_dim(mi.base.font, "Unknown: " + name(), dim);
120 MacroData const & macro = MacroTable::globalMacros().get(name());
122 if (macroBackup_ != macro)
125 if (macro.locked()) {
126 mathed_string_dim(mi.base.font, "Self reference: " + name(), dim);
127 } else if (editing(mi.base.bv)) {
128 Font font = mi.base.font;
129 augmentFont(font, from_ascii("lyxtex"));
130 tmpl_.metrics(mi, dim);
132 dim.wid += mathed_string_width(font, name()) + 10;
134 int ww = mathed_string_width(font, from_ascii("#1: "));
135 for (idx_type i = 0; i < nargs(); ++i) {
136 MathData const & c = cell(i);
138 dim.wid = max(dim.wid, c.width() + ww);
139 dim.des += c.height() + 10;
144 expanded_.metrics(mi, dim);
149 metricsMarkers2(dim);
157 void MathMacro::draw(PainterInfo & pi, int x, int y) const
159 if (!MacroTable::globalMacros().has(name())) {
161 drawStrRed(pi, x, y, "Unknown: " + name());
163 MacroData const & macro = MacroTable::globalMacros().get(name());
166 for (size_t i = 0; i < nargs(); ++i)
167 cell(i).setXY(*pi.base.bv, x, y);
169 if (macro.locked()) {
171 drawStrRed(pi, x, y, "Self reference: " + name());
172 } else if (editing_) {
173 Font font = pi.base.font;
174 augmentFont(font, from_ascii("lyxtex"));
175 int h = y - dim_.ascent() + 2 + tmpl_.ascent();
176 pi.pain.text(x + 3, h, name(), font);
177 int const w = mathed_string_width(font, name());
178 tmpl_.draw(pi, x + w + 12, h);
179 h += tmpl_.descent();
181 docstring t = from_ascii("#1: ");
182 mathed_string_dim(font, t, ldim);
183 for (idx_type i = 0; i < nargs(); ++i) {
184 MathData const & c = cell(i);
185 h += max(c.ascent(), ldim.asc) + 5;
186 c.draw(pi, x + ldim.wid, h);
187 char_type str[] = { '#', '1', ':', '\0' };
188 str[1] += static_cast<char_type>(i);
189 pi.pain.text(x + 3, h, str, font);
190 h += max(c.descent(), ldim.des) + 5;
194 expanded_.draw(pi, x, y);
198 // edit mode changed?
199 if (editing_ != editing(pi.base.bv) || macroBackup_ != macro)
200 pi.base.bv->cursor().updateFlags(Update::Force);
202 drawMarkers2(pi, x, y);
206 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
208 // We may have 0 arguments, but InsetMathNest requires at least one.
210 InsetMathNest::drawSelection(pi, x, y);
214 void MathMacro::validate(LaTeXFeatures & features) const
216 string const require = MacroTable::globalMacros().get(name()).requires();
217 if (!require.empty())
218 features.require(require);
220 if (name() == "binom" || name() == "mathcircumflex")
221 features.require(to_utf8(name()));
225 Inset * MathMacro::editXY(Cursor & cur, int x, int y)
227 // We may have 0 arguments, but InsetMathNest requires at least one.
229 // Prevent crash due to cold coordcache
230 // FIXME: This is only a workaround, the call of
231 // InsetMathNest::editXY is correct. The correct fix would
232 // ensure that the coordcache of the arguments is valid.
233 if (!editing(&cur.bv())) {
237 return InsetMathNest::editXY(cur, x, y);
243 bool MathMacro::idxFirst(Cursor & cur) const
245 cur.updateFlags(Update::Force);
246 return InsetMathNest::idxFirst(cur);
250 bool MathMacro::idxLast(Cursor & cur) const
252 cur.updateFlags(Update::Force);
253 return InsetMathNest::idxLast(cur);
257 bool MathMacro::notifyCursorLeaves(Cursor & cur)
259 cur.updateFlags(Update::Force);
260 return InsetMathNest::notifyCursorLeaves(cur);
264 void MathMacro::maple(MapleStream & os) const
267 lyx::maple(expanded_, os);
271 void MathMacro::mathmlize(MathStream & os) const
274 lyx::mathmlize(expanded_, os);
278 void MathMacro::octave(OctaveStream & os) const
281 lyx::octave(expanded_, os);
285 void MathMacro::updateExpansion() const
287 MacroData const & macro = MacroTable::globalMacros().get(name());
289 // create MathMacroArgumentValue object pointing to the cells of the macro
290 vector<MathData> values(nargs());
291 for (size_t i = 0; i != nargs(); ++i)
292 values[i].insert(0, MathAtom(new MathMacroArgumentValue(*this, i)));
293 macro.expand(values, expanded_);
294 asArray(macro.def(), tmpl_);
295 macroBackup_ = macro;
299 void MathMacro::infoize(odocstream & os) const
301 os << "Macro: " << name();
305 void MathMacro::infoize2(odocstream & os) const
307 os << "Macro: " << name();