]> git.lyx.org Git - lyx.git/blob - src/mathed/math_hullinset.C
39a231889a443072ac0f80b1a1eba225e2b0437d
[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
194
195 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
196 {
197         MathFontSetChanger dummy(pi.base, standardFont());
198         MathGridInset::draw(pi, x, y);
199
200         if (numberedType()) {
201                 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
202                 for (row_type row = 0; row < nrows(); ++row) {
203                         int const yy = y + rowinfo_[row].offset_;
204                         MathFontSetChanger dummy(pi.base, "mathrm");
205                         drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
206                 }
207         }
208 }
209
210
211 void MathHullInset::metricsT(TextMetricsInfo const &) const
212 {
213 #if 0
214         if (display()) {
215                 MathGridInset::metricsT(mi);
216         } else
217 #endif
218         {
219                 ostringstream os;
220                 WriteStream wi(os, false, true);
221                 write(wi);
222                 width_   = os.str().size();
223                 ascent_  = 1;
224                 descent_ = 0;
225         }
226 }
227
228
229 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
230 {
231 #if 0
232         if (display()) {
233                 MathGridInset::drawT(pain, x, y);
234         } else
235 #endif
236         {
237                 ostringstream os;
238                 WriteStream wi(os, false, true);
239                 write(wi);
240                 pain.draw(x, y, os.str().c_str());
241         }
242 }
243
244
245 string MathHullInset::label(row_type row) const
246 {
247         row_type n = nrows();
248         lyx::Assert(row < n);
249         return label_[row];
250 }
251
252
253 void MathHullInset::label(row_type row, string const & label)
254 {
255         label_[row] = label;
256 }
257
258
259 void MathHullInset::numbered(row_type row, bool num)
260 {
261         nonum_[row] = !num;
262 }
263
264
265 bool MathHullInset::numbered(row_type row) const
266 {
267         return !nonum_[row];
268 }
269
270
271 bool MathHullInset::ams() const
272 {
273         return
274                 type_ == "align" ||
275                 type_ == "multline" ||
276                 type_ == "gather" ||
277                 type_ == "alignat" ||
278                 type_ == "xalignat" ||
279                 type_ == "xxalignat";
280 }
281
282
283 bool MathHullInset::display() const
284 {
285         return type_ != "simple" && type_ != "none";
286 }
287
288
289 vector<string> const MathHullInset::getLabelList() const
290 {
291         vector<string> res;
292         for (row_type row = 0; row < nrows(); ++row)
293                 if (!label_[row].empty() && nonum_[row] != 1)
294                         res.push_back(label_[row]);
295         return res;
296 }
297
298
299 bool MathHullInset::numberedType() const
300 {
301         if (type_ == "none")
302                 return false;
303         if (type_ == "simple")
304                 return false;
305         if (type_ == "xxalignat")
306                 return false;
307         for (row_type row = 0; row < nrows(); ++row)
308                 if (!nonum_[row])
309                         return true;
310         return false;
311 }
312
313
314 void MathHullInset::validate(LaTeXFeatures & features) const
315 {
316         if (ams())
317                 features.require("amsmath");
318
319
320         // Validation is necessary only if not using AMS math.
321         // To be safe, we will always run mathedvalidate.
322         //if (features.amsstyle)
323         //  return;
324
325         features.require("boldsymbol");
326         //features.binom      = true;
327
328         MathNestInset::validate(features);
329 }
330
331
332 void MathHullInset::header_write(WriteStream & os) const
333 {
334         bool n = numberedType();
335
336         if (type_ == "none")
337                 ;
338
339         else if (type_ == "simple") {
340                 os << '$';
341                 if (cell(0).empty())
342                         os << ' ';
343         }
344
345         else if (type_ == "equation") {
346                 if (n)
347                         os << "\\begin{equation" << star(n) << "}\n";
348                 else
349                         os << "\\[\n";
350         }
351
352         else if (type_ == "eqnarray" || type_ == "align")
353                         os << "\\begin{" << type_ << star(n) << "}\n";
354
355         else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") 
356                 os << "\\begin{" << type_ << star(n) << "}"
357                   << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
358
359         else if (type_ == "multline" || type_ == "gather") 
360                 os << "\\begin{" << type_ << "}\n";
361
362         else 
363                 os << "\\begin{unknown" << star(n) << "}";
364 }
365
366
367 void MathHullInset::footer_write(WriteStream & os) const
368 {
369         bool n = numberedType();
370
371         if (type_ == "none")
372                 os << "\n";
373
374         else if (type_ == "simple")
375                 os << '$';
376
377         else if (type_ == "equation")
378                 if (n)
379                         os << "\\end{equation" << star(n) << "}\n";
380                 else
381                         os << "\\]\n";
382
383         else if (type_ == "eqnarray" || type_ == "align" || type_ == "alignat"
384               || type_ == "xalignat")
385                 os << "\n\\end{" << type_ << star(n) << "}\n";
386
387         else if (type_ == "xxalignat" || type_ == "multline" || type_ == "gather")
388                 os << "\n\\end{" << type_ << "}\n";
389
390         else
391                 os << "\\end{unknown" << star(n) << "}";
392 }
393
394
395 void MathHullInset::addRow(row_type row)
396 {
397         nonum_.insert(nonum_.begin() + row + 1, !numberedType());
398         label_.insert(label_.begin() + row + 1, string());
399         MathGridInset::addRow(row);
400 }
401
402
403 void MathHullInset::delRow(row_type row)
404 {
405         MathGridInset::delRow(row);
406         nonum_.erase(nonum_.begin() + row);
407         label_.erase(label_.begin() + row);
408 }
409
410
411 void MathHullInset::addFancyCol(col_type col)
412 {
413         if (type_ == "equation")
414                 mutate("eqnarray");
415         
416         else if (type_ == "eqnarray") {
417                 mutate("align");
418                 addFancyCol(col);
419         }
420
421         else if (type_ == "align") {
422                 mutate("alignat");
423                 addFancyCol(col);
424         }
425
426         else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
427                 MathGridInset::addCol(col);
428                 MathGridInset::addCol(col + 1);
429         }
430 }
431
432
433 void MathHullInset::delFancyCol(col_type col)
434 {
435         if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
436                 MathGridInset::delCol(col + 1);
437                 MathGridInset::delCol(col);
438         }
439 }
440
441
442 string MathHullInset::nicelabel(row_type row) const
443 {
444         if (nonum_[row])
445                 return string();
446         if (label_[row].empty())
447                 return string("(#)");
448         return "(" + label_[row] + ")";
449 }
450
451
452 void MathHullInset::glueall()
453 {
454         MathArray ar;
455         for (idx_type i = 0; i < nargs(); ++i)
456                 ar.push_back(cell(i));
457         *this = MathHullInset("simple");
458         cell(0) = ar;
459         setDefaults();
460 }
461
462
463 string const & MathHullInset::getType() const
464 {
465         return type_;
466 }
467
468
469 void MathHullInset::setType(string const & type)
470 {
471         type_ = type;
472         setDefaults();
473 }
474
475
476
477 void MathHullInset::mutate(string const & newtype)
478 {
479         //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
480
481         // we try to move along the chain
482         // none <-> simple <-> equation <-> eqnarray 
483
484         if (newtype == "dump") {
485                 dump();
486         }
487
488         else if (newtype == type_) {
489                 // done
490         }
491
492         else if (type_ == "none") {
493                 setType("simple");
494                 numbered(0, false);
495                 mutate(newtype);
496         }
497
498         else if (type_ == "simple") {
499                 if (newtype == "none") {
500                         setType("none");
501                 } else {
502                         setType("equation");
503                         numbered(0, false);
504                         mutate(newtype);
505                 }
506         }
507
508         else if (type_ == "equation") {
509                 if (smaller(newtype, type_)) {
510                         setType("simple");
511                         mutate(newtype);
512                 } else if (newtype == "eqnarray") {
513                         MathGridInset::addCol(1);
514                         MathGridInset::addCol(1);
515
516                         // split it "nicely" on the firest relop
517                         pos_type pos = firstRelOp(cell(0));
518                         cell(1) = MathArray(cell(0), pos, cell(0).size());
519                         cell(0).erase(pos, cell(0).size());
520
521                         if (cell(1).size()) {
522                                 cell(2) = MathArray(cell(1), 1, cell(1).size());
523                                 cell(1).erase(1, cell(1).size());
524                         }
525                         setType("eqnarray");
526                         mutate(newtype);
527                 } else {
528                         MathGridInset::addCol(1);
529                         // split it "nicely"
530                         pos_type pos = firstRelOp(cell(0));
531                         cell(1) = cell(0);
532                         cell(0).erase(pos, cell(0).size());
533                         cell(1).erase(0, pos);
534                         setType("align");
535                         mutate(newtype);
536                 }
537         }
538
539         else if (type_ == "eqnarray") {
540                 if (smaller(newtype, type_)) {
541                         // set correct (no)numbering
542                         bool allnonum = true;
543                         for (row_type row = 0; row < nrows(); ++row)
544                                 if (!nonum_[row])
545                                         allnonum = false;
546
547                         // set first non-empty label
548                         string label;
549                         for (row_type row = 0; row < nrows(); ++row) {
550                                 if (!label_[row].empty()) {
551                                         label = label_[row];
552                                         break;
553                                 }
554                         }
555
556                         glueall();
557                         nonum_[0] = allnonum;
558                         label_[0] = label;
559                         mutate(newtype);
560                 } else { // align & Co.
561                         for (row_type row = 0; row < nrows(); ++row) {
562                                 idx_type c = 3 * row + 1;
563                                 cell(c).push_back(cell(c + 1));
564                         }
565                         MathGridInset::delCol(2);
566                         setType("align");
567                         mutate(newtype);
568                 }
569         }
570
571         else if (type_ == "align") {
572                 if (smaller(newtype, type_)) {
573                         MathGridInset::addCol(1);
574                         setType("eqnarray");
575                         mutate(newtype);
576                 } else {
577                         setType(newtype);
578                 }
579         }
580
581         else if (type_ == "multline") {
582                 if (newtype == "gather") {
583                         setType("gather");
584                 } else {
585                         lyxerr << "mutation from '" << type_
586                                 << "' to '" << newtype << "' not implemented"
587                                                  << endl;
588                 }
589         }
590
591         else if (type_ == "gather") {
592                 if (newtype == "multline") {
593                         setType("multline");
594                 } else {
595                         lyxerr << "mutation from '" << type_
596                                 << "' to '" << newtype << "' not implemented" << endl;
597                 }
598         }
599
600         else {
601                 lyxerr << "mutation from '" << type_
602                                          << "' to '" << newtype << "' not implemented" << endl;
603         }
604 }
605
606
607 void MathHullInset::write(WriteStream & os) const
608 {
609         header_write(os);
610
611         bool n = numberedType();
612
613         for (row_type row = 0; row < nrows(); ++row) {
614                 for (col_type col = 0; col < ncols(); ++col)
615                         os << cell(index(row, col)) << eocString(col);
616                 if (n) {
617                         if (!label_[row].empty())
618                                 os << "\\label{" << label_[row] << "}";
619                         if (nonum_[row])
620                                 os << "\\nonumber ";
621                 }
622                 os << eolString(row);
623         }
624
625         footer_write(os);
626 }
627
628
629 void MathHullInset::normalize(NormalStream & os) const
630 {
631         os << "[formula " << type_ << " ";
632         MathGridInset::normalize(os);
633         os << "] ";
634 }
635
636
637 void MathHullInset::mathmlize(MathMLStream & os) const
638 {
639         MathGridInset::mathmlize(os);
640 }
641
642
643 void MathHullInset::infoize(std::ostream & os) const
644 {
645         os << "Type: " << type_;
646 }
647
648
649 void MathHullInset::check() const
650 {
651         lyx::Assert(nonum_.size() == nrows());
652         lyx::Assert(label_.size() == nrows());
653 }
654
655
656
657
658
659 //
660 // MathParInset
661 //
662
663 MathParInset::MathParInset()
664 {
665         lyxerr << "constructing MathParInset\n";
666 }
667
668
669 void MathParInset::metrics(MathMetricsInfo & mi) const
670 {
671         MathFontSetChanger dummy(mi.base, "textnormal");
672         MathGridInset::metrics(mi);
673 }
674
675
676 void MathParInset::draw(MathPainterInfo & pi, int x, int y) const
677 {
678         MathFontSetChanger dummy(pi.base, "textnormal");
679         MathGridInset::draw(pi, x, y);
680 }
681
682
683 void MathParInset::write(WriteStream & os) const
684 {
685         for (idx_type i = 0; i < nargs(); ++i)
686                 os << cell(i) << "\n";
687 }
688
689
690 void MathParInset::infoize(std::ostream & os) const
691 {
692         os << "Type: Paragraph ";
693 }
694