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.
16 #include "math_data.h"
17 #include "math_parser.h"
18 #include "math_hullinset.h"
19 #include "math_mathmlstream.h"
20 #include "textpainter.h"
22 #include "BufferView.h"
27 #include "outputparams.h"
29 #include "frontends/Painter.h"
31 #include "graphics/PreviewLoader.h"
33 #include "insets/render_preview.h"
35 #include "support/std_sstream.h"
37 #include <boost/bind.hpp>
41 using std::ostringstream;
47 InsetFormula::InsetFormula()
48 : par_(MathAtom(new MathHullInset)),
49 preview_(new RenderPreview)
51 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(string const & data)
65 : par_(MathAtom(new MathHullInset)),
66 preview_(new RenderPreview)
68 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
71 if (!mathed_parse_normal(par_, data))
72 lyxerr << "cannot interpret '" << data << "' as math" << endl;
76 InsetFormula::~InsetFormula()
80 auto_ptr<InsetBase> InsetFormula::clone() const
82 return auto_ptr<InsetBase>(new InsetFormula(*this));
86 void InsetFormula::write(Buffer const &, ostream & os) const
88 WriteStream wi(os, false, false);
89 os << par_->fileInsetLabel() << ' ';
94 int InsetFormula::latex(Buffer const &, ostream & os,
95 OutputParams const & runparams) const
97 WriteStream wi(os, runparams.moving_arg, true);
103 int InsetFormula::plaintext(Buffer const &, ostream & os,
104 OutputParams const &) const
106 if (0 && display()) {
109 par()->metricsT(mi, dim);
110 TextPainter tpain(dim.width(), dim.height());
111 par()->drawT(tpain, 0, dim.ascent());
113 // reset metrics cache to "real" values
115 return tpain.textheight();
117 WriteStream wi(os, false, true);
118 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
124 int InsetFormula::linuxdoc(Buffer const & buf, ostream & os,
125 OutputParams const & runparams) const
127 return docbook(buf, os, runparams);
131 int InsetFormula::docbook(Buffer const & buf, ostream & os,
132 OutputParams const & runparams) const
135 ms << MTag("equation");
138 int res = plaintext(buf, ms.os(), runparams);
144 ms << ETag("equation");
145 return ms.line() + res;
149 void InsetFormula::read(Buffer const &, LyXLex & lex)
151 mathed_parse_normal(par_, lex);
152 // remove extra 'mathrm' for chemistry stuff.
153 // will be re-added on write
154 if (par_->asHullInset()->getType() =="chemistry") {
155 lyxerr << "this is chemistry" << endl;
156 if (par_->cell(0).size() == 1) {
157 lyxerr << "this is size 1" << endl;
158 if (par_->cell(0)[0]->asFontInset()) {
159 lyxerr << "this is a font inset "
160 << "replacing " << par_.nucleus()->cell(0) <<
161 " with " << par_->cell(0)[0]->cell(0) << endl;
169 void InsetFormula::draw(PainterInfo & pi, int x, int y) const
174 // The previews are drawn only when we're not editing the inset.
175 bool const use_preview = !pi.base.bv->cursor().isInside(this)
176 && RenderPreview::activated()
177 && preview_->previewReady();
179 int const w = dim_.wid;
180 int const d = dim_.des;
181 int const a = dim_.asc;
185 // one pixel gap in front
186 preview_->draw(pi, x + 1, y);
188 PainterInfo p(pi.base.bv);
189 p.base.style = LM_ST_TEXT;
190 p.base.font = pi.base.font;
191 p.base.font.setColor(LColor::math);
192 if (lcolor.getX11Name(LColor::mathbg)
193 != lcolor.getX11Name(LColor::background))
194 p.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
196 if (!pi.base.bv->cursor().isInside(this)) {
197 pi.base.bv->cursor().drawSelection(pi);
198 //p.pain.rectangle(x, y - a, w, h, LColor::mathframe);
206 void InsetFormula::getLabelList(Buffer const & buffer,
207 vector<string> & res) const
209 par()->getLabelList(buffer, res);
213 InsetOld::Code InsetFormula::lyxCode() const
215 return InsetOld::MATH_CODE;
219 void InsetFormula::validate(LaTeXFeatures & features) const
221 par_->validate(features);
225 bool InsetFormula::insetAllowed(InsetOld::Code code) const
228 code == InsetOld::LABEL_CODE
229 || code == InsetOld::REF_CODE
230 || code == InsetOld::ERT_CODE;
234 void InsetFormula::metrics(MetricsInfo & m, Dimension & dim) const
236 bool const use_preview = !m.base.bv->cursor().isInside(this)
237 && RenderPreview::activated()
238 && preview_->previewReady();
241 preview_->metrics(m, dim);
242 // insert a one pixel gap in front of the formula
248 mi.base.style = LM_ST_TEXT;
249 mi.base.font.setColor(LColor::math);
250 par()->metrics(mi, dim);
259 void InsetFormula::mutate(string const & type)
261 par_.nucleus()->mutate(type);
269 void InsetFormula::statusChanged() const
271 LyX::cref().updateInset(this);
277 string const latex_string(InsetFormula const & inset, Buffer const &)
280 WriteStream wi(os, false, false);
281 inset.par()->write(wi);
288 void InsetFormula::addPreview(lyx::graphics::PreviewLoader & ploader) const
290 string const snippet = latex_string(*this, ploader.buffer());
291 preview_->addPreview(snippet, ploader);
295 void InsetFormula::generatePreview(Buffer const & buffer) const
297 string const snippet = latex_string(*this, buffer);
298 preview_->addPreview(snippet, buffer);
299 preview_->startLoading(buffer);