]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
several small fixes for mathed
[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_macrotable.h"
18 #include "math_macrotemplate.h"
19 #include "math_mathmlstream.h"
20 #include "cursor.h"
21 #include "debug.h"
22 #include "LaTeXFeatures.h"
23
24
25 using std::string;
26 using std::max;
27 using std::auto_ptr;
28 using std::endl;
29
30
31 MathMacro::MathMacro(string const & name)
32         : MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
33                 tmplate_(MathMacroTable::provide(name))
34 {}
35
36
37 MathMacro::MathMacro(MathMacro const & m)
38         : MathNestInset(m),
39                 tmplate_(m.tmplate_) // don't copy 'expanded_'!
40 {}
41
42
43
44 auto_ptr<InsetBase> MathMacro::clone() const
45 {
46         return auto_ptr<InsetBase>(new MathMacro(*this));
47 }
48
49
50 string MathMacro::name() const
51 {
52         return tmplate_->asMacroTemplate()->name();
53 }
54
55
56 bool MathMacro::defining() const
57 {
58         return false;
59         //return mathcursor->formula()->getInsetName() == name();
60 }
61
62
63 void MathMacro::expand() const
64 {
65         expanded_ = tmplate_->cell(tmplate_->cell(1).empty() ? 0 : 1);
66 }
67
68
69 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
70 {
71         augmentFont(font_, "lyxtex");
72         mi_ = mi;
73
74         if (defining()) {
75
76                 mathed_string_dim(font_, name(), dim);
77
78         } else if (editing(mi.base.bv)) {
79
80                 expand();
81                 expanded_.metrics(mi, dim);
82                 metricsMarkers(dim);
83
84                 dim.wid += mathed_string_width(font_, name()) + 10;
85
86                 int ww = mathed_string_width(font_, "#1: ");
87
88                 for (idx_type i = 0; i < nargs(); ++i) {
89                         MathArray const & c = cell(i);
90                         c.metrics(mi);
91                         dim.wid  = max(dim.wid, c.width() + ww);
92                         dim.des += c.height() + 10;
93                 }
94
95         } else {
96
97                 expand();
98                 expanded_.substitute(*this);
99                 expanded_.metrics(mi, dim);
100
101         }
102
103         dim_ = dim;
104 }
105
106
107 void MathMacro::draw(PainterInfo & pi, int x, int y) const
108 {
109         metrics(mi_, dim_);
110
111         LyXFont texfont;
112         augmentFont(texfont, "lyxtex");
113
114         if (defining()) {
115                 drawStr(pi, texfont, x, y, name());
116                 return;
117         }
118
119         if (editing(pi.base.bv)) {
120                 int h = y - dim_.ascent() + 2 + expanded_.ascent();
121                 drawStr(pi, font_, x + 3, h, name());
122
123                 int const w = mathed_string_width(font_, name());
124                 expanded_.draw(pi, x + w + 12, h);
125                 h += expanded_.descent();
126
127                 Dimension ldim;
128                 mathed_string_dim(font_, "#1: ", ldim);
129
130                 for (idx_type i = 0; i < nargs(); ++i) {
131                         MathArray const & c = cell(i);
132                         h += max(c.ascent(), ldim.asc) + 5;
133                         c.draw(pi, x + ldim.wid, h);
134                         char str[] = "#1:";
135                         str[1] += static_cast<char>(i);
136                         drawStr(pi, texfont, x + 3, h, str);
137                         h += max(c.descent(), ldim.des) + 5;
138                 }
139                 return;
140         }
141
142         expanded_.draw(pi, x, y);
143 }
144
145
146 void MathMacro::dump() const
147 {
148         MathMacroTable::dump();
149         lyxerr << "\n macro: '" << this << "'\n"
150                << " name: '" << name() << "'\n"
151                << " template: '";
152         WriteStream wi(lyxerr);
153         tmplate_->write(wi);
154         lyxerr << "'" << endl;
155 }
156
157
158 bool MathMacro::idxUpDown(LCursor & cur, bool up) const
159 {
160         if (up) {
161                 if (!MathNestInset::idxLeft(cur))
162                         return false;
163         } else {
164                 if (!MathNestInset::idxRight(cur))
165                         return false;
166         }
167         cur.pos() = cur.cell().x2pos(cur.x_target());
168         return true;
169 }
170
171
172 bool MathMacro::idxLeft(LCursor &) const
173 {
174         return false;
175 }
176
177
178 bool MathMacro::idxRight(LCursor &) const
179 {
180         return false;
181 }
182
183
184 void MathMacro::validate(LaTeXFeatures & features) const
185 {
186         if (name() == "binom" || name() == "mathcircumflex")
187                 features.require(name());
188         //MathInset::validate(features);
189 }
190
191
192 void MathMacro::maple(MapleStream & os) const
193 {
194         updateExpansion();
195         ::maple(expanded_, os);
196 }
197
198
199 void MathMacro::mathmlize(MathMLStream & os) const
200 {
201         updateExpansion();
202         ::mathmlize(expanded_, os);
203 }
204
205
206 void MathMacro::octave(OctaveStream & os) const
207 {
208         updateExpansion();
209         ::octave(expanded_, os);
210 }
211
212
213 void MathMacro::updateExpansion() const
214 {
215         expand();
216         expanded_.substitute(*this);
217 }
218
219
220 void MathMacro::infoize(std::ostream & os) const
221 {
222         os << "Macro: " << name();
223 }
224
225
226 void MathMacro::infoize2(std::ostream & os) const
227 {
228         os << "Macro: " << name();
229 }