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.
20 #include "math_cursor.h"
21 #include "math_parser.h"
22 #include "math_charinset.h"
23 #include "math_arrayinset.h"
24 #include "metricsinfo.h"
25 #include "math_deliminset.h"
26 #include "math_hullinset.h"
27 #include "math_support.h"
28 #include "math_mathmlstream.h"
29 #include "textpainter.h"
32 #include "BufferView.h"
35 #include "latexrunparams.h"
37 #include "funcrequest.h"
40 #include "support/LOstream.h"
41 #include "support/LAssert.h"
42 #include "support/lyxlib.h"
43 #include "support/systemcall.h"
44 #include "support/filetools.h"
46 #include "frontends/Alert.h"
47 #include "frontends/LyXView.h"
48 #include "frontends/Painter.h"
50 #include "graphics/PreviewedInset.h"
51 #include "graphics/PreviewImage.h"
58 class InsetFormula::PreviewImpl : public grfx::PreviewedInset {
61 PreviewImpl(InsetFormula & p) : PreviewedInset(p) {}
65 bool previewWanted() const;
67 string const latexString() const;
69 InsetFormula & parent() const
71 return *static_cast<InsetFormula*>(inset());
77 InsetFormula::InsetFormula(bool chemistry)
78 : par_(MathAtom(new MathHullInset)),
79 preview_(new PreviewImpl(*this))
86 InsetFormula::InsetFormula(InsetFormula const & other)
87 : InsetFormulaBase(other),
89 preview_(new PreviewImpl(*this))
93 InsetFormula::InsetFormula(BufferView * bv)
94 : par_(MathAtom(new MathHullInset)),
95 preview_(new PreviewImpl(*this))
97 view_ = bv->owner()->view();
101 InsetFormula::InsetFormula(string const & data)
102 : par_(MathAtom(new MathHullInset)),
103 preview_(new PreviewImpl(*this))
107 if (!mathed_parse_normal(par_, data))
108 lyxerr << "cannot interpret '" << data << "' as math\n";
113 InsetFormula::~InsetFormula()
117 Inset * InsetFormula::clone(Buffer const &, bool) const
119 return new InsetFormula(*this);
123 void InsetFormula::write(Buffer const *, ostream & os) const
125 WriteStream wi(os, false, false);
126 os << par_->fileInsetLabel() << ' ';
131 int InsetFormula::latex(Buffer const *, ostream & os,
132 LatexRunParams const & runparams) const
134 WriteStream wi(os, runparams.fragile, true);
140 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
142 if (0 && display()) {
145 TextPainter tpain(par()->width(), par()->height());
146 par()->drawT(tpain, 0, par()->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(BufferView * bv, LyXFont const & font,
210 int y, float & xx) const
213 // This initiates the loading of the preview, so should come
214 // before the metrics are computed.
215 bool const use_preview = preview_->previewReady();
217 int const x = int(xx);
218 int const w = width(bv, font);
219 int const d = descent(bv, font);
220 int const a = ascent(bv, font);
223 PainterInfo pi(bv->painter());
226 pi.pain.image(x + 1, y - a, w, h, // one pixel gap in front
227 *(preview_->pimage()->image()));
229 pi.base.style = LM_ST_TEXT;
231 pi.base.font.setColor(LColor::math);
232 if (lcolor.getX11Name(LColor::mathbg)
233 != lcolor.getX11Name(LColor::background))
234 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
237 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
239 mathcursor->drawSelection(pi);
240 //pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
243 par_->draw(pi, x, y);
252 vector<string> const InsetFormula::getLabelList() const
255 par()->getLabelList(res);
260 Inset::Code InsetFormula::lyxCode() const
262 return Inset::MATH_CODE;
266 void InsetFormula::validate(LaTeXFeatures & features) const
268 par_->validate(features);
272 bool InsetFormula::insetAllowed(Inset::Code code) const
275 code == Inset::LABEL_CODE
276 || code == Inset::REF_CODE
277 || code == Inset::ERT_CODE;
281 void InsetFormula::dimension(BufferView * bv, LyXFont const & font,
282 Dimension & dim) const
285 if (preview_->previewReady()) {
286 dim.a = preview_->pimage()->ascent();
287 int const descent = preview_->pimage()->descent();
288 dim.d = display() ? descent + 12 : descent;
289 // insert a one pixel gap in front of the formula
290 dim.w = 1 + preview_->pimage()->width();
292 dim = par_->dimensions();
299 void InsetFormula::mutate(string const & type)
301 par_.nucleus()->mutate(type);
309 void InsetFormula::addPreview(grfx::PreviewLoader & ploader) const
311 preview_->addPreview(ploader);
315 void InsetFormula::generatePreview() const
317 preview_->generatePreview();
321 bool InsetFormula::PreviewImpl::previewWanted() const
323 return !parent().par_->asNestInset()->editing();
327 string const InsetFormula::PreviewImpl::latexString() const
330 WriteStream wi(ls, false, false);
331 parent().par_->write(wi);
332 return STRCONV(ls.str());