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