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"
17 #include "BufferParams.h"
18 #include "BufferView.h"
22 #include "DispatchResult.h"
24 #include "FloatList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
28 #include "LaTeXFeatures.h"
30 #include "OutputParams.h"
31 #include "TextClass.h"
32 #include "TocBackend.h"
34 #include "support/convert.h"
35 #include "support/docstream.h"
42 using std::istringstream;
44 using std::ostringstream;
47 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
48 : InsetCollapsable(bp), name_(from_utf8(type))
50 setLabel(_("wrap: ") + floatName(type, bp));
53 params_.placement = "o";
54 params_.overhang = Length(0, Length::PCW);
55 params_.width = Length(50, Length::PCW);
59 InsetWrap::~InsetWrap()
61 InsetWrapMailer(*this).hideDialog();
65 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
68 case LFUN_INSET_MODIFY: {
69 InsetWrapParams params;
70 InsetWrapMailer::string2params(to_utf8(cmd.argument()), params);
71 params_.lines = params.lines;
72 params_.placement = params.placement;
73 params_.overhang = params.overhang;
74 params_.width = params.width;
78 case LFUN_INSET_DIALOG_UPDATE:
79 InsetWrapMailer(*this).updateDialog(&cur.bv());
82 case LFUN_MOUSE_RELEASE: {
83 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
84 InsetWrapMailer(*this).showDialog(&cur.bv());
87 InsetCollapsable::doDispatch(cur, cmd);
92 InsetCollapsable::doDispatch(cur, cmd);
98 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
99 FuncStatus & flag) const
101 switch (cmd.action) {
102 case LFUN_INSET_MODIFY:
103 case LFUN_INSET_DIALOG_UPDATE:
108 return InsetCollapsable::getStatus(cur, cmd, flag);
113 void InsetWrap::updateLabels(Buffer const & buf, ParIterator const & it)
115 Counters & cnts = buf.params().getTextClass().counters();
116 string const saveflt = cnts.current_float();
118 // Tell to captions what the current float is
119 cnts.current_float(params().type);
121 InsetCollapsable::updateLabels(buf, it);
124 cnts.current_float(saveflt);
128 void InsetWrapParams::write(ostream & os) const
130 os << "Wrap " << type << '\n';
131 os << "lines " << lines << "\n";
132 os << "placement " << placement << "\n";
133 os << "overhang " << overhang.asString() << "\n";
134 os << "width \"" << width.asString() << "\"\n";
138 void InsetWrapParams::read(Lexer & lex)
143 if (token == "lines")
146 lyxerr << "InsetWrap::Read:: Missing 'lines'-tag!"
148 // take countermeasures
149 lex.pushToken(token);
154 if (token == "placement")
157 lyxerr << "InsetWrap::Read:: Missing 'placement'-tag!"
159 lex.pushToken(token);
164 if (token == "overhang") {
166 overhang = Length(lex.getString());
168 lyxerr << "InsetWrap::Read:: Missing 'overhang'-tag!"
170 lex.pushToken(token);
175 if (token == "width") {
177 width = Length(lex.getString());
179 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
181 lex.pushToken(token);
186 void InsetWrap::write(Buffer const & buf, ostream & os) const
189 InsetCollapsable::write(buf, os);
193 void InsetWrap::read(Buffer const & buf, Lexer & lex)
196 InsetCollapsable::read(buf, lex);
200 void InsetWrap::validate(LaTeXFeatures & features) const
202 features.require("wrapfig");
203 InsetCollapsable::validate(features);
207 Inset * InsetWrap::clone() const
209 return new InsetWrap(*this);
213 docstring const InsetWrap::editMessage() const
215 return _("Opened Wrap Inset");
219 int InsetWrap::latex(Buffer const & buf, odocstream & os,
220 OutputParams const & runparams) const
222 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
223 // no optional argument when lines are zero
224 if (params_.lines != 0)
225 os << '[' << params_.lines << ']';
226 os << '{' << from_ascii(params_.placement) << '}';
227 Length over(params_.overhang);
228 // no optional argument when the value is zero
229 if (over.value() != 0)
230 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
231 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
232 int const i = InsetText::latex(buf, os, runparams);
233 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
238 int InsetWrap::plaintext(Buffer const & buf, odocstream & os,
239 OutputParams const & runparams) const
241 os << '[' << buf.B_("wrap") << ' ' << floatName(params_.type, buf.params()) << ":\n";
242 InsetText::plaintext(buf, os, runparams);
245 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
249 int InsetWrap::docbook(Buffer const & buf, odocstream & os,
250 OutputParams const & runparams) const
253 os << '<' << from_ascii(params_.type) << '>';
254 int const i = InsetText::docbook(buf, os, runparams);
255 os << "</" << from_ascii(params_.type) << '>';
260 bool InsetWrap::insetAllowed(InsetCode code) const
268 return InsetCollapsable::insetAllowed(code);
273 bool InsetWrap::showInsetDialog(BufferView * bv) const
275 if (!InsetText::showInsetDialog(bv))
276 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
281 string const InsetWrapMailer::name_("wrap");
283 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
288 string const InsetWrapMailer::inset2string(Buffer const &) const
290 return params2string(inset_.params());
294 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
296 params = InsetWrapParams();
300 istringstream data(in);
306 if (!lex || name != name_)
307 return print_mailer_error("InsetWrapMailer", in, 1, name_);
309 // This is part of the inset proper that is usually swallowed
310 // by Text::readInset
313 if (!lex || id != "Wrap")
314 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
316 // We have to read the type here!
322 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
325 data << name_ << ' ';