From 984cbba50465a490ca4060f6e51c4263d80f8791 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Fri, 2 Aug 2002 12:38:20 +0000 Subject: [PATCH] * Mathed now caches the BufferView as a weak_ptr. * PreviewedInset caches the LaTeX snippet. * Further clean-up of mathed's preview code. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4838 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/graphics/ChangeLog | 4 ++++ src/graphics/PreviewedInset.C | 20 ++++++++------------ src/graphics/PreviewedInset.h | 10 ++++++---- src/mathed/ChangeLog | 15 +++++++++++++++ src/mathed/formula.C | 6 ++---- src/mathed/formulabase.C | 11 ++++++----- src/mathed/formulabase.h | 9 ++++----- src/mathed/math_metricsinfo.C | 2 +- src/mathed/math_metricsinfo.h | 4 +++- src/mathed/math_nestinset.C | 9 +-------- 10 files changed, 50 insertions(+), 40 deletions(-) diff --git a/src/graphics/ChangeLog b/src/graphics/ChangeLog index f32a45eb87..df1936d610 100644 --- a/src/graphics/ChangeLog +++ b/src/graphics/ChangeLog @@ -1,3 +1,7 @@ +2002-08-02 Angus Leeming + + * PreviewedInset.[Ch]: cache the LaTeX snippet. + 2002-08-01 Angus Leeming * PreviewedInset.[Ch]: new files. An abstract base class that can help diff --git a/src/graphics/PreviewedInset.C b/src/graphics/PreviewedInset.C index a5ccfa2ca2..1350396f6f 100644 --- a/src/graphics/PreviewedInset.C +++ b/src/graphics/PreviewedInset.C @@ -33,7 +33,7 @@ bool PreviewedInset::activated() } -void PreviewedInset::generatePreview() const +void PreviewedInset::generatePreview() { if (!Previews::activated() || !previewWanted() || !view() || !view()->buffer()) @@ -46,15 +46,14 @@ void PreviewedInset::generatePreview() const } -void PreviewedInset::addPreview(grfx::PreviewLoader & ploader) const +void PreviewedInset::addPreview(grfx::PreviewLoader & ploader) { if (!Previews::activated() || !previewWanted()) return; - // Generate the LaTeX snippet. - string const snippet = latexString(); + snippet_ = latexString(); - pimage_ = ploader.preview(snippet); + pimage_ = ploader.preview(snippet_); if (pimage_) return; @@ -66,7 +65,7 @@ void PreviewedInset::addPreview(grfx::PreviewLoader & ploader) const boost::bind(&PreviewedInset::imageReady, this, _1)); } - ploader.add(snippet); + ploader.add(snippet_); } @@ -76,13 +75,10 @@ bool PreviewedInset::previewReady() const !view() || !view()->buffer()) return false; - // If the cached grfx::PreviewImage is invalid, update it. - string const snippet = latexString(); - - if (!pimage_ || snippet != pimage_->snippet()) { + if (!pimage_ || snippet_ != pimage_->snippet()) { grfx::PreviewLoader & ploader = grfx::Previews::get().loader(view()->buffer()); - pimage_ = ploader.preview(snippet); + pimage_ = ploader.preview(snippet_); } if (!pimage_) @@ -95,7 +91,7 @@ bool PreviewedInset::previewReady() const void PreviewedInset::imageReady(grfx::PreviewImage const & pimage) const { // Check snippet against the Inset's current contents - if (latexString() != pimage.snippet()) + if (snippet_ != pimage.snippet()) return; pimage_ = &pimage; diff --git a/src/graphics/PreviewedInset.h b/src/graphics/PreviewedInset.h index 4ab1ecda9a..87baa3b9d9 100644 --- a/src/graphics/PreviewedInset.h +++ b/src/graphics/PreviewedInset.h @@ -41,12 +41,12 @@ public: /** Find the PreviewLoader, add a LaTeX snippet to it and * start the loading process. */ - void generatePreview() const; + void generatePreview(); /** Add a LaTeX snippet to the PreviewLoader but do not start the * loading process. */ - void addPreview(PreviewLoader & ploader) const; + void addPreview(PreviewLoader & ploader); /// The preview has been generated and is ready to use. bool previewReady() const; @@ -73,10 +73,12 @@ private: /// Inset & inset_; - /// We don't own this + /// + string snippet_; + /// We don't own this. Cached for efficiency reasons. mutable PreviewImage const * pimage_; /// - mutable boost::signals::connection connection_; + boost::signals::connection connection_; }; } // namespace grfx diff --git a/src/mathed/ChangeLog b/src/mathed/ChangeLog index b3ffc3a435..c17805e5d4 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -1,3 +1,18 @@ +2002-08-02 Angus Leeming + + * formulabase.[Ch]: store the BufferView as a weak_ptr. + (updatePreview): removed. + (insetUnlock): invoke generatePreview(). + + * formula.C (InsetFormula): pass the shared_ptr to view_, not the raw + BufferView. + (read, localDispatch): remove those calls to updatePreview(). + + * math_metricsinfo.[Ch]: store the BufferView as a weak_ptr. + + * math_nestinset.C (notifyCursorLeaves): empty, because + generatePreview() is now called from InsetFormulaBase::insetUnlock. + 2002-08-01 Angus Leeming * formula.C: move code into the new PreviewedInset class. diff --git a/src/mathed/formula.C b/src/mathed/formula.C index 34ff4820aa..9d71398d28 100644 --- a/src/mathed/formula.C +++ b/src/mathed/formula.C @@ -103,7 +103,7 @@ InsetFormula::InsetFormula(BufferView * bv) : par_(MathAtom(new MathHullInset)), preview_(new PreviewImpl(*this)) { - view_ = bv; + view_ = bv->owner()->view(); } @@ -191,7 +191,6 @@ void InsetFormula::read(Buffer const *, LyXLex & lex) { mathed_parse_normal(par_, lex); metrics(); - updatePreview(); } @@ -207,6 +206,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(); bool const use_preview = preview_->previewReady(); int const x = int(xx); @@ -390,8 +390,6 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action, result = InsetFormulaBase::localDispatch(bv, action, arg); } - //updatePreview(); - return result; } diff --git a/src/mathed/formulabase.C b/src/mathed/formulabase.C index 1fc4ff5a75..4d7c4ab61b 100644 --- a/src/mathed/formulabase.C +++ b/src/mathed/formulabase.C @@ -89,7 +89,7 @@ bool openNewInset(BufferView * bv, UpdatableInset * new_inset) InsetFormulaBase::InsetFormulaBase() - : view_(0), font_(), xo_(0), yo_(0) + : font_(), xo_(0), yo_(0) { // This is needed as long the math parser is not re-entrant initMath(); @@ -151,7 +151,7 @@ void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const void InsetFormulaBase::metrics(BufferView * bv) const { if (bv) - view_ = bv; + view_ = bv->owner()->view(); MathMetricsInfo mi; mi.view = view_; //mi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT; @@ -204,6 +204,7 @@ void InsetFormulaBase::insetUnlock(BufferView * bv) } releaseMathCursor(bv); } + generatePreview(); bv->updateInset(this, false); } @@ -836,13 +837,13 @@ Inset::Code InsetFormulaBase::lyxCode() const int InsetFormulaBase::ylow() const { - return yo_ - ascent(view_, font_); + return yo_ - ascent(view(), font_); } int InsetFormulaBase::yhigh() const { - return yo_ + descent(view_, font_); + return yo_ + descent(view(), font_); } @@ -854,7 +855,7 @@ int InsetFormulaBase::xlow() const int InsetFormulaBase::xhigh() const { - return xo_ + width(view_, font_); + return xo_ + width(view(), font_); } diff --git a/src/mathed/formulabase.h b/src/mathed/formulabase.h index 02366a13e1..205f894ed0 100644 --- a/src/mathed/formulabase.h +++ b/src/mathed/formulabase.h @@ -23,6 +23,8 @@ #include "frontends/mouse_state.h" #include "lyxfont.h" +#include + #include class Buffer; @@ -99,7 +101,7 @@ public: /// virtual void updateLocal(BufferView * bv, bool mark_dirty); /// - BufferView * view() const { return view_; } + BufferView * view() const { return view_.get(); } /// virtual bool searchForward(BufferView *, string const &, @@ -115,9 +117,6 @@ public: virtual void revealCodes(BufferView *) const; /// virtual Inset::EDITABLE editable() const { return HIGHLY_EDITABLE; } - /// - virtual void updatePreview() {} - private: /// unimplemented @@ -127,7 +126,7 @@ private: protected: /// - mutable BufferView * view_; + mutable boost::weak_ptr view_; /// mutable LyXFont font_; diff --git a/src/mathed/math_metricsinfo.C b/src/mathed/math_metricsinfo.C index 90d553c6f0..10e2e195f9 100644 --- a/src/mathed/math_metricsinfo.C +++ b/src/mathed/math_metricsinfo.C @@ -16,7 +16,7 @@ MathMetricsBase::MathMetricsBase() MathMetricsInfo::MathMetricsInfo() - : view(0), fullredraw(false) + : fullredraw(false) {} diff --git a/src/mathed/math_metricsinfo.h b/src/mathed/math_metricsinfo.h index f4d6ab6275..54202829fe 100644 --- a/src/mathed/math_metricsinfo.h +++ b/src/mathed/math_metricsinfo.h @@ -4,6 +4,8 @@ #include "lyxfont.h" #include "LString.h" +#include + class BufferView; class Painter; class MathNestInset; @@ -46,7 +48,7 @@ struct MathMetricsInfo { /// MathMetricsBase base; /// - BufferView * view; + boost::weak_ptr view; /// bool fullredraw; }; diff --git a/src/mathed/math_nestinset.C b/src/mathed/math_nestinset.C index 9a90a9462c..91d84fd674 100644 --- a/src/mathed/math_nestinset.C +++ b/src/mathed/math_nestinset.C @@ -319,11 +319,4 @@ void MathNestInset::normalize(NormalStream & os) const void MathNestInset::notifyCursorLeaves() -{ - // Generate a preview only if we are leaving the InsetFormula itself - if (!mathcursor || mathcursor->depth() != 1) - return; - - InsetFormulaBase * inset = mathcursor->formula(); - inset->generatePreview(); -} +{} -- 2.39.2