]> git.lyx.org Git - lyx.git/blob - src/mathed/math_scriptinset.C
small bugfixes, new inset for comments and the main part is moving lfun
[lyx.git] / src / mathed / math_scriptinset.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include "math_scriptinset.h"
6 #include "math_support.h"
7 #include "math_symbolinset.h"
8 #include "math_mathmlstream.h"
9 #include "support/LAssert.h"
10 #include "debug.h"
11
12
13 using std::max;
14
15
16 MathScriptInset::MathScriptInset()
17         : MathNestInset(3), limits_(0)
18 {
19         script_[0] = false;
20         script_[1] = false;
21 }
22
23
24 MathScriptInset::MathScriptInset(bool up)
25         : MathNestInset(3), limits_(0)
26 {
27         script_[0] = !up;
28         script_[1] = up;
29 }
30
31
32 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
33         : MathNestInset(3), limits_(0)
34 {
35         script_[0] = !up;
36         script_[1] = up;
37         cell(2).push_back(at);
38 }
39
40
41
42 MathInset * MathScriptInset::clone() const
43 {
44         return new MathScriptInset(*this);
45 }
46
47
48 MathScriptInset const * MathScriptInset::asScriptInset() const
49 {
50         return this;
51 }
52
53
54 MathScriptInset * MathScriptInset::asScriptInset()
55 {
56         return this;
57 }
58
59
60 bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
61 {
62         idx = 2;
63         pos = 0;
64         return true;
65 }
66
67
68 bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
69 {
70         idx = 2;
71         pos = nuc().size();
72         return true;
73 }
74
75
76 MathArray const & MathScriptInset::down() const
77 {
78         return cell(0);
79 }
80
81
82 MathArray & MathScriptInset::down()
83 {
84         return cell(0);
85 }
86
87
88 MathArray const & MathScriptInset::up() const
89 {
90         return cell(1);
91 }
92
93
94 MathArray & MathScriptInset::up()
95 {
96         return cell(1);
97 }
98
99
100 void MathScriptInset::ensure(bool up)
101 {
102         script_[up] = true;
103 }
104
105
106 MathArray const & MathScriptInset::nuc() const
107 {
108         return cell(2);
109 }
110
111
112 MathArray & MathScriptInset::nuc()
113 {
114         return cell(2);
115 }
116
117
118 int MathScriptInset::dy0() const
119 {
120         int nd = ndes();
121         if (!hasDown())
122                 return nd;
123         int des = down().ascent();
124         if (hasLimits())
125                 des += nd + 2;
126         else
127                 des = max(des, nd);
128         return des;
129 }
130
131
132 int MathScriptInset::dy1() const
133 {
134         int na = nasc();
135         if (!hasUp())
136                 return na;
137         int asc = up().descent();
138         if (hasLimits())
139                 asc += na + 2;
140         else
141                 asc = max(asc, na);
142         asc = max(asc, 5);
143         return asc;
144 }
145
146
147 int MathScriptInset::dx0() const
148 {
149         lyx::Assert(hasDown());
150         return hasLimits() ? (dim_.w - down().width()) / 2 : nwid();
151 }
152
153
154 int MathScriptInset::dx1() const
155 {
156         lyx::Assert(hasUp());
157         return hasLimits() ? (dim_.w - up().width()) / 2 : nwid();
158 }
159
160
161 int MathScriptInset::dxx() const
162 {
163         return hasLimits() ? (dim_.w - nwid()) / 2  :  0;
164 }
165
166
167 int MathScriptInset::nwid() const
168 {
169         return nuc().size() ? nuc().width() : 2;
170 }
171
172
173 int MathScriptInset::nasc() const
174 {
175         return nuc().size() ? nuc().ascent() : 5;
176 }
177
178
179 int MathScriptInset::ndes() const
180 {
181         return nuc().size() ? nuc().descent() : 0;
182 }
183
184
185 void MathScriptInset::metrics(MathMetricsInfo & mi) const
186 {
187         cell(2).metrics(mi);
188         MathScriptChanger dummy(mi.base);
189         cell(0).metrics(mi);
190         cell(1).metrics(mi);
191         dim_.w = 0;
192         if (hasLimits()) {
193                 dim_.w = nwid();
194                 if (hasUp())
195                         dim_.w = max(dim_.w, up().width());
196                 if (hasDown())
197                         dim_.w = max(dim_.w, down().width());
198         } else {
199                 if (hasUp())
200                         dim_.w = max(dim_.w, up().width());
201                 if (hasDown())
202                         dim_.w = max(dim_.w, down().width());
203                 dim_.w += nwid();
204         }
205         dim_.a = dy1() + (hasUp() ? up().ascent() : 0);
206         dim_.d = dy0() + (hasDown() ? down().descent() : 0);
207         metricsMarkers2();
208 }
209
210
211 void MathScriptInset::draw(MathPainterInfo & pi, int x, int y) const
212 {
213         if (nuc().size())
214                 nuc().draw(pi, x + dxx(), y);
215         else if (editing())
216                 drawStr(pi, pi.base.font, x + dxx(), y, ".");
217         MathScriptChanger dummy(pi.base);
218         if (hasUp())
219                 up().draw(pi, x + dx1(), y - dy1());
220         if (hasDown())
221                 down().draw(pi, x + dx0(), y + dy0());
222         drawMarkers2(pi, x, y);
223 }
224
225
226 void MathScriptInset::metricsT(TextMetricsInfo const & mi) const
227 {
228         if (hasUp())
229                 up().metricsT(mi);
230         if (hasDown())
231                 down().metricsT(mi);
232         nuc().metricsT(mi);
233 }
234
235
236 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
237 {
238         if (nuc().size())
239                 nuc().drawT(pain, x + dxx(), y);
240         if (hasUp())
241                 up().drawT(pain, x + dx1(), y - dy1());
242         if (hasDown())
243                 down().drawT(pain, x + dx0(), y + dy0());
244 }
245
246
247
248 bool MathScriptInset::hasLimits() const
249 {
250         // obvious cases
251         if (limits_ == 1)
252                 return true;
253         if (limits_ == -1)
254                 return false;
255
256         // we can only display limits if the nucleus wants some
257         if (!nuc().size())
258                 return false;
259         if (!nuc().back()->isScriptable())
260                 return false;
261
262         // per default \int has limits beside the \int even in displayed formulas
263         if (nuc().back()->asSymbolInset())
264                 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
265                         return false;
266
267         // assume "real" limits for everything else
268         return true;
269 }
270
271
272 void MathScriptInset::removeEmptyScripts()
273 {
274         for (int i = 0; i <= 1; ++i)
275                 if (script_[i] && cell(i).size() == 0) {
276                         cell(i).clear();
277                         script_[i] = false;
278                 }
279 }
280
281
282 void MathScriptInset::removeScript(bool up)
283 {
284         cell(up).clear();
285         script_[up] = false;
286 }
287
288
289 bool MathScriptInset::has(bool up) const
290 {
291         return script_[up];
292 }
293
294
295 bool MathScriptInset::hasUp() const
296 {
297         return script_[1];
298 }
299
300
301 bool MathScriptInset::hasDown() const
302 {
303         return script_[0];
304 }
305
306
307 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
308 {
309         return false;
310 }
311
312
313 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
314 {
315         return false;
316 }
317
318
319 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
320         int) const
321 {
322         if (idx == 1) {
323                 // if we are 'up' we can't go further up
324                 if (up)
325                         return false;
326                 // otherwise go to last base position
327                 idx = 2;
328                 pos = cell(2).size();   
329         }
330
331         else if (idx == 0) {
332                 // if we are 'down' we can't go further down
333                 if (!up)
334                         return false;
335                 idx = 2;
336                 pos = cell(2).size();   
337         }
338         
339         else {
340                 // in nucleus
341                 // don't go up/down unless in last position
342                 if (pos != cell(2).size())
343                         return false;   
344                 // don't go up/down if there is no cell.
345                 if (!has(up))
346                         return false;
347                 // otherwise move into the first position
348                 idx = up;
349                 pos = 0;
350         }
351         return true;
352 }
353
354
355 void MathScriptInset::write(WriteStream & os) const
356 {
357         if (nuc().size()) {
358                 os << nuc();
359                 if (nuc().back()->takesLimits()) {
360                         if (limits_ == -1)
361                                 os << "\\nolimits ";
362                         if (limits_ == 1)
363                                 os << "\\limits ";
364                 }
365         } else {
366                 if (os.firstitem())
367                         lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
368                 else
369                         os << "{}";
370         }
371
372         if (hasDown() && down().size())
373                 os << "_{" << down() << '}';
374
375         if (hasUp() && up().size())
376                 os << "^{" << up() << '}';
377
378         if (lock_ && !os.latex())
379                 os << "\\lyxlock ";
380 }
381
382
383 void MathScriptInset::normalize(NormalStream & os) const
384 {
385         bool d = hasDown() && down().size();
386         bool u = hasUp() && up().size();
387
388         if (u && d)
389                 os << "[subsup ";
390         else if (u)
391                 os << "[sup ";
392         else if (d)
393                 os << "[sub ";
394
395         if (nuc().size())
396                 os << nuc() << ' ';
397         else
398                 os << "[par]";
399
400         if (u && d)
401                 os << down() << ' ' << up() << ']';
402         else if (d)
403                 os << down() << ']';
404         else if (u)
405                 os << up() << ']';
406 }
407
408
409 void MathScriptInset::maplize(MapleStream & os) const
410 {
411         if (nuc().size())
412                 os << nuc();
413         if (hasDown() && down().size())
414                 os << '[' << down() << ']';
415         if (hasUp() && up().size())
416                 os << "^(" << up() << ')';
417 }
418
419
420 void MathScriptInset::mathematicize(MathematicaStream & os) const
421 {
422         bool d = hasDown() && down().size();
423         bool u = hasUp() && up().size();
424
425         if (nuc().size()) {
426                 if (d) 
427                         os << "Subscript[" << nuc();
428                 else
429                         os << nuc();
430         }
431
432         if (u)
433                 os << "^(" << up() << ")";
434
435         if (nuc().size())
436                 if (d)
437                         os << "," << down() << "]"; 
438 }
439
440
441 void MathScriptInset::mathmlize( MathMLStream & os) const
442 {
443         bool d = hasDown() && down().size();
444         bool u = hasUp() && up().size();
445
446         if (u && d)
447                 os << MTag("msubsup");
448         else if (u)
449                 os << MTag("msup");
450         else if (d)
451                 os << MTag("msub");
452
453         if (nuc().size())
454                 os << nuc();
455         else
456                 os << "<mrow/>";
457
458         if (u && d)
459                 os << down() << up() << ETag("msubsup");
460         else if (u)
461                 os << up() << ETag("msup");
462         else if (d)
463                 os << down() << ETag("msub");
464 }
465
466
467 void MathScriptInset::octavize(OctaveStream & os) const
468 {
469         if (nuc().size())
470                 os << nuc();
471         if (hasDown() && down().size())
472                 os << '[' << down() << ']';
473         if (hasUp() && up().size())
474                 os << "^(" << up() << ')';
475 }
476
477
478 void MathScriptInset::infoize(std::ostream & os) const
479 {
480         os << "Scripts";
481         if (limits_)
482                 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
483 }