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(BufferView * bv)
68 : par_(MathAtom(new MathHullInset)), loader_(0)
74 InsetFormula::InsetFormula(string const & data)
75 : par_(MathAtom(new MathHullInset)), loader_(0)
79 if (!mathed_parse_normal(par_, data))
80 lyxerr << "cannot interpret '" << data << "' as math\n";
85 Inset * InsetFormula::clone(Buffer const &, bool) const
87 return new InsetFormula(*this);
91 void InsetFormula::write(Buffer const *, ostream & os) const
94 WriteStream wi(os, false, false);
99 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
101 WriteStream wi(os, fragile, true);
108 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
113 TextPainter tpain(par()->width(), par()->height());
114 par()->drawT(tpain, 0, par()->ascent());
116 // reset metrics cache to "real" values
118 return tpain.textheight();
120 WriteStream wi(os, false, true);
127 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
129 return docbook(buf, os, false);
133 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
136 ms << MTag("equation");
139 int res = ascii(buf, ms.os(), 0);
143 ms << par_.nucleus();
145 ms << ETag("equation");
146 return ms.line() + res;
150 void InsetFormula::read(Buffer const *, LyXLex & lex)
152 mathed_parse_normal(par_, lex);
158 //ostream & operator<<(ostream & os, LyXCursor const & c)
160 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
165 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
166 int y, float & xx, bool) const
168 int const x = int(xx);
169 int const w = width(bv, font);
170 int const d = descent(bv, font);
171 int const a = ascent(bv, font);
174 MathPainterInfo pi(bv->painter());
177 pi.pain.image(x + 1, y - a + 1, w - 2, h - 2, *(loader_->image()));
179 //pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
180 pi.base.style = LM_ST_TEXT;
182 pi.base.font.setColor(LColor::math);
183 if (lcolor.getX11Name(LColor::mathbg)
184 != lcolor.getX11Name(LColor::background))
185 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
188 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
190 mathcursor->drawSelection(pi);
191 pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
194 par_->draw(pi, x, y);
201 setCursorVisible(false);
205 vector<string> const InsetFormula::getLabelList() const
207 return hull()->getLabelList();
211 UpdatableInset::RESULT
212 InsetFormula::localDispatch(BufferView * bv, kb_action action,
215 RESULT result = DISPATCHED;
220 bv->lockedInsetStoreUndo(Undo::INSERT);
221 mathcursor->breakLine();
222 mathcursor->normalize();
223 updateLocal(bv, true);
226 case LFUN_MATH_NUMBER:
228 //lyxerr << "toggling all numbers\n";
230 bv->lockedInsetStoreUndo(Undo::INSERT);
231 bool old = hull()->numberedType();
232 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
233 hull()->numbered(row, !old);
234 bv->owner()->message(old ? _("No number") : _("Number"));
235 updateLocal(bv, true);
240 case LFUN_MATH_NONUMBER:
242 //lyxerr << "toggling line number\n";
244 bv->lockedInsetStoreUndo(Undo::INSERT);
245 MathCursor::row_type row = mathcursor->hullRow();
246 bool old = hull()->numbered(row);
247 bv->owner()->message(old ? _("No number") : _("Number"));
248 hull()->numbered(row, !old);
249 updateLocal(bv, true);
254 case LFUN_INSERT_LABEL:
256 bv->lockedInsetStoreUndo(Undo::INSERT);
258 MathCursor::row_type row = mathcursor->hullRow();
259 string old_label = hull()->label(row);
260 string new_label = arg;
262 if (new_label.empty()) {
263 string const default_label =
264 (lyxrc.label_init_length >= 0) ? "eq:" : "";
265 pair<bool, string> const res = old_label.empty()
266 ? Alert::askForText(_("Enter new label to insert:"), default_label)
267 : Alert::askForText(_("Enter label:"), old_label);
270 new_label = frontStrip(strip(res.second));
273 //if (new_label == old_label)
274 // break; // Nothing to do
276 if (!new_label.empty()) {
277 lyxerr << "setting label to '" << new_label << "'\n";
278 hull()->numbered(row, true);
281 #warning FIXME: please check you really mean repaint() ... is it needed,
282 #warning and if so, should it be update() instead ?
283 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
286 hull()->label(row, new_label);
288 updateLocal(bv, true);
292 case LFUN_MATH_MUTATE:
294 bv->lockedInsetStoreUndo(Undo::EDIT);
297 mathcursor->getPos(x, y);
299 mathcursor->setPos(x, y);
300 mathcursor->normalize();
301 updateLocal(bv, true);
305 case LFUN_MATH_EXTERN:
307 bv->lockedInsetStoreUndo(Undo::EDIT);
309 mathcursor->handleExtern(arg);
310 // re-compute inset dimension
312 updateLocal(bv, true);
316 case LFUN_MATH_DISPLAY:
320 mathcursor->getPos(x, y);
321 if (hullType() == "simple")
325 mathcursor->setPos(x, y);
326 mathcursor->normalize();
327 updateLocal(bv, true);
331 case LFUN_PASTESELECTION:
333 string const clip = bv->getClipboard();
335 mathed_parse_normal(par_, clip);
340 result = InsetFormulaBase::localDispatch(bv, action, arg);
349 bool InsetFormula::display() const
351 return hullType() != "simple" && hullType() != "none";
355 MathHullInset const * InsetFormula::hull() const
357 lyx::Assert(par_->asHullInset());
358 return par_->asHullInset();
362 MathHullInset * InsetFormula::hull()
364 lyx::Assert(par_->asHullInset());
365 return par_->asHullInset();
369 Inset::Code InsetFormula::lyxCode() const
371 return Inset::MATH_CODE;
375 void InsetFormula::validate(LaTeXFeatures & features) const
377 par_->validate(features);
381 bool InsetFormula::insetAllowed(Inset::Code code) const
384 (code == Inset::LABEL_CODE && display())
385 || code == Inset::REF_CODE
386 || code == Inset::ERT_CODE;
390 int InsetFormula::ascent(BufferView *, LyXFont const &) const
392 const int a = par_->ascent();
395 return a + 1 - (par_->height() - loader_->image()->getHeight()) / 2;
399 int InsetFormula::descent(BufferView *, LyXFont const &) const
401 const int d = par_->descent();
404 return d + 1 - (par_->height() - loader_->image()->getHeight()) / 2;
408 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
411 return canPreview() ? loader_->image()->getWidth() : par_->width();
415 string InsetFormula::hullType() const
417 return hull() ? hull()->getType() : "none";
421 void InsetFormula::mutate(string const & type )
424 hull()->mutate(type);
432 bool InsetFormula::canPreview() const
434 return lyxrc.preview && loader_ && !par_->asNestInset()->editing()
435 && loader_->status() == grfx::Ready;
439 void InsetFormula::statusChanged()
441 lyxerr << "### InsetFormula::statusChanged called!, status: "
442 << loader_->status() << "\n";
443 if (loader_->status() == grfx::Ready)
444 view()->updateInset(this, false);
445 else if (loader_->status() == grfx::WaitingToLoad)
446 loader_->startLoading();
450 void InsetFormula::updatePreview()
452 // nothing to be done if no preview requested
453 lyxerr << "### updatePreview() called\n";
459 WriteStream wi(ls, false, false);
461 string const data = ls.str();
463 // the preview cache, maps contents to image loaders
464 typedef std::map<string, boost::shared_ptr<grfx::Loader> > cache_type;
465 static cache_type theCache;
466 static int theCounter = 0;
468 // set our loader corresponding to our current data
469 cache_type::const_iterator it = theCache.find(data);
472 if (it != theCache.end()) {
473 // we have already a loader, connect to it anyway
474 //lyxerr << "### updatePreview(), old loader: " << loader_ << "\n";
475 loader_ = it->second.get();
476 loader_->statusChanged.connect
477 (boost::bind(&InsetFormula::statusChanged, this));
481 // construct new file name
482 static string const dir = OnlyPath(lyx::tempName());
484 os << dir << theCounter++ << ".lyxpreview";
485 string file = os.str();
487 // the real work starts
488 //lyxerr << "### updatePreview(), new file " << file << "\n";
489 std::ofstream of(file.c_str());
491 << "\\documentclass{article}"
492 << "\\usepackage{amssymb}"
493 << "\\thispagestyle{empty}"
495 << "\\begin{document}"
497 << "\\end{document}\n";
500 // now we are done, start actual loading we will get called back via
501 // InsetFormula::statusChanged() if this is finished
502 theCache[data].reset(new grfx::Loader(file));
503 //lyxerr << "### updatePreview(), new loader: " << loader_ << "\n";
504 loader_ = theCache.find(data)->second.get();
505 loader_->startLoading();
506 loader_->statusChanged.connect(boost::bind(&InsetFormula::statusChanged, this));