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"
57 class InsetFormula::PreviewImpl : public lyx::graphics::PreviewedInset {
60 PreviewImpl(InsetFormula & p) : PreviewedInset(p) {}
64 bool previewWanted() const;
66 string const latexString() const;
68 InsetFormula & parent() const
70 return *static_cast<InsetFormula*>(inset());
76 InsetFormula::InsetFormula(bool chemistry)
77 : par_(MathAtom(new MathHullInset)),
78 preview_(new PreviewImpl(*this))
85 InsetFormula::InsetFormula(InsetFormula const & other)
86 : InsetFormulaBase(other),
88 preview_(new PreviewImpl(*this))
92 InsetFormula::InsetFormula(BufferView *)
93 : par_(MathAtom(new MathHullInset)),
94 preview_(new PreviewImpl(*this))
96 //view_ = bv->owner()->view();
100 InsetFormula::InsetFormula(string const & data)
101 : par_(MathAtom(new MathHullInset)),
102 preview_(new PreviewImpl(*this))
106 if (!mathed_parse_normal(par_, data))
107 lyxerr << "cannot interpret '" << data << "' as math\n";
112 InsetFormula::~InsetFormula()
116 auto_ptr<InsetBase> InsetFormula::clone() const
118 return auto_ptr<InsetBase>(new InsetFormula(*this));
122 void InsetFormula::write(Buffer const *, ostream & os) const
124 WriteStream wi(os, false, false);
125 os << par_->fileInsetLabel() << ' ';
130 int InsetFormula::latex(Buffer const *, ostream & os,
131 LatexRunParams const & runparams) const
133 WriteStream wi(os, runparams.moving_arg, true);
139 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
141 if (0 && display()) {
144 par()->metricsT(mi, dim);
145 TextPainter tpain(dim.width(), dim.height());
146 par()->drawT(tpain, 0, dim.ascent());
148 // reset metrics cache to "real" values
150 return tpain.textheight();
152 WriteStream wi(os, false, true);
153 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
159 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
161 return docbook(buf, os, false);
165 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
168 ms << MTag("equation");
171 int res = ascii(buf, ms.os(), 0);
177 ms << ETag("equation");
178 return ms.line() + res;
182 void InsetFormula::read(Buffer const *, LyXLex & lex)
184 mathed_parse_normal(par_, lex);
185 // remove extra 'mathrm' for chemistry stuff.
186 // will be re-added on write
187 if (par_->asHullInset()->getType() =="chemistry") {
188 lyxerr << "this is chemistry\n";
189 if (par_->cell(0).size() == 1) {
190 lyxerr << "this is size 1\n";
191 if (par_->cell(0)[0]->asFontInset()) {
192 lyxerr << "this is a font inset \n";
193 lyxerr << "replacing " << par_.nucleus()->cell(0) <<
194 " with " << par_->cell(0)[0]->cell(0) << "\n";
202 //ostream & operator<<(ostream & os, LyXCursor const & c)
204 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
209 void InsetFormula::draw(PainterInfo & pi, int x, int y) const
212 // This initiates the loading of the preview, so should come
213 // before the metrics are computed.
214 bool const use_preview = preview_->previewReady();
216 int const w = dim_.wid;
217 int const d = dim_.des;
218 int const a = dim_.asc;
222 pi.pain.image(x + 1, y - a, w, h, // one pixel gap in front
223 *(preview_->pimage()->image()));
225 PainterInfo p(pi.base.bv);
226 p.base.style = LM_ST_TEXT;
227 p.base.font = pi.base.font;
228 p.base.font.setColor(LColor::math);
229 if (lcolor.getX11Name(LColor::mathbg)
230 != lcolor.getX11Name(LColor::background))
231 p.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
234 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
236 mathcursor->drawSelection(pi);
237 //p.pain.rectangle(x, y - a, w, h, LColor::mathframe);
248 void InsetFormula::getLabelList(std::vector<string> & res) const
250 par()->getLabelList(res);
254 InsetOld::Code InsetFormula::lyxCode() const
256 return InsetOld::MATH_CODE;
260 void InsetFormula::validate(LaTeXFeatures & features) const
262 par_->validate(features);
266 bool InsetFormula::insetAllowed(InsetOld::Code code) const
269 code == InsetOld::LABEL_CODE
270 || code == InsetOld::REF_CODE
271 || code == InsetOld::ERT_CODE;
275 void InsetFormula::metrics(MetricsInfo & m, Dimension & dim) const
278 if (preview_->previewReady()) {
279 dim.asc = preview_->pimage()->ascent();
280 dim.des = preview_->pimage()->descent();
281 // insert a one pixel gap in front of the formula
282 dim.wid = 1 + preview_->pimage()->width();
287 mi.base.style = LM_ST_TEXT;
288 mi.base.font.setColor(LColor::math);
289 par()->metrics(mi, dim);
297 void InsetFormula::mutate(string const & type)
299 par_.nucleus()->mutate(type);
307 void InsetFormula::addPreview(lyx::graphics::PreviewLoader & ploader) const
309 preview_->addPreview(ploader);
313 void InsetFormula::generatePreview() const
315 preview_->generatePreview();
319 bool InsetFormula::PreviewImpl::previewWanted() const
321 return !parent().par_->asNestInset()->editing();
325 string const InsetFormula::PreviewImpl::latexString() const
328 WriteStream wi(ls, false, false);
329 parent().par_->write(wi);
330 return STRCONV(ls.str());