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"
23 #include "support/std_sstream.h"
26 #include "frontends/Painter.h"
28 #include "graphics/PreviewedInset.h"
29 #include "graphics/PreviewImage.h"
33 using std::ostringstream;
39 class InsetFormula::PreviewImpl : public lyx::graphics::PreviewedInset {
42 PreviewImpl(InsetFormula & p) : PreviewedInset(p) {}
46 bool previewWanted() const;
48 string const latexString() const;
50 InsetFormula & parent() const
52 return *static_cast<InsetFormula*>(inset());
58 InsetFormula::InsetFormula(bool chemistry)
59 : par_(MathAtom(new MathHullInset)),
60 preview_(new PreviewImpl(*this))
67 InsetFormula::InsetFormula(InsetFormula const & other)
68 : InsetFormulaBase(other),
70 preview_(new PreviewImpl(*this))
74 InsetFormula::InsetFormula(BufferView *)
75 : par_(MathAtom(new MathHullInset)),
76 preview_(new PreviewImpl(*this))
78 //view_ = bv->owner()->view();
82 InsetFormula::InsetFormula(string const & data)
83 : par_(MathAtom(new MathHullInset)),
84 preview_(new PreviewImpl(*this))
88 if (!mathed_parse_normal(par_, data))
89 lyxerr << "cannot interpret '" << data << "' as math" << endl;
94 InsetFormula::~InsetFormula()
98 auto_ptr<InsetBase> InsetFormula::clone() const
100 return auto_ptr<InsetBase>(new InsetFormula(*this));
104 void InsetFormula::write(Buffer const &, ostream & os) const
106 WriteStream wi(os, false, false);
107 os << par_->fileInsetLabel() << ' ';
112 int InsetFormula::latex(Buffer const &, ostream & os,
113 LatexRunParams const & runparams) const
115 WriteStream wi(os, runparams.moving_arg, true);
121 int InsetFormula::ascii(Buffer const &, ostream & os, int) const
123 if (0 && display()) {
126 par()->metricsT(mi, dim);
127 TextPainter tpain(dim.width(), dim.height());
128 par()->drawT(tpain, 0, dim.ascent());
130 // reset metrics cache to "real" values
132 return tpain.textheight();
134 WriteStream wi(os, false, true);
135 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
141 int InsetFormula::linuxdoc(Buffer const & buf, ostream & os) const
143 return docbook(buf, os, false);
147 int InsetFormula::docbook(Buffer const & buf, ostream & os, bool) const
150 ms << MTag("equation");
153 int res = ascii(buf, ms.os(), 0);
159 ms << ETag("equation");
160 return ms.line() + res;
164 void InsetFormula::read(Buffer const &, LyXLex & lex)
166 mathed_parse_normal(par_, lex);
167 // remove extra 'mathrm' for chemistry stuff.
168 // will be re-added on write
169 if (par_->asHullInset()->getType() =="chemistry") {
170 lyxerr << "this is chemistry" << endl;
171 if (par_->cell(0).size() == 1) {
172 lyxerr << "this is size 1" << endl;
173 if (par_->cell(0)[0]->asFontInset()) {
174 lyxerr << "this is a font inset "
175 << "replacing " << par_.nucleus()->cell(0) <<
176 " with " << par_->cell(0)[0]->cell(0) << endl;
184 //ostream & operator<<(ostream & os, LyXCursor const & c)
186 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
191 void InsetFormula::draw(PainterInfo & pi, int x, int y) const
194 // This initiates the loading of the preview, so should come
195 // before the metrics are computed.
196 bool const use_preview = preview_->previewReady();
198 int const w = dim_.wid;
199 int const d = dim_.des;
200 int const a = dim_.asc;
204 pi.pain.image(x + 1, y - a, w, h, // one pixel gap in front
205 *(preview_->pimage()->image()));
207 PainterInfo p(pi.base.bv);
208 p.base.style = LM_ST_TEXT;
209 p.base.font = pi.base.font;
210 p.base.font.setColor(LColor::math);
211 if (lcolor.getX11Name(LColor::mathbg)
212 != lcolor.getX11Name(LColor::background))
213 p.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
216 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
218 mathcursor->drawSelection(pi);
219 //p.pain.rectangle(x, y - a, w, h, LColor::mathframe);
222 par_->draw(p, x + offset_, y);
230 void InsetFormula::getLabelList(vector<string> & res) const
232 par()->getLabelList(res);
236 InsetOld::Code InsetFormula::lyxCode() const
238 return InsetOld::MATH_CODE;
242 void InsetFormula::validate(LaTeXFeatures & features) const
244 par_->validate(features);
248 bool InsetFormula::insetAllowed(InsetOld::Code code) const
251 code == InsetOld::LABEL_CODE
252 || code == InsetOld::REF_CODE
253 || code == InsetOld::ERT_CODE;
257 void InsetFormula::metrics(MetricsInfo & m, Dimension & dim) const
260 if (preview_->previewReady()) {
261 dim.asc = preview_->pimage()->ascent();
262 dim.des = preview_->pimage()->descent();
263 // insert a one pixel gap in front of the formula
264 dim.wid = 1 + preview_->pimage()->width();
269 mi.base.style = LM_ST_TEXT;
270 mi.base.font.setColor(LColor::math);
271 par()->metrics(mi, dim);
277 offset_ = (m.base.textwidth - dim.wid) / 2;
278 dim.wid = m.base.textwidth;
287 void InsetFormula::mutate(string const & type)
289 par_.nucleus()->mutate(type);
297 void InsetFormula::addPreview(lyx::graphics::PreviewLoader & ploader) const
299 preview_->addPreview(ploader);
303 void InsetFormula::generatePreview() const
305 preview_->generatePreview();
309 bool InsetFormula::PreviewImpl::previewWanted() const
311 return !parent().par_->asNestInset()->editing();
315 string const InsetFormula::PreviewImpl::latexString() const
318 WriteStream wi(ls, false, false);
319 parent().par_->write(wi);