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"
25 #include "outputparams.h"
27 #include "frontends/Painter.h"
29 #include "graphics/PreviewLoader.h"
31 #include "insets/render_preview.h"
33 #include "support/std_sstream.h"
35 #include <boost/bind.hpp>
39 using std::ostringstream;
45 InsetFormula::InsetFormula()
46 : par_(MathAtom(new MathHullInset)),
47 preview_(new RenderPreview)
49 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
53 InsetFormula::InsetFormula(InsetFormula const & other)
54 : InsetFormulaBase(other),
56 preview_(new RenderPreview)
58 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
62 InsetFormula::InsetFormula(string const & data)
63 : par_(MathAtom(new MathHullInset)),
64 preview_(new RenderPreview)
66 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
69 if (!mathed_parse_normal(par_, data))
70 lyxerr << "cannot interpret '" << data << "' as math" << endl;
74 InsetFormula::~InsetFormula()
78 auto_ptr<InsetBase> InsetFormula::clone() const
80 return auto_ptr<InsetBase>(new InsetFormula(*this));
84 void InsetFormula::write(Buffer const &, ostream & os) const
86 WriteStream wi(os, false, false);
87 os << par_->fileInsetLabel() << ' ';
92 int InsetFormula::latex(Buffer const &, ostream & os,
93 OutputParams const & runparams) const
95 WriteStream wi(os, runparams.moving_arg, true);
101 int InsetFormula::plaintext(Buffer const &, ostream & os,
102 OutputParams const &) const
104 if (0 && display()) {
107 par()->metricsT(mi, dim);
108 TextPainter tpain(dim.width(), dim.height());
109 par()->drawT(tpain, 0, dim.ascent());
111 // reset metrics cache to "real" values
113 return tpain.textheight();
115 WriteStream wi(os, false, true);
116 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
122 int InsetFormula::linuxdoc(Buffer const & buf, ostream & os,
123 OutputParams const & runparams) const
125 return docbook(buf, os, runparams);
129 int InsetFormula::docbook(Buffer const & buf, ostream & os,
130 OutputParams const & runparams) const
133 ms << MTag("equation");
136 int res = plaintext(buf, ms.os(), runparams);
142 ms << ETag("equation");
143 return ms.line() + res;
147 void InsetFormula::read(Buffer const &, LyXLex & lex)
149 mathed_parse_normal(par_, lex);
150 // remove extra 'mathrm' for chemistry stuff.
151 // will be re-added on write
152 if (par_->asHullInset()->getType() =="chemistry") {
153 lyxerr << "this is chemistry" << endl;
154 if (par_->cell(0).size() == 1) {
155 lyxerr << "this is size 1" << endl;
156 if (par_->cell(0)[0]->asFontInset()) {
157 lyxerr << "this is a font inset "
158 << "replacing " << par_.nucleus()->cell(0) <<
159 " with " << par_->cell(0)[0]->cell(0) << endl;
169 bool editing_inset(InsetFormula const * inset)
172 (const_cast<InsetFormulaBase const *>(mathcursor::formula()) ==
179 void InsetFormula::draw(PainterInfo & pi, int x, int y) const
184 // The previews are drawn only when we're not editing the inset.
185 bool const use_preview = !editing_inset(this)
186 && RenderPreview::activated()
187 && preview_->previewReady();
189 int const w = dim_.wid;
190 int const d = dim_.des;
191 int const a = dim_.asc;
195 // one pixel gap in front
196 preview_->draw(pi, x + 1, y);
198 PainterInfo p(pi.base.bv);
199 p.base.style = LM_ST_TEXT;
200 p.base.font = pi.base.font;
201 p.base.font.setColor(LColor::math);
202 if (lcolor.getX11Name(LColor::mathbg)
203 != lcolor.getX11Name(LColor::background))
204 p.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
206 if (editing_inset(this)) {
207 mathcursor::drawSelection(pi);
208 //p.pain.rectangle(x, y - a, w, h, LColor::mathframe);
216 void InsetFormula::getLabelList(Buffer const & buffer,
217 vector<string> & res) const
219 par()->getLabelList(buffer, res);
223 InsetOld::Code InsetFormula::lyxCode() const
225 return InsetOld::MATH_CODE;
229 void InsetFormula::validate(LaTeXFeatures & features) const
231 par_->validate(features);
235 bool InsetFormula::insetAllowed(InsetOld::Code code) const
238 code == InsetOld::LABEL_CODE
239 || code == InsetOld::REF_CODE
240 || code == InsetOld::ERT_CODE;
244 void InsetFormula::metrics(MetricsInfo & m, Dimension & dim) const
246 bool const use_preview = !editing_inset(this)
247 && RenderPreview::activated()
248 && preview_->previewReady();
251 preview_->metrics(m, dim);
252 // insert a one pixel gap in front of the formula
258 mi.base.style = LM_ST_TEXT;
259 mi.base.font.setColor(LColor::math);
260 par()->metrics(mi, dim);
269 void InsetFormula::mutate(string const & type)
271 par_.nucleus()->mutate(type);
279 void InsetFormula::statusChanged() const
281 LyX::cref().updateInset(this);
287 string const latex_string(InsetFormula const & inset, Buffer const &)
290 WriteStream wi(os, false, false);
291 inset.par()->write(wi);
298 void InsetFormula::addPreview(lyx::graphics::PreviewLoader & ploader) const
300 string const snippet = latex_string(*this, ploader.buffer());
301 preview_->addPreview(snippet, ploader);
305 void InsetFormula::generatePreview(Buffer const & buffer) const
307 string const snippet = latex_string(*this, buffer);
308 preview_->addPreview(snippet, buffer);
309 preview_->startLoading(buffer);