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"
31 #include "BufferView.h"
34 #include "latexrunparams.h"
36 #include "funcrequest.h"
39 #include "support/LOstream.h"
40 #include "support/LAssert.h"
41 #include "support/lyxlib.h"
42 #include "support/systemcall.h"
43 #include "support/filetools.h"
45 #include "frontends/Alert.h"
46 #include "frontends/LyXView.h"
47 #include "frontends/Painter.h"
49 #include "graphics/PreviewedInset.h"
50 #include "graphics/PreviewImage.h"
57 class InsetFormula::PreviewImpl : public grfx::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 Inset * InsetFormula::clone(Buffer const &) const
118 return new InsetFormula(*this);
122 // Inset * InsetFormula::clone(Buffer const &, bool) const
124 // return new InsetFormula(*this);
128 void InsetFormula::write(Buffer const *, ostream & os) const
130 WriteStream wi(os, false, false);
131 os << par_->fileInsetLabel() << ' ';
136 int InsetFormula::latex(Buffer const *, ostream & os,
137 LatexRunParams const & runparams) const
139 WriteStream wi(os, runparams.moving_arg, true);
145 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
147 if (0 && display()) {
150 par()->metricsT(mi, dim);
151 TextPainter tpain(dim.width(), dim.height());
152 par()->drawT(tpain, 0, dim.ascent());
154 // reset metrics cache to "real" values
156 return tpain.textheight();
158 WriteStream wi(os, false, true);
159 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
165 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
167 return docbook(buf, os, false);
171 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
174 ms << MTag("equation");
177 int res = ascii(buf, ms.os(), 0);
183 ms << ETag("equation");
184 return ms.line() + res;
188 void InsetFormula::read(Buffer const *, LyXLex & lex)
190 mathed_parse_normal(par_, lex);
191 // remove extra 'mathrm' for chemistry stuff.
192 // will be re-added on write
193 if (par_->asHullInset()->getType() =="chemistry") {
194 lyxerr << "this is chemistry\n";
195 if (par_->cell(0).size() == 1) {
196 lyxerr << "this is size 1\n";
197 if (par_->cell(0)[0]->asFontInset()) {
198 lyxerr << "this is a font inset \n";
199 lyxerr << "replacing " << par_.nucleus()->cell(0) <<
200 " with " << par_->cell(0)[0]->cell(0) << "\n";
208 //ostream & operator<<(ostream & os, LyXCursor const & c)
210 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
215 void InsetFormula::draw(PainterInfo & pi, int x, int y) const
218 // This initiates the loading of the preview, so should come
219 // before the metrics are computed.
220 bool const use_preview = preview_->previewReady();
224 mi.base.bv = pi.base.bv;
225 mi.base.font = pi.base.font;
228 int const w = dim.wid;
229 int const d = dim.des;
230 int const a = dim.asc;
234 pi.pain.image(x + 1, y - a, w, h, // one pixel gap in front
235 *(preview_->pimage()->image()));
237 PainterInfo p(pi.base.bv);
238 p.base.style = LM_ST_TEXT;
239 p.base.font = pi.base.font;
240 p.base.font.setColor(LColor::math);
241 if (lcolor.getX11Name(LColor::mathbg)
242 != lcolor.getX11Name(LColor::background))
243 p.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
246 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
248 mathcursor->drawSelection(pi);
249 //p.pain.rectangle(x, y - a, w, h, LColor::mathframe);
260 vector<string> const InsetFormula::getLabelList() const
263 par()->getLabelList(res);
268 Inset::Code InsetFormula::lyxCode() const
270 return Inset::MATH_CODE;
274 void InsetFormula::validate(LaTeXFeatures & features) const
276 par_->validate(features);
280 bool InsetFormula::insetAllowed(Inset::Code code) const
283 code == Inset::LABEL_CODE
284 || code == Inset::REF_CODE
285 || code == Inset::ERT_CODE;
289 void InsetFormula::metrics(MetricsInfo & m, Dimension & dim) const
292 if (preview_->previewReady()) {
293 dim_.asc = preview_->pimage()->ascent();
294 dim_.des = preview_->pimage()->descent();
295 // insert a one pixel gap in front of the formula
296 dim_.wid = 1 + preview_->pimage()->width();
301 mi.base.style = LM_ST_TEXT;
302 mi.base.font.setColor(LColor::math);
303 par()->metrics(mi, dim_);
311 void InsetFormula::mutate(string const & type)
313 par_.nucleus()->mutate(type);
321 void InsetFormula::addPreview(grfx::PreviewLoader & ploader) const
323 preview_->addPreview(ploader);
327 void InsetFormula::generatePreview() const
329 preview_->generatePreview();
333 bool InsetFormula::PreviewImpl::previewWanted() const
335 return !parent().par_->asNestInset()->editing();
339 string const InsetFormula::PreviewImpl::latexString() const
342 WriteStream wi(ls, false, false);
343 parent().par_->write(wi);
344 return STRCONV(ls.str());