From 571594d508179d838c0a1395af13fd4a611485c3 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Tue, 13 Apr 2004 17:38:16 +0000 Subject: [PATCH] Re-enable previews for mathed. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8651 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/ChangeLog | 5 ++ src/mathed/ChangeLog | 9 ++++ src/mathed/math_hullinset.C | 102 +++++++++++++++++++++++++++++++++++- src/mathed/math_hullinset.h | 16 +++++- src/text3.C | 4 ++ 5 files changed, 132 insertions(+), 4 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 8889978f4a..3c70268038 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2004-04-13 Angus Leeming + + * text3.C (dispatch): call Inset::.notifyCursorLeaves when the + cursor is clicked out of an inset. + 2004-04-13 Angus Leeming * lyx_main.[Ch] (updateInset): pass it an InsetBase pointer rather diff --git a/src/mathed/ChangeLog b/src/mathed/ChangeLog index da3b8109b2..8b8a17e243 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -1,3 +1,12 @@ +2004-04-13 Angus Leeming + + * math_hullinset.[Ch]: add a RenderPreview variable. + (copy c-tor, copy assignment operator, d-tor, notifyCursorLeaves, + addPreview): new member functions. The copy c-tor and assignment op + could be replaced by the compiler-generated defaults if preview_ + was stored as a RenderPreview var rather than a scoped pointer. + (metrics, draw): use the preview renderer if previewing is turned on. + 2004-04-05 Angus Leeming * math_scriptinset.C (up, down, notifyCursorLeaves): ensure that diff --git a/src/mathed/math_hullinset.C b/src/mathed/math_hullinset.C index bcacee79cf..87d2804dd0 100644 --- a/src/mathed/math_hullinset.C +++ b/src/mathed/math_hullinset.C @@ -26,15 +26,22 @@ #include "gettext.h" #include "LaTeXFeatures.h" #include "LColor.h" +#include "lyx_main.h" #include "lyxrc.h" #include "outputparams.h" #include "textpainter.h" #include "undo.h" +#include "insets/render_preview.h" + #include "frontends/Alert.h" +#include "graphics/PreviewLoader.h" + #include "support/std_sstream.h" +#include + using std::endl; using std::max; @@ -114,7 +121,8 @@ namespace { MathHullInset::MathHullInset() - : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1) + : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1), + preview_(new RenderPreview(this)) { //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl; //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl; @@ -125,18 +133,42 @@ MathHullInset::MathHullInset() MathHullInset::MathHullInset(string const & type) - : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1) + : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1), + preview_(new RenderPreview(this)) { setDefaults(); } +MathHullInset::MathHullInset(MathHullInset const & other) + : MathGridInset(other), + type_(other.type_), nonum_(other.nonum_), label_(other.label_), + preview_(new RenderPreview(this)) +{} + + +MathHullInset::~MathHullInset() +{} + + auto_ptr MathHullInset::clone() const { return auto_ptr(new MathHullInset(*this)); } +void MathHullInset::operator=(MathHullInset const & other) +{ + if (this == &other) + return; + *static_cast(this) = MathGridInset(other); + type_ = other.type_; + nonum_ = other.nonum_; + label_ = other.label_; + preview_.reset(new RenderPreview(*other.preview_, this)); +} + + MathInset::mode_type MathHullInset::currentMode() const { if (type_ == "none") @@ -194,6 +226,20 @@ char const * MathHullInset::standardFont() const void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const { + bool const use_preview = (!editing(mi.base.bv) && + RenderPreview::activated() && + preview_->previewReady()); + + if (use_preview) { + preview_->metrics(mi, dim); + // insert a one pixel gap in front of the formula + dim.wid += 1; + if (display()) + dim.des += 12; + dim_ = dim; + return; + } + FontSetChanger dummy1(mi.base, standardFont()); StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT); @@ -228,6 +274,18 @@ void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const void MathHullInset::draw(PainterInfo & pi, int x, int y) const { + // The previews are drawn only when we're not editing the inset. + bool const use_preview = (!editing(pi.base.bv) && + RenderPreview::activated() && + preview_->previewReady()); + + if (use_preview) { + // one pixel gap in front + preview_->draw(pi, x + 1, y); + setPosCache(pi, x, y); + return; + } + FontSetChanger dummy1(pi.base, standardFont()); StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT); MathGridInset::draw(pi, x + 1, y); @@ -272,6 +330,38 @@ void MathHullInset::drawT(TextPainter & pain, int x, int y) const } +namespace { + +string const latex_string(MathHullInset const & inset) +{ + ostringstream ls; + WriteStream wi(ls, false, false); + inset.write(wi); + return ls.str(); +} + +} // namespace anon + + +void MathHullInset::addPreview(lyx::graphics::PreviewLoader & ploader) const +{ + string const snippet = latex_string(*this); + preview_->addPreview(snippet, ploader); +} + + +void MathHullInset::notifyCursorLeaves(LCursor & cur) +{ + if (!RenderPreview::activated()) + return; + + Buffer const & buffer = cur.buffer(); + string const snippet = latex_string(*this); + preview_->addPreview(snippet, buffer); + preview_->startLoading(buffer); +} + + string MathHullInset::label(row_type row) const { row_type n = nrows(); @@ -784,6 +874,14 @@ void MathHullInset::priv_dispatch(LCursor & cur, FuncRequest & cmd) { switch (cmd.action) { + case LFUN_FINISHED_LEFT: + case LFUN_FINISHED_RIGHT: + case LFUN_FINISHED_UP: + case LFUN_FINISHED_DOWN: + MathGridInset::priv_dispatch(cur, cmd); + notifyCursorLeaves(cur); + break; + case LFUN_BREAKPARAGRAPH: // just swallow this break; diff --git a/src/mathed/math_hullinset.h b/src/mathed/math_hullinset.h index ba0ebfb4de..fd3e565973 100644 --- a/src/mathed/math_hullinset.h +++ b/src/mathed/math_hullinset.h @@ -13,6 +13,9 @@ #define MATH_HULLINSET_H #include "math_gridinset.h" +#include + +class RenderPreview; /// This provides an interface between "LyX insets" and "LyX math insets" @@ -23,8 +26,14 @@ public: /// explicit MathHullInset(std::string const & type); /// + MathHullInset(MathHullInset const &); + /// + ~MathHullInset(); + /// std::auto_ptr clone() const; /// + void operator=(MathHullInset const &); + /// mode_type currentMode() const; /// void metrics(MetricsInfo & mi, Dimension & dim) const; @@ -104,10 +113,12 @@ public: int docbook(Buffer const &, std::ostream &, OutputParams const &) const; + /// get notification when the cursor leaves this inset + void notifyCursorLeaves(LCursor & cur); /// //bool insetAllowed(Code code) const; /// - //void addPreview(lyx::graphics::PreviewLoader &) const; + void addPreview(lyx::graphics::PreviewLoader &) const; protected: @@ -147,7 +158,8 @@ private: std::vector nonum_; /// std::vector label_; - + /// + boost::scoped_ptr preview_; // // Incorporate me // diff --git a/src/text3.C b/src/text3.C index 8f1c0b6799..a5b9962d04 100644 --- a/src/text3.C +++ b/src/text3.C @@ -1148,6 +1148,10 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd) finishUndo(); cur.x_target() = cursorX(cur.top()); + // Has the cursor just left the inset? + if (bv->cursor().inMathed() && !cur.inMathed()) + bv->cursor().inset().notifyCursorLeaves(bv->cursor()); + // Set cursor here. bv->cursor() = cur; -- 2.39.2