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 "commandtags.h"
24 #include "math_cursor.h"
25 #include "math_parser.h"
26 #include "math_charinset.h"
27 #include "math_arrayinset.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"
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"
65 class InsetFormula::PreviewImpl : public grfx::PreviewedInset {
68 PreviewImpl(InsetFormula & p) : PreviewedInset(p) {}
72 bool previewWanted() const;
74 string const latexString() const;
76 BufferView * view() const
78 return parent().view();
81 InsetFormula & parent() const
83 return *static_cast<InsetFormula*>(inset());
89 InsetFormula::InsetFormula()
90 : par_(MathAtom(new MathHullInset)),
91 preview_(new PreviewImpl(*this))
95 InsetFormula::InsetFormula(InsetFormula const & other)
96 : InsetFormulaBase(other),
98 preview_(new PreviewImpl(*this))
102 InsetFormula::InsetFormula(BufferView * bv)
103 : par_(MathAtom(new MathHullInset)),
104 preview_(new PreviewImpl(*this))
106 view_ = bv->owner()->view();
110 InsetFormula::InsetFormula(string const & data)
111 : par_(MathAtom(new MathHullInset)),
112 preview_(new PreviewImpl(*this))
116 if (!mathed_parse_normal(par_, data))
117 lyxerr << "cannot interpret '" << data << "' as math\n";
122 InsetFormula::~InsetFormula()
126 Inset * InsetFormula::clone(Buffer const &, bool) const
128 return new InsetFormula(*this);
132 void InsetFormula::write(Buffer const *, ostream & os) const
134 WriteStream wi(os, false, false);
135 os << par_->fileInsetLabel() << " ";
140 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
142 WriteStream wi(os, fragile, true);
148 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
153 TextPainter tpain(par()->width(), par()->height());
154 par()->drawT(tpain, 0, par()->ascent());
156 // reset metrics cache to "real" values
158 return tpain.textheight();
160 WriteStream wi(os, false, true);
161 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
167 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
169 return docbook(buf, os, false);
173 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
176 ms << MTag("equation");
179 int res = ascii(buf, ms.os(), 0);
183 ms << par_.nucleus();
185 ms << ETag("equation");
186 return ms.line() + res;
190 void InsetFormula::read(Buffer const *, LyXLex & lex)
192 mathed_parse_normal(par_, lex);
197 //ostream & operator<<(ostream & os, LyXCursor const & c)
199 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
204 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
205 int y, float & xx, bool) const
207 // This initiates the loading of the preview, so should come
208 // before the metrics are computed.
209 view_ = bv->owner()->view();
210 bool const use_preview = preview_->previewReady();
212 int const x = int(xx);
213 int const w = width(bv, font);
214 int const d = descent(bv, font);
215 int const a = ascent(bv, font);
218 MathPainterInfo pi(bv->painter());
221 pi.pain.image(x, y - a, w, h,
222 *(preview_->pimage()->image(*this, *bv)));
224 //pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
225 pi.base.style = LM_ST_TEXT;
227 pi.base.font.setColor(LColor::math);
228 if (lcolor.getX11Name(LColor::mathbg)
229 != lcolor.getX11Name(LColor::background))
230 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
233 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
235 mathcursor->drawSelection(pi);
236 //pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
239 par_->draw(pi, x + 1, y);
246 setCursorVisible(false);
250 vector<string> const InsetFormula::getLabelList() const
252 return par()->getLabelList();
256 UpdatableInset::RESULT
257 InsetFormula::localDispatch(BufferView * bv, kb_action action,
260 RESULT result = DISPATCHED;
265 bv->lockedInsetStoreUndo(Undo::INSERT);
266 mathcursor->breakLine();
267 mathcursor->normalize();
268 updateLocal(bv, true);
271 case LFUN_MATH_NUMBER:
275 //lyxerr << "toggling all numbers\n";
277 bv->lockedInsetStoreUndo(Undo::INSERT);
278 bool old = par()->numberedType();
279 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
280 hull()->numbered(row, !old);
281 bv->owner()->message(old ? _("No number") : _("Number"));
282 updateLocal(bv, true);
287 case LFUN_MATH_NONUMBER:
289 //lyxerr << "toggling line number\n";
291 bv->lockedInsetStoreUndo(Undo::INSERT);
292 MathCursor::row_type row = mathcursor->hullRow();
293 bool old = hull()->numbered(row);
294 bv->owner()->message(old ? _("No number") : _("Number"));
295 hull()->numbered(row, !old);
296 updateLocal(bv, true);
301 case LFUN_INSERT_LABEL:
306 bv->lockedInsetStoreUndo(Undo::INSERT);
308 MathCursor::row_type row = mathcursor->hullRow();
309 string old_label = hull()->label(row);
310 string new_label = arg;
312 if (new_label.empty()) {
313 string const default_label =
314 (lyxrc.label_init_length >= 0) ? "eq:" : "";
315 pair<bool, string> const res = old_label.empty()
316 ? Alert::askForText(_("Enter new label to insert:"), default_label)
317 : Alert::askForText(_("Enter label:"), old_label);
320 new_label = trim(res.second);
323 //if (new_label == old_label)
324 // break; // Nothing to do
326 if (!new_label.empty()) {
327 lyxerr << "setting label to '" << new_label << "'\n";
328 hull()->numbered(row, true);
331 #warning FIXME: please check you really mean repaint() ... is it needed,
332 #warning and if so, should it be update() instead ?
333 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
336 hull()->label(row, new_label);
338 updateLocal(bv, true);
342 case LFUN_MATH_MUTATE:
344 bv->lockedInsetStoreUndo(Undo::EDIT);
347 mathcursor->getPos(x, y);
349 mathcursor->setPos(x, y);
350 mathcursor->normalize();
351 updateLocal(bv, true);
355 case LFUN_MATH_EXTERN:
357 bv->lockedInsetStoreUndo(Undo::EDIT);
359 mathcursor->handleExtern(arg);
360 // re-compute inset dimension
362 updateLocal(bv, true);
366 case LFUN_MATH_DISPLAY:
370 mathcursor->getPos(x, y);
371 if (hullType() == "simple")
375 mathcursor->setPos(x, y);
376 mathcursor->normalize();
377 updateLocal(bv, true);
381 case LFUN_PASTESELECTION:
383 string const clip = bv->getClipboard();
385 mathed_parse_normal(par_, clip);
390 result = InsetFormulaBase::localDispatch(bv, action, arg);
397 bool InsetFormula::display() const
399 return hullType() != "simple" && hullType() != "none";
403 MathHullInset * InsetFormula::hull() const
405 lyx::Assert(par_->asHullInset());
406 return par_->asHullInset();
410 Inset::Code InsetFormula::lyxCode() const
412 return Inset::MATH_CODE;
416 void InsetFormula::validate(LaTeXFeatures & features) const
418 par_->validate(features);
422 bool InsetFormula::insetAllowed(Inset::Code code) const
425 (code == Inset::LABEL_CODE && display())
426 || code == Inset::REF_CODE
427 || code == Inset::ERT_CODE;
431 int InsetFormula::ascent(BufferView *, LyXFont const &) const
433 return preview_->previewReady() ?
434 preview_->pimage()->ascent() : 1 + par_->ascent();
438 int InsetFormula::descent(BufferView *, LyXFont const &) const
440 if (!preview_->previewReady())
441 return 1 + par_->descent();
443 int const descent = preview_->pimage()->descent();
444 return display() ? descent + 12 : descent;
448 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
451 return preview_->previewReady() ?
452 preview_->pimage()->width() : par_->width();
456 string InsetFormula::hullType() const
458 return par()->getType();
462 void InsetFormula::mutate(string const & type)
472 void InsetFormula::addPreview(grfx::PreviewLoader & ploader) const
474 preview_->addPreview(ploader);
478 void InsetFormula::generatePreview() const
480 preview_->generatePreview();
484 bool InsetFormula::PreviewImpl::previewWanted() const
486 return !parent().par_->asNestInset()->editing();
490 string const InsetFormula::PreviewImpl::latexString() const
493 WriteStream wi(ls, false, false);
494 parent().par_->write(wi);
495 return ls.str().c_str();