X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2FInsetPreview.cpp;h=c306ae91d21ea3678bf31f7ec935e96073f74a5d;hb=e8c932ff842ced0b26c205ed6983102b6e7e50c2;hp=1b3456eac947f3e3d415e52a4afdb28fdefd023b;hpb=72838f24b9fc9e2dd4b73cabc4225180c8973b42;p=lyx.git diff --git a/src/insets/InsetPreview.cpp b/src/insets/InsetPreview.cpp index 1b3456eac9..c306ae91d2 100644 --- a/src/insets/InsetPreview.cpp +++ b/src/insets/InsetPreview.cpp @@ -19,11 +19,15 @@ #include "MetricsInfo.h" #include "OutputParams.h" #include "RenderPreview.h" +#include "texstream.h" #include "frontends/Painter.h" #include "graphics/PreviewImage.h" +#include "mathed/InsetMathHull.h" +#include "mathed/MacroTable.h" + #include using namespace std; @@ -31,16 +35,15 @@ using namespace std; namespace lyx { -InsetPreview::InsetPreview(Buffer * buf) - : InsetText(buf), - preview_(new RenderPreview(this)), use_preview_(true) +InsetPreview::InsetPreview(Buffer * buf) + : InsetText(buf), preview_(new RenderPreview(this)) { setDrawFrame(true); setFrameColor(Color_previewframe); } -InsetPreview::~InsetPreview() +InsetPreview::~InsetPreview() {} @@ -51,6 +54,18 @@ InsetPreview::InsetPreview(InsetPreview const & other) } +InsetPreview & InsetPreview::operator=(InsetPreview const & other) +{ + if (&other == this) + return *this; + + InsetText::operator=(other); + preview_.reset(new RenderPreview(*other.preview_, this)); + + return *this; +} + + void InsetPreview::write(ostream & os) const { os << "Preview" << "\n"; @@ -65,14 +80,38 @@ void InsetPreview::addPreview(DocIterator const & inset_pos, } -void InsetPreview::preparePreview(DocIterator const & pos) const +void InsetPreview::preparePreview(DocIterator const & pos) const { - TexRow texrow; odocstringstream str; - otexstream os(str, texrow); + otexstream os(str); OutputParams runparams(&pos.buffer()->params().encoding()); latex(os, runparams); - docstring const snippet = str.str(); + + // collect macros at this position + MacroNameSet macros; + pos.buffer()->listMacroNames(macros); + + // look for math insets and collect definitions for the used macros + MacroNameSet defs; + DocIterator dit = doc_iterator_begin(pos.buffer(), this); + DocIterator const dend = doc_iterator_end(pos.buffer(), this); + if (!dit.nextInset()) + dit.forwardInset(); + for (; dit != dend; dit.forwardInset()) { + InsetMath * im = dit.nextInset()->asInsetMath(); + InsetMathHull * hull = im ? im->asHullInset() : 0; + if (!hull) + continue; + for (idx_type idx = 0; idx < hull->nargs(); ++idx) + hull->usedMacros(hull->cell(idx), pos, macros, defs); + } + MacroNameSet::iterator it = defs.begin(); + MacroNameSet::iterator end = defs.end(); + docstring macro_preamble; + for (; it != end; ++it) + macro_preamble.append(*it); + + docstring const snippet = macro_preamble + str.str(); preview_->addPreview(snippet, *pos.buffer()); } @@ -97,15 +136,11 @@ void InsetPreview::reloadPreview(DocIterator const & pos) const void InsetPreview::draw(PainterInfo & pi, int x, int y) const { - use_preview_ = previewState(pi.base.bv); - - if (use_preview_) { + if (previewState(pi.base.bv)) { // one pixel gap in front - preview_->draw(pi, x + 1 + TEXT_TO_INSET_OFFSET, y); - setPosCache(pi, x, y); - return; - } - InsetText::draw(pi, x, y); + preview_->draw(pi, x + 1, y); + } else + InsetText::draw(pi, x, y); } @@ -118,7 +153,7 @@ void InsetPreview::edit(Cursor & cur, bool front, EntryDirection entry_from) Inset * InsetPreview::editXY(Cursor & cur, int x, int y) { - if (use_preview_) { + if (previewState(&cur.bv())) { edit(cur, true, ENTRY_DIRECTION_IGNORE); return this; } @@ -131,20 +166,14 @@ void InsetPreview::metrics(MetricsInfo & mi, Dimension & dim) const { if (previewState(mi.base.bv)) { preview_->metrics(mi, dim); - mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET; - + dim.wid = max(dim.wid, 4); dim.asc = max(dim.asc, 4); - + dim.asc += TEXT_TO_INSET_OFFSET; dim.des += TEXT_TO_INSET_OFFSET; - dim.wid += TEXT_TO_INSET_OFFSET; - dim_ = dim; - dim.wid += TEXT_TO_INSET_OFFSET; // insert a one pixel gap dim.wid += 1; - // Cache the inset dimension. - setDimCache(mi, dim); Dimension dim_dummy; MetricsInfo mi_dummy = mi; InsetText::metrics(mi_dummy, dim_dummy);