]> git.lyx.org Git - lyx.git/blob - src/mathed/math_hullinset.C
the lfun3 patches (overall cleanup and "localizing" dispatch() in mathed)
[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 "funcrequest.h"
15 #include "Lsstream.h"
16 #include "LaTeXFeatures.h"
17 #include "support/LAssert.h"
18
19 #include <vector>
20
21 using std::vector;
22 using std::max;
23 using std::endl;
24
25 namespace {
26
27         int getCols(string const & type)
28         {
29                 if (type == "eqnarray")
30                         return 3;
31                 if (type == "align")
32                         return 2;
33                 if (type == "alignat")
34                         return 2;
35                 if (type == "xalignat")
36                         return 2;
37                 if (type == "xxalignat")
38                         return 2;
39                 return 1;
40         }
41
42
43         // returns position of first relation operator in the array
44         // used for "intelligent splitting"
45         MathArray::size_type firstRelOp(MathArray const & ar)
46         {
47                 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
48                         if ((*it)->isRelOp())
49                                 return it - ar.begin();
50                 return ar.size();
51         }
52
53
54         char const * star(bool numbered)
55         {
56                 return numbered ? "" : "*";
57         }
58
59
60         int typecode(string const & s)
61         {
62                 if (s == "none")      return 0;
63                 if (s == "simple")    return 1;
64                 if (s == "equation")  return 2;
65                 if (s == "eqnarray")  return 3;
66                 if (s == "align")     return 4;
67                 if (s == "alignat")   return 5;
68                 if (s == "xalignat")  return 6;
69                 if (s == "xxalignat") return 7;
70                 if (s == "multline")  return 8;
71                 if (s == "gather")    return 9;
72                 lyxerr << "unknown hull type '" << s << "'\n";
73                 return 0;
74         }
75
76         bool smaller(string const & s, string const & t)
77         {
78                 return typecode(s) < typecode(t);
79         }
80
81
82 } // end anon namespace
83
84
85 MathHullInset::MathHullInset()
86         : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
87 {
88         setDefaults();
89 }
90
91
92 MathHullInset::MathHullInset(string const & type)
93         : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
94 {
95         setDefaults();
96 }
97
98
99 MathInset * MathHullInset::clone() const
100 {
101         return new MathHullInset(*this);
102 }
103
104
105 MathInset::mode_type MathHullInset::currentMode() const
106 {
107         if (type_ == "none")
108                 return UNDECIDED_MODE;
109         // definitely math mode ...
110         return MATH_MODE;
111 }
112
113
114 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
115 {
116         idx = 0;
117         pos = 0;
118         return true;
119 }
120
121
122 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
123 {
124         idx = nargs() - 1;
125         pos = cell(idx).size();
126         return true;
127 }
128
129
130 char MathHullInset::defaultColAlign(col_type col)
131 {
132         if (type_ == "eqnarray")
133                 return "rcl"[col];
134         if (typecode(type_) >= typecode("align"))
135                 return "rl"[col & 1];
136         return 'c';
137 }
138
139
140 int MathHullInset::defaultColSpace(col_type col)
141 {
142         if (type_ == "align" || type_ == "alignat")
143                 return 0;
144         if (type_ == "xalignat")
145                 return (col & 1) ? 20 : 0;
146         if (type_ == "xxalignat")
147                 return (col & 1) ? 40 : 0;
148         return 0;
149 }
150
151
152 char const * MathHullInset::standardFont() const
153 {
154         if (type_ == "none")
155                 return "lyxnochange";
156         return "mathnormal";
157 }
158
159
160 void MathHullInset::metrics(MathMetricsInfo & mi) const
161 {
162         MathFontSetChanger dummy1(mi.base, standardFont());
163         MathStyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
164
165         // let the cells adjust themselves
166         MathGridInset::metrics(mi);
167
168         if (display()) {
169                 dim_.a += 12;
170                 dim_.d += 12;
171         }
172
173         if (numberedType()) {
174                 MathFontSetChanger dummy(mi.base, "mathbf");
175                 int l = 0;
176                 for (row_type row = 0; row < nrows(); ++row)
177                         l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
178
179                 if (l)
180                         dim_.w += 30 + l;
181         }
182
183         // make it at least as high as the current font
184         int asc = 0;
185         int des = 0;
186         math_font_max_dim(mi.base.font, asc, des);
187         dim_.a = max(dim_.a,  asc);
188         dim_.d = max(dim_.d, des);
189
190         // for markers
191         metricsMarkers2();
192 }
193
194
195 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
196 {
197         MathFontSetChanger dummy1(pi.base, standardFont());
198         MathStyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
199         MathGridInset::draw(pi, x + 1, y);
200
201         if (numberedType()) {
202                 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
203                 for (row_type row = 0; row < nrows(); ++row) {
204                         int const yy = y + rowinfo_[row].offset_;
205                         MathFontSetChanger dummy(pi.base, "mathrm");
206                         drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
207                 }
208         }
209
210         drawMarkers2(pi, x, y);
211 }
212
213
214 void MathHullInset::metricsT(TextMetricsInfo const & mi) const
215 {
216         if (display()) {
217                 MathGridInset::metricsT(mi);
218         } else {
219                 ostringstream os;
220                 WriteStream wi(os, false, true);
221                 write(wi);
222                 dim_.w = os.str().size();
223                 dim_.a = 1;
224                 dim_.d = 0;
225         }
226 }
227
228
229 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
230 {
231         if (display()) {
232                 MathGridInset::drawT(pain, x, y);
233         } else {
234                 ostringstream os;
235                 WriteStream wi(os, false, true);
236                 write(wi);
237                 pain.draw(x, y, os.str().c_str());
238         }
239 }
240
241
242 string MathHullInset::label(row_type row) const
243 {
244         row_type n = nrows();
245         lyx::Assert(row < n);
246         return label_[row];
247 }
248
249
250 void MathHullInset::label(row_type row, string const & label)
251 {
252         //lyxerr << "setting label '" << label << "' for row " << row << endl;
253         label_[row] = label;
254 }
255
256
257 void MathHullInset::numbered(row_type row, bool num)
258 {
259         nonum_[row] = !num;
260 }
261
262
263 bool MathHullInset::numbered(row_type row) const
264 {
265         return !nonum_[row];
266 }
267
268
269 bool MathHullInset::ams() const
270 {
271         return
272                 type_ == "align" ||
273                 type_ == "multline" ||
274                 type_ == "gather" ||
275                 type_ == "alignat" ||
276                 type_ == "xalignat" ||
277                 type_ == "xxalignat";
278 }
279
280
281 bool MathHullInset::display() const
282 {
283         return type_ != "simple" && type_ != "none";
284 }
285
286
287 void MathHullInset::getLabelList(std::vector<string> & labels) const
288 {
289         for (row_type row = 0; row < nrows(); ++row)
290                 if (!label_[row].empty() && nonum_[row] != 1)
291                         labels.push_back(label_[row]);
292 }
293
294
295 bool MathHullInset::numberedType() const
296 {
297         if (type_ == "none")
298                 return false;
299         if (type_ == "simple")
300                 return false;
301         if (type_ == "xxalignat")
302                 return false;
303         for (row_type row = 0; row < nrows(); ++row)
304                 if (!nonum_[row])
305                         return true;
306         return false;
307 }
308
309
310 void MathHullInset::validate(LaTeXFeatures & features) const
311 {
312         if (ams())
313                 features.require("amsmath");
314
315
316         // Validation is necessary only if not using AMS math.
317         // To be safe, we will always run mathedvalidate.
318         //if (features.amsstyle)
319         //  return;
320
321         features.require("boldsymbol");
322         //features.binom      = true;
323
324         MathNestInset::validate(features);
325 }
326
327
328 void MathHullInset::header_write(WriteStream & os) const
329 {
330         bool n = numberedType();
331
332         if (type_ == "none")
333                 ;
334
335         else if (type_ == "simple") {
336                 os << '$';
337                 if (cell(0).empty())
338                         os << ' ';
339         }
340
341         else if (type_ == "equation") {
342                 if (n)
343                         os << "\\begin{equation" << star(n) << "}\n";
344                 else
345                         os << "\\[\n";
346         }
347
348         else if (type_ == "eqnarray" || type_ == "align")
349                         os << "\\begin{" << type_ << star(n) << "}\n";
350
351         else if (type_ == "alignat" || type_ == "xalignat") 
352                 os << "\\begin{" << type_ << star(n) << "}"
353                   << "{" << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
354  
355         else if (type_ == "xxalignat") 
356                 os << "\\begin{" << type_ << "}"
357                   << "{" << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
358  
359         else if (type_ == "multline" || type_ == "gather") 
360                 os << "\\begin{" << type_ << "}\n";
361
362         else 
363                 os << "\\begin{unknown" << star(n) << "}";
364 }
365
366
367 void MathHullInset::footer_write(WriteStream & os) const
368 {
369         bool n = numberedType();
370
371         if (type_ == "none")
372                 os << "\n";
373
374         else if (type_ == "simple")
375                 os << '$';
376
377         else if (type_ == "equation")
378                 if (n)
379                         os << "\\end{equation" << star(n) << "}\n";
380                 else
381                         os << "\\]\n";
382
383         else if (type_ == "eqnarray" || type_ == "align" || type_ == "alignat"
384               || type_ == "xalignat")
385                 os << "\n\\end{" << type_ << star(n) << "}\n";
386
387         else if (type_ == "xxalignat" || type_ == "multline" || type_ == "gather")
388                 os << "\n\\end{" << type_ << "}\n";
389
390         else
391                 os << "\\end{unknown" << star(n) << "}";
392 }
393
394
395 void MathHullInset::addRow(row_type row)
396 {
397         nonum_.insert(nonum_.begin() + row + 1, !numberedType());
398         label_.insert(label_.begin() + row + 1, string());
399         MathGridInset::addRow(row);
400 }
401
402
403 void MathHullInset::delRow(row_type row)
404 {
405         MathGridInset::delRow(row);
406         nonum_.erase(nonum_.begin() + row);
407         label_.erase(label_.begin() + row);
408 }
409
410
411 void MathHullInset::addFancyCol(col_type col)
412 {
413         if (type_ == "equation")
414                 mutate("eqnarray");
415         
416         else if (type_ == "eqnarray") {
417                 mutate("align");
418                 addFancyCol(col);
419         }
420
421         else if (type_ == "align" || type_ == "alignat"
422               || type_ == "xalignat" || type_ == "xxalignat") 
423                 MathGridInset::addCol(col);
424 }
425
426
427 void MathHullInset::delFancyCol(col_type col)
428 {
429         if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") 
430                 MathGridInset::delCol(col);
431 }
432
433
434 string MathHullInset::nicelabel(row_type row) const
435 {
436         if (nonum_[row])
437                 return string();
438         if (label_[row].empty())
439                 return string("(#)");
440         return "(" + label_[row] + ")";
441 }
442
443
444 void MathHullInset::glueall()
445 {
446         MathArray ar;
447         for (idx_type i = 0; i < nargs(); ++i)
448                 ar.append(cell(i));
449         *this = MathHullInset("simple");
450         cell(0) = ar;
451         setDefaults();
452 }
453
454
455 string const & MathHullInset::getType() const
456 {
457         return type_;
458 }
459
460
461 void MathHullInset::setType(string const & type)
462 {
463         type_ = type;
464         setDefaults();
465 }
466
467
468
469 void MathHullInset::mutate(string const & newtype)
470 {
471         //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
472
473         // we try to move along the chain
474         // none <-> simple <-> equation <-> eqnarray 
475
476         if (newtype == "dump") {
477                 dump();
478         }
479
480         else if (newtype == type_) {
481                 // done
482         }
483
484         else if (type_ == "none") {
485                 setType("simple");
486                 numbered(0, false);
487                 mutate(newtype);
488         }
489
490         else if (type_ == "simple") {
491                 if (newtype == "none") {
492                         setType("none");
493                 } else {
494                         setType("equation");
495                         numbered(0, false);
496                         mutate(newtype);
497                 }
498         }
499
500         else if (type_ == "equation") {
501                 if (smaller(newtype, type_)) {
502                         setType("simple");
503                         mutate(newtype);
504                 } else if (newtype == "eqnarray") {
505                         MathGridInset::addCol(1);
506                         MathGridInset::addCol(1);
507
508                         // split it "nicely" on the firest relop
509                         pos_type pos = firstRelOp(cell(0));
510                         cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
511                         cell(0).erase(pos, cell(0).size());
512
513                         if (cell(1).size()) {
514                                 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
515                                 cell(1).erase(1, cell(1).size());
516                         }
517                         setType("eqnarray");
518                         mutate(newtype);
519                 } else if (newtype == "multline" || newtype == "gather") {
520                         setType(newtype);
521                         numbered(0, false);
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 }
649
650
651 MathInset::result_type MathHullInset::dispatch
652         (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
653 {
654         switch (cmd.action) {
655
656                 case LFUN_BREAKLINE:
657                         if (type_ == "simple" || type_ == "equation") {
658                                 mutate("eqnarray");
659                                 idx = 1;
660                                 pos = 0;
661                                 return DISPATCHED_POP;
662                         } 
663                         return MathGridInset::dispatch(cmd, idx, pos);
664
665                 case LFUN_MATH_NUMBER:
666                         //lyxerr << "toggling all numbers\n";
667                         if (display()) {
668                                 //bv->lockedInsetStoreUndo(Undo::INSERT);
669                                 bool old = numberedType();
670                                 for (row_type row = 0; row < nrows(); ++row)
671                                         numbered(row, !old);
672                                 //bv->owner()->message(old ? _("No number") : _("Number"));
673                                 //updateLocal(bv, true);
674                         }
675                         return DISPATCHED; 
676                 
677                 case LFUN_MATH_NONUMBER:        
678                         if (display()) {
679                                 //bv->lockedInsetStoreUndo(Undo::INSERT);
680                                 bool old = numbered(row(idx));
681                                 //bv->owner()->message(old ? _("No number") : _("Number"));
682                                 numbered(row(idx), !old);
683                                 //updateLocal(bv, true);
684                         }
685                         return DISPATCHED; 
686
687                 default:
688                         return UNDISPATCHED; 
689         }
690         return UNDISPATCHED;
691 }
692