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/PreviewedInset.h"
29 #include "graphics/PreviewImage.h"
31 #include "support/std_sstream.h"
33 #include <boost/bind.hpp>
37 using std::ostringstream;
43 class InsetFormula::PreviewImpl : public PreviewedInset {
46 PreviewImpl(InsetFormula const & p) : parent_(p) {}
50 bool previewWanted(Buffer const &) const;
52 string const latexString(Buffer const &) const;
54 InsetFormula const & parent_;
58 InsetFormula::InsetFormula(bool chemistry)
59 : par_(MathAtom(new MathHullInset)),
60 preview_(new PreviewImpl(*this))
62 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
68 InsetFormula::InsetFormula(InsetFormula const & other)
69 : InsetFormulaBase(other),
71 preview_(new PreviewImpl(*this))
73 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
77 InsetFormula::InsetFormula(BufferView *)
78 : par_(MathAtom(new MathHullInset)),
79 preview_(new PreviewImpl(*this))
81 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
85 InsetFormula::InsetFormula(string const & data)
86 : par_(MathAtom(new MathHullInset)),
87 preview_(new PreviewImpl(*this))
89 preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
92 if (!mathed_parse_normal(par_, data))
93 lyxerr << "cannot interpret '" << data << "' as math" << endl;
97 InsetFormula::~InsetFormula()
101 auto_ptr<InsetBase> InsetFormula::clone() const
103 return auto_ptr<InsetBase>(new InsetFormula(*this));
107 void InsetFormula::write(Buffer const &, ostream & os) const
109 WriteStream wi(os, false, false);
110 os << par_->fileInsetLabel() << ' ';
115 int InsetFormula::latex(Buffer const &, ostream & os,
116 LatexRunParams const & runparams) const
118 WriteStream wi(os, runparams.moving_arg, true);
124 int InsetFormula::ascii(Buffer const &, ostream & os, int) const
126 if (0 && display()) {
129 par()->metricsT(mi, dim);
130 TextPainter tpain(dim.width(), dim.height());
131 par()->drawT(tpain, 0, dim.ascent());
133 // reset metrics cache to "real" values
135 return tpain.textheight();
137 WriteStream wi(os, false, true);
138 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
144 int InsetFormula::linuxdoc(Buffer const & buf, ostream & os) const
146 return docbook(buf, os, false);
150 int InsetFormula::docbook(Buffer const & buf, ostream & os, bool) const
153 ms << MTag("equation");
156 int res = ascii(buf, ms.os(), 0);
162 ms << ETag("equation");
163 return ms.line() + res;
167 void InsetFormula::read(Buffer const &, LyXLex & lex)
169 mathed_parse_normal(par_, lex);
170 // remove extra 'mathrm' for chemistry stuff.
171 // will be re-added on write
172 if (par_->asHullInset()->getType() =="chemistry") {
173 lyxerr << "this is chemistry" << endl;
174 if (par_->cell(0).size() == 1) {
175 lyxerr << "this is size 1" << endl;
176 if (par_->cell(0)[0]->asFontInset()) {
177 lyxerr << "this is a font inset "
178 << "replacing " << par_.nucleus()->cell(0) <<
179 " with " << par_->cell(0)[0]->cell(0) << endl;
187 //ostream & operator<<(ostream & os, LyXCursor const & c)
189 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
194 void InsetFormula::draw(PainterInfo & pi, int x, int y) const
196 BufferView * bv = pi.base.bv;
199 // The previews are drawn only when we're not editing the inset.
200 bool const editing_inset = mathcursor && mathcursor->formula() == this;
201 bool const use_preview = !editing_inset && preview_->previewReady();
203 int const w = dim_.wid;
204 int const d = dim_.des;
205 int const a = dim_.asc;
209 pi.pain.image(x + 1, y - a, w, h, // one pixel gap in front
210 *(preview_->pimage()->image()));
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);
221 mathcursor->drawSelection(pi);
222 //p.pain.rectangle(x, y - a, w, h, LColor::mathframe);
225 par_->draw(p, x + offset_, y);
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
265 bool const editing_inset = mathcursor && mathcursor->formula() == this;
266 bool const use_preview = !editing_inset && preview_->previewReady();
269 dim.asc = preview_->pimage()->ascent();
270 dim.des = preview_->pimage()->descent();
271 // insert a one pixel gap in front of the formula
272 dim.wid = 1 + preview_->pimage()->width();
277 mi.base.style = LM_ST_TEXT;
278 mi.base.font.setColor(LColor::math);
279 par()->metrics(mi, dim);
285 offset_ = (m.base.textwidth - dim.wid) / 2;
286 dim.wid = m.base.textwidth;
295 void InsetFormula::mutate(string const & type)
297 par_.nucleus()->mutate(type);
305 void InsetFormula::statusChanged() const
308 view()->updateInset(this);
312 void InsetFormula::addPreview(lyx::graphics::PreviewLoader & ploader) const
314 preview_->addPreview(ploader);
318 void InsetFormula::generatePreview(Buffer const & buffer) const
320 preview_->generatePreview(buffer);
324 bool InsetFormula::PreviewImpl::previewWanted(Buffer const &) const
326 return !parent_.par_->asNestInset()->editing();
330 string const InsetFormula::PreviewImpl::latexString(Buffer const &) const
333 WriteStream wi(ls, false, false);
334 parent_.par_->write(wi);