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)
39 preview_(new RenderPreview(this)), use_preview_(true)
42 setFrameColor(Color_previewframe);
46 InsetPreview::~InsetPreview()
50 InsetPreview::InsetPreview(InsetPreview const & other)
53 preview_.reset(new RenderPreview(*other.preview_, this));
57 void InsetPreview::write(ostream & os) const
59 os << "Preview" << "\n";
64 void InsetPreview::addPreview(DocIterator const & inset_pos,
65 graphics::PreviewLoader &) const
67 preparePreview(inset_pos);
71 void InsetPreview::preparePreview(DocIterator const & pos) const
75 otexstream os(str, texrow);
76 OutputParams runparams(&pos.buffer()->params().encoding());
79 // collect macros at this position
81 pos.buffer()->listMacroNames(macros);
83 // look for math insets and collect definitions for the used macros
85 DocIterator dit = doc_iterator_begin(pos.buffer(), this);
86 DocIterator const dend = doc_iterator_end(pos.buffer(), this);
89 for (; dit != dend; dit.forwardInset()) {
90 InsetMath * im = dit.nextInset()->asInsetMath();
91 InsetMathHull * hull = im ? im->asHullInset() : 0;
94 for (idx_type idx = 0; idx < hull->nargs(); ++idx)
95 hull->usedMacros(hull->cell(idx), pos, macros, defs);
97 MacroNameSet::iterator it = defs.begin();
98 MacroNameSet::iterator end = defs.end();
99 docstring macro_preamble;
100 for (; it != end; ++it)
101 macro_preamble.append(*it);
103 docstring const snippet = macro_preamble + str.str();
104 preview_->addPreview(snippet, *pos.buffer());
108 bool InsetPreview::previewState(BufferView * bv) const
110 if (!editing(bv) && RenderPreview::previewText()) {
111 graphics::PreviewImage const * pimage =
112 preview_->getPreviewImage(bv->buffer());
113 return pimage && pimage->image();
119 void InsetPreview::reloadPreview(DocIterator const & pos) const
122 preview_->startLoading(*pos.buffer());
126 void InsetPreview::draw(PainterInfo & pi, int x, int y) const
128 use_preview_ = previewState(pi.base.bv);
131 // one pixel gap in front
132 preview_->draw(pi, x + 1 + TEXT_TO_INSET_OFFSET, y);
133 setPosCache(pi, x, y);
136 InsetText::draw(pi, x, y);
140 void InsetPreview::edit(Cursor & cur, bool front, EntryDirection entry_from)
143 InsetText::edit(cur, front, entry_from);
147 Inset * InsetPreview::editXY(Cursor & cur, int x, int y)
150 edit(cur, true, ENTRY_DIRECTION_IGNORE);
154 return InsetText::editXY(cur, x, y);
158 void InsetPreview::metrics(MetricsInfo & mi, Dimension & dim) const
160 if (previewState(mi.base.bv)) {
161 preview_->metrics(mi, dim);
162 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
164 dim.wid = max(dim.wid, 4);
165 dim.asc = max(dim.asc, 4);
167 dim.asc += TEXT_TO_INSET_OFFSET;
168 dim.des += TEXT_TO_INSET_OFFSET;
169 dim.wid += TEXT_TO_INSET_OFFSET;
171 dim.wid += TEXT_TO_INSET_OFFSET;
172 // insert a one pixel gap
174 // Cache the inset dimension.
175 setDimCache(mi, dim);
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);