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 "MathStream.h"
22 #include "BufferView.h"
23 #include "LaTeXFeatures.h"
24 #include "frontends/Painter.h"
36 MathMacro::MathMacro(docstring 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 docstring 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 mathed_string_dim(mi.base.font, "Unknown: " + name(), dim);
66 } else if (editing(mi.base.bv)) {
68 asArray(MacroTable::globalMacros().get(name()).def(), tmpl_);
69 LyXFont font = mi.base.font;
70 augmentFont(font, from_ascii("lyxtex"));
71 tmpl_.metrics(mi, dim);
73 dim.wid += mathed_string_width(font, name()) + 10;
75 int ww = mathed_string_width(font, from_ascii("#1: "));
76 for (idx_type i = 0; i < nargs(); ++i) {
77 MathArray const & c = cell(i);
79 dim.wid = max(dim.wid, c.width() + ww);
80 dim.des += c.height() + 10;
83 MacroTable::globalMacros().get(name()).expand(cells_, expanded_);
84 expanded_.metrics(mi, dim);
91 void MathMacro::draw(PainterInfo & pi, int x, int y) const
93 if (!MacroTable::globalMacros().has(name())) {
95 drawStrRed(pi, x, y, "Unknown: " + name());
96 } else if (editing(pi.base.bv)) {
97 LyXFont font = pi.base.font;
98 augmentFont(font, from_ascii("lyxtex"));
99 int h = y - dim_.ascent() + 2 + tmpl_.ascent();
100 pi.pain.text(x + 3, h, name(), font);
101 int const w = mathed_string_width(font, name());
102 tmpl_.draw(pi, x + w + 12, h);
103 h += tmpl_.descent();
106 mathed_string_dim(font, name(), ldim);
107 for (idx_type i = 0; i < nargs(); ++i) {
108 MathArray const & c = cell(i);
109 h += max(c.ascent(), ldim.asc) + 5;
110 c.draw(pi, x + ldim.wid, h);
111 char_type str[] = { '#', '1', ':', '\0' };
112 str[1] += static_cast<char_type>(i);
113 pi.pain.text(x + 3, h, str, font);
114 h += max(c.descent(), ldim.des) + 5;
117 expanded_.draw(pi, x, y);
119 drawMarkers2(pi, x, y);
123 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
125 // We may have 0 arguments, but InsetMathNest requires at least one.
127 InsetMathNest::drawSelection(pi, x, y);
131 void MathMacro::validate(LaTeXFeatures & features) const
133 if (name() == "binom" || name() == "mathcircumflex")
134 features.require(to_utf8(name()));
138 InsetBase * MathMacro::editXY(LCursor & cur, int x, int y)
140 // We may have 0 arguments, but InsetMathNest requires at least one.
142 // Prevent crash due to cold coordcache
143 // FIXME: This is only a workaround, the call of
144 // InsetMathNest::editXY is correct. The correct fix would
145 // ensure that the coordcache of the arguments is valid.
146 if (!editing(&cur.bv())) {
150 return InsetMathNest::editXY(cur, x, y);
156 void MathMacro::maple(MapleStream & os) const
159 lyx::maple(expanded_, os);
163 void MathMacro::mathmlize(MathStream & os) const
166 lyx::mathmlize(expanded_, os);
170 void MathMacro::octave(OctaveStream & os) const
173 lyx::octave(expanded_, os);
177 void MathMacro::updateExpansion() const
179 //expanded_.substitute(*this);
183 void MathMacro::infoize(odocstream & os) const
185 os << "Macro: " << name();
189 void MathMacro::infoize2(odocstream & os) const
191 os << "Macro: " << name();