]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
Fix to bug 2362: Deleting superscript also deletes subscript.
[lyx.git] / src / mathed / math_macro.C
1 /**
2  * \file math_macro.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 "math_macro.h"
15 #include "math_support.h"
16 #include "math_extern.h"
17 #include "math_mathmlstream.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 using std::string;
27 using std::max;
28 using std::auto_ptr;
29 using std::endl;
30
31
32 MathMacro::MathMacro(string const & name, int numargs)
33         : MathNestInset(numargs), name_(name)
34 {}
35
36
37 auto_ptr<InsetBase> MathMacro::doClone() const
38 {
39         return auto_ptr<InsetBase>(new MathMacro(*this));
40 }
41
42
43 string MathMacro::name() const
44 {
45         return name_;
46 }
47
48
49 void MathMacro::cursorPos(CursorSlice const & sl, bool boundary, int & x,
50                 int & y) const
51 {
52         // We may have 0 arguments, but MathNestInset requires at least one.
53         if (nargs() > 0)
54                 MathNestInset::cursorPos(sl, boundary, x, y);
55 }
56
57
58 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
59 {
60         if (!MacroTable::globalMacros().has(name())) {
61                 mathed_string_dim(mi.base.font, "Unknown: " + name(), dim);
62         } else if (editing(mi.base.bv)) {
63                 asArray(MacroTable::globalMacros().get(name()).def(), tmpl_);
64                 LyXFont font = mi.base.font;
65                 augmentFont(font, "lyxtex");
66                 tmpl_.metrics(mi, dim);
67                 dim.wid += mathed_string_width(font, name()) + 10;
68                 int ww = mathed_string_width(font, "#1: ");
69                 for (idx_type i = 0; i < nargs(); ++i) {
70                         MathArray const & c = cell(i);
71                         c.metrics(mi);
72                         dim.wid  = max(dim.wid, c.width() + ww);
73                         dim.des += c.height() + 10;
74                 }
75         } else {
76                 MacroTable::globalMacros().get(name()).expand(cells_, expanded_);
77                 expanded_.metrics(mi, dim);
78         }
79         metricsMarkers2(dim);
80         dim_ = dim;
81 }
82
83
84 void MathMacro::draw(PainterInfo & pi, int x, int y) const
85 {
86         if (!MacroTable::globalMacros().has(name())) {
87                 drawStrRed(pi, x, y, "Unknown: " + name());
88         } else if (editing(pi.base.bv)) {
89                 LyXFont font = pi.base.font;
90                 augmentFont(font, "lyxtex");
91                 int h = y - dim_.ascent() + 2 + tmpl_.ascent();
92                 pi.pain.text(x + 3, h, name(), font);
93                 int const w = mathed_string_width(font, name());
94                 tmpl_.draw(pi, x + w + 12, h);
95                 h += tmpl_.descent();
96                 Dimension ldim;
97                 mathed_string_dim(font, "#1: ", ldim);
98                 for (idx_type i = 0; i < nargs(); ++i) {
99                         MathArray const & c = cell(i);
100                         h += max(c.ascent(), ldim.asc) + 5;
101                         c.draw(pi, x + ldim.wid, h);
102                         char str[] = "#1:";
103                         str[1] += static_cast<char>(i);
104                         pi.pain.text(x + 3, h, str, font);
105                         h += max(c.descent(), ldim.des) + 5;
106                 }
107         } else {
108                 expanded_.draw(pi, x, y);
109         }
110         drawMarkers2(pi, x, y);
111 }
112
113
114 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
115 {
116         // We may have 0 arguments, but MathNestInset requires at least one.
117         if (nargs() > 0)
118                 MathNestInset::drawSelection(pi, x, y);
119 }
120
121
122 void MathMacro::validate(LaTeXFeatures & features) const
123 {
124         if (name() == "binom" || name() == "mathcircumflex")
125                 features.require(name());
126 }
127
128
129 InsetBase * MathMacro::editXY(LCursor & cur, int x, int y)
130 {
131         // We may have 0 arguments, but MathNestInset requires at least one.
132         if (nargs() > 0) {
133                 // Prevent crash due to cold coordcache
134                 // FIXME: This is only a workaround, the call of
135                 // MathNestInset::editXY is correct. The correct fix would
136                 // ensure that the coordcache of the arguments is valid.
137                 if (!editing(&cur.bv())) {
138                         edit(cur, true);
139                         return this;
140                 }
141                 return MathNestInset::editXY(cur, x, y);
142         }
143         return this;
144 }
145
146
147 void MathMacro::maple(MapleStream & os) const
148 {
149         updateExpansion();
150         ::maple(expanded_, os);
151 }
152
153
154 void MathMacro::mathmlize(MathMLStream & os) const
155 {
156         updateExpansion();
157         ::mathmlize(expanded_, os);
158 }
159
160
161 void MathMacro::octave(OctaveStream & os) const
162 {
163         updateExpansion();
164         ::octave(expanded_, os);
165 }
166
167
168 void MathMacro::updateExpansion() const
169 {
170         //expanded_.substitute(*this);
171 }
172
173
174 void MathMacro::infoize(std::ostream & os) const
175 {
176         os << "Macro: " << name();
177 }
178
179
180 void MathMacro::infoize2(std::ostream & os) const
181 {
182         os << "Macro: " << name();
183
184 }