3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jürgen Spitzmüller
8 * Full author contact details are available in file CREDITS.
15 #include "BufferParams.h"
16 #include "BufferView.h"
18 #include "Dimension.h"
19 #include "FuncRequest.h"
20 #include "FuncStatus.h"
21 #include "LaTeXFeatures.h"
23 #include "MetricsInfo.h"
24 #include "OutputParams.h"
25 #include "RenderPreview.h"
26 #include "texstream.h"
28 #include "frontends/Painter.h"
30 #include "graphics/PreviewImage.h"
39 InsetIPA::InsetIPA(Buffer * buf)
41 preview_(new RenderPreview(this))
44 setFrameColor(Color_insetframe);
52 InsetIPA::InsetIPA(InsetIPA const & other)
55 preview_.reset(new RenderPreview(*other.preview_, this));
59 InsetIPA & InsetIPA::operator=(InsetIPA const & other)
64 InsetText::operator=(other);
65 preview_.reset(new RenderPreview(*other.preview_, this));
71 void InsetIPA::write(ostream & os) const
78 void InsetIPA::doDispatch(Cursor & cur, FuncRequest & cmd)
80 switch (cmd.action()) {
81 case LFUN_QUOTE_INSERT: {
82 FuncRequest fr(LFUN_SELF_INSERT, "\"");
83 InsetText::doDispatch(cur, fr);
87 InsetText::doDispatch(cur, cmd);
94 bool InsetIPA::getStatus(Cursor & cur, FuncRequest const & cmd,
95 FuncStatus & flag) const
97 switch (cmd.action()) {
98 case LFUN_SCRIPT_INSERT: {
99 if (cmd.argument() == "subscript") {
100 flag.setEnabled(false);
106 flag.setEnabled(true);
112 return InsetText::getStatus(cur, cmd, flag);
116 void InsetIPA::addPreview(DocIterator const & inset_pos,
117 graphics::PreviewLoader &) const
119 preparePreview(inset_pos);
123 void InsetIPA::preparePreview(DocIterator const & pos) const
125 odocstringstream str;
127 OutputParams runparams(&pos.buffer()->params().encoding());
128 latex(os, runparams);
129 docstring const snippet = str.str();
130 preview_->addPreview(snippet, *pos.buffer());
134 bool InsetIPA::previewState(BufferView * bv) const
136 if (!editing(bv) && RenderPreview::previewText()) {
137 graphics::PreviewImage const * pimage =
138 preview_->getPreviewImage(bv->buffer());
139 return pimage && pimage->image();
145 void InsetIPA::reloadPreview(DocIterator const & pos) const
148 preview_->startLoading(*pos.buffer());
152 void InsetIPA::draw(PainterInfo & pi, int x, int y) const
154 if (previewState(pi.base.bv)) {
155 preview_->draw(pi, x, y);
158 InsetText::draw(pi, x, y);
162 void InsetIPA::edit(Cursor & cur, bool front, EntryDirection entry_from)
165 InsetText::edit(cur, front, entry_from);
169 Inset * InsetIPA::editXY(Cursor & cur, int x, int y)
171 if (previewState(&cur.bv())) {
172 edit(cur, true, ENTRY_DIRECTION_IGNORE);
176 return InsetText::editXY(cur, x, y);
180 void InsetIPA::metrics(MetricsInfo & mi, Dimension & dim) const
182 if (previewState(mi.base.bv)) {
183 preview_->metrics(mi, dim);
185 dim.wid = max(dim.wid, 4);
186 dim.asc = max(dim.asc, 4);
188 dim.asc += topOffset(mi.base.bv);
189 dim.des += bottomOffset(mi.base.bv);
190 // insert a one pixel gap
193 MetricsInfo mi_dummy = mi;
194 InsetText::metrics(mi_dummy, dim_dummy);
197 InsetText::metrics(mi, dim);
201 bool InsetIPA::notifyCursorLeaves(Cursor const & old, Cursor & cur)
204 cur.screenUpdateFlags(Update::Force);
205 return InsetText::notifyCursorLeaves(old, cur);
209 void InsetIPA::validate(LaTeXFeatures & features) const
211 features.require("tipa");
212 features.require("tipx");
214 InsetText::validate(features);
218 void InsetIPA::latex(otexstream & os, OutputParams const & runparams_in) const
220 OutputParams runparams(runparams_in);
221 runparams.inIPA = true;
222 bool const multipar = (text().paragraphs().size() > 1);
223 // fontspec knows \textipa, but not the IPA environment
224 bool const nontexfonts = buffer_->params().useNonTeXFonts;
225 if (multipar && !nontexfonts)
226 os << "\\begin{IPA}\n";
229 InsetText::latex(os, runparams);
230 if (multipar && !nontexfonts)
231 os << "\n\\end{IPA}";
237 void InsetIPA::docbook(XMLStream & xs, OutputParams const & runparams) const
239 OutputParams rp(runparams);
241 InsetText::docbook(xs, rp);
245 docstring InsetIPA::xhtml(XMLStream & xs, OutputParams const & runparams_in) const
247 OutputParams runparams(runparams_in);
248 runparams.inIPA = true;
249 return InsetText::xhtml(xs, runparams);
253 bool InsetIPA::insetAllowed(InsetCode code) const
256 // code that is allowed