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