2 * \file InsetMathMacro.C
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 "MathMLStream.h"
22 #include "BufferView.h"
23 #include "LaTeXFeatures.h"
24 #include "frontends/Painter.h"
34 MathMacro::MathMacro(string const & name, int numargs)
35 : InsetMathNest(numargs), name_(name)
39 auto_ptr<InsetBase> MathMacro::doClone() const
41 return auto_ptr<InsetBase>(new MathMacro(*this));
45 string MathMacro::name() const
51 void MathMacro::cursorPos(BufferView const & bv,
52 CursorSlice const & sl, bool boundary, int & x, int & y) const
54 // We may have 0 arguments, but InsetMathNest requires at least one.
56 InsetMathNest::cursorPos(bv, sl, boundary, x, y);
60 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
62 if (!MacroTable::globalMacros().has(name())) {
63 mathed_string_dim(mi.base.font, lyx::from_utf8("Unknown: " + name()), dim);
64 } else if (editing(mi.base.bv)) {
66 asArray(lyx::from_utf8(MacroTable::globalMacros().get(name()).def()), tmpl_);
67 LyXFont font = mi.base.font;
68 augmentFont(font, "lyxtex");
69 tmpl_.metrics(mi, dim);
71 dim.wid += mathed_string_width(font, lyx::from_utf8(name())) + 10;
73 int ww = mathed_string_width(font, lyx::from_ascii("#1: "));
74 for (idx_type i = 0; i < nargs(); ++i) {
75 MathArray const & c = cell(i);
77 dim.wid = max(dim.wid, c.width() + ww);
78 dim.des += c.height() + 10;
81 MacroTable::globalMacros().get(name()).expand(cells_, expanded_);
82 expanded_.metrics(mi, dim);
89 void MathMacro::draw(PainterInfo & pi, int x, int y) const
91 if (!MacroTable::globalMacros().has(name())) {
93 drawStrRed(pi, x, y, lyx::from_utf8("Unknown: " + name()));
94 } else if (editing(pi.base.bv)) {
95 LyXFont font = pi.base.font;
96 augmentFont(font, "lyxtex");
97 int h = y - dim_.ascent() + 2 + tmpl_.ascent();
99 docstring dn = lyx::from_utf8(name());
100 pi.pain.text(x + 3, h, dn, font);
101 int const w = mathed_string_width(font, dn);
102 tmpl_.draw(pi, x + w + 12, h);
103 h += tmpl_.descent();
105 mathed_string_dim(font, lyx::from_ascii("#1: "), ldim);
106 for (idx_type i = 0; i < nargs(); ++i) {
107 MathArray const & c = cell(i);
108 h += max(c.ascent(), ldim.asc) + 5;
109 c.draw(pi, x + ldim.wid, h);
110 lyx::char_type str[] = { '#', '1', ':', '\0' };
111 str[1] += static_cast<lyx::char_type>(i);
112 pi.pain.text(x + 3, h, str, font);
113 h += max(c.descent(), ldim.des) + 5;
116 expanded_.draw(pi, x, y);
118 drawMarkers2(pi, x, y);
122 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
124 // We may have 0 arguments, but InsetMathNest requires at least one.
126 InsetMathNest::drawSelection(pi, x, y);
130 void MathMacro::validate(LaTeXFeatures & features) const
132 if (name() == "binom" || name() == "mathcircumflex")
133 features.require(name());
137 InsetBase * MathMacro::editXY(LCursor & cur, int x, int y)
139 // We may have 0 arguments, but InsetMathNest requires at least one.
141 // Prevent crash due to cold coordcache
142 // FIXME: This is only a workaround, the call of
143 // InsetMathNest::editXY is correct. The correct fix would
144 // ensure that the coordcache of the arguments is valid.
145 if (!editing(&cur.bv())) {
149 return InsetMathNest::editXY(cur, x, y);
155 void MathMacro::maple(MapleStream & os) const
158 ::maple(expanded_, os);
162 void MathMacro::mathmlize(MathMLStream & os) const
165 ::mathmlize(expanded_, os);
169 void MathMacro::octave(OctaveStream & os) const
172 ::octave(expanded_, os);
176 void MathMacro::updateExpansion() const
178 //expanded_.substitute(*this);
182 void MathMacro::infoize(std::ostream & os) const
184 os << "Macro: " << name();
188 void MathMacro::infoize2(std::ostream & os) const
190 os << "Macro: " << name();