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