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);
157 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
162 TextPainter tpain(par()->width(), par()->height());
163 par()->drawT(tpain, 0, par()->ascent());
165 // reset metrics cache to "real" values
167 return tpain.textheight();
169 WriteStream wi(os, false, true);
170 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
176 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
178 return docbook(buf, os, false);
182 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
185 ms << MTag("equation");
188 int res = ascii(buf, ms.os(), 0);
192 ms << par_.nucleus();
194 ms << ETag("equation");
195 return ms.line() + res;
199 void InsetFormula::read(Buffer const *, LyXLex & lex)
201 mathed_parse_normal(par_, lex);
207 //ostream & operator<<(ostream & os, LyXCursor const & c)
209 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
214 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
215 int y, float & xx, bool) const
217 int const x = int(xx);
218 int const w = width(bv, font);
219 int const d = descent(bv, font);
220 int const a = ascent(bv, font);
223 MathPainterInfo pi(bv->painter());
225 if (preview_->usePreview()) {
226 pi.pain.image(x + 1, y - a + 1, w - 2, h - 2,
227 *(preview_->pimage_->image()));
229 //pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
230 pi.base.style = LM_ST_TEXT;
232 pi.base.font.setColor(LColor::math);
233 if (lcolor.getX11Name(LColor::mathbg)
234 != lcolor.getX11Name(LColor::background))
235 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
238 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
240 mathcursor->drawSelection(pi);
241 //pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
244 par_->draw(pi, x + 1, y);
251 setCursorVisible(false);
255 vector<string> const InsetFormula::getLabelList() const
257 return par()->getLabelList();
261 UpdatableInset::RESULT
262 InsetFormula::localDispatch(BufferView * bv, kb_action action,
265 RESULT result = DISPATCHED;
270 bv->lockedInsetStoreUndo(Undo::INSERT);
271 mathcursor->breakLine();
272 mathcursor->normalize();
273 updateLocal(bv, true);
276 case LFUN_MATH_NUMBER:
278 //lyxerr << "toggling all numbers\n";
280 bv->lockedInsetStoreUndo(Undo::INSERT);
281 bool old = par()->numberedType();
282 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
283 hull()->numbered(row, !old);
284 bv->owner()->message(old ? _("No number") : _("Number"));
285 updateLocal(bv, true);
290 case LFUN_MATH_NONUMBER:
292 //lyxerr << "toggling line number\n";
294 bv->lockedInsetStoreUndo(Undo::INSERT);
295 MathCursor::row_type row = mathcursor->hullRow();
296 bool old = hull()->numbered(row);
297 bv->owner()->message(old ? _("No number") : _("Number"));
298 hull()->numbered(row, !old);
299 updateLocal(bv, true);
304 case LFUN_INSERT_LABEL:
306 bv->lockedInsetStoreUndo(Undo::INSERT);
308 MathCursor::row_type row = mathcursor->hullRow();
309 string old_label = hull()->label(row);
310 string new_label = arg;
312 if (new_label.empty()) {
313 string const default_label =
314 (lyxrc.label_init_length >= 0) ? "eq:" : "";
315 pair<bool, string> const res = old_label.empty()
316 ? Alert::askForText(_("Enter new label to insert:"), default_label)
317 : Alert::askForText(_("Enter label:"), old_label);
320 new_label = frontStrip(strip(res.second));
323 //if (new_label == old_label)
324 // break; // Nothing to do
326 if (!new_label.empty()) {
327 lyxerr << "setting label to '" << new_label << "'\n";
328 hull()->numbered(row, true);
331 #warning FIXME: please check you really mean repaint() ... is it needed,
332 #warning and if so, should it be update() instead ?
333 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
336 hull()->label(row, new_label);
338 updateLocal(bv, true);
342 case LFUN_MATH_MUTATE:
344 bv->lockedInsetStoreUndo(Undo::EDIT);
347 mathcursor->getPos(x, y);
349 mathcursor->setPos(x, y);
350 mathcursor->normalize();
351 updateLocal(bv, true);
355 case LFUN_MATH_EXTERN:
357 bv->lockedInsetStoreUndo(Undo::EDIT);
359 mathcursor->handleExtern(arg);
360 // re-compute inset dimension
362 updateLocal(bv, true);
366 case LFUN_MATH_DISPLAY:
370 mathcursor->getPos(x, y);
371 if (hullType() == "simple")
375 mathcursor->setPos(x, y);
376 mathcursor->normalize();
377 updateLocal(bv, true);
381 case LFUN_PASTESELECTION:
383 string const clip = bv->getClipboard();
385 mathed_parse_normal(par_, clip);
390 result = InsetFormulaBase::localDispatch(bv, action, arg);
399 bool InsetFormula::display() const
401 return hullType() != "simple" && hullType() != "none";
405 MathHullInset const * InsetFormula::hull() const
407 lyx::Assert(par_->asHullInset());
408 return par_->asHullInset();
412 MathHullInset * InsetFormula::hull()
414 lyx::Assert(par_->asHullInset());
415 return par_->asHullInset();
419 Inset::Code InsetFormula::lyxCode() const
421 return Inset::MATH_CODE;
425 void InsetFormula::validate(LaTeXFeatures & features) const
427 par_->validate(features);
431 bool InsetFormula::insetAllowed(Inset::Code code) const
434 (code == Inset::LABEL_CODE && display())
435 || code == Inset::REF_CODE
436 || code == Inset::ERT_CODE;
440 int InsetFormula::ascent(BufferView *, LyXFont const &) const
442 return preview_->usePreview() ?
443 1 + preview_->pimage_->ascent() : 1 + par_->ascent();
447 int InsetFormula::descent(BufferView *, LyXFont const &) const
449 return preview_->usePreview() ?
450 1 + preview_->pimage_->descent() : 1 + par_->descent();
454 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
457 return preview_->usePreview() ?
458 preview_->pimage_->width() : par_->width();
462 string InsetFormula::hullType() const
464 return par()->getType();
468 void InsetFormula::mutate(string const & type)
478 void InsetFormula::generatePreview(grfx::PreviewLoader & ploader) const
480 // Do nothing if no preview is desired.
481 if (!grfx::Previews::activated())
484 preview_->generatePreview(ploader);
488 void InsetFormula::PreviewImpl::generatePreview(grfx::PreviewLoader & ploader)
490 // Generate the LaTeX snippet.
491 string const snippet = latexString();
493 pimage_ = ploader.preview(snippet);
497 // If this is the first time of calling, connect to the
498 // grfx::PreviewLoader signal that'll inform us when the preview image
499 // is ready for loading.
500 if (!connection_.connected()) {
501 connection_ = ploader.imageReady.connect(
502 boost::bind(&PreviewImpl::previewReady, this, _1));
505 ploader.add(snippet);
509 bool InsetFormula::PreviewImpl::usePreview() const
511 BufferView * view = parent_.view();
513 if (!grfx::Previews::activated() ||
514 parent_.par_->asNestInset()->editing() ||
515 !view || !view->buffer())
518 // If the cached grfx::PreviewImage is invalid, update it.
519 string const snippet = latexString();
520 if (!pimage_ || snippet != pimage_->snippet()) {
521 grfx::PreviewLoader & ploader =
522 grfx::Previews::get().loader(view->buffer());
523 pimage_ = ploader.preview(snippet);
529 return pimage_->image();
533 string const InsetFormula::PreviewImpl::latexString() const
536 WriteStream wi(ls, false, false);
537 parent_.par_->write(wi);
538 return ls.str().c_str();
542 void InsetFormula::PreviewImpl::previewReady(grfx::PreviewImage const & pimage)
544 // Check snippet against the Inset's current contents
545 if (latexString() != pimage.snippet())
549 BufferView * view = parent_.view();
551 view->updateInset(&parent_, false);