]> git.lyx.org Git - features.git/blob - src/mathed/InsetMathHull.cpp
add comment about std::vector<bool>
[features.git] / src / mathed / InsetMathHull.cpp
1 /**
2  * \file InsetMathHull.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathArray.h"
14 #include "InsetMathChar.h"
15 #include "InsetMathColor.h"
16 #include "MathData.h"
17 #include "InsetMathDelim.h"
18 #include "MathExtern.h"
19 #include "MathFactory.h"
20 #include "InsetMathHull.h"
21 #include "MathStream.h"
22 #include "MathParser.h"
23 #include "InsetMathSpace.h"
24 #include "MathStream.h"
25 #include "MathSupport.h"
26 #include "InsetMathRef.h"
27
28 #include "Buffer.h"
29 #include "BufferParams.h"
30 #include "BufferView.h"
31 #include "CutAndPaste.h"
32 #include "FuncStatus.h"
33 #include "LaTeXFeatures.h"
34 #include "Cursor.h"
35 #include "DispatchResult.h"
36 #include "FuncRequest.h"
37 #include "LyXRC.h"
38 #include "OutputParams.h"
39 #include "ParIterator.h"
40 #include "sgml.h"
41 #include "Text.h"
42 #include "TextPainter.h"
43 #include "TocBackend.h"
44
45 #include "insets/RenderPreview.h"
46 #include "insets/InsetLabel.h"
47
48 #include "graphics/PreviewImage.h"
49 #include "graphics/PreviewLoader.h"
50
51 #include "frontends/Painter.h"
52
53 #include "support/debug.h"
54 #include "support/gettext.h"
55 #include "support/lstrings.h"
56
57 #include <sstream>
58
59 using namespace std;
60 using namespace lyx::support;
61
62 namespace lyx {
63
64 using cap::grabAndEraseSelection;
65
66 namespace {
67
68         int getCols(HullType type)
69         {
70                 switch (type) {
71                         case hullEqnArray:
72                                 return 3;
73                         case hullAlign:
74                         case hullFlAlign:
75                         case hullAlignAt:
76                         case hullXAlignAt:
77                         case hullXXAlignAt:
78                                 return 2;
79                         default:
80                                 return 1;
81                 }
82         }
83
84
85         // returns position of first relation operator in the array
86         // used for "intelligent splitting"
87         size_t firstRelOp(MathData const & ar)
88         {
89                 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
90                         if ((*it)->isRelOp())
91                                 return it - ar.begin();
92                 return ar.size();
93         }
94
95
96         char const * star(bool numbered)
97         {
98                 return numbered ? "" : "*";
99         }
100
101
102 } // end anon namespace
103
104
105 HullType hullType(docstring const & s)
106 {
107         if (s == "none")      return hullNone;
108         if (s == "simple")    return hullSimple;
109         if (s == "equation")  return hullEquation;
110         if (s == "eqnarray")  return hullEqnArray;
111         if (s == "align")     return hullAlign;
112         if (s == "alignat")   return hullAlignAt;
113         if (s == "xalignat")  return hullXAlignAt;
114         if (s == "xxalignat") return hullXXAlignAt;
115         if (s == "multline")  return hullMultline;
116         if (s == "gather")    return hullGather;
117         if (s == "flalign")   return hullFlAlign;
118         lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
119         return HullType(-1);
120 }
121
122
123 docstring hullName(HullType type)
124 {
125         switch (type) {
126                 case hullNone:       return from_ascii("none");
127                 case hullSimple:     return from_ascii("simple");
128                 case hullEquation:   return from_ascii("equation");
129                 case hullEqnArray:   return from_ascii("eqnarray");
130                 case hullAlign:      return from_ascii("align");
131                 case hullAlignAt:    return from_ascii("alignat");
132                 case hullXAlignAt:   return from_ascii("xalignat");
133                 case hullXXAlignAt:  return from_ascii("xxalignat");
134                 case hullMultline:   return from_ascii("multline");
135                 case hullGather:     return from_ascii("gather");
136                 case hullFlAlign:    return from_ascii("flalign");
137                 default:
138                         lyxerr << "unknown hull type '" << type << "'" << endl;
139                         return from_ascii("none");
140         }
141 }
142
143 static InsetLabel * dummy_pointer = 0;
144
145 InsetMathHull::InsetMathHull()
146         : InsetMathGrid(1, 1), type_(hullNone), nonum_(1, false), label_(1, dummy_pointer),
147           preview_(new RenderPreview(this))
148 {
149         //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
150         //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
151         //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
152         //lyxerr << "sizeof FontInfo: " << sizeof(FontInfo) << endl;
153         initMath();
154         setDefaults();
155 }
156
157
158 InsetMathHull::InsetMathHull(HullType type)
159         : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1, false), label_(1, dummy_pointer),
160           preview_(new RenderPreview(this))
161 {
162         initMath();
163         setDefaults();
164 }
165
166
167 InsetMathHull::InsetMathHull(InsetMathHull const & other)
168 {
169         operator=(other);
170 }
171
172
173 InsetMathHull::~InsetMathHull()
174 {
175         for (size_t i = 0; i < label_.size(); ++i)
176                 delete label_[i];
177 }
178
179
180 Inset * InsetMathHull::clone() const
181 {
182         return new InsetMathHull(*this);
183 }
184
185
186 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
187 {
188         if (this == &other)
189                 return *this;
190         InsetMathGrid::operator=(other);
191         type_  = other.type_;
192         nonum_ = other.nonum_;
193         for (size_t i = 0; i < label_.size(); ++i)
194                 delete label_[i];
195         label_ = other.label_;
196         for (size_t i = 0; i != label_.size(); ++i) {
197                 if (label_[i])
198                         label_[i] = new InsetLabel(*label_[i]);
199         }
200         preview_.reset(new RenderPreview(*other.preview_, this));
201
202         return *this;
203 }
204
205
206 void InsetMathHull::setBuffer(Buffer & buffer)
207 {
208         buffer_ = &buffer;
209         for (idx_type i = 0, n = nargs(); i != n; ++i) {
210                 MathData & data = cell(i);
211                 for (size_t j = 0; j != data.size(); ++j)
212                         data[j].nucleus()->setBuffer(buffer);
213         }
214
215         for (size_t i = 0; i != label_.size(); ++i) {
216                 if (label_[i])
217                         label_[i]->setBuffer(buffer);
218         }
219 }
220
221
222 void InsetMathHull::updateLabels(ParIterator const & it)
223 {
224         if (!buffer_) {
225                 //FIXME: buffer_ should be set at creation for this inset! Problem is
226                 // This inset is created at too many places (see Parser::parse1() in
227                 // MathParser.cpp).
228                 return;
229         }
230         for (size_t i = 0; i != label_.size(); ++i) {
231                 if (label_[i])
232                         label_[i]->updateLabels(it);
233         }
234 }
235
236
237 void InsetMathHull::addToToc(ParConstIterator const & pit) const
238 {
239         if (!buffer_) {
240                 //FIXME: buffer_ should be set at creation for this inset! Problem is
241                 // This inset is created at too many places (see Parser::parse1() in
242                 // MathParser.cpp).
243                 return;
244         }
245
246         // FIXME: it would be way better to directly use InsetLabel instead of this
247         // label list. But it should be possible to copy&paste the code in
248         // InsetLabel::addToToc() anyway.
249
250         Toc & toc = buffer().tocBackend().toc("equation");
251
252         for (row_type row = 0; row != nrows(); ++row) {
253                 if (nonum_[row])
254                         continue;
255                 if (label_[row])
256                         label_[row]->addToToc(pit);
257                 toc.push_back(TocItem(pit, 0, nicelabel(row)));
258         }
259 }
260
261
262 Inset * InsetMathHull::editXY(Cursor & cur, int x, int y)
263 {
264         if (use_preview_) {
265                 edit(cur, true);
266                 return this;
267         }
268         return InsetMathNest::editXY(cur, x, y);
269 }
270
271
272 InsetMath::mode_type InsetMathHull::currentMode() const
273 {
274         if (type_ == hullNone)
275                 return UNDECIDED_MODE;
276         // definitely math mode ...
277         return MATH_MODE;
278 }
279
280
281 bool InsetMathHull::idxFirst(Cursor & cur) const
282 {
283         cur.idx() = 0;
284         cur.pos() = 0;
285         return true;
286 }
287
288
289 bool InsetMathHull::idxLast(Cursor & cur) const
290 {
291         cur.idx() = nargs() - 1;
292         cur.pos() = cur.lastpos();
293         return true;
294 }
295
296
297 char InsetMathHull::defaultColAlign(col_type col)
298 {
299         if (type_ == hullEqnArray)
300                 return "rcl"[col];
301         if (type_ == hullGather)
302                 return 'c';
303         if (type_ >= hullAlign)
304                 return "rl"[col & 1];
305         return 'c';
306 }
307
308
309 int InsetMathHull::defaultColSpace(col_type col)
310 {
311         if (type_ == hullAlign || type_ == hullAlignAt)
312                 return 0;
313         if (type_ == hullXAlignAt)
314                 return (col & 1) ? 20 : 0;
315         if (type_ == hullXXAlignAt || type_ == hullFlAlign)
316                 return (col & 1) ? 40 : 0;
317         return 0;
318 }
319
320
321 docstring InsetMathHull::standardFont() const
322 {
323         return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal");
324 }
325
326
327 bool InsetMathHull::previewState(BufferView * bv) const
328 {
329         if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
330                 graphics::PreviewImage const * pimage =
331                         preview_->getPreviewImage(bv->buffer());
332                 return pimage && pimage->image();
333         }
334         return false;
335 }
336
337
338 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
339 {
340         if (previewState(mi.base.bv)) {
341                 preview_->metrics(mi, dim);
342                 // insert a one pixel gap in front of the formula
343                 dim.wid += 1;
344                 if (display())
345                         dim.des += displayMargin();
346                 // Cache the inset dimension. 
347                 setDimCache(mi, dim);
348                 return;
349         }
350
351         FontSetChanger dummy1(mi.base, standardFont());
352         StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
353
354         // let the cells adjust themselves
355         InsetMathGrid::metrics(mi, dim);
356
357         if (display()) {
358                 dim.asc += displayMargin();
359                 dim.des += displayMargin();
360         }
361
362         if (numberedType()) {
363                 FontSetChanger dummy(mi.base, from_ascii("mathbf"));
364                 int l = 0;
365                 for (row_type row = 0; row < nrows(); ++row)
366                         l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
367
368                 if (l)
369                         dim.wid += 30 + l;
370         }
371
372         // make it at least as high as the current font
373         int asc = 0;
374         int des = 0;
375         math_font_max_dim(mi.base.font, asc, des);
376         dim.asc = max(dim.asc, asc);
377         dim.des = max(dim.des, des);
378         // Cache the inset dimension.
379         // FIXME: This will overwrite InsetMathGrid dimension, is that OK?
380         setDimCache(mi, dim);
381 }
382
383
384 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
385 {
386         use_preview_ = previewState(pi.base.bv);
387         Dimension const dim = dimension(*pi.base.bv);
388
389         // background of mathed under focus is not painted because
390         // selection at the top level of nested inset is difficult to handle.
391         if (!editing(pi.base.bv))
392                 pi.pain.fillRectangle(x + 1, y - dim.asc + 1, dim.wid - 2,
393                                 dim.asc + dim.des - 1, Color_mathbg);
394
395         if (use_preview_) {
396                 // one pixel gap in front
397                 preview_->draw(pi, x + 1, y);
398                 setPosCache(pi, x, y);
399                 return;
400         }
401
402         FontSetChanger dummy1(pi.base, standardFont());
403         StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
404         InsetMathGrid::draw(pi, x + 1, y);
405
406         if (numberedType()) {
407                 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
408                 for (row_type row = 0; row < nrows(); ++row) {
409                         int const yy = y + rowinfo_[row].offset_;
410                         FontSetChanger dummy(pi.base, from_ascii("mathrm"));
411                         docstring const nl = nicelabel(row);
412                         pi.draw(xx, yy, nl);
413                 }
414         }
415         setPosCache(pi, x, y);
416 }
417
418
419 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
420 {
421         if (display()) {
422                 InsetMathGrid::metricsT(mi, dim);
423         } else {
424                 odocstringstream os;
425                 WriteStream wi(os, false, true);
426                 write(wi);
427                 dim.wid = os.str().size();
428                 dim.asc = 1;
429                 dim.des = 0;
430         }
431 }
432
433
434 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
435 {
436         if (display()) {
437                 InsetMathGrid::drawT(pain, x, y);
438         } else {
439                 odocstringstream os;
440                 WriteStream wi(os, false, true);
441                 write(wi);
442                 pain.draw(x, y, os.str().c_str());
443         }
444 }
445
446
447 namespace {
448
449 docstring const latex_string(InsetMathHull const & inset)
450 {
451         odocstringstream ls;
452         WriteStream wi(ls, false, false);
453         inset.write(wi);
454         return ls.str();
455 }
456
457 } // namespace anon
458
459
460 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
461 {
462         if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
463                 docstring const snippet = latex_string(*this);
464                 preview_->addPreview(snippet, ploader);
465         }
466 }
467
468
469 bool InsetMathHull::notifyCursorLeaves(Cursor const & /*old*/, Cursor & cur)
470 {
471         if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
472                 Buffer const & buffer = cur.buffer();
473                 docstring const snippet = latex_string(*this);
474                 preview_->addPreview(snippet, buffer);
475                 preview_->startLoading(buffer);
476                 cur.updateFlags(Update::Force);
477         }
478         return false;
479 }
480
481
482 docstring InsetMathHull::label(row_type row) const
483 {
484         BOOST_ASSERT(row < nrows());
485         if (InsetLabel * il = label_[row])
486                 return il->screenLabel();
487         return docstring();
488 }
489
490
491 void InsetMathHull::label(row_type row, docstring const & label)
492 {
493         //lyxerr << "setting label '" << label << "' for row " << row << endl;
494         if (label_[row]) {
495                 label_[row]->updateCommand(label);
496                 return;
497         }
498         InsetCommandParams p(LABEL_CODE);
499         p["name"] = label;
500         label_[row] = new InsetLabel(p);
501         if (buffer_)
502                 label_[row]->setBuffer(buffer());
503 }
504
505
506 void InsetMathHull::numbered(row_type row, bool num)
507 {
508         nonum_[row] = !num;
509         if (nonum_[row] && label_[row]) {
510                 delete label_[row];
511                 label_[row] = 0;
512         }
513 }
514
515
516 bool InsetMathHull::numbered(row_type row) const
517 {
518         return !nonum_[row];
519 }
520
521
522 bool InsetMathHull::ams() const
523 {
524         return
525                 type_ == hullAlign ||
526                 type_ == hullFlAlign ||
527                 type_ == hullMultline ||
528                 type_ == hullGather ||
529                 type_ == hullAlignAt ||
530                 type_ == hullXAlignAt ||
531                 type_ == hullXXAlignAt;
532 }
533
534
535 Inset::DisplayType InsetMathHull::display() const
536 {
537         return (type_ != hullSimple && type_ != hullNone) ? AlignCenter : Inline;
538 }
539
540
541 void InsetMathHull::getLabelList(vector<docstring> & labels) const
542 {
543         for (row_type row = 0; row < nrows(); ++row)
544                 if (label_[row] && !nonum_[row])
545                         labels.push_back(label_[row]->screenLabel());
546 }
547
548
549 bool InsetMathHull::numberedType() const
550 {
551         if (type_ == hullNone)
552                 return false;
553         if (type_ == hullSimple)
554                 return false;
555         if (type_ == hullXXAlignAt)
556                 return false;
557         for (row_type row = 0; row < nrows(); ++row)
558                 if (!nonum_[row])
559                         return true;
560         return false;
561 }
562
563
564 void InsetMathHull::validate(LaTeXFeatures & features) const
565 {
566         if (ams())
567                 features.require("amsmath");
568
569
570         // Validation is necessary only if not using AMS math.
571         // To be safe, we will always run mathedvalidate.
572         //if (features.amsstyle)
573         //  return;
574
575         features.require("boldsymbol");
576         //features.binom      = true;
577
578         InsetMathGrid::validate(features);
579 }
580
581
582 void InsetMathHull::header_write(WriteStream & os) const
583 {
584         bool n = numberedType();
585
586         switch(type_) {
587         case hullNone:
588                 break;
589
590         case hullSimple:
591                 os << '$';
592                 if (cell(0).empty())
593                         os << ' ';
594                 break;
595
596         case hullEquation:
597                 if (n)
598                         os << "\\begin{equation" << star(n) << "}\n";
599                 else
600                         os << "\\[\n";
601                 break;
602
603         case hullEqnArray:
604         case hullAlign:
605         case hullFlAlign:
606         case hullGather:
607         case hullMultline:
608                 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
609                 break;
610
611         case hullAlignAt:
612         case hullXAlignAt:
613                 os << "\\begin{" << hullName(type_) << star(n) << '}'
614                   << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
615                 break;
616
617         case hullXXAlignAt:
618                 os << "\\begin{" << hullName(type_) << '}'
619                   << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
620                 break;
621
622         default:
623                 os << "\\begin{unknown" << star(n) << '}';
624                 break;
625         }
626 }
627
628
629 void InsetMathHull::footer_write(WriteStream & os) const
630 {
631         bool n = numberedType();
632
633         switch(type_) {
634         case hullNone:
635                 os << "\n";
636                 break;
637
638         case hullSimple:
639                 os << '$';
640                 break;
641
642         case hullEquation:
643                 if (n)
644                         os << "\\end{equation" << star(n) << "}\n";
645                 else
646                         os << "\\]\n";
647                 break;
648
649         case hullEqnArray:
650         case hullAlign:
651         case hullFlAlign:
652         case hullAlignAt:
653         case hullXAlignAt:
654         case hullGather:
655         case hullMultline:
656                 os << "\\end{" << hullName(type_) << star(n) << "}\n";
657                 break;
658
659         case hullXXAlignAt:
660                 os << "\\end{" << hullName(type_) << "}\n";
661                 break;
662
663         default:
664                 os << "\\end{unknown" << star(n) << '}';
665                 break;
666         }
667 }
668
669
670 bool InsetMathHull::rowChangeOK() const
671 {
672         return
673                 type_ == hullEqnArray || type_ == hullAlign ||
674                 type_ == hullFlAlign || type_ == hullAlignAt ||
675                 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
676                 type_ == hullGather || type_ == hullMultline;
677 }
678
679
680 bool InsetMathHull::colChangeOK() const
681 {
682         return
683                 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
684                 type_ == hullXAlignAt || type_ == hullXXAlignAt;
685 }
686
687
688 void InsetMathHull::addRow(row_type row)
689 {
690         if (!rowChangeOK())
691                 return;
692         nonum_.insert(nonum_.begin() + row + 1, !numberedType());
693         label_.insert(label_.begin() + row + 1, dummy_pointer);
694         InsetMathGrid::addRow(row);
695 }
696
697
698 void InsetMathHull::swapRow(row_type row)
699 {
700         if (nrows() <= 1)
701                 return;
702         if (row + 1 == nrows())
703                 --row;
704         // gcc implements the standard std::vector<bool> which is *not* a container:
705         //   http://www.gotw.ca/publications/N1185.pdf
706         // As a results, it doesn't like this:
707         //      swap(nonum_[row], nonum_[row + 1]);
708         // so we do it manually:
709         bool const b = nonum_[row];
710         nonum_[row] = nonum_[row + 1];
711         nonum_[row + 1] = b;
712         swap(label_[row], label_[row + 1]);
713         InsetMathGrid::swapRow(row);
714 }
715
716
717 void InsetMathHull::delRow(row_type row)
718 {
719         if (nrows() <= 1 || !rowChangeOK())
720                 return;
721         InsetMathGrid::delRow(row);
722         // The last dummy row has no number info nor a label.
723         // Test nrows() + 1 because we have already erased the row.
724         if (row == nrows() + 1)
725                 row--;
726         nonum_.erase(nonum_.begin() + row);
727         delete label_[row];
728         label_.erase(label_.begin() + row);
729 }
730
731
732 void InsetMathHull::addCol(col_type col)
733 {
734         if (!colChangeOK())
735                 return;
736         InsetMathGrid::addCol(col);
737 }
738
739
740 void InsetMathHull::delCol(col_type col)
741 {
742         if (ncols() <= 1 || !colChangeOK())
743                 return;
744         InsetMathGrid::delCol(col);
745 }
746
747
748 docstring InsetMathHull::nicelabel(row_type row) const
749 {
750         if (nonum_[row])
751                 return docstring();
752         if (!label_[row])
753                 return from_ascii("(#)");
754         return '(' + label_[row]->screenLabel() + ')';
755 }
756
757
758 void InsetMathHull::glueall()
759 {
760         MathData ar;
761         for (idx_type i = 0; i < nargs(); ++i)
762                 ar.append(cell(i));
763         *this = InsetMathHull(hullSimple);
764         cell(0) = ar;
765         setDefaults();
766 }
767
768
769 void InsetMathHull::splitTo2Cols()
770 {
771         BOOST_ASSERT(ncols() == 1);
772         InsetMathGrid::addCol(1);
773         for (row_type row = 0; row < nrows(); ++row) {
774                 idx_type const i = 2 * row;
775                 pos_type pos = firstRelOp(cell(i));
776                 cell(i + 1) = MathData(cell(i).begin() + pos, cell(i).end());
777                 cell(i).erase(pos, cell(i).size());
778         }
779 }
780
781
782 void InsetMathHull::splitTo3Cols()
783 {
784         BOOST_ASSERT(ncols() < 3);
785         if (ncols() < 2)
786                 splitTo2Cols();
787         InsetMathGrid::addCol(2);
788         for (row_type row = 0; row < nrows(); ++row) {
789                 idx_type const i = 3 * row + 1;
790                 if (cell(i).size()) {
791                         cell(i + 1) = MathData(cell(i).begin() + 1, cell(i).end());
792                         cell(i).erase(1, cell(i).size());
793                 }
794         }
795 }
796
797
798 void InsetMathHull::changeCols(col_type cols)
799 {
800         if (ncols() == cols)
801                 return;
802         else if (ncols() < cols) {
803                 // split columns
804                 if (cols < 3)
805                         splitTo2Cols();
806                 else {
807                         splitTo3Cols();
808                         while (ncols() < cols)
809                                 InsetMathGrid::addCol(ncols());
810                 }
811                 return;
812         }
813
814         // combine columns
815         for (row_type row = 0; row < nrows(); ++row) {
816                 idx_type const i = row * ncols();
817                 for (col_type col = cols; col < ncols(); ++col) {
818                         cell(i + cols - 1).append(cell(i + col));
819                 }
820         }
821         // delete columns
822         while (ncols() > cols) {
823                 InsetMathGrid::delCol(ncols() - 1);
824         }
825 }
826
827
828 HullType InsetMathHull::getType() const
829 {
830         return type_;
831 }
832
833
834 void InsetMathHull::setType(HullType type)
835 {
836         type_ = type;
837         setDefaults();
838 }
839
840
841 void InsetMathHull::mutate(HullType newtype)
842 {
843         //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
844
845         // we try to move along the chain
846         // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
847         //                                     ^                                     |
848         //                                     +-------------------------------------+
849         // we use eqnarray as intermediate type for mutations that are not
850         // directly supported because it handles labels and numbering for
851         // "down mutation".
852
853         if (newtype == type_) {
854                 // done
855         }
856
857         else if (newtype < hullNone) {
858                 // unknown type
859                 dump();
860         }
861
862         else if (type_ == hullNone) {
863                 setType(hullSimple);
864                 numbered(0, false);
865                 mutate(newtype);
866         }
867
868         else if (type_ == hullSimple) {
869                 if (newtype == hullNone) {
870                         setType(hullNone);
871                         numbered(0, false);
872                 } else {
873                         setType(hullEquation);
874                         numbered(0, false);
875                         mutate(newtype);
876                 }
877         }
878
879         else if (type_ == hullEquation) {
880                 if (newtype < type_) {
881                         setType(hullSimple);
882                         numbered(0, false);
883                         mutate(newtype);
884                 } else if (newtype == hullEqnArray) {
885                         // split it "nicely" on the first relop
886                         splitTo3Cols();
887                         setType(hullEqnArray);
888                 } else if (newtype == hullMultline || newtype == hullGather) {
889                         setType(newtype);
890                 } else {
891                         // split it "nicely"
892                         splitTo2Cols();
893                         setType(hullAlign);
894                         mutate(newtype);
895                 }
896         }
897
898         else if (type_ == hullEqnArray) {
899                 if (newtype < type_) {
900                         // set correct (no)numbering
901                         nonum_[0] = true;
902                         for (row_type row = 0; row < nrows(); ++row) {
903                                 if (!nonum_[row]) {
904                                         nonum_[0] = false;
905                                         break;
906                                 }
907                         }
908
909                         // set first non-empty label
910                         for (row_type row = 0; row < nrows(); ++row) {
911                                 if (label_[row]) {
912                                         label_[0] = label_[row];
913                                         break;
914                                 }
915                         }
916
917                         glueall();
918                         mutate(newtype);
919                 } else { // align & Co.
920                         changeCols(2);
921                         setType(hullAlign);
922                         mutate(newtype);
923                 }
924         }
925
926         else if (type_ ==  hullAlign || type_ == hullAlignAt ||
927                  type_ == hullXAlignAt || type_ == hullFlAlign) {
928                 if (newtype < hullAlign) {
929                         changeCols(3);
930                         setType(hullEqnArray);
931                         mutate(newtype);
932                 } else if (newtype == hullGather || newtype == hullMultline) {
933                         changeCols(1);
934                         setType(newtype);
935                 } else if (newtype ==   hullXXAlignAt) {
936                         for (row_type row = 0; row < nrows(); ++row)
937                                 numbered(row, false);
938                         setType(newtype);
939                 } else {
940                         setType(newtype);
941                 }
942         }
943
944         else if (type_ == hullXXAlignAt) {
945                 for (row_type row = 0; row < nrows(); ++row)
946                         numbered(row, false);
947                 if (newtype < hullAlign) {
948                         changeCols(3);
949                         setType(hullEqnArray);
950                         mutate(newtype);
951                 } else if (newtype == hullGather || newtype == hullMultline) {
952                         changeCols(1);
953                         setType(newtype);
954                 } else {
955                         setType(newtype);
956                 }
957         }
958
959         else if (type_ == hullMultline || type_ == hullGather) {
960                 if (newtype == hullGather || newtype == hullMultline)
961                         setType(newtype);
962                 else if (newtype == hullAlign || newtype == hullFlAlign  ||
963                          newtype == hullAlignAt || newtype == hullXAlignAt) {
964                         splitTo2Cols();
965                         setType(newtype);
966                 } else if (newtype ==   hullXXAlignAt) {
967                         splitTo2Cols();
968                         for (row_type row = 0; row < nrows(); ++row)
969                                 numbered(row, false);
970                         setType(newtype);
971                 } else {
972                         splitTo3Cols();
973                         setType(hullEqnArray);
974                         mutate(newtype);
975                 }
976         }
977
978         else {
979                 lyxerr << "mutation from '" << to_utf8(hullName(type_))
980                        << "' to '" << to_utf8(hullName(newtype))
981                        << "' not implemented" << endl;
982         }
983 }
984
985
986 docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
987 {
988         docstring res;
989         if (numberedType()) {
990                 if (label_[row] && !nonum_[row])
991                         res += "\\label{" + label_[row]->getParam("name") + '}';
992                 if (nonum_[row] && (type_ != hullMultline))
993                         res += "\\nonumber ";
994         }
995         return res + InsetMathGrid::eolString(row, emptyline, fragile);
996 }
997
998
999 void InsetMathHull::write(WriteStream & os) const
1000 {
1001         header_write(os);
1002         InsetMathGrid::write(os);
1003         footer_write(os);
1004 }
1005
1006
1007 void InsetMathHull::normalize(NormalStream & os) const
1008 {
1009         os << "[formula " << hullName(type_) << ' ';
1010         InsetMathGrid::normalize(os);
1011         os << "] ";
1012 }
1013
1014
1015 void InsetMathHull::mathmlize(MathStream & os) const
1016 {
1017         InsetMathGrid::mathmlize(os);
1018 }
1019
1020
1021 void InsetMathHull::infoize(odocstream & os) const
1022 {
1023         os << "Type: " << hullName(type_);
1024 }
1025
1026
1027 void InsetMathHull::check() const
1028 {
1029         BOOST_ASSERT(nonum_.size() == nrows());
1030         BOOST_ASSERT(label_.size() == nrows());
1031 }
1032
1033
1034 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
1035 {
1036         docstring dlang;
1037         docstring extra;
1038         idocstringstream iss(func.argument());
1039         iss >> dlang >> extra;
1040         if (extra.empty())
1041                 extra = from_ascii("noextra");
1042         string const lang = to_ascii(dlang);
1043
1044         // FIXME: temporarily disabled
1045         //if (cur.selection()) {
1046         //      MathData ar;
1047         //      selGet(cur.ar);
1048         //      lyxerr << "use selection: " << ar << endl;
1049         //      insert(pipeThroughExtern(lang, extra, ar));
1050         //      return;
1051         //}
1052
1053         MathData eq;
1054         eq.push_back(MathAtom(new InsetMathChar('=')));
1055
1056         // go to first item in line
1057         cur.idx() -= cur.idx() % ncols();
1058         cur.pos() = 0;
1059
1060         if (getType() == hullSimple) {
1061                 size_type pos = cur.cell().find_last(eq);
1062                 MathData ar;
1063                 if (cur.inMathed() && cur.selection()) {
1064                         asArray(grabAndEraseSelection(cur), ar);
1065                 } else if (pos == cur.cell().size()) {
1066                         ar = cur.cell();
1067                         lyxerr << "use whole cell: " << ar << endl;
1068                 } else {
1069                         ar = MathData(cur.cell().begin() + pos + 1, cur.cell().end());
1070                         lyxerr << "use partial cell form pos: " << pos << endl;
1071                 }
1072                 cur.cell().append(eq);
1073                 cur.cell().append(pipeThroughExtern(lang, extra, ar));
1074                 cur.pos() = cur.lastpos();
1075                 return;
1076         }
1077
1078         if (getType() == hullEquation) {
1079                 lyxerr << "use equation inset" << endl;
1080                 mutate(hullEqnArray);
1081                 MathData & ar = cur.cell();
1082                 lyxerr << "use cell: " << ar << endl;
1083                 ++cur.idx();
1084                 cur.cell() = eq;
1085                 ++cur.idx();
1086                 cur.cell() = pipeThroughExtern(lang, extra, ar);
1087                 // move to end of line
1088                 cur.pos() = cur.lastpos();
1089                 return;
1090         }
1091
1092         {
1093                 lyxerr << "use eqnarray" << endl;
1094                 cur.idx() += 2 - cur.idx() % ncols();
1095                 cur.pos() = 0;
1096                 MathData ar = cur.cell();
1097                 lyxerr << "use cell: " << ar << endl;
1098                 // FIXME: temporarily disabled
1099                 addRow(cur.row());
1100                 ++cur.idx();
1101                 ++cur.idx();
1102                 cur.cell() = eq;
1103                 ++cur.idx();
1104                 cur.cell() = pipeThroughExtern(lang, extra, ar);
1105                 cur.pos() = cur.lastpos();
1106         }
1107 }
1108
1109
1110 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1111 {
1112         //lyxerr << "action: " << cmd.action << endl;
1113         switch (cmd.action) {
1114
1115         case LFUN_FINISHED_BACKWARD:
1116         case LFUN_FINISHED_FORWARD:
1117         case LFUN_FINISHED_RIGHT:
1118         case LFUN_FINISHED_LEFT:
1119                 //lyxerr << "action: " << cmd.action << endl;
1120                 InsetMathGrid::doDispatch(cur, cmd);
1121                 cur.undispatched();
1122                 break;
1123
1124         case LFUN_BREAK_PARAGRAPH:
1125                 // just swallow this
1126                 break;
1127
1128         case LFUN_NEW_LINE:
1129                 // some magic for the common case
1130                 if (type_ == hullSimple || type_ == hullEquation) {
1131                         cur.recordUndoInset();
1132                         bool const align =
1133                                 cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
1134                         mutate(align ? hullAlign : hullEqnArray);
1135                         cur.idx() = nrows() * ncols() - 1;
1136                         cur.pos() = cur.lastpos();
1137                 }
1138                 InsetMathGrid::doDispatch(cur, cmd);
1139                 break;
1140
1141         case LFUN_MATH_NUMBER_TOGGLE: {
1142                 //lyxerr << "toggling all numbers" << endl;
1143                 cur.recordUndoInset();
1144                 bool old = numberedType();
1145                 if (type_ == hullMultline)
1146                         numbered(nrows() - 1, !old);
1147                 else
1148                         for (row_type row = 0; row < nrows(); ++row)
1149                                 numbered(row, !old);
1150                 
1151                 cur.message(old ? _("No number") : _("Number"));
1152                 break;
1153         }
1154
1155         case LFUN_MATH_NUMBER_LINE_TOGGLE: {
1156                 cur.recordUndoInset();
1157                 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1158                 bool old = numbered(r);
1159                 cur.message(old ? _("No number") : _("Number"));
1160                 numbered(r, !old);
1161                 break;
1162         }
1163
1164         case LFUN_LABEL_INSERT: {
1165                 cur.recordUndoInset();
1166                 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1167                 docstring old_label = label(r);
1168                 docstring const default_label = from_ascii(
1169                         (lyxrc.label_init_length >= 0) ? "eq:" : "");
1170                 if (old_label.empty())
1171                         old_label = default_label;
1172
1173                 InsetCommandParams p(LABEL_CODE);
1174                 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1175                 string const data = InsetCommandMailer::params2string("label", p);
1176
1177                 if (cmd.argument().empty())
1178                         cur.bv().showDialog("label", data);
1179                 else {
1180                         FuncRequest fr(LFUN_INSET_INSERT, data);
1181                         dispatch(cur, fr);
1182                 }
1183                 break;
1184         }
1185
1186         case LFUN_INSET_INSERT: {
1187                 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1188                 // FIXME: this should be cleaned up to use InsetLabel methods directly.
1189                 string const name = cmd.getArg(0);
1190                 if (name == "label") {
1191                         InsetCommandParams p(LABEL_CODE);
1192                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1193                         docstring str = p["name"];
1194                         cur.recordUndoInset();
1195                         row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1196                         str = trim(str);
1197                         if (!str.empty())
1198                                 numbered(r, true);
1199                         docstring old = label(r);
1200                         if (str != old) {
1201                                 if (label_[r])
1202                                         // The label will take care of the reference update.
1203                                         label(r, str);
1204                                 else {
1205                                         label(r, str);
1206                                         // Newly created inset so initialize it.
1207                                         label_[r]->initView();
1208                                 }
1209                         }
1210                         break;
1211                 }
1212                 InsetMathGrid::doDispatch(cur, cmd);
1213                 return;
1214         }
1215
1216         case LFUN_MATH_EXTERN:
1217                 cur.recordUndoInset();
1218                 doExtern(cur, cmd);
1219                 break;
1220
1221         case LFUN_MATH_MUTATE: {
1222                 cur.recordUndoInset();
1223                 row_type row = cur.row();
1224                 col_type col = cur.col();
1225                 mutate(hullType(cmd.argument()));
1226                 cur.idx() = row * ncols() + col;
1227                 if (cur.idx() > cur.lastidx()) {
1228                         cur.idx() = cur.lastidx();
1229                         cur.pos() = cur.lastpos();
1230                 }
1231                 if (cur.pos() > cur.lastpos())
1232                         cur.pos() = cur.lastpos();
1233                 
1234                 // FIXME: find some more clever handling of the selection,
1235                 // i.e. preserve it.
1236                 cur.clearSelection();
1237                 //cur.dispatched(FINISHED);
1238                 break;
1239         }
1240
1241         case LFUN_MATH_DISPLAY: {
1242                 cur.recordUndoInset();
1243                 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1244                 cur.idx() = 0;
1245                 cur.pos() = cur.lastpos();
1246                 //cur.dispatched(FINISHED);
1247                 break;
1248         }
1249
1250         default:
1251                 InsetMathGrid::doDispatch(cur, cmd);
1252                 break;
1253         }
1254 }
1255
1256
1257 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1258                 FuncStatus & status) const
1259 {
1260         switch (cmd.action) {
1261         case LFUN_FINISHED_BACKWARD:
1262         case LFUN_FINISHED_FORWARD:
1263         case LFUN_FINISHED_RIGHT:
1264         case LFUN_FINISHED_LEFT:
1265         case LFUN_UP:
1266         case LFUN_DOWN:
1267         case LFUN_NEW_LINE:
1268         case LFUN_MATH_EXTERN:
1269         case LFUN_MATH_MUTATE:
1270         case LFUN_MATH_DISPLAY:
1271                 // we handle these
1272                 status.enabled(true);
1273                 return true;
1274         case LFUN_MATH_NUMBER_TOGGLE:
1275                 // FIXME: what is the right test, this or the one of
1276                 // LABEL_INSERT?
1277                 status.enabled(display());
1278                 status.setOnOff(numberedType());
1279                 return true;
1280         case LFUN_MATH_NUMBER_LINE_TOGGLE: {
1281                 // FIXME: what is the right test, this or the one of
1282                 // LABEL_INSERT?
1283                 status.enabled(display());
1284                 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1285                 status.setOnOff(numbered(r));
1286                 return true;
1287         }
1288         case LFUN_LABEL_INSERT:
1289                 status.enabled(type_ != hullSimple);
1290                 return true;
1291         case LFUN_INSET_INSERT:
1292                 if (cmd.getArg(0) == "label") {
1293                         status.enabled(type_ != hullSimple);
1294                         return true;
1295                 }
1296                 return InsetMathGrid::getStatus(cur, cmd, status);
1297         case LFUN_TABULAR_FEATURE: {
1298                 istringstream is(to_utf8(cmd.argument()));
1299                 string s;
1300                 is >> s;
1301                 if (!rowChangeOK()
1302                     && (s == "append-row"
1303                         || s == "delete-row"
1304                         || s == "copy-row")) {
1305                         status.message(bformat(
1306                                 from_utf8(N_("Can't change number of rows in '%1$s'")),
1307                                 hullName(type_)));
1308                         status.enabled(false);
1309                         return true;
1310                 }
1311                 if (!colChangeOK()
1312                     && (s == "append-column"
1313                         || s == "delete-column"
1314                         || s == "copy-column")) {
1315                         status.message(bformat(
1316                                 from_utf8(N_("Can't change number of columns in '%1$s'")),
1317                                 hullName(type_)));
1318                         status.enabled(false);
1319                         return true;
1320                 }
1321                 if ((type_ == hullSimple
1322                   || type_ == hullEquation
1323                   || type_ == hullNone) &&
1324                     (s == "add-hline-above" || s == "add-hline-below")) {
1325                         status.message(bformat(
1326                                 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1327                                 hullName(type_)));
1328                         status.enabled(false);
1329                         return true;
1330                 }
1331                 if (s == "add-vline-left" || s == "add-vline-right") {
1332                         status.message(bformat(
1333                                 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1334                                 hullName(type_)));
1335                         status.enabled(false);
1336                         return true;
1337                 }
1338                 if (s == "valign-top" || s == "valign-middle"
1339                  || s == "valign-bottom" || s == "align-left"
1340                  || s == "align-center" || s == "align-right") {
1341                         status.enabled(false);
1342                         return true;
1343                 }
1344                 return InsetMathGrid::getStatus(cur, cmd, status);
1345         }
1346         default:
1347                 return InsetMathGrid::getStatus(cur, cmd, status);
1348         }
1349
1350         // This cannot really happen, but inserted to shut-up gcc
1351         return InsetMathGrid::getStatus(cur, cmd, status);
1352 }
1353
1354
1355 /////////////////////////////////////////////////////////////////////
1356
1357
1358
1359 // simply scrap this function if you want
1360 void InsetMathHull::mutateToText()
1361 {
1362 #if 0
1363         // translate to latex
1364         ostringstream os;
1365         latex(NULL, os, false, false);
1366         string str = os.str();
1367
1368         // insert this text
1369         Text * lt = view_->cursor().innerText();
1370         string::const_iterator cit = str.begin();
1371         string::const_iterator end = str.end();
1372         for (; cit != end; ++cit)
1373                 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1374
1375         // remove ourselves
1376         //dispatch(LFUN_ESCAPE);
1377 #endif
1378 }
1379
1380
1381 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1382         docstring const & font)
1383 {
1384         // this whole function is a hack and won't work for incremental font
1385         // changes...
1386         cur.recordUndo();
1387         if (cur.inset().asInsetMath()->name() == font)
1388                 cur.handleFont(to_utf8(font));
1389         else {
1390                 cur.handleNest(createInsetMath(font));
1391                 cur.insert(arg);
1392         }
1393 }
1394
1395
1396 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1397 {
1398         cur.recordUndo();
1399         Font font;
1400         bool b;
1401         font.fromString(to_utf8(arg), b);
1402         if (font.fontInfo().color() != Color_inherit) {
1403                 MathAtom at = MathAtom(new InsetMathColor(true, font.fontInfo().color()));
1404                 cur.handleNest(at, 0);
1405         }
1406 }
1407
1408
1409 void InsetMathHull::edit(Cursor & cur, bool front, EntryDirection entry_from)
1410 {
1411         cur.push(*this);
1412         bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_LEFT || 
1413                 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1414         enter_front ? idxFirst(cur) : idxLast(cur);
1415         // The inset formula dimension is not necessarily the same as the
1416         // one of the instant preview image, so we have to indicate to the
1417         // BufferView that a metrics update is needed.
1418         cur.updateFlags(Update::Force);
1419 }
1420
1421
1422 docstring InsetMathHull::editMessage() const
1423 {
1424         return _("Math editor mode");
1425 }
1426
1427
1428 void InsetMathHull::revealCodes(Cursor & cur) const
1429 {
1430         if (!cur.inMathed())
1431                 return;
1432         odocstringstream os;
1433         cur.info(os);
1434         cur.message(os.str());
1435 /*
1436         // write something to the minibuffer
1437         // translate to latex
1438         cur.markInsert(bv);
1439         ostringstream os;
1440         write(NULL, os);
1441         string str = os.str();
1442         cur.markErase(bv);
1443         string::size_type pos = 0;
1444         string res;
1445         for (string::iterator it = str.begin(); it != str.end(); ++it) {
1446                 if (*it == '\n')
1447                         res += ' ';
1448                 else if (*it == '\0') {
1449                         res += "  -X-  ";
1450                         pos = it - str.begin();
1451                 }
1452                 else
1453                         res += *it;
1454         }
1455         if (pos > 30)
1456                 res = res.substr(pos - 30);
1457         if (res.size() > 60)
1458                 res = res.substr(0, 60);
1459         cur.message(res);
1460 */
1461 }
1462
1463
1464 InsetCode InsetMathHull::lyxCode() const
1465 {
1466         return MATH_CODE;
1467 }
1468
1469
1470 /////////////////////////////////////////////////////////////////////
1471
1472
1473 #if 0
1474 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1475                                      bool, bool)
1476 {
1477         // FIXME: completely broken
1478         static InsetMathHull * lastformula = 0;
1479         static CursorBase current = DocIterator(ibegin(nucleus()));
1480         static MathData ar;
1481         static string laststr;
1482
1483         if (lastformula != this || laststr != str) {
1484                 //lyxerr << "reset lastformula to " << this << endl;
1485                 lastformula = this;
1486                 laststr = str;
1487                 current = ibegin(nucleus());
1488                 ar.clear();
1489                 mathed_parse_cell(ar, str);
1490         } else {
1491                 increment(current);
1492         }
1493         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1494
1495         for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1496                 CursorSlice & top = it.back();
1497                 MathData const & a = top.asInsetMath()->cell(top.idx_);
1498                 if (a.matchpart(ar, top.pos_)) {
1499                         bv->cursor().setSelection(it, ar.size());
1500                         current = it;
1501                         top.pos_ += ar.size();
1502                         bv->update();
1503                         return true;
1504                 }
1505         }
1506
1507         //lyxerr << "not found!" << endl;
1508         lastformula = 0;
1509         return false;
1510 }
1511 #endif
1512
1513
1514 void InsetMathHull::write(ostream & os) const
1515 {
1516         odocstringstream oss;
1517         WriteStream wi(oss, false, false);
1518         oss << "Formula ";
1519         write(wi);
1520         os << to_utf8(oss.str());
1521 }
1522
1523
1524 void InsetMathHull::read(Lexer & lex)
1525 {
1526         MathAtom at;
1527         mathed_parse_normal(at, lex);
1528         operator=(*at->asHullInset());
1529 }
1530
1531
1532 int InsetMathHull::plaintext(odocstream & os, OutputParams const &) const
1533 {
1534         if (0 && display()) {
1535                 Dimension dim;
1536                 TextMetricsInfo mi;
1537                 metricsT(mi, dim);
1538                 TextPainter tpain(dim.width(), dim.height());
1539                 drawT(tpain, 0, dim.ascent());
1540                 tpain.show(os, 3);
1541                 // reset metrics cache to "real" values
1542                 //metrics();
1543                 return tpain.textheight();
1544         } else {
1545                 odocstringstream oss;
1546                 WriteStream wi(oss, false, true);
1547                 wi << cell(0);
1548
1549                 docstring const str = oss.str();
1550                 os << str;
1551                 return str.size();
1552         }
1553 }
1554
1555
1556 int InsetMathHull::docbook(odocstream & os, OutputParams const & runparams) const
1557 {
1558         MathStream ms(os);
1559         int res = 0;
1560         docstring name;
1561         if (getType() == hullSimple)
1562                 name = from_ascii("inlineequation");
1563         else
1564                 name = from_ascii("informalequation");
1565
1566         docstring bname = name;
1567         if (!label(0).empty())
1568                 bname += " id='" + sgml::cleanID(buffer(), runparams, label(0)) + "'";
1569
1570         ++ms.tab(); ms.cr(); ms.os() << '<' << bname << '>';
1571
1572         odocstringstream ls;
1573         if (runparams.flavor == OutputParams::XML) {
1574                 ms << MTag("alt role='tex' ");
1575                 // Workaround for db2latex: db2latex always includes equations with
1576                 // \ensuremath{} or \begin{display}\end{display}
1577                 // so we strip LyX' math environment
1578                 WriteStream wi(ls, false, false);
1579                 InsetMathGrid::write(wi);
1580                 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&amp;"), "<", "&lt;"));
1581                 ms << ETag("alt");
1582                 ms << MTag("math");
1583                 ms << ETag("alt");
1584                 ms << MTag("math");
1585                 InsetMathGrid::mathmlize(ms);
1586                 ms << ETag("math");
1587         } else {
1588                 ms << MTag("alt role='tex'");
1589                 res = latex(ls, runparams);
1590                 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&amp;"), "<", "&lt;"));
1591                 ms << ETag("alt");
1592         }
1593
1594         ms << from_ascii("<graphic fileref=\"eqn/");
1595         if (!label(0).empty())
1596                 ms << sgml::cleanID(buffer(), runparams, label(0));
1597         else
1598                 ms << sgml::uniqueID(from_ascii("anon"));
1599
1600         if (runparams.flavor == OutputParams::XML)
1601                 ms << from_ascii("\"/>");
1602         else
1603                 ms << from_ascii("\">");
1604
1605         ms.cr(); --ms.tab(); ms.os() << "</" << name << '>';
1606
1607         return ms.line() + res;
1608 }
1609
1610
1611 void InsetMathHull::textString(odocstream & os) const
1612 {
1613         plaintext(os, OutputParams(0));
1614 }
1615
1616
1617 } // namespace lyx