3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "InsetWrap.h"
15 #include "InsetCaption.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
22 #include "DispatchResult.h"
24 #include "FloatList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "LaTeXFeatures.h"
30 #include "texstream.h"
31 #include "TextClass.h"
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
37 #include "frontends/Application.h"
46 InsetWrap::InsetWrap(Buffer * buf, string const & type)
47 : InsetCaptionable(buf)
51 params_.placement = "o";
52 params_.overhang = Length(0, Length::PCW);
53 params_.width = Length(50, Length::PCW);
57 InsetWrap::~InsetWrap()
59 hideDialogs("wrap", this);
63 // Enforce equality of float type and caption type.
64 void InsetWrap::setCaptionType(std::string const & type)
66 InsetCaptionable::setCaptionType(type);
67 params_.type = captionType();
68 setLabel(_("wrap: ") + floatName(type));
72 docstring InsetWrap::layoutName() const
74 return "Wrap:" + from_utf8(params_.type);
78 docstring InsetWrap::toolTip(BufferView const & bv, int x, int y) const
81 return InsetCaptionable::toolTip(bv, x, y);
82 OutputParams rp(&buffer().params().encoding());
83 docstring caption_tip = getCaptionText(rp);
84 if (!caption_tip.empty())
85 caption_tip += from_ascii("\n");
86 return toolTipText(caption_tip);
90 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
92 switch (cmd.action()) {
93 case LFUN_INSET_MODIFY: {
94 cur.recordUndoInset(this);
95 InsetWrapParams params;
96 InsetWrap::string2params(to_utf8(cmd.argument()), params);
97 params_.lines = params.lines;
98 params_.placement = params.placement;
99 params_.overhang = params.overhang;
100 params_.width = params.width;
104 case LFUN_INSET_DIALOG_UPDATE:
105 cur.bv().updateDialog("wrap", params2string(params()));
109 InsetCaptionable::doDispatch(cur, cmd);
115 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
116 FuncStatus & flag) const
118 switch (cmd.action()) {
119 case LFUN_INSET_MODIFY:
120 case LFUN_INSET_DIALOG_UPDATE:
121 flag.setEnabled(true);
125 return InsetCaptionable::getStatus(cur, cmd, flag);
130 void InsetWrap::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
132 InsetCaptionable::updateBuffer(it, utype, deleted);
136 void InsetWrapParams::write(ostream & os) const
138 os << "Wrap " << type << '\n';
139 os << "lines " << lines << '\n';
140 os << "placement " << placement << '\n';
141 os << "overhang " << overhang.asString() << '\n';
142 os << "width \"" << width.asString() << "\"\n";
146 void InsetWrapParams::read(Lexer & lex)
148 lex.setContext("InsetWrapParams::read");
149 lex >> "lines" >> lines;
150 lex >> "placement" >> placement;
151 lex >> "overhang" >> overhang;
152 lex >> "width" >> width;
156 void InsetWrap::write(ostream & os) const
159 InsetCaptionable::write(os);
163 void InsetWrap::read(Lexer & lex)
166 InsetCaptionable::read(lex);
170 void InsetWrap::validate(LaTeXFeatures & features) const
172 features.require("wrapfig");
173 features.inFloat(true);
174 InsetCaptionable::validate(features);
175 features.inFloat(false);
179 void InsetWrap::latex(otexstream & os, OutputParams const & runparams_in) const
181 OutputParams runparams(runparams_in);
182 runparams.inFloat = OutputParams::MAINFLOAT;
183 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
184 // no optional argument when lines are zero
185 if (params_.lines != 0)
186 os << '[' << params_.lines << ']';
187 os << '{' << from_ascii(params_.placement) << '}';
188 Length over(params_.overhang);
189 // no optional argument when the value is zero
190 if (over.value() != 0)
191 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
192 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
193 InsetText::latex(os, runparams);
194 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
198 int InsetWrap::plaintext(odocstringstream & os,
199 OutputParams const & runparams, size_t max_length) const
201 os << '[' << buffer().B_("wrap") << ' '
202 << floatName(params_.type) << ":\n";
203 InsetText::plaintext(os, runparams, max_length);
206 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
210 void InsetWrap::docbook(XMLStream & xs, OutputParams const & runparams) const
212 InsetLayout const & il = getLayout();
214 xs << xml::StartTag(il.docbooktag(), il.docbookattr()); // TODO: Detect when there is a title.
215 InsetText::docbook(xs, runparams);
216 xs << xml::EndTag(il.docbooktag());
220 docstring InsetWrap::xhtml(XMLStream & xs, OutputParams const & rp) const
222 string const len = params_.width.asHTMLString();
223 string const width = len.empty() ? "50%" : len;
224 InsetLayout const & il = getLayout();
225 string const & tag = il.htmltag();
226 string const attr = il.htmlattr() + " style='width:" + width + ";'";
227 xs << xml::StartTag(tag, attr);
228 docstring const deferred =
229 InsetText::insetAsXHTML(xs, rp, InsetText::WriteInnerTag);
230 xs << xml::EndTag(tag);
235 bool InsetWrap::insetAllowed(InsetCode code) const
243 return InsetCaptionable::insetAllowed(code);
248 bool InsetWrap::showInsetDialog(BufferView * bv) const
250 if (!InsetText::showInsetDialog(bv))
251 bv->showDialog("wrap", params2string(params()),
252 const_cast<InsetWrap *>(this));
257 void InsetWrap::string2params(string const & in, InsetWrapParams & params)
259 params = InsetWrapParams();
260 istringstream data(in);
263 lex.setContext("InsetWrap::string2params");
265 lex >> "Wrap"; // Part of the inset proper, swallowed by Text::readInset
266 lex >> params.type; // We have to read the type here!
271 string InsetWrap::params2string(InsetWrapParams const & params)
274 data << "wrap" << ' ';