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