]> git.lyx.org Git - lyx.git/blob - src/mathed/math_hullinset.C
11833f0ed6fd55ab2f757fcdc896babdad1df373
[lyx.git] / src / mathed / math_hullinset.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_hullinset.h"
8 #include "math_mathmlstream.h"
9 #include "math_streamstr.h"
10 #include "math_support.h"
11 #include "debug.h"
12 #include "frontends/Painter.h"
13 #include "textpainter.h"
14 #include "Lsstream.h"
15 #include "LaTeXFeatures.h"
16 #include "support/LAssert.h"
17
18 #include <vector>
19
20 using std::vector;
21 using std::max;
22 using std::endl;
23
24 namespace {
25
26         int getCols(string const & type)
27         {
28                 if (type == "eqnarray")
29                         return 3;
30                 if (type == "align")
31                         return 2;
32                 if (type == "alignat")
33                         return 2;
34                 if (type == "xalignat")
35                         return 2;
36                 if (type == "xxalignat")
37                         return 2;
38                 return 1;
39         }
40
41
42         // returns position of first relation operator in the array
43         // used for "intelligent splitting"
44         MathArray::size_type firstRelOp(MathArray const & ar)
45         {
46                 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
47                         if ((*it)->isRelOp())
48                                 return it - ar.begin();
49                 return ar.size();
50         }
51
52
53         char const * star(bool numbered)
54         {
55                 return numbered ? "" : "*";
56         }
57
58
59         int typecode(string const & s)
60         {
61                 if (s == "none")      return 0;
62                 if (s == "simple")    return 1;
63                 if (s == "equation")  return 2;
64                 if (s == "eqnarray")  return 3;
65                 if (s == "align")     return 4;
66                 if (s == "alignat")   return 5;
67                 if (s == "xalignat")  return 6;
68                 if (s == "xxalignat") return 7;
69                 if (s == "multline")  return 8;
70                 if (s == "gather")    return 9;
71                 lyxerr << "unknown hull type '" << s << "'\n";
72                 return 0;
73         }
74
75         bool smaller(string const & s, string const & t)
76         {
77                 return typecode(s) < typecode(t);
78         }
79
80
81 } // end anon namespace
82
83
84 MathHullInset::MathHullInset()
85         : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
86 {
87         setDefaults();
88 }
89
90
91 MathHullInset::MathHullInset(string const & type)
92         : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
93 {
94         setDefaults();
95 }
96
97
98 MathHullInset::MathHullInset(string const & type, MathGridInset const & grid)
99         : MathGridInset(grid), type_(type), nonum_(1), label_(1)
100 {
101         setDefaults();
102 }
103
104
105 MathHullInset::MathHullInset(string const & type, col_type cols)
106         : MathGridInset(cols, 1), type_(type), nonum_(1), label_(1)
107 {
108         setDefaults();
109 }
110
111
112 MathInset * MathHullInset::clone() const
113 {
114         return new MathHullInset(*this);
115 }
116
117
118 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
119 {
120         idx = 0;
121         pos = 0;
122         return true;
123 }
124
125
126 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
127 {
128         idx = nargs() - 1;
129         pos = cell(idx).size();
130         return true;
131 }
132
133
134 char MathHullInset::defaultColAlign(col_type col)
135 {
136         if (type_ == "eqnarray")
137                 return "rcl"[col];
138         if (typecode(type_) >= typecode("align"))
139                 return "rl"[col & 1];
140         return 'c';
141 }
142
143
144 int MathHullInset::defaultColSpace(col_type col)
145 {
146         if (type_ == "align" || type_ == "alignat")
147                 return 0;
148         if (type_ == "xalignat")
149                 return (col & 1) ? 20 : 0;
150         if (type_ == "xxalignat")
151                 return (col & 1) ? 40 : 0;
152         return 0;
153 }
154
155
156 char const * MathHullInset::standardFont() const
157 {
158         if (type_ == "none")
159                 return "lyxnochange";
160         return "mathnormal";
161 }
162
163
164 void MathHullInset::metrics(MathMetricsInfo & mi) const
165 {
166         MathFontSetChanger dummy(mi.base, standardFont());
167
168         // let the cells adjust themselves
169         MathGridInset::metrics(mi);
170
171         if (display()) {
172                 ascent_  += 12;
173                 descent_ += 12;
174         }
175
176         if (numberedType()) {
177                 MathFontSetChanger dummy(mi.base, "mathbf");
178                 int l = 0;
179                 for (row_type row = 0; row < nrows(); ++row)
180                         l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
181
182                 if (l)
183                         width_ += 30 + l;
184         }
185
186         // make it at least as high as the current font
187         int asc = 0;
188         int des = 0;
189         math_font_max_dim(mi.base.font, asc, des);
190         ascent_  = max(ascent_,  asc);
191         descent_ = max(descent_, des);
192
193         // for markers
194         width_   += 2;
195         descent_ += 1;
196         ascent_  += 1;
197 }
198
199
200 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
201 {
202         MathFontSetChanger dummy(pi.base, standardFont());
203         MathGridInset::draw(pi, x + 1, y);
204
205         if (numberedType()) {
206                 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
207                 for (row_type row = 0; row < nrows(); ++row) {
208                         int const yy = y + rowinfo_[row].offset_;
209                         MathFontSetChanger dummy(pi.base, "mathrm");
210                         drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
211                 }
212         }
213
214         drawMarkers2(pi, x, y);
215 }
216
217
218 void MathHullInset::metricsT(TextMetricsInfo const &) const
219 {
220 #if 0
221         if (display()) {
222                 MathGridInset::metricsT(mi);
223         } else
224 #endif
225         {
226                 ostringstream os;
227                 WriteStream wi(os, false, true);
228                 write(wi);
229                 width_   = os.str().size();
230                 ascent_  = 1;
231                 descent_ = 0;
232         }
233 }
234
235
236 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
237 {
238 #if 0
239         if (display()) {
240                 MathGridInset::drawT(pain, x, y);
241         } else
242 #endif
243         {
244                 ostringstream os;
245                 WriteStream wi(os, false, true);
246                 write(wi);
247                 pain.draw(x, y, os.str().c_str());
248         }
249 }
250
251
252 string MathHullInset::label(row_type row) const
253 {
254         row_type n = nrows();
255         lyx::Assert(row < n);
256         return label_[row];
257 }
258
259
260 void MathHullInset::label(row_type row, string const & label)
261 {
262         lyxerr << "setting label '" << label << "' for row " << row << endl;
263         label_[row] = label;
264 }
265
266
267 void MathHullInset::numbered(row_type row, bool num)
268 {
269         nonum_[row] = !num;
270 }
271
272
273 bool MathHullInset::numbered(row_type row) const
274 {
275         return !nonum_[row];
276 }
277
278
279 bool MathHullInset::ams() const
280 {
281         return
282                 type_ == "align" ||
283                 type_ == "multline" ||
284                 type_ == "gather" ||
285                 type_ == "alignat" ||
286                 type_ == "xalignat" ||
287                 type_ == "xxalignat";
288 }
289
290
291 bool MathHullInset::display() const
292 {
293         return type_ != "simple" && type_ != "none";
294 }
295
296
297 vector<string> MathHullInset::getLabelList() const
298 {
299         vector<string> res;
300         for (row_type row = 0; row < nrows(); ++row)
301                 if (!label_[row].empty() && nonum_[row] != 1)
302                         res.push_back(label_[row]);
303         return res;
304 }
305
306
307 bool MathHullInset::numberedType() const
308 {
309         if (type_ == "none")
310                 return false;
311         if (type_ == "simple")
312                 return false;
313         if (type_ == "xxalignat")
314                 return false;
315         for (row_type row = 0; row < nrows(); ++row)
316                 if (!nonum_[row])
317                         return true;
318         return false;
319 }
320
321
322 void MathHullInset::validate(LaTeXFeatures & features) const
323 {
324         if (ams())
325                 features.require("amsmath");
326
327
328         // Validation is necessary only if not using AMS math.
329         // To be safe, we will always run mathedvalidate.
330         //if (features.amsstyle)
331         //  return;
332
333         features.require("boldsymbol");
334         //features.binom      = true;
335
336         MathNestInset::validate(features);
337 }
338
339
340 void MathHullInset::header_write(WriteStream & os) const
341 {
342         bool n = numberedType();
343
344         if (type_ == "none")
345                 ;
346
347         else if (type_ == "simple") {
348                 os << '$';
349                 if (cell(0).empty())
350                         os << ' ';
351         }
352
353         else if (type_ == "equation") {
354                 if (n)
355                         os << "\\begin{equation" << star(n) << "}\n";
356                 else
357                         os << "\\[\n";
358         }
359
360         else if (type_ == "eqnarray" || type_ == "align")
361                         os << "\\begin{" << type_ << star(n) << "}\n";
362
363         else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") 
364                 os << "\\begin{" << type_ << star(n) << "}"
365                   << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
366
367         else if (type_ == "multline" || type_ == "gather") 
368                 os << "\\begin{" << type_ << "}\n";
369
370         else 
371                 os << "\\begin{unknown" << star(n) << "}";
372 }
373
374
375 void MathHullInset::footer_write(WriteStream & os) const
376 {
377         bool n = numberedType();
378
379         if (type_ == "none")
380                 os << "\n";
381
382         else if (type_ == "simple")
383                 os << '$';
384
385         else if (type_ == "equation")
386                 if (n)
387                         os << "\\end{equation" << star(n) << "}\n";
388                 else
389                         os << "\\]\n";
390
391         else if (type_ == "eqnarray" || type_ == "align" || type_ == "alignat"
392               || type_ == "xalignat")
393                 os << "\n\\end{" << type_ << star(n) << "}\n";
394
395         else if (type_ == "xxalignat" || type_ == "multline" || type_ == "gather")
396                 os << "\n\\end{" << type_ << "}\n";
397
398         else
399                 os << "\\end{unknown" << star(n) << "}";
400 }
401
402
403 void MathHullInset::addRow(row_type row)
404 {
405         nonum_.insert(nonum_.begin() + row + 1, !numberedType());
406         label_.insert(label_.begin() + row + 1, string());
407         MathGridInset::addRow(row);
408 }
409
410
411 void MathHullInset::delRow(row_type row)
412 {
413         MathGridInset::delRow(row);
414         nonum_.erase(nonum_.begin() + row);
415         label_.erase(label_.begin() + row);
416 }
417
418
419 void MathHullInset::addFancyCol(col_type col)
420 {
421         if (type_ == "equation")
422                 mutate("eqnarray");
423         
424         else if (type_ == "eqnarray") {
425                 mutate("align");
426                 addFancyCol(col);
427         }
428
429         else if (type_ == "align") {
430                 mutate("alignat");
431                 addFancyCol(col);
432         }
433
434         else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
435                 MathGridInset::addCol(col);
436                 MathGridInset::addCol(col + 1);
437         }
438 }
439
440
441 void MathHullInset::delFancyCol(col_type col)
442 {
443         if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
444                 MathGridInset::delCol(col + 1);
445                 MathGridInset::delCol(col);
446         }
447 }
448
449
450 string MathHullInset::nicelabel(row_type row) const
451 {
452         if (nonum_[row])
453                 return string();
454         if (label_[row].empty())
455                 return string("(#)");
456         return "(" + label_[row] + ")";
457 }
458
459
460 void MathHullInset::glueall()
461 {
462         MathArray ar;
463         for (idx_type i = 0; i < nargs(); ++i)
464                 ar.push_back(cell(i));
465         *this = MathHullInset("simple");
466         cell(0) = ar;
467         setDefaults();
468 }
469
470
471 string const & MathHullInset::getType() const
472 {
473         return type_;
474 }
475
476
477 void MathHullInset::setType(string const & type)
478 {
479         type_ = type;
480         setDefaults();
481 }
482
483
484
485 void MathHullInset::mutate(string const & newtype)
486 {
487         //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
488
489         // we try to move along the chain
490         // none <-> simple <-> equation <-> eqnarray 
491
492         if (newtype == "dump") {
493                 dump();
494         }
495
496         else if (newtype == type_) {
497                 // done
498         }
499
500         else if (type_ == "none") {
501                 setType("simple");
502                 numbered(0, false);
503                 mutate(newtype);
504         }
505
506         else if (type_ == "simple") {
507                 if (newtype == "none") {
508                         setType("none");
509                 } else {
510                         setType("equation");
511                         numbered(0, false);
512                         mutate(newtype);
513                 }
514         }
515
516         else if (type_ == "equation") {
517                 if (smaller(newtype, type_)) {
518                         setType("simple");
519                         mutate(newtype);
520                 } else if (newtype == "eqnarray") {
521                         MathGridInset::addCol(1);
522                         MathGridInset::addCol(1);
523
524                         // split it "nicely" on the firest relop
525                         pos_type pos = firstRelOp(cell(0));
526                         cell(1) = MathArray(cell(0), pos, cell(0).size());
527                         cell(0).erase(pos, cell(0).size());
528
529                         if (cell(1).size()) {
530                                 cell(2) = MathArray(cell(1), 1, cell(1).size());
531                                 cell(1).erase(1, cell(1).size());
532                         }
533                         setType("eqnarray");
534                         mutate(newtype);
535                 } else {
536                         MathGridInset::addCol(1);
537                         // split it "nicely"
538                         pos_type pos = firstRelOp(cell(0));
539                         cell(1) = cell(0);
540                         cell(0).erase(pos, cell(0).size());
541                         cell(1).erase(0, pos);
542                         setType("align");
543                         mutate(newtype);
544                 }
545         }
546
547         else if (type_ == "eqnarray") {
548                 if (smaller(newtype, type_)) {
549                         // set correct (no)numbering
550                         bool allnonum = true;
551                         for (row_type row = 0; row < nrows(); ++row)
552                                 if (!nonum_[row])
553                                         allnonum = false;
554
555                         // set first non-empty label
556                         string label;
557                         for (row_type row = 0; row < nrows(); ++row) {
558                                 if (!label_[row].empty()) {
559                                         label = label_[row];
560                                         break;
561                                 }
562                         }
563
564                         glueall();
565                         nonum_[0] = allnonum;
566                         label_[0] = label;
567                         mutate(newtype);
568                 } else { // align & Co.
569                         for (row_type row = 0; row < nrows(); ++row) {
570                                 idx_type c = 3 * row + 1;
571                                 cell(c).push_back(cell(c + 1));
572                         }
573                         MathGridInset::delCol(2);
574                         setType("align");
575                         mutate(newtype);
576                 }
577         }
578
579         else if (type_ == "align") {
580                 if (smaller(newtype, type_)) {
581                         MathGridInset::addCol(1);
582                         setType("eqnarray");
583                         mutate(newtype);
584                 } else {
585                         setType(newtype);
586                 }
587         }
588
589         else if (type_ == "multline") {
590                 if (newtype == "gather") {
591                         setType("gather");
592                 } else {
593                         lyxerr << "mutation from '" << type_
594                                 << "' to '" << newtype << "' not implemented"
595                                                  << endl;
596                 }
597         }
598
599         else if (type_ == "gather") {
600                 if (newtype == "multline") {
601                         setType("multline");
602                 } else {
603                         lyxerr << "mutation from '" << type_
604                                 << "' to '" << newtype << "' not implemented" << endl;
605                 }
606         }
607
608         else {
609                 lyxerr << "mutation from '" << type_
610                                          << "' to '" << newtype << "' not implemented" << endl;
611         }
612 }
613
614
615 void MathHullInset::write(WriteStream & os) const
616 {
617         header_write(os);
618
619         bool n = numberedType();
620
621         for (row_type row = 0; row < nrows(); ++row) {
622                 for (col_type col = 0; col < ncols(); ++col)
623                         os << cell(index(row, col)) << eocString(col);
624                 if (n) {
625                         if (!label_[row].empty())
626                                 os << "\\label{" << label_[row] << "}";
627                         if (nonum_[row])
628                                 os << "\\nonumber ";
629                 }
630                 os << eolString(row);
631         }
632
633         footer_write(os);
634 }
635
636
637 void MathHullInset::normalize(NormalStream & os) const
638 {
639         os << "[formula " << type_ << " ";
640         MathGridInset::normalize(os);
641         os << "] ";
642 }
643
644
645 void MathHullInset::mathmlize(MathMLStream & os) const
646 {
647         MathGridInset::mathmlize(os);
648 }
649
650
651 void MathHullInset::infoize(std::ostream & os) const
652 {
653         os << "Type: " << type_;
654 }
655
656
657 void MathHullInset::check() const
658 {
659         lyx::Assert(nonum_.size() == nrows());
660         lyx::Assert(label_.size() == nrows());
661 }