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"
34 #include "BufferView.h"
38 #include "funcrequest.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 InsetFormula & parent() const
78 return *static_cast<InsetFormula*>(inset());
84 InsetFormula::InsetFormula()
85 : par_(MathAtom(new MathHullInset)),
86 preview_(new PreviewImpl(*this))
90 InsetFormula::InsetFormula(InsetFormula const & other)
91 : InsetFormulaBase(other),
93 preview_(new PreviewImpl(*this))
97 InsetFormula::InsetFormula(BufferView * bv)
98 : par_(MathAtom(new MathHullInset)),
99 preview_(new PreviewImpl(*this))
101 view_ = bv->owner()->view();
105 InsetFormula::InsetFormula(string const & data)
106 : par_(MathAtom(new MathHullInset)),
107 preview_(new PreviewImpl(*this))
111 if (!mathed_parse_normal(par_, data))
112 lyxerr << "cannot interpret '" << data << "' as math\n";
117 InsetFormula::~InsetFormula()
121 Inset * InsetFormula::clone(Buffer const &, bool) const
123 return new InsetFormula(*this);
127 void InsetFormula::write(Buffer const *, ostream & os) const
129 WriteStream wi(os, false, false);
130 os << par_->fileInsetLabel() << " ";
135 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
137 WriteStream wi(os, fragile, true);
143 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
148 TextPainter tpain(par()->width(), par()->height());
149 par()->drawT(tpain, 0, par()->ascent());
151 // reset metrics cache to "real" values
153 return tpain.textheight();
155 WriteStream wi(os, false, true);
156 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
162 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
164 return docbook(buf, os, false);
168 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
171 ms << MTag("equation");
174 int res = ascii(buf, ms.os(), 0);
178 ms << par_.nucleus();
180 ms << ETag("equation");
181 return ms.line() + res;
185 void InsetFormula::read(Buffer const *, LyXLex & lex)
187 mathed_parse_normal(par_, lex);
192 //ostream & operator<<(ostream & os, LyXCursor const & c)
194 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
199 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
200 int y, float & xx, bool) const
202 // This initiates the loading of the preview, so should come
203 // before the metrics are computed.
204 preview_->setView(bv);
205 bool const use_preview = preview_->previewReady();
207 int const x = int(xx);
208 int const w = width(bv, font);
209 int const d = descent(bv, font);
210 int const a = ascent(bv, font);
213 MathPainterInfo pi(bv->painter());
216 pi.pain.image(x, y - a, w, h,
217 *(preview_->pimage()->image(*this, *bv)));
219 //pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
220 pi.base.style = LM_ST_TEXT;
222 pi.base.font.setColor(LColor::math);
223 if (lcolor.getX11Name(LColor::mathbg)
224 != lcolor.getX11Name(LColor::background))
225 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
228 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
230 mathcursor->drawSelection(pi);
231 //pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
234 par_->draw(pi, x + 1, y);
241 setCursorVisible(false);
245 vector<string> const InsetFormula::getLabelList() const
248 par()->getLabelList(res);
253 UpdatableInset::RESULT
254 InsetFormula::localDispatch(BufferView * bv, FuncRequest const & ev)
256 RESULT result = DISPATCHED;
261 bv->lockedInsetStoreUndo(Undo::INSERT);
262 mathcursor->breakLine();
263 mathcursor->normalize();
264 updateLocal(bv, true);
267 case LFUN_MATH_NUMBER:
271 //lyxerr << "toggling all numbers\n";
273 bv->lockedInsetStoreUndo(Undo::INSERT);
274 bool old = par()->numberedType();
275 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
276 hull()->numbered(row, !old);
277 bv->owner()->message(old ? _("No number") : _("Number"));
278 updateLocal(bv, true);
283 case LFUN_MATH_NONUMBER:
285 //lyxerr << "toggling line number\n";
287 bv->lockedInsetStoreUndo(Undo::INSERT);
288 MathCursor::row_type row = mathcursor->hullRow();
289 bool old = hull()->numbered(row);
290 bv->owner()->message(old ? _("No number") : _("Number"));
291 hull()->numbered(row, !old);
292 updateLocal(bv, true);
297 case LFUN_INSERT_LABEL:
302 bv->lockedInsetStoreUndo(Undo::INSERT);
304 MathCursor::row_type row = mathcursor->hullRow();
305 string old_label = hull()->label(row);
306 string new_label = ev.argument;
308 if (new_label.empty()) {
309 string const default_label =
310 (lyxrc.label_init_length >= 0) ? "eq:" : "";
311 pair<bool, string> const res = old_label.empty()
312 ? Alert::askForText(_("Enter new label to insert:"), default_label)
313 : Alert::askForText(_("Enter label:"), old_label);
316 new_label = trim(res.second);
319 //if (new_label == old_label)
320 // break; // Nothing to do
322 if (!new_label.empty()) {
323 lyxerr << "setting label to '" << new_label << "'\n";
324 hull()->numbered(row, true);
327 #warning FIXME: please check you really mean repaint() ... is it needed,
328 #warning and if so, should it be update() instead ?
329 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
332 hull()->label(row, new_label);
334 updateLocal(bv, true);
338 case LFUN_MATH_MUTATE:
340 bv->lockedInsetStoreUndo(Undo::EDIT);
343 mathcursor->getPos(x, y);
345 mathcursor->setPos(x, y);
346 mathcursor->normalize();
347 updateLocal(bv, true);
351 case LFUN_MATH_EXTERN:
353 bv->lockedInsetStoreUndo(Undo::EDIT);
355 mathcursor->handleExtern(ev.argument);
356 // re-compute inset dimension
358 updateLocal(bv, true);
362 case LFUN_MATH_DISPLAY:
366 mathcursor->getPos(x, y);
367 if (hullType() == "simple")
371 mathcursor->setPos(x, y);
372 mathcursor->normalize();
373 updateLocal(bv, true);
377 case LFUN_PASTESELECTION:
379 string const clip = bv->getClipboard();
381 mathed_parse_normal(par_, clip);
386 result = InsetFormulaBase::localDispatch(bv, ev);
393 bool InsetFormula::display() const
395 return hullType() != "simple" && hullType() != "none";
399 MathHullInset * InsetFormula::hull() const
401 lyx::Assert(par_->asHullInset());
402 return par_->asHullInset();
406 Inset::Code InsetFormula::lyxCode() const
408 return Inset::MATH_CODE;
412 void InsetFormula::validate(LaTeXFeatures & features) const
414 par_->validate(features);
418 bool InsetFormula::insetAllowed(Inset::Code code) const
421 (code == Inset::LABEL_CODE && display())
422 || code == Inset::REF_CODE
423 || code == Inset::ERT_CODE;
427 int InsetFormula::ascent(BufferView *, LyXFont const &) const
429 return preview_->previewReady() ?
430 preview_->pimage()->ascent() : 1 + par_->ascent();
434 int InsetFormula::descent(BufferView *, LyXFont const &) const
436 if (!preview_->previewReady())
437 return 1 + par_->descent();
439 int const descent = preview_->pimage()->descent();
440 return display() ? descent + 12 : descent;
444 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
447 return preview_->previewReady() ?
448 preview_->pimage()->width() : par_->width();
452 string InsetFormula::hullType() const
454 return par()->getType();
458 void InsetFormula::mutate(string const & type)
468 void InsetFormula::addPreview(grfx::PreviewLoader & ploader) const
470 preview_->addPreview(ploader);
474 void InsetFormula::generatePreview() const
476 preview_->generatePreview();
480 bool InsetFormula::PreviewImpl::previewWanted() const
482 return !parent().par_->asNestInset()->editing();
486 string const InsetFormula::PreviewImpl::latexString() const
489 WriteStream wi(ls, false, false);
490 parent().par_->write(wi);
491 return ls.str().c_str();