3 * Purpose: Implementation of formula inset
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
6 * Description: Allows the edition of math paragraphs inside Lyx.
8 * Copyright: 1996-1998 Alejandro Aguilar Sierra
10 * Version: 0.4, Lyx project.
12 * You are free to use and modify this code under the terms of
13 * the GNU General Public Licence version 2 or later.
17 #pragma implementation
23 #include "math_cursor.h"
24 #include "math_parser.h"
25 #include "math_charinset.h"
26 #include "math_arrayinset.h"
27 #include "math_metricsinfo.h"
28 #include "math_deliminset.h"
29 #include "math_hullinset.h"
30 #include "math_support.h"
31 #include "math_mathmlstream.h"
32 #include "textpainter.h"
34 #include "BufferView.h"
38 #include "funcrequest.h"
40 #include "support/LOstream.h"
41 #include "support/LAssert.h"
42 #include "support/lyxlib.h"
43 #include "support/systemcall.h"
44 #include "support/filetools.h"
46 #include "frontends/Alert.h"
47 #include "frontends/LyXView.h"
48 #include "frontends/Painter.h"
50 #include "graphics/PreviewedInset.h"
51 #include "graphics/PreviewImage.h"
65 class InsetFormula::PreviewImpl : public grfx::PreviewedInset {
68 PreviewImpl(InsetFormula & p) : PreviewedInset(p) {}
72 bool previewWanted() const;
74 string const latexString() const;
76 InsetFormula & parent() const
78 return *static_cast<InsetFormula*>(inset());
84 InsetFormula::InsetFormula()
85 : par_(MathAtom(new MathHullInset)),
86 preview_(new PreviewImpl(*this))
90 InsetFormula::InsetFormula(InsetFormula const & other)
91 : InsetFormulaBase(other),
93 preview_(new PreviewImpl(*this))
97 InsetFormula::InsetFormula(BufferView * bv)
98 : par_(MathAtom(new MathHullInset)),
99 preview_(new PreviewImpl(*this))
101 view_ = bv->owner()->view();
105 InsetFormula::InsetFormula(string const & data)
106 : par_(MathAtom(new MathHullInset)),
107 preview_(new PreviewImpl(*this))
111 if (!mathed_parse_normal(par_, data))
112 lyxerr << "cannot interpret '" << data << "' as math\n";
117 InsetFormula::~InsetFormula()
121 Inset * InsetFormula::clone(Buffer const &, bool) const
123 return new InsetFormula(*this);
127 void InsetFormula::write(Buffer const *, ostream & os) const
129 WriteStream wi(os, false, false);
130 os << par_->fileInsetLabel() << " ";
135 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
137 WriteStream wi(os, fragile, true);
143 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
148 TextPainter tpain(par()->width(), par()->height());
149 par()->drawT(tpain, 0, par()->ascent());
151 // reset metrics cache to "real" values
153 return tpain.textheight();
155 WriteStream wi(os, false, true);
156 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
162 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
164 return docbook(buf, os, false);
168 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
171 ms << MTag("equation");
174 int res = ascii(buf, ms.os(), 0);
180 ms << ETag("equation");
181 return ms.line() + res;
185 void InsetFormula::read(Buffer const *, LyXLex & lex)
187 mathed_parse_normal(par_, lex);
192 //ostream & operator<<(ostream & os, LyXCursor const & c)
194 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
199 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
200 int y, float & xx, bool) const
202 // This initiates the loading of the preview, so should come
203 // before the metrics are computed.
204 preview_->setView(bv);
205 bool const use_preview = preview_->previewReady();
207 int const x = int(xx);
208 int const w = width(bv, font);
209 int const d = descent(bv, font);
210 int const a = ascent(bv, font);
213 MathPainterInfo pi(bv->painter());
216 pi.pain.image(x, y - a, w, h,
217 *(preview_->pimage()->image(*this, *bv)));
219 pi.base.style = LM_ST_TEXT;
221 pi.base.font.setColor(LColor::math);
222 if (lcolor.getX11Name(LColor::mathbg)
223 != lcolor.getX11Name(LColor::background))
224 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
227 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
229 mathcursor->drawSelection(pi);
230 //pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
233 par_->draw(pi, x, y);
240 setCursorVisible(false);
244 vector<string> const InsetFormula::getLabelList() const
247 par()->getLabelList(res);
252 Inset::Code InsetFormula::lyxCode() const
254 return Inset::MATH_CODE;
258 void InsetFormula::validate(LaTeXFeatures & features) const
260 par_->validate(features);
264 bool InsetFormula::insetAllowed(Inset::Code code) const
267 (code == Inset::LABEL_CODE)
268 || code == Inset::REF_CODE
269 || code == Inset::ERT_CODE;
273 int InsetFormula::ascent(BufferView *, LyXFont const &) const
275 return preview_->previewReady() ?
276 preview_->pimage()->ascent() : 1 + par_->ascent();
280 int InsetFormula::descent(BufferView *, LyXFont const &) const
282 if (!preview_->previewReady())
283 return 1 + par_->descent();
285 int const descent = preview_->pimage()->descent();
286 return display() ? descent + 12 : descent;
290 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
293 return preview_->previewReady() ?
294 preview_->pimage()->width() : par_->width();
299 void InsetFormula::mutate(string const & type)
301 par_.nucleus()->mutate(type);
310 void InsetFormula::addPreview(grfx::PreviewLoader & ploader) const
312 preview_->addPreview(ploader);
316 void InsetFormula::generatePreview() const
318 preview_->generatePreview();
322 bool InsetFormula::PreviewImpl::previewWanted() const
324 return !parent().par_->asNestInset()->editing();
328 string const InsetFormula::PreviewImpl::latexString() const
331 WriteStream wi(ls, false, false);
332 parent().par_->write(wi);
333 return ls.str().c_str();