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"
50 #include <boost/bind.hpp>
51 #include <boost/utility.hpp>
63 InsetFormula::InsetFormula()
64 : par_(MathAtom(new MathHullInset))
70 InsetFormula::InsetFormula(InsetFormula const & f)
71 : InsetFormulaBase(f), par_(f.par_), loader_(f.loader_.filename())
77 InsetFormula::InsetFormula(MathInsetTypes t)
78 : par_(MathAtom(new MathHullInset(t)))
84 InsetFormula::InsetFormula(string const & s)
87 bool res = mathed_parse_normal(par_, s);
90 res = mathed_parse_normal(par_, "$" + s + "$");
93 lyxerr << "cannot interpret '" << s << "' as math\n";
94 par_ = MathAtom(new MathHullInset(LM_OT_SIMPLE));
102 Inset * InsetFormula::clone(Buffer const &, bool) const
104 return new InsetFormula(*this);
108 void InsetFormula::write(Buffer const *, ostream & os) const
111 WriteStream wi(os, false, false);
116 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
118 WriteStream wi(os, fragile, true);
124 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
129 TextPainter tpain(par()->width(), par()->height());
130 par()->drawT(tpain, 0, par()->ascent());
132 // reset metrics cache to "real" values
134 return tpain.textheight();
136 WriteStream wi(os, false, true);
143 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
145 return docbook(buf, os, false);
149 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
152 ms << MTag("equation");
155 int res = ascii(buf, ms.os(), 0);
159 ms << par_.nucleus();
161 ms << ETag("equation");
162 return ms.line() + res;
166 void InsetFormula::read(Buffer const *, LyXLex & lex)
168 mathed_parse_normal(par_, lex);
173 //ostream & operator<<(ostream & os, LyXCursor const & c)
175 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
180 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
181 int y, float & xx, bool) const
183 int const x = int(xx);
184 int const w = width(bv, font);
185 int const d = descent(bv, font);
186 int const a = ascent(bv, font);
189 MathPainterInfo pi(bv->painter());
192 pi.pain.image(x + 1, y - a + 1, w - 2, h - 2, *(loader_.image()));
194 pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
196 pi.base.font.setColor(LColor::math);
197 if (lcolor.getX11Name(LColor::mathbg)
198 != lcolor.getX11Name(LColor::background))
199 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
202 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
204 mathcursor->drawSelection(pi);
205 pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
208 par_->draw(pi, x, y);
215 setCursorVisible(false);
219 vector<string> const InsetFormula::getLabelList() const
221 return hull()->getLabelList();
225 UpdatableInset::RESULT
226 InsetFormula::localDispatch(BufferView * bv, kb_action action,
229 RESULT result = DISPATCHED;
234 bv->lockedInsetStoreUndo(Undo::INSERT);
235 mathcursor->breakLine();
236 mathcursor->normalize();
237 updateLocal(bv, true);
240 case LFUN_MATH_NUMBER:
242 //lyxerr << "toggling all numbers\n";
244 bv->lockedInsetStoreUndo(Undo::INSERT);
245 bool old = hull()->numberedType();
246 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
247 hull()->numbered(row, !old);
248 bv->owner()->message(old ? _("No number") : _("Number"));
249 updateLocal(bv, true);
254 case LFUN_MATH_NONUMBER:
256 //lyxerr << "toggling line number\n";
258 bv->lockedInsetStoreUndo(Undo::INSERT);
259 MathCursor::row_type row = mathcursor->hullRow();
260 bool old = hull()->numbered(row);
261 bv->owner()->message(old ? _("No number") : _("Number"));
262 hull()->numbered(row, !old);
263 updateLocal(bv, true);
268 case LFUN_INSERT_LABEL:
270 bv->lockedInsetStoreUndo(Undo::INSERT);
272 MathCursor::row_type row = mathcursor->hullRow();
273 string old_label = hull()->label(row);
274 string new_label = arg;
276 if (new_label.empty()) {
277 string const default_label =
278 (lyxrc.label_init_length >= 0) ? "eq:" : "";
279 pair<bool, string> const res = old_label.empty()
280 ? Alert::askForText(_("Enter new label to insert:"), default_label)
281 : Alert::askForText(_("Enter label:"), old_label);
284 new_label = frontStrip(strip(res.second));
287 //if (new_label == old_label)
288 // break; // Nothing to do
290 if (!new_label.empty()) {
291 lyxerr << "setting label to '" << new_label << "'\n";
292 hull()->numbered(row, true);
295 #warning FIXME: please check you really mean repaint() ... is it needed,
296 #warning and if so, should it be update() instead ?
297 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
300 hull()->label(row, new_label);
302 updateLocal(bv, true);
306 case LFUN_MATH_MUTATE:
308 bv->lockedInsetStoreUndo(Undo::EDIT);
311 mathcursor->getPos(x, y);
313 mathcursor->setPos(x, y);
314 mathcursor->normalize();
315 updateLocal(bv, true);
319 case LFUN_MATH_EXTERN:
321 bv->lockedInsetStoreUndo(Undo::EDIT);
323 mathcursor->handleExtern(arg);
324 // re-compute inset dimension
326 updateLocal(bv, true);
330 case LFUN_MATH_DISPLAY:
334 mathcursor->getPos(x, y);
335 if (getType() == LM_OT_SIMPLE)
336 hull()->mutate(LM_OT_EQUATION);
338 hull()->mutate(LM_OT_SIMPLE);
339 mathcursor->setPos(x, y);
340 mathcursor->normalize();
341 updateLocal(bv, true);
345 case LFUN_PASTESELECTION:
347 string const clip = bv->getClipboard();
349 mathed_parse_normal(par_, clip);
354 result = InsetFormulaBase::localDispatch(bv, action, arg);
361 bool InsetFormula::display() const
363 return getType() != LM_OT_SIMPLE;
367 MathHullInset const * InsetFormula::hull() const
369 lyx::Assert(par_->asHullInset());
370 return par_->asHullInset();
374 MathHullInset * InsetFormula::hull()
376 lyx::Assert(par_->asHullInset());
377 return par_->asHullInset();
381 Inset::Code InsetFormula::lyxCode() const
383 return Inset::MATH_CODE;
387 void InsetFormula::validate(LaTeXFeatures & features) const
389 par_->validate(features);
393 bool InsetFormula::insetAllowed(Inset::Code code) const
396 (code == Inset::LABEL_CODE && display())
397 || code == Inset::REF_CODE
398 || code == Inset::ERT_CODE;
402 int InsetFormula::ascent(BufferView *, LyXFont const &) const
404 const int a = par_->ascent();
407 return a + 1 - (par_->height() - loader_.image()->getHeight()) / 2;
411 int InsetFormula::descent(BufferView *, LyXFont const &) const
413 const int d = par_->descent();
416 return d + 1 - (par_->height() - loader_.image()->getHeight()) / 2;
420 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
423 return canPreview() ? loader_.image()->getWidth() : par_->width();
427 MathInsetTypes InsetFormula::getType() const
429 return hull()->getType();
437 bool InsetFormula::canPreview() const
439 return lyxrc.preview && !par_->asNestInset()->editing()
440 && loader_.status() == grfx::Ready;
444 void InsetFormula::statusChanged()
446 //lyxerr << "### InsetFormula::statusChanged called!, status: "
447 // << loader_.status() << "\n";
448 if (loader_.status() == grfx::Ready)
449 view()->updateInset(this, false);
450 else if (loader_.status() == grfx::WaitingToLoad)
451 loader_.startLoading();
455 void InsetFormula::init()
458 loader_.statusChanged.connect
459 (boost::bind(&InsetFormula::statusChanged, this));
463 void InsetFormula::updatePreview() const
465 // nothing to be done if no preview requested
468 //lyxerr << "### updatePreview() called\n";
472 WriteStream wi(ls, false, false);
474 string const data = ls.str();
476 // built some unique filename
479 for (string::const_iterator it = data.begin(); it != data.end(); ++it)
480 os << char('A' + (*it & 15)) << char('a' + (*it >> 4));
481 string base = os.str();
482 string dir = OnlyPath(lyx::tempName());
483 string file = dir + base + ".lyxpreview";
485 // everything is fine already
486 if (loader_.filename() == file)
489 // the real work starts
490 std::ofstream of(file.c_str());
492 << "\\documentclass{article}"
493 << "\\usepackage{amssymb}"
494 << "\\thispagestyle{empty}"
496 << "\\begin{document}"
498 << "\\end{document}\n";
501 // now we are done, start actual loading we will get called back via
502 // InsetFormula::statusChanged() if this is finished
503 loader_.reset(file); }