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_charinset.h"
18 #include "math_arrayinset.h"
19 #include "metricsinfo.h"
20 #include "math_deliminset.h"
21 #include "math_hullinset.h"
22 #include "math_support.h"
23 #include "math_mathmlstream.h"
24 #include "textpainter.h"
26 #include "BufferView.h"
29 #include "latexrunparams.h"
31 #include "funcrequest.h"
34 #include "support/LOstream.h"
35 #include "support/LAssert.h"
36 #include "support/lyxlib.h"
37 #include "support/systemcall.h"
38 #include "support/filetools.h"
40 #include "frontends/Alert.h"
41 #include "frontends/LyXView.h"
42 #include "frontends/Painter.h"
44 #include "graphics/PreviewedInset.h"
45 #include "graphics/PreviewImage.h"
54 class InsetFormula::PreviewImpl : public lyx::graphics::PreviewedInset {
57 PreviewImpl(InsetFormula & p) : PreviewedInset(p) {}
61 bool previewWanted() const;
63 string const latexString() const;
65 InsetFormula & parent() const
67 return *static_cast<InsetFormula*>(inset());
73 InsetFormula::InsetFormula(bool chemistry)
74 : par_(MathAtom(new MathHullInset)),
75 preview_(new PreviewImpl(*this))
82 InsetFormula::InsetFormula(InsetFormula const & other)
83 : InsetFormulaBase(other),
85 preview_(new PreviewImpl(*this))
89 InsetFormula::InsetFormula(BufferView *)
90 : par_(MathAtom(new MathHullInset)),
91 preview_(new PreviewImpl(*this))
93 //view_ = bv->owner()->view();
97 InsetFormula::InsetFormula(string const & data)
98 : par_(MathAtom(new MathHullInset)),
99 preview_(new PreviewImpl(*this))
103 if (!mathed_parse_normal(par_, data))
104 lyxerr << "cannot interpret '" << data << "' as math" << endl;
109 InsetFormula::~InsetFormula()
113 auto_ptr<InsetBase> InsetFormula::clone() const
115 return auto_ptr<InsetBase>(new InsetFormula(*this));
119 void InsetFormula::write(Buffer const *, ostream & os) const
121 WriteStream wi(os, false, false);
122 os << par_->fileInsetLabel() << ' ';
127 int InsetFormula::latex(Buffer const *, ostream & os,
128 LatexRunParams const & runparams) const
130 WriteStream wi(os, runparams.moving_arg, true);
136 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
138 if (0 && display()) {
141 par()->metricsT(mi, dim);
142 TextPainter tpain(dim.width(), dim.height());
143 par()->drawT(tpain, 0, dim.ascent());
145 // reset metrics cache to "real" values
147 return tpain.textheight();
149 WriteStream wi(os, false, true);
150 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
156 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
158 return docbook(buf, os, false);
162 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
165 ms << MTag("equation");
168 int res = ascii(buf, ms.os(), 0);
174 ms << ETag("equation");
175 return ms.line() + res;
179 void InsetFormula::read(Buffer const *, LyXLex & lex)
181 mathed_parse_normal(par_, lex);
182 // remove extra 'mathrm' for chemistry stuff.
183 // will be re-added on write
184 if (par_->asHullInset()->getType() =="chemistry") {
185 lyxerr << "this is chemistry" << endl;
186 if (par_->cell(0).size() == 1) {
187 lyxerr << "this is size 1" << endl;
188 if (par_->cell(0)[0]->asFontInset()) {
189 lyxerr << "this is a font inset "
190 << "replacing " << par_.nucleus()->cell(0) <<
191 " with " << par_->cell(0)[0]->cell(0) << endl;
199 //ostream & operator<<(ostream & os, LyXCursor const & c)
201 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
206 void InsetFormula::draw(PainterInfo & pi, int x, int y) const
209 // This initiates the loading of the preview, so should come
210 // before the metrics are computed.
211 bool const use_preview = preview_->previewReady();
213 int const w = dim_.wid;
214 int const d = dim_.des;
215 int const a = dim_.asc;
219 pi.pain.image(x + 1, y - a, w, h, // one pixel gap in front
220 *(preview_->pimage()->image()));
222 PainterInfo p(pi.base.bv);
223 p.base.style = LM_ST_TEXT;
224 p.base.font = pi.base.font;
225 p.base.font.setColor(LColor::math);
226 if (lcolor.getX11Name(LColor::mathbg)
227 != lcolor.getX11Name(LColor::background))
228 p.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
231 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
233 mathcursor->drawSelection(pi);
234 //p.pain.rectangle(x, y - a, w, h, LColor::mathframe);
237 par_->draw(p, x + offset_, y);
245 void InsetFormula::getLabelList(vector<string> & res) const
247 par()->getLabelList(res);
251 InsetOld::Code InsetFormula::lyxCode() const
253 return InsetOld::MATH_CODE;
257 void InsetFormula::validate(LaTeXFeatures & features) const
259 par_->validate(features);
263 bool InsetFormula::insetAllowed(InsetOld::Code code) const
266 code == InsetOld::LABEL_CODE
267 || code == InsetOld::REF_CODE
268 || code == InsetOld::ERT_CODE;
272 void InsetFormula::metrics(MetricsInfo & m, Dimension & dim) const
275 if (preview_->previewReady()) {
276 dim.asc = preview_->pimage()->ascent();
277 dim.des = preview_->pimage()->descent();
278 // insert a one pixel gap in front of the formula
279 dim.wid = 1 + preview_->pimage()->width();
284 mi.base.style = LM_ST_TEXT;
285 mi.base.font.setColor(LColor::math);
286 par()->metrics(mi, dim);
292 offset_ = (m.base.textwidth - dim.wid) / 2;
293 dim.wid = m.base.textwidth;
302 void InsetFormula::mutate(string const & type)
304 par_.nucleus()->mutate(type);
312 void InsetFormula::addPreview(lyx::graphics::PreviewLoader & ploader) const
314 preview_->addPreview(ploader);
318 void InsetFormula::generatePreview() const
320 preview_->generatePreview();
324 bool InsetFormula::PreviewImpl::previewWanted() const
326 return !parent().par_->asNestInset()->editing();
330 string const InsetFormula::PreviewImpl::latexString() const
333 WriteStream wi(ls, false, false);
334 parent().par_->write(wi);
335 return STRCONV(ls.str());