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"
26 #include "frontends/Painter.h"
28 #include "graphics/PreviewedInset.h"
29 #include "graphics/PreviewImage.h"
38 class InsetFormula::PreviewImpl : public lyx::graphics::PreviewedInset {
41 PreviewImpl(InsetFormula & p) : PreviewedInset(p) {}
45 bool previewWanted() const;
47 string const latexString() const;
49 InsetFormula & parent() const
51 return *static_cast<InsetFormula*>(inset());
57 InsetFormula::InsetFormula(bool chemistry)
58 : par_(MathAtom(new MathHullInset)),
59 preview_(new PreviewImpl(*this))
66 InsetFormula::InsetFormula(InsetFormula const & other)
67 : InsetFormulaBase(other),
69 preview_(new PreviewImpl(*this))
73 InsetFormula::InsetFormula(BufferView *)
74 : par_(MathAtom(new MathHullInset)),
75 preview_(new PreviewImpl(*this))
77 //view_ = bv->owner()->view();
81 InsetFormula::InsetFormula(string const & data)
82 : par_(MathAtom(new MathHullInset)),
83 preview_(new PreviewImpl(*this))
87 if (!mathed_parse_normal(par_, data))
88 lyxerr << "cannot interpret '" << data << "' as math" << endl;
93 InsetFormula::~InsetFormula()
97 auto_ptr<InsetBase> InsetFormula::clone() const
99 return auto_ptr<InsetBase>(new InsetFormula(*this));
103 void InsetFormula::write(Buffer const &, ostream & os) const
105 WriteStream wi(os, false, false);
106 os << par_->fileInsetLabel() << ' ';
111 int InsetFormula::latex(Buffer const &, ostream & os,
112 LatexRunParams const & runparams) const
114 WriteStream wi(os, runparams.moving_arg, true);
120 int InsetFormula::ascii(Buffer const &, ostream & os, int) const
122 if (0 && display()) {
125 par()->metricsT(mi, dim);
126 TextPainter tpain(dim.width(), dim.height());
127 par()->drawT(tpain, 0, dim.ascent());
129 // reset metrics cache to "real" values
131 return tpain.textheight();
133 WriteStream wi(os, false, true);
134 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
140 int InsetFormula::linuxdoc(Buffer const & buf, ostream & os) const
142 return docbook(buf, os, false);
146 int InsetFormula::docbook(Buffer const & buf, ostream & os, bool) const
149 ms << MTag("equation");
152 int res = ascii(buf, ms.os(), 0);
158 ms << ETag("equation");
159 return ms.line() + res;
163 void InsetFormula::read(Buffer const &, LyXLex & lex)
165 mathed_parse_normal(par_, lex);
166 // remove extra 'mathrm' for chemistry stuff.
167 // will be re-added on write
168 if (par_->asHullInset()->getType() =="chemistry") {
169 lyxerr << "this is chemistry" << endl;
170 if (par_->cell(0).size() == 1) {
171 lyxerr << "this is size 1" << endl;
172 if (par_->cell(0)[0]->asFontInset()) {
173 lyxerr << "this is a font inset "
174 << "replacing " << par_.nucleus()->cell(0) <<
175 " with " << par_->cell(0)[0]->cell(0) << endl;
183 //ostream & operator<<(ostream & os, LyXCursor const & c)
185 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
190 void InsetFormula::draw(PainterInfo & pi, int x, int y) const
193 // This initiates the loading of the preview, so should come
194 // before the metrics are computed.
195 bool const use_preview = preview_->previewReady();
197 int const w = dim_.wid;
198 int const d = dim_.des;
199 int const a = dim_.asc;
203 pi.pain.image(x + 1, y - a, w, h, // one pixel gap in front
204 *(preview_->pimage()->image()));
206 PainterInfo p(pi.base.bv);
207 p.base.style = LM_ST_TEXT;
208 p.base.font = pi.base.font;
209 p.base.font.setColor(LColor::math);
210 if (lcolor.getX11Name(LColor::mathbg)
211 != lcolor.getX11Name(LColor::background))
212 p.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
215 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
217 mathcursor->drawSelection(pi);
218 //p.pain.rectangle(x, y - a, w, h, LColor::mathframe);
221 par_->draw(p, x + offset_, y);
229 void InsetFormula::getLabelList(vector<string> & res) const
231 par()->getLabelList(res);
235 InsetOld::Code InsetFormula::lyxCode() const
237 return InsetOld::MATH_CODE;
241 void InsetFormula::validate(LaTeXFeatures & features) const
243 par_->validate(features);
247 bool InsetFormula::insetAllowed(InsetOld::Code code) const
250 code == InsetOld::LABEL_CODE
251 || code == InsetOld::REF_CODE
252 || code == InsetOld::ERT_CODE;
256 void InsetFormula::metrics(MetricsInfo & m, Dimension & dim) const
259 if (preview_->previewReady()) {
260 dim.asc = preview_->pimage()->ascent();
261 dim.des = preview_->pimage()->descent();
262 // insert a one pixel gap in front of the formula
263 dim.wid = 1 + preview_->pimage()->width();
268 mi.base.style = LM_ST_TEXT;
269 mi.base.font.setColor(LColor::math);
270 par()->metrics(mi, dim);
276 offset_ = (m.base.textwidth - dim.wid) / 2;
277 dim.wid = m.base.textwidth;
286 void InsetFormula::mutate(string const & type)
288 par_.nucleus()->mutate(type);
296 void InsetFormula::addPreview(lyx::graphics::PreviewLoader & ploader) const
298 preview_->addPreview(ploader);
302 void InsetFormula::generatePreview() const
304 preview_->generatePreview();
308 bool InsetFormula::PreviewImpl::previewWanted() const
310 return !parent().par_->asNestInset()->editing();
314 string const InsetFormula::PreviewImpl::latexString() const
317 WriteStream wi(ls, false, false);
318 parent().par_->write(wi);
319 return STRCONV(ls.str());