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)
68 InsetFormula::InsetFormula(string const & type)
69 : par_(MathAtom(new MathHullInset(type))), loader_(0)
76 Inset * InsetFormula::clone(Buffer const &, bool) const
78 return new InsetFormula(*this);
82 void InsetFormula::write(Buffer const *, ostream & os) const
85 WriteStream wi(os, false, false);
90 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
92 WriteStream wi(os, fragile, true);
98 void InsetFormula::read(string const & s)
101 bool res = mathed_parse_normal(par_, s);
104 res = mathed_parse_normal(par_, "$" + s + "$");
107 lyxerr << "cannot interpret '" << s << "' as math\n";
108 par_ = MathAtom(new MathHullInset("simple"));
116 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
121 TextPainter tpain(par()->width(), par()->height());
122 par()->drawT(tpain, 0, par()->ascent());
124 // reset metrics cache to "real" values
126 return tpain.textheight();
128 WriteStream wi(os, false, true);
135 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
137 return docbook(buf, os, false);
141 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
144 ms << MTag("equation");
147 int res = ascii(buf, ms.os(), 0);
151 ms << par_.nucleus();
153 ms << ETag("equation");
154 return ms.line() + res;
158 void InsetFormula::read(Buffer const *, LyXLex & lex)
160 mathed_parse_normal(par_, lex);
166 //ostream & operator<<(ostream & os, LyXCursor const & c)
168 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
173 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
174 int y, float & xx, bool) const
176 int const x = int(xx);
177 int const w = width(bv, font);
178 int const d = descent(bv, font);
179 int const a = ascent(bv, font);
182 MathPainterInfo pi(bv->painter());
185 pi.pain.image(x + 1, y - a + 1, w - 2, h - 2, *(loader_->image()));
187 pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
189 pi.base.font.setColor(LColor::math);
190 if (lcolor.getX11Name(LColor::mathbg)
191 != lcolor.getX11Name(LColor::background))
192 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
195 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
197 mathcursor->drawSelection(pi);
198 pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
201 par_->draw(pi, x, y);
208 setCursorVisible(false);
212 vector<string> const InsetFormula::getLabelList() const
214 return hull()->getLabelList();
218 UpdatableInset::RESULT
219 InsetFormula::localDispatch(BufferView * bv, kb_action action,
222 RESULT result = DISPATCHED;
227 bv->lockedInsetStoreUndo(Undo::INSERT);
228 mathcursor->breakLine();
229 mathcursor->normalize();
230 updateLocal(bv, true);
233 case LFUN_MATH_NUMBER:
235 //lyxerr << "toggling all numbers\n";
237 bv->lockedInsetStoreUndo(Undo::INSERT);
238 bool old = hull()->numberedType();
239 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
240 hull()->numbered(row, !old);
241 bv->owner()->message(old ? _("No number") : _("Number"));
242 updateLocal(bv, true);
247 case LFUN_MATH_NONUMBER:
249 //lyxerr << "toggling line number\n";
251 bv->lockedInsetStoreUndo(Undo::INSERT);
252 MathCursor::row_type row = mathcursor->hullRow();
253 bool old = hull()->numbered(row);
254 bv->owner()->message(old ? _("No number") : _("Number"));
255 hull()->numbered(row, !old);
256 updateLocal(bv, true);
261 case LFUN_INSERT_LABEL:
263 bv->lockedInsetStoreUndo(Undo::INSERT);
265 MathCursor::row_type row = mathcursor->hullRow();
266 string old_label = hull()->label(row);
267 string new_label = arg;
269 if (new_label.empty()) {
270 string const default_label =
271 (lyxrc.label_init_length >= 0) ? "eq:" : "";
272 pair<bool, string> const res = old_label.empty()
273 ? Alert::askForText(_("Enter new label to insert:"), default_label)
274 : Alert::askForText(_("Enter label:"), old_label);
277 new_label = frontStrip(strip(res.second));
280 //if (new_label == old_label)
281 // break; // Nothing to do
283 if (!new_label.empty()) {
284 lyxerr << "setting label to '" << new_label << "'\n";
285 hull()->numbered(row, true);
288 #warning FIXME: please check you really mean repaint() ... is it needed,
289 #warning and if so, should it be update() instead ?
290 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
293 hull()->label(row, new_label);
295 updateLocal(bv, true);
299 case LFUN_MATH_MUTATE:
301 bv->lockedInsetStoreUndo(Undo::EDIT);
304 mathcursor->getPos(x, y);
306 mathcursor->setPos(x, y);
307 mathcursor->normalize();
308 updateLocal(bv, true);
312 case LFUN_MATH_EXTERN:
314 bv->lockedInsetStoreUndo(Undo::EDIT);
316 mathcursor->handleExtern(arg);
317 // re-compute inset dimension
319 updateLocal(bv, true);
323 case LFUN_MATH_DISPLAY:
327 mathcursor->getPos(x, y);
328 if (hullType() == "simple")
329 hull()->mutate("equation");
331 hull()->mutate("simple");
332 mathcursor->setPos(x, y);
333 mathcursor->normalize();
334 updateLocal(bv, true);
338 case LFUN_PASTESELECTION:
340 string const clip = bv->getClipboard();
342 mathed_parse_normal(par_, clip);
347 result = InsetFormulaBase::localDispatch(bv, action, arg);
356 bool InsetFormula::display() const
358 return hullType() != "simple" && hullType() != "none";
362 MathHullInset const * InsetFormula::hull() const
364 lyx::Assert(par_->asHullInset());
365 return par_->asHullInset();
369 MathHullInset * InsetFormula::hull()
371 lyx::Assert(par_->asHullInset());
372 return par_->asHullInset();
376 Inset::Code InsetFormula::lyxCode() const
378 return Inset::MATH_CODE;
382 void InsetFormula::validate(LaTeXFeatures & features) const
384 par_->validate(features);
388 bool InsetFormula::insetAllowed(Inset::Code code) const
391 (code == Inset::LABEL_CODE && display())
392 || code == Inset::REF_CODE
393 || code == Inset::ERT_CODE;
397 int InsetFormula::ascent(BufferView *, LyXFont const &) const
399 const int a = par_->ascent();
402 return a + 1 - (par_->height() - loader_->image()->getHeight()) / 2;
406 int InsetFormula::descent(BufferView *, LyXFont const &) const
408 const int d = par_->descent();
411 return d + 1 - (par_->height() - loader_->image()->getHeight()) / 2;
415 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
418 return canPreview() ? loader_->image()->getWidth() : par_->width();
422 string const & InsetFormula::hullType() const
424 return hull()->getType();
428 void InsetFormula::mutate(string const & type )
430 hull()->mutate(type);
438 bool InsetFormula::canPreview() const
440 return lyxrc.preview && loader_ && !par_->asNestInset()->editing()
441 && loader_->status() == grfx::Ready;
445 void InsetFormula::statusChanged()
447 lyxerr << "### InsetFormula::statusChanged called!, status: "
448 << loader_->status() << "\n";
449 if (loader_->status() == grfx::Ready)
450 view()->updateInset(this, false);
451 else if (loader_->status() == grfx::WaitingToLoad)
452 loader_->startLoading();
456 void InsetFormula::updatePreview()
458 // nothing to be done if no preview requested
459 lyxerr << "### updatePreview() called\n";
465 WriteStream wi(ls, false, false);
467 string const data = ls.str();
469 // the preview cache, maps contents to image loaders
470 typedef std::map<string, boost::shared_ptr<grfx::Loader> > cache_type;
471 static cache_type theCache;
472 static int theCounter = 0;
474 // set our loader corresponding to our current data
475 cache_type::const_iterator it = theCache.find(data);
478 if (it != theCache.end()) {
479 // we have already a loader, connect to it anyway
480 //lyxerr << "### updatePreview(), old loader: " << loader_ << "\n";
481 loader_ = it->second.get();
482 loader_->statusChanged.connect
483 (boost::bind(&InsetFormula::statusChanged, this));
487 // construct new file name
488 static string const dir = OnlyPath(lyx::tempName());
490 os << dir << theCounter++ << ".lyxpreview";
491 string file = os.str();
493 // the real work starts
494 //lyxerr << "### updatePreview(), new file " << file << "\n";
495 std::ofstream of(file.c_str());
497 << "\\documentclass{article}"
498 << "\\usepackage{amssymb}"
499 << "\\thispagestyle{empty}"
501 << "\\begin{document}"
503 << "\\end{document}\n";
506 // now we are done, start actual loading we will get called back via
507 // InsetFormula::statusChanged() if this is finished
508 theCache[data].reset(new grfx::Loader(file));
509 //lyxerr << "### updatePreview(), new loader: " << loader_ << "\n";
510 loader_ = theCache.find(data)->second.get();
511 loader_->startLoading();
512 loader_->statusChanged.connect(boost::bind(&InsetFormula::statusChanged, this));