3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
17 #include "BufferParams.h"
18 #include "BufferView.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
27 #include "LyXAction.h"
28 #include "MetricsInfo.h"
29 #include "OutputParams.h"
30 #include "ParagraphParameters.h"
31 #include "Paragraph.h"
32 #include "TextClass.h"
34 #include "graphics/PreviewImage.h"
36 #include "insets/RenderPreview.h"
38 #include "frontends/alert.h"
39 #include "frontends/Application.h"
41 #include "support/debug.h"
42 #include "support/gettext.h"
43 #include "support/lstrings.h"
48 using namespace lyx::support;
52 InsetERT::InsetERT(Buffer * buf, CollapseStatus status)
53 : InsetCollapsable(buf), preview_(new RenderPreview(this))
59 InsetERT::InsetERT(InsetERT const & other) : InsetCollapsable(other)
64 InsetERT & InsetERT::operator=(InsetERT const & other)
68 InsetCollapsable::operator=(other);
69 buffer_ = other.buffer_;
70 preview_.reset(new RenderPreview(*other.preview_, this));
78 hideDialogs("ert", this);
82 void InsetERT::write(ostream & os) const
85 InsetCollapsable::write(os);
89 int InsetERT::plaintext(odocstream & os, OutputParams const & rp) const
92 // do not output TeX code
95 ParagraphList::const_iterator par = paragraphs().begin();
96 ParagraphList::const_iterator end = paragraphs().end();
99 pos_type siz = par->size();
100 for (pos_type i = 0; i < siz; ++i) {
101 char_type const c = par->getChar(i);
102 // output the active characters
119 int InsetERT::docbook(odocstream & os, OutputParams const &) const
121 // FIXME can we do the same thing here as for LaTeX?
122 ParagraphList::const_iterator par = paragraphs().begin();
123 ParagraphList::const_iterator end = paragraphs().end();
127 pos_type siz = par->size();
128 for (pos_type i = 0; i < siz; ++i)
129 os.put(par->getChar(i));
141 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
143 switch (cmd.action) {
144 case LFUN_INSET_MODIFY: {
145 setStatus(cur, string2params(to_utf8(cmd.argument())));
149 InsetCollapsable::doDispatch(cur, cmd);
155 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
156 FuncStatus & status) const
158 switch (cmd.action) {
159 case LFUN_INSET_MODIFY:
160 status.setEnabled(true);
164 return InsetCollapsable::getStatus(cur, cmd, status);
169 docstring const InsetERT::buttonLabel(BufferView const & bv) const
171 if (decoration() == InsetLayout::CLASSIC)
172 return isOpen(bv) ? _("ERT") : getNewLabel(_("ERT"));
174 return getNewLabel(_("ERT"));
178 bool InsetERT::showInsetDialog(BufferView * bv) const
180 bv->showDialog("ert", params2string(status(*bv)),
181 const_cast<InsetERT *>(this));
186 InsetCollapsable::CollapseStatus InsetERT::string2params(string const & in)
190 istringstream data(in);
193 lex.setContext("InsetERT::string2params");
197 return static_cast<CollapseStatus>(s);
201 string InsetERT::params2string(CollapseStatus status)
204 data << "ert" << ' ' << status;
209 docstring InsetERT::xhtml(XHTMLStream &, OutputParams const &) const
215 bool InsetERT::previewState(BufferView * bv) const
217 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
218 graphics::PreviewImage const * pimage =
219 preview_->getPreviewImage(bv->buffer());
220 return pimage && pimage->image();
226 void InsetERT::addPreview(DocIterator const & inset_pos,
227 graphics::PreviewLoader & ploader) const
229 preparePreview(inset_pos);
233 void InsetERT::preparePreview(DocIterator const & pos) const
235 odocstringstream str;
236 OutputParams runparams(&pos.buffer()->params().encoding());
237 latex(str, runparams);
238 docstring const snippet = str.str();
239 LYXERR(Debug::MACROS, "Preview snippet: " << snippet);
240 preview_->addPreview(snippet, *pos.buffer());
244 void InsetERT::reloadPreview(DocIterator const & pos) const
247 preview_->startLoading(*pos.buffer());
251 bool InsetERT::notifyCursorLeaves(Cursor const & old, Cursor & cur)
254 cur.updateFlags(Update::Force);
255 return InsetCollapsable::notifyCursorLeaves(old, cur);
259 void InsetERT::draw(PainterInfo & pi, int x, int y) const
261 use_preview_ = previewState(pi.base.bv);
264 // one pixel gap in front
265 preview_->draw(pi, x + 1, y);
266 setPosCache(pi, x, y);
269 InsetCollapsable::draw(pi, x, y);
273 Inset * InsetERT::editXY(Cursor & cur, int x, int y)
279 return InsetCollapsable::editXY(cur, x, y);
283 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
285 if (previewState(mi.base.bv)) {
286 preview_->metrics(mi, dim);
287 // insert a one pixel gap in front of the formula
289 // Cache the inset dimension.
290 setDimCache(mi, dim);
291 Dimension dim_dummy = dim;
292 MetricsInfo mi_dummy = mi;
293 InsetCollapsable::metrics(mi_dummy, dim_dummy);
296 InsetCollapsable::metrics(mi, dim);