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.
17 #pragma implementation
23 #include "math_cursor.h"
24 #include "math_parser.h"
25 #include "math_charinset.h"
26 #include "math_arrayinset.h"
27 #include "math_metricsinfo.h"
28 #include "math_deliminset.h"
29 #include "math_hullinset.h"
30 #include "math_support.h"
31 #include "math_mathmlstream.h"
32 #include "textpainter.h"
35 #include "BufferView.h"
39 #include "funcrequest.h"
41 #include "support/LOstream.h"
42 #include "support/LAssert.h"
43 #include "support/lyxlib.h"
44 #include "support/systemcall.h"
45 #include "support/filetools.h"
47 #include "frontends/Alert.h"
48 #include "frontends/LyXView.h"
49 #include "frontends/Painter.h"
51 #include "graphics/PreviewedInset.h"
52 #include "graphics/PreviewImage.h"
66 class InsetFormula::PreviewImpl : public grfx::PreviewedInset {
69 PreviewImpl(InsetFormula & p) : PreviewedInset(p) {}
73 bool previewWanted() const;
75 string const latexString() const;
77 InsetFormula & parent() const
79 return *static_cast<InsetFormula*>(inset());
85 InsetFormula::InsetFormula()
86 : par_(MathAtom(new MathHullInset)),
87 preview_(new PreviewImpl(*this))
91 InsetFormula::InsetFormula(InsetFormula const & other)
92 : InsetFormulaBase(other),
94 preview_(new PreviewImpl(*this))
98 InsetFormula::InsetFormula(BufferView * bv)
99 : par_(MathAtom(new MathHullInset)),
100 preview_(new PreviewImpl(*this))
102 view_ = bv->owner()->view();
106 InsetFormula::InsetFormula(string const & data)
107 : par_(MathAtom(new MathHullInset)),
108 preview_(new PreviewImpl(*this))
112 if (!mathed_parse_normal(par_, data))
113 lyxerr << "cannot interpret '" << data << "' as math\n";
118 InsetFormula::~InsetFormula()
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, bool fragile, bool) const
138 WriteStream wi(os, fragile, true);
144 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
149 TextPainter tpain(par()->width(), par()->height());
150 par()->drawT(tpain, 0, par()->ascent());
152 // reset metrics cache to "real" values
154 return tpain.textheight();
156 WriteStream wi(os, false, true);
157 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
163 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
165 return docbook(buf, os, false);
169 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
172 ms << MTag("equation");
175 int res = ascii(buf, ms.os(), 0);
179 ms << par_.nucleus();
181 ms << ETag("equation");
182 return ms.line() + res;
186 void InsetFormula::read(Buffer const *, LyXLex & lex)
188 mathed_parse_normal(par_, lex);
193 //ostream & operator<<(ostream & os, LyXCursor const & c)
195 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
200 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
201 int y, float & xx, bool) const
203 // This initiates the loading of the preview, so should come
204 // before the metrics are computed.
205 preview_->setView(bv);
206 bool const use_preview = preview_->previewReady();
208 int const x = int(xx);
209 int const w = width(bv, font);
210 int const d = descent(bv, font);
211 int const a = ascent(bv, font);
214 MathPainterInfo pi(bv->painter());
217 pi.pain.image(x, y - a, w, h,
218 *(preview_->pimage()->image(*this, *bv)));
220 //pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
221 pi.base.style = LM_ST_TEXT;
223 pi.base.font.setColor(LColor::math);
224 if (lcolor.getX11Name(LColor::mathbg)
225 != lcolor.getX11Name(LColor::background))
226 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
229 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
231 mathcursor->drawSelection(pi);
232 //pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
235 par_->draw(pi, x + 1, y);
242 setCursorVisible(false);
246 vector<string> const InsetFormula::getLabelList() const
249 par()->getLabelList(res);
254 UpdatableInset::RESULT
255 InsetFormula::localDispatch(BufferView * bv, FuncRequest const & ev)
257 RESULT result = DISPATCHED;
262 bv->lockedInsetStoreUndo(Undo::INSERT);
263 mathcursor->breakLine();
264 mathcursor->normalize();
265 updateLocal(bv, true);
268 case LFUN_MATH_NUMBER:
272 //lyxerr << "toggling all numbers\n";
274 bv->lockedInsetStoreUndo(Undo::INSERT);
275 bool old = par()->numberedType();
276 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
277 hull()->numbered(row, !old);
278 bv->owner()->message(old ? _("No number") : _("Number"));
279 updateLocal(bv, true);
284 case LFUN_MATH_NONUMBER:
286 //lyxerr << "toggling line number\n";
288 bv->lockedInsetStoreUndo(Undo::INSERT);
289 MathCursor::row_type row = mathcursor->hullRow();
290 bool old = hull()->numbered(row);
291 bv->owner()->message(old ? _("No number") : _("Number"));
292 hull()->numbered(row, !old);
293 updateLocal(bv, true);
298 case LFUN_INSERT_LABEL:
303 bv->lockedInsetStoreUndo(Undo::INSERT);
305 MathCursor::row_type row = mathcursor->hullRow();
306 string old_label = hull()->label(row);
307 string new_label = ev.argument;
309 if (new_label.empty()) {
310 string const default_label =
311 (lyxrc.label_init_length >= 0) ? "eq:" : "";
312 pair<bool, string> const res = old_label.empty()
313 ? Alert::askForText(_("Enter new label to insert:"), default_label)
314 : Alert::askForText(_("Enter label:"), old_label);
317 new_label = trim(res.second);
320 //if (new_label == old_label)
321 // break; // Nothing to do
323 if (!new_label.empty()) {
324 lyxerr << "setting label to '" << new_label << "'\n";
325 hull()->numbered(row, true);
328 #warning FIXME: please check you really mean repaint() ... is it needed,
329 #warning and if so, should it be update() instead ?
330 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
333 hull()->label(row, new_label);
335 updateLocal(bv, true);
339 case LFUN_MATH_MUTATE:
341 bv->lockedInsetStoreUndo(Undo::EDIT);
344 mathcursor->getPos(x, y);
346 mathcursor->setPos(x, y);
347 mathcursor->normalize();
348 updateLocal(bv, true);
352 case LFUN_MATH_EXTERN:
354 bv->lockedInsetStoreUndo(Undo::EDIT);
356 mathcursor->handleExtern(ev.argument);
357 // re-compute inset dimension
359 updateLocal(bv, true);
363 case LFUN_MATH_DISPLAY:
367 mathcursor->getPos(x, y);
368 if (hullType() == "simple")
372 mathcursor->setPos(x, y);
373 mathcursor->normalize();
374 updateLocal(bv, true);
378 case LFUN_PASTESELECTION:
380 string const clip = bv->getClipboard();
382 mathed_parse_normal(par_, clip);
387 result = InsetFormulaBase::localDispatch(bv, ev);
394 bool InsetFormula::display() const
396 return hullType() != "simple" && hullType() != "none";
400 MathHullInset * InsetFormula::hull() const
402 lyx::Assert(par_->asHullInset());
403 return par_->asHullInset();
407 Inset::Code InsetFormula::lyxCode() const
409 return Inset::MATH_CODE;
413 void InsetFormula::validate(LaTeXFeatures & features) const
415 par_->validate(features);
419 bool InsetFormula::insetAllowed(Inset::Code code) const
422 (code == Inset::LABEL_CODE && display())
423 || code == Inset::REF_CODE
424 || code == Inset::ERT_CODE;
428 int InsetFormula::ascent(BufferView *, LyXFont const &) const
430 return preview_->previewReady() ?
431 preview_->pimage()->ascent() : 1 + par_->ascent();
435 int InsetFormula::descent(BufferView *, LyXFont const &) const
437 if (!preview_->previewReady())
438 return 1 + par_->descent();
440 int const descent = preview_->pimage()->descent();
441 return display() ? descent + 12 : descent;
445 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
448 return preview_->previewReady() ?
449 preview_->pimage()->width() : par_->width();
453 string InsetFormula::hullType() const
455 return par()->getType();
459 void InsetFormula::mutate(string const & type)
469 void InsetFormula::addPreview(grfx::PreviewLoader & ploader) const
471 preview_->addPreview(ploader);
475 void InsetFormula::generatePreview() const
477 preview_->generatePreview();
481 bool InsetFormula::PreviewImpl::previewWanted() const
483 return !parent().par_->asNestInset()->editing();
487 string const InsetFormula::PreviewImpl::latexString() const
490 WriteStream wi(ls, false, false);
491 parent().par_->write(wi);
492 return ls.str().c_str();