]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathMacro.C
Account for kerning when positioning scripts in the MathFontOld and
[lyx.git] / src / mathed / InsetMathMacro.C
1 /**
2  * \file InsetMathMacro.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetMathMacro.h"
15 #include "MathSupport.h"
16 #include "MathExtern.h"
17 #include "MathStream.h"
18
19 #include "buffer.h"
20 #include "cursor.h"
21 #include "debug.h"
22 #include "BufferView.h"
23 #include "LaTeXFeatures.h"
24 #include "frontends/Painter.h"
25
26
27 namespace lyx {
28
29 using std::string;
30 using std::max;
31 using std::auto_ptr;
32 using std::endl;
33 using std::vector;
34
35
36 MathMacro::MathMacro(docstring const & name, int numargs)
37         : InsetMathNest(numargs), name_(name)
38 {}
39
40
41 auto_ptr<InsetBase> MathMacro::doClone() const
42 {
43         return auto_ptr<InsetBase>(new MathMacro(*this));
44 }
45
46
47 docstring MathMacro::name() const
48 {
49         return name_;
50 }
51
52
53 void MathMacro::cursorPos(BufferView const & bv,
54                 CursorSlice const & sl, bool boundary, int & x, int & y) const
55 {
56         // We may have 0 arguments, but InsetMathNest requires at least one.
57         if (nargs() > 0)
58                 InsetMathNest::cursorPos(bv, sl, boundary, x, y);
59 }
60
61
62 bool MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
63 {
64         if (!MacroTable::globalMacros().has(name())) {
65                 mathed_string_dim(mi.base.font, "Unknown: " + name(), dim);
66         } else if (editing(mi.base.bv)) {
67                 // FIXME UNICODE
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);
72                 // FIXME UNICODE
73                 dim.wid += mathed_string_width(font, name()) + 10;
74                 // FIXME UNICODE
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);
78                         c.metrics(mi);
79                         dim.wid  = max(dim.wid, c.width() + ww);
80                         dim.des += c.height() + 10;
81                 }
82         } else {
83                 MacroTable::globalMacros().get(name()).expand(cells_, expanded_);
84                 expanded_.metrics(mi, dim);
85         }
86         metricsMarkers2(dim);
87         if (dim_ == dim)
88                 return false;
89         dim_ = dim;
90         return true;
91 }
92
93
94 void MathMacro::draw(PainterInfo & pi, int x, int y) const
95 {
96         if (!MacroTable::globalMacros().has(name())) {
97                 // FIXME UNICODE
98                 drawStrRed(pi, x, y, "Unknown: " + name());
99         } else if (editing(pi.base.bv)) {
100                 LyXFont font = pi.base.font;
101                 augmentFont(font, from_ascii("lyxtex"));
102                 int h = y - dim_.ascent() + 2 + tmpl_.ascent();
103                 pi.pain.text(x + 3, h, name(), font);
104                 int const w = mathed_string_width(font, name());
105                 tmpl_.draw(pi, x + w + 12, h);
106                 h += tmpl_.descent();
107                 Dimension ldim;
108                 string t = "#1: ";
109                 mathed_string_dim(font, name(), ldim);
110                 for (idx_type i = 0; i < nargs(); ++i) {
111                         MathArray const & c = cell(i);
112                         h += max(c.ascent(), ldim.asc) + 5;
113                         c.draw(pi, x + ldim.wid, h);
114                         char_type str[] = { '#', '1', ':', '\0' };
115                         str[1] += static_cast<char_type>(i);
116                         pi.pain.text(x + 3, h, str, font);
117                         h += max(c.descent(), ldim.des) + 5;
118                 }
119         } else {
120                 expanded_.draw(pi, x, y);
121         }
122         drawMarkers2(pi, x, y);
123 }
124
125
126 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
127 {
128         // We may have 0 arguments, but InsetMathNest requires at least one.
129         if (nargs() > 0)
130                 InsetMathNest::drawSelection(pi, x, y);
131 }
132
133
134 void MathMacro::validate(LaTeXFeatures & features) const
135 {
136         if (name() == "binom" || name() == "mathcircumflex")
137                 features.require(to_utf8(name()));
138 }
139
140
141 InsetBase * MathMacro::editXY(LCursor & cur, int x, int y)
142 {
143         // We may have 0 arguments, but InsetMathNest requires at least one.
144         if (nargs() > 0) {
145                 // Prevent crash due to cold coordcache
146                 // FIXME: This is only a workaround, the call of
147                 // InsetMathNest::editXY is correct. The correct fix would
148                 // ensure that the coordcache of the arguments is valid.
149                 if (!editing(&cur.bv())) {
150                         edit(cur, true);
151                         return this;
152                 }
153                 return InsetMathNest::editXY(cur, x, y);
154         }
155         return this;
156 }
157
158
159 void MathMacro::maple(MapleStream & os) const
160 {
161         updateExpansion();
162         lyx::maple(expanded_, os);
163 }
164
165
166 void MathMacro::mathmlize(MathStream & os) const
167 {
168         updateExpansion();
169         lyx::mathmlize(expanded_, os);
170 }
171
172
173 void MathMacro::octave(OctaveStream & os) const
174 {
175         updateExpansion();
176         lyx::octave(expanded_, os);
177 }
178
179
180 void MathMacro::updateExpansion() const
181 {
182         //expanded_.substitute(*this);
183 }
184
185
186 void MathMacro::infoize(odocstream & os) const
187 {
188         os << "Macro: " << name();
189 }
190
191
192 void MathMacro::infoize2(odocstream & os) const
193 {
194         os << "Macro: " << name();
195
196 }
197
198
199 } // namespace lyx