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 "math_macro.h"
15 #include "math_support.h"
16 #include "math_extern.h"
17 #include "math_mathmlstream.h"
22 #include "BufferView.h"
23 #include "LaTeXFeatures.h"
24 #include "frontends/Painter.h"
32 MathMacro::MathMacro(string const & name, int numargs)
33 : MathNestInset(numargs), name_(name)
37 auto_ptr<InsetBase> MathMacro::doClone() const
39 return auto_ptr<InsetBase>(new MathMacro(*this));
43 string MathMacro::name() const
49 void MathMacro::cursorPos(CursorSlice const & sl, bool boundary, int & x,
52 // We may have 0 arguments, but MathNestInset requires at least one.
54 MathNestInset::cursorPos(sl, boundary, x, y);
58 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
60 if (!MacroTable::globalMacros().has(name())) {
61 mathed_string_dim(mi.base.font, "Unknown: " + name(), dim);
62 } else if (editing(mi.base.bv)) {
63 asArray(MacroTable::globalMacros().get(name()).def(), tmpl_);
64 LyXFont font = mi.base.font;
65 augmentFont(font, "lyxtex");
66 tmpl_.metrics(mi, dim);
67 dim.wid += mathed_string_width(font, name()) + 10;
68 int ww = mathed_string_width(font, "#1: ");
69 for (idx_type i = 0; i < nargs(); ++i) {
70 MathArray const & c = cell(i);
72 dim.wid = max(dim.wid, c.width() + ww);
73 dim.des += c.height() + 10;
76 MacroTable::globalMacros().get(name()).expand(cells_, expanded_);
77 expanded_.metrics(mi, dim);
84 void MathMacro::draw(PainterInfo & pi, int x, int y) const
86 if (!MacroTable::globalMacros().has(name())) {
87 drawStrRed(pi, x, y, "Unknown: " + name());
88 } else if (editing(pi.base.bv)) {
89 LyXFont font = pi.base.font;
90 augmentFont(font, "lyxtex");
91 int h = y - dim_.ascent() + 2 + tmpl_.ascent();
92 pi.pain.text(x + 3, h, name(), font);
93 int const w = mathed_string_width(font, name());
94 tmpl_.draw(pi, x + w + 12, h);
97 mathed_string_dim(font, "#1: ", ldim);
98 for (idx_type i = 0; i < nargs(); ++i) {
99 MathArray const & c = cell(i);
100 h += max(c.ascent(), ldim.asc) + 5;
101 c.draw(pi, x + ldim.wid, h);
103 str[1] += static_cast<char>(i);
104 pi.pain.text(x + 3, h, str, font);
105 h += max(c.descent(), ldim.des) + 5;
108 expanded_.draw(pi, x, y);
110 drawMarkers2(pi, x, y);
114 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
116 // We may have 0 arguments, but MathNestInset requires at least one.
118 MathNestInset::drawSelection(pi, x, y);
122 void MathMacro::validate(LaTeXFeatures & features) const
124 if (name() == "binom" || name() == "mathcircumflex")
125 features.require(name());
129 InsetBase * MathMacro::editXY(LCursor & cur, int x, int y)
131 // We may have 0 arguments, but MathNestInset requires at least one.
133 // Prevent crash due to cold coordcache
134 // FIXME: This is only a workaround, the call of
135 // MathNestInset::editXY is correct. The correct fix would
136 // ensure that the coordcache of the arguments is valid.
137 if (!editing(&cur.bv())) {
141 return MathNestInset::editXY(cur, x, y);
147 void MathMacro::maple(MapleStream & os) const
150 ::maple(expanded_, os);
154 void MathMacro::mathmlize(MathMLStream & os) const
157 ::mathmlize(expanded_, os);
161 void MathMacro::octave(OctaveStream & os) const
164 ::octave(expanded_, os);
168 void MathMacro::updateExpansion() const
170 //expanded_.substitute(*this);
174 void MathMacro::infoize(std::ostream & os) const
176 os << "Macro: " << name();
180 void MathMacro::infoize2(std::ostream & os) const
182 os << "Macro: " << name();