+
+
+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));
+}
+