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