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 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, kb_action action,
258 RESULT result = DISPATCHED;
263 bv->lockedInsetStoreUndo(Undo::INSERT);
264 mathcursor->breakLine();
265 mathcursor->normalize();
266 updateLocal(bv, true);
269 case LFUN_MATH_NUMBER:
273 //lyxerr << "toggling all numbers\n";
275 bv->lockedInsetStoreUndo(Undo::INSERT);
276 bool old = par()->numberedType();
277 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
278 hull()->numbered(row, !old);
279 bv->owner()->message(old ? _("No number") : _("Number"));
280 updateLocal(bv, true);
285 case LFUN_MATH_NONUMBER:
287 //lyxerr << "toggling line number\n";
289 bv->lockedInsetStoreUndo(Undo::INSERT);
290 MathCursor::row_type row = mathcursor->hullRow();
291 bool old = hull()->numbered(row);
292 bv->owner()->message(old ? _("No number") : _("Number"));
293 hull()->numbered(row, !old);
294 updateLocal(bv, true);
299 case LFUN_INSERT_LABEL:
304 bv->lockedInsetStoreUndo(Undo::INSERT);
306 MathCursor::row_type row = mathcursor->hullRow();
307 string old_label = hull()->label(row);
308 string new_label = arg;
310 if (new_label.empty()) {
311 string const default_label =
312 (lyxrc.label_init_length >= 0) ? "eq:" : "";
313 pair<bool, string> const res = old_label.empty()
314 ? Alert::askForText(_("Enter new label to insert:"), default_label)
315 : Alert::askForText(_("Enter label:"), old_label);
318 new_label = trim(res.second);
321 //if (new_label == old_label)
322 // break; // Nothing to do
324 if (!new_label.empty()) {
325 lyxerr << "setting label to '" << new_label << "'\n";
326 hull()->numbered(row, true);
329 #warning FIXME: please check you really mean repaint() ... is it needed,
330 #warning and if so, should it be update() instead ?
331 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
334 hull()->label(row, new_label);
336 updateLocal(bv, true);
340 case LFUN_MATH_MUTATE:
342 bv->lockedInsetStoreUndo(Undo::EDIT);
345 mathcursor->getPos(x, y);
347 mathcursor->setPos(x, y);
348 mathcursor->normalize();
349 updateLocal(bv, true);
353 case LFUN_MATH_EXTERN:
355 bv->lockedInsetStoreUndo(Undo::EDIT);
357 mathcursor->handleExtern(arg);
358 // re-compute inset dimension
360 updateLocal(bv, true);
364 case LFUN_MATH_DISPLAY:
368 mathcursor->getPos(x, y);
369 if (hullType() == "simple")
373 mathcursor->setPos(x, y);
374 mathcursor->normalize();
375 updateLocal(bv, true);
379 case LFUN_PASTESELECTION:
381 string const clip = bv->getClipboard();
383 mathed_parse_normal(par_, clip);
388 result = InsetFormulaBase::localDispatch(bv, action, arg);
395 bool InsetFormula::display() const
397 return hullType() != "simple" && hullType() != "none";
401 MathHullInset * InsetFormula::hull() const
403 lyx::Assert(par_->asHullInset());
404 return par_->asHullInset();
408 Inset::Code InsetFormula::lyxCode() const
410 return Inset::MATH_CODE;
414 void InsetFormula::validate(LaTeXFeatures & features) const
416 par_->validate(features);
420 bool InsetFormula::insetAllowed(Inset::Code code) const
423 (code == Inset::LABEL_CODE && display())
424 || code == Inset::REF_CODE
425 || code == Inset::ERT_CODE;
429 int InsetFormula::ascent(BufferView *, LyXFont const &) const
431 return preview_->previewReady() ?
432 preview_->pimage()->ascent() : 1 + par_->ascent();
436 int InsetFormula::descent(BufferView *, LyXFont const &) const
438 if (!preview_->previewReady())
439 return 1 + par_->descent();
441 int const descent = preview_->pimage()->descent();
442 return display() ? descent + 12 : descent;
446 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
449 return preview_->previewReady() ?
450 preview_->pimage()->width() : par_->width();
454 string InsetFormula::hullType() const
456 return par()->getType();
460 void InsetFormula::mutate(string const & type)
470 void InsetFormula::addPreview(grfx::PreviewLoader & ploader) const
472 preview_->addPreview(ploader);
476 void InsetFormula::generatePreview() const
478 preview_->generatePreview();
482 bool InsetFormula::PreviewImpl::previewWanted() const
484 return !parent().par_->asNestInset()->editing();
488 string const InsetFormula::PreviewImpl::latexString() const
491 WriteStream wi(ls, false, false);
492 parent().par_->write(wi);
493 return ls.str().c_str();