]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/formula.C
fix compilation error
[lyx.git] / src / mathed / formula.C
index f936639491046e2009d1fcdeff64c89360e43a6b..a6c0b602018d1942208792fcdc2a17c6e4868572 100644 (file)
@@ -44,7 +44,6 @@
 #include "math_support.h"
 #include "math_mathmlstream.h"
 #include "textpainter.h"
-#include "preview.h"
 
 #include <fstream>
 #include <boost/bind.hpp>
@@ -61,43 +60,27 @@ using std::getline;
 
 
 InsetFormula::InsetFormula()
-       : par_(MathAtom(new MathHullInset))
-{
-       init();
-}
+       : par_(MathAtom(new MathHullInset)), loader_(0)
+{}
 
 
-InsetFormula::InsetFormula(InsetFormula const & f)
-       : InsetFormulaBase(f), par_(f.par_), loader_(f.loader_.filename())
+InsetFormula::InsetFormula(BufferView * bv)
+       : par_(MathAtom(new MathHullInset)), loader_(0)
 {
-       init();
+       view_ = bv;
 }
 
 
-InsetFormula::InsetFormula(MathInsetTypes t)
-       : par_(MathAtom(new MathHullInset(t)))
+InsetFormula::InsetFormula(string const & data)
+       : par_(MathAtom(new MathHullInset)), loader_(0)
 {
-       init();
+       if (!data.size())
+               return;
+       if (!mathed_parse_normal(par_, data)) 
+               lyxerr << "cannot interpret '" << data << "' as math\n";
 }
 
 
-InsetFormula::InsetFormula(string const & s)
-{
-       if (s.size()) {
-               bool res = mathed_parse_normal(par_, s);
-
-               if (!res)
-                       res = mathed_parse_normal(par_, "$" + s + "$");
-
-               if (!res) {
-                       lyxerr << "cannot interpret '" << s << "' as math\n";
-                       par_ = MathAtom(new MathHullInset(LM_OT_SIMPLE));
-               }
-       }
-       init();
-       metrics();
-}
-
 
 Inset * InsetFormula::clone(Buffer const &, bool) const
 {
@@ -121,6 +104,7 @@ int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
 }
 
 
+
 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
 {
 #if 0
@@ -167,6 +151,7 @@ void InsetFormula::read(Buffer const *, LyXLex & lex)
 {
        mathed_parse_normal(par_, lex);
        metrics();
+       updatePreview();
 }
 
 
@@ -189,9 +174,10 @@ void InsetFormula::draw(BufferView * bv, LyXFont const & font,
        MathPainterInfo pi(bv->painter());
 
        if (canPreview()) {
-               pi.pain.image(x + 1, y - a + 1, w - 2, h - 2, *(loader_.image()));
+               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 = 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)
@@ -309,7 +295,7 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
                        int x;
                        int y;
                        mathcursor->getPos(x, y);
-                       hull()->mutate(arg);
+                       mutate(arg);
                        mathcursor->setPos(x, y);
                        mathcursor->normalize();
                        updateLocal(bv, true);
@@ -332,10 +318,10 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
                        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);
@@ -354,13 +340,15 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
                        result = InsetFormulaBase::localDispatch(bv, action, arg);
        }
 
+       //updatePreview();
+
        return result;
 }
 
 
 bool InsetFormula::display() const
 {
-       return getType() != LM_OT_SIMPLE;
+       return hullType() != "simple" && hullType() != "none";
 }
 
 
@@ -404,7 +392,7 @@ int InsetFormula::ascent(BufferView *, LyXFont const &) const
        const int a = par_->ascent();
        if (!canPreview())
                return a + 1;
-       return a + 1 - (par_->height() - loader_.image()->getHeight()) / 2;
+       return a + 1 - (par_->height() - loader_->image()->getHeight()) / 2;
 }
 
 
@@ -413,20 +401,27 @@ int InsetFormula::descent(BufferView *, LyXFont const &) const
        const int d = par_->descent();
        if (!canPreview())
                return d + 1;
-       return d + 1 - (par_->height() - loader_.image()->getHeight()) / 2;
+       return d + 1 - (par_->height() - loader_->image()->getHeight()) / 2;
 }
 
 
 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
 {
        metrics(bv, font);
-       return canPreview() ? loader_.image()->getWidth() : par_->width();
+       return canPreview() ? loader_->image()->getWidth() : par_->width();
+}
+
+
+string InsetFormula::hullType() const
+{
+       return hull() ? hull()->getType() : "none";
 }
 
 
-MathInsetTypes InsetFormula::getType() const
+void InsetFormula::mutate(string const & type )
 {
-       return hull()->getType();
+       if (hull())
+               hull()->mutate(type);
 }
 
 
@@ -436,36 +431,28 @@ MathInsetTypes InsetFormula::getType() const
 
 bool InsetFormula::canPreview() const
 {
-       return lyxrc.preview && !par_->asNestInset()->editing()
-               && loader_.status() == grfx::Ready;
+       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) 
+       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::init()
-{
-       if (lyxrc.preview)
-               loader_.statusChanged.connect
-                       (boost::bind(&InsetFormula::statusChanged, this));
+       else if (loader_->status() == grfx::WaitingToLoad)
+               loader_->startLoading();
 }
 
 
-void InsetFormula::updatePreview() const
+void InsetFormula::updatePreview()
 {
        // nothing to be done if no preview requested
+       lyxerr << "### updatePreview() called\n";
        if (!lyxrc.preview)
                return;
-       //lyxerr << "### updatePreview() called\n";
 
        // get LaTeX 
        ostringstream ls;
@@ -473,49 +460,49 @@ void InsetFormula::updatePreview() const
        par_->write(wi);
        string const data = ls.str();
 
-       // built some unique filename
-       ostringstream os;
-       os << "preview_";
-       for (string::const_iterator it = data.begin(); it != data.end(); ++it) 
-               os << char('A' + (*it & 15)) << char('a' + (*it >> 4));
-       string base = os.str();
-       string dir  = OnlyPath(lyx::tempName());
-       string file = dir + base + ".eps";
-
-       // everything is fine already
-       if (loader_.filename() == file)
+       // 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;
+       }
 
-       // The real work starts.
-       string const texfile = dir + base + ".tex";
-       std::ofstream of(texfile.c_str());
+       // 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";
+                << "\\documentclass{article}"
+                << "\\usepackage{amssymb}"
+                << "\\thispagestyle{empty}"
+                << "\\pdfoutput=0"
+                << "\\begin{document}"
+                << data
+                << "\\end{document}\n";
        of.close();
 
-       string const cmd =
-//             "latex " + base + ".tex ; " + "
-//    "dvips -x 2500 -R -E -o " + base + ".eps " + base + ".dvi ";
-       // Herbert says this is faster
-               "pdflatex --interaction batchmode " + base + "; " +
-               "dvips -x 2000 -R -E -o " + base + ".eps " + base + ".dvi ";
-       //lyxerr << "calling: '" << "(cd " + dir + "; " + cmd + ")\n";
-       Systemcall sc;
-       sc.startscript(Systemcall::Wait, "(cd " + dir + "; " + cmd + ")");
-
-       // now we are done, start actual loading
-       loader_.reset(file);
-       //lyxerr << "file '" << file << "' registered for preview\n";
-
-       // clean up a bit
-       lyx::unlink(dir + base + ".tex");
-       lyx::unlink(dir + base + ".aux");
-       lyx::unlink(dir + base + ".dvi");
-       lyx::unlink(dir + base + ".log");
+       // 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));
 }
+