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(CursorSlice const & sl, bool boundary, int & x,
54 // We may have 0 arguments, but InsetMathNest requires at least one.
56 InsetMathNest::cursorPos(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, "Unknown: " + name(), dim);
64 } else if (editing(mi.base.bv)) {
65 asArray(MacroTable::globalMacros().get(name()).def(), tmpl_);
66 LyXFont font = mi.base.font;
67 augmentFont(font, "lyxtex");
68 tmpl_.metrics(mi, dim);
69 dim.wid += mathed_string_width(font, name()) + 10;
70 int ww = mathed_string_width(font, "#1: ");
71 for (idx_type i = 0; i < nargs(); ++i) {
72 MathArray const & c = cell(i);
74 dim.wid = max(dim.wid, c.width() + ww);
75 dim.des += c.height() + 10;
78 MacroTable::globalMacros().get(name()).expand(cells_, expanded_);
79 expanded_.metrics(mi, dim);
86 void MathMacro::draw(PainterInfo & pi, int x, int y) const
88 if (!MacroTable::globalMacros().has(name())) {
89 drawStrRed(pi, x, y, "Unknown: " + name());
90 } else if (editing(pi.base.bv)) {
91 LyXFont font = pi.base.font;
92 augmentFont(font, "lyxtex");
93 int h = y - dim_.ascent() + 2 + tmpl_.ascent();
94 docstring dn(name().begin(), name().end());
95 pi.pain.text(x + 3, h, dn, font);
96 int const w = mathed_string_width(font, name());
97 tmpl_.draw(pi, x + w + 12, h);
100 mathed_string_dim(font, "#1: ", ldim);
101 for (idx_type i = 0; i < nargs(); ++i) {
102 MathArray const & c = cell(i);
103 h += max(c.ascent(), ldim.asc) + 5;
104 c.draw(pi, x + ldim.wid, h);
105 lyx::char_type str[] = { '#', '1', ':', '\0' };
106 str[1] += static_cast<lyx::char_type>(i);
107 pi.pain.text(x + 3, h, str, font);
108 h += max(c.descent(), ldim.des) + 5;
111 expanded_.draw(pi, x, y);
113 drawMarkers2(pi, x, y);
117 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
119 // We may have 0 arguments, but InsetMathNest requires at least one.
121 InsetMathNest::drawSelection(pi, x, y);
125 void MathMacro::validate(LaTeXFeatures & features) const
127 if (name() == "binom" || name() == "mathcircumflex")
128 features.require(name());
132 InsetBase * MathMacro::editXY(LCursor & cur, int x, int y)
134 // We may have 0 arguments, but InsetMathNest requires at least one.
136 // Prevent crash due to cold coordcache
137 // FIXME: This is only a workaround, the call of
138 // InsetMathNest::editXY is correct. The correct fix would
139 // ensure that the coordcache of the arguments is valid.
140 if (!editing(&cur.bv())) {
144 return InsetMathNest::editXY(cur, x, y);
150 void MathMacro::maple(MapleStream & os) const
153 ::maple(expanded_, os);
157 void MathMacro::mathmlize(MathMLStream & os) const
160 ::mathmlize(expanded_, os);
164 void MathMacro::octave(OctaveStream & os) const
167 ::octave(expanded_, os);
171 void MathMacro::updateExpansion() const
173 //expanded_.substitute(*this);
177 void MathMacro::infoize(std::ostream & os) const
179 os << "Macro: " << name();
183 void MathMacro::infoize2(std::ostream & os) const
185 os << "Macro: " << name();