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(bool chemistry)
46 : par_(MathAtom(new MathHullInset)),
47 preview_(new RenderPreview)
49 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
55 InsetFormula::InsetFormula(InsetFormula const & other)
56 : InsetFormulaBase(other),
58 preview_(new RenderPreview)
60 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
64 InsetFormula::InsetFormula(BufferView *)
65 : par_(MathAtom(new MathHullInset)),
66 preview_(new RenderPreview)
68 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
72 InsetFormula::InsetFormula(string const & data)
73 : par_(MathAtom(new MathHullInset)),
74 preview_(new RenderPreview)
76 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
79 if (!mathed_parse_normal(par_, data))
80 lyxerr << "cannot interpret '" << data << "' as math" << endl;
84 InsetFormula::~InsetFormula()
88 auto_ptr<InsetBase> InsetFormula::clone() const
90 return auto_ptr<InsetBase>(new InsetFormula(*this));
94 void InsetFormula::write(Buffer const &, ostream & os) const
96 WriteStream wi(os, false, false);
97 os << par_->fileInsetLabel() << ' ';
102 int InsetFormula::latex(Buffer const &, ostream & os,
103 OutputParams const & runparams) const
105 WriteStream wi(os, runparams.moving_arg, true);
111 int InsetFormula::plaintext(Buffer const &, ostream & os,
112 OutputParams const &) const
114 if (0 && display()) {
117 par()->metricsT(mi, dim);
118 TextPainter tpain(dim.width(), dim.height());
119 par()->drawT(tpain, 0, dim.ascent());
121 // reset metrics cache to "real" values
123 return tpain.textheight();
125 WriteStream wi(os, false, true);
126 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
132 int InsetFormula::linuxdoc(Buffer const & buf, ostream & os,
133 OutputParams const & runparams) const
135 return docbook(buf, os, runparams);
139 int InsetFormula::docbook(Buffer const & buf, ostream & os,
140 OutputParams const & runparams) const
143 ms << MTag("equation");
146 int res = plaintext(buf, ms.os(), runparams);
152 ms << ETag("equation");
153 return ms.line() + res;
157 void InsetFormula::read(Buffer const &, LyXLex & lex)
159 mathed_parse_normal(par_, lex);
160 // remove extra 'mathrm' for chemistry stuff.
161 // will be re-added on write
162 if (par_->asHullInset()->getType() =="chemistry") {
163 lyxerr << "this is chemistry" << endl;
164 if (par_->cell(0).size() == 1) {
165 lyxerr << "this is size 1" << endl;
166 if (par_->cell(0)[0]->asFontInset()) {
167 lyxerr << "this is a font inset "
168 << "replacing " << par_.nucleus()->cell(0) <<
169 " with " << par_->cell(0)[0]->cell(0) << endl;
177 //ostream & operator<<(ostream & os, LyXCursor const & c)
179 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
186 bool editing_inset(InsetFormula const * inset)
189 (const_cast<InsetFormulaBase const *>(mathcursor->formula()) ==
196 void InsetFormula::draw(PainterInfo & pi, int x, int y) const
198 // The previews are drawn only when we're not editing the inset.
199 bool const use_preview = (!editing_inset(this) &&
200 RenderPreview::activated() &&
201 preview_->previewReady());
203 int const w = dim_.wid;
204 int const d = dim_.des;
205 int const a = dim_.asc;
209 // one pixel gap in front
210 preview_->draw(pi, x + 1, y);
212 PainterInfo p(pi.base.bv);
213 p.base.style = LM_ST_TEXT;
214 p.base.font = pi.base.font;
215 p.base.font.setColor(LColor::math);
216 if (lcolor.getX11Name(LColor::mathbg)
217 != lcolor.getX11Name(LColor::background))
218 p.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
220 if (editing_inset(this)) {
221 mathcursor->drawSelection(pi);
222 //p.pain.rectangle(x, y - a, w, h, LColor::mathframe);
233 void InsetFormula::getLabelList(Buffer const & buffer,
234 vector<string> & res) const
236 par()->getLabelList(buffer, res);
240 InsetOld::Code InsetFormula::lyxCode() const
242 return InsetOld::MATH_CODE;
246 void InsetFormula::validate(LaTeXFeatures & features) const
248 par_->validate(features);
252 bool InsetFormula::insetAllowed(InsetOld::Code code) const
255 code == InsetOld::LABEL_CODE
256 || code == InsetOld::REF_CODE
257 || code == InsetOld::ERT_CODE;
261 void InsetFormula::metrics(MetricsInfo & m, Dimension & dim) const
263 bool const use_preview = (!editing_inset(this) &&
264 RenderPreview::activated() &&
265 preview_->previewReady());
268 preview_->metrics(m, dim);
269 // insert a one pixel gap in front of the formula
275 mi.base.style = LM_ST_TEXT;
276 mi.base.font.setColor(LColor::math);
277 par()->metrics(mi, dim);
286 void InsetFormula::mutate(string const & type)
288 par_.nucleus()->mutate(type);
296 void InsetFormula::statusChanged() const
298 LyX::cref().updateInset(this);
304 string const latex_string(InsetFormula const & inset, Buffer const &)
307 WriteStream wi(ls, false, false);
308 inset.par()->write(wi);
315 void InsetFormula::addPreview(lyx::graphics::PreviewLoader & ploader) const
317 string const snippet = latex_string(*this, ploader.buffer());
318 preview_->addPreview(snippet, ploader);
322 void InsetFormula::generatePreview(Buffer const & buffer) const
324 string const snippet = latex_string(*this, buffer);
325 preview_->addPreview(snippet, buffer);
326 preview_->startLoading(buffer);