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"
30 #include "BufferView.h"
33 #include "support/LOstream.h"
34 #include "support/LAssert.h"
35 #include "support/lyxlib.h"
36 #include "support/systemcall.h"
37 #include "support/filetools.h"
38 #include "frontends/Alert.h"
39 #include "frontends/LyXView.h"
40 #include "frontends/Painter.h"
41 #include "graphics/GraphicsImage.h"
43 #include "math_hullinset.h"
44 #include "math_support.h"
45 #include "math_mathmlstream.h"
46 #include "textpainter.h"
49 #include <boost/bind.hpp>
50 #include <boost/utility.hpp>
62 InsetFormula::InsetFormula()
63 : par_(MathAtom(new MathHullInset)), loader_(0)
67 InsetFormula::InsetFormula(MathInsetTypes t)
68 : par_(MathAtom(new MathHullInset(t))), loader_(0)
72 InsetFormula::InsetFormula(string const & s)
75 bool res = mathed_parse_normal(par_, s);
78 res = mathed_parse_normal(par_, "$" + s + "$");
81 lyxerr << "cannot interpret '" << s << "' as math\n";
82 par_ = MathAtom(new MathHullInset(LM_OT_SIMPLE));
90 Inset * InsetFormula::clone(Buffer const &, bool) const
92 return new InsetFormula(*this);
96 void InsetFormula::write(Buffer const *, ostream & os) const
99 WriteStream wi(os, false, false);
104 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
106 WriteStream wi(os, fragile, true);
112 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
117 TextPainter tpain(par()->width(), par()->height());
118 par()->drawT(tpain, 0, par()->ascent());
120 // reset metrics cache to "real" values
122 return tpain.textheight();
124 WriteStream wi(os, false, true);
131 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
133 return docbook(buf, os, false);
137 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
140 ms << MTag("equation");
143 int res = ascii(buf, ms.os(), 0);
147 ms << par_.nucleus();
149 ms << ETag("equation");
150 return ms.line() + res;
154 void InsetFormula::read(Buffer const *, LyXLex & lex)
156 mathed_parse_normal(par_, lex);
162 //ostream & operator<<(ostream & os, LyXCursor const & c)
164 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
169 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
170 int y, float & xx, bool) const
172 int const x = int(xx);
173 int const w = width(bv, font);
174 int const d = descent(bv, font);
175 int const a = ascent(bv, font);
178 MathPainterInfo pi(bv->painter());
181 pi.pain.image(x + 1, y - a + 1, w - 2, h - 2, *(loader_->image()));
183 pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
185 pi.base.font.setColor(LColor::math);
186 if (lcolor.getX11Name(LColor::mathbg)
187 != lcolor.getX11Name(LColor::background))
188 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
191 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
193 mathcursor->drawSelection(pi);
194 pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
197 par_->draw(pi, x, y);
204 setCursorVisible(false);
208 vector<string> const InsetFormula::getLabelList() const
210 return hull()->getLabelList();
214 UpdatableInset::RESULT
215 InsetFormula::localDispatch(BufferView * bv, kb_action action,
218 RESULT result = DISPATCHED;
223 bv->lockedInsetStoreUndo(Undo::INSERT);
224 mathcursor->breakLine();
225 mathcursor->normalize();
226 updateLocal(bv, true);
229 case LFUN_MATH_NUMBER:
231 //lyxerr << "toggling all numbers\n";
233 bv->lockedInsetStoreUndo(Undo::INSERT);
234 bool old = hull()->numberedType();
235 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
236 hull()->numbered(row, !old);
237 bv->owner()->message(old ? _("No number") : _("Number"));
238 updateLocal(bv, true);
243 case LFUN_MATH_NONUMBER:
245 //lyxerr << "toggling line number\n";
247 bv->lockedInsetStoreUndo(Undo::INSERT);
248 MathCursor::row_type row = mathcursor->hullRow();
249 bool old = hull()->numbered(row);
250 bv->owner()->message(old ? _("No number") : _("Number"));
251 hull()->numbered(row, !old);
252 updateLocal(bv, true);
257 case LFUN_INSERT_LABEL:
259 bv->lockedInsetStoreUndo(Undo::INSERT);
261 MathCursor::row_type row = mathcursor->hullRow();
262 string old_label = hull()->label(row);
263 string new_label = arg;
265 if (new_label.empty()) {
266 string const default_label =
267 (lyxrc.label_init_length >= 0) ? "eq:" : "";
268 pair<bool, string> const res = old_label.empty()
269 ? Alert::askForText(_("Enter new label to insert:"), default_label)
270 : Alert::askForText(_("Enter label:"), old_label);
273 new_label = frontStrip(strip(res.second));
276 //if (new_label == old_label)
277 // break; // Nothing to do
279 if (!new_label.empty()) {
280 lyxerr << "setting label to '" << new_label << "'\n";
281 hull()->numbered(row, true);
284 #warning FIXME: please check you really mean repaint() ... is it needed,
285 #warning and if so, should it be update() instead ?
286 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
289 hull()->label(row, new_label);
291 updateLocal(bv, true);
295 case LFUN_MATH_MUTATE:
297 bv->lockedInsetStoreUndo(Undo::EDIT);
300 mathcursor->getPos(x, y);
302 mathcursor->setPos(x, y);
303 mathcursor->normalize();
304 updateLocal(bv, true);
308 case LFUN_MATH_EXTERN:
310 bv->lockedInsetStoreUndo(Undo::EDIT);
312 mathcursor->handleExtern(arg);
313 // re-compute inset dimension
315 updateLocal(bv, true);
319 case LFUN_MATH_DISPLAY:
323 mathcursor->getPos(x, y);
324 if (getType() == LM_OT_SIMPLE)
325 hull()->mutate(LM_OT_EQUATION);
327 hull()->mutate(LM_OT_SIMPLE);
328 mathcursor->setPos(x, y);
329 mathcursor->normalize();
330 updateLocal(bv, true);
334 case LFUN_PASTESELECTION:
336 string const clip = bv->getClipboard();
338 mathed_parse_normal(par_, clip);
343 result = InsetFormulaBase::localDispatch(bv, action, arg);
352 bool InsetFormula::display() const
354 return getType() != LM_OT_SIMPLE;
358 MathHullInset const * InsetFormula::hull() const
360 lyx::Assert(par_->asHullInset());
361 return par_->asHullInset();
365 MathHullInset * InsetFormula::hull()
367 lyx::Assert(par_->asHullInset());
368 return par_->asHullInset();
372 Inset::Code InsetFormula::lyxCode() const
374 return Inset::MATH_CODE;
378 void InsetFormula::validate(LaTeXFeatures & features) const
380 par_->validate(features);
384 bool InsetFormula::insetAllowed(Inset::Code code) const
387 (code == Inset::LABEL_CODE && display())
388 || code == Inset::REF_CODE
389 || code == Inset::ERT_CODE;
393 int InsetFormula::ascent(BufferView *, LyXFont const &) const
395 const int a = par_->ascent();
398 return a + 1 - (par_->height() - loader_->image()->getHeight()) / 2;
402 int InsetFormula::descent(BufferView *, LyXFont const &) const
404 const int d = par_->descent();
407 return d + 1 - (par_->height() - loader_->image()->getHeight()) / 2;
411 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
414 return canPreview() ? loader_->image()->getWidth() : par_->width();
418 MathInsetTypes InsetFormula::getType() const
420 return hull()->getType();
428 bool InsetFormula::canPreview() const
430 return lyxrc.preview && loader_ && !par_->asNestInset()->editing()
431 && loader_->status() == grfx::Ready;
435 void InsetFormula::statusChanged()
437 lyxerr << "### InsetFormula::statusChanged called!, status: "
438 << loader_->status() << "\n";
439 if (loader_->status() == grfx::Ready)
440 view()->updateInset(this, false);
441 else if (loader_->status() == grfx::WaitingToLoad)
442 loader_->startLoading();
446 void InsetFormula::updatePreview()
448 // nothing to be done if no preview requested
449 lyxerr << "### updatePreview() called\n";
455 WriteStream wi(ls, false, false);
457 string const data = ls.str();
459 // the preview cache, maps contents to image loaders
460 typedef std::map<string, boost::shared_ptr<grfx::Loader> > cache_type;
461 static cache_type theCache;
462 static int theCounter = 0;
464 // set our loader corresponding to our current data
465 cache_type::const_iterator it = theCache.find(data);
468 if (it != theCache.end()) {
469 // we have already a loader, connect to it anyway
470 //lyxerr << "### updatePreview(), old loader: " << loader_ << "\n";
471 loader_ = it->second.get();
472 loader_->statusChanged.connect
473 (boost::bind(&InsetFormula::statusChanged, this));
477 // construct new file name
478 static string const dir = OnlyPath(lyx::tempName());
480 os << dir << theCounter++ << ".lyxpreview";
481 string file = os.str();
483 // the real work starts
484 //lyxerr << "### updatePreview(), new file " << file << "\n";
485 std::ofstream of(file.c_str());
487 << "\\documentclass{article}"
488 << "\\usepackage{amssymb}"
489 << "\\thispagestyle{empty}"
491 << "\\begin{document}"
493 << "\\end{document}\n";
496 // now we are done, start actual loading we will get called back via
497 // InsetFormula::statusChanged() if this is finished
498 theCache[data].reset(new grfx::Loader(file));
499 //lyxerr << "### updatePreview(), new loader: " << loader_ << "\n";
500 loader_ = theCache.find(data)->second.get();
501 loader_->startLoading();
502 loader_->statusChanged.connect(boost::bind(&InsetFormula::statusChanged, this));