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)
53 InsetWrap::~InsetWrap()
55 hideDialogs("wrap", this);
59 // Enforce equality of float type and caption type.
60 void InsetWrap::setCaptionType(std::string const & type)
62 InsetCaptionable::setCaptionType(type);
63 params_.type = captionType();
64 setLabel(_("Wrap: ") + floatName(type));
68 docstring InsetWrap::layoutName() const
70 return "Wrap:" + from_utf8(params_.type);
74 docstring InsetWrap::toolTip(BufferView const & bv, int x, int y) const
77 return InsetCaptionable::toolTip(bv, x, y);
78 OutputParams rp(&buffer().params().encoding());
79 docstring caption_tip = getCaptionText(rp);
80 if (!caption_tip.empty())
81 caption_tip += from_ascii("\n");
82 return toolTipText(caption_tip);
86 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
88 switch (cmd.action()) {
89 case LFUN_INSET_MODIFY: {
90 cur.recordUndoInset(this);
91 InsetWrapParams params;
92 InsetWrap::string2params(to_utf8(cmd.argument()), params);
93 params_.lines = params.lines;
94 params_.placement = params.placement;
95 params_.overhang = params.overhang;
96 params_.width = params.width;
100 case LFUN_INSET_DIALOG_UPDATE:
101 cur.bv().updateDialog("wrap", params2string(params()));
105 InsetCaptionable::doDispatch(cur, cmd);
111 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
112 FuncStatus & flag) const
114 switch (cmd.action()) {
115 case LFUN_INSET_MODIFY:
116 case LFUN_INSET_DIALOG_UPDATE:
117 flag.setEnabled(true);
121 return InsetCaptionable::getStatus(cur, cmd, flag);
126 void InsetWrap::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
128 InsetCaptionable::updateBuffer(it, utype, deleted);
132 void InsetWrapParams::write(ostream & os) const
134 os << "Wrap " << type << '\n';
135 os << "lines " << lines << '\n';
136 os << "placement " << placement << '\n';
137 os << "overhang " << overhang.asString() << '\n';
138 os << "width \"" << width.asString() << "\"\n";
142 void InsetWrapParams::read(Lexer & lex)
144 lex.setContext("InsetWrapParams::read");
145 lex >> "lines" >> lines;
146 lex >> "placement" >> placement;
147 lex >> "overhang" >> overhang;
148 lex >> "width" >> width;
152 void InsetWrap::write(ostream & os) const
155 InsetCaptionable::write(os);
159 void InsetWrap::read(Lexer & lex)
162 InsetCaptionable::read(lex);
166 void InsetWrap::validate(LaTeXFeatures & features) const
168 features.require("wrapfig");
169 features.inFloat(true);
170 InsetCaptionable::validate(features);
171 features.inFloat(false);
175 void InsetWrap::latex(otexstream & os, OutputParams const & runparams_in) const
177 OutputParams runparams(runparams_in);
178 runparams.inFloat = OutputParams::MAINFLOAT;
179 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
180 // no optional argument when lines are zero
181 if (params_.lines != 0)
182 os << '[' << params_.lines << ']';
183 os << '{' << from_ascii(params_.placement) << '}';
184 Length over(params_.overhang);
185 // no optional argument when the value is zero
186 if (over.value() != 0)
187 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
188 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
189 InsetText::latex(os, runparams);
190 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
194 int InsetWrap::plaintext(odocstringstream & os,
195 OutputParams const & runparams, size_t max_length) const
197 os << '[' << buffer().B_("wrap") << ' '
198 << floatName(params_.type) << ":\n";
199 InsetText::plaintext(os, runparams, max_length);
202 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
206 void InsetWrap::docbook(XMLStream & xs, OutputParams const & runparams) const
208 InsetLayout const & il = getLayout();
210 xs << xml::StartTag(il.docbooktag(), il.docbookattr()); // TODO: Detect when there is a title.
211 InsetText::docbook(xs, runparams);
212 xs << xml::EndTag(il.docbooktag());
216 docstring InsetWrap::xhtml(XMLStream & xs, OutputParams const & rp) const
218 string const len = params_.width.asHTMLString();
219 string const width = len.empty() ? "50%" : len;
220 InsetLayout const & il = getLayout();
221 string const & tag = il.htmltag();
222 string const attr = il.htmlattr() + " style='width:" + width + ";'";
223 xs << xml::StartTag(tag, attr);
224 docstring const deferred =
225 InsetText::insetAsXHTML(xs, rp, InsetText::WriteInnerTag);
226 xs << xml::EndTag(tag);
231 bool InsetWrap::insetAllowed(InsetCode code) const
239 return InsetCaptionable::insetAllowed(code);
244 bool InsetWrap::showInsetDialog(BufferView * bv) const
246 if (!InsetText::showInsetDialog(bv))
247 bv->showDialog("wrap", params2string(params()),
248 const_cast<InsetWrap *>(this));
253 void InsetWrap::string2params(string const & in, InsetWrapParams & params)
255 params = InsetWrapParams();
256 istringstream data(in);
259 lex.setContext("InsetWrap::string2params");
261 lex >> "Wrap"; // Part of the inset proper, swallowed by Text::readInset
262 lex >> params.type; // We have to read the type here!
267 string InsetWrap::params2string(InsetWrapParams const & params)
270 data << "wrap" << ' ';