#include "BufferView.h"
#include "gettext.h"
#include "debug.h"
-#include "frontends/Alert.h"
#include "support/LOstream.h"
#include "support/LAssert.h"
-#include "support/filetools.h" // LibFileSearch
+#include "support/lyxlib.h"
+#include "support/systemcall.h"
+#include "support/filetools.h"
+#include "frontends/Alert.h"
#include "frontends/LyXView.h"
#include "frontends/Painter.h"
+#include "graphics/GraphicsImage.h"
#include "lyxrc.h"
#include "math_hullinset.h"
#include "math_support.h"
#include "math_mathmlstream.h"
#include "textpainter.h"
-#include "preview.h"
-#include "graphics/GraphicsCacheItem.h"
+#include <fstream>
+#include <boost/bind.hpp>
+#include <boost/utility.hpp>
using std::ostream;
using std::ifstream;
InsetFormula::InsetFormula()
- : par_(MathAtom(new MathHullInset))
-{}
-
-
-InsetFormula::InsetFormula(MathInsetTypes t)
- : par_(MathAtom(new MathHullInset(t)))
+ : par_(MathAtom(new MathHullInset)), loader_(0)
{}
-InsetFormula::InsetFormula(string const & s)
+InsetFormula::InsetFormula(BufferView * bv)
+ : par_(MathAtom(new MathHullInset)), loader_(0)
{
- if (s.size()) {
- bool res = mathed_parse_normal(par_, s);
+ view_ = bv;
+}
- if (!res)
- res = mathed_parse_normal(par_, "$" + s + "$");
- if (!res) {
- lyxerr << "cannot interpret '" << s << "' as math\n";
- par_ = MathAtom(new MathHullInset(LM_OT_SIMPLE));
- }
- }
- metrics();
+InsetFormula::InsetFormula(string const & data)
+ : par_(MathAtom(new MathHullInset)), loader_(0)
+{
+ if (!data.size())
+ return;
+ if (!mathed_parse_normal(par_, data))
+ lyxerr << "cannot interpret '" << data << "' as math\n";
}
+
Inset * InsetFormula::clone(Buffer const &, bool) const
{
return new InsetFormula(*this);
}
+
int InsetFormula::ascii(Buffer const *, ostream & os, int) const
{
#if 0
{
mathed_parse_normal(par_, lex);
metrics();
+ updatePreview();
}
void InsetFormula::draw(BufferView * bv, LyXFont const & font,
int y, float & xx, bool) const
{
- metrics(bv, font);
-
int const x = int(xx);
- int const w = par_->width();
- int const h = par_->height();
- int const a = par_->ascent();
+ int const w = width(bv, font);
+ int const d = descent(bv, font);
+ int const a = ascent(bv, font);
+ int const h = a + d;
MathPainterInfo pi(bv->painter());
- pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
- pi.base.font = font;
- pi.base.font.setColor(LColor::math);
-
- if (lcolor.getX11Name(LColor::mathbg)!=lcolor.getX11Name(LColor::background))
- pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
-
- if (mathcursor &&
- const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
- {
- mathcursor->drawSelection(pi);
- pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
- }
- par_->draw(pi, x, y);
-
- // preview stuff
- if (lyxrc.preview) {
- ostringstream os;
- WriteStream wi(os, false, false);
- par_->write(wi);
- if (preview(os.str(), preview_)) {
- cerr << "image could be drawn\n";
- pi.pain.image(x + w + 2, y - a + 1, w - 2, h - 2, *(preview_->image()));
- } else {
- pi.pain.fillRectangle(x + w, y - a, w, h, LColor::white);
+ if (canPreview()) {
+ pi.pain.image(x + 1, y - a + 1, w - 2, h - 2, *(loader_->image()));
+ } else {
+ //pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
+ pi.base.style = LM_ST_TEXT;
+ pi.base.font = font;
+ pi.base.font.setColor(LColor::math);
+ if (lcolor.getX11Name(LColor::mathbg)
+ != lcolor.getX11Name(LColor::background))
+ pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
+
+ if (mathcursor &&
+ const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
+ {
+ mathcursor->drawSelection(pi);
+ pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
}
- pi.pain.rectangle(x + w, y - a, w, h, LColor::mathframe);
- xx += w;
+
+ par_->draw(pi, x, y);
}
xx += w;
int x;
int y;
mathcursor->getPos(x, y);
- hull()->mutate(arg);
+ mutate(arg);
mathcursor->setPos(x, y);
mathcursor->normalize();
updateLocal(bv, true);
int x = 0;
int y = 0;
mathcursor->getPos(x, y);
- if (getType() == LM_OT_SIMPLE)
- hull()->mutate(LM_OT_EQUATION);
+ if (hullType() == "simple")
+ mutate("equation");
else
- hull()->mutate(LM_OT_SIMPLE);
+ mutate("simple");
mathcursor->setPos(x, y);
mathcursor->normalize();
updateLocal(bv, true);
result = InsetFormulaBase::localDispatch(bv, action, arg);
}
+ //updatePreview();
+
return result;
}
bool InsetFormula::display() const
{
- return getType() != LM_OT_SIMPLE;
+ return hullType() != "simple" && hullType() != "none";
}
int InsetFormula::ascent(BufferView *, LyXFont const &) const
{
- return par_->ascent() + 1;
+ const int a = par_->ascent();
+ if (!canPreview())
+ return a + 1;
+ return a + 1 - (par_->height() - loader_->image()->getHeight()) / 2;
}
int InsetFormula::descent(BufferView *, LyXFont const &) const
{
- return par_->descent() + 1;
+ const int d = par_->descent();
+ if (!canPreview())
+ return d + 1;
+ return d + 1 - (par_->height() - loader_->image()->getHeight()) / 2;
}
int InsetFormula::width(BufferView * bv, LyXFont const & font) const
{
metrics(bv, font);
- int const w = par_->width();
- // double the space for the preview if needed
- return lyxrc.preview ? 2 * w : w;
+ return canPreview() ? loader_->image()->getWidth() : par_->width();
}
-MathInsetTypes InsetFormula::getType() const
+string InsetFormula::hullType() const
{
- return hull()->getType();
+ return hull() ? hull()->getType() : "none";
}
+
+
+void InsetFormula::mutate(string const & type )
+{
+ if (hull())
+ hull()->mutate(type);
+}
+
+
+//
+// preview stuff
+//
+
+bool InsetFormula::canPreview() const
+{
+ return lyxrc.preview && loader_ && !par_->asNestInset()->editing()
+ && loader_->status() == grfx::Ready;
+}
+
+
+void InsetFormula::statusChanged()
+{
+ lyxerr << "### InsetFormula::statusChanged called!, status: "
+ << loader_->status() << "\n";
+ if (loader_->status() == grfx::Ready)
+ view()->updateInset(this, false);
+ else if (loader_->status() == grfx::WaitingToLoad)
+ loader_->startLoading();
+}
+
+
+void InsetFormula::updatePreview()
+{
+ // nothing to be done if no preview requested
+ lyxerr << "### updatePreview() called\n";
+ if (!lyxrc.preview)
+ return;
+
+ // get LaTeX
+ ostringstream ls;
+ WriteStream wi(ls, false, false);
+ par_->write(wi);
+ string const data = ls.str();
+
+ // the preview cache, maps contents to image loaders
+ typedef std::map<string, boost::shared_ptr<grfx::Loader> > cache_type;
+ static cache_type theCache;
+ static int theCounter = 0;
+
+ // set our loader corresponding to our current data
+ cache_type::const_iterator it = theCache.find(data);
+
+ // is this old data?
+ if (it != theCache.end()) {
+ // we have already a loader, connect to it anyway
+ //lyxerr << "### updatePreview(), old loader: " << loader_ << "\n";
+ loader_ = it->second.get();
+ loader_->statusChanged.connect
+ (boost::bind(&InsetFormula::statusChanged, this));
+ return;
+ }
+
+ // construct new file name
+ static string const dir = OnlyPath(lyx::tempName());
+ ostringstream os;
+ os << dir << theCounter++ << ".lyxpreview";
+ string file = os.str();
+
+ // the real work starts
+ //lyxerr << "### updatePreview(), new file " << file << "\n";
+ std::ofstream of(file.c_str());
+ of << "\\batchmode"
+ << "\\documentclass{article}"
+ << "\\usepackage{amssymb}"
+ << "\\thispagestyle{empty}"
+ << "\\pdfoutput=0"
+ << "\\begin{document}"
+ << data
+ << "\\end{document}\n";
+ of.close();
+
+ // now we are done, start actual loading we will get called back via
+ // InsetFormula::statusChanged() if this is finished
+ theCache[data].reset(new grfx::Loader(file));
+ //lyxerr << "### updatePreview(), new loader: " << loader_ << "\n";
+ loader_ = theCache.find(data)->second.get();
+ loader_->startLoading();
+ loader_->statusChanged.connect(boost::bind(&InsetFormula::statusChanged, this));
+}
+