3 * Purpose: Implementation of formula inset
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
6 * Description: Allows the edition of math paragraphs inside Lyx.
8 * Copyright: 1996-1998 Alejandro Aguilar Sierra
10 * Version: 0.4, Lyx project.
12 * You are free to use and modify this code under the terms of
13 * the GNU General Public Licence version 2 or later.
19 #include "math_cursor.h"
20 #include "math_parser.h"
21 #include "math_charinset.h"
22 #include "math_arrayinset.h"
23 #include "metricsinfo.h"
24 #include "math_deliminset.h"
25 #include "math_hullinset.h"
26 #include "math_support.h"
27 #include "math_mathmlstream.h"
28 #include "textpainter.h"
30 #include "BufferView.h"
33 #include "latexrunparams.h"
35 #include "funcrequest.h"
38 #include "support/LOstream.h"
39 #include "support/LAssert.h"
40 #include "support/lyxlib.h"
41 #include "support/systemcall.h"
42 #include "support/filetools.h"
44 #include "frontends/Alert.h"
45 #include "frontends/LyXView.h"
46 #include "frontends/Painter.h"
48 #include "graphics/PreviewedInset.h"
49 #include "graphics/PreviewImage.h"
56 class InsetFormula::PreviewImpl : public lyx::graphics::PreviewedInset {
59 PreviewImpl(InsetFormula & p) : PreviewedInset(p) {}
63 bool previewWanted() const;
65 string const latexString() const;
67 InsetFormula & parent() const
69 return *static_cast<InsetFormula*>(inset());
75 InsetFormula::InsetFormula(bool chemistry)
76 : par_(MathAtom(new MathHullInset)),
77 preview_(new PreviewImpl(*this))
84 InsetFormula::InsetFormula(InsetFormula const & other)
85 : InsetFormulaBase(other),
87 preview_(new PreviewImpl(*this))
91 InsetFormula::InsetFormula(BufferView *)
92 : par_(MathAtom(new MathHullInset)),
93 preview_(new PreviewImpl(*this))
95 //view_ = bv->owner()->view();
99 InsetFormula::InsetFormula(string const & data)
100 : par_(MathAtom(new MathHullInset)),
101 preview_(new PreviewImpl(*this))
105 if (!mathed_parse_normal(par_, data))
106 lyxerr << "cannot interpret '" << data << "' as math\n";
111 InsetFormula::~InsetFormula()
115 Inset * InsetFormula::clone() const
117 return new InsetFormula(*this);
121 void InsetFormula::write(Buffer const *, ostream & os) const
123 WriteStream wi(os, false, false);
124 os << par_->fileInsetLabel() << ' ';
129 int InsetFormula::latex(Buffer const *, ostream & os,
130 LatexRunParams const & runparams) const
132 WriteStream wi(os, runparams.moving_arg, true);
138 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
140 if (0 && display()) {
143 par()->metricsT(mi, dim);
144 TextPainter tpain(dim.width(), dim.height());
145 par()->drawT(tpain, 0, dim.ascent());
147 // reset metrics cache to "real" values
149 return tpain.textheight();
151 WriteStream wi(os, false, true);
152 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
158 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
160 return docbook(buf, os, false);
164 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
167 ms << MTag("equation");
170 int res = ascii(buf, ms.os(), 0);
176 ms << ETag("equation");
177 return ms.line() + res;
181 void InsetFormula::read(Buffer const *, LyXLex & lex)
183 mathed_parse_normal(par_, lex);
184 // remove extra 'mathrm' for chemistry stuff.
185 // will be re-added on write
186 if (par_->asHullInset()->getType() =="chemistry") {
187 lyxerr << "this is chemistry\n";
188 if (par_->cell(0).size() == 1) {
189 lyxerr << "this is size 1\n";
190 if (par_->cell(0)[0]->asFontInset()) {
191 lyxerr << "this is a font inset \n";
192 lyxerr << "replacing " << par_.nucleus()->cell(0) <<
193 " with " << par_->cell(0)[0]->cell(0) << "\n";
201 //ostream & operator<<(ostream & os, LyXCursor const & c)
203 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
208 void InsetFormula::draw(PainterInfo & pi, int x, int y) const
211 // This initiates the loading of the preview, so should come
212 // before the metrics are computed.
213 bool const use_preview = preview_->previewReady();
215 int const w = dim_.wid;
216 int const d = dim_.des;
217 int const a = dim_.asc;
221 pi.pain.image(x + 1, y - a, w, h, // one pixel gap in front
222 *(preview_->pimage()->image()));
224 PainterInfo p(pi.base.bv);
225 p.base.style = LM_ST_TEXT;
226 p.base.font = pi.base.font;
227 p.base.font.setColor(LColor::math);
228 if (lcolor.getX11Name(LColor::mathbg)
229 != lcolor.getX11Name(LColor::background))
230 p.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
233 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
235 mathcursor->drawSelection(pi);
236 //p.pain.rectangle(x, y - a, w, h, LColor::mathframe);
247 vector<string> const InsetFormula::getLabelList() const
250 par()->getLabelList(res);
255 Inset::Code InsetFormula::lyxCode() const
257 return Inset::MATH_CODE;
261 void InsetFormula::validate(LaTeXFeatures & features) const
263 par_->validate(features);
267 bool InsetFormula::insetAllowed(Inset::Code code) const
270 code == Inset::LABEL_CODE
271 || code == Inset::REF_CODE
272 || code == Inset::ERT_CODE;
276 void InsetFormula::metrics(MetricsInfo & m, Dimension & dim) const
279 if (preview_->previewReady()) {
280 dim.asc = preview_->pimage()->ascent();
281 dim.des = preview_->pimage()->descent();
282 // insert a one pixel gap in front of the formula
283 dim.wid = 1 + preview_->pimage()->width();
288 mi.base.style = LM_ST_TEXT;
289 mi.base.font.setColor(LColor::math);
290 par()->metrics(mi, dim);
298 void InsetFormula::mutate(string const & type)
300 par_.nucleus()->mutate(type);
308 void InsetFormula::addPreview(lyx::graphics::PreviewLoader & ploader) const
310 preview_->addPreview(ploader);
314 void InsetFormula::generatePreview() const
316 preview_->generatePreview();
320 bool InsetFormula::PreviewImpl::previewWanted() const
322 return !parent().par_->asNestInset()->editing();
326 string const InsetFormula::PreviewImpl::latexString() const
329 WriteStream wi(ls, false, false);
330 parent().par_->write(wi);
331 return STRCONV(ls.str());