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"
34 InsetPreview::InsetPreview(Buffer * buf)
36 preview_(new RenderPreview(this)), use_preview_(true)
38 setAutoBreakRows(true);
40 setFrameColor(Color_previewframe);
44 InsetPreview::~InsetPreview()
48 InsetPreview::InsetPreview(InsetPreview const & other)
51 preview_.reset(new RenderPreview(*other.preview_, this));
55 void InsetPreview::write(ostream & os) const
57 os << "Preview" << "\n";
62 void InsetPreview::addPreview(DocIterator const & inset_pos,
63 graphics::PreviewLoader &) const
65 preparePreview(inset_pos);
69 void InsetPreview::preparePreview(DocIterator const & pos) const
73 otexstream os(str, texrow);
74 OutputParams runparams(&pos.buffer()->params().encoding());
76 docstring const snippet = str.str();
77 preview_->addPreview(snippet, *pos.buffer());
81 bool InsetPreview::previewState(BufferView * bv) const
83 if (!editing(bv) && RenderPreview::previewText()) {
84 graphics::PreviewImage const * pimage =
85 preview_->getPreviewImage(bv->buffer());
86 return pimage && pimage->image();
92 void InsetPreview::reloadPreview(DocIterator const & pos) const
95 preview_->startLoading(*pos.buffer());
99 void InsetPreview::draw(PainterInfo & pi, int x, int y) const
101 use_preview_ = previewState(pi.base.bv);
104 // one pixel gap in front
105 preview_->draw(pi, x + 1 + TEXT_TO_INSET_OFFSET, y);
106 setPosCache(pi, x, y);
109 InsetText::draw(pi, x, y);
113 void InsetPreview::edit(Cursor & cur, bool front, EntryDirection entry_from)
116 InsetText::edit(cur, front, entry_from);
120 Inset * InsetPreview::editXY(Cursor & cur, int x, int y)
123 edit(cur, true, ENTRY_DIRECTION_IGNORE);
127 return InsetText::editXY(cur, x, y);
131 void InsetPreview::metrics(MetricsInfo & mi, Dimension & dim) const
133 if (previewState(mi.base.bv)) {
134 preview_->metrics(mi, dim);
135 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
137 dim.wid = max(dim.wid, 4);
138 dim.asc = max(dim.asc, 4);
140 dim.asc += TEXT_TO_INSET_OFFSET;
141 dim.des += TEXT_TO_INSET_OFFSET;
142 dim.wid += TEXT_TO_INSET_OFFSET;
144 dim.wid += TEXT_TO_INSET_OFFSET;
145 // insert a one pixel gap
147 // Cache the inset dimension.
148 setDimCache(mi, dim);
150 MetricsInfo mi_dummy = mi;
151 InsetText::metrics(mi_dummy, dim_dummy);
154 InsetText::metrics(mi, dim);
158 bool InsetPreview::notifyCursorLeaves(Cursor const & old, Cursor & cur)
161 cur.screenUpdateFlags(Update::Force);
162 return InsetText::notifyCursorLeaves(old, cur);