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 "texstream.h"
30 #include "TextClass.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/Lexer.h"
37 #include "frontends/Application.h"
48 InsetWrap::InsetWrap(Buffer * buf, string const & type)
49 : InsetCaptionable(buf)
55 InsetWrap::~InsetWrap()
57 hideDialogs("wrap", this);
61 // Enforce equality of float type and caption type.
62 void InsetWrap::setCaptionType(std::string const & type)
64 InsetCaptionable::setCaptionType(type);
65 params_.type = captionType();
66 setLabel(_("Wrap: ") + floatName(type));
70 docstring InsetWrap::layoutName() const
72 return "Wrap:" + from_utf8(params_.type);
76 docstring InsetWrap::toolTip(BufferView const & bv, int x, int y) const
79 return InsetCaptionable::toolTip(bv, x, y);
80 OutputParams rp(&buffer().params().encoding());
81 docstring caption_tip = getCaptionText(rp);
82 if (!caption_tip.empty())
83 caption_tip += from_ascii("\n");
84 return toolTipText(caption_tip);
88 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
90 switch (cmd.action()) {
91 case LFUN_INSET_MODIFY: {
92 cur.recordUndoInset(this);
93 InsetWrapParams params;
94 InsetWrap::string2params(to_utf8(cmd.argument()), params);
95 params_.lines = params.lines;
96 params_.placement = params.placement;
97 params_.overhang = params.overhang;
98 params_.width = params.width;
102 case LFUN_INSET_DIALOG_UPDATE:
103 cur.bv().updateDialog("wrap", params2string(params()));
107 InsetCaptionable::doDispatch(cur, cmd);
113 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
114 FuncStatus & flag) const
116 switch (cmd.action()) {
117 case LFUN_INSET_MODIFY:
118 case LFUN_INSET_DIALOG_UPDATE:
119 flag.setEnabled(true);
123 return InsetCaptionable::getStatus(cur, cmd, flag);
128 void InsetWrap::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
130 InsetCaptionable::updateBuffer(it, utype, deleted);
134 void InsetWrapParams::write(ostream & os) const
136 os << "Wrap " << type << '\n';
137 os << "lines " << lines << '\n';
138 os << "placement " << placement << '\n';
139 os << "overhang " << overhang.asString() << '\n';
140 os << "width \"" << width.asString() << "\"\n";
144 void InsetWrapParams::read(Lexer & lex)
146 lex.setContext("InsetWrapParams::read");
147 lex >> "lines" >> lines;
148 lex >> "placement" >> placement;
149 lex >> "overhang" >> overhang;
150 lex >> "width" >> width;
154 void InsetWrap::write(ostream & os) const
157 InsetCaptionable::write(os);
161 void InsetWrap::read(Lexer & lex)
164 InsetCaptionable::read(lex);
168 void InsetWrap::validate(LaTeXFeatures & features) const
170 features.require("wrapfig");
171 features.inFloat(true);
172 InsetCaptionable::validate(features);
173 features.inFloat(false);
177 void InsetWrap::latex(otexstream & os, OutputParams const & runparams_in) const
179 OutputParams runparams(runparams_in);
180 runparams.inFloat = OutputParams::MAINFLOAT;
181 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
182 // no optional argument when lines are zero
183 if (params_.lines != 0)
184 os << '[' << params_.lines << ']';
185 os << '{' << from_ascii(params_.placement) << '}';
186 Length over(params_.overhang);
187 // no optional argument when the value is zero
188 if (over.value() != 0)
189 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
190 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
191 InsetText::latex(os, runparams);
192 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
196 int InsetWrap::plaintext(odocstringstream & os,
197 OutputParams const & runparams, size_t max_length) const
199 os << '[' << buffer().B_("wrap") << ' '
200 << floatName(params_.type) << ":\n";
201 InsetText::plaintext(os, runparams, max_length);
204 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
208 void InsetWrap::docbook(XMLStream & xs, OutputParams const & runparams) const
210 InsetLayout const & il = getLayout();
212 xs << xml::StartTag(il.docbooktag(), il.docbookattr()); // TODO: Detect when there is a title.
213 InsetText::docbook(xs, runparams);
214 xs << xml::EndTag(il.docbooktag());
218 docstring InsetWrap::xhtml(XMLStream & xs, OutputParams const & rp) const
220 string const len = params_.width.asHTMLString();
221 string const width = len.empty() ? "50%" : len;
222 InsetLayout const & il = getLayout();
223 string const & tag = il.htmltag();
224 string const attr = il.htmlGetAttrString() + " style='width:" + width + ";'";
225 xs << xml::StartTag(tag, attr);
226 docstring const deferred =
227 InsetText::insetAsXHTML(xs, rp, InsetText::WriteInnerTag);
228 xs << xml::EndTag(tag);
233 bool InsetWrap::insetAllowed(InsetCode code) const
241 return InsetCaptionable::insetAllowed(code);
246 bool InsetWrap::showInsetDialog(BufferView * bv) const
248 if (!InsetText::showInsetDialog(bv))
249 bv->showDialog("wrap", params2string(params()),
250 const_cast<InsetWrap *>(this));
255 void InsetWrap::string2params(string const & in, InsetWrapParams & params)
257 params = InsetWrapParams();
258 istringstream data(in);
261 lex.setContext("InsetWrap::string2params");
263 lex >> "Wrap"; // Part of the inset proper, swallowed by Text::readInset
264 lex >> params.type; // We have to read the type here!
269 string InsetWrap::params2string(InsetWrapParams const & params)
272 data << "wrap" << ' ';