]> git.lyx.org Git - lyx.git/blob - src/mathed/math_hullinset.C
next shot at xxalignat
[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                 dim_.a += 12;
173                 dim_.d += 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                         dim_.w += 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         dim_.a = max(dim_.a,  asc);
191         dim_.d = max(dim_.d, des);
192
193         // for markers
194         metricsMarkers2();
195 }
196
197
198 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
199 {
200         MathFontSetChanger dummy(pi.base, standardFont());
201         MathGridInset::draw(pi, x + 1, y);
202
203         if (numberedType()) {
204                 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
205                 for (row_type row = 0; row < nrows(); ++row) {
206                         int const yy = y + rowinfo_[row].offset_;
207                         MathFontSetChanger dummy(pi.base, "mathrm");
208                         drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
209                 }
210         }
211
212         drawMarkers2(pi, x, y);
213 }
214
215
216 void MathHullInset::metricsT(TextMetricsInfo const & mi) const
217 {
218         if (display()) {
219                 MathGridInset::metricsT(mi);
220         } else {
221                 ostringstream os;
222                 WriteStream wi(os, false, true);
223                 write(wi);
224                 dim_.w = os.str().size();
225                 dim_.a = 1;
226                 dim_.d = 0;
227         }
228 }
229
230
231 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
232 {
233         if (display()) {
234                 MathGridInset::drawT(pain, x, y);
235         } else {
236                 ostringstream os;
237                 WriteStream wi(os, false, true);
238                 write(wi);
239                 pain.draw(x, y, os.str().c_str());
240         }
241 }
242
243
244 string MathHullInset::label(row_type row) const
245 {
246         row_type n = nrows();
247         lyx::Assert(row < n);
248         return label_[row];
249 }
250
251
252 void MathHullInset::label(row_type row, string const & label)
253 {
254         lyxerr << "setting label '" << label << "' for row " << row << endl;
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> 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") 
356                 os << "\\begin{" << type_ << star(n) << "}"
357                   << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
358  
359         else if (type_ == "xxalignat") 
360                 os << "\\begin{" << type_ << "}"
361                   << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
362  
363         else if (type_ == "multline" || type_ == "gather") 
364                 os << "\\begin{" << type_ << "}\n";
365
366         else 
367                 os << "\\begin{unknown" << star(n) << "}";
368 }
369
370
371 void MathHullInset::footer_write(WriteStream & os) const
372 {
373         bool n = numberedType();
374
375         if (type_ == "none")
376                 os << "\n";
377
378         else if (type_ == "simple")
379                 os << '$';
380
381         else if (type_ == "equation")
382                 if (n)
383                         os << "\\end{equation" << star(n) << "}\n";
384                 else
385                         os << "\\]\n";
386
387         else if (type_ == "eqnarray" || type_ == "align" || type_ == "alignat"
388               || type_ == "xalignat")
389                 os << "\n\\end{" << type_ << star(n) << "}\n";
390
391         else if (type_ == "xxalignat" || type_ == "multline" || type_ == "gather")
392                 os << "\n\\end{" << type_ << "}\n";
393
394         else
395                 os << "\\end{unknown" << star(n) << "}";
396 }
397
398
399 void MathHullInset::addRow(row_type row)
400 {
401         nonum_.insert(nonum_.begin() + row + 1, !numberedType());
402         label_.insert(label_.begin() + row + 1, string());
403         MathGridInset::addRow(row);
404 }
405
406
407 void MathHullInset::delRow(row_type row)
408 {
409         MathGridInset::delRow(row);
410         nonum_.erase(nonum_.begin() + row);
411         label_.erase(label_.begin() + row);
412 }
413
414
415 void MathHullInset::addFancyCol(col_type col)
416 {
417         if (type_ == "equation")
418                 mutate("eqnarray");
419         
420         else if (type_ == "eqnarray") {
421                 mutate("align");
422                 addFancyCol(col);
423         }
424
425         else if (type_ == "align") {
426                 mutate("alignat");
427                 addFancyCol(col);
428         }
429
430         else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
431                 MathGridInset::addCol(col);
432                 MathGridInset::addCol(col + 1);
433         }
434 }
435
436
437 void MathHullInset::delFancyCol(col_type col)
438 {
439         if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
440                 MathGridInset::delCol(col + 1);
441                 MathGridInset::delCol(col);
442         }
443 }
444
445
446 string MathHullInset::nicelabel(row_type row) const
447 {
448         if (nonum_[row])
449                 return string();
450         if (label_[row].empty())
451                 return string("(#)");
452         return "(" + label_[row] + ")";
453 }
454
455
456 void MathHullInset::glueall()
457 {
458         MathArray ar;
459         for (idx_type i = 0; i < nargs(); ++i)
460                 ar.push_back(cell(i));
461         *this = MathHullInset("simple");
462         cell(0) = ar;
463         setDefaults();
464 }
465
466
467 string const & MathHullInset::getType() const
468 {
469         return type_;
470 }
471
472
473 void MathHullInset::setType(string const & type)
474 {
475         type_ = type;
476         setDefaults();
477 }
478
479
480
481 void MathHullInset::mutate(string const & newtype)
482 {
483         //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
484
485         // we try to move along the chain
486         // none <-> simple <-> equation <-> eqnarray 
487
488         if (newtype == "dump") {
489                 dump();
490         }
491
492         else if (newtype == type_) {
493                 // done
494         }
495
496         else if (type_ == "none") {
497                 setType("simple");
498                 numbered(0, false);
499                 mutate(newtype);
500         }
501
502         else if (type_ == "simple") {
503                 if (newtype == "none") {
504                         setType("none");
505                 } else {
506                         setType("equation");
507                         numbered(0, false);
508                         mutate(newtype);
509                 }
510         }
511
512         else if (type_ == "equation") {
513                 if (smaller(newtype, type_)) {
514                         setType("simple");
515                         mutate(newtype);
516                 } else if (newtype == "eqnarray") {
517                         MathGridInset::addCol(1);
518                         MathGridInset::addCol(1);
519
520                         // split it "nicely" on the firest relop
521                         pos_type pos = firstRelOp(cell(0));
522                         cell(1) = MathArray(cell(0), pos, cell(0).size());
523                         cell(0).erase(pos, cell(0).size());
524
525                         if (cell(1).size()) {
526                                 cell(2) = MathArray(cell(1), 1, cell(1).size());
527                                 cell(1).erase(1, cell(1).size());
528                         }
529                         setType("eqnarray");
530                         mutate(newtype);
531                 } else {
532                         MathGridInset::addCol(1);
533                         // split it "nicely"
534                         pos_type pos = firstRelOp(cell(0));
535                         cell(1) = cell(0);
536                         cell(0).erase(pos, cell(0).size());
537                         cell(1).erase(0, pos);
538                         setType("align");
539                         mutate(newtype);
540                 }
541         }
542
543         else if (type_ == "eqnarray") {
544                 if (smaller(newtype, type_)) {
545                         // set correct (no)numbering
546                         bool allnonum = true;
547                         for (row_type row = 0; row < nrows(); ++row)
548                                 if (!nonum_[row])
549                                         allnonum = false;
550
551                         // set first non-empty label
552                         string label;
553                         for (row_type row = 0; row < nrows(); ++row) {
554                                 if (!label_[row].empty()) {
555                                         label = label_[row];
556                                         break;
557                                 }
558                         }
559
560                         glueall();
561                         nonum_[0] = allnonum;
562                         label_[0] = label;
563                         mutate(newtype);
564                 } else { // align & Co.
565                         for (row_type row = 0; row < nrows(); ++row) {
566                                 idx_type c = 3 * row + 1;
567                                 cell(c).push_back(cell(c + 1));
568                         }
569                         MathGridInset::delCol(2);
570                         setType("align");
571                         mutate(newtype);
572                 }
573         }
574
575         else if (type_ == "align") {
576                 if (smaller(newtype, type_)) {
577                         MathGridInset::addCol(1);
578                         setType("eqnarray");
579                         mutate(newtype);
580                 } else {
581                         setType(newtype);
582                 }
583         }
584
585         else if (type_ == "multline") {
586                 if (newtype == "gather") {
587                         setType("gather");
588                 } else {
589                         lyxerr << "mutation from '" << type_
590                                 << "' to '" << newtype << "' not implemented"
591                                                  << endl;
592                 }
593         }
594
595         else if (type_ == "gather") {
596                 if (newtype == "multline") {
597                         setType("multline");
598                 } else {
599                         lyxerr << "mutation from '" << type_
600                                 << "' to '" << newtype << "' not implemented" << endl;
601                 }
602         }
603
604         else {
605                 lyxerr << "mutation from '" << type_
606                                          << "' to '" << newtype << "' not implemented" << endl;
607         }
608 }
609
610
611 void MathHullInset::write(WriteStream & os) const
612 {
613         header_write(os);
614
615         bool n = numberedType();
616
617         for (row_type row = 0; row < nrows(); ++row) {
618                 for (col_type col = 0; col < ncols(); ++col)
619                         os << cell(index(row, col)) << eocString(col);
620                 if (n) {
621                         if (!label_[row].empty())
622                                 os << "\\label{" << label_[row] << "}";
623                         if (nonum_[row])
624                                 os << "\\nonumber ";
625                 }
626                 os << eolString(row);
627         }
628
629         footer_write(os);
630 }
631
632
633 void MathHullInset::normalize(NormalStream & os) const
634 {
635         os << "[formula " << type_ << " ";
636         MathGridInset::normalize(os);
637         os << "] ";
638 }
639
640
641 void MathHullInset::mathmlize(MathMLStream & os) const
642 {
643         MathGridInset::mathmlize(os);
644 }
645
646
647 void MathHullInset::infoize(std::ostream & os) const
648 {
649         os << "Type: " << type_;
650 }
651
652
653 void MathHullInset::check() const
654 {
655         lyx::Assert(nonum_.size() == nrows());
656         lyx::Assert(label_.size() == nrows());
657 }