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_metricsinfo.h"
29 #include "math_deliminset.h"
30 #include "math_hullinset.h"
31 #include "math_support.h"
32 #include "math_mathmlstream.h"
33 #include "textpainter.h"
36 #include "BufferView.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 BufferView * view() const
79 return parent().view();
82 InsetFormula & parent() const
84 return *static_cast<InsetFormula*>(inset());
90 InsetFormula::InsetFormula()
91 : par_(MathAtom(new MathHullInset)),
92 preview_(new PreviewImpl(*this))
96 InsetFormula::InsetFormula(InsetFormula const & other)
97 : InsetFormulaBase(other),
99 preview_(new PreviewImpl(*this))
103 InsetFormula::InsetFormula(BufferView * bv)
104 : par_(MathAtom(new MathHullInset)),
105 preview_(new PreviewImpl(*this))
107 view_ = bv->owner()->view();
111 InsetFormula::InsetFormula(string const & data)
112 : par_(MathAtom(new MathHullInset)),
113 preview_(new PreviewImpl(*this))
117 if (!mathed_parse_normal(par_, data))
118 lyxerr << "cannot interpret '" << data << "' as math\n";
123 InsetFormula::~InsetFormula()
127 Inset * InsetFormula::clone(Buffer const &, bool) const
129 return new InsetFormula(*this);
133 void InsetFormula::write(Buffer const *, ostream & os) const
135 WriteStream wi(os, false, false);
136 os << par_->fileInsetLabel() << " ";
141 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
143 WriteStream wi(os, fragile, true);
149 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
154 TextPainter tpain(par()->width(), par()->height());
155 par()->drawT(tpain, 0, par()->ascent());
157 // reset metrics cache to "real" values
159 return tpain.textheight();
161 WriteStream wi(os, false, true);
162 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
168 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
170 return docbook(buf, os, false);
174 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
177 ms << MTag("equation");
180 int res = ascii(buf, ms.os(), 0);
184 ms << par_.nucleus();
186 ms << ETag("equation");
187 return ms.line() + res;
191 void InsetFormula::read(Buffer const *, LyXLex & lex)
193 mathed_parse_normal(par_, lex);
198 //ostream & operator<<(ostream & os, LyXCursor const & c)
200 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
205 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
206 int y, float & xx, bool) const
208 // This initiates the loading of the preview, so should come
209 // before the metrics are computed.
210 view_ = bv->owner()->view();
211 bool const use_preview = preview_->previewReady();
213 int const x = int(xx);
214 int const w = width(bv, font);
215 int const d = descent(bv, font);
216 int const a = ascent(bv, font);
219 MathPainterInfo pi(bv->painter());
222 pi.pain.image(x, y - a, w, h,
223 *(preview_->pimage()->image(*this, *bv)));
225 //pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
226 pi.base.style = LM_ST_TEXT;
228 pi.base.font.setColor(LColor::math);
229 if (lcolor.getX11Name(LColor::mathbg)
230 != lcolor.getX11Name(LColor::background))
231 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
234 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
236 mathcursor->drawSelection(pi);
237 //pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
240 par_->draw(pi, x + 1, y);
247 setCursorVisible(false);
251 vector<string> const InsetFormula::getLabelList() const
254 par()->getLabelList(res);
259 UpdatableInset::RESULT
260 InsetFormula::localDispatch(BufferView * bv, kb_action action,
263 RESULT result = DISPATCHED;
268 bv->lockedInsetStoreUndo(Undo::INSERT);
269 mathcursor->breakLine();
270 mathcursor->normalize();
271 updateLocal(bv, true);
274 case LFUN_MATH_NUMBER:
278 //lyxerr << "toggling all numbers\n";
280 bv->lockedInsetStoreUndo(Undo::INSERT);
281 bool old = par()->numberedType();
282 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
283 hull()->numbered(row, !old);
284 bv->owner()->message(old ? _("No number") : _("Number"));
285 updateLocal(bv, true);
290 case LFUN_MATH_NONUMBER:
292 //lyxerr << "toggling line number\n";
294 bv->lockedInsetStoreUndo(Undo::INSERT);
295 MathCursor::row_type row = mathcursor->hullRow();
296 bool old = hull()->numbered(row);
297 bv->owner()->message(old ? _("No number") : _("Number"));
298 hull()->numbered(row, !old);
299 updateLocal(bv, true);
304 case LFUN_INSERT_LABEL:
309 bv->lockedInsetStoreUndo(Undo::INSERT);
311 MathCursor::row_type row = mathcursor->hullRow();
312 string old_label = hull()->label(row);
313 string new_label = arg;
315 if (new_label.empty()) {
316 string const default_label =
317 (lyxrc.label_init_length >= 0) ? "eq:" : "";
318 pair<bool, string> const res = old_label.empty()
319 ? Alert::askForText(_("Enter new label to insert:"), default_label)
320 : Alert::askForText(_("Enter label:"), old_label);
323 new_label = trim(res.second);
326 //if (new_label == old_label)
327 // break; // Nothing to do
329 if (!new_label.empty()) {
330 lyxerr << "setting label to '" << new_label << "'\n";
331 hull()->numbered(row, true);
334 #warning FIXME: please check you really mean repaint() ... is it needed,
335 #warning and if so, should it be update() instead ?
336 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
339 hull()->label(row, new_label);
341 updateLocal(bv, true);
345 case LFUN_MATH_MUTATE:
347 bv->lockedInsetStoreUndo(Undo::EDIT);
350 mathcursor->getPos(x, y);
352 mathcursor->setPos(x, y);
353 mathcursor->normalize();
354 updateLocal(bv, true);
358 case LFUN_MATH_EXTERN:
360 bv->lockedInsetStoreUndo(Undo::EDIT);
362 mathcursor->handleExtern(arg);
363 // re-compute inset dimension
365 updateLocal(bv, true);
369 case LFUN_MATH_DISPLAY:
373 mathcursor->getPos(x, y);
374 if (hullType() == "simple")
378 mathcursor->setPos(x, y);
379 mathcursor->normalize();
380 updateLocal(bv, true);
384 case LFUN_PASTESELECTION:
386 string const clip = bv->getClipboard();
388 mathed_parse_normal(par_, clip);
393 result = InsetFormulaBase::localDispatch(bv, action, arg);
400 bool InsetFormula::display() const
402 return hullType() != "simple" && hullType() != "none";
406 MathHullInset * InsetFormula::hull() const
408 lyx::Assert(par_->asHullInset());
409 return par_->asHullInset();
413 Inset::Code InsetFormula::lyxCode() const
415 return Inset::MATH_CODE;
419 void InsetFormula::validate(LaTeXFeatures & features) const
421 par_->validate(features);
425 bool InsetFormula::insetAllowed(Inset::Code code) const
428 (code == Inset::LABEL_CODE && display())
429 || code == Inset::REF_CODE
430 || code == Inset::ERT_CODE;
434 int InsetFormula::ascent(BufferView *, LyXFont const &) const
436 return preview_->previewReady() ?
437 preview_->pimage()->ascent() : 1 + par_->ascent();
441 int InsetFormula::descent(BufferView *, LyXFont const &) const
443 if (!preview_->previewReady())
444 return 1 + par_->descent();
446 int const descent = preview_->pimage()->descent();
447 return display() ? descent + 12 : descent;
451 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
454 return preview_->previewReady() ?
455 preview_->pimage()->width() : par_->width();
459 string InsetFormula::hullType() const
461 return par()->getType();
465 void InsetFormula::mutate(string const & type)
475 void InsetFormula::addPreview(grfx::PreviewLoader & ploader) const
477 preview_->addPreview(ploader);
481 void InsetFormula::generatePreview() const
483 preview_->generatePreview();
487 bool InsetFormula::PreviewImpl::previewWanted() const
489 return !parent().par_->asNestInset()->editing();
493 string const InsetFormula::PreviewImpl::latexString() const
496 WriteStream wi(ls, false, false);
497 parent().par_->write(wi);
498 return ls.str().c_str();