3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
9 * Full author contact details are available in file CREDITS.
15 #include "math_cursor.h"
16 #include "math_parser.h"
17 #include "math_hullinset.h"
18 #include "math_mathmlstream.h"
19 #include "textpainter.h"
21 #include "BufferView.h"
23 #include "latexrunparams.h"
26 #include "frontends/Painter.h"
28 #include "graphics/PreviewLoader.h"
30 #include "insets/render_preview.h"
32 #include "support/std_sstream.h"
34 #include <boost/bind.hpp>
38 using std::ostringstream;
44 InsetFormula::InsetFormula(bool chemistry)
45 : par_(MathAtom(new MathHullInset)),
46 preview_(new RenderPreview)
48 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
54 InsetFormula::InsetFormula(InsetFormula const & other)
55 : InsetFormulaBase(other),
57 preview_(new RenderPreview)
59 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
63 InsetFormula::InsetFormula(BufferView *)
64 : par_(MathAtom(new MathHullInset)),
65 preview_(new RenderPreview)
67 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
71 InsetFormula::InsetFormula(string const & data)
72 : par_(MathAtom(new MathHullInset)),
73 preview_(new RenderPreview)
75 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
78 if (!mathed_parse_normal(par_, data))
79 lyxerr << "cannot interpret '" << data << "' as math" << endl;
83 InsetFormula::~InsetFormula()
87 auto_ptr<InsetBase> InsetFormula::clone() const
89 return auto_ptr<InsetBase>(new InsetFormula(*this));
93 void InsetFormula::write(Buffer const &, ostream & os) const
95 WriteStream wi(os, false, false);
96 os << par_->fileInsetLabel() << ' ';
101 int InsetFormula::latex(Buffer const &, ostream & os,
102 LatexRunParams const & runparams) const
104 WriteStream wi(os, runparams.moving_arg, true);
110 int InsetFormula::ascii(Buffer const &, ostream & os, int) const
112 if (0 && display()) {
115 par()->metricsT(mi, dim);
116 TextPainter tpain(dim.width(), dim.height());
117 par()->drawT(tpain, 0, dim.ascent());
119 // reset metrics cache to "real" values
121 return tpain.textheight();
123 WriteStream wi(os, false, true);
124 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
130 int InsetFormula::linuxdoc(Buffer const & buf, ostream & os) const
132 return docbook(buf, os, false);
136 int InsetFormula::docbook(Buffer const & buf, ostream & os, bool) const
139 ms << MTag("equation");
142 int res = ascii(buf, ms.os(), 0);
148 ms << ETag("equation");
149 return ms.line() + res;
153 void InsetFormula::read(Buffer const &, LyXLex & lex)
155 mathed_parse_normal(par_, lex);
156 // remove extra 'mathrm' for chemistry stuff.
157 // will be re-added on write
158 if (par_->asHullInset()->getType() =="chemistry") {
159 lyxerr << "this is chemistry" << endl;
160 if (par_->cell(0).size() == 1) {
161 lyxerr << "this is size 1" << endl;
162 if (par_->cell(0)[0]->asFontInset()) {
163 lyxerr << "this is a font inset "
164 << "replacing " << par_.nucleus()->cell(0) <<
165 " with " << par_->cell(0)[0]->cell(0) << endl;
173 //ostream & operator<<(ostream & os, LyXCursor const & c)
175 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
182 bool editing_inset(InsetFormula const * inset)
185 (const_cast<InsetFormulaBase const *>(mathcursor->formula()) ==
192 void InsetFormula::draw(PainterInfo & pi, int x, int y) const
196 // The previews are drawn only when we're not editing the inset.
197 bool const use_preview = (!editing_inset(this) &&
198 RenderPreview::activated() &&
199 preview_->previewReady());
201 int const w = dim_.wid;
202 int const d = dim_.des;
203 int const a = dim_.asc;
207 // one pixel gap in front
208 preview_->draw(pi, x + 1, y);
210 PainterInfo p(pi.base.bv);
211 p.base.style = LM_ST_TEXT;
212 p.base.font = pi.base.font;
213 p.base.font.setColor(LColor::math);
214 if (lcolor.getX11Name(LColor::mathbg)
215 != lcolor.getX11Name(LColor::background))
216 p.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
218 if (editing_inset(this)) {
219 mathcursor->drawSelection(pi);
220 //p.pain.rectangle(x, y - a, w, h, LColor::mathframe);
223 par_->draw(p, x + offset_, y);
231 void InsetFormula::getLabelList(Buffer const & buffer,
232 vector<string> & res) const
234 par()->getLabelList(buffer, res);
238 InsetOld::Code InsetFormula::lyxCode() const
240 return InsetOld::MATH_CODE;
244 void InsetFormula::validate(LaTeXFeatures & features) const
246 par_->validate(features);
250 bool InsetFormula::insetAllowed(InsetOld::Code code) const
253 code == InsetOld::LABEL_CODE
254 || code == InsetOld::REF_CODE
255 || code == InsetOld::ERT_CODE;
259 void InsetFormula::metrics(MetricsInfo & m, Dimension & dim) const
261 bool const use_preview = (!editing_inset(this) &&
262 RenderPreview::activated() &&
263 preview_->previewReady());
266 preview_->metrics(m, dim);
267 // insert a one pixel gap in front of the formula
273 mi.base.style = LM_ST_TEXT;
274 mi.base.font.setColor(LColor::math);
275 par()->metrics(mi, dim);
281 offset_ = (m.base.textwidth - dim.wid) / 2;
282 dim.wid = m.base.textwidth;
291 void InsetFormula::mutate(string const & type)
293 par_.nucleus()->mutate(type);
301 void InsetFormula::statusChanged() const
304 view()->updateInset(this);
310 string const latex_string(InsetFormula const & inset, Buffer const &)
313 WriteStream wi(ls, false, false);
314 inset.par()->write(wi);
321 void InsetFormula::addPreview(lyx::graphics::PreviewLoader & ploader) const
323 string const snippet = latex_string(*this, ploader.buffer());
324 preview_->addPreview(snippet, ploader);
328 void InsetFormula::generatePreview(Buffer const & buffer) const
330 string const snippet = latex_string(*this, buffer);
331 preview_->generatePreview(snippet, buffer);