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