]> git.lyx.org Git - features.git/blob - src/mathed/math_matrixinset.C
some support for the [x][x]alignat environments
[features.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)->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 int MathMatrixInset::defaultColSpace(int col)
96 {
97         switch (getType()) {
98                 case LM_OT_ALIGN:
99                 case LM_OT_ALIGNAT:
100                         return 0;
101                 case LM_OT_XALIGNAT:
102                         return (col & 1) ? 20 : 0;
103                 case LM_OT_XXALIGNAT:
104                         return (col & 1) ? 40 : 0;
105                 default:;
106         }
107         return 10;
108 }
109
110
111 void MathMatrixInset::setDefaults()
112 {
113         for (int col = 0; col < ncols(); ++col) {
114                 colinfo_[col].align_ = defaultColAlign(col);
115                 colinfo_[col].skip_  = defaultColSpace(col);
116         }
117 }
118
119
120 void MathMatrixInset::metrics(MathStyles) const
121 {
122         size_ = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
123
124         // let the cells adjust themselves
125         MathGridInset::metrics(size_);
126
127         if (display()) {
128                 ascent_  += 12;
129                 descent_ += 12;
130         }       
131
132         if (numberedType()) {
133                 int l = 0;
134                 for (int row = 0; row < nrows(); ++row)
135                         l = std::max(l, mathed_string_width(LM_TC_BF, size(), nicelabel(row)));
136
137                 if (l)
138                         width_ += 30 + l;
139         }
140
141         // make it at least as high as the current font
142         int asc = 0;
143         int des = 0;
144         math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
145         ascent_  = std::max(ascent_,  asc);
146         descent_ = std::max(descent_, des);
147 }
148
149
150 void MathMatrixInset::draw(Painter & pain, int x, int y) const
151 {
152         xo(x);
153         yo(y);
154
155         MathGridInset::draw(pain, x, y);
156
157         if (numberedType()) {
158                 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
159                 for (int row = 0; row < nrows(); ++row) {
160                         int yy = y + rowinfo_[row].offset_;
161                         drawStr(pain, LM_TC_BF, size(), xx, yy, nicelabel(row));
162                 }
163         }
164 }
165
166
167 void MathMatrixInset::write(std::ostream & os, bool fragile) const
168 {
169   header_write(os);
170
171         bool n = numberedType();
172
173         for (int row = 0; row < nrows(); ++row) {
174                 for (int col = 0; col < ncols(); ++col) {
175                         cell(index(row, col)).write(os, fragile);
176                         os << eocString(col);
177                 }
178                 if (n) {
179                         if (!label_[row].empty())
180                                 os << "\\label{" << label_[row] << "}";
181                         if (nonum_[row])
182                                 os << "\\nonumber ";
183                 }
184                 os << eolString(row);
185         }
186
187   footer_write(os);
188 }
189
190
191 string MathMatrixInset::label(int row) const
192 {
193         return label_[row];
194 }
195
196
197 void MathMatrixInset::label(int row, string const & label)
198 {
199         label_[row] = label; 
200 }
201
202
203 void MathMatrixInset::numbered(int row, bool num)
204 {
205         nonum_[row] = !num; 
206 }
207
208
209 bool MathMatrixInset::numbered(int row) const
210 {
211         return !nonum_[row];
212 }
213
214
215 bool MathMatrixInset::ams() const
216 {
217         return true;
218 }
219
220
221 bool MathMatrixInset::display() const
222 {
223         return getType() != LM_OT_SIMPLE;
224 }
225
226
227 std::vector<string> const MathMatrixInset::getLabelList() const
228 {
229         std::vector<string> res;
230         for (int row = 0; row < nrows(); ++row)
231                 if (!label_[row].empty() && nonum_[row] != 1)
232                         res.push_back(label_[row]);
233         return res;
234 }
235
236
237 bool MathMatrixInset::numberedType() const
238 {
239         if (getType() == LM_OT_SIMPLE)
240                 return false;
241         for (int row = 0; row < nrows(); ++row)
242                 if (!nonum_[row])
243                         return true;
244         return false;
245 }
246
247
248 void MathMatrixInset::validate(LaTeXFeatures & features) const
249 {
250         features.amsstyle = ams();
251
252         // Validation is necessary only if not using AMS math.
253         // To be safe, we will always run mathedvalidate.
254         //if (features.amsstyle)
255         //  return;
256
257         features.boldsymbol = true;
258         //features.binom      = true;
259
260         MathNestInset::validate(features);
261 }
262
263
264 void MathMatrixInset::header_write(std::ostream & os) const
265 {
266         bool n = numberedType();
267
268         switch (getType()) {
269                 case LM_OT_SIMPLE:
270                         os << '$';
271                         if (cell(0).empty())
272                                 os << ' ';
273                         break;
274
275                 case LM_OT_EQUATION:
276                         if (n)
277                                 os << "\\begin{equation" << star(n) << "}\n"; 
278                         else
279                                 os << "\\[\n"; 
280                         break;
281
282                 case LM_OT_EQNARRAY:
283                         os << "\\begin{eqnarray" << star(n) << "}\n";
284                         break;
285
286                 case LM_OT_ALIGN:
287                         os << "\\begin{align" << star(n) << "}";
288                         break;
289
290                 case LM_OT_ALIGNAT:
291                         os << "\\begin{alignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
292                         break;
293
294                 case LM_OT_XALIGNAT:
295                         os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
296                         break;
297
298                 case LM_OT_XXALIGNAT:
299                         os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\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                 default:
345                         os << "\\end{unknown" << star(n) << "}";
346         }
347 }
348
349
350 void MathMatrixInset::addRow(int row) 
351 {
352         nonum_.insert(nonum_.begin() + row + 1, !numberedType());
353         label_.insert(label_.begin() + row + 1, string());
354         MathGridInset::addRow(row);
355 }
356
357
358 void MathMatrixInset::appendRow()
359 {
360         nonum_.push_back(!numberedType());
361         label_.push_back(string());
362         MathGridInset::appendRow();
363 }
364
365
366 void MathMatrixInset::delRow(int row) 
367 {
368         MathGridInset::delRow(row);
369         nonum_.erase(nonum_.begin() + row);
370         label_.erase(label_.begin() + row);
371 }
372
373
374 void MathMatrixInset::addCol(int col)
375 {
376         switch (getType()) {
377                 case LM_OT_EQUATION:
378                         mutate(LM_OT_EQNARRAY);
379                         break;
380
381                 case LM_OT_EQNARRAY:
382                         mutate(LM_OT_ALIGN);
383                         addCol(col);
384                         break;
385
386                 case LM_OT_ALIGN:
387                         mutate(LM_OT_ALIGNAT);
388                         addCol(col);
389                         break;
390
391                 case LM_OT_ALIGNAT:
392                 case LM_OT_XALIGNAT:
393                 case LM_OT_XXALIGNAT:
394                         MathGridInset::addCol(col);
395                         MathGridInset::addCol(col + 1);
396                         break;
397
398                 default:
399                         break;
400         }
401 }
402
403
404 void MathMatrixInset::delCol(int col)
405 {
406         switch (getType()) {
407                 case LM_OT_ALIGNAT:
408                 case LM_OT_XALIGNAT:
409                 case LM_OT_XXALIGNAT:
410                         MathGridInset::delCol(col + 1);
411                         MathGridInset::delCol(col);
412                         break;
413                 default:
414                         break;
415         }
416 }
417
418
419 string MathMatrixInset::nicelabel(int row) const
420 {
421         if (nonum_[row])
422                 return string();
423         if (label_[row].empty())
424                 return string("(#)");
425         return "(" + label_[row] + ")";
426 }
427
428
429 namespace {
430         MathInsetTypes typecode(string const & s)
431         {
432                 if (s == "equation")
433                         return LM_OT_EQUATION;
434                 if (s == "display")
435                         return LM_OT_EQUATION;
436                 if (s == "eqnarray")
437                         return LM_OT_EQNARRAY;
438                 if (s == "align")
439                         return LM_OT_ALIGN;
440                 if (s == "alignat")
441                         return LM_OT_ALIGN;
442                 if (s == "xalignat")
443                         return LM_OT_XALIGNAT;
444                 if (s == "xxalignat")
445                         return LM_OT_XXALIGNAT;
446                 if (s == "multline")
447                         return LM_OT_MULTLINE;
448                 return LM_OT_SIMPLE;
449         }       
450 }
451
452 void MathMatrixInset::mutate(string const & newtype)
453 {
454         if (newtype == "dump") {
455                 dump();
456                 return;
457         }
458         //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
459         mutate(typecode(newtype));
460 }
461
462 void MathMatrixInset::glueall()
463 {
464         MathArray ar;
465         for (int i = 0; i < nargs(); ++i)
466                 ar.push_back(cell(i));
467         *this = MathMatrixInset(LM_OT_SIMPLE);
468         cell(0) = ar;
469 }
470
471
472 MathInsetTypes MathMatrixInset::getType() const
473 {
474         return objtype_;
475 }
476
477
478 void MathMatrixInset::setType(MathInsetTypes t)
479 {
480         objtype_ = t;
481         setDefaults();
482 }
483
484
485
486 void MathMatrixInset::mutate(MathInsetTypes newtype)
487 {
488         //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
489
490         if (newtype == getType())
491                 return;
492
493         switch (getType()) {
494                 case LM_OT_SIMPLE:
495                         setType(LM_OT_EQUATION);
496                         numbered(0, false);
497                         mutate(newtype);
498                         break;
499
500                 case LM_OT_EQUATION:
501                         switch (newtype) {
502                                 case LM_OT_SIMPLE:
503                                         setType(LM_OT_SIMPLE);
504                                         break;
505
506                                 case LM_OT_ALIGN: 
507                                 case LM_OT_ALIGNAT:
508                                 case LM_OT_XALIGNAT:
509                                 case LM_OT_XXALIGNAT: {
510
511                                         MathGridInset::addCol(1);
512
513                                         // split it "nicely"
514                                         int pos = firstRelOp(cell(0));  
515                                         cell(1) = cell(0);
516                                         cell(0).erase(pos, cell(0).size());
517                                         cell(1).erase(0, pos);
518                                         setType(LM_OT_ALIGN);
519                                         mutate(newtype);
520                                         break;
521                                 }
522
523                                 case LM_OT_EQNARRAY:
524                                 default:
525                                         MathGridInset::addCol(1);
526                                         MathGridInset::addCol(1);
527
528                                         // split it "nicely" on the firest relop
529                                         int pos = firstRelOp(cell(0));  
530                                         cell(1) = cell(0);
531                                         cell(0).erase(pos, cell(0).size());
532                                         cell(1).erase(0, pos);
533
534                                         if (cell(1).size()) {
535                                                 cell(2) = cell(1);
536                                                 cell(1).erase(1, cell(1).size());
537                                                 cell(2).erase(0);
538                                         }
539
540                                         setType(LM_OT_EQNARRAY);
541                                         mutate(newtype);
542                                         break;
543                                 }
544                         break;
545
546                 case LM_OT_EQNARRAY:
547                         switch (newtype) {
548                                 case LM_OT_SIMPLE:
549                                 case LM_OT_EQUATION: {
550                                         // set correct (no)numbering
551                                         bool allnonum = true;
552                                         for (int r = 0; r < nrows(); ++r) {
553                                                 if (!nonum_[r])
554                                                         allnonum = false;
555                                         }
556
557                                         // set first non-empty label
558                                         string label;
559                                         for (int r = 0; r < nrows(); ++r) {
560                                                 if (!label_[r].empty()) {
561                                                         label = label_[r];
562                                                         break;
563                                                 }
564                                         }
565
566                                         glueall();
567
568                                         nonum_[0] = allnonum;
569                                         label_[0] = label;
570                                         mutate(newtype);
571                                         break;
572                                 }
573
574                                 case LM_OT_ALIGN:
575                                 case LM_OT_ALIGNAT:
576                                 case LM_OT_XALIGNAT:
577                                 case LM_OT_XXALIGNAT:
578                                 default: {
579                                         for (int row = 0; row < nrows(); ++row) {
580                                                 int c = 3 * row + 1;
581                                                 cell(c).push_back(cell(c + 1));
582                                         }
583                                         MathGridInset::delCol(2);
584                                         setType(LM_OT_ALIGN);
585                                         mutate(newtype);
586                                         break;
587                                 }
588                         }
589                         break;
590
591                 case LM_OT_ALIGN:
592                         switch (newtype) {
593                                 case LM_OT_SIMPLE:
594                                 case LM_OT_EQUATION:
595                                 case LM_OT_EQNARRAY:
596                                         MathGridInset::addCol(1);
597                                         setType(LM_OT_EQNARRAY);
598                                         mutate(newtype);
599                                         break;
600                                 
601                                 case LM_OT_ALIGNAT:
602                                 case LM_OT_XALIGNAT:
603                                 case LM_OT_XXALIGNAT:
604                                         setType(newtype);
605                                         break;
606
607                                 default:
608                                         lyxerr << "mutation from '" << getType()
609                                                 << "' to '" << newtype << "' not implemented\n";
610                                         break;
611                         }
612                         break;
613
614                 default:
615                         lyxerr << "mutation from '" << getType()
616                                 << "' to '" << newtype << "' not implemented\n";
617         }
618 }