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"
29 #include "math_hullinset.h"
30 #include "math_support.h"
31 #include "math_mathmlstream.h"
32 #include "textpainter.h"
35 #include "BufferView.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/GraphicsImage.h"
51 #include "graphics/PreviewLoader.h"
52 #include "graphics/PreviewImage.h"
53 #include "graphics/Previews.h"
56 #include <boost/bind.hpp>
57 #include <boost/signals/trackable.hpp>
58 #include <boost/signals/connection.hpp>
59 #include <boost/utility.hpp>
70 struct InsetFormula::PreviewImpl : public boost::signals::trackable {
72 PreviewImpl(InsetFormula & p) : parent_(p), pimage_(0) {}
75 void generatePreview(grfx::PreviewLoader & previewer);
77 /** This method is connected to the grfx::PreviewLoader::imageReady
80 void previewReady(grfx::PreviewImage const &);
83 string const latexString() const;
86 bool usePreview() const;
89 InsetFormula & parent_;
91 mutable grfx::PreviewImage const * pimage_;
93 boost::signals::connection connection_;
98 InsetFormula::InsetFormula()
99 : par_(MathAtom(new MathHullInset)),
100 preview_(new PreviewImpl(*this))
104 InsetFormula::InsetFormula(InsetFormula const & other)
105 : InsetFormulaBase(other),
107 preview_(new PreviewImpl(*this))
111 InsetFormula::InsetFormula(BufferView * bv)
112 : par_(MathAtom(new MathHullInset)),
113 preview_(new PreviewImpl(*this))
119 InsetFormula::InsetFormula(string const & data)
120 : par_(MathAtom(new MathHullInset)),
121 preview_(new PreviewImpl(*this))
125 if (!mathed_parse_normal(par_, data))
126 lyxerr << "cannot interpret '" << data << "' as math\n";
131 InsetFormula::~InsetFormula()
135 Inset * InsetFormula::clone(Buffer const &, bool) const
137 return new InsetFormula(*this);
141 void InsetFormula::write(Buffer const *, ostream & os) const
143 WriteStream wi(os, false, false);
144 os << par_->fileInsetLabel() << " ";
149 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
151 WriteStream wi(os, fragile, true);
158 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
163 TextPainter tpain(par()->width(), par()->height());
164 par()->drawT(tpain, 0, par()->ascent());
166 // reset metrics cache to "real" values
168 return tpain.textheight();
170 WriteStream wi(os, false, true);
177 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
179 return docbook(buf, os, false);
183 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
186 ms << MTag("equation");
189 int res = ascii(buf, ms.os(), 0);
193 ms << par_.nucleus();
195 ms << ETag("equation");
196 return ms.line() + res;
200 void InsetFormula::read(Buffer const *, LyXLex & lex)
202 mathed_parse_normal(par_, lex);
208 //ostream & operator<<(ostream & os, LyXCursor const & c)
210 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
215 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
216 int y, float & xx, bool) const
218 int const x = int(xx);
219 int const w = width(bv, font);
220 int const d = descent(bv, font);
221 int const a = ascent(bv, font);
224 MathPainterInfo pi(bv->painter());
226 if (preview_->usePreview()) {
227 pi.pain.image(x + 1, y - a + 1, w - 2, h - 2,
228 *(preview_->pimage_->image()));
230 //pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
231 pi.base.style = LM_ST_TEXT;
233 pi.base.font.setColor(LColor::math);
234 if (lcolor.getX11Name(LColor::mathbg)
235 != lcolor.getX11Name(LColor::background))
236 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
239 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
241 mathcursor->drawSelection(pi);
242 //pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
245 par_->draw(pi, x + 1, y);
252 setCursorVisible(false);
256 vector<string> const InsetFormula::getLabelList() const
258 return par()->getLabelList();
262 UpdatableInset::RESULT
263 InsetFormula::localDispatch(BufferView * bv, kb_action action,
266 RESULT result = DISPATCHED;
271 bv->lockedInsetStoreUndo(Undo::INSERT);
272 mathcursor->breakLine();
273 mathcursor->normalize();
274 updateLocal(bv, true);
277 case LFUN_MATH_NUMBER:
279 //lyxerr << "toggling all numbers\n";
281 bv->lockedInsetStoreUndo(Undo::INSERT);
282 bool old = par()->numberedType();
283 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
284 hull()->numbered(row, !old);
285 bv->owner()->message(old ? _("No number") : _("Number"));
286 updateLocal(bv, true);
291 case LFUN_MATH_NONUMBER:
293 //lyxerr << "toggling line number\n";
295 bv->lockedInsetStoreUndo(Undo::INSERT);
296 MathCursor::row_type row = mathcursor->hullRow();
297 bool old = hull()->numbered(row);
298 bv->owner()->message(old ? _("No number") : _("Number"));
299 hull()->numbered(row, !old);
300 updateLocal(bv, true);
305 case LFUN_INSERT_LABEL:
307 bv->lockedInsetStoreUndo(Undo::INSERT);
309 MathCursor::row_type row = mathcursor->hullRow();
310 string old_label = hull()->label(row);
311 string new_label = arg;
313 if (new_label.empty()) {
314 string const default_label =
315 (lyxrc.label_init_length >= 0) ? "eq:" : "";
316 pair<bool, string> const res = old_label.empty()
317 ? Alert::askForText(_("Enter new label to insert:"), default_label)
318 : Alert::askForText(_("Enter label:"), old_label);
321 new_label = frontStrip(strip(res.second));
324 //if (new_label == old_label)
325 // break; // Nothing to do
327 if (!new_label.empty()) {
328 lyxerr << "setting label to '" << new_label << "'\n";
329 hull()->numbered(row, true);
332 #warning FIXME: please check you really mean repaint() ... is it needed,
333 #warning and if so, should it be update() instead ?
334 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
337 hull()->label(row, new_label);
339 updateLocal(bv, true);
343 case LFUN_MATH_MUTATE:
345 bv->lockedInsetStoreUndo(Undo::EDIT);
348 mathcursor->getPos(x, y);
350 mathcursor->setPos(x, y);
351 mathcursor->normalize();
352 updateLocal(bv, true);
356 case LFUN_MATH_EXTERN:
358 bv->lockedInsetStoreUndo(Undo::EDIT);
360 mathcursor->handleExtern(arg);
361 // re-compute inset dimension
363 updateLocal(bv, true);
367 case LFUN_MATH_DISPLAY:
371 mathcursor->getPos(x, y);
372 if (hullType() == "simple")
376 mathcursor->setPos(x, y);
377 mathcursor->normalize();
378 updateLocal(bv, true);
382 case LFUN_PASTESELECTION:
384 string const clip = bv->getClipboard();
386 mathed_parse_normal(par_, clip);
391 result = InsetFormulaBase::localDispatch(bv, action, arg);
400 bool InsetFormula::display() const
402 return hullType() != "simple" && hullType() != "none";
406 MathHullInset const * InsetFormula::hull() const
408 lyx::Assert(par_->asHullInset());
409 return par_->asHullInset();
413 MathHullInset * InsetFormula::hull()
415 lyx::Assert(par_->asHullInset());
416 return par_->asHullInset();
420 Inset::Code InsetFormula::lyxCode() const
422 return Inset::MATH_CODE;
426 void InsetFormula::validate(LaTeXFeatures & features) const
428 par_->validate(features);
432 bool InsetFormula::insetAllowed(Inset::Code code) const
435 (code == Inset::LABEL_CODE && display())
436 || code == Inset::REF_CODE
437 || code == Inset::ERT_CODE;
441 int InsetFormula::ascent(BufferView *, LyXFont const &) const
443 return preview_->usePreview() ?
444 1 + preview_->pimage_->ascent() : 1 + par_->ascent();
448 int InsetFormula::descent(BufferView *, LyXFont const &) const
450 return preview_->usePreview() ?
451 1 + preview_->pimage_->descent() : 1 + par_->descent();
455 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
458 return preview_->usePreview() ?
459 preview_->pimage_->width() : par_->width();
463 string InsetFormula::hullType() const
465 return par()->getType();
469 void InsetFormula::mutate(string const & type)
479 void InsetFormula::generatePreview(grfx::PreviewLoader & ploader) const
481 // Do nothing if no preview is desired.
482 if (!grfx::Previews::activated())
485 preview_->generatePreview(ploader);
489 void InsetFormula::PreviewImpl::generatePreview(grfx::PreviewLoader & ploader)
491 // Generate the LaTeX snippet.
492 string const snippet = latexString();
494 pimage_ = ploader.preview(snippet);
498 // If this is the first time of calling, connect to the
499 // grfx::PreviewLoader signal that'll inform us when the preview image
500 // is ready for loading.
501 if (!connection_.connected()) {
502 connection_ = ploader.imageReady.connect(
503 boost::bind(&PreviewImpl::previewReady, this, _1));
506 ploader.add(snippet);
510 bool InsetFormula::PreviewImpl::usePreview() const
512 BufferView * view = parent_.view();
514 if (!grfx::Previews::activated() ||
515 parent_.par_->asNestInset()->editing() ||
516 !view || !view->buffer())
519 // If the cached grfx::PreviewImage is invalid, update it.
520 string const snippet = latexString();
521 if (!pimage_ || snippet != pimage_->snippet()) {
522 grfx::PreviewLoader & ploader =
523 grfx::Previews::get().loader(view->buffer());
524 pimage_ = ploader.preview(snippet);
530 return pimage_->image();
534 string const InsetFormula::PreviewImpl::latexString() const
537 WriteStream wi(ls, false, false);
538 parent_.par_->write(wi);
539 return ls.str().c_str();
543 void InsetFormula::PreviewImpl::previewReady(grfx::PreviewImage const & pimage)
545 // Check snippet against the Inset's current contents
546 if (latexString() != pimage.snippet())
550 BufferView * view = parent_.view();
552 view->updateInset(&parent_, false);