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"
29 #include "output_xhtml.h"
30 #include "TextClass.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
36 #include "frontends/Application.h"
45 InsetWrap::InsetWrap(Buffer * buf, string const & type)
46 : InsetCaptionable(buf)
50 params_.placement = "o";
51 params_.overhang = Length(0, Length::PCW);
52 params_.width = Length(50, Length::PCW);
56 InsetWrap::~InsetWrap()
58 hideDialogs("wrap", this);
62 // Enforce equality of float type and caption type.
63 void InsetWrap::setCaptionType(std::string const & type)
65 InsetCaptionable::setCaptionType(type);
66 params_.type = captionType();
67 setLabel(_("wrap: ") + floatName(type));
71 docstring InsetWrap::layoutName() const
73 return "Wrap:" + from_utf8(params_.type);
77 docstring InsetWrap::toolTip(BufferView const & bv, int x, int y) const
80 return InsetCaptionable::toolTip(bv, x, y);
81 OutputParams rp(&buffer().params().encoding());
82 docstring caption_tip = getCaptionText(rp);
83 if (!caption_tip.empty())
84 caption_tip += from_ascii("\n");
85 return toolTipText(caption_tip);
89 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
91 switch (cmd.action()) {
92 case LFUN_INSET_MODIFY: {
93 cur.recordUndoInset(this);
94 InsetWrapParams params;
95 InsetWrap::string2params(to_utf8(cmd.argument()), params);
96 params_.lines = params.lines;
97 params_.placement = params.placement;
98 params_.overhang = params.overhang;
99 params_.width = params.width;
103 case LFUN_INSET_DIALOG_UPDATE:
104 cur.bv().updateDialog("wrap", params2string(params()));
108 InsetCaptionable::doDispatch(cur, cmd);
114 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
115 FuncStatus & flag) const
117 switch (cmd.action()) {
118 case LFUN_INSET_MODIFY:
119 case LFUN_INSET_DIALOG_UPDATE:
120 flag.setEnabled(true);
124 return InsetCaptionable::getStatus(cur, cmd, flag);
129 void InsetWrap::updateBuffer(ParIterator const & it, UpdateType utype)
131 InsetCaptionable::updateBuffer(it, utype);
135 void InsetWrapParams::write(ostream & os) const
137 os << "Wrap " << type << '\n';
138 os << "lines " << lines << '\n';
139 os << "placement " << placement << '\n';
140 os << "overhang " << overhang.asString() << '\n';
141 os << "width \"" << width.asString() << "\"\n";
145 void InsetWrapParams::read(Lexer & lex)
147 lex.setContext("InsetWrapParams::read");
148 lex >> "lines" >> lines;
149 lex >> "placement" >> placement;
150 lex >> "overhang" >> overhang;
151 lex >> "width" >> width;
155 void InsetWrap::write(ostream & os) const
158 InsetCaptionable::write(os);
162 void InsetWrap::read(Lexer & lex)
165 InsetCaptionable::read(lex);
169 void InsetWrap::validate(LaTeXFeatures & features) const
171 features.require("wrapfig");
172 features.inFloat(true);
173 InsetCaptionable::validate(features);
174 features.inFloat(false);
178 void InsetWrap::latex(otexstream & os, OutputParams const & runparams_in) const
180 OutputParams runparams(runparams_in);
181 runparams.inFloat = OutputParams::MAINFLOAT;
182 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
183 // no optional argument when lines are zero
184 if (params_.lines != 0)
185 os << '[' << params_.lines << ']';
186 os << '{' << from_ascii(params_.placement) << '}';
187 Length over(params_.overhang);
188 // no optional argument when the value is zero
189 if (over.value() != 0)
190 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
191 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
192 InsetText::latex(os, runparams);
193 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
197 int InsetWrap::plaintext(odocstringstream & os,
198 OutputParams const & runparams, size_t max_length) const
200 os << '[' << buffer().B_("wrap") << ' '
201 << floatName(params_.type) << ":\n";
202 InsetText::plaintext(os, runparams, max_length);
205 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
209 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
212 os << '<' << from_ascii(params_.type) << '>';
213 int const i = InsetText::docbook(os, runparams);
214 os << "</" << from_ascii(params_.type) << '>';
219 docstring InsetWrap::xhtml(XHTMLStream & xs, OutputParams const & rp) const
221 string const len = params_.width.asHTMLString();
222 string const width = len.empty() ? "50%" : len;
223 InsetLayout const & il = getLayout();
224 string const & tag = il.htmltag();
225 string const attr = il.htmlattr() + " style='width:" + width + ";'";
226 xs << html::StartTag(tag, attr);
227 docstring const deferred =
228 InsetText::insetAsXHTML(xs, rp, InsetText::WriteInnerTag);
229 xs << html::EndTag(tag);
234 bool InsetWrap::insetAllowed(InsetCode code) const
242 return InsetCaptionable::insetAllowed(code);
247 bool InsetWrap::showInsetDialog(BufferView * bv) const
249 if (!InsetText::showInsetDialog(bv))
250 bv->showDialog("wrap", params2string(params()),
251 const_cast<InsetWrap *>(this));
256 void InsetWrap::string2params(string const & in, InsetWrapParams & params)
258 params = InsetWrapParams();
259 istringstream data(in);
262 lex.setContext("InsetWrap::string2params");
264 lex >> "Wrap"; // Part of the inset proper, swallowed by Text::readInset
265 lex >> params.type; // We have to read the type here!
270 string InsetWrap::params2string(InsetWrapParams const & params)
273 data << "wrap" << ' ';