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
253 par()->getLabelList(res);
258 UpdatableInset::RESULT
259 InsetFormula::localDispatch(BufferView * bv, kb_action action,
262 RESULT result = DISPATCHED;
267 bv->lockedInsetStoreUndo(Undo::INSERT);
268 mathcursor->breakLine();
269 mathcursor->normalize();
270 updateLocal(bv, true);
273 case LFUN_MATH_NUMBER:
277 //lyxerr << "toggling all numbers\n";
279 bv->lockedInsetStoreUndo(Undo::INSERT);
280 bool old = par()->numberedType();
281 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
282 hull()->numbered(row, !old);
283 bv->owner()->message(old ? _("No number") : _("Number"));
284 updateLocal(bv, true);
289 case LFUN_MATH_NONUMBER:
291 //lyxerr << "toggling line number\n";
293 bv->lockedInsetStoreUndo(Undo::INSERT);
294 MathCursor::row_type row = mathcursor->hullRow();
295 bool old = hull()->numbered(row);
296 bv->owner()->message(old ? _("No number") : _("Number"));
297 hull()->numbered(row, !old);
298 updateLocal(bv, true);
303 case LFUN_INSERT_LABEL:
308 bv->lockedInsetStoreUndo(Undo::INSERT);
310 MathCursor::row_type row = mathcursor->hullRow();
311 string old_label = hull()->label(row);
312 string new_label = arg;
314 if (new_label.empty()) {
315 string const default_label =
316 (lyxrc.label_init_length >= 0) ? "eq:" : "";
317 pair<bool, string> const res = old_label.empty()
318 ? Alert::askForText(_("Enter new label to insert:"), default_label)
319 : Alert::askForText(_("Enter label:"), old_label);
322 new_label = trim(res.second);
325 //if (new_label == old_label)
326 // break; // Nothing to do
328 if (!new_label.empty()) {
329 lyxerr << "setting label to '" << new_label << "'\n";
330 hull()->numbered(row, true);
333 #warning FIXME: please check you really mean repaint() ... is it needed,
334 #warning and if so, should it be update() instead ?
335 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
338 hull()->label(row, new_label);
340 updateLocal(bv, true);
344 case LFUN_MATH_MUTATE:
346 bv->lockedInsetStoreUndo(Undo::EDIT);
349 mathcursor->getPos(x, y);
351 mathcursor->setPos(x, y);
352 mathcursor->normalize();
353 updateLocal(bv, true);
357 case LFUN_MATH_EXTERN:
359 bv->lockedInsetStoreUndo(Undo::EDIT);
361 mathcursor->handleExtern(arg);
362 // re-compute inset dimension
364 updateLocal(bv, true);
368 case LFUN_MATH_DISPLAY:
372 mathcursor->getPos(x, y);
373 if (hullType() == "simple")
377 mathcursor->setPos(x, y);
378 mathcursor->normalize();
379 updateLocal(bv, true);
383 case LFUN_PASTESELECTION:
385 string const clip = bv->getClipboard();
387 mathed_parse_normal(par_, clip);
392 result = InsetFormulaBase::localDispatch(bv, action, arg);
399 bool InsetFormula::display() const
401 return hullType() != "simple" && hullType() != "none";
405 MathHullInset * InsetFormula::hull() const
407 lyx::Assert(par_->asHullInset());
408 return par_->asHullInset();
412 Inset::Code InsetFormula::lyxCode() const
414 return Inset::MATH_CODE;
418 void InsetFormula::validate(LaTeXFeatures & features) const
420 par_->validate(features);
424 bool InsetFormula::insetAllowed(Inset::Code code) const
427 (code == Inset::LABEL_CODE && display())
428 || code == Inset::REF_CODE
429 || code == Inset::ERT_CODE;
433 int InsetFormula::ascent(BufferView *, LyXFont const &) const
435 return preview_->previewReady() ?
436 preview_->pimage()->ascent() : 1 + par_->ascent();
440 int InsetFormula::descent(BufferView *, LyXFont const &) const
442 if (!preview_->previewReady())
443 return 1 + par_->descent();
445 int const descent = preview_->pimage()->descent();
446 return display() ? descent + 12 : descent;
450 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
453 return preview_->previewReady() ?
454 preview_->pimage()->width() : par_->width();
458 string InsetFormula::hullType() const
460 return par()->getType();
464 void InsetFormula::mutate(string const & type)
474 void InsetFormula::addPreview(grfx::PreviewLoader & ploader) const
476 preview_->addPreview(ploader);
480 void InsetFormula::generatePreview() const
482 preview_->generatePreview();
486 bool InsetFormula::PreviewImpl::previewWanted() const
488 return !parent().par_->asNestInset()->editing();
492 string const InsetFormula::PreviewImpl::latexString() const
495 WriteStream wi(ls, false, false);
496 parent().par_->write(wi);
497 return ls.str().c_str();