]> git.lyx.org Git - lyx.git/commitdiff
Cache the BufferView as a weak_ptr; get rid of those horrible current_views.
authorAngus Leeming <leeming@lyx.org>
Fri, 2 Aug 2002 16:30:58 +0000 (16:30 +0000)
committerAngus Leeming <leeming@lyx.org>
Fri, 2 Aug 2002 16:30:58 +0000 (16:30 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4846 a592a061-630c-0410-9148-cb99ea01b6c8

12 files changed:
src/graphics/ChangeLog
src/graphics/GraphicsLoader.C
src/graphics/PreviewLoader.C
src/graphics/PreviewLoader.h
src/graphics/PreviewedInset.C
src/graphics/PreviewedInset.h
src/insets/ChangeLog
src/insets/insetcite.C
src/insets/insetgraphics.C
src/insets/insetinclude.C
src/mathed/ChangeLog
src/mathed/formula.C

index df1936d610826c3fe50d2a14aca9fcb294b9d8f3..e09183afbd050282da6f284b69025b77c5a53237 100644 (file)
@@ -1,3 +1,13 @@
+2002-08-02  Angus Leeming  <leeming@lyx.org>
+
+       * GraphicsLoader.C: cache the BufferView as a weak_ptr.
+
+       * PreviewLoader.[Ch] (buffer): new method, returning the owner.
+       (add): don't add empty snippets.
+
+       * PreviewedInset.[Ch]: cache the BufferView as a weak_ptr.
+       (view, setView): access to to the cache_.
+
 2002-08-02  Angus Leeming  <leeming@lyx.org>
 
        * PreviewedInset.[Ch]: cache the LaTeX snippet.
index c819f4379fef9ab33960e81dea240209ac2d755c..6873fdf7d154d1527d5d0dee90a24254a012b123 100644 (file)
 #endif
 
 #include "GraphicsLoader.h"
+
+#include "BufferView.h"
+
 #include "GraphicsCache.h"
 #include "GraphicsCacheItem.h"
 #include "GraphicsImage.h"
 #include "GraphicsParams.h"
 #include "GraphicsSupport.h"
 
+#include "frontends/LyXView.h"
 #include "frontends/Timeout.h"
 
+#include <boost/weak_ptr.hpp>
 #include <boost/bind.hpp>
 #include <boost/signals/trackable.hpp>
 
@@ -70,7 +75,7 @@ private:
        ///
        InsetList insets;
        ///
-       BufferView const * view;
+       boost::weak_ptr<BufferView const> view;
 };
 
 
@@ -194,7 +199,7 @@ Image const * Loader::image() const
 
 Loader::Impl::Impl(Params const & params)
        : status_(WaitingToLoad), params_(params),
-         timer(2000, Timeout::ONETIME), view(0)
+         timer(2000, Timeout::ONETIME)
 {
        timer.timeout.connect(boost::bind(&Impl::checkedLoading, this));
 }
@@ -298,7 +303,7 @@ void Loader::Impl::startLoading(Inset const & inset, BufferView const & bv)
        it = std::find(it, end, &inset);
        if (it == end)
                insets.push_back(&inset);
-       view = &bv;
+       view = bv.owner()->view();
 
        timer.start();
 }
@@ -326,10 +331,11 @@ private:
 
 void Loader::Impl::checkedLoading()
 {
-       if (insets.empty() || !view)
+       if (insets.empty() || !view.get())
                return;
 
-       std::list<VisibleParagraph> const vps = getVisibleParagraphs(*view);
+       std::list<VisibleParagraph> const vps =
+               getVisibleParagraphs(*view.get());
 
        InsetList::const_iterator it  = insets.begin();
        InsetList::const_iterator end = insets.end();
index a72cf57085e88c664f7ffbf57d2f8b586d914855..8e15ac8184ce97aca8ed37009fe3f3ece3185aef 100644 (file)
@@ -137,6 +137,8 @@ struct PreviewLoader::Impl : public boost::signals::trackable {
        /// Emit this signal when an image is ready for display.
        boost::signal1<void, PreviewImage const &> imageReady;
 
+       Buffer const & buffer() const { return buffer_; }
+
 private:
        /// Called by the Forkedcall process that generated the bitmap files.
        void finishedGenerating(string const &, pid_t, int);
@@ -232,6 +234,12 @@ void PreviewLoader::emitSignal(PreviewImage const & pimage) const
        pimpl_->imageReady(pimage);
 }
 
+
+Buffer const & PreviewLoader::buffer() const
+{
+       return pimpl_->buffer();
+}
+
 } // namespace grfx
 
 
@@ -383,9 +391,13 @@ void PreviewLoader::Impl::add(string const & latex_snippet)
        if (!pconverter_ || status(latex_snippet) != NotFound)
                return;
 
-       lyxerr[Debug::GRAPHICS] << "adding snippet:\n" << latex_snippet << endl;
+       string const snippet = trim(latex_snippet);
+       if (snippet.empty())
+               return;
+
+       lyxerr[Debug::GRAPHICS] << "adding snippet:\n" << snippet << endl;
 
-       pending_.push_back(latex_snippet);
+       pending_.push_back(snippet);
 }
 
 
index 82947e2ac484e0722244b7627803faea481b6362..b0238474e7849ffaf36d2e25496261ff4f3de6d0 100644 (file)
@@ -84,6 +84,9 @@ public:
         */
        void emitSignal(PreviewImage const &) const;
 
+       /// Which buffer owns this loader.
+       Buffer const & buffer() const;
+
 private:
        /// Use the Pimpl idiom to hide the internals.
        class Impl;
index 1350396f6f03278dce53ad834ca207298405da44..07a889b4e3563065f6300c64e026835f50182f77 100644 (file)
 #include <config.h>
 
 #include "PreviewedInset.h"
-
-#include "BufferView.h"
-
 #include "GraphicsImage.h"
 #include "PreviewLoader.h"
 #include "PreviewImage.h"
 #include "Previews.h"
 
+#include "buffer.h"
+#include "BufferView.h"
+
+#include "frontends/LyXView.h"
+
+#include "support/lstrings.h"
+
 #include <boost/bind.hpp>
 
 
@@ -42,7 +46,8 @@ void PreviewedInset::generatePreview()
        grfx::Previews & previews = grfx::Previews::get();
        grfx::PreviewLoader & loader = previews.loader(view()->buffer());
        addPreview(loader);
-       loader.startLoading();
+       if (!snippet_.empty())
+               loader.startLoading();
 }
 
 
@@ -51,7 +56,11 @@ void PreviewedInset::addPreview(grfx::PreviewLoader & ploader)
        if (!Previews::activated() || !previewWanted())
                return;
 
-       snippet_ = latexString();
+       setView(ploader.buffer().getUser());
+
+       snippet_ = trim(latexString());
+       if (snippet_.empty())
+               return;
 
        pimage_ = ploader.preview(snippet_);
        if (pimage_)
@@ -71,7 +80,7 @@ void PreviewedInset::addPreview(grfx::PreviewLoader & ploader)
 
 bool PreviewedInset::previewReady() const
 {
-       if (!grfx::Previews::activated() || !previewWanted() ||
+       if (!Previews::activated() || !previewWanted() ||
            !view() || !view()->buffer())
                return false;
 
@@ -88,6 +97,14 @@ bool PreviewedInset::previewReady() const
 }
 
 
+void PreviewedInset::setView(BufferView * bv)
+{
+       if (!bv)
+               return;
+       
+       view_ = bv->owner()->view();
+}
+
 void PreviewedInset::imageReady(grfx::PreviewImage const & pimage) const
 {
        // Check snippet against the Inset's current contents
@@ -95,6 +112,7 @@ void PreviewedInset::imageReady(grfx::PreviewImage const & pimage) const
                return;
 
        pimage_ = &pimage;
+
        if (view())
                view()->updateInset(&inset_, false);
 }
index b7147478feda18704dd6a8dd9b5544bbce75bb1a..eba7fbfd9b38864d26b03f3461dae60ff51d911e 100644 (file)
@@ -19,6 +19,7 @@
 #endif
 
 #include "LString.h"
+#include <boost/weak_ptr.hpp>
 #include <boost/signals/trackable.hpp>
 #include <boost/signals/connection.hpp>
 
@@ -56,9 +57,14 @@ public:
        /// If !previewReady() returns 0.
        PreviewImage const * pimage() const { return pimage_; }
 
+       ///
+       void setView(BufferView *);
+
 protected:
        /// Allow the daughter classes to cast up to the parent inset.
        Inset * inset() const { return &inset_; }
+       ///
+       BufferView * view() const { return view_.get(); }
 
 private:
        /** This method is connected to the grfx::PreviewLoader::imageReady
@@ -68,8 +74,6 @@ private:
 
        /// Does the owning inset want a preview?
        virtual bool previewWanted() const = 0;
-       ///
-       virtual BufferView * view() const = 0;
        /// a wrapper to Inset::latex
        virtual string const latexString() const = 0;
 
@@ -77,6 +81,9 @@ private:
        Inset & inset_;
        ///
        string snippet_;
+       ///
+       boost::weak_ptr<BufferView> view_;
+       
        /// We don't own this. Cached for efficiency reasons.
        mutable PreviewImage const * pimage_;
        ///
index 2bb07559b7a829c3e9de216c922eb57f045903d4..6050ae6bfee4816f6dbae709204ca0277b64676e 100644 (file)
@@ -1,3 +1,13 @@
+2002-08-02  Angus Leeming  <leeming@lyx.org>
+
+       * insetcite.C: fix typo.
+
+       * insetgraphics.C: kill current_view and instead cache the BufferView
+       as a weak_ptr.
+
+       * insetinclude.C: kill current_view and pass the BufferView to the
+       PreviewedInset so that it can cache it.
+
 2002-08-01  Angus Leeming  <leeming@lyx.org>
 
        * inset.h (generatePreview): renamed as addPreview.
index 0cdc98d2b51f22a8d6e6bffb79be3d30ff498725..dffb0abefe91a1af93e49cdcd2487556a50d23a1 100644 (file)
@@ -40,7 +40,7 @@ using std::map;
 namespace {
 
 // An optimisation. We assume that until the first InsetCitation::edit is
-// called, we're loding the buffer and that, therefore, we don't need to
+// called, we're loading the buffer and that, therefore, we don't need to
 // reload the bibkey list
 std::map<Buffer const *, bool> loading_buffer;
 
index 67a5bd0e536210836a6f6291e3896d5fce798908..681f8c1810380ff7e8e73a073670fbf85bf1d58f 100644 (file)
@@ -64,7 +64,6 @@ TODO
 #include "graphics/GraphicsImage.h"
 #include "graphics/GraphicsParams.h"
 
-#include "frontends/LyXView.h"
 #include "lyxtext.h"
 #include "buffer.h"
 #include "BufferView.h"
@@ -77,8 +76,10 @@ TODO
 #include "LaTeXFeatures.h"
 #include "lyxlex.h"
 
-#include "frontends/Dialogs.h"
 #include "frontends/Alert.h"
+#include "frontends/Dialogs.h"
+#include "frontends/LyXView.h"
+
 #include "frontends/controllers/helper_funcs.h" // getVectorFromString
 
 #include "support/LAssert.h"
@@ -88,14 +89,12 @@ TODO
 #include "support/systemcall.h"
 #include "support/os.h"
 
+#include <boost/weak_ptr.hpp>
 #include <boost/bind.hpp>
 #include <boost/signals/trackable.hpp>
 
 #include <algorithm> // For the std::max
 
-// Very, Very UGLY!
-extern BufferView * current_view;
-
 extern string system_tempdir;
 
 using std::ostream;
@@ -147,6 +146,8 @@ struct InsetGraphics::Cache : boost::signals::trackable
        grfx::Loader loader;
        ///
        unsigned long checksum;
+       ///
+       boost::weak_ptr<BufferView> view;
 
 private:
        ///
@@ -293,6 +294,7 @@ int InsetGraphics::width(BufferView *, LyXFont const & font) const
 void InsetGraphics::draw(BufferView * bv, LyXFont const & font,
                         int baseline, float & x, bool) const
 {
+       cache_->view = bv->owner()->view();
        int oasc = cache_->old_ascent;
 
        int ldescent = descent(bv, font);
@@ -888,7 +890,8 @@ void InsetGraphics::validate(LaTeXFeatures & features) const
 
 void InsetGraphics::statusChanged()
 {
-       current_view->updateInset(this, false);
+       if (cache_->view.get())
+               cache_->view.get()->updateInset(this, false);
 }
 
 
index f8c62f404a1eee194aaaef038be977af61fb72d4..6ecc6142196fc938554bbc90737f223b7d4ca8bf 100644 (file)
 #include "BufferView.h"
 #include "debug.h"
 #include "lyxrc.h"
-#include "frontends/LyXView.h"
 #include "LaTeXFeatures.h"
 #include "gettext.h"
 
 #include "frontends/Dialogs.h"
+#include "frontends/LyXView.h"
 #include "frontends/Painter.h"
 
 #include "support/filetools.h"
@@ -33,7 +33,7 @@ using std::vector;
 using std::pair;
 
 extern BufferList bufferlist;
-extern BufferView * current_view;
+
 
 class InsetInclude::PreviewImpl : public grfx::PreviewedInset {
 public:
@@ -44,9 +44,6 @@ public:
        bool previewWanted() const;
        ///
        string const latexString() const;
-
-       ///
-       BufferView * view() const { return current_view; }
        ///
        InsetInclude & parent() const {
                return *static_cast<InsetInclude*>(inset());
@@ -500,6 +497,7 @@ int InsetInclude::width(BufferView * bv, LyXFont const & font) const
 void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
                        float & xx, bool b) const
 {
+       preview_->setView(bv);
        if (!preview_->previewReady()) {
                InsetButton::draw(bv, font, y, xx, b);
                return;
@@ -524,7 +522,6 @@ void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
 
 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
 {
-       lyxerr << "InsetInclude::addPreview" << endl;
        preview_->addPreview(ploader);
 }
 
index c17805e5d40766bcead0b1afff6c893d16ae273c..bc8c6f6c08788337c9c5bd1c2173e59470a5b19f 100644 (file)
@@ -1,3 +1,8 @@
+2002-08-02  Angus Leeming  <leeming@lyx.org>
+
+       * formula.C: pass the BufferView to the PreviewedInset so that it can
+       cache it.
+
 2002-08-02  Angus Leeming  <leeming@lyx.org>
 
        * formulabase.[Ch]: store the BufferView as a weak_ptr.
index 8cc653e901c0f051da891a5201be13837de42bcb..3f05043ecb39b7589a2bd1d9cee682826b9500f9 100644 (file)
@@ -74,11 +74,6 @@ private:
        ///
        string const latexString() const;
        ///
-       BufferView * view() const
-       {
-               return parent().view();
-       }
-       ///
        InsetFormula & parent() const
        {
                return *static_cast<InsetFormula*>(inset());
@@ -207,7 +202,7 @@ void InsetFormula::draw(BufferView * bv, LyXFont const & font,
 {
        // This initiates the loading of the preview, so should come
        // before the metrics are computed.
-       view_ = bv->owner()->view();
+       preview_->setView(bv);
        bool const use_preview = preview_->previewReady();
 
        int const x = int(xx);