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"
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 * bv)
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 &, bool) const
118 return 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, LatexRunParams const &,
131 bool fragile, bool) const
133 WriteStream wi(os, fragile, true);
139 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
141 if (0 && display()) {
144 TextPainter tpain(par()->width(), par()->height());
145 par()->drawT(tpain, 0, par()->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(BufferView * bv, LyXFont const & font,
209 int y, float & xx) 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 x = int(xx);
217 int const w = width(bv, font);
218 int const d = descent(bv, font);
219 int const a = ascent(bv, font);
222 PainterInfo pi(bv->painter());
225 pi.pain.image(x + 1, y - a, w, h, // one pixel gap in front
226 *(preview_->pimage()->image()));
228 pi.base.style = LM_ST_TEXT;
230 pi.base.font.setColor(LColor::math);
231 if (lcolor.getX11Name(LColor::mathbg)
232 != lcolor.getX11Name(LColor::background))
233 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
236 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
238 mathcursor->drawSelection(pi);
239 //pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
242 par_->draw(pi, x, y);
251 vector<string> const InsetFormula::getLabelList() const
254 par()->getLabelList(res);
259 Inset::Code InsetFormula::lyxCode() const
261 return Inset::MATH_CODE;
265 void InsetFormula::validate(LaTeXFeatures & features) const
267 par_->validate(features);
271 bool InsetFormula::insetAllowed(Inset::Code code) const
274 code == Inset::LABEL_CODE
275 || code == Inset::REF_CODE
276 || code == Inset::ERT_CODE;
280 void InsetFormula::dimension(BufferView * bv, LyXFont const & font,
281 Dimension & dim) const
284 if (preview_->previewReady()) {
285 dim.a = preview_->pimage()->ascent();
286 int const descent = preview_->pimage()->descent();
287 dim.d = display() ? descent + 12 : descent;
288 // insert a one pixel gap in front of the formula
289 dim.w = 1 + preview_->pimage()->width();
291 dim = par_->dimensions();
298 void InsetFormula::mutate(string const & type)
300 par_.nucleus()->mutate(type);
308 void InsetFormula::addPreview(grfx::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());