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"
36 MathMacro::MathMacro(string const & name, int numargs)
37 : InsetMathNest(numargs), name_(name)
41 auto_ptr<InsetBase> MathMacro::doClone() const
43 return auto_ptr<InsetBase>(new MathMacro(*this));
47 string MathMacro::name() const
53 void MathMacro::cursorPos(BufferView const & bv,
54 CursorSlice const & sl, bool boundary, int & x, int & y) const
56 // We may have 0 arguments, but InsetMathNest requires at least one.
58 InsetMathNest::cursorPos(bv, sl, boundary, x, y);
62 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
64 if (!MacroTable::globalMacros().has(name())) {
65 string t = "Unknown: " + name();
66 vector<char_type> n(t.begin(), t.end());
67 mathed_string_dim(mi.base.font, n, dim);
68 } else if (editing(mi.base.bv)) {
70 asArray(from_utf8(MacroTable::globalMacros().get(name()).def()), tmpl_);
71 LyXFont font = mi.base.font;
72 augmentFont(font, "lyxtex");
73 tmpl_.metrics(mi, dim);
75 dim.wid += mathed_string_width(font, from_utf8(name())) + 10;
77 int ww = mathed_string_width(font, from_ascii("#1: "));
78 for (idx_type i = 0; i < nargs(); ++i) {
79 MathArray const & c = cell(i);
81 dim.wid = max(dim.wid, c.width() + ww);
82 dim.des += c.height() + 10;
85 MacroTable::globalMacros().get(name()).expand(cells_, expanded_);
86 expanded_.metrics(mi, dim);
93 void MathMacro::draw(PainterInfo & pi, int x, int y) const
95 if (!MacroTable::globalMacros().has(name())) {
97 drawStrRed(pi, x, y, from_utf8("Unknown: " + name()));
98 } else if (editing(pi.base.bv)) {
99 LyXFont font = pi.base.font;
100 augmentFont(font, "lyxtex");
101 int h = y - dim_.ascent() + 2 + tmpl_.ascent();
103 docstring dn = from_utf8(name());
104 pi.pain.text(x + 3, h, dn, font);
105 int const w = mathed_string_width(font, dn);
106 tmpl_.draw(pi, x + w + 12, h);
107 h += tmpl_.descent();
110 vector<char_type> n(t.begin(), t.end());
111 mathed_string_dim(font, n, ldim);
112 for (idx_type i = 0; i < nargs(); ++i) {
113 MathArray const & c = cell(i);
114 h += max(c.ascent(), ldim.asc) + 5;
115 c.draw(pi, x + ldim.wid, h);
116 char_type str[] = { '#', '1', ':', '\0' };
117 str[1] += static_cast<char_type>(i);
118 pi.pain.text(x + 3, h, str, font);
119 h += max(c.descent(), ldim.des) + 5;
122 expanded_.draw(pi, x, y);
124 drawMarkers2(pi, x, y);
128 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
130 // We may have 0 arguments, but InsetMathNest requires at least one.
132 InsetMathNest::drawSelection(pi, x, y);
136 void MathMacro::validate(LaTeXFeatures & features) const
138 if (name() == "binom" || name() == "mathcircumflex")
139 features.require(name());
143 InsetBase * MathMacro::editXY(LCursor & cur, int x, int y)
145 // We may have 0 arguments, but InsetMathNest requires at least one.
147 // Prevent crash due to cold coordcache
148 // FIXME: This is only a workaround, the call of
149 // InsetMathNest::editXY is correct. The correct fix would
150 // ensure that the coordcache of the arguments is valid.
151 if (!editing(&cur.bv())) {
155 return InsetMathNest::editXY(cur, x, y);
161 void MathMacro::maple(MapleStream & os) const
164 lyx::maple(expanded_, os);
168 void MathMacro::mathmlize(MathMLStream & os) const
171 lyx::mathmlize(expanded_, os);
175 void MathMacro::octave(OctaveStream & os) const
178 lyx::octave(expanded_, os);
182 void MathMacro::updateExpansion() const
184 //expanded_.substitute(*this);
188 void MathMacro::infoize(std::ostream & os) const
190 os << "Macro: " << name();
194 void MathMacro::infoize2(std::ostream & os) const
196 os << "Macro: " << name();