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 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 otexstream os(str, texrow);
88 OutputParams runparams(&pos.buffer()->params().encoding());
91 // collect macros at this position
93 pos.buffer()->listMacroNames(macros);
95 // look for math insets and collect definitions for the used macros
97 DocIterator dit = doc_iterator_begin(pos.buffer(), this);
98 DocIterator const dend = doc_iterator_end(pos.buffer(), this);
101 for (; dit != dend; dit.forwardInset()) {
102 InsetMath * im = dit.nextInset()->asInsetMath();
103 InsetMathHull * hull = im ? im->asHullInset() : 0;
106 for (idx_type idx = 0; idx < hull->nargs(); ++idx)
107 hull->usedMacros(hull->cell(idx), pos, macros, defs);
109 MacroNameSet::iterator it = defs.begin();
110 MacroNameSet::iterator end = defs.end();
111 docstring macro_preamble;
112 for (; it != end; ++it)
113 macro_preamble.append(*it);
115 docstring const snippet = macro_preamble + str.str();
116 preview_->addPreview(snippet, *pos.buffer());
120 bool InsetPreview::previewState(BufferView * bv) const
122 if (!editing(bv) && RenderPreview::previewText()) {
123 graphics::PreviewImage const * pimage =
124 preview_->getPreviewImage(bv->buffer());
125 return pimage && pimage->image();
131 void InsetPreview::reloadPreview(DocIterator const & pos) const
134 preview_->startLoading(*pos.buffer());
138 void InsetPreview::draw(PainterInfo & pi, int x, int y) const
140 use_preview_ = previewState(pi.base.bv);
143 // one pixel gap in front
144 preview_->draw(pi, x + 1 + TEXT_TO_INSET_OFFSET, y);
145 setPosCache(pi, x, y);
148 InsetText::draw(pi, x, y);
152 void InsetPreview::edit(Cursor & cur, bool front, EntryDirection entry_from)
155 InsetText::edit(cur, front, entry_from);
159 Inset * InsetPreview::editXY(Cursor & cur, int x, int y)
162 edit(cur, true, ENTRY_DIRECTION_IGNORE);
166 return InsetText::editXY(cur, x, y);
170 void InsetPreview::metrics(MetricsInfo & mi, Dimension & dim) const
172 if (previewState(mi.base.bv)) {
173 preview_->metrics(mi, dim);
174 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
176 dim.wid = max(dim.wid, 4);
177 dim.asc = max(dim.asc, 4);
179 dim.asc += TEXT_TO_INSET_OFFSET;
180 dim.des += TEXT_TO_INSET_OFFSET;
181 dim.wid += TEXT_TO_INSET_OFFSET;
183 dim.wid += TEXT_TO_INSET_OFFSET;
184 // insert a one pixel gap
186 // Cache the inset dimension.
187 setDimCache(mi, dim);
189 MetricsInfo mi_dummy = mi;
190 InsetText::metrics(mi_dummy, dim_dummy);
193 InsetText::metrics(mi, dim);
197 bool InsetPreview::notifyCursorLeaves(Cursor const & old, Cursor & cur)
200 cur.screenUpdateFlags(Update::Force);
201 return InsetText::notifyCursorLeaves(old, cur);