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