]> git.lyx.org Git - lyx.git/blob - src/mathed/math_matrixinset.C
don't #include too much stuff in .h
[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 "Painter.h"
11 #include "LaTeXFeatures.h"
12 #include "math_mathmlstream.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::mathmlize(MathMLStream & os) const
189 {
190         MathGridInset::mathmlize(os);
191 }
192
193
194 string MathMatrixInset::label(row_type row) const
195 {
196         return label_[row];
197 }
198
199
200 void MathMatrixInset::label(row_type row, string const & label)
201 {
202         label_[row] = label; 
203 }
204
205
206 void MathMatrixInset::numbered(row_type row, bool num)
207 {
208         nonum_[row] = !num; 
209 }
210
211
212 bool MathMatrixInset::numbered(row_type row) const
213 {
214         return !nonum_[row];
215 }
216
217
218 bool MathMatrixInset::ams() const
219 {
220         return true;
221
222         return 
223                 objtype_ == LM_OT_ALIGN ||
224                 objtype_ == LM_OT_MULTLINE ||
225                 objtype_ == LM_OT_GATHER ||
226                 objtype_ == LM_OT_ALIGNAT ||
227                 objtype_ == LM_OT_XALIGNAT ||
228                 objtype_ == LM_OT_XXALIGNAT;
229 }
230
231
232 bool MathMatrixInset::display() const
233 {
234         return getType() != LM_OT_SIMPLE;
235 }
236
237
238 std::vector<string> const MathMatrixInset::getLabelList() const
239 {
240         std::vector<string> res;
241         for (row_type row = 0; row < nrows(); ++row)
242                 if (!label_[row].empty() && nonum_[row] != 1)
243                         res.push_back(label_[row]);
244         return res;
245 }
246
247
248 bool MathMatrixInset::numberedType() const
249 {
250         if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
251                 return false;
252         for (row_type row = 0; row < nrows(); ++row)
253                 if (!nonum_[row])
254                         return true;
255         return false;
256 }
257
258
259 void MathMatrixInset::validate(LaTeXFeatures & features) const
260 {
261         features.amsstyle = ams();
262
263         // Validation is necessary only if not using AMS math.
264         // To be safe, we will always run mathedvalidate.
265         //if (features.amsstyle)
266         //  return;
267
268         features.boldsymbol = true;
269         //features.binom      = true;
270
271         MathNestInset::validate(features);
272 }
273
274
275 void MathMatrixInset::header_write(std::ostream & os) const
276 {
277         bool n = numberedType();
278
279         switch (getType()) {
280                 case LM_OT_SIMPLE:
281                         os << '$';
282                         if (cell(0).empty())
283                                 os << ' ';
284                         break;
285
286                 case LM_OT_EQUATION:
287                         if (n)
288                                 os << "\\begin{equation" << star(n) << "}\n"; 
289                         else
290                                 os << "\\[\n"; 
291                         break;
292
293                 case LM_OT_EQNARRAY:
294                         os << "\\begin{eqnarray" << star(n) << "}\n";
295                         break;
296
297                 case LM_OT_ALIGN:
298                         os << "\\begin{align" << star(n) << "}\n";
299                         break;
300
301                 case LM_OT_ALIGNAT:
302                         os << "\\begin{alignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
303                         break;
304
305                 case LM_OT_XALIGNAT:
306                         os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
307                         break;
308
309                 case LM_OT_XXALIGNAT:
310                         os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\n";
311                         break;
312
313                 case LM_OT_MULTLINE:
314                         os << "\\begin{multline}\n";
315                         break;
316
317                 case LM_OT_GATHER:
318                         os << "\\begin{gather}\n";
319                         break;
320
321                 default:
322                         os << "\\begin{unknown" << star(n) << "}";
323         }
324 }
325
326
327 void MathMatrixInset::footer_write(std::ostream & os) const
328 {
329         bool n = numberedType();
330
331         switch (getType()) {
332                 case LM_OT_SIMPLE:
333                         os << '$';
334                         break;
335
336                 case LM_OT_EQUATION:
337                         if (n)
338                                 os << "\\end{equation" << star(n) << "}\n"; 
339                         else
340                                 os << "\\]\n"; 
341                         break;
342
343                 case LM_OT_EQNARRAY:
344                         os << "\n\\end{eqnarray" << star(n) << "}\n";
345                         break;
346
347                 case LM_OT_ALIGN:
348                         os << "\n\\end{align" << star(n) << "}\n";
349                         break;
350
351                 case LM_OT_ALIGNAT:
352                         os << "\n\\end{alignat" << star(n) << "}\n";
353                         break;
354
355                 case LM_OT_XALIGNAT:
356                         os << "\n\\end{xalignat" << star(n) << "}\n";
357                         break;
358
359                 case LM_OT_XXALIGNAT:
360                         os << "\n\\end{xxalignat}\n";
361                         break;
362
363                 case LM_OT_MULTLINE:
364                         os << "\n\\end{multline}\n";
365                         break;
366
367                 case LM_OT_GATHER:
368                         os << "\n\\end{gather}\n";
369                         break;
370
371                 default:
372                         os << "\\end{unknown" << star(n) << "}";
373         }
374 }
375
376
377 void MathMatrixInset::addRow(row_type row) 
378 {
379         nonum_.insert(nonum_.begin() + row + 1, !numberedType());
380         label_.insert(label_.begin() + row + 1, string());
381         MathGridInset::addRow(row);
382 }
383
384
385 void MathMatrixInset::appendRow()
386 {
387         nonum_.push_back(!numberedType());
388         label_.push_back(string());
389         MathGridInset::appendRow();
390 }
391
392
393 void MathMatrixInset::delRow(row_type row) 
394 {
395         MathGridInset::delRow(row);
396         nonum_.erase(nonum_.begin() + row);
397         label_.erase(label_.begin() + row);
398 }
399
400
401 void MathMatrixInset::addCol(col_type col)
402 {
403         switch (getType()) {
404                 case LM_OT_EQUATION:
405                         mutate(LM_OT_EQNARRAY);
406                         break;
407
408                 case LM_OT_EQNARRAY:
409                         mutate(LM_OT_ALIGN);
410                         addCol(col);
411                         break;
412
413                 case LM_OT_ALIGN:
414                         mutate(LM_OT_ALIGNAT);
415                         addCol(col);
416                         break;
417
418                 case LM_OT_ALIGNAT:
419                 case LM_OT_XALIGNAT:
420                 case LM_OT_XXALIGNAT:
421                         MathGridInset::addCol(col);
422                         MathGridInset::addCol(col + 1);
423                         break;
424
425                 default:
426                         break;
427         }
428 }
429
430
431 void MathMatrixInset::delCol(col_type col)
432 {
433         switch (getType()) {
434                 case LM_OT_ALIGNAT:
435                 case LM_OT_XALIGNAT:
436                 case LM_OT_XXALIGNAT:
437                         MathGridInset::delCol(col + 1);
438                         MathGridInset::delCol(col);
439                         break;
440                 default:
441                         break;
442         }
443 }
444
445
446 string MathMatrixInset::nicelabel(row_type row) const
447 {
448         if (nonum_[row])
449                 return string();
450         if (label_[row].empty())
451                 return string("(#)");
452         return "(" + label_[row] + ")";
453 }
454
455
456 void MathMatrixInset::mutate(string const & newtype)
457 {
458         if (newtype == "dump") {
459                 dump();
460                 return;
461         }
462         //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
463         mutate(typecode(newtype));
464 }
465
466
467 void MathMatrixInset::glueall()
468 {
469         MathArray ar;
470         for (idx_type i = 0; i < nargs(); ++i)
471                 ar.push_back(cell(i));
472         *this = MathMatrixInset(LM_OT_SIMPLE);
473         cell(0) = ar;
474 }
475
476
477 MathInsetTypes MathMatrixInset::getType() const
478 {
479         return objtype_;
480 }
481
482
483 void MathMatrixInset::setType(MathInsetTypes t)
484 {
485         objtype_ = t;
486         setDefaults();
487 }
488
489
490
491 void MathMatrixInset::mutate(MathInsetTypes newtype)
492 {
493         //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
494
495         if (newtype == getType())
496                 return;
497
498         switch (getType()) {
499                 case LM_OT_SIMPLE:
500                         setType(LM_OT_EQUATION);
501                         numbered(0, false);
502                         mutate(newtype);
503                         break;
504
505                 case LM_OT_EQUATION:
506                         switch (newtype) {
507                                 case LM_OT_SIMPLE:
508                                         setType(LM_OT_SIMPLE);
509                                         break;
510
511                                 case LM_OT_ALIGN: 
512                                 case LM_OT_ALIGNAT:
513                                 case LM_OT_XALIGNAT:
514                                 case LM_OT_XXALIGNAT: {
515
516                                         MathGridInset::addCol(1);
517
518                                         // split it "nicely"
519                                         pos_type pos = firstRelOp(cell(0));     
520                                         cell(1) = cell(0);
521                                         cell(0).erase(pos, cell(0).size());
522                                         cell(1).erase(0, pos);
523                                         setType(LM_OT_ALIGN);
524                                         mutate(newtype);
525                                         break;
526                                 }
527
528                                 case LM_OT_EQNARRAY:
529                                 default:
530                                         MathGridInset::addCol(1);
531                                         MathGridInset::addCol(1);
532
533                                         // split it "nicely" on the firest relop
534                                         pos_type pos = firstRelOp(cell(0));     
535                                         cell(1) = cell(0);
536                                         cell(0).erase(pos, cell(0).size());
537                                         cell(1).erase(0, pos);
538
539                                         if (cell(1).size()) {
540                                                 cell(2) = cell(1);
541                                                 cell(1).erase(1, cell(1).size());
542                                                 cell(2).erase(0);
543                                         }
544
545                                         setType(LM_OT_EQNARRAY);
546                                         mutate(newtype);
547                                         break;
548                                 }
549                         break;
550
551                 case LM_OT_EQNARRAY:
552                         switch (newtype) {
553                                 case LM_OT_SIMPLE:
554                                 case LM_OT_EQUATION: {
555                                         // set correct (no)numbering
556                                         bool allnonum = true;
557                                         for (row_type row = 0; row < nrows(); ++row) {
558                                                 if (!nonum_[row])
559                                                         allnonum = false;
560                                         }
561
562                                         // set first non-empty label
563                                         string label;
564                                         for (row_type row = 0; row < nrows(); ++row) {
565                                                 if (!label_[row].empty()) {
566                                                         label = label_[row];
567                                                         break;
568                                                 }
569                                         }
570
571                                         glueall();
572
573                                         nonum_[0] = allnonum;
574                                         label_[0] = label;
575                                         mutate(newtype);
576                                         break;
577                                 }
578
579                                 case LM_OT_ALIGN:
580                                 case LM_OT_ALIGNAT:
581                                 case LM_OT_XALIGNAT:
582                                 case LM_OT_XXALIGNAT:
583                                 default: {
584                                         for (row_type row = 0; row < nrows(); ++row) {
585                                                 idx_type c = 3 * row + 1;
586                                                 cell(c).push_back(cell(c + 1));
587                                         }
588                                         MathGridInset::delCol(2);
589                                         setType(LM_OT_ALIGN);
590                                         mutate(newtype);
591                                         break;
592                                 }
593                         }
594                         break;
595
596                 case LM_OT_ALIGN:
597                         switch (newtype) {
598                                 case LM_OT_SIMPLE:
599                                 case LM_OT_EQUATION:
600                                 case LM_OT_EQNARRAY:
601                                         MathGridInset::addCol(1);
602                                         setType(LM_OT_EQNARRAY);
603                                         mutate(newtype);
604                                         break;
605                                 
606                                 case LM_OT_ALIGNAT:
607                                 case LM_OT_XALIGNAT:
608                                 case LM_OT_XXALIGNAT:
609                                         setType(newtype);
610                                         break;
611
612                                 default:
613                                         lyxerr << "mutation from '" << getType()
614                                                 << "' to '" << newtype << "' not implemented\n";
615                                         break;
616                         }
617                         break;
618
619                 case LM_OT_MULTLINE:
620                         switch (newtype) {
621                                 case LM_OT_GATHER:
622                                         setType(LM_OT_GATHER);
623                                         break;
624                                 default:
625                                         lyxerr << "mutation from '" << getType()
626                                                 << "' to '" << newtype << "' not implemented\n";
627                                         break;
628                         }
629
630                 case LM_OT_GATHER:
631                         switch (newtype) {
632                                 case LM_OT_MULTLINE:
633                                         setType(LM_OT_MULTLINE);
634                                         break;
635                                 default:
636                                         lyxerr << "mutation from '" << getType()
637                                                 << "' to '" << newtype << "' not implemented\n";
638                                         break;
639                         }
640
641                 default:
642                         lyxerr << "mutation from '" << getType()
643                                 << "' to '" << newtype << "' not implemented\n";
644         }
645 }
646
647
648 void MathMatrixInset::write(MathWriteInfo & os) const
649 {
650   header_write(os.os);
651
652         bool n = numberedType();
653
654         for (row_type row = 0; row < nrows(); ++row) {
655                 for (col_type col = 0; col < ncols(); ++col) 
656                         os << cell(index(row, col)) << eocString(col).c_str();
657                 if (n) {
658                         if (!label_[row].empty())
659                                 os << "\\label{" << label_[row].c_str() << "}";
660                         if (nonum_[row])
661                                 os << "\\nonumber ";
662                 }
663                 os << eolString(row).c_str();
664         }
665
666   footer_write(os.os);
667 }
668
669
670 void MathMatrixInset::writeNormal(NormalStream & os) const
671 {
672         os << "[formula " << normalName(getType()).c_str() << " ";
673         MathGridInset::writeNormal(os);
674         os << "] ";
675 }
676
677