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