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