2 * \file InsetPreview.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Vincent van Ravesteijn
8 * Full author contact details are available in file CREDITS.
12 #include "InsetPreview.h"
15 #include "BufferParams.h"
16 #include "BufferView.h"
18 #include "Dimension.h"
19 #include "MetricsInfo.h"
20 #include "RenderPreview.h"
21 #include "texstream.h"
23 #include "frontends/Painter.h"
25 #include "graphics/PreviewImage.h"
27 #include "mathed/InsetMathHull.h"
28 #include "mathed/MacroTable.h"
37 InsetPreview::InsetPreview(Buffer * buf)
38 : InsetText(buf), preview_(new RenderPreview(this))
41 setFrameColor(Color_previewframe);
45 InsetPreview::~InsetPreview()
49 InsetPreview::InsetPreview(InsetPreview const & other)
52 preview_.reset(new RenderPreview(*other.preview_, this));
56 InsetPreview & InsetPreview::operator=(InsetPreview const & other)
61 InsetText::operator=(other);
62 preview_.reset(new RenderPreview(*other.preview_, this));
68 void InsetPreview::write(ostream & os) const
70 os << "Preview" << "\n";
75 void InsetPreview::addPreview(DocIterator const & inset_pos,
76 graphics::PreviewLoader &) const
78 preparePreview(inset_pos);
82 void InsetPreview::preparePreview(DocIterator const & pos) const
86 OutputParams runparams(&pos.buffer()->params().encoding());
89 // collect macros at this position
91 pos.buffer()->listMacroNames(macros);
93 // look for math insets and collect definitions for the used macros
95 DocIterator dit = doc_iterator_begin(pos.buffer(), this);
96 DocIterator const dend = doc_iterator_end(pos.buffer(), this);
99 for (; dit != dend; dit.forwardInset()) {
100 InsetMath * im = dit.nextInset()->asInsetMath();
101 InsetMathHull * hull = im ? im->asHullInset() : nullptr;
104 for (idx_type idx = 0; idx < hull->nargs(); ++idx)
105 hull->usedMacros(hull->cell(idx), pos, macros, defs);
107 MacroNameSet::iterator it = defs.begin();
108 MacroNameSet::iterator end = defs.end();
109 docstring macro_preamble;
110 for (; it != end; ++it)
111 macro_preamble.append(*it);
113 docstring const snippet = macro_preamble + str.str();
114 preview_->addPreview(snippet, *pos.buffer());
118 bool InsetPreview::previewState(BufferView * bv) const
120 if (!editing(bv) && RenderPreview::previewText()) {
121 graphics::PreviewImage const * pimage =
122 preview_->getPreviewImage(bv->buffer());
123 return pimage && pimage->image();
129 void InsetPreview::reloadPreview(DocIterator const & pos) const
132 preview_->startLoading(*pos.buffer());
136 void InsetPreview::draw(PainterInfo & pi, int x, int y) const
138 if (previewState(pi.base.bv)) {
139 // one pixel gap in front
140 preview_->draw(pi, x + 1, y);
142 InsetText::draw(pi, x, y);
146 void InsetPreview::edit(Cursor & cur, bool front, EntryDirection entry_from)
149 InsetText::edit(cur, front, entry_from);
153 Inset * InsetPreview::editXY(Cursor & cur, int x, int y)
155 if (previewState(&cur.bv())) {
156 edit(cur, true, ENTRY_DIRECTION_IGNORE);
160 return InsetText::editXY(cur, x, y);
164 void InsetPreview::metrics(MetricsInfo & mi, Dimension & dim) const
166 if (previewState(mi.base.bv)) {
167 preview_->metrics(mi, dim);
169 dim.wid = max(dim.wid, 4);
170 dim.asc = max(dim.asc, 4);
172 dim.asc += topOffset(mi.base.bv);
173 dim.des += bottomOffset(mi.base.bv);
174 // insert a one pixel gap
177 MetricsInfo mi_dummy = mi;
178 InsetText::metrics(mi_dummy, dim_dummy);
181 InsetText::metrics(mi, dim);
185 bool InsetPreview::notifyCursorLeaves(Cursor const & old, Cursor & cur)
188 cur.screenUpdateFlags(Update::Force);
189 return InsetText::notifyCursorLeaves(old, cur);