]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathHull.cpp
Change the interface to a paragraph's layout. We still store a LayoutPtr, but now...
[lyx.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 doesn't like this:
705         //      swap(nonum_[row], nonum_[row + 1]);
706         // so we do it manually:
707         bool const b = nonum_[row];
708         nonum_[row] = nonum_[row + 1];
709         nonum_[row + 1] = b;
710         swap(label_[row], label_[row + 1]);
711         InsetMathGrid::swapRow(row);
712 }
713
714
715 void InsetMathHull::delRow(row_type row)
716 {
717         if (nrows() <= 1 || !rowChangeOK())
718                 return;
719         InsetMathGrid::delRow(row);
720         // The last dummy row has no number info nor a label.
721         // Test nrows() + 1 because we have already erased the row.
722         if (row == nrows() + 1)
723                 row--;
724         nonum_.erase(nonum_.begin() + row);
725         delete label_[row];
726         label_.erase(label_.begin() + row);
727 }
728
729
730 void InsetMathHull::addCol(col_type col)
731 {
732         if (!colChangeOK())
733                 return;
734         InsetMathGrid::addCol(col);
735 }
736
737
738 void InsetMathHull::delCol(col_type col)
739 {
740         if (ncols() <= 1 || !colChangeOK())
741                 return;
742         InsetMathGrid::delCol(col);
743 }
744
745
746 docstring InsetMathHull::nicelabel(row_type row) const
747 {
748         if (nonum_[row])
749                 return docstring();
750         if (!label_[row])
751                 return from_ascii("(#)");
752         return '(' + label_[row]->screenLabel() + ')';
753 }
754
755
756 void InsetMathHull::glueall()
757 {
758         MathData ar;
759         for (idx_type i = 0; i < nargs(); ++i)
760                 ar.append(cell(i));
761         *this = InsetMathHull(hullSimple);
762         cell(0) = ar;
763         setDefaults();
764 }
765
766
767 void InsetMathHull::splitTo2Cols()
768 {
769         BOOST_ASSERT(ncols() == 1);
770         InsetMathGrid::addCol(1);
771         for (row_type row = 0; row < nrows(); ++row) {
772                 idx_type const i = 2 * row;
773                 pos_type pos = firstRelOp(cell(i));
774                 cell(i + 1) = MathData(cell(i).begin() + pos, cell(i).end());
775                 cell(i).erase(pos, cell(i).size());
776         }
777 }
778
779
780 void InsetMathHull::splitTo3Cols()
781 {
782         BOOST_ASSERT(ncols() < 3);
783         if (ncols() < 2)
784                 splitTo2Cols();
785         InsetMathGrid::addCol(2);
786         for (row_type row = 0; row < nrows(); ++row) {
787                 idx_type const i = 3 * row + 1;
788                 if (cell(i).size()) {
789                         cell(i + 1) = MathData(cell(i).begin() + 1, cell(i).end());
790                         cell(i).erase(1, cell(i).size());
791                 }
792         }
793 }
794
795
796 void InsetMathHull::changeCols(col_type cols)
797 {
798         if (ncols() == cols)
799                 return;
800         else if (ncols() < cols) {
801                 // split columns
802                 if (cols < 3)
803                         splitTo2Cols();
804                 else {
805                         splitTo3Cols();
806                         while (ncols() < cols)
807                                 InsetMathGrid::addCol(ncols());
808                 }
809                 return;
810         }
811
812         // combine columns
813         for (row_type row = 0; row < nrows(); ++row) {
814                 idx_type const i = row * ncols();
815                 for (col_type col = cols; col < ncols(); ++col) {
816                         cell(i + cols - 1).append(cell(i + col));
817                 }
818         }
819         // delete columns
820         while (ncols() > cols) {
821                 InsetMathGrid::delCol(ncols() - 1);
822         }
823 }
824
825
826 HullType InsetMathHull::getType() const
827 {
828         return type_;
829 }
830
831
832 void InsetMathHull::setType(HullType type)
833 {
834         type_ = type;
835         setDefaults();
836 }
837
838
839 void InsetMathHull::mutate(HullType newtype)
840 {
841         //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
842
843         // we try to move along the chain
844         // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
845         //                                     ^                                     |
846         //                                     +-------------------------------------+
847         // we use eqnarray as intermediate type for mutations that are not
848         // directly supported because it handles labels and numbering for
849         // "down mutation".
850
851         if (newtype == type_) {
852                 // done
853         }
854
855         else if (newtype < hullNone) {
856                 // unknown type
857                 dump();
858         }
859
860         else if (type_ == hullNone) {
861                 setType(hullSimple);
862                 numbered(0, false);
863                 mutate(newtype);
864         }
865
866         else if (type_ == hullSimple) {
867                 if (newtype == hullNone) {
868                         setType(hullNone);
869                         numbered(0, false);
870                 } else {
871                         setType(hullEquation);
872                         numbered(0, false);
873                         mutate(newtype);
874                 }
875         }
876
877         else if (type_ == hullEquation) {
878                 if (newtype < type_) {
879                         setType(hullSimple);
880                         numbered(0, false);
881                         mutate(newtype);
882                 } else if (newtype == hullEqnArray) {
883                         // split it "nicely" on the first relop
884                         splitTo3Cols();
885                         setType(hullEqnArray);
886                 } else if (newtype == hullMultline || newtype == hullGather) {
887                         setType(newtype);
888                 } else {
889                         // split it "nicely"
890                         splitTo2Cols();
891                         setType(hullAlign);
892                         mutate(newtype);
893                 }
894         }
895
896         else if (type_ == hullEqnArray) {
897                 if (newtype < type_) {
898                         // set correct (no)numbering
899                         nonum_[0] = true;
900                         for (row_type row = 0; row < nrows(); ++row) {
901                                 if (!nonum_[row]) {
902                                         nonum_[0] = false;
903                                         break;
904                                 }
905                         }
906
907                         // set first non-empty label
908                         for (row_type row = 0; row < nrows(); ++row) {
909                                 if (label_[row]) {
910                                         label_[0] = label_[row];
911                                         break;
912                                 }
913                         }
914
915                         glueall();
916                         mutate(newtype);
917                 } else { // align & Co.
918                         changeCols(2);
919                         setType(hullAlign);
920                         mutate(newtype);
921                 }
922         }
923
924         else if (type_ ==  hullAlign || type_ == hullAlignAt ||
925                  type_ == hullXAlignAt || type_ == hullFlAlign) {
926                 if (newtype < hullAlign) {
927                         changeCols(3);
928                         setType(hullEqnArray);
929                         mutate(newtype);
930                 } else if (newtype == hullGather || newtype == hullMultline) {
931                         changeCols(1);
932                         setType(newtype);
933                 } else if (newtype ==   hullXXAlignAt) {
934                         for (row_type row = 0; row < nrows(); ++row)
935                                 numbered(row, false);
936                         setType(newtype);
937                 } else {
938                         setType(newtype);
939                 }
940         }
941
942         else if (type_ == hullXXAlignAt) {
943                 for (row_type row = 0; row < nrows(); ++row)
944                         numbered(row, false);
945                 if (newtype < hullAlign) {
946                         changeCols(3);
947                         setType(hullEqnArray);
948                         mutate(newtype);
949                 } else if (newtype == hullGather || newtype == hullMultline) {
950                         changeCols(1);
951                         setType(newtype);
952                 } else {
953                         setType(newtype);
954                 }
955         }
956
957         else if (type_ == hullMultline || type_ == hullGather) {
958                 if (newtype == hullGather || newtype == hullMultline)
959                         setType(newtype);
960                 else if (newtype == hullAlign || newtype == hullFlAlign  ||
961                          newtype == hullAlignAt || newtype == hullXAlignAt) {
962                         splitTo2Cols();
963                         setType(newtype);
964                 } else if (newtype ==   hullXXAlignAt) {
965                         splitTo2Cols();
966                         for (row_type row = 0; row < nrows(); ++row)
967                                 numbered(row, false);
968                         setType(newtype);
969                 } else {
970                         splitTo3Cols();
971                         setType(hullEqnArray);
972                         mutate(newtype);
973                 }
974         }
975
976         else {
977                 lyxerr << "mutation from '" << to_utf8(hullName(type_))
978                        << "' to '" << to_utf8(hullName(newtype))
979                        << "' not implemented" << endl;
980         }
981 }
982
983
984 docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
985 {
986         docstring res;
987         if (numberedType()) {
988                 if (label_[row] && !nonum_[row])
989                         res += "\\label{" + label_[row]->getParam("name") + '}';
990                 if (nonum_[row] && (type_ != hullMultline))
991                         res += "\\nonumber ";
992         }
993         return res + InsetMathGrid::eolString(row, emptyline, fragile);
994 }
995
996
997 void InsetMathHull::write(WriteStream & os) const
998 {
999         header_write(os);
1000         InsetMathGrid::write(os);
1001         footer_write(os);
1002 }
1003
1004
1005 void InsetMathHull::normalize(NormalStream & os) const
1006 {
1007         os << "[formula " << hullName(type_) << ' ';
1008         InsetMathGrid::normalize(os);
1009         os << "] ";
1010 }
1011
1012
1013 void InsetMathHull::mathmlize(MathStream & os) const
1014 {
1015         InsetMathGrid::mathmlize(os);
1016 }
1017
1018
1019 void InsetMathHull::infoize(odocstream & os) const
1020 {
1021         os << "Type: " << hullName(type_);
1022 }
1023
1024
1025 void InsetMathHull::check() const
1026 {
1027         BOOST_ASSERT(nonum_.size() == nrows());
1028         BOOST_ASSERT(label_.size() == nrows());
1029 }
1030
1031
1032 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
1033 {
1034         docstring dlang;
1035         docstring extra;
1036         idocstringstream iss(func.argument());
1037         iss >> dlang >> extra;
1038         if (extra.empty())
1039                 extra = from_ascii("noextra");
1040         string const lang = to_ascii(dlang);
1041
1042         // FIXME: temporarily disabled
1043         //if (cur.selection()) {
1044         //      MathData ar;
1045         //      selGet(cur.ar);
1046         //      lyxerr << "use selection: " << ar << endl;
1047         //      insert(pipeThroughExtern(lang, extra, ar));
1048         //      return;
1049         //}
1050
1051         MathData eq;
1052         eq.push_back(MathAtom(new InsetMathChar('=')));
1053
1054         // go to first item in line
1055         cur.idx() -= cur.idx() % ncols();
1056         cur.pos() = 0;
1057
1058         if (getType() == hullSimple) {
1059                 size_type pos = cur.cell().find_last(eq);
1060                 MathData ar;
1061                 if (cur.inMathed() && cur.selection()) {
1062                         asArray(grabAndEraseSelection(cur), ar);
1063                 } else if (pos == cur.cell().size()) {
1064                         ar = cur.cell();
1065                         lyxerr << "use whole cell: " << ar << endl;
1066                 } else {
1067                         ar = MathData(cur.cell().begin() + pos + 1, cur.cell().end());
1068                         lyxerr << "use partial cell form pos: " << pos << endl;
1069                 }
1070                 cur.cell().append(eq);
1071                 cur.cell().append(pipeThroughExtern(lang, extra, ar));
1072                 cur.pos() = cur.lastpos();
1073                 return;
1074         }
1075
1076         if (getType() == hullEquation) {
1077                 lyxerr << "use equation inset" << endl;
1078                 mutate(hullEqnArray);
1079                 MathData & ar = cur.cell();
1080                 lyxerr << "use cell: " << ar << endl;
1081                 ++cur.idx();
1082                 cur.cell() = eq;
1083                 ++cur.idx();
1084                 cur.cell() = pipeThroughExtern(lang, extra, ar);
1085                 // move to end of line
1086                 cur.pos() = cur.lastpos();
1087                 return;
1088         }
1089
1090         {
1091                 lyxerr << "use eqnarray" << endl;
1092                 cur.idx() += 2 - cur.idx() % ncols();
1093                 cur.pos() = 0;
1094                 MathData ar = cur.cell();
1095                 lyxerr << "use cell: " << ar << endl;
1096                 // FIXME: temporarily disabled
1097                 addRow(cur.row());
1098                 ++cur.idx();
1099                 ++cur.idx();
1100                 cur.cell() = eq;
1101                 ++cur.idx();
1102                 cur.cell() = pipeThroughExtern(lang, extra, ar);
1103                 cur.pos() = cur.lastpos();
1104         }
1105 }
1106
1107
1108 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1109 {
1110         //lyxerr << "action: " << cmd.action << endl;
1111         switch (cmd.action) {
1112
1113         case LFUN_FINISHED_BACKWARD:
1114         case LFUN_FINISHED_FORWARD:
1115         case LFUN_FINISHED_RIGHT:
1116         case LFUN_FINISHED_LEFT:
1117                 //lyxerr << "action: " << cmd.action << endl;
1118                 InsetMathGrid::doDispatch(cur, cmd);
1119                 cur.undispatched();
1120                 break;
1121
1122         case LFUN_BREAK_PARAGRAPH:
1123                 // just swallow this
1124                 break;
1125
1126         case LFUN_NEW_LINE:
1127                 // some magic for the common case
1128                 if (type_ == hullSimple || type_ == hullEquation) {
1129                         cur.recordUndoInset();
1130                         bool const align =
1131                                 cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
1132                         mutate(align ? hullAlign : hullEqnArray);
1133                         cur.idx() = nrows() * ncols() - 1;
1134                         cur.pos() = cur.lastpos();
1135                 }
1136                 InsetMathGrid::doDispatch(cur, cmd);
1137                 break;
1138
1139         case LFUN_MATH_NUMBER_TOGGLE: {
1140                 //lyxerr << "toggling all numbers" << endl;
1141                 cur.recordUndoInset();
1142                 bool old = numberedType();
1143                 if (type_ == hullMultline)
1144                         numbered(nrows() - 1, !old);
1145                 else
1146                         for (row_type row = 0; row < nrows(); ++row)
1147                                 numbered(row, !old);
1148                 
1149                 cur.message(old ? _("No number") : _("Number"));
1150                 break;
1151         }
1152
1153         case LFUN_MATH_NUMBER_LINE_TOGGLE: {
1154                 cur.recordUndoInset();
1155                 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1156                 bool old = numbered(r);
1157                 cur.message(old ? _("No number") : _("Number"));
1158                 numbered(r, !old);
1159                 break;
1160         }
1161
1162         case LFUN_LABEL_INSERT: {
1163                 cur.recordUndoInset();
1164                 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1165                 docstring old_label = label(r);
1166                 docstring const default_label = from_ascii(
1167                         (lyxrc.label_init_length >= 0) ? "eq:" : "");
1168                 if (old_label.empty())
1169                         old_label = default_label;
1170
1171                 InsetCommandParams p(LABEL_CODE);
1172                 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1173                 string const data = InsetCommandMailer::params2string("label", p);
1174
1175                 if (cmd.argument().empty())
1176                         cur.bv().showDialog("label", data);
1177                 else {
1178                         FuncRequest fr(LFUN_INSET_INSERT, data);
1179                         dispatch(cur, fr);
1180                 }
1181                 break;
1182         }
1183
1184         case LFUN_INSET_INSERT: {
1185                 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1186                 // FIXME: this should be cleaned up to use InsetLabel methods directly.
1187                 string const name = cmd.getArg(0);
1188                 if (name == "label") {
1189                         InsetCommandParams p(LABEL_CODE);
1190                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1191                         docstring str = p["name"];
1192                         cur.recordUndoInset();
1193                         row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1194                         str = trim(str);
1195                         if (!str.empty())
1196                                 numbered(r, true);
1197                         docstring old = label(r);
1198                         if (str != old) {
1199                                 if (label_[r])
1200                                         // The label will take care of the reference update.
1201                                         label(r, str);
1202                                 else {
1203                                         label(r, str);
1204                                         // Newly created inset so initialize it.
1205                                         label_[r]->initView();
1206                                 }
1207                         }
1208                         break;
1209                 }
1210                 InsetMathGrid::doDispatch(cur, cmd);
1211                 return;
1212         }
1213
1214         case LFUN_MATH_EXTERN:
1215                 cur.recordUndoInset();
1216                 doExtern(cur, cmd);
1217                 break;
1218
1219         case LFUN_MATH_MUTATE: {
1220                 cur.recordUndoInset();
1221                 row_type row = cur.row();
1222                 col_type col = cur.col();
1223                 mutate(hullType(cmd.argument()));
1224                 cur.idx() = row * ncols() + col;
1225                 if (cur.idx() > cur.lastidx()) {
1226                         cur.idx() = cur.lastidx();
1227                         cur.pos() = cur.lastpos();
1228                 }
1229                 if (cur.pos() > cur.lastpos())
1230                         cur.pos() = cur.lastpos();
1231                 
1232                 // FIXME: find some more clever handling of the selection,
1233                 // i.e. preserve it.
1234                 cur.clearSelection();
1235                 //cur.dispatched(FINISHED);
1236                 break;
1237         }
1238
1239         case LFUN_MATH_DISPLAY: {
1240                 cur.recordUndoInset();
1241                 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1242                 cur.idx() = 0;
1243                 cur.pos() = cur.lastpos();
1244                 //cur.dispatched(FINISHED);
1245                 break;
1246         }
1247
1248         default:
1249                 InsetMathGrid::doDispatch(cur, cmd);
1250                 break;
1251         }
1252 }
1253
1254
1255 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1256                 FuncStatus & status) const
1257 {
1258         switch (cmd.action) {
1259         case LFUN_FINISHED_BACKWARD:
1260         case LFUN_FINISHED_FORWARD:
1261         case LFUN_FINISHED_RIGHT:
1262         case LFUN_FINISHED_LEFT:
1263         case LFUN_UP:
1264         case LFUN_DOWN:
1265         case LFUN_NEW_LINE:
1266         case LFUN_MATH_EXTERN:
1267         case LFUN_MATH_MUTATE:
1268         case LFUN_MATH_DISPLAY:
1269                 // we handle these
1270                 status.enabled(true);
1271                 return true;
1272         case LFUN_MATH_NUMBER_TOGGLE:
1273                 // FIXME: what is the right test, this or the one of
1274                 // LABEL_INSERT?
1275                 status.enabled(display());
1276                 status.setOnOff(numberedType());
1277                 return true;
1278         case LFUN_MATH_NUMBER_LINE_TOGGLE: {
1279                 // FIXME: what is the right test, this or the one of
1280                 // LABEL_INSERT?
1281                 status.enabled(display());
1282                 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1283                 status.setOnOff(numbered(r));
1284                 return true;
1285         }
1286         case LFUN_LABEL_INSERT:
1287                 status.enabled(type_ != hullSimple);
1288                 return true;
1289         case LFUN_INSET_INSERT:
1290                 if (cmd.getArg(0) == "label") {
1291                         status.enabled(type_ != hullSimple);
1292                         return true;
1293                 }
1294                 return InsetMathGrid::getStatus(cur, cmd, status);
1295         case LFUN_TABULAR_FEATURE: {
1296                 istringstream is(to_utf8(cmd.argument()));
1297                 string s;
1298                 is >> s;
1299                 if (!rowChangeOK()
1300                     && (s == "append-row"
1301                         || s == "delete-row"
1302                         || s == "copy-row")) {
1303                         status.message(bformat(
1304                                 from_utf8(N_("Can't change number of rows in '%1$s'")),
1305                                 hullName(type_)));
1306                         status.enabled(false);
1307                         return true;
1308                 }
1309                 if (!colChangeOK()
1310                     && (s == "append-column"
1311                         || s == "delete-column"
1312                         || s == "copy-column")) {
1313                         status.message(bformat(
1314                                 from_utf8(N_("Can't change number of columns in '%1$s'")),
1315                                 hullName(type_)));
1316                         status.enabled(false);
1317                         return true;
1318                 }
1319                 if ((type_ == hullSimple
1320                   || type_ == hullEquation
1321                   || type_ == hullNone) &&
1322                     (s == "add-hline-above" || s == "add-hline-below")) {
1323                         status.message(bformat(
1324                                 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1325                                 hullName(type_)));
1326                         status.enabled(false);
1327                         return true;
1328                 }
1329                 if (s == "add-vline-left" || s == "add-vline-right") {
1330                         status.message(bformat(
1331                                 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1332                                 hullName(type_)));
1333                         status.enabled(false);
1334                         return true;
1335                 }
1336                 if (s == "valign-top" || s == "valign-middle"
1337                  || s == "valign-bottom" || s == "align-left"
1338                  || s == "align-center" || s == "align-right") {
1339                         status.enabled(false);
1340                         return true;
1341                 }
1342                 return InsetMathGrid::getStatus(cur, cmd, status);
1343         }
1344         default:
1345                 return InsetMathGrid::getStatus(cur, cmd, status);
1346         }
1347
1348         // This cannot really happen, but inserted to shut-up gcc
1349         return InsetMathGrid::getStatus(cur, cmd, status);
1350 }
1351
1352
1353 /////////////////////////////////////////////////////////////////////
1354
1355
1356
1357 // simply scrap this function if you want
1358 void InsetMathHull::mutateToText()
1359 {
1360 #if 0
1361         // translate to latex
1362         ostringstream os;
1363         latex(NULL, os, false, false);
1364         string str = os.str();
1365
1366         // insert this text
1367         Text * lt = view_->cursor().innerText();
1368         string::const_iterator cit = str.begin();
1369         string::const_iterator end = str.end();
1370         for (; cit != end; ++cit)
1371                 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1372
1373         // remove ourselves
1374         //dispatch(LFUN_ESCAPE);
1375 #endif
1376 }
1377
1378
1379 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1380         docstring const & font)
1381 {
1382         // this whole function is a hack and won't work for incremental font
1383         // changes...
1384         cur.recordUndo();
1385         if (cur.inset().asInsetMath()->name() == font)
1386                 cur.handleFont(to_utf8(font));
1387         else {
1388                 cur.handleNest(createInsetMath(font));
1389                 cur.insert(arg);
1390         }
1391 }
1392
1393
1394 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1395 {
1396         cur.recordUndo();
1397         Font font;
1398         bool b;
1399         font.fromString(to_utf8(arg), b);
1400         if (font.fontInfo().color() != Color_inherit) {
1401                 MathAtom at = MathAtom(new InsetMathColor(true, font.fontInfo().color()));
1402                 cur.handleNest(at, 0);
1403         }
1404 }
1405
1406
1407 void InsetMathHull::edit(Cursor & cur, bool front, EntryDirection entry_from)
1408 {
1409         cur.push(*this);
1410         bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_LEFT || 
1411                 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1412         enter_front ? idxFirst(cur) : idxLast(cur);
1413         // The inset formula dimension is not necessarily the same as the
1414         // one of the instant preview image, so we have to indicate to the
1415         // BufferView that a metrics update is needed.
1416         cur.updateFlags(Update::Force);
1417 }
1418
1419
1420 docstring InsetMathHull::editMessage() const
1421 {
1422         return _("Math editor mode");
1423 }
1424
1425
1426 void InsetMathHull::revealCodes(Cursor & cur) const
1427 {
1428         if (!cur.inMathed())
1429                 return;
1430         odocstringstream os;
1431         cur.info(os);
1432         cur.message(os.str());
1433 /*
1434         // write something to the minibuffer
1435         // translate to latex
1436         cur.markInsert(bv);
1437         ostringstream os;
1438         write(NULL, os);
1439         string str = os.str();
1440         cur.markErase(bv);
1441         string::size_type pos = 0;
1442         string res;
1443         for (string::iterator it = str.begin(); it != str.end(); ++it) {
1444                 if (*it == '\n')
1445                         res += ' ';
1446                 else if (*it == '\0') {
1447                         res += "  -X-  ";
1448                         pos = it - str.begin();
1449                 }
1450                 else
1451                         res += *it;
1452         }
1453         if (pos > 30)
1454                 res = res.substr(pos - 30);
1455         if (res.size() > 60)
1456                 res = res.substr(0, 60);
1457         cur.message(res);
1458 */
1459 }
1460
1461
1462 InsetCode InsetMathHull::lyxCode() const
1463 {
1464         return MATH_CODE;
1465 }
1466
1467
1468 /////////////////////////////////////////////////////////////////////
1469
1470
1471 #if 0
1472 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1473                                      bool, bool)
1474 {
1475         // FIXME: completely broken
1476         static InsetMathHull * lastformula = 0;
1477         static CursorBase current = DocIterator(ibegin(nucleus()));
1478         static MathData ar;
1479         static string laststr;
1480
1481         if (lastformula != this || laststr != str) {
1482                 //lyxerr << "reset lastformula to " << this << endl;
1483                 lastformula = this;
1484                 laststr = str;
1485                 current = ibegin(nucleus());
1486                 ar.clear();
1487                 mathed_parse_cell(ar, str);
1488         } else {
1489                 increment(current);
1490         }
1491         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1492
1493         for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1494                 CursorSlice & top = it.back();
1495                 MathData const & a = top.asInsetMath()->cell(top.idx_);
1496                 if (a.matchpart(ar, top.pos_)) {
1497                         bv->cursor().setSelection(it, ar.size());
1498                         current = it;
1499                         top.pos_ += ar.size();
1500                         bv->update();
1501                         return true;
1502                 }
1503         }
1504
1505         //lyxerr << "not found!" << endl;
1506         lastformula = 0;
1507         return false;
1508 }
1509 #endif
1510
1511
1512 void InsetMathHull::write(ostream & os) const
1513 {
1514         odocstringstream oss;
1515         WriteStream wi(oss, false, false);
1516         oss << "Formula ";
1517         write(wi);
1518         os << to_utf8(oss.str());
1519 }
1520
1521
1522 void InsetMathHull::read(Lexer & lex)
1523 {
1524         MathAtom at;
1525         mathed_parse_normal(at, lex);
1526         operator=(*at->asHullInset());
1527 }
1528
1529
1530 int InsetMathHull::plaintext(odocstream & os, OutputParams const &) const
1531 {
1532         if (0 && display()) {
1533                 Dimension dim;
1534                 TextMetricsInfo mi;
1535                 metricsT(mi, dim);
1536                 TextPainter tpain(dim.width(), dim.height());
1537                 drawT(tpain, 0, dim.ascent());
1538                 tpain.show(os, 3);
1539                 // reset metrics cache to "real" values
1540                 //metrics();
1541                 return tpain.textheight();
1542         } else {
1543                 odocstringstream oss;
1544                 WriteStream wi(oss, false, true);
1545                 wi << cell(0);
1546
1547                 docstring const str = oss.str();
1548                 os << str;
1549                 return str.size();
1550         }
1551 }
1552
1553
1554 int InsetMathHull::docbook(odocstream & os, OutputParams const & runparams) const
1555 {
1556         MathStream ms(os);
1557         int res = 0;
1558         docstring name;
1559         if (getType() == hullSimple)
1560                 name = from_ascii("inlineequation");
1561         else
1562                 name = from_ascii("informalequation");
1563
1564         docstring bname = name;
1565         if (!label(0).empty())
1566                 bname += " id='" + sgml::cleanID(buffer(), runparams, label(0)) + "'";
1567
1568         ++ms.tab(); ms.cr(); ms.os() << '<' << bname << '>';
1569
1570         odocstringstream ls;
1571         if (runparams.flavor == OutputParams::XML) {
1572                 ms << MTag("alt role='tex' ");
1573                 // Workaround for db2latex: db2latex always includes equations with
1574                 // \ensuremath{} or \begin{display}\end{display}
1575                 // so we strip LyX' math environment
1576                 WriteStream wi(ls, false, false);
1577                 InsetMathGrid::write(wi);
1578                 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&amp;"), "<", "&lt;"));
1579                 ms << ETag("alt");
1580                 ms << MTag("math");
1581                 ms << ETag("alt");
1582                 ms << MTag("math");
1583                 InsetMathGrid::mathmlize(ms);
1584                 ms << ETag("math");
1585         } else {
1586                 ms << MTag("alt role='tex'");
1587                 res = latex(ls, runparams);
1588                 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&amp;"), "<", "&lt;"));
1589                 ms << ETag("alt");
1590         }
1591
1592         ms << from_ascii("<graphic fileref=\"eqn/");
1593         if (!label(0).empty())
1594                 ms << sgml::cleanID(buffer(), runparams, label(0));
1595         else
1596                 ms << sgml::uniqueID(from_ascii("anon"));
1597
1598         if (runparams.flavor == OutputParams::XML)
1599                 ms << from_ascii("\"/>");
1600         else
1601                 ms << from_ascii("\">");
1602
1603         ms.cr(); --ms.tab(); ms.os() << "</" << name << '>';
1604
1605         return ms.line() + res;
1606 }
1607
1608
1609 void InsetMathHull::textString(odocstream & os) const
1610 {
1611         plaintext(os, OutputParams(0));
1612 }
1613
1614
1615 } // namespace lyx