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"
19 #include "MetricsInfo.h"
20 #include "OutputParams.h"
21 #include "RenderPreview.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 otexstream os(str, texrow);
87 OutputParams runparams(&pos.buffer()->params().encoding());
90 // collect macros at this position
92 pos.buffer()->listMacroNames(macros);
94 // look for math insets and collect definitions for the used macros
96 DocIterator dit = doc_iterator_begin(pos.buffer(), this);
97 DocIterator const dend = doc_iterator_end(pos.buffer(), this);
100 for (; dit != dend; dit.forwardInset()) {
101 InsetMath * im = dit.nextInset()->asInsetMath();
102 InsetMathHull * hull = im ? im->asHullInset() : 0;
105 for (idx_type idx = 0; idx < hull->nargs(); ++idx)
106 hull->usedMacros(hull->cell(idx), pos, macros, defs);
108 MacroNameSet::iterator it = defs.begin();
109 MacroNameSet::iterator end = defs.end();
110 docstring macro_preamble;
111 for (; it != end; ++it)
112 macro_preamble.append(*it);
114 docstring const snippet = macro_preamble + str.str();
115 preview_->addPreview(snippet, *pos.buffer());
119 bool InsetPreview::previewState(BufferView * bv) const
121 if (!editing(bv) && RenderPreview::previewText()) {
122 graphics::PreviewImage const * pimage =
123 preview_->getPreviewImage(bv->buffer());
124 return pimage && pimage->image();
130 void InsetPreview::reloadPreview(DocIterator const & pos) const
133 preview_->startLoading(*pos.buffer());
137 void InsetPreview::draw(PainterInfo & pi, int x, int y) const
139 if (previewState(pi.base.bv)) {
140 // one pixel gap in front
141 preview_->draw(pi, x + 1 + TEXT_TO_INSET_OFFSET, y);
142 setPosCache(pi, x, y);
144 InsetText::draw(pi, x, y);
148 void InsetPreview::edit(Cursor & cur, bool front, EntryDirection entry_from)
151 InsetText::edit(cur, front, entry_from);
155 Inset * InsetPreview::editXY(Cursor & cur, int x, int y)
157 if (previewState(&cur.bv())) {
158 edit(cur, true, ENTRY_DIRECTION_IGNORE);
162 return InsetText::editXY(cur, x, y);
166 void InsetPreview::metrics(MetricsInfo & mi, Dimension & dim) const
168 if (previewState(mi.base.bv)) {
169 preview_->metrics(mi, dim);
170 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
172 dim.wid = max(dim.wid, 4);
173 dim.asc = max(dim.asc, 4);
175 dim.asc += TEXT_TO_INSET_OFFSET;
176 dim.des += TEXT_TO_INSET_OFFSET;
177 dim.wid += TEXT_TO_INSET_OFFSET;
178 dim.wid += TEXT_TO_INSET_OFFSET;
179 // insert a one pixel gap
181 // Cache the inset dimension.
182 setDimCache(mi, dim);
184 MetricsInfo mi_dummy = mi;
185 InsetText::metrics(mi_dummy, dim_dummy);
188 InsetText::metrics(mi, dim);
192 bool InsetPreview::notifyCursorLeaves(Cursor const & old, Cursor & cur)
195 cur.screenUpdateFlags(Update::Force);
196 return InsetText::notifyCursorLeaves(old, cur);