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 // This initiates the loading of the preview, so should come
218 // before the metrics are computed.
219 bool const use_preview = preview_->usePreview();
221 int const x = int(xx);
222 int const w = width(bv, font);
223 int const d = descent(bv, font);
224 int const a = ascent(bv, font);
227 MathPainterInfo pi(bv->painter());
230 pi.pain.image(x, y - a, w, h,
231 *(preview_->pimage_->image(*this, *bv)));
233 //pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
234 pi.base.style = LM_ST_TEXT;
236 pi.base.font.setColor(LColor::math);
237 if (lcolor.getX11Name(LColor::mathbg)
238 != lcolor.getX11Name(LColor::background))
239 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
242 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
244 mathcursor->drawSelection(pi);
245 //pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
248 par_->draw(pi, x + 1, y);
255 setCursorVisible(false);
259 vector<string> const InsetFormula::getLabelList() const
261 return par()->getLabelList();
265 UpdatableInset::RESULT
266 InsetFormula::localDispatch(BufferView * bv, kb_action action,
269 RESULT result = DISPATCHED;
274 bv->lockedInsetStoreUndo(Undo::INSERT);
275 mathcursor->breakLine();
276 mathcursor->normalize();
277 updateLocal(bv, true);
280 case LFUN_MATH_NUMBER:
284 //lyxerr << "toggling all numbers\n";
286 bv->lockedInsetStoreUndo(Undo::INSERT);
287 bool old = par()->numberedType();
288 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
289 hull()->numbered(row, !old);
290 bv->owner()->message(old ? _("No number") : _("Number"));
291 updateLocal(bv, true);
296 case LFUN_MATH_NONUMBER:
298 //lyxerr << "toggling line number\n";
300 bv->lockedInsetStoreUndo(Undo::INSERT);
301 MathCursor::row_type row = mathcursor->hullRow();
302 bool old = hull()->numbered(row);
303 bv->owner()->message(old ? _("No number") : _("Number"));
304 hull()->numbered(row, !old);
305 updateLocal(bv, true);
310 case LFUN_INSERT_LABEL:
315 bv->lockedInsetStoreUndo(Undo::INSERT);
317 MathCursor::row_type row = mathcursor->hullRow();
318 string old_label = hull()->label(row);
319 string new_label = arg;
321 if (new_label.empty()) {
322 string const default_label =
323 (lyxrc.label_init_length >= 0) ? "eq:" : "";
324 pair<bool, string> const res = old_label.empty()
325 ? Alert::askForText(_("Enter new label to insert:"), default_label)
326 : Alert::askForText(_("Enter label:"), old_label);
329 new_label = frontStrip(strip(res.second));
332 //if (new_label == old_label)
333 // break; // Nothing to do
335 if (!new_label.empty()) {
336 lyxerr << "setting label to '" << new_label << "'\n";
337 hull()->numbered(row, true);
340 #warning FIXME: please check you really mean repaint() ... is it needed,
341 #warning and if so, should it be update() instead ?
342 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
345 hull()->label(row, new_label);
347 updateLocal(bv, true);
351 case LFUN_MATH_MUTATE:
353 bv->lockedInsetStoreUndo(Undo::EDIT);
356 mathcursor->getPos(x, y);
358 mathcursor->setPos(x, y);
359 mathcursor->normalize();
360 updateLocal(bv, true);
364 case LFUN_MATH_EXTERN:
366 bv->lockedInsetStoreUndo(Undo::EDIT);
368 mathcursor->handleExtern(arg);
369 // re-compute inset dimension
371 updateLocal(bv, true);
375 case LFUN_MATH_DISPLAY:
379 mathcursor->getPos(x, y);
380 if (hullType() == "simple")
384 mathcursor->setPos(x, y);
385 mathcursor->normalize();
386 updateLocal(bv, true);
390 case LFUN_PASTESELECTION:
392 string const clip = bv->getClipboard();
394 mathed_parse_normal(par_, clip);
399 result = InsetFormulaBase::localDispatch(bv, action, arg);
408 bool InsetFormula::display() const
410 return hullType() != "simple" && hullType() != "none";
414 MathHullInset * InsetFormula::hull() const
416 lyx::Assert(par_->asHullInset());
417 return par_->asHullInset();
421 Inset::Code InsetFormula::lyxCode() const
423 return Inset::MATH_CODE;
427 void InsetFormula::validate(LaTeXFeatures & features) const
429 par_->validate(features);
433 bool InsetFormula::insetAllowed(Inset::Code code) const
436 (code == Inset::LABEL_CODE && display())
437 || code == Inset::REF_CODE
438 || code == Inset::ERT_CODE;
442 int InsetFormula::ascent(BufferView *, LyXFont const &) const
444 return preview_->usePreview() ?
445 preview_->pimage_->ascent() : 1 + par_->ascent();
449 int InsetFormula::descent(BufferView *, LyXFont const &) const
451 if (!preview_->usePreview())
452 return 1 + par_->descent();
454 int const descent = preview_->pimage_->descent();
455 return display() ? descent + 12 : descent;
459 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
462 return preview_->usePreview() ?
463 preview_->pimage_->width() : par_->width();
467 string InsetFormula::hullType() const
469 return par()->getType();
473 void InsetFormula::mutate(string const & type)
483 void InsetFormula::generatePreview(grfx::PreviewLoader & ploader) const
485 // Do nothing if no preview is desired.
486 if (!grfx::Previews::activated())
489 preview_->generatePreview(ploader);
493 void InsetFormula::PreviewImpl::generatePreview(grfx::PreviewLoader & ploader)
495 // Generate the LaTeX snippet.
496 string const snippet = latexString();
498 pimage_ = ploader.preview(snippet);
502 // If this is the first time of calling, connect to the
503 // grfx::PreviewLoader signal that'll inform us when the preview image
504 // is ready for loading.
505 if (!connection_.connected()) {
506 connection_ = ploader.connect(
507 boost::bind(&PreviewImpl::previewReady, this, _1));
510 ploader.add(snippet);
514 bool InsetFormula::PreviewImpl::usePreview() const
516 BufferView * view = parent_.view();
518 if (!grfx::Previews::activated() ||
519 parent_.par_->asNestInset()->editing() ||
520 !view || !view->buffer())
523 // If the cached grfx::PreviewImage is invalid, update it.
524 string const snippet = latexString();
525 if (!pimage_ || snippet != pimage_->snippet()) {
526 grfx::PreviewLoader & ploader =
527 grfx::Previews::get().loader(view->buffer());
528 pimage_ = ploader.preview(snippet);
534 return pimage_->image(parent_, *view);
538 string const InsetFormula::PreviewImpl::latexString() const
541 WriteStream wi(ls, false, false);
542 parent_.par_->write(wi);
543 return ls.str().c_str();
547 void InsetFormula::PreviewImpl::previewReady(grfx::PreviewImage const & pimage)
549 // Check snippet against the Inset's current contents
550 if (latexString() != pimage.snippet())
554 BufferView * view = parent_.view();
556 view->updateInset(&parent_, false);