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"
22 #include "latexrunparams.h"
25 #include "frontends/Painter.h"
27 #include "graphics/PreviewedInset.h"
28 #include "graphics/PreviewImage.h"
30 #include "support/std_sstream.h"
35 using std::ostringstream;
41 class InsetFormula::PreviewImpl : public lyx::graphics::PreviewedInset {
44 PreviewImpl(InsetFormula & p) : PreviewedInset(p) {}
48 bool previewWanted(Buffer const &) const;
50 string const latexString(Buffer const &) const;
52 InsetFormula const & parent() const
54 return dynamic_cast<InsetFormula const &>(inset());
60 InsetFormula::InsetFormula(bool chemistry)
61 : par_(MathAtom(new MathHullInset)),
62 preview_(new PreviewImpl(*this))
69 InsetFormula::InsetFormula(InsetFormula const & other)
70 : InsetFormulaBase(other),
72 preview_(new PreviewImpl(*this))
76 InsetFormula::InsetFormula(BufferView *)
77 : par_(MathAtom(new MathHullInset)),
78 preview_(new PreviewImpl(*this))
80 //view_ = bv->owner()->view();
84 InsetFormula::InsetFormula(string const & data)
85 : par_(MathAtom(new MathHullInset)),
86 preview_(new PreviewImpl(*this))
90 if (!mathed_parse_normal(par_, data))
91 lyxerr << "cannot interpret '" << data << "' as math" << endl;
96 InsetFormula::~InsetFormula()
100 auto_ptr<InsetBase> InsetFormula::clone() const
102 return auto_ptr<InsetBase>(new InsetFormula(*this));
106 void InsetFormula::write(Buffer const &, ostream & os) const
108 WriteStream wi(os, false, false);
109 os << par_->fileInsetLabel() << ' ';
114 int InsetFormula::latex(Buffer const &, ostream & os,
115 LatexRunParams const & runparams) const
117 WriteStream wi(os, runparams.moving_arg, true);
123 int InsetFormula::ascii(Buffer const &, ostream & os, int) const
125 if (0 && display()) {
128 par()->metricsT(mi, dim);
129 TextPainter tpain(dim.width(), dim.height());
130 par()->drawT(tpain, 0, dim.ascent());
132 // reset metrics cache to "real" values
134 return tpain.textheight();
136 WriteStream wi(os, false, true);
137 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
143 int InsetFormula::linuxdoc(Buffer const & buf, ostream & os) const
145 return docbook(buf, os, false);
149 int InsetFormula::docbook(Buffer const & buf, ostream & os, bool) const
152 ms << MTag("equation");
155 int res = ascii(buf, ms.os(), 0);
161 ms << ETag("equation");
162 return ms.line() + res;
166 void InsetFormula::read(Buffer const &, LyXLex & lex)
168 mathed_parse_normal(par_, lex);
169 // remove extra 'mathrm' for chemistry stuff.
170 // will be re-added on write
171 if (par_->asHullInset()->getType() =="chemistry") {
172 lyxerr << "this is chemistry" << endl;
173 if (par_->cell(0).size() == 1) {
174 lyxerr << "this is size 1" << endl;
175 if (par_->cell(0)[0]->asFontInset()) {
176 lyxerr << "this is a font inset "
177 << "replacing " << par_.nucleus()->cell(0) <<
178 " with " << par_->cell(0)[0]->cell(0) << endl;
186 //ostream & operator<<(ostream & os, LyXCursor const & c)
188 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
193 void InsetFormula::draw(PainterInfo & pi, int x, int y) const
196 // This initiates the loading of the preview, so should come
197 // before the metrics are computed.
198 bool const use_preview = preview_->previewReady();
200 int const w = dim_.wid;
201 int const d = dim_.des;
202 int const a = dim_.asc;
206 pi.pain.image(x + 1, y - a, w, h, // one pixel gap in front
207 *(preview_->pimage()->image()));
209 PainterInfo p(pi.base.bv);
210 p.base.style = LM_ST_TEXT;
211 p.base.font = pi.base.font;
212 p.base.font.setColor(LColor::math);
213 if (lcolor.getX11Name(LColor::mathbg)
214 != lcolor.getX11Name(LColor::background))
215 p.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
218 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
220 mathcursor->drawSelection(pi);
221 //p.pain.rectangle(x, y - a, w, h, LColor::mathframe);
224 par_->draw(p, x + offset_, y);
232 void InsetFormula::getLabelList(Buffer const & buffer,
233 vector<string> & res) const
235 par()->getLabelList(buffer, res);
239 InsetOld::Code InsetFormula::lyxCode() const
241 return InsetOld::MATH_CODE;
245 void InsetFormula::validate(LaTeXFeatures & features) const
247 par_->validate(features);
251 bool InsetFormula::insetAllowed(InsetOld::Code code) const
254 code == InsetOld::LABEL_CODE
255 || code == InsetOld::REF_CODE
256 || code == InsetOld::ERT_CODE;
260 void InsetFormula::metrics(MetricsInfo & m, Dimension & dim) const
263 if (preview_->previewReady()) {
264 dim.asc = preview_->pimage()->ascent();
265 dim.des = preview_->pimage()->descent();
266 // insert a one pixel gap in front of the formula
267 dim.wid = 1 + preview_->pimage()->width();
272 mi.base.style = LM_ST_TEXT;
273 mi.base.font.setColor(LColor::math);
274 par()->metrics(mi, dim);
280 offset_ = (m.base.textwidth - dim.wid) / 2;
281 dim.wid = m.base.textwidth;
290 void InsetFormula::mutate(string const & type)
292 par_.nucleus()->mutate(type);
300 void InsetFormula::addPreview(lyx::graphics::PreviewLoader & ploader) const
302 preview_->addPreview(ploader);
306 void InsetFormula::generatePreview(Buffer const & buffer) const
308 preview_->generatePreview(buffer);
312 bool InsetFormula::PreviewImpl::previewWanted(Buffer const &) const
314 return !parent().par_->asNestInset()->editing();
318 string const InsetFormula::PreviewImpl::latexString(Buffer const &) const
321 WriteStream wi(ls, false, false);
322 parent().par_->write(wi);