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"
22 #include "texstream.h"
24 #include "frontends/Painter.h"
26 #include "graphics/PreviewImage.h"
28 #include "mathed/InsetMathHull.h"
29 #include "mathed/MacroTable.h"
38 InsetPreview::InsetPreview(Buffer * buf)
39 : InsetText(buf), preview_(new RenderPreview(this))
42 setFrameColor(Color_previewframe);
46 InsetPreview::~InsetPreview()
50 InsetPreview::InsetPreview(InsetPreview const & other)
53 preview_.reset(new RenderPreview(*other.preview_, this));
57 InsetPreview & InsetPreview::operator=(InsetPreview const & other)
62 InsetText::operator=(other);
63 preview_.reset(new RenderPreview(*other.preview_, this));
69 void InsetPreview::write(ostream & os) const
71 os << "Preview" << "\n";
76 void InsetPreview::addPreview(DocIterator const & inset_pos,
77 graphics::PreviewLoader &) const
79 preparePreview(inset_pos);
83 void InsetPreview::preparePreview(DocIterator const & pos) const
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() : nullptr;
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, y);
143 InsetText::draw(pi, x, y);
147 void InsetPreview::edit(Cursor & cur, bool front, EntryDirection entry_from)
150 InsetText::edit(cur, front, entry_from);
154 Inset * InsetPreview::editXY(Cursor & cur, int x, int y)
156 if (previewState(&cur.bv())) {
157 edit(cur, true, ENTRY_DIRECTION_IGNORE);
161 return InsetText::editXY(cur, x, y);
165 void InsetPreview::metrics(MetricsInfo & mi, Dimension & dim) const
167 if (previewState(mi.base.bv)) {
168 preview_->metrics(mi, dim);
170 dim.wid = max(dim.wid, 4);
171 dim.asc = max(dim.asc, 4);
173 dim.asc += topOffset(mi.base.bv);
174 dim.des += bottomOffset(mi.base.bv);
175 // insert a one pixel gap
178 MetricsInfo mi_dummy = mi;
179 InsetText::metrics(mi_dummy, dim_dummy);
182 InsetText::metrics(mi, dim);
186 bool InsetPreview::notifyCursorLeaves(Cursor const & old, Cursor & cur)
189 cur.screenUpdateFlags(Update::Force);
190 return InsetText::notifyCursorLeaves(old, cur);